TableLayoutSettings.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // TableLayoutSettings.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.Collections.Generic;
  32. using System.Windows.Forms.Layout;
  33. using System.Runtime.Serialization;
  34. namespace System.Windows.Forms
  35. {
  36. [Serializable]
  37. [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
  38. public sealed class TableLayoutSettings : LayoutSettings, ISerializable
  39. {
  40. private TableLayoutColumnStyleCollection column_styles;
  41. private TableLayoutRowStyleCollection row_styles;
  42. private TableLayoutPanelGrowStyle grow_style;
  43. private int column_count;
  44. private int row_count;
  45. private Dictionary<Object, int> columns;
  46. private Dictionary<Object, int> column_spans;
  47. private Dictionary<Object, int> rows;
  48. private Dictionary<Object, int> row_spans;
  49. private TableLayoutPanel panel;
  50. internal bool isSerialized;
  51. #region Internal Constructor
  52. internal TableLayoutSettings (TableLayoutPanel panel)
  53. {
  54. this.column_styles = new TableLayoutColumnStyleCollection (panel);
  55. this.row_styles = new TableLayoutRowStyleCollection (panel);
  56. this.grow_style = TableLayoutPanelGrowStyle.AddRows;
  57. this.column_count = 0;
  58. this.row_count = 0;
  59. this.columns = new Dictionary<object, int> ();
  60. this.column_spans = new Dictionary<object, int> ();
  61. this.rows = new Dictionary<object, int> ();
  62. this.row_spans = new Dictionary<object, int> ();
  63. this.panel = panel;
  64. }
  65. private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
  66. {
  67. TypeConverter converter = TypeDescriptor.GetConverter (this);
  68. string text = serializationInfo.GetString ("SerializedString");
  69. if (!string.IsNullOrEmpty (text) && (converter != null)) {
  70. TableLayoutSettings settings = converter.ConvertFromInvariantString (text) as TableLayoutSettings;
  71. this.column_styles = settings.column_styles;
  72. this.row_styles = settings.row_styles;
  73. this.grow_style = settings.grow_style;
  74. this.column_count = settings.column_count;
  75. this.row_count = settings.row_count;
  76. this.columns = settings.columns;
  77. this.column_spans = settings.column_spans;
  78. this.rows = settings.rows;
  79. this.row_spans = settings.row_spans;
  80. this.panel = settings.panel;
  81. this.isSerialized = true;
  82. }
  83. }
  84. #endregion
  85. #region Public Properties
  86. [DefaultValue (0)]
  87. public int ColumnCount {
  88. get { return this.column_count; }
  89. set {
  90. if (value < 0)
  91. throw new ArgumentOutOfRangeException();
  92. column_count = value;
  93. }
  94. }
  95. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  96. public TableLayoutColumnStyleCollection ColumnStyles {
  97. get { return this.column_styles; }
  98. }
  99. [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
  100. public TableLayoutPanelGrowStyle GrowStyle {
  101. get { return this.grow_style; }
  102. set {
  103. if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
  104. throw new ArgumentException();
  105. grow_style = value;
  106. }
  107. }
  108. public override LayoutEngine LayoutEngine {
  109. get { return this.panel.LayoutEngine; }
  110. }
  111. [DefaultValue (0)]
  112. public int RowCount {
  113. get { return this.row_count; }
  114. set {
  115. if (value < 0)
  116. throw new ArgumentOutOfRangeException ();
  117. row_count = value;
  118. }
  119. }
  120. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  121. public TableLayoutRowStyleCollection RowStyles {
  122. get { return row_styles; }
  123. }
  124. #endregion
  125. #region Public Methods
  126. [DefaultValue (-1)]
  127. public TableLayoutPanelCellPosition GetCellPosition (Object control)
  128. {
  129. if (control == null)
  130. throw new ArgumentNullException ();
  131. int column;
  132. int row;
  133. if (!columns.TryGetValue (control, out column))
  134. column = -1;
  135. if (!rows.TryGetValue (control, out row))
  136. row = -1;
  137. return new TableLayoutPanelCellPosition (column, row);
  138. }
  139. [DefaultValue (-1)]
  140. public int GetColumn (Object control)
  141. {
  142. if (control == null)
  143. throw new ArgumentNullException ();
  144. int retval;
  145. if (columns.TryGetValue (control, out retval))
  146. return retval;
  147. return -1;
  148. }
  149. public int GetColumnSpan (Object control)
  150. {
  151. if (control == null)
  152. throw new ArgumentNullException ();
  153. int retval;
  154. if (column_spans.TryGetValue (control, out retval))
  155. return retval;
  156. return 1;
  157. }
  158. [DefaultValue (-1)]
  159. public int GetRow (Object control)
  160. {
  161. if (control == null)
  162. throw new ArgumentNullException ();
  163. int retval;
  164. if (rows.TryGetValue (control, out retval))
  165. return retval;
  166. return -1;
  167. }
  168. public int GetRowSpan (Object control)
  169. {
  170. if (control == null)
  171. throw new ArgumentNullException ();
  172. int retval;
  173. if (row_spans.TryGetValue (control, out retval))
  174. return retval;
  175. return 1;
  176. }
  177. [DefaultValue (-1)]
  178. public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
  179. {
  180. if (control == null)
  181. throw new ArgumentNullException ();
  182. columns[control] = cellPosition.Column;
  183. rows[control] = cellPosition.Row;
  184. }
  185. public void SetColumn (Object control, int column)
  186. {
  187. if (control == null)
  188. throw new ArgumentNullException ();
  189. if (column < -1)
  190. throw new ArgumentException ();
  191. columns[control] = column;
  192. }
  193. public void SetColumnSpan (Object control, int value)
  194. {
  195. if (control == null)
  196. throw new ArgumentNullException ();
  197. if (value < -1)
  198. throw new ArgumentException ();
  199. column_spans[control] = value;
  200. }
  201. public void SetRow (Object control, int row)
  202. {
  203. if (control == null)
  204. throw new ArgumentNullException ();
  205. if (row < -1)
  206. throw new ArgumentException ();
  207. rows[control] = row;
  208. }
  209. public void SetRowSpan (Object control, int value)
  210. {
  211. if (control == null)
  212. throw new ArgumentNullException ();
  213. if (value < -1)
  214. throw new ArgumentException ();
  215. row_spans[control] = value;
  216. }
  217. #endregion
  218. #region Internal Methods
  219. internal List<ControlInfo> GetControls ()
  220. {
  221. List<ControlInfo> list = new List<ControlInfo>();
  222. foreach (KeyValuePair <object, int> control in columns) {
  223. ControlInfo info = new ControlInfo();
  224. info.Control = control.Key;
  225. info.Col = GetColumn(control);
  226. info.ColSpan = GetColumnSpan (control);
  227. info.Row = GetRow (control);
  228. info.RowSpan = GetRowSpan (control);
  229. list.Add (info);
  230. }
  231. return list;
  232. }
  233. #endregion
  234. #region ISerializable Members
  235. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  236. {
  237. TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
  238. string text = conv.ConvertToInvariantString (this);
  239. info.AddValue ("SerializedString", text);
  240. }
  241. #endregion
  242. }
  243. internal struct ControlInfo
  244. {
  245. public object Control;
  246. public int Row;
  247. public int RowSpan;
  248. public int Col;
  249. public int ColSpan;
  250. }
  251. }
  252. #endif