TableLayoutPanel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // TableLayoutPanel.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.ComponentModel;
  31. using System.Runtime.InteropServices;
  32. using System.Windows.Forms.Layout;
  33. namespace System.Windows.Forms
  34. {
  35. [ComVisible (true)]
  36. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  37. [ProvideProperty ("CellPosition", typeof (Control))]
  38. [ProvideProperty ("Column", typeof (Control))]
  39. [ProvideProperty ("ColumnSpan", typeof (Control))]
  40. [ProvideProperty ("Row", typeof (Control))]
  41. [ProvideProperty ("RowSpan", typeof (Control))]
  42. [DefaultProperty ("ColumnCount")]
  43. [Docking (DockingBehavior.Never)]
  44. public class TableLayoutPanel : Panel, IExtenderProvider
  45. {
  46. private TableLayoutSettings settings;
  47. private static TableLayout layout_engine = new TableLayout ();
  48. private TableLayoutPanelCellBorderStyle cell_border_style;
  49. // This is the row/column the Control actually got placed
  50. internal Control[,] actual_positions;
  51. // Widths and heights of each column/row
  52. internal int[] column_widths;
  53. internal int[] row_heights;
  54. #region Public Constructor
  55. public TableLayoutPanel ()
  56. {
  57. settings = new TableLayoutSettings(this);
  58. cell_border_style = TableLayoutPanelCellBorderStyle.None;
  59. }
  60. #endregion
  61. #region Public Properties
  62. [Localizable (true)]
  63. [Browsable (false)]
  64. [EditorBrowsable (EditorBrowsableState.Never)]
  65. new public BorderStyle BorderStyle {
  66. get { return base.BorderStyle; }
  67. set { base.BorderStyle = value; }
  68. }
  69. [Localizable (true)]
  70. [DefaultValue (TableLayoutPanelCellBorderStyle.None)]
  71. public TableLayoutPanelCellBorderStyle CellBorderStyle {
  72. get { return this.cell_border_style; }
  73. set { this.cell_border_style = value; }
  74. }
  75. [Localizable (true)]
  76. [DefaultValue (0)]
  77. public int ColumnCount {
  78. get { return settings.ColumnCount; }
  79. set { settings.ColumnCount = value; }
  80. }
  81. [Browsable (false)]
  82. [DisplayName ("Columns")]
  83. [MergableProperty (false)]
  84. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  85. public TableLayoutColumnStyleCollection ColumnStyles {
  86. get { return settings.ColumnStyles; }
  87. }
  88. [Browsable (false)]
  89. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  90. new public TableLayoutControlCollection Controls {
  91. get { return (TableLayoutControlCollection) base.Controls; }
  92. }
  93. [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
  94. public TableLayoutPanelGrowStyle GrowStyle {
  95. get { return settings.GrowStyle; }
  96. set { settings.GrowStyle = value; }
  97. }
  98. public override System.Windows.Forms.Layout.LayoutEngine LayoutEngine {
  99. get { return TableLayoutPanel.layout_engine; }
  100. }
  101. [Browsable (false)]
  102. [EditorBrowsable (EditorBrowsableState.Never)]
  103. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  104. public TableLayoutSettings LayoutSettings {
  105. get { return this.settings; }
  106. set {
  107. if (value.isSerialized) {
  108. this.settings = value;
  109. value.isSerialized = false;
  110. } else
  111. throw new NotSupportedException ("LayoutSettings value cannot be set directly.");
  112. }
  113. }
  114. [Localizable (true)]
  115. [DefaultValue (0)]
  116. public int RowCount {
  117. get { return settings.RowCount; }
  118. set { this.settings.RowCount = value; }
  119. }
  120. [Browsable (false)]
  121. [DisplayName ("Rows")]
  122. [MergableProperty (false)]
  123. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  124. public TableLayoutRowStyleCollection RowStyles {
  125. get { return settings.RowStyles; }
  126. }
  127. #endregion
  128. #region Public Methods
  129. [DefaultValue (-1)]
  130. [DisplayName ("Cell")]
  131. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  132. public TableLayoutPanelCellPosition GetCellPosition (Control control)
  133. {
  134. return settings.GetCellPosition (control);
  135. }
  136. [DisplayName ("Column")]
  137. [DefaultValue (-1)]
  138. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  139. public int GetColumn (Control control)
  140. {
  141. return settings.GetColumn (control);
  142. }
  143. [DisplayName ("ColumnSpan")]
  144. [DefaultValue (1)]
  145. public int GetColumnSpan (Control control)
  146. {
  147. return settings.GetColumnSpan (control);
  148. }
  149. [Browsable (false)]
  150. [EditorBrowsable (EditorBrowsableState.Never)]
  151. public int[] GetColumnWidths ()
  152. {
  153. return this.column_widths;
  154. }
  155. public Control GetControlFromPosition (int column, int row)
  156. {
  157. if (column < 0 || row < 0)
  158. throw new ArgumentException ();
  159. TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition (column, row);
  160. foreach (Control c in this.Controls)
  161. if (settings.GetCellPosition (c) == pos)
  162. return c;
  163. return null;
  164. }
  165. public TableLayoutPanelCellPosition GetPositionFromControl (Control control)
  166. {
  167. for (int x = 0; x < this.actual_positions.GetLength (0); x++)
  168. for (int y = 0; y < this.actual_positions.GetLength (1); y++)
  169. if (this.actual_positions[x, y] == control)
  170. return new TableLayoutPanelCellPosition (x, y);
  171. return new TableLayoutPanelCellPosition (-1, -1);
  172. }
  173. [DisplayName ("Row")]
  174. [DefaultValue ("-1")]
  175. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  176. public int GetRow (Control control)
  177. {
  178. return settings.GetRow (control);
  179. }
  180. [Browsable (false)]
  181. [EditorBrowsable (EditorBrowsableState.Never)]
  182. public int[] GetRowHeights ()
  183. {
  184. return this.row_heights;
  185. }
  186. [DisplayName ("RowSpan")]
  187. [DefaultValue (1)]
  188. public int GetRowSpan (Control control)
  189. {
  190. return settings.GetRowSpan (control);
  191. }
  192. public void SetCellPosition (Control control, TableLayoutPanelCellPosition position)
  193. {
  194. settings.SetCellPosition (control, position);
  195. this.PerformLayout ();
  196. }
  197. public void SetColumn (Control control, int column)
  198. {
  199. settings.SetColumn (control, column);
  200. this.PerformLayout ();
  201. }
  202. public void SetColumnSpan (Control control, int value)
  203. {
  204. settings.SetColumnSpan (control, value);
  205. this.PerformLayout ();
  206. }
  207. public void SetRow (Control control, int row)
  208. {
  209. settings.SetRow (control, row);
  210. this.PerformLayout ();
  211. }
  212. public void SetRowSpan (Control control, int value)
  213. {
  214. settings.SetRowSpan (control, value);
  215. this.PerformLayout ();
  216. }
  217. #endregion
  218. #region Protected Methods
  219. [EditorBrowsable (EditorBrowsableState.Advanced)]
  220. protected override ControlCollection CreateControlsInstance ()
  221. {
  222. return new TableLayoutControlCollection (this);
  223. }
  224. protected virtual void OnCellPaint (TableLayoutCellPaintEventArgs e)
  225. {
  226. TableLayoutCellPaintEventHandler eh = (TableLayoutCellPaintEventHandler)(Events [CellPaintEvent]);
  227. if (eh != null)
  228. eh (this, e);
  229. }
  230. [EditorBrowsable (EditorBrowsableState.Advanced)]
  231. protected override void OnLayout (LayoutEventArgs levent)
  232. {
  233. base.OnLayout (levent);
  234. }
  235. protected override void OnPaintBackground (PaintEventArgs e)
  236. {
  237. base.OnPaintBackground (e);
  238. }
  239. #endregion
  240. #region Public Events
  241. static object CellPaintEvent = new object ();
  242. public event TableLayoutCellPaintEventHandler CellPaint {
  243. add { Events.AddHandler (CellPaintEvent, value); }
  244. remove { Events.RemoveHandler (CellPaintEvent, value); }
  245. }
  246. #endregion
  247. #region IExtenderProvider
  248. bool IExtenderProvider.CanExtend (object extendee)
  249. {
  250. if (extendee is Control)
  251. if ((extendee as Control).Parent == this)
  252. return true;
  253. return false;
  254. }
  255. #endregion
  256. }
  257. }
  258. #endif