Generic.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <[email protected]>
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Windows.Forms;
  27. using System.Drawing;
  28. namespace System.Windows.Forms.WebBrowserDialogs
  29. {
  30. internal class Generic : Form
  31. {
  32. TableLayoutPanel table;
  33. public Generic (string title)
  34. {
  35. this.SuspendLayout ();
  36. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  37. this.AutoSize = true;
  38. this.ControlBox = true;
  39. this.MinimizeBox = false;
  40. this.MaximizeBox = false;
  41. this.ShowInTaskbar = (Owner == null);
  42. this.FormBorderStyle = FormBorderStyle.FixedDialog;
  43. table = new TableLayoutPanel ();
  44. table.SuspendLayout ();
  45. table.AutoSize = true;
  46. this.Controls.Add (table);
  47. this.Text = title;
  48. }
  49. public new DialogResult Show ()
  50. {
  51. return this.RunDialog ();
  52. }
  53. private void InitSize ()
  54. {
  55. }
  56. protected void InitTable (int rows, int cols)
  57. {
  58. table.ColumnCount = cols;
  59. for (int i = 0; i < cols; i++)
  60. table.ColumnStyles.Add (new ColumnStyle ());
  61. table.RowCount = rows;
  62. for (int i = 0; i < rows; i++)
  63. table.RowStyles.Add (new RowStyle ());
  64. }
  65. protected void AddLabel (int row, int col, int colspan, string text, int width, int height)
  66. {
  67. Label ctl = new Label ();
  68. ctl.Text = text;
  69. if (width == -1 && height == -1)
  70. ctl.AutoSize = true;
  71. else {
  72. ctl.Width = width;
  73. ctl.Height = height;
  74. }
  75. table.Controls.Add (ctl, col, row);
  76. if (colspan > 1)
  77. table.SetColumnSpan (ctl, colspan);
  78. }
  79. protected void AddButton (int row, int col, int colspan, string text, int width, int height, bool isAccept, bool isCancel, EventHandler onClick)
  80. {
  81. Button ctl = new Button ();
  82. ctl.Text = text;
  83. if (width == -1 && height == -1) {
  84. //SizeF s = TextRenderer.MeasureString (text, ctl.Font);
  85. //ctl.Width = (int) ((float) s.Width / 62f);
  86. //ctl.Height = (int)s.Height;
  87. } else {
  88. ctl.Width = width;
  89. ctl.Height = height;
  90. }
  91. if (onClick != null)
  92. ctl.Click += onClick;
  93. if (isAccept)
  94. AcceptButton = ctl;
  95. if (isCancel)
  96. CancelButton = ctl;
  97. table.Controls.Add (ctl, col, row);
  98. if (colspan > 1)
  99. table.SetColumnSpan (ctl, colspan);
  100. }
  101. protected void AddCheck (int row, int col, int colspan, string text, bool check, int width, int height, EventHandler onCheck)
  102. {
  103. CheckBox ctl = new CheckBox ();
  104. ctl.Text = text;
  105. ctl.Checked = check;
  106. if (width == -1 && height == -1) {
  107. SizeF s = TextRenderer.MeasureString (text, ctl.Font);
  108. ctl.Width += (int) ((float) s.Width / 62f);
  109. if (s.Height > ctl.Height)
  110. ctl.Height = (int) s.Height;
  111. } else {
  112. ctl.Width = width;
  113. ctl.Height = height;
  114. }
  115. if (onCheck != null)
  116. ctl.CheckedChanged += onCheck;
  117. table.Controls.Add (ctl, col, row);
  118. if (colspan > 1)
  119. table.SetColumnSpan (ctl, colspan);
  120. }
  121. protected void AddText (int row, int col, int colspan, string text, int width, int height, EventHandler onText)
  122. {
  123. TextBox ctl = new TextBox ();
  124. ctl.Text = text;
  125. if (width > -1)
  126. ctl.Width = width;
  127. if (height > -1)
  128. ctl.Height = height;
  129. if (onText != null)
  130. ctl.TextChanged += onText;
  131. table.Controls.Add (ctl, col, row);
  132. if (colspan > 1)
  133. table.SetColumnSpan (ctl, colspan);
  134. }
  135. protected void AddPassword (int row, int col, int colspan, string text, int width, int height, EventHandler onText)
  136. {
  137. TextBox ctl = new TextBox ();
  138. ctl.PasswordChar = '*';
  139. ctl.Text = text;
  140. if (width > -1)
  141. ctl.Width = width;
  142. if (height > -1)
  143. ctl.Height = height;
  144. if (onText != null)
  145. ctl.TextChanged += onText;
  146. table.Controls.Add (ctl, col, row);
  147. if (colspan > 1)
  148. table.SetColumnSpan (ctl, colspan);
  149. }
  150. protected DialogResult RunDialog ()
  151. {
  152. this.StartPosition = FormStartPosition.CenterScreen;
  153. InitSize ();
  154. table.ResumeLayout (false);
  155. table.PerformLayout ();
  156. this.ResumeLayout (false);
  157. this.PerformLayout ();
  158. this.ShowDialog ();
  159. return this.DialogResult;
  160. }
  161. }
  162. }