tbf/TBF/Rig/Elde/CoverTest/CoverTestForm.cs

114 lines
2.7 KiB
C#
Raw Normal View History

2017-05-04 14:31:40 +00:00
///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Drawing;
using System.Windows.Forms;
using log4net;
2017-05-04 14:31:40 +00:00
using TBF.Resources;
2021-03-05 13:47:21 +00:00
namespace TBF.Rig.Elde.CoverTest
2017-05-04 14:31:40 +00:00
{
public partial class CoverTestForm : Form
2017-05-04 14:31:40 +00:00
{
ILog bypassLog;
string message;
2017-05-04 14:31:40 +00:00
/// Set to 'true' when the form closes
public bool Completed { get { return completed; } }
bool completed;
2019-12-03 19:57:37 +00:00
Users.Entities.GID[] bypassLevel;
2017-05-04 14:31:40 +00:00
2019-12-03 19:57:37 +00:00
public CoverTestForm(string message, Users.Entities.GID[] bypassLevel, ILog bypassLog)
2017-05-04 14:31:40 +00:00
{
InitializeComponent();
ControlBox = false;
this.message = message;
2017-05-04 14:31:40 +00:00
this.bypassLevel = bypassLevel;
this.bypassLog = bypassLog;
2017-05-04 14:31:40 +00:00
Localize();
messageLabel.Text = this.message;
2017-05-04 14:31:40 +00:00
float textWidth = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(messageLabel.Text, messageLabel.Font).Width;
int rqrdFormWidth = (int)textWidth + 80; /// Form width calculated from the text width
if (rqrdFormWidth > Width)
{
int shift = (rqrdFormWidth - Width) / 2;
Width = rqrdFormWidth;
bypassButton.Left += shift;
}
completed = false;
StartForceCloseHandler();
}
void Localize()
{
Text = Strings.Message;
bypassButton.Text = Strings.Bypass;
}
private void bypassButton_Click(object sender, EventArgs e)
{
if (!Users.GlobalData.CurrentUser.IsMemberOf(bypassLevel))
{
if ((new Users.Forms.LoginDlg(bypassLevel)).ShowDialog() != DialogResult.OK)
{
return;
}
else
{
if (Program.MainWnd != null) Program.MainWnd.UpdateUser();
}
}
bypassLog.InfoFormat("{0} bypassed by {1}", message, Users.GlobalData.CurrentUser.UserName);
2017-05-04 14:31:40 +00:00
completed = true;
Close();
}
#region Forced close handling
/// <summary>
/// Used by CoverTest, AskYesNoOp and MessageBoxOp.
/// </summary>
public void OnCloseThisForm(object sender, EventArgs args)
{
if (CloseThisFormHandler == null) return;
try
{
CloseThisFormHandler(sender, null);
}
catch (Exception e)
{
}
}
public event EventHandler<EventArgs> CloseThisFormHandler;
2017-05-04 14:31:40 +00:00
public void StartForceCloseHandler()
{
CloseThisFormHandler += delegate(object sender, EventArgs args)
2017-05-04 14:31:40 +00:00
{
if (InvokeRequired)
Invoke(new EventHandler<EventArgs>(OnForceClose), sender, args);
else
OnForceClose(sender, args);
2017-05-04 14:31:40 +00:00
};
}
private void OnForceClose(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
#endregion
}
}