/// /// Copyright (c) 2013-2015 Senus Slovensko a.s. /// using System; using System.Windows.Forms; using Config.Entities; using TBF.Resources; namespace TBF.UI.Shared { 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, 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 Custom1 = (1 << 13), /// optional Custom2 = (1 << 14), /// optional Custom3 = (1 << 15), /// optional } readonly System.Windows.Forms.Button[] buttonCtrls; /// /// 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 string Custom1Caption { set { customButton1.Text = value; } } public string Custom2Caption { set { customButton2.Text = value; } } public string Custom3Caption { set { customButton3.Text = value; } } 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; /// /// 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 Form ParentForm; /// Used as a current form reference when changing and restoring a user public Common.GID[] RequiredGroupMembership; /// /// Default constructor /// public SharedButtons() { InitializeComponent(); 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, customButton1, customButton2, customButton3, }; OptionalButtons = Buttons.None; buttonsRepositionsed = false; } /// /// 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; 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; } } public void DisableUnlock() { unlockButton.Enabled = false; } /// /// Update the visibility of buttons and OK button text when the dialog is unlocked /// public void UnlockButtons() { RepositionButtons(OptionalButtons); unlockButton.Enabled = false; okButton.Text = Strings.OkBtnText; cancelButton.Visible = true; ShowOrHideButtons(OptionalButtons, true); lockState = LockState.Unlocked; } /// /// Enable specified buttons /// public void EnableButtons(Buttons selectedButtons) { EnableOrDisableButtons(selectedButtons, true); } /// /// Disable specified buttons /// public void DisableButtons(Buttons selectedButtons) { EnableOrDisableButtons(selectedButtons, false); } /// /// 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 ExportClicked; public event EventHandler ImportClicked; public event EventHandler CompareClicked; public event EventHandler MoreClicked; public event EventHandler Custom1Clicked; public event EventHandler Custom2Clicked; public event EventHandler Custom3Clicked; /// /// 'Unlock' button handler /// /// /// public void unlockBtn_Click(object sender, EventArgs e) { if (lockState == LockState.Locked) { if (!Users.CurrentUser.IsMemberOf(RequiredGroupMembership)) { if ((new Users.Forms.LoginDlg(RequiredGroupMembership, ParentForm)).ShowDialog() != DialogResult.OK) return; } else if (Program.LocalSettings.AlwaysAskPasswdWhenUnlocking) { if ((new Users.Forms.LoginDlg(Users.CurrentUser.UserName(), RequiredGroupMembership, ParentForm)).ShowDialog() != DialogResult.OK) return; } if (Program.MainWnd != null) Program.MainWnd.UpdateUser(); 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 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); } private void customButton1_Click(object s, EventArgs e) { if (Custom1Clicked != null) Custom1Clicked(s, e); } private void customButton2_Click(object s, EventArgs e) { if (Custom2Clicked != null) Custom2Clicked(s, e); } private void customButton3_Click(object s, EventArgs e) { if (Custom3Clicked != null) Custom3Clicked(s, e); } } }