OSDN Git Service

AppliStation-GUI,汎用ダイアログ用クラスを追加
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / OptionDialog.cs
diff --git a/AppliStation/AppliStation.Util/OptionDialog.cs b/AppliStation/AppliStation.Util/OptionDialog.cs
new file mode 100644 (file)
index 0000000..c976b2e
--- /dev/null
@@ -0,0 +1,146 @@
+using System;\r
+using System.Drawing;\r
+using System.Windows.Forms;\r
+\r
+namespace AppliStation.Util\r
+{\r
+       /// <summary>\r
+       /// Description of OptionDialog.\r
+       /// </summary>\r
+       public partial class OptionDialog : Form\r
+       {\r
+               private object userInputValue = null;\r
+               \r
+               public OptionDialog()\r
+               {\r
+                       //\r
+                       // The InitializeComponent() call is required for Windows Forms designer support.\r
+                       //\r
+                       InitializeComponent();\r
+                       \r
+                       mainInstLabel.Font = SystemFonts.CaptionFont;\r
+               }\r
+               \r
+               private void SomeButtonClick(object sender, EventArgs e)\r
+               {\r
+                       this.Close();\r
+               }\r
+               \r
+               private void HandleUserInput(object sender, EventArgs e)\r
+               {\r
+                       TextBoxBase textBox = sender as TextBoxBase;\r
+                       RadioButton radioButton = sender as RadioButton;\r
+                       \r
+                       if (textBox != null) {\r
+                               this.userInputValue = textBox.Text;     \r
+                       }\r
+                       if (radioButton != null) {\r
+                               this.userInputValue = radioButton.Tag;  \r
+                       }\r
+               }\r
+               \r
+               public string MainInstructionText {\r
+                       set {\r
+                               mainInstLabel.Text = value ?? string.Empty;\r
+                               mainInstLabel.Visible = ! string.IsNullOrEmpty(value);\r
+                       }\r
+                       get {   return mainInstLabel.Text;      }\r
+               }\r
+               public string ContentText {\r
+                       set {\r
+                               contentLabel.Text = value ?? string.Empty;\r
+                               contentLabel.Visible = ! string.IsNullOrEmpty(value);\r
+                       }\r
+                       get {   return mainInstLabel.Text;      }\r
+               }\r
+               public Image Image {\r
+                       set {   iconPictureBox.Image = value;   }\r
+                       get {   return iconPictureBox.Image;    }\r
+               }\r
+               public MessageBoxButtons Buttons {\r
+                       set {\r
+                               okButton.Enabled         = okButton.Visible             = (value == MessageBoxButtons.OK) || (value == MessageBoxButtons.OKCancel);\r
+                               cancelButton.Enabled = cancelButton.Visible     = (value == MessageBoxButtons.OKCancel) || (value == MessageBoxButtons.YesNoCancel);\r
+                               yesButton.Enabled        = yesButton.Visible    = (value == MessageBoxButtons.YesNo) || (value == MessageBoxButtons.YesNoCancel);\r
+                               noButton.Enabled         = noButton.Visible             = (value == MessageBoxButtons.YesNo) || (value == MessageBoxButtons.YesNoCancel);\r
+                               \r
+                               if (okButton.Enabled) {\r
+                                       this.AcceptButton = okButton;\r
+                               } else if (yesButton.Enabled) {\r
+                                       this.AcceptButton = yesButton;\r
+                               }\r
+                       }\r
+                       get {\r
+                               bool ok = okButton.Enabled;\r
+                               bool cancel = cancelButton.Enabled;\r
+                               bool yes = yesButton.Enabled;\r
+                               bool no = noButton.Enabled;\r
+                               \r
+                               if (ok && !cancel && !yes && !no) {\r
+                                       return MessageBoxButtons.OK;\r
+                               } else if (ok && cancel && !yes && !no) {\r
+                                       return MessageBoxButtons.OKCancel;\r
+                               } else if (!ok && !cancel && yes && no) {\r
+                                       return MessageBoxButtons.YesNo;\r
+                               } else if (!ok && cancel && yes && no) {\r
+                                       return MessageBoxButtons.YesNoCancel;\r
+                               } else {\r
+                                       return (MessageBoxButtons) 0xFFFFu;\r
+                               }\r
+                       }\r
+               }\r
+               public Control.ControlCollection Content {\r
+                       get {   return contentFlowLayoutPanel.Controls; }\r
+               }\r
+               \r
+               public object UserInputValue {\r
+                       get { return userInputValue; }\r
+               }\r
+               \r
+               public void BuildRadioButtons(string[] options, int initialOption) {\r
+                       this.Content.Clear();\r
+                       \r
+                       for (int i = 0; i < options.Length; i++) {\r
+                               RadioButton optRadios = new RadioButton();\r
+                               optRadios.Text = options[i];\r
+                               optRadios.Tag = i;\r
+                               optRadios.Click += HandleUserInput;\r
+                               \r
+                               if (i == initialOption) {\r
+                                       optRadios.Checked = true;\r
+                                       this.userInputValue = initialOption;\r
+                               } else {\r
+                                       optRadios.Checked = false;\r
+                               }\r
+                               \r
+                               this.Content.Add(optRadios);\r
+                       }\r
+               }\r
+               \r
+               public static OptionDialog createMessageDialog(string message, string title, string mainInstruction, Icon icon)\r
+               {\r
+                       return createOptionDialog(message, title, mainInstruction, icon, MessageBoxButtons.OK, null, -1);\r
+               }\r
+               \r
+               public static OptionDialog createConfirmDialog(string message, string title, string mainInstruction, Icon icon, MessageBoxButtons buttons)\r
+               {\r
+                       return createOptionDialog(message, title, mainInstruction, icon, buttons, null, -1);\r
+               }\r
+               \r
+               public static OptionDialog createOptionDialog(string message, string title, string mainInstruction, Icon icon, MessageBoxButtons buttons, string[] options, int initialOption)\r
+               {\r
+                       OptionDialog dialog = new OptionDialog();\r
+                       dialog.Text = title;\r
+                       dialog.ContentText = message;\r
+                       dialog.MainInstructionText = mainInstruction;\r
+                       dialog.Image = (icon != null)? icon.ToBitmap() : null;\r
+                       dialog.Buttons = buttons;\r
+                       \r
+                       if (options != null) {\r
+                               dialog.BuildRadioButtons(options, initialOption);\r
+                       }\r
+                       \r
+                       return dialog;\r
+               }\r
+       }\r
+}\r