* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TableLayoutSettings.cs
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 (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.ComponentModel;
32 using System.Collections.Generic;
33 using System.Windows.Forms.Layout;
34 using System.Runtime.Serialization;
35
36 namespace System.Windows.Forms 
37 {
38         [Serializable]
39         [TypeConverter (typeof (TableLayoutSettingsTypeConverter))]
40         public sealed class TableLayoutSettings : LayoutSettings, ISerializable 
41         {
42                 private TableLayoutColumnStyleCollection column_styles;
43                 private TableLayoutRowStyleCollection row_styles;
44                 private TableLayoutPanelGrowStyle grow_style;
45                 private int column_count;
46                 private int row_count;
47                 private Dictionary<Object, int> columns;
48                 private Dictionary<Object, int> column_spans;
49                 private Dictionary<Object, int> rows;
50                 private Dictionary<Object, int> row_spans;
51                 internal TableLayoutPanel panel;
52                 internal bool isSerialized;
53
54                 #region Internal Constructor
55                 internal TableLayoutSettings (TableLayoutPanel panel)
56                 {
57                         this.column_styles = new TableLayoutColumnStyleCollection (panel);
58                         this.row_styles = new TableLayoutRowStyleCollection (panel);
59                         this.grow_style = TableLayoutPanelGrowStyle.AddRows;
60                         this.column_count = 0;
61                         this.row_count = 0;
62                         this.columns = new Dictionary<object, int> ();
63                         this.column_spans = new Dictionary<object, int> ();
64                         this.rows = new Dictionary<object, int> ();
65                         this.row_spans = new Dictionary<object, int> ();
66                         this.panel = panel;
67                 }
68
69                 private TableLayoutSettings (SerializationInfo serializationInfo, StreamingContext context)
70                 {
71                         TypeConverter converter = TypeDescriptor.GetConverter (this);
72                         string text = serializationInfo.GetString ("SerializedString");
73                         if (!string.IsNullOrEmpty (text) && (converter != null)) {
74                                 TableLayoutSettings settings = converter.ConvertFromInvariantString (text) as TableLayoutSettings;
75                                 this.column_styles = settings.column_styles;
76                                 this.row_styles = settings.row_styles;
77                                 this.grow_style = settings.grow_style;
78                                 this.column_count = settings.column_count;
79                                 this.row_count = settings.row_count;
80                                 this.columns = settings.columns;
81                                 this.column_spans = settings.column_spans;
82                                 this.rows = settings.rows;
83                                 this.row_spans = settings.row_spans;
84                                 this.panel = settings.panel;
85                                 this.isSerialized = true;
86                         }
87                 }
88                 #endregion              
89
90                 #region Public Properties
91                 [DefaultValue (0)]
92                 public int ColumnCount {
93                         get { return this.column_count; }
94                         set {
95                                 if (value < 0)
96                                         throw new ArgumentOutOfRangeException();
97                                         
98                                 if (column_count != value) {
99                                         column_count = value;
100                                         if (panel != null)
101                                                 panel.PerformLayout (panel, "ColumnCount");
102                                 }
103                         }
104                 }
105
106                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
107                 public TableLayoutColumnStyleCollection ColumnStyles {
108                         get { return this.column_styles; }
109                 }
110
111                 [DefaultValue (TableLayoutPanelGrowStyle.AddRows)]
112                 public TableLayoutPanelGrowStyle GrowStyle {
113                         get { return this.grow_style; }
114                         set {
115                                 if (!Enum.IsDefined (typeof(TableLayoutPanelGrowStyle), value))
116                                         throw new ArgumentException();
117                                         
118                                 if (grow_style != value) {
119                                         grow_style = value;
120                                         if (panel != null)
121                                                 panel.PerformLayout (panel, "GrowStyle");
122                                 }
123                         }
124                 }
125                 
126                 public override LayoutEngine LayoutEngine {
127                         get {
128                                 if (panel != null)
129                                         return panel.LayoutEngine;
130                                 return base.LayoutEngine; 
131                         }
132                 }
133                 
134                 [DefaultValue (0)]
135                 public int RowCount {
136                         get { return this.row_count; }
137                         set {
138                                 if (value < 0)
139                                         throw new ArgumentOutOfRangeException ();
140
141                                 if (row_count != value) {
142                                         row_count = value;
143
144                                         if (panel != null)
145                                                 panel.PerformLayout ();
146                                 }
147                         }
148                 }
149
150                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
151                 public TableLayoutRowStyleCollection RowStyles {
152                         get { return row_styles; }
153                 }
154                 #endregion
155
156                 #region Public Methods
157                 [DefaultValue (-1)]
158                 public TableLayoutPanelCellPosition GetCellPosition (Object control)
159                 {
160                         if (control == null)
161                                 throw new ArgumentNullException ();
162
163                         int column;
164                         int row;
165
166                         if (!columns.TryGetValue (control, out column))
167                                 column = -1;
168                         if (!rows.TryGetValue (control, out row))
169                                 row = -1;
170
171                         return new TableLayoutPanelCellPosition (column, row);
172                 }
173
174                 [DefaultValue (-1)]
175                 public int GetColumn (Object control)
176                 {
177                         if (control == null)
178                                 throw new ArgumentNullException ();
179
180                         int retval;
181
182                         if (columns.TryGetValue (control, out retval))
183                                 return retval;
184
185                         return -1;
186                 }
187
188                 public int GetColumnSpan (Object control)
189                 {
190                         if (control == null)
191                                 throw new ArgumentNullException ();
192
193                         int retval;
194
195                         if (column_spans.TryGetValue (control, out retval))
196                                 return retval;
197
198                         return 1;
199                 }
200
201                 [DefaultValue (-1)]
202                 public int GetRow (Object control)
203                 {
204                         if (control == null)
205                                 throw new ArgumentNullException ();
206
207                         int retval;
208
209                         if (rows.TryGetValue (control, out retval))
210                                 return retval;
211
212                         return -1;
213                 }
214
215                 public int GetRowSpan (Object control)
216                 {
217                         if (control == null)
218                                 throw new ArgumentNullException ();
219
220                         int retval;
221
222                         if (row_spans.TryGetValue (control, out retval))
223                                 return retval;
224
225                         return 1;
226                 }
227
228                 [DefaultValue (-1)]
229                 public void SetCellPosition (Object control, TableLayoutPanelCellPosition cellPosition)
230                 {
231                         if (control == null)
232                                 throw new ArgumentNullException ();
233
234                         columns[control] = cellPosition.Column;
235                         rows[control] = cellPosition.Row;
236
237                         if (panel != null)
238                                 panel.PerformLayout ();
239                 }
240
241                 public void SetColumn (Object control, int column)
242                 {
243                         if (control == null)
244                                 throw new ArgumentNullException ();
245                         if (column < -1)
246                                 throw new ArgumentException ();
247                                 
248                         columns[control] = column;
249
250                         if (panel != null)
251                                 panel.PerformLayout ();
252                 }
253
254                 public void SetColumnSpan (Object control, int value)
255                 {
256                         if (control == null)
257                                 throw new ArgumentNullException ();
258                         if (value < -1)
259                                 throw new ArgumentException ();
260
261                         column_spans[control] = value;
262
263                         if (panel != null)
264                                 panel.PerformLayout ();
265                 }
266
267                 public void SetRow (Object control, int row)
268                 {
269                         if (control == null)
270                                 throw new ArgumentNullException ();
271                         if (row < -1)
272                                 throw new ArgumentException ();
273
274                         rows[control] = row;
275
276                         if (panel != null)
277                                 panel.PerformLayout ();
278                 }
279
280                 public void SetRowSpan (Object control, int value)
281                 {
282                         if (control == null)
283                                 throw new ArgumentNullException ();
284                         if (value < -1)
285                                 throw new ArgumentException ();
286
287                         row_spans[control] = value;
288                         
289                         if (panel != null)
290                                 panel.PerformLayout ();
291                 }
292                 #endregion
293
294                 #region Internal Methods
295                 internal List<ControlInfo> GetControls ()
296                 {
297                         List<ControlInfo> list = new List<ControlInfo>();
298                         foreach (KeyValuePair <object, int> control in columns) {
299                                 ControlInfo info = new ControlInfo();
300                                 info.Control = control.Key;
301                                 info.Col = GetColumn(control.Key);
302                                 info.ColSpan = GetColumnSpan (control.Key);
303                                 info.Row = GetRow (control.Key);
304                                 info.RowSpan = GetRowSpan (control.Key);
305                                 list.Add (info);
306                         }
307                         return list;
308                 }
309
310                 #endregion
311
312                 #region ISerializable Members
313                 void ISerializable.GetObjectData (SerializationInfo si, StreamingContext context)
314                 {
315                         TableLayoutSettingsTypeConverter conv = new TableLayoutSettingsTypeConverter ();
316                         string text = conv.ConvertToInvariantString (this);
317                         si.AddValue ("SerializedString", text);
318                 }
319                 #endregion
320                 
321                 internal class StyleConverter : TypeConverter
322                 {
323                         public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
324                         {
325                                 if ((value == null) || !(value is String))
326                                         return base.ConvertFrom (context, culture, value);
327
328                                 return Enum.Parse (typeof (StyleConverter), (string)value, true);
329                         }
330
331                         public override object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
332                         {
333                                 if ((value == null) || !(value is StyleConverter) || (destinationType != typeof (string)))
334                                         return base.ConvertTo (context, culture, value, destinationType);
335
336                                 return ((StyleConverter)value).ToString ();
337                         }
338                 }
339         }
340
341         internal struct ControlInfo
342         {
343                 public object Control;
344                 public int      Row;
345                 public int RowSpan;
346                 public int Col;
347                 public int ColSpan;
348         }
349 }
350 #endif