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
9 // The above copyright notice and this permission notice shall be
\r
10 // included in all copies or substantial portions of the Software.
\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
20 // Copyright (c) 2006 Novell, Inc.
\r
25 using System.ComponentModel;
\r
26 using System.Drawing;
\r
27 using System.Globalization;
\r
30 using System.Collections.Generic;
\r
32 namespace System.Windows.Forms.Layout
\r
34 public class TableLayoutSettingsTypeConverter : TypeConverter
\r
36 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
\r
38 if (destinationType == typeof (string))
\r
41 return base.CanConvertTo (context, destinationType);
\r
44 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
\r
46 if (sourceType == typeof(string))
\r
49 return base.CanConvertFrom (context, sourceType);
\r
54 public override object ConvertTo (ITypeDescriptorContext context,
\r
55 CultureInfo culture,
\r
57 Type destinationType)
\r
59 if (!(value is TableLayoutSettings) || destinationType != typeof (string))
\r
60 return base.ConvertTo (context, culture, value, destinationType);
\r
62 TableLayoutSettings settings = value as TableLayoutSettings;
\r
63 StringWriter sw = new StringWriter ();
\r
64 XmlTextWriter xw = new XmlTextWriter (sw);
\r
65 xw.WriteStartDocument ();
\r
66 List<ControlInfo> list = settings.GetControls ();
\r
67 xw.WriteStartElement ("TableLayoutSettings");
\r
68 xw.WriteStartElement ("Controls");
\r
70 foreach (ControlInfo info in list) {
\r
71 xw.WriteStartElement ("Control");
\r
72 xw.WriteAttributeString ("Name", info.Control.ToString ());
\r
73 xw.WriteAttributeString ("Row", info.Row.ToString ());
\r
74 xw.WriteAttributeString ("RowSpan", info.RowSpan.ToString ());
\r
75 xw.WriteAttributeString ("Column", info.Col.ToString ());
\r
76 xw.WriteAttributeString ("ColumnSpan", info.ColSpan.ToString ());
\r
77 xw.WriteEndElement ();
\r
79 xw.WriteEndElement ();
\r
82 List<string> styles = new List<string> ();
\r
84 foreach (ColumnStyle style in settings.ColumnStyles) {
\r
85 styles.Add (style.SizeType.ToString ());
\r
86 styles.Add (style.Width.ToString (CultureInfo.InvariantCulture));
\r
90 xw.WriteStartElement ("Columns");
\r
91 xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));
\r
92 xw.WriteEndElement ();
\r
95 foreach (RowStyle style in settings.RowStyles) {
\r
96 styles.Add (style.SizeType.ToString ());
\r
97 styles.Add (style.Height.ToString (CultureInfo.InvariantCulture));
\r
100 xw.WriteStartElement ("Rows");
\r
101 xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));
\r
102 xw.WriteEndElement ();
\r
104 xw.WriteEndElement ();
\r
105 xw.WriteEndDocument ();
\r
108 return sw.ToString ();
\r
112 public override object ConvertFrom (ITypeDescriptorContext context,
\r
113 CultureInfo culture,
\r
116 if (!(value is string))
\r
117 return base.ConvertFrom(context, culture, value);
\r
119 XmlDocument xmldoc = new XmlDocument();
\r
120 xmldoc.LoadXml (value as string);
\r
121 TableLayoutSettings settings = new TableLayoutSettings(null);
122 int count = ParseControl (xmldoc, settings);
\r
123 ParseColumnStyle (xmldoc, settings);
\r
124 ParseRowStyle (xmldoc, settings);
\r
125 settings.RowCount = count;
\r
132 private int ParseControl (XmlDocument xmldoc, TableLayoutSettings settings)
\r
135 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Control")) {
\r
136 if (node.Attributes["Name"] == null || string.IsNullOrEmpty(node.Attributes["Name"].Value))
\r
138 if (node.Attributes["Row"] != null) {
\r
139 settings.SetRow (node.Attributes["Name"].Value, GetValue (node.Attributes["Row"].Value));
\r
142 if (node.Attributes["RowSpan"] != null) {
\r
143 settings.SetRowSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["RowSpan"].Value));
\r
145 if (node.Attributes["Column"] != null)
\r
146 settings.SetColumn (node.Attributes["Name"].Value, GetValue (node.Attributes["Column"].Value));
\r
147 if (node.Attributes["ColumnSpan"] != null)
\r
148 settings.SetColumnSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["ColumnSpan"].Value));
\r
153 private void ParseColumnStyle (XmlDocument xmldoc, TableLayoutSettings settings)
\r
155 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Columns")) {
\r
156 if (node.Attributes["Styles"] == null)
\r
158 string styles = node.Attributes["Styles"].Value;
\r
159 if (string.IsNullOrEmpty (styles))
\r
161 string[] list = BuggySplit (styles);
\r
162 for (int i = 0; i < list.Length; i+=2) {
\r
164 SizeType type = (SizeType) Enum.Parse (typeof (SizeType), list[i]);
\r
165 float.TryParse (list[i+1], NumberStyles.Float, CultureInfo.InvariantCulture, out width);
\r
166 settings.ColumnStyles.Add (new ColumnStyle (type, width));
\r
171 private void ParseRowStyle (XmlDocument xmldoc, TableLayoutSettings settings)
\r
173 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Rows")) {
\r
174 if (node.Attributes["Styles"] == null)
\r
176 string styles = node.Attributes["Styles"].Value;
\r
177 if (string.IsNullOrEmpty(styles))
\r
179 string[] list = BuggySplit (styles);
\r
180 for (int i = 0; i < list.Length; i += 2) {
\r
182 SizeType type = (SizeType) Enum.Parse (typeof (SizeType), list[i]);
\r
183 float.TryParse (list[i + 1], NumberStyles.Float, CultureInfo.InvariantCulture, out height);
\r
184 settings.RowStyles.Add (new RowStyle (type, height));
\r
189 private int GetValue (string attValue)
\r
192 if (!string.IsNullOrEmpty (attValue)) {
\r
193 int.TryParse (attValue, out val);
\r
198 // .Net accidently uses the local culture separator, so
\r
199 // Percent,50.0,Percent,50.0 can be
\r
200 // Percent,50,0,Percent,50,0
\r
201 // Make sure we can handle this
\r
202 private string[] BuggySplit (string s)
\r
204 List<string> l = new List<string> ();
\r
206 string[] split = s.Split (',');
\r
208 for (int i = 0; i < split.Length; i++) {
\r
209 switch (split[i].ToLowerInvariant ()) {
\r
216 if (i + 1 < split.Length) {
\r
219 if (float.TryParse (split[i + 1], out test)) {
\r
220 l.Add (string.Format ("{0}.{1}", split[i], split[i + 1]));
\r
230 return l.ToArray ();
\r