* ScrollableControlTest.cs: Added AutoScrollPositiontest
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Layout / TableLayoutSettingsTypeConverter.cs
1 // Permission is hereby granted, free of charge, to any person obtaining\r
2 // a copy of this software and associated documentation files (the\r
3 // "Software"), to deal in the Software without restriction, including\r
4 // without limitation the rights to use, copy, modify, merge, publish,\r
5 // distribute, sublicense, and/or sell copies of the Software, and to\r
6 // permit persons to whom the Software is furnished to do so, subject to\r
7 // the following conditions:\r
8 // \r
9 // The above copyright notice and this permission notice shall be\r
10 // included in all copies or substantial portions of the Software.\r
11 // \r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
19 //\r
20 // Copyright (c) 2006 Novell, Inc.\r
21 //\r
22 \r
23 #if NET_2_0\r
24 \r
25 using System;\r
26 using System.ComponentModel;\r
27 using System.Drawing;\r
28 using System.Globalization;\r
29 using System.Xml;\r
30 using System.IO;\r
31 using System.Collections.Generic;\r
32 \r
33 namespace System.Windows.Forms.Layout\r
34 {\r
35         public class TableLayoutSettingsTypeConverter : TypeConverter\r
36         {\r
37                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)\r
38                 {\r
39                         if (destinationType == typeof (string))\r
40                                 return true;\r
41 \r
42                         return base.CanConvertTo (context, destinationType);\r
43                 }\r
44 \r
45                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)\r
46                 {\r
47                         if (sourceType == typeof(string))\r
48                                 return true;\r
49 \r
50                         return base.CanConvertFrom (context, sourceType);\r
51                 }\r
52 \r
53                 \r
54 \r
55                 public override object ConvertTo (ITypeDescriptorContext context,\r
56                                                   CultureInfo culture,\r
57                                                   object value,\r
58                                                   Type destinationType)\r
59                 {\r
60                         if (!(value is TableLayoutSettings) || destinationType != typeof (string))\r
61                                 return base.ConvertTo (context, culture, value, destinationType);\r
62 \r
63                         TableLayoutSettings settings = value as TableLayoutSettings;\r
64                         StringWriter sw = new StringWriter ();\r
65                         XmlTextWriter xw = new XmlTextWriter (sw);\r
66                         xw.WriteStartDocument ();\r
67                         List<ControlInfo> list = settings.GetControls ();\r
68                         xw.WriteStartElement ("TableLayoutSettings");\r
69                         xw.WriteStartElement ("Controls");\r
70                         \r
71                         foreach (ControlInfo info in list) {\r
72                                 xw.WriteStartElement ("Control");\r
73                                 xw.WriteAttributeString ("Name", info.Control.ToString ());\r
74                                 xw.WriteAttributeString ("Row", info.Row.ToString ());\r
75                                 xw.WriteAttributeString ("RowSpan", info.RowSpan.ToString ());\r
76                                 xw.WriteAttributeString ("Column", info.Col.ToString ());\r
77                                 xw.WriteAttributeString ("ColumnSpan", info.ColSpan.ToString ());\r
78                                 xw.WriteEndElement ();\r
79                         }\r
80                         xw.WriteEndElement ();\r
81 \r
82 \r
83                         List<string> styles = new List<string> ();\r
84                         \r
85                         foreach (ColumnStyle style in settings.ColumnStyles) {\r
86                                 styles.Add (style.SizeType.ToString ());\r
87                                 styles.Add (style.Width.ToString ());\r
88                         }\r
89 \r
90                         \r
91                         xw.WriteStartElement ("Columms");\r
92                         xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));\r
93                         xw.WriteEndElement ();\r
94                         \r
95                         styles.Clear();\r
96                         foreach (RowStyle style in settings.RowStyles) {\r
97                                 styles.Add (style.SizeType.ToString ());\r
98                                 styles.Add (style.Height.ToString ());\r
99                         }\r
100 \r
101                         xw.WriteStartElement ("Rows");\r
102                         xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));\r
103                         xw.WriteEndElement ();\r
104 \r
105                         xw.WriteEndElement ();\r
106                         xw.WriteEndDocument ();\r
107                         xw.Close ();\r
108 \r
109                         return sw.ToString ();\r
110 \r
111                 }\r
112 \r
113                 public override object ConvertFrom (ITypeDescriptorContext context,\r
114                                                     CultureInfo culture,\r
115                                                     object value)\r
116                 {\r
117                         if (!(value is string))\r
118                                 return base.ConvertFrom(context, culture, value);\r
119 \r
120                         XmlDocument xmldoc = new XmlDocument();\r
121                         xmldoc.LoadXml (value as string);\r
122                         TableLayoutSettings settings = new TableLayoutSettings(new TableLayoutPanel ());\r
123                         int count = ParseControl (xmldoc, settings);\r
124                         ParseColumnStyle (xmldoc, settings);\r
125                         ParseRowStyle (xmldoc, settings);\r
126                         settings.RowCount = count;\r
127                         \r
128 \r
129                         return settings;\r
130                 }\r
131 \r
132 \r
133                 private int ParseControl (XmlDocument xmldoc, TableLayoutSettings settings)\r
134                 {\r
135                         int count = 0;\r
136                         foreach (XmlNode node in xmldoc.GetElementsByTagName ("Control")) {\r
137                                 if (node.Attributes["Name"] == null || string.IsNullOrEmpty(node.Attributes["Name"].Value))\r
138                                         continue;\r
139                                 if (node.Attributes["Row"] != null) {\r
140                                         settings.SetRow (node.Attributes["Name"].Value, GetValue (node.Attributes["Row"].Value));\r
141                                         count++;\r
142                                 }\r
143                                 if (node.Attributes["RowSpan"] != null) {\r
144                                         settings.SetRowSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["RowSpan"].Value));\r
145                                 }\r
146                                 if (node.Attributes["Column"] != null)\r
147                                         settings.SetColumn (node.Attributes["Name"].Value, GetValue (node.Attributes["Column"].Value));\r
148                                 if (node.Attributes["ColumnSpan"] != null)\r
149                                         settings.SetColumnSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["ColumnSpan"].Value));\r
150                         }\r
151                         return count;\r
152                 }\r
153 \r
154                 private void ParseColumnStyle (XmlDocument xmldoc, TableLayoutSettings settings)\r
155                 {\r
156                         foreach (XmlNode node in xmldoc.GetElementsByTagName ("Columns")) {\r
157                                 if (node.Attributes["Styles"] == null)\r
158                                         continue;\r
159                                 string styles = node.Attributes["Styles"].Value;\r
160                                 if (styles == null)\r
161                                         continue;\r
162                                 string[] list = styles.Split (',');\r
163                                 for (int i = 0; i < list.Length; i+=2) {\r
164                                         float width = 0f;\r
165                                         SizeType type = (SizeType) Enum.Parse (typeof (SizeType), list[i]);\r
166                                         float.TryParse (list[i+1], out width);\r
167                                         settings.ColumnStyles.Add (new ColumnStyle (type, width));\r
168                                 }                               \r
169                         }\r
170                 }\r
171 \r
172                 private void ParseRowStyle (XmlDocument xmldoc, TableLayoutSettings settings)\r
173                 {\r
174                         foreach (XmlNode node in xmldoc.GetElementsByTagName ("Rows")) {\r
175                                 if (node.Attributes["Styles"] == null)\r
176                                         continue;\r
177                                 string styles = node.Attributes["Styles"].Value;\r
178                                 if (styles == null)\r
179                                         continue;\r
180                                 string[] list = styles.Split (',');\r
181                                 for (int i = 0; i < list.Length; i += 2) {\r
182                                         float height = 0f;\r
183                                         SizeType type = (SizeType) Enum.Parse (typeof (SizeType), list[i]);\r
184                                         float.TryParse (list[i + 1], out height);\r
185                                         settings.RowStyles.Add (new RowStyle (type, height));\r
186                                 }\r
187                         }\r
188                 }\r
189 \r
190                 private int GetValue (string attValue)\r
191                 {\r
192                         int val = -1;\r
193                         if (!string.IsNullOrEmpty (attValue)) {\r
194                                 int.TryParse (attValue, out val);\r
195                         }\r
196                         return val;\r
197                 }\r
198         }\r
199 }\r
200 \r
201 #endif\r