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