OSDN Git Service

AppliStation, OptionDialog のサイズ感の修正
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / OptionDialog.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 \r
5 namespace AppliStation.Util\r
6 {\r
7         /// <summary>\r
8         /// Description of OptionDialog.\r
9         /// </summary>\r
10         public partial class OptionDialog : Form\r
11         {\r
12                 private object userInputValue = null;\r
13                 \r
14                 public OptionDialog()\r
15                 {\r
16                         //\r
17                         // The InitializeComponent() call is required for Windows Forms designer support.\r
18                         //\r
19                         InitializeComponent();\r
20                 }\r
21                 \r
22                 private void SomeButtonClick(object sender, EventArgs e)\r
23                 {\r
24                         this.Close();\r
25                 }\r
26                 \r
27                 private void HandleUserInput(object sender, EventArgs e)\r
28                 {\r
29                         TextBoxBase textBox = sender as TextBoxBase;\r
30                         ButtonBase button       = sender as ButtonBase;\r
31                         \r
32                         if (textBox != null) {\r
33                                 this.userInputValue = textBox.Text;     \r
34                         }\r
35                         if (button != null) {\r
36                                 this.userInputValue = button.Tag;\r
37                         }\r
38                 }\r
39                 \r
40                 public string MainInstructionText {\r
41                         set {\r
42                                 mainInstLabel.Text = value ?? string.Empty;\r
43                                 mainInstLabel.Visible = ! string.IsNullOrEmpty(value);\r
44                         }\r
45                         get {   return mainInstLabel.Text;      }\r
46                 }\r
47                 public string ContentText {\r
48                         set {\r
49                                 contentLabel.Text = value ?? string.Empty;\r
50                                 contentLabel.Visible = ! string.IsNullOrEmpty(value);\r
51                         }\r
52                         get {   return mainInstLabel.Text;      }\r
53                 }\r
54                 public Image Image {\r
55                         set {\r
56                                 iconPictureBox.Image = value;\r
57                                 if (value != null) {\r
58                                         iconPictureBox.Size = iconPictureBox.Image.Size;\r
59                                 } else {\r
60                                         iconPictureBox.Size = new Size(0, 0);\r
61                                 }\r
62                         }\r
63                         get {   return iconPictureBox.Image;    }\r
64                 }\r
65                 public MessageBoxButtons Buttons {\r
66                         set {\r
67                                 okButton.Enabled         = okButton.Visible             = (value == MessageBoxButtons.OK) || (value == MessageBoxButtons.OKCancel);\r
68                                 cancelButton.Enabled = cancelButton.Visible     = (value == MessageBoxButtons.OKCancel) || (value == MessageBoxButtons.YesNoCancel);\r
69                                 yesButton.Enabled        = yesButton.Visible    = (value == MessageBoxButtons.YesNo) || (value == MessageBoxButtons.YesNoCancel);\r
70                                 noButton.Enabled         = noButton.Visible             = (value == MessageBoxButtons.YesNo) || (value == MessageBoxButtons.YesNoCancel);\r
71                                 \r
72                                 if (okButton.Enabled) {\r
73                                         this.AcceptButton = okButton;\r
74                                 } else if (yesButton.Enabled) {\r
75                                         this.AcceptButton = yesButton;\r
76                                 }\r
77                         }\r
78                         get {\r
79                                 bool ok = okButton.Enabled;\r
80                                 bool cancel = cancelButton.Enabled;\r
81                                 bool yes = yesButton.Enabled;\r
82                                 bool no = noButton.Enabled;\r
83                                 \r
84                                 if (ok && !cancel && !yes && !no) {\r
85                                         return MessageBoxButtons.OK;\r
86                                 } else if (ok && cancel && !yes && !no) {\r
87                                         return MessageBoxButtons.OKCancel;\r
88                                 } else if (!ok && !cancel && yes && no) {\r
89                                         return MessageBoxButtons.YesNo;\r
90                                 } else if (!ok && cancel && yes && no) {\r
91                                         return MessageBoxButtons.YesNoCancel;\r
92                                 } else {\r
93                                         return (MessageBoxButtons) 0xFFFFu;\r
94                                 }\r
95                         }\r
96                 }\r
97                 public Control.ControlCollection Content {\r
98                         get {   return contentFlowLayoutPanel.Controls; }\r
99                 }\r
100                 \r
101                 public object UserInputValue {\r
102                         get { return userInputValue; }\r
103                 }\r
104                 \r
105                 public void BuildRadioButtons(string[] options, int initialOption) {\r
106                         this.Content.Clear();\r
107                         \r
108                         for (int i = 0; i < options.Length; i++) {\r
109                                 RadioButton optRadios = new RadioButton();\r
110                                 optRadios.Text = options[i];\r
111                                 optRadios.Tag = i;\r
112                                 optRadios.Click += HandleUserInput;\r
113                                 optRadios.AutoSize = true;\r
114                                 \r
115                                 if (i == initialOption) {\r
116                                         optRadios.Checked = true;\r
117                                         this.userInputValue = initialOption;\r
118                                 } else {\r
119                                         optRadios.Checked = false;\r
120                                 }\r
121                                 \r
122                                 this.Content.Add(optRadios);\r
123                         }\r
124                 }\r
125                 \r
126                 public void BuildCommandLinkButtons(string[] options, int initialOption)\r
127                 {\r
128                         OperatingSystem os = Environment.OSVersion;\r
129                         \r
130                         this.Content.Clear();\r
131                         \r
132                         int defaultButtonWidth = this.Content.Owner.Width;\r
133                         for (int i = 0; i < options.Length; i++) {\r
134                                 Button button = null;\r
135                                 if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 6) {\r
136                                         button = new CommandLinkButton();\r
137                                         string[] labelData = options[i].Split(new char[]{';'}, 2);\r
138                                         button.Text = labelData[0];\r
139                                         if (labelData.Length > 1 && !string.IsNullOrEmpty(labelData[1])) {\r
140                                                 ((CommandLinkButton) button).Note = labelData[1];\r
141                                                 button.Size = new Size(defaultButtonWidth, 68);\r
142                                         } else {\r
143                                                 button.Size = new Size(defaultButtonWidth, 72);\r
144                                         }\r
145                                         \r
146                                         this.Content.Add(button);\r
147                                 } else {\r
148                                         button = new Button();\r
149                                         string[] labelData = options[i].Split(new char[]{';'}, 2);\r
150                                         button.Text = labelData[0];\r
151                                         button.Font = new Font(button.Font.FontFamily, button.Font.Size * 1.25f, FontStyle.Underline);\r
152                                         button.Size = new Size(defaultButtonWidth, button.Height);\r
153                                         button.TextAlign = ContentAlignment.MiddleLeft;\r
154                                         button.FlatStyle = FlatStyle.Flat;\r
155                                         button.FlatAppearance.BorderSize = 0;\r
156                                         button.FlatAppearance.MouseDownBackColor = SystemColors.ButtonShadow;\r
157                                         button.FlatAppearance.MouseOverBackColor = button.BackColor;\r
158                                         button.ForeColor = SystemColors.HotTrack;\r
159                                         button.Cursor = Cursors.Hand;\r
160                                         \r
161                                         this.Content.Add(button);\r
162                                         \r
163                                         if (labelData.Length > 1 && !string.IsNullOrEmpty(labelData[1])) {\r
164                                                 Label label = new Label();\r
165                                                 label.Text = labelData[1];\r
166                                                 label.Margin = new Padding(10, 0, 0, 5);\r
167                                                 label.AutoSize = true;\r
168                                                 label.UseMnemonic = false;\r
169                                                 this.Content.Add(label);\r
170                                         }\r
171                                 }\r
172                                 \r
173                                 button.Tag = i;\r
174                                 button.Click += HandleUserInput;\r
175                                 button.DialogResult = DialogResult.OK;\r
176                                 \r
177                                 if (i == initialOption) {\r
178                                         button.Focus();\r
179                                         this.userInputValue = initialOption;\r
180                                 }\r
181                         }\r
182                         this.Invalidate();\r
183                 }\r
184                 \r
185                 public static OptionDialog createMessageDialog(string message, string title, string mainInstruction, Icon icon)\r
186                 {\r
187                         return createOptionDialog(message, title, mainInstruction, icon, MessageBoxButtons.OK, null, -1);\r
188                 }\r
189                 \r
190                 public static OptionDialog createConfirmDialog(string message, string title, string mainInstruction, Icon icon, MessageBoxButtons buttons)\r
191                 {\r
192                         return createOptionDialog(message, title, mainInstruction, icon, buttons, null, -1);\r
193                 }\r
194                 \r
195                 public static OptionDialog createOptionDialog(string message, string title, string mainInstruction, Icon icon, MessageBoxButtons buttons, string[] options, int initialOption)\r
196                 {\r
197                         OptionDialog dialog = new OptionDialog();\r
198                         dialog.Text = title;\r
199                         dialog.ContentText = message;\r
200                         dialog.MainInstructionText = mainInstruction;\r
201                         dialog.Image = (icon != null)? icon.ToBitmap() : null;\r
202                         dialog.Buttons = buttons;\r
203                         \r
204                         if (options != null) {\r
205                                 dialog.BuildRadioButtons(options, initialOption);\r
206                         }\r
207                         \r
208                         return dialog;\r
209                 }\r
210                 \r
211                 public static OptionDialog createCommandSelectionDialog(string message, string title, string mainInstruction, Icon icon, string[] options, int initialOption)\r
212                 {\r
213                         OptionDialog dialog = new OptionDialog();\r
214                         dialog.Text = title;\r
215                         dialog.ContentText = message;\r
216                         dialog.MainInstructionText = mainInstruction;\r
217                         dialog.Image = (icon != null)? icon.ToBitmap() : null;\r
218                         dialog.Buttons = MessageBoxButtons.OKCancel;\r
219                         \r
220                         if (options != null) {\r
221                                 dialog.BuildCommandLinkButtons(options, initialOption);\r
222                                 dialog.okButton.Visible = false;\r
223                         }\r
224                         \r
225                         return dialog;\r
226                 }\r
227                 \r
228                 void OptionDialogStyleChanged(object sender, EventArgs e)\r
229                 {\r
230                         mainInstLabel.Font = SystemFonts.CaptionFont;\r
231                         if (System.Windows.Forms.VisualStyles.VisualStyleInformation.IsEnabledByUser) {\r
232                                 mainInstLabel.ForeColor = Color.FromArgb(0x003399);\r
233                                 \r
234                                 this.BackColor = SystemColors.Window;\r
235                                 this.ForeColor = SystemColors.WindowText;\r
236                                 separatorLabel.Visible = true;\r
237                         } else {\r
238                                 mainInstLabel.ForeColor = Color.Empty;\r
239                                 \r
240                                 this.BackColor = Color.Empty;\r
241                                 this.ForeColor = Color.Empty;\r
242                                 separatorLabel.Visible = false;\r
243                         }\r
244                 }\r
245 \r
246                 #region CommandLinkButton\r
247 \r
248                 private class CommandLinkButton : Button\r
249                 {\r
250                         private string note = null;\r
251                         \r
252                         private bool isCommandLink = false;\r
253                         \r
254                         public CommandLinkButton()\r
255                         {\r
256                                 this.FlatStyle = FlatStyle.System;\r
257                         }\r
258                         \r
259                         protected override CreateParams CreateParams {\r
260                                 get {\r
261                                         OperatingSystem os = Environment.OSVersion;\r
262                                         \r
263                                         CreateParams cParams = base.CreateParams;\r
264                                         // Vista未満はなにもしない\r
265                                         if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 6) {\r
266                                                 cParams.Style |= 0x0000000E;    // BS_COMMANDLINK\r
267                                                 this.isCommandLink = true;\r
268                                         } else {\r
269                                                 this.Padding = new Padding(5);\r
270                                         }\r
271                                         return cParams;\r
272                                 }\r
273                         }\r
274                         \r
275                         public string Note {\r
276                                 set {\r
277                                         if (this.isCommandLink) {\r
278                                                 this.note = value;\r
279                                                 //SendMessage(hWnd, BCM_SETNOTE, NULL, (PCWSTR)value);\r
280                                                 NativeMethods.SendMessage(this.Handle, 0x00001609, IntPtr.Zero, System.Runtime.InteropServices.Marshal.StringToHGlobalAuto(value));\r
281                                         }\r
282                                 }\r
283                                 get { return this.note; }\r
284                         }\r
285                         \r
286                 }\r
287                 \r
288                 #endregion\r
289         }\r
290 }\r