Merge pull request #3085 from lateralusX/jlorenss/win-x64-pinvoke-empty-struct
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms.WebBrowserDialogs / Generic.cs
1 // Permission is hereby granted, free of charge, to any person obtaining\r
2 // a copy of this software and associated documentation files (the\r
3 // "Software"), to deal in the Software without restriction, including\r
4 // without limitation the rights to use, copy, modify, merge, publish,\r
5 // distribute, sublicense, and/or sell copies of the Software, and to\r
6 // permit persons to whom the Software is furnished to do so, subject to\r
7 // the following conditions:\r
8 // \r
9 // The above copyright notice and this permission notice shall be\r
10 // included in all copies or substantial portions of the Software.\r
11 // \r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
19 //\r
20 // Copyright (c) 2007 Novell, Inc.\r
21 //\r
22 // Authors:\r
23 //      Andreia Gaita   <avidigal@novell.com>\r
24 \r
25 \r
26 using System;\r
27 using System.Collections.Generic;\r
28 using System.Windows.Forms;\r
29 using System.Drawing;\r
30 \r
31 namespace System.Windows.Forms.WebBrowserDialogs\r
32 {\r
33         internal class Generic : Form\r
34         {\r
35                 TableLayoutPanel table;\r
36 \r
37                 public Generic (string title)\r
38                 {\r
39                         this.SuspendLayout ();\r
40                         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;\r
41                         this.AutoSize = true;\r
42                         this.ControlBox = true;\r
43                         this.MinimizeBox = false;\r
44                         this.MaximizeBox = false;\r
45                         this.ShowInTaskbar = (Owner == null);\r
46                         this.FormBorderStyle = FormBorderStyle.FixedDialog;\r
47 \r
48                         table = new TableLayoutPanel ();\r
49                         table.SuspendLayout ();\r
50                         table.AutoSize = true;\r
51                         this.Controls.Add (table);\r
52 \r
53                         this.Text = title;\r
54                 }\r
55 \r
56                 public new DialogResult Show ()\r
57                 {\r
58                         return this.RunDialog ();\r
59                 }\r
60 \r
61                 private void InitSize ()\r
62                 {\r
63                 }\r
64 \r
65                 protected void InitTable (int rows, int cols)\r
66                 {\r
67                         table.ColumnCount = cols;\r
68                         for (int i = 0; i < cols; i++)\r
69                                 table.ColumnStyles.Add (new ColumnStyle ());\r
70                         table.RowCount = rows;\r
71                         for (int i = 0; i < rows; i++)\r
72                                 table.RowStyles.Add (new RowStyle ());\r
73                 }\r
74 \r
75                 protected void AddLabel (int row, int col, int colspan, string text, int width, int height)\r
76                 {\r
77                         Label ctl = new Label ();\r
78                         ctl.Text = text;\r
79                         if (width == -1 && height == -1)\r
80                                 ctl.AutoSize = true;\r
81                         else {\r
82                                 ctl.Width = width;\r
83                                 ctl.Height = height;\r
84                         }\r
85                         table.Controls.Add (ctl, col, row);\r
86                         if (colspan > 1)\r
87                                 table.SetColumnSpan (ctl, colspan);\r
88                 }\r
89 \r
90                 protected void AddButton (int row, int col, int colspan, string text, int width, int height, bool isAccept, bool isCancel, EventHandler onClick)\r
91                 {\r
92                         Button ctl = new Button ();\r
93                         ctl.Text = text;\r
94                         if (width == -1 && height == -1) {\r
95                                 //SizeF s = TextRenderer.MeasureString (text, ctl.Font);\r
96                                 //ctl.Width = (int) ((float) s.Width / 62f);\r
97                                 //ctl.Height = (int)s.Height;\r
98                         } else {\r
99                                 ctl.Width = width;\r
100                                 ctl.Height = height;\r
101                         }\r
102 \r
103                         if (onClick != null)\r
104                                 ctl.Click += onClick;\r
105                         if (isAccept)\r
106                                 AcceptButton = ctl;\r
107                         if (isCancel)\r
108                                 CancelButton = ctl;\r
109                         table.Controls.Add (ctl, col, row);\r
110                         if (colspan > 1)\r
111                                 table.SetColumnSpan (ctl, colspan);\r
112                 }\r
113 \r
114                 protected void AddCheck (int row, int col, int colspan, string text, bool check, int width, int height, EventHandler onCheck)\r
115                 {\r
116                         CheckBox ctl = new CheckBox ();\r
117                         ctl.Text = text;\r
118                         ctl.Checked = check;\r
119 \r
120                         if (width == -1 && height == -1) {\r
121                                 SizeF s = TextRenderer.MeasureString (text, ctl.Font);\r
122                                 ctl.Width += (int) ((float) s.Width / 62f);\r
123                                 if (s.Height > ctl.Height)\r
124                                         ctl.Height = (int) s.Height;\r
125                         } else {\r
126                                 ctl.Width = width;\r
127                                 ctl.Height = height;\r
128                         }\r
129 \r
130                         if (onCheck != null)\r
131                                 ctl.CheckedChanged += onCheck;\r
132 \r
133                         table.Controls.Add (ctl, col, row);\r
134                         if (colspan > 1)\r
135                                 table.SetColumnSpan (ctl, colspan);\r
136                 }\r
137 \r
138                 protected void AddText (int row, int col, int colspan, string text, int width, int height, EventHandler onText)\r
139                 {\r
140                         TextBox ctl = new TextBox ();\r
141                         ctl.Text = text;\r
142 \r
143                         if (width > -1)\r
144                                 ctl.Width = width;\r
145                         if (height > -1)\r
146                                 ctl.Height = height;\r
147 \r
148                         if (onText != null)\r
149                                 ctl.TextChanged += onText;\r
150 \r
151                         table.Controls.Add (ctl, col, row);\r
152                         if (colspan > 1)\r
153                                 table.SetColumnSpan (ctl, colspan);\r
154                 }\r
155 \r
156                 protected void AddPassword (int row, int col, int colspan, string text, int width, int height, EventHandler onText)\r
157                 {\r
158                         TextBox ctl = new TextBox ();\r
159                         ctl.PasswordChar = '*';\r
160                         ctl.Text = text;\r
161 \r
162                         if (width > -1)\r
163                                 ctl.Width = width;\r
164                         if (height > -1)\r
165                                 ctl.Height = height;\r
166 \r
167                         if (onText != null)\r
168                                 ctl.TextChanged += onText;\r
169 \r
170                         table.Controls.Add (ctl, col, row);\r
171                         if (colspan > 1)\r
172                                 table.SetColumnSpan (ctl, colspan);\r
173                 }\r
174 \r
175                 protected DialogResult RunDialog ()\r
176                 {\r
177                         this.StartPosition = FormStartPosition.CenterScreen;\r
178 \r
179                         InitSize ();\r
180 \r
181                         table.ResumeLayout (false);\r
182                         table.PerformLayout ();\r
183                         this.ResumeLayout (false);\r
184                         this.PerformLayout ();\r
185 \r
186                         this.ShowDialog ();\r
187 \r
188                         return this.DialogResult;\r
189                 }\r
190         }\r
191 }\r