tbf/TestBenchFramework/UiControls/SharedButtons.cs
Milan Hanajik 0160dc965d - RegulValve ADC Open/Close values calibration implemented
- 'More' button added to ComponentCfgCtrl-s
- Dataflow from ComponentParametersDlg to components: CfgChangeHandler
- Dataflow from components to ComponentParametersDlg: CmdResponseHandler
- Detection of parameter changes and appropriate MesssageBox display:
  1.warning message when component configuration change requires program restart
  2.confirmation message box when any configuration changes would be lost
2015-01-03 14:45:58 +01:00

289 lines
10 KiB
C#

using System;
using System.Windows.Forms;
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,
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
}
/// <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;
public bool MoreActive;
/// <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 Grp.GID RequiredGroupMembership;
Entities.User originalUser;
/// <summary>
/// Default constructor
/// </summary>
public SharedButtons()
{
originalUser = Entities.User.CurrentUser;
lockState = LockState.Locked;
MoreActive = false;
OptionalButtons = Buttons.None;
RequiredGroupMembership = Grp.GID.None;
InitializeComponent();
}
/// <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;
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 = addButton.Top + 70;
}
#if DEBUG
//unlockBtn_Click(sender, e);
#endif
}
/// <summary>
/// Update the visibility of buttons and OK button text when the dialog is unlocked
/// </summary>
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;
}
/// <summary>
/// Enable specified buttons
/// </summary>
public void EnableButtons(Buttons buttons)
{
SetEnabled(buttons, true);
}
/// <summary>
/// Disable specified buttons
/// </summary>
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()
{
Entities.User.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;
/// <summary>
/// 'Unlock' button handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void unlockBtn_Click(object sender, EventArgs e)
{
if (lockState == LockState.Locked)
{
if (!Entities.User.CurrentUser.IsMemberOf(RequiredGroupMembership))
{
if ((new LoginDlg(RequiredGroupMembership)).ShowDialog() != DialogResult.OK) return;
}
else if (Program.LocalSettings.AlwaysAskPasswdWhenUnlocking)
{
if ((new LoginDlg(Entities.User.CurrentUser.Name, RequiredGroupMembership)).ShowDialog() != DialogResult.OK) return;
}
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 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); }
}
}