tbf/TBF/UiControls/SharedButtons.cs

293 lines
10 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Windows.Forms;
using Config.Entities;
using TBF.Resources;
namespace TBF.UiControls
{
public partial class SharedButtons : UserControl
{
/// <summary>
/// Masks to specify the enabled state and the visibility of buttons.
/// Unlock button is hard coded in this control, cannot be set from the parent.
/// </summary>
public enum Buttons
{
None = 0,
Add = (1 << 0), /// optional
Remove = (1 << 1), /// optional
Up = (1 << 2), /// optional
Down = (1 << 3), /// optional
Edit = (1 << 4), /// optional
Copy = (1 << 5), /// optional
Export = (1 << 6), /// optional
Import = (1 << 7), /// optional
Compare = (1 << 8), /// optional
NewTab = (1 << 9), /// optional
RenameTab = (1 << 10), /// optional
RemoveTab = (1 << 11), /// optional
More = (1 << 12), /// optional
}
readonly System.Windows.Forms.Button[] buttonCtrls;
/// <summary>
/// Optional buttons are Add, Remove, Up, Down, Edit and Copy.
/// OK and Cancel buttons are always visible (after unlock), but not always enabled.
/// </summary>
public Buttons OptionalButtons;
public enum LockState
{
Locked, /// 'Unlock', 'Close' (=OK) are shown, all the othere are hidden
Unlocked, /// Unlock, Ok, Cancel, Add,Remove and all optional buttons are shown
}
LockState lockState;
bool buttonsRepositionsed;
public bool MoreActive;
public int MoreButtonTop;
/// <summary>
/// Used by controls to indicate which item is selected
/// and to appropriately update Remove, Up and Down Buttons.
/// </summary>
public enum SelectedItemPos
{
None,
First,
Last,
FirstAndLast,
Middle,
}
public Users.Grp.GID[] RequiredGroupMembership;
Users.Entities.User originalUser;
/// <summary>
/// Default constructor
/// </summary>
public SharedButtons()
{
InitializeComponent();
originalUser = Users.GlobalData.CurrentUser;
lockState = LockState.Locked;
MoreActive = false;
RequiredGroupMembership = null;
buttonCtrls = new System.Windows.Forms.Button[]
{
addButton, removeButton, upButton, downButton, editButton, copyButton,
exportButton, importButton, compareButton,
newTabButton, renameTabButton, removeTabButton, moreButton,
};
OptionalButtons = Buttons.None;
buttonsRepositionsed = false;
}
/// <summary>
/// Initialize the buttons when the dialog is loaded
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SharedDlgButtons_Load(object sender, EventArgs e)
{
unlockButton.Text = Strings.UnlockBtnText;
okButton.Text = Strings.CloseBtnText;
cancelButton.Text = Strings.CancelBtnText;
addButton.Text = Strings.AddBtnText;
removeButton.Text = Strings.RemoveBtnText;
upButton.Text = Strings.UpBtnText;
downButton.Text = Strings.DownBtnText;
editButton.Text = Strings.EditBtnText;
copyButton.Text = Strings.CopyBtnText;
exportButton.Text = Strings.Export;
importButton.Text = Strings.Import;
compareButton.Text = Strings.Compare;
newTabButton.Text = Strings.NewTabBtnText;
renameTabButton.Text = Strings.RenameTabBtnText;
removeTabButton.Text = Strings.RemoveTabBtnText;
moreButton.Text = Strings.MoreBtnText;
cancelButton.Hide();
ShowOrHideButtons((Buttons)0x7FFFFFFF, false);
}
void RepositionButtons(Buttons enabledButtons)
{
if (!buttonsRepositionsed)
{
buttonsRepositionsed = true; /// Reposition buttons once
int spacing = unlockButton.Height + 3;
const int ExtraSpacing = 15;
int nextTop = cancelButton.Top + spacing + ExtraSpacing;
for (int i = 0; i < buttonCtrls.Length; i++)
{
Buttons btn = (Buttons)(1 << i);
if ((OptionalButtons & btn) != 0)
{
buttonCtrls[i].Top = nextTop;
nextTop += spacing + ((btn == Buttons.Down || btn == Buttons.Compare || btn == Buttons.RemoveTab) ? ExtraSpacing : 0);
}
}
}
}
void ShowOrHideButtons(Buttons selectedButtons, bool value)
{
for (int i = 0; i < buttonCtrls.Length; i++)
{
if ((selectedButtons & (Buttons)(1 << i)) != 0) buttonCtrls[i].Visible = value;
}
}
public void EnableOrDisableButtons(Buttons selectedButtons, bool value)
{
for (int i = 0; i < buttonCtrls.Length; i++)
{
if ((selectedButtons & (Buttons)(1 << i)) != 0) buttonCtrls[i].Enabled = value;
}
}
/// <summary>
/// Update the visibility of buttons and OK button text when the dialog is unlocked
/// </summary>
public void UnlockButtons()
{
RepositionButtons(OptionalButtons);
unlockButton.Enabled = false;
okButton.Text = Strings.OkBtnText;
cancelButton.Visible = true;
ShowOrHideButtons(OptionalButtons, true);
lockState = LockState.Unlocked;
}
/// <summary>
/// Enable specified buttons
/// </summary>
public void EnableButtons(Buttons selectedButtons)
{
EnableOrDisableButtons(selectedButtons, true);
}
/// <summary>
/// Disable specified buttons
/// </summary>
public void DisableButtons(Buttons selectedButtons)
{
EnableOrDisableButtons(selectedButtons, false);
}
void RestoreUser()
{
Users.GlobalData.CurrentUser = originalUser;
Program.MainWnd.UpdateUser();
}
/// <summary>
/// Events invoked when buttons are clicked (and in case of Unlock kbutton also accepted)
/// </summary>
public event EventHandler Unlocked;
public event EventHandler OKClicked;
public event EventHandler CancelClicked;
public event EventHandler AddClicked;
public event EventHandler RemoveClicked;
public event EventHandler UpClicked;
public event EventHandler DownClicked;
public event EventHandler EditClicked;
public event EventHandler CopyClicked;
public event EventHandler NewTabClicked;
public event EventHandler RenameTabClicked;
public event EventHandler RemoveTabClicked;
public event EventHandler MoreClicked;
public event EventHandler ExportClicked;
public event EventHandler ImportClicked;
public event EventHandler CompareClicked;
/// <summary>
/// 'Unlock' button handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void unlockBtn_Click(object sender, EventArgs e)
{
if (lockState == LockState.Locked)
{
if (!Users.GlobalData.CurrentUser.IsMemberOf(RequiredGroupMembership))
{
if ((new Users.Forms.LoginDlg(RequiredGroupMembership)).ShowDialog() != DialogResult.OK)
return;
}
else if (Program.LocalSettings.AlwaysAskPasswdWhenUnlocking)
{
if ((new Users.Forms.LoginDlg(Users.GlobalData.CurrentUser.UserName, RequiredGroupMembership)).ShowDialog() != DialogResult.OK)
return;
}
if (Program.MainWnd != null) Program.MainWnd.UpdateUser();
UnlockButtons();
if (Unlocked != null) Unlocked(sender, e);
}
}
/// <summary>
/// 'OK'/'Close' button handler. Handled as 'Cancel' in the locked state ('Close' button).
/// </summary>
private void okBtn_Click(object sender, EventArgs e)
{
if (lockState == LockState.Locked)
{
if (CancelClicked != null) CancelClicked(sender, e);
}
else
{
if (OKClicked != null) OKClicked(sender, e);
}
}
private void moreButton_Click(object sender, EventArgs e)
{
if (MoreActive)
{
MoreActive = false;
moreButton.Text = Strings.MoreBtnText;
}
else
{
MoreActive = true;
moreButton.Text = Strings.LessBtnText;
}
if (MoreClicked != null) MoreClicked(sender, e);
}
///
/// All remaining handlers:
///
private void cancelBtn_Click(object s, EventArgs e) { if (CancelClicked != null) CancelClicked(s, e); }
private void addBtn_Click(object s, EventArgs e) { if (AddClicked != null) AddClicked(s, e); }
private void removeBtn_Click(object s, EventArgs e) { if (RemoveClicked != null) RemoveClicked(s, e); }
private void upBtn_Click(object s, EventArgs e) { if (UpClicked != null) UpClicked(s, e); }
private void downBtn_Click(object s, EventArgs e) { if (DownClicked != null) DownClicked(s, e); }
private void editBtn_Click(object s, EventArgs e) { if (EditClicked != null) EditClicked(s, e); }
private void copyBtn_Click(object s, EventArgs e) { if (CopyClicked != null) CopyClicked(s, e); }
private void exportButton_Click(object s, EventArgs e) { if (ExportClicked != null) ExportClicked(s, e); }
private void importButton_Click(object s, EventArgs e) { if (ImportClicked != null) ImportClicked(s, e); }
private void compareButton_Click(object s, EventArgs e) { if (CompareClicked != null) CompareClicked(s, e); }
private void newTabButton_Click(object s, EventArgs e) { if (NewTabClicked != null) NewTabClicked(s, e); }
private void renameTabButton_Click(object s, EventArgs e) { if (RenameTabClicked != null) RenameTabClicked(s, e); }
private void removeTabButton_Click(object s, EventArgs e) { if (RemoveTabClicked != null) RemoveTabClicked(s, e); }
}
}