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
26 using System.ComponentModel;
\r
27 using System.Drawing;
\r
28 using System.Globalization;
\r
31 using System.Collections.Generic;
\r
33 namespace System.Windows.Forms.Layout
\r
35 public class TableLayoutSettingsTypeConverter : TypeConverter
\r
37 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
\r
39 if (destinationType == typeof (string))
\r
42 return base.CanConvertTo (context, destinationType);
\r
45 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
\r
47 if (sourceType == typeof(string))
\r
50 return base.CanConvertFrom (context, sourceType);
\r
55 public override object ConvertTo (ITypeDescriptorContext context,
\r
56 CultureInfo culture,
\r
58 Type destinationType)
\r
60 if (!(value is TableLayoutSettings) || destinationType != typeof (string))
\r
61 return base.ConvertTo (context, culture, value, destinationType);
\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
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
80 xw.WriteEndElement ();
\r
83 List<string> styles = new List<string> ();
\r
85 foreach (ColumnStyle style in settings.ColumnStyles) {
\r
86 styles.Add (style.SizeType.ToString ());
\r
87 styles.Add (style.Width.ToString ());
\r
91 xw.WriteStartElement ("Columms");
\r
92 xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));
\r
93 xw.WriteEndElement ();
\r
96 foreach (RowStyle style in settings.RowStyles) {
\r
97 styles.Add (style.SizeType.ToString ());
\r
98 styles.Add (style.Height.ToString ());
\r
101 xw.WriteStartElement ("Rows");
\r
102 xw.WriteAttributeString ("Styles", String.Join (",", styles.ToArray ()));
\r
103 xw.WriteEndElement ();
\r
105 xw.WriteEndElement ();
\r
106 xw.WriteEndDocument ();
\r
109 return sw.ToString ();
\r
113 public override object ConvertFrom (ITypeDescriptorContext context,
\r
114 CultureInfo culture,
\r
117 if (!(value is string))
\r
118 return base.ConvertFrom(context, culture, value);
\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
133 private int ParseControl (XmlDocument xmldoc, TableLayoutSettings settings)
\r
136 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Control")) {
\r
137 if (node.Attributes["Name"] == null || string.IsNullOrEmpty(node.Attributes["Name"].Value))
\r
139 if (node.Attributes["Row"] != null) {
\r
140 settings.SetRow (node.Attributes["Name"].Value, GetValue (node.Attributes["Row"].Value));
\r
143 if (node.Attributes["RowSpan"] != null) {
\r
144 settings.SetRowSpan (node.Attributes["Name"].Value, GetValue (node.Attributes["RowSpan"].Value));
\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
154 private void ParseColumnStyle (XmlDocument xmldoc, TableLayoutSettings settings)
\r
156 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Columns")) {
\r
157 if (node.Attributes["Styles"] == null)
\r
159 string styles = node.Attributes["Styles"].Value;
\r
160 if (styles == null)
\r
162 string[] list = styles.Split (',');
\r
163 for (int i = 0; i < list.Length; i+=2) {
\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
172 private void ParseRowStyle (XmlDocument xmldoc, TableLayoutSettings settings)
\r
174 foreach (XmlNode node in xmldoc.GetElementsByTagName ("Rows")) {
\r
175 if (node.Attributes["Styles"] == null)
\r
177 string styles = node.Attributes["Styles"].Value;
\r
178 if (styles == null)
\r
180 string[] list = styles.Split (',');
\r
181 for (int i = 0; i < list.Length; i += 2) {
\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
190 private int GetValue (string attValue)
\r
193 if (!string.IsNullOrEmpty (attValue)) {
\r
194 int.TryParse (attValue, out val);
\r