/// /// 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 { /// /// 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. /// public enum Buttons { None = 0, OK = 1, /// always visible, text = 'Close' when locked Cancel = 2, /// always visible Add = 4, /// optional Remove = 8, /// optional Up = 16, /// optional Down = 32, /// optional Edit = 64, /// optional Copy = 128, /// optional NewTab = 256, /// optional RenameTab = 512, /// optional RemoveTab = 1024, /// optional More = 2048, /// optional } /// /// Optional buttons are Add, Remove, Up, Down, Edit and Copy. /// OK and Cancel buttons are always visible (after unlock), but not always enabled. /// 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; public bool MoreActive; public int MoreButtonTop; /// /// Used by controls to indicate which item is selected /// and to appropriately update Remove, Up and Down Buttons. /// public enum SelectedItemPos { None, First, Last, FirstAndLast, Middle, } public Config.Grp.GID RequiredGroupMembership; Config.IUser originalUser; /// /// Default constructor /// public SharedButtons() { originalUser = Config.Data.CurrentUser; lockState = LockState.Locked; MoreActive = false; OptionalButtons = Buttons.None; RequiredGroupMembership = Config.Grp.GID.None; InitializeComponent(); } /// /// Initialize the buttons when the dialog is loaded /// /// /// 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; newTabButton.Text = Strings.NewTabBtnText; renameTabButton.Text = Strings.RenameTabBtnText; removeTabButton.Text = Strings.RemoveTabBtnText; moreButton.Text = Strings.MoreBtnText; cancelButton.Hide(); addButton.Hide(); removeButton.Hide(); upButton.Hide(); downButton.Hide(); editButton.Hide(); copyButton.Hide(); newTabButton.Hide(); renameTabButton.Hide(); removeTabButton.Hide(); moreButton.Hide(); /// Reposition 'Edit' and 'Copy" buttons in case both 'Up' and 'Down' are hidden if (((OptionalButtons & Buttons.Up) == 0) && ((OptionalButtons & Buttons.Down) == 0)) { editButton.Top = upButton.Top; copyButton.Top = downButton.Top; } /// Reposition Tab-related button in case both 'Edit' and 'Copy" are hidden if (((OptionalButtons & Buttons.Edit) == 0) && ((OptionalButtons & Buttons.Copy) == 0)) { removeTabButton.Top = newTabButton.Top + 15; newTabButton.Top = editButton.Top + 15; renameTabButton.Top = copyButton.Top + 15; } if ((OptionalButtons & Buttons.Add) == 0 && (OptionalButtons & Buttons.Remove) == 0 && (OptionalButtons & Buttons.Up) == 0 && (OptionalButtons & Buttons.Down) == 0 && (OptionalButtons & Buttons.Edit) == 0 && (OptionalButtons & Buttons.Copy) == 0 && (OptionalButtons & Buttons.NewTab) == 0 && (OptionalButtons & Buttons.RenameTab) == 0 && (OptionalButtons & Buttons.RemoveTab) == 0) { moreButton.Top = MoreButtonTop; } #if DEBUG //unlockBtn_Click(sender, e); #endif } /// /// Update the visibility of buttons and OK button text when the dialog is unlocked /// public void UnlockButtons() { unlockButton.Enabled = false; okButton.Text = Strings.OkBtnText; cancelButton.Show(); if ((OptionalButtons & Buttons.Add) != 0) addButton.Show(); if ((OptionalButtons & Buttons.Remove) != 0) removeButton.Show(); if ((OptionalButtons & Buttons.Up) != 0) upButton.Show(); if ((OptionalButtons & Buttons.Down) != 0) downButton.Show(); if ((OptionalButtons & Buttons.Edit) != 0) editButton.Show(); if ((OptionalButtons & Buttons.Copy) != 0) copyButton.Show(); if ((OptionalButtons & Buttons.NewTab) != 0) newTabButton.Show(); if ((OptionalButtons & Buttons.RenameTab) != 0) renameTabButton.Show(); if ((OptionalButtons & Buttons.RemoveTab) != 0) removeTabButton.Show(); if ((OptionalButtons & Buttons.More) != 0) moreButton.Show(); lockState = LockState.Unlocked; } /// /// Enable specified buttons /// public void EnableButtons(Buttons buttons) { SetEnabled(buttons, true); } /// /// Disable specified buttons /// public void DisableButtons(Buttons buttons) { SetEnabled(buttons, false); } public void SetEnabled(Buttons buttons, bool value) { if ((buttons & Buttons.OK) != 0) okButton.Enabled = value; if ((buttons & Buttons.Cancel) != 0) cancelButton.Enabled = value; if ((buttons & Buttons.Add) != 0) addButton.Enabled = value; if ((buttons & Buttons.Remove) != 0) removeButton.Enabled = value; if ((buttons & Buttons.Up) != 0) upButton.Enabled = value; if ((buttons & Buttons.Down) != 0) downButton.Enabled = value; if ((buttons & Buttons.Edit) != 0) editButton.Enabled = value; if ((buttons & Buttons.Copy) != 0) copyButton.Enabled = value; if ((buttons & Buttons.NewTab) != 0) newTabButton.Enabled = value; if ((buttons & Buttons.RenameTab) != 0) renameTabButton.Enabled = value; if ((buttons & Buttons.RemoveTab) != 0) removeTabButton.Enabled = value; if ((buttons & Buttons.More) != 0) moreButton.Enabled = value; } void RestoreUser() { Config.Data.CurrentUser = originalUser; Program.MainWnd.UpdateUser(); } /// /// Events invoked when buttons are clicked (and in case of Unlock kbutton also accepted) /// 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; /// /// 'Unlock' button handler /// /// /// public void unlockBtn_Click(object sender, EventArgs e) { if (lockState == LockState.Locked) { if (!Config.Data.CurrentUser.IsMemberOf(RequiredGroupMembership)) { if ((new Forms.LoginDlg(RequiredGroupMembership)).ShowDialog() != DialogResult.OK) return; } else if (Program.LocalSettings.AlwaysAskPasswdWhenUnlocking) { if ((new Forms.LoginDlg(Config.Data.CurrentUser.UserName, RequiredGroupMembership)).ShowDialog() != DialogResult.OK) return; } UnlockButtons(); if (Unlocked != null) Unlocked(sender, e); } } /// /// 'OK'/'Close' button handler. Handled as 'Cancel' in the locked state ('Close' button). /// 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 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); } } }