1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | using System.Drawing;
|
---|
5 | using WeifenLuo.WinFormsUI.Docking;
|
---|
6 | using System.IO;
|
---|
7 | using System.Text;
|
---|
8 | using System.Xml;
|
---|
9 | using System.Globalization;
|
---|
10 |
|
---|
11 | namespace WeifenLuo.WinFormsUI.Docking
|
---|
12 | {
|
---|
13 | partial class DockPanel
|
---|
14 | {
|
---|
15 | private static class Persistor
|
---|
16 | {
|
---|
17 | private const string ConfigFileVersion = "1.0";
|
---|
18 | private static string[] CompatibleConfigFileVersions = new string[] { };
|
---|
19 |
|
---|
20 | private class DummyContent : DockContent
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 | private struct DockPanelStruct
|
---|
25 | {
|
---|
26 | private double m_dockLeftPortion;
|
---|
27 | public double DockLeftPortion
|
---|
28 | {
|
---|
29 | get { return m_dockLeftPortion; }
|
---|
30 | set { m_dockLeftPortion = value; }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private double m_dockRightPortion;
|
---|
34 | public double DockRightPortion
|
---|
35 | {
|
---|
36 | get { return m_dockRightPortion; }
|
---|
37 | set { m_dockRightPortion = value; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | private double m_dockTopPortion;
|
---|
41 | public double DockTopPortion
|
---|
42 | {
|
---|
43 | get { return m_dockTopPortion; }
|
---|
44 | set { m_dockTopPortion = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private double m_dockBottomPortion;
|
---|
48 | public double DockBottomPortion
|
---|
49 | {
|
---|
50 | get { return m_dockBottomPortion; }
|
---|
51 | set { m_dockBottomPortion = value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private int m_indexActiveDocumentPane;
|
---|
55 | public int IndexActiveDocumentPane
|
---|
56 | {
|
---|
57 | get { return m_indexActiveDocumentPane; }
|
---|
58 | set { m_indexActiveDocumentPane = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private int m_indexActivePane;
|
---|
62 | public int IndexActivePane
|
---|
63 | {
|
---|
64 | get { return m_indexActivePane; }
|
---|
65 | set { m_indexActivePane = value; }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private struct ContentStruct
|
---|
70 | {
|
---|
71 | private string m_persistString;
|
---|
72 | public string PersistString
|
---|
73 | {
|
---|
74 | get { return m_persistString; }
|
---|
75 | set { m_persistString = value; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private double m_autoHidePortion;
|
---|
79 | public double AutoHidePortion
|
---|
80 | {
|
---|
81 | get { return m_autoHidePortion; }
|
---|
82 | set { m_autoHidePortion = value; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private bool m_isHidden;
|
---|
86 | public bool IsHidden
|
---|
87 | {
|
---|
88 | get { return m_isHidden; }
|
---|
89 | set { m_isHidden = value; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private bool m_isFloat;
|
---|
93 | public bool IsFloat
|
---|
94 | {
|
---|
95 | get { return m_isFloat; }
|
---|
96 | set { m_isFloat = value; }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private struct PaneStruct
|
---|
101 | {
|
---|
102 | private DockState m_dockState;
|
---|
103 | public DockState DockState
|
---|
104 | {
|
---|
105 | get { return m_dockState; }
|
---|
106 | set { m_dockState = value; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private int m_indexActiveContent;
|
---|
110 | public int IndexActiveContent
|
---|
111 | {
|
---|
112 | get { return m_indexActiveContent; }
|
---|
113 | set { m_indexActiveContent = value; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private int[] m_indexContents;
|
---|
117 | public int[] IndexContents
|
---|
118 | {
|
---|
119 | get { return m_indexContents; }
|
---|
120 | set { m_indexContents = value; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private int m_zOrderIndex;
|
---|
124 | public int ZOrderIndex
|
---|
125 | {
|
---|
126 | get { return m_zOrderIndex; }
|
---|
127 | set { m_zOrderIndex = value; }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | private struct NestedPane
|
---|
132 | {
|
---|
133 | private int m_indexPane;
|
---|
134 | public int IndexPane
|
---|
135 | {
|
---|
136 | get { return m_indexPane; }
|
---|
137 | set { m_indexPane = value; }
|
---|
138 | }
|
---|
139 |
|
---|
140 | private int m_indexPrevPane;
|
---|
141 | public int IndexPrevPane
|
---|
142 | {
|
---|
143 | get { return m_indexPrevPane; }
|
---|
144 | set { m_indexPrevPane = value; }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private DockAlignment m_alignment;
|
---|
148 | public DockAlignment Alignment
|
---|
149 | {
|
---|
150 | get { return m_alignment; }
|
---|
151 | set { m_alignment = value; }
|
---|
152 | }
|
---|
153 |
|
---|
154 | private double m_proportion;
|
---|
155 | public double Proportion
|
---|
156 | {
|
---|
157 | get { return m_proportion; }
|
---|
158 | set { m_proportion = value; }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | private struct DockWindowStruct
|
---|
163 | {
|
---|
164 | private DockState m_dockState;
|
---|
165 | public DockState DockState
|
---|
166 | {
|
---|
167 | get { return m_dockState; }
|
---|
168 | set { m_dockState = value; }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private int m_zOrderIndex;
|
---|
172 | public int ZOrderIndex
|
---|
173 | {
|
---|
174 | get { return m_zOrderIndex; }
|
---|
175 | set { m_zOrderIndex = value; }
|
---|
176 | }
|
---|
177 |
|
---|
178 | private NestedPane[] m_nestedPanes;
|
---|
179 | public NestedPane[] NestedPanes
|
---|
180 | {
|
---|
181 | get { return m_nestedPanes; }
|
---|
182 | set { m_nestedPanes = value; }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private struct FloatWindowStruct
|
---|
187 | {
|
---|
188 | private Rectangle m_bounds;
|
---|
189 | public Rectangle Bounds
|
---|
190 | {
|
---|
191 | get { return m_bounds; }
|
---|
192 | set { m_bounds = value; }
|
---|
193 | }
|
---|
194 |
|
---|
195 | private int m_zOrderIndex;
|
---|
196 | public int ZOrderIndex
|
---|
197 | {
|
---|
198 | get { return m_zOrderIndex; }
|
---|
199 | set { m_zOrderIndex = value; }
|
---|
200 | }
|
---|
201 |
|
---|
202 | private NestedPane[] m_nestedPanes;
|
---|
203 | public NestedPane[] NestedPanes
|
---|
204 | {
|
---|
205 | get { return m_nestedPanes; }
|
---|
206 | set { m_nestedPanes = value; }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | public static void SaveAsXml(DockPanel dockPanel, string fileName)
|
---|
211 | {
|
---|
212 | SaveAsXml(dockPanel, fileName, Encoding.Unicode);
|
---|
213 | }
|
---|
214 |
|
---|
215 | public static void SaveAsXml(DockPanel dockPanel, string fileName, Encoding encoding)
|
---|
216 | {
|
---|
217 | FileStream fs = new FileStream(fileName, FileMode.Create);
|
---|
218 | try
|
---|
219 | {
|
---|
220 | SaveAsXml(dockPanel, fs, encoding);
|
---|
221 | }
|
---|
222 | finally
|
---|
223 | {
|
---|
224 | fs.Close();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | public static void SaveAsXml(DockPanel dockPanel, Stream stream, Encoding encoding)
|
---|
229 | {
|
---|
230 | SaveAsXml(dockPanel, stream, encoding, false);
|
---|
231 | }
|
---|
232 |
|
---|
233 | public static void SaveAsXml(DockPanel dockPanel, Stream stream, Encoding encoding, bool upstream)
|
---|
234 | {
|
---|
235 | XmlTextWriter xmlOut = new XmlTextWriter(stream, encoding);
|
---|
236 |
|
---|
237 | // Use indenting for readability
|
---|
238 | xmlOut.Formatting = Formatting.Indented;
|
---|
239 |
|
---|
240 | if (!upstream)
|
---|
241 | xmlOut.WriteStartDocument();
|
---|
242 |
|
---|
243 | // Always begin file with identification and warning
|
---|
244 | xmlOut.WriteComment(Strings.DockPanel_Persistor_XmlFileComment1);
|
---|
245 | xmlOut.WriteComment(Strings.DockPanel_Persistor_XmlFileComment2);
|
---|
246 |
|
---|
247 | // Associate a version number with the root element so that future version of the code
|
---|
248 | // will be able to be backwards compatible or at least recognise out of date versions
|
---|
249 | xmlOut.WriteStartElement("DockPanel");
|
---|
250 | xmlOut.WriteAttributeString("FormatVersion", ConfigFileVersion);
|
---|
251 | xmlOut.WriteAttributeString("DockLeftPortion", dockPanel.DockLeftPortion.ToString(CultureInfo.InvariantCulture));
|
---|
252 | xmlOut.WriteAttributeString("DockRightPortion", dockPanel.DockRightPortion.ToString(CultureInfo.InvariantCulture));
|
---|
253 | xmlOut.WriteAttributeString("DockTopPortion", dockPanel.DockTopPortion.ToString(CultureInfo.InvariantCulture));
|
---|
254 | xmlOut.WriteAttributeString("DockBottomPortion", dockPanel.DockBottomPortion.ToString(CultureInfo.InvariantCulture));
|
---|
255 |
|
---|
256 | if (!Win32Helper.IsRunningOnMono)
|
---|
257 | {
|
---|
258 | xmlOut.WriteAttributeString("ActiveDocumentPane", dockPanel.Panes.IndexOf(dockPanel.ActiveDocumentPane).ToString(CultureInfo.InvariantCulture));
|
---|
259 | xmlOut.WriteAttributeString("ActivePane", dockPanel.Panes.IndexOf(dockPanel.ActivePane).ToString(CultureInfo.InvariantCulture));
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Contents
|
---|
263 | xmlOut.WriteStartElement("Contents");
|
---|
264 | xmlOut.WriteAttributeString("Count", dockPanel.Contents.Count.ToString(CultureInfo.InvariantCulture));
|
---|
265 | foreach (IDockContent content in dockPanel.Contents)
|
---|
266 | {
|
---|
267 | xmlOut.WriteStartElement("Content");
|
---|
268 | xmlOut.WriteAttributeString("ID", dockPanel.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
|
---|
269 | xmlOut.WriteAttributeString("PersistString", content.DockHandler.PersistString);
|
---|
270 | xmlOut.WriteAttributeString("AutoHidePortion", content.DockHandler.AutoHidePortion.ToString(CultureInfo.InvariantCulture));
|
---|
271 | xmlOut.WriteAttributeString("IsHidden", content.DockHandler.IsHidden.ToString(CultureInfo.InvariantCulture));
|
---|
272 | xmlOut.WriteAttributeString("IsFloat", content.DockHandler.IsFloat.ToString(CultureInfo.InvariantCulture));
|
---|
273 | xmlOut.WriteEndElement();
|
---|
274 | }
|
---|
275 | xmlOut.WriteEndElement();
|
---|
276 |
|
---|
277 | // Panes
|
---|
278 | xmlOut.WriteStartElement("Panes");
|
---|
279 | xmlOut.WriteAttributeString("Count", dockPanel.Panes.Count.ToString(CultureInfo.InvariantCulture));
|
---|
280 | foreach (DockPane pane in dockPanel.Panes)
|
---|
281 | {
|
---|
282 | xmlOut.WriteStartElement("Pane");
|
---|
283 | xmlOut.WriteAttributeString("ID", dockPanel.Panes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
|
---|
284 | xmlOut.WriteAttributeString("DockState", pane.DockState.ToString());
|
---|
285 | xmlOut.WriteAttributeString("ActiveContent", dockPanel.Contents.IndexOf(pane.ActiveContent).ToString(CultureInfo.InvariantCulture));
|
---|
286 | xmlOut.WriteStartElement("Contents");
|
---|
287 | xmlOut.WriteAttributeString("Count", pane.Contents.Count.ToString(CultureInfo.InvariantCulture));
|
---|
288 | foreach (IDockContent content in pane.Contents)
|
---|
289 | {
|
---|
290 | xmlOut.WriteStartElement("Content");
|
---|
291 | xmlOut.WriteAttributeString("ID", pane.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
|
---|
292 | xmlOut.WriteAttributeString("RefID", dockPanel.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
|
---|
293 | xmlOut.WriteEndElement();
|
---|
294 | }
|
---|
295 | xmlOut.WriteEndElement();
|
---|
296 | xmlOut.WriteEndElement();
|
---|
297 | }
|
---|
298 | xmlOut.WriteEndElement();
|
---|
299 |
|
---|
300 | // DockWindows
|
---|
301 | xmlOut.WriteStartElement("DockWindows");
|
---|
302 | int dockWindowId = 0;
|
---|
303 | foreach (DockWindow dw in dockPanel.DockWindows)
|
---|
304 | {
|
---|
305 | xmlOut.WriteStartElement("DockWindow");
|
---|
306 | xmlOut.WriteAttributeString("ID", dockWindowId.ToString(CultureInfo.InvariantCulture));
|
---|
307 | dockWindowId++;
|
---|
308 | xmlOut.WriteAttributeString("DockState", dw.DockState.ToString());
|
---|
309 | xmlOut.WriteAttributeString("ZOrderIndex", dockPanel.Controls.IndexOf(dw).ToString(CultureInfo.InvariantCulture));
|
---|
310 | xmlOut.WriteStartElement("NestedPanes");
|
---|
311 | xmlOut.WriteAttributeString("Count", dw.NestedPanes.Count.ToString(CultureInfo.InvariantCulture));
|
---|
312 | foreach (DockPane pane in dw.NestedPanes)
|
---|
313 | {
|
---|
314 | xmlOut.WriteStartElement("Pane");
|
---|
315 | xmlOut.WriteAttributeString("ID", dw.NestedPanes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
|
---|
316 | xmlOut.WriteAttributeString("RefID", dockPanel.Panes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
|
---|
317 | NestedDockingStatus status = pane.NestedDockingStatus;
|
---|
318 | xmlOut.WriteAttributeString("PrevPane", dockPanel.Panes.IndexOf(status.PreviousPane).ToString(CultureInfo.InvariantCulture));
|
---|
319 | xmlOut.WriteAttributeString("Alignment", status.Alignment.ToString());
|
---|
320 | xmlOut.WriteAttributeString("Proportion", status.Proportion.ToString(CultureInfo.InvariantCulture));
|
---|
321 | xmlOut.WriteEndElement();
|
---|
322 | }
|
---|
323 | xmlOut.WriteEndElement();
|
---|
324 | xmlOut.WriteEndElement();
|
---|
325 | }
|
---|
326 | xmlOut.WriteEndElement();
|
---|
327 |
|
---|
328 | // FloatWindows
|
---|
329 | RectangleConverter rectConverter = new RectangleConverter();
|
---|
330 | xmlOut.WriteStartElement("FloatWindows");
|
---|
331 | xmlOut.WriteAttributeString("Count", dockPanel.FloatWindows.Count.ToString(CultureInfo.InvariantCulture));
|
---|
332 | foreach (FloatWindow fw in dockPanel.FloatWindows)
|
---|
333 | {
|
---|
334 | xmlOut.WriteStartElement("FloatWindow");
|
---|
335 | xmlOut.WriteAttributeString("ID", dockPanel.FloatWindows.IndexOf(fw).ToString(CultureInfo.InvariantCulture));
|
---|
336 | xmlOut.WriteAttributeString("Bounds", rectConverter.ConvertToInvariantString(fw.Bounds));
|
---|
337 | xmlOut.WriteAttributeString("ZOrderIndex", fw.DockPanel.FloatWindows.IndexOf(fw).ToString(CultureInfo.InvariantCulture));
|
---|
338 | xmlOut.WriteStartElement("NestedPanes");
|
---|
339 | xmlOut.WriteAttributeString("Count", fw.NestedPanes.Count.ToString(CultureInfo.InvariantCulture));
|
---|
340 | foreach (DockPane pane in fw.NestedPanes)
|
---|
341 | {
|
---|
342 | xmlOut.WriteStartElement("Pane");
|
---|
343 | xmlOut.WriteAttributeString("ID", fw.NestedPanes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
|
---|
344 | xmlOut.WriteAttributeString("RefID", dockPanel.Panes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
|
---|
345 | NestedDockingStatus status = pane.NestedDockingStatus;
|
---|
346 | xmlOut.WriteAttributeString("PrevPane", dockPanel.Panes.IndexOf(status.PreviousPane).ToString(CultureInfo.InvariantCulture));
|
---|
347 | xmlOut.WriteAttributeString("Alignment", status.Alignment.ToString());
|
---|
348 | xmlOut.WriteAttributeString("Proportion", status.Proportion.ToString(CultureInfo.InvariantCulture));
|
---|
349 | xmlOut.WriteEndElement();
|
---|
350 | }
|
---|
351 | xmlOut.WriteEndElement();
|
---|
352 | xmlOut.WriteEndElement();
|
---|
353 | }
|
---|
354 | xmlOut.WriteEndElement(); // </FloatWindows>
|
---|
355 |
|
---|
356 | xmlOut.WriteEndElement();
|
---|
357 |
|
---|
358 | if (!upstream)
|
---|
359 | {
|
---|
360 | xmlOut.WriteEndDocument();
|
---|
361 | xmlOut.Close();
|
---|
362 | }
|
---|
363 | else
|
---|
364 | xmlOut.Flush();
|
---|
365 | }
|
---|
366 |
|
---|
367 | public static void LoadFromXml(DockPanel dockPanel, string fileName, DeserializeDockContent deserializeContent)
|
---|
368 | {
|
---|
369 | FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
---|
370 | try
|
---|
371 | {
|
---|
372 | LoadFromXml(dockPanel, fs, deserializeContent);
|
---|
373 | }
|
---|
374 | finally
|
---|
375 | {
|
---|
376 | fs.Close();
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | public static void LoadFromXml(DockPanel dockPanel, Stream stream, DeserializeDockContent deserializeContent)
|
---|
381 | {
|
---|
382 | LoadFromXml(dockPanel, stream, deserializeContent, true);
|
---|
383 | }
|
---|
384 |
|
---|
385 | private static ContentStruct[] LoadContents(XmlTextReader xmlIn)
|
---|
386 | {
|
---|
387 | int countOfContents = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
388 | ContentStruct[] contents = new ContentStruct[countOfContents];
|
---|
389 | MoveToNextElement(xmlIn);
|
---|
390 | for (int i = 0; i < countOfContents; i++)
|
---|
391 | {
|
---|
392 | int id = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
393 | if (xmlIn.Name != "Content" || id != i)
|
---|
394 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
395 |
|
---|
396 | contents[i].PersistString = xmlIn.GetAttribute("PersistString");
|
---|
397 | contents[i].AutoHidePortion = Convert.ToDouble(xmlIn.GetAttribute("AutoHidePortion"), CultureInfo.InvariantCulture);
|
---|
398 | contents[i].IsHidden = Convert.ToBoolean(xmlIn.GetAttribute("IsHidden"), CultureInfo.InvariantCulture);
|
---|
399 | contents[i].IsFloat = Convert.ToBoolean(xmlIn.GetAttribute("IsFloat"), CultureInfo.InvariantCulture);
|
---|
400 | MoveToNextElement(xmlIn);
|
---|
401 | }
|
---|
402 |
|
---|
403 | return contents;
|
---|
404 | }
|
---|
405 |
|
---|
406 | private static PaneStruct[] LoadPanes(XmlTextReader xmlIn)
|
---|
407 | {
|
---|
408 | EnumConverter dockStateConverter = new EnumConverter(typeof(DockState));
|
---|
409 | int countOfPanes = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
410 | PaneStruct[] panes = new PaneStruct[countOfPanes];
|
---|
411 | MoveToNextElement(xmlIn);
|
---|
412 | for (int i = 0; i < countOfPanes; i++)
|
---|
413 | {
|
---|
414 | int id = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
415 | if (xmlIn.Name != "Pane" || id != i)
|
---|
416 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
417 |
|
---|
418 | panes[i].DockState = (DockState)dockStateConverter.ConvertFrom(xmlIn.GetAttribute("DockState"));
|
---|
419 | panes[i].IndexActiveContent = Convert.ToInt32(xmlIn.GetAttribute("ActiveContent"), CultureInfo.InvariantCulture);
|
---|
420 | panes[i].ZOrderIndex = -1;
|
---|
421 |
|
---|
422 | MoveToNextElement(xmlIn);
|
---|
423 | if (xmlIn.Name != "Contents")
|
---|
424 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
425 | int countOfPaneContents = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
426 | panes[i].IndexContents = new int[countOfPaneContents];
|
---|
427 | MoveToNextElement(xmlIn);
|
---|
428 | for (int j = 0; j < countOfPaneContents; j++)
|
---|
429 | {
|
---|
430 | int id2 = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
431 | if (xmlIn.Name != "Content" || id2 != j)
|
---|
432 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
433 |
|
---|
434 | panes[i].IndexContents[j] = Convert.ToInt32(xmlIn.GetAttribute("RefID"), CultureInfo.InvariantCulture);
|
---|
435 | MoveToNextElement(xmlIn);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | return panes;
|
---|
440 | }
|
---|
441 |
|
---|
442 | private static DockWindowStruct[] LoadDockWindows(XmlTextReader xmlIn, DockPanel dockPanel)
|
---|
443 | {
|
---|
444 | EnumConverter dockStateConverter = new EnumConverter(typeof(DockState));
|
---|
445 | EnumConverter dockAlignmentConverter = new EnumConverter(typeof(DockAlignment));
|
---|
446 | int countOfDockWindows = dockPanel.DockWindows.Count;
|
---|
447 | DockWindowStruct[] dockWindows = new DockWindowStruct[countOfDockWindows];
|
---|
448 | MoveToNextElement(xmlIn);
|
---|
449 | for (int i = 0; i < countOfDockWindows; i++)
|
---|
450 | {
|
---|
451 | int id = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
452 | if (xmlIn.Name != "DockWindow" || id != i)
|
---|
453 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
454 |
|
---|
455 | dockWindows[i].DockState = (DockState)dockStateConverter.ConvertFrom(xmlIn.GetAttribute("DockState"));
|
---|
456 | dockWindows[i].ZOrderIndex = Convert.ToInt32(xmlIn.GetAttribute("ZOrderIndex"), CultureInfo.InvariantCulture);
|
---|
457 | MoveToNextElement(xmlIn);
|
---|
458 | if (xmlIn.Name != "DockList" && xmlIn.Name != "NestedPanes")
|
---|
459 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
460 | int countOfNestedPanes = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
461 | dockWindows[i].NestedPanes = new NestedPane[countOfNestedPanes];
|
---|
462 | MoveToNextElement(xmlIn);
|
---|
463 | for (int j = 0; j < countOfNestedPanes; j++)
|
---|
464 | {
|
---|
465 | int id2 = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
466 | if (xmlIn.Name != "Pane" || id2 != j)
|
---|
467 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
468 | dockWindows[i].NestedPanes[j].IndexPane = Convert.ToInt32(xmlIn.GetAttribute("RefID"), CultureInfo.InvariantCulture);
|
---|
469 | dockWindows[i].NestedPanes[j].IndexPrevPane = Convert.ToInt32(xmlIn.GetAttribute("PrevPane"), CultureInfo.InvariantCulture);
|
---|
470 | dockWindows[i].NestedPanes[j].Alignment = (DockAlignment)dockAlignmentConverter.ConvertFrom(xmlIn.GetAttribute("Alignment"));
|
---|
471 | dockWindows[i].NestedPanes[j].Proportion = Convert.ToDouble(xmlIn.GetAttribute("Proportion"), CultureInfo.InvariantCulture);
|
---|
472 | MoveToNextElement(xmlIn);
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | return dockWindows;
|
---|
477 | }
|
---|
478 |
|
---|
479 | private static FloatWindowStruct[] LoadFloatWindows(XmlTextReader xmlIn)
|
---|
480 | {
|
---|
481 | EnumConverter dockAlignmentConverter = new EnumConverter(typeof(DockAlignment));
|
---|
482 | RectangleConverter rectConverter = new RectangleConverter();
|
---|
483 | int countOfFloatWindows = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
484 | FloatWindowStruct[] floatWindows = new FloatWindowStruct[countOfFloatWindows];
|
---|
485 | MoveToNextElement(xmlIn);
|
---|
486 | for (int i = 0; i < countOfFloatWindows; i++)
|
---|
487 | {
|
---|
488 | int id = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
489 | if (xmlIn.Name != "FloatWindow" || id != i)
|
---|
490 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
491 |
|
---|
492 | floatWindows[i].Bounds = (Rectangle)rectConverter.ConvertFromInvariantString(xmlIn.GetAttribute("Bounds"));
|
---|
493 | floatWindows[i].ZOrderIndex = Convert.ToInt32(xmlIn.GetAttribute("ZOrderIndex"), CultureInfo.InvariantCulture);
|
---|
494 | MoveToNextElement(xmlIn);
|
---|
495 | if (xmlIn.Name != "DockList" && xmlIn.Name != "NestedPanes")
|
---|
496 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
497 | int countOfNestedPanes = Convert.ToInt32(xmlIn.GetAttribute("Count"), CultureInfo.InvariantCulture);
|
---|
498 | floatWindows[i].NestedPanes = new NestedPane[countOfNestedPanes];
|
---|
499 | MoveToNextElement(xmlIn);
|
---|
500 | for (int j = 0; j < countOfNestedPanes; j++)
|
---|
501 | {
|
---|
502 | int id2 = Convert.ToInt32(xmlIn.GetAttribute("ID"), CultureInfo.InvariantCulture);
|
---|
503 | if (xmlIn.Name != "Pane" || id2 != j)
|
---|
504 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
505 | floatWindows[i].NestedPanes[j].IndexPane = Convert.ToInt32(xmlIn.GetAttribute("RefID"), CultureInfo.InvariantCulture);
|
---|
506 | floatWindows[i].NestedPanes[j].IndexPrevPane = Convert.ToInt32(xmlIn.GetAttribute("PrevPane"), CultureInfo.InvariantCulture);
|
---|
507 | floatWindows[i].NestedPanes[j].Alignment = (DockAlignment)dockAlignmentConverter.ConvertFrom(xmlIn.GetAttribute("Alignment"));
|
---|
508 | floatWindows[i].NestedPanes[j].Proportion = Convert.ToDouble(xmlIn.GetAttribute("Proportion"), CultureInfo.InvariantCulture);
|
---|
509 | MoveToNextElement(xmlIn);
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | return floatWindows;
|
---|
514 | }
|
---|
515 |
|
---|
516 | public static void LoadFromXml(DockPanel dockPanel, Stream stream, DeserializeDockContent deserializeContent, bool closeStream)
|
---|
517 | {
|
---|
518 |
|
---|
519 | if (dockPanel.Contents.Count != 0)
|
---|
520 | throw new InvalidOperationException(Strings.DockPanel_LoadFromXml_AlreadyInitialized);
|
---|
521 |
|
---|
522 | XmlTextReader xmlIn = new XmlTextReader(stream);
|
---|
523 | xmlIn.WhitespaceHandling = WhitespaceHandling.None;
|
---|
524 | xmlIn.MoveToContent();
|
---|
525 |
|
---|
526 | while (!xmlIn.Name.Equals("DockPanel"))
|
---|
527 | {
|
---|
528 | if (!MoveToNextElement(xmlIn))
|
---|
529 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
530 | }
|
---|
531 |
|
---|
532 | string formatVersion = xmlIn.GetAttribute("FormatVersion");
|
---|
533 | if (!IsFormatVersionValid(formatVersion))
|
---|
534 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidFormatVersion);
|
---|
535 |
|
---|
536 | DockPanelStruct dockPanelStruct = new DockPanelStruct();
|
---|
537 | dockPanelStruct.DockLeftPortion = Convert.ToDouble(xmlIn.GetAttribute("DockLeftPortion"), CultureInfo.InvariantCulture);
|
---|
538 | dockPanelStruct.DockRightPortion = Convert.ToDouble(xmlIn.GetAttribute("DockRightPortion"), CultureInfo.InvariantCulture);
|
---|
539 | dockPanelStruct.DockTopPortion = Convert.ToDouble(xmlIn.GetAttribute("DockTopPortion"), CultureInfo.InvariantCulture);
|
---|
540 | dockPanelStruct.DockBottomPortion = Convert.ToDouble(xmlIn.GetAttribute("DockBottomPortion"), CultureInfo.InvariantCulture);
|
---|
541 | dockPanelStruct.IndexActiveDocumentPane = Convert.ToInt32(xmlIn.GetAttribute("ActiveDocumentPane"), CultureInfo.InvariantCulture);
|
---|
542 | dockPanelStruct.IndexActivePane = Convert.ToInt32(xmlIn.GetAttribute("ActivePane"), CultureInfo.InvariantCulture);
|
---|
543 |
|
---|
544 | // Load Contents
|
---|
545 | MoveToNextElement(xmlIn);
|
---|
546 | if (xmlIn.Name != "Contents")
|
---|
547 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
548 | ContentStruct[] contents = LoadContents(xmlIn);
|
---|
549 |
|
---|
550 | // Load Panes
|
---|
551 | if (xmlIn.Name != "Panes")
|
---|
552 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
553 | PaneStruct[] panes = LoadPanes(xmlIn);
|
---|
554 |
|
---|
555 | // Load DockWindows
|
---|
556 | if (xmlIn.Name != "DockWindows")
|
---|
557 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
558 | DockWindowStruct[] dockWindows = LoadDockWindows(xmlIn, dockPanel);
|
---|
559 |
|
---|
560 | // Load FloatWindows
|
---|
561 | if (xmlIn.Name != "FloatWindows")
|
---|
562 | throw new ArgumentException(Strings.DockPanel_LoadFromXml_InvalidXmlFormat);
|
---|
563 | FloatWindowStruct[] floatWindows = LoadFloatWindows(xmlIn);
|
---|
564 |
|
---|
565 | if (closeStream)
|
---|
566 | xmlIn.Close();
|
---|
567 |
|
---|
568 | dockPanel.SuspendLayout(true);
|
---|
569 |
|
---|
570 | dockPanel.DockLeftPortion = dockPanelStruct.DockLeftPortion;
|
---|
571 | dockPanel.DockRightPortion = dockPanelStruct.DockRightPortion;
|
---|
572 | dockPanel.DockTopPortion = dockPanelStruct.DockTopPortion;
|
---|
573 | dockPanel.DockBottomPortion = dockPanelStruct.DockBottomPortion;
|
---|
574 |
|
---|
575 | // Set DockWindow ZOrders
|
---|
576 | int prevMaxDockWindowZOrder = int.MaxValue;
|
---|
577 | for (int i = 0; i < dockWindows.Length; i++)
|
---|
578 | {
|
---|
579 | int maxDockWindowZOrder = -1;
|
---|
580 | int index = -1;
|
---|
581 | for (int j = 0; j < dockWindows.Length; j++)
|
---|
582 | {
|
---|
583 | if (dockWindows[j].ZOrderIndex > maxDockWindowZOrder && dockWindows[j].ZOrderIndex < prevMaxDockWindowZOrder)
|
---|
584 | {
|
---|
585 | maxDockWindowZOrder = dockWindows[j].ZOrderIndex;
|
---|
586 | index = j;
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | dockPanel.DockWindows[dockWindows[index].DockState].BringToFront();
|
---|
591 | prevMaxDockWindowZOrder = maxDockWindowZOrder;
|
---|
592 | }
|
---|
593 |
|
---|
594 | // Create Contents
|
---|
595 | for (int i = 0; i < contents.Length; i++)
|
---|
596 | {
|
---|
597 | IDockContent content = deserializeContent(contents[i].PersistString);
|
---|
598 | if (content == null)
|
---|
599 | content = new DummyContent();
|
---|
600 | content.DockHandler.DockPanel = dockPanel;
|
---|
601 | content.DockHandler.AutoHidePortion = contents[i].AutoHidePortion;
|
---|
602 | content.DockHandler.IsHidden = true;
|
---|
603 | content.DockHandler.IsFloat = contents[i].IsFloat;
|
---|
604 | }
|
---|
605 |
|
---|
606 | // Create panes
|
---|
607 | for (int i = 0; i < panes.Length; i++)
|
---|
608 | {
|
---|
609 | DockPane pane = null;
|
---|
610 | for (int j = 0; j < panes[i].IndexContents.Length; j++)
|
---|
611 | {
|
---|
612 | IDockContent content = dockPanel.Contents[panes[i].IndexContents[j]];
|
---|
613 | if (j == 0)
|
---|
614 | pane = dockPanel.DockPaneFactory.CreateDockPane(content, panes[i].DockState, false);
|
---|
615 | else if (panes[i].DockState == DockState.Float)
|
---|
616 | content.DockHandler.FloatPane = pane;
|
---|
617 | else
|
---|
618 | content.DockHandler.PanelPane = pane;
|
---|
619 | }
|
---|
620 | }
|
---|
621 |
|
---|
622 | // Assign Panes to DockWindows
|
---|
623 | for (int i = 0; i < dockWindows.Length; i++)
|
---|
624 | {
|
---|
625 | for (int j = 0; j < dockWindows[i].NestedPanes.Length; j++)
|
---|
626 | {
|
---|
627 | DockWindow dw = dockPanel.DockWindows[dockWindows[i].DockState];
|
---|
628 | int indexPane = dockWindows[i].NestedPanes[j].IndexPane;
|
---|
629 | DockPane pane = dockPanel.Panes[indexPane];
|
---|
630 | int indexPrevPane = dockWindows[i].NestedPanes[j].IndexPrevPane;
|
---|
631 | DockPane prevPane = (indexPrevPane == -1) ? dw.NestedPanes.GetDefaultPreviousPane(pane) : dockPanel.Panes[indexPrevPane];
|
---|
632 | DockAlignment alignment = dockWindows[i].NestedPanes[j].Alignment;
|
---|
633 | double proportion = dockWindows[i].NestedPanes[j].Proportion;
|
---|
634 | pane.DockTo(dw, prevPane, alignment, proportion);
|
---|
635 | if (panes[indexPane].DockState == dw.DockState)
|
---|
636 | panes[indexPane].ZOrderIndex = dockWindows[i].ZOrderIndex;
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|
640 | // Create float windows
|
---|
641 | for (int i = 0; i < floatWindows.Length; i++)
|
---|
642 | {
|
---|
643 | FloatWindow fw = null;
|
---|
644 | for (int j = 0; j < floatWindows[i].NestedPanes.Length; j++)
|
---|
645 | {
|
---|
646 | int indexPane = floatWindows[i].NestedPanes[j].IndexPane;
|
---|
647 | DockPane pane = dockPanel.Panes[indexPane];
|
---|
648 | if (j == 0)
|
---|
649 | fw = dockPanel.FloatWindowFactory.CreateFloatWindow(dockPanel, pane, floatWindows[i].Bounds);
|
---|
650 | else
|
---|
651 | {
|
---|
652 | int indexPrevPane = floatWindows[i].NestedPanes[j].IndexPrevPane;
|
---|
653 | DockPane prevPane = indexPrevPane == -1 ? null : dockPanel.Panes[indexPrevPane];
|
---|
654 | DockAlignment alignment = floatWindows[i].NestedPanes[j].Alignment;
|
---|
655 | double proportion = floatWindows[i].NestedPanes[j].Proportion;
|
---|
656 | pane.DockTo(fw, prevPane, alignment, proportion);
|
---|
657 | }
|
---|
658 |
|
---|
659 | if (panes[indexPane].DockState == fw.DockState)
|
---|
660 | panes[indexPane].ZOrderIndex = floatWindows[i].ZOrderIndex;
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | // sort IDockContent by its Pane's ZOrder
|
---|
665 | int[] sortedContents = null;
|
---|
666 | if (contents.Length > 0)
|
---|
667 | {
|
---|
668 | sortedContents = new int[contents.Length];
|
---|
669 | for (int i = 0; i < contents.Length; i++)
|
---|
670 | sortedContents[i] = i;
|
---|
671 |
|
---|
672 | int lastDocument = contents.Length;
|
---|
673 | for (int i = 0; i < contents.Length - 1; i++)
|
---|
674 | {
|
---|
675 | for (int j = i + 1; j < contents.Length; j++)
|
---|
676 | {
|
---|
677 | DockPane pane1 = dockPanel.Contents[sortedContents[i]].DockHandler.Pane;
|
---|
678 | int ZOrderIndex1 = pane1 == null ? 0 : panes[dockPanel.Panes.IndexOf(pane1)].ZOrderIndex;
|
---|
679 | DockPane pane2 = dockPanel.Contents[sortedContents[j]].DockHandler.Pane;
|
---|
680 | int ZOrderIndex2 = pane2 == null ? 0 : panes[dockPanel.Panes.IndexOf(pane2)].ZOrderIndex;
|
---|
681 | if (ZOrderIndex1 > ZOrderIndex2)
|
---|
682 | {
|
---|
683 | int temp = sortedContents[i];
|
---|
684 | sortedContents[i] = sortedContents[j];
|
---|
685 | sortedContents[j] = temp;
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | // show non-document IDockContent first to avoid screen flickers
|
---|
692 | for (int i = 0; i < contents.Length; i++)
|
---|
693 | {
|
---|
694 | IDockContent content = dockPanel.Contents[sortedContents[i]];
|
---|
695 | if (content.DockHandler.Pane != null && content.DockHandler.Pane.DockState != DockState.Document)
|
---|
696 | content.DockHandler.IsHidden = contents[sortedContents[i]].IsHidden;
|
---|
697 | }
|
---|
698 |
|
---|
699 | // after all non-document IDockContent, show document IDockContent
|
---|
700 | for (int i = 0; i < contents.Length; i++)
|
---|
701 | {
|
---|
702 | IDockContent content = dockPanel.Contents[sortedContents[i]];
|
---|
703 | if (content.DockHandler.Pane != null && content.DockHandler.Pane.DockState == DockState.Document)
|
---|
704 | content.DockHandler.IsHidden = contents[sortedContents[i]].IsHidden;
|
---|
705 | }
|
---|
706 |
|
---|
707 | for (int i = 0; i < panes.Length; i++)
|
---|
708 | dockPanel.Panes[i].ActiveContent = panes[i].IndexActiveContent == -1 ? null : dockPanel.Contents[panes[i].IndexActiveContent];
|
---|
709 |
|
---|
710 | if (dockPanelStruct.IndexActiveDocumentPane != -1)
|
---|
711 | dockPanel.Panes[dockPanelStruct.IndexActiveDocumentPane].Activate();
|
---|
712 |
|
---|
713 | if (dockPanelStruct.IndexActivePane != -1)
|
---|
714 | dockPanel.Panes[dockPanelStruct.IndexActivePane].Activate();
|
---|
715 |
|
---|
716 | for (int i = dockPanel.Contents.Count - 1; i >= 0; i--)
|
---|
717 | if (dockPanel.Contents[i] is DummyContent)
|
---|
718 | dockPanel.Contents[i].DockHandler.Form.Close();
|
---|
719 |
|
---|
720 | dockPanel.ResumeLayout(true, true);
|
---|
721 | }
|
---|
722 |
|
---|
723 | private static bool MoveToNextElement(XmlTextReader xmlIn)
|
---|
724 | {
|
---|
725 | if (!xmlIn.Read())
|
---|
726 | return false;
|
---|
727 |
|
---|
728 | while (xmlIn.NodeType == XmlNodeType.EndElement)
|
---|
729 | {
|
---|
730 | if (!xmlIn.Read())
|
---|
731 | return false;
|
---|
732 | }
|
---|
733 |
|
---|
734 | return true;
|
---|
735 | }
|
---|
736 |
|
---|
737 | private static bool IsFormatVersionValid(string formatVersion)
|
---|
738 | {
|
---|
739 | if (formatVersion == ConfigFileVersion)
|
---|
740 | return true;
|
---|
741 |
|
---|
742 | foreach (string s in CompatibleConfigFileVersions)
|
---|
743 | if (s == formatVersion)
|
---|
744 | return true;
|
---|
745 |
|
---|
746 | return false;
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 | public void SaveAsXml(string fileName)
|
---|
751 | {
|
---|
752 | Persistor.SaveAsXml(this, fileName);
|
---|
753 | }
|
---|
754 |
|
---|
755 | public void SaveAsXml(string fileName, Encoding encoding)
|
---|
756 | {
|
---|
757 | Persistor.SaveAsXml(this, fileName, encoding);
|
---|
758 | }
|
---|
759 |
|
---|
760 | public void SaveAsXml(Stream stream, Encoding encoding)
|
---|
761 | {
|
---|
762 | Persistor.SaveAsXml(this, stream, encoding);
|
---|
763 | }
|
---|
764 |
|
---|
765 | public void SaveAsXml(Stream stream, Encoding encoding, bool upstream)
|
---|
766 | {
|
---|
767 | Persistor.SaveAsXml(this, stream, encoding, upstream);
|
---|
768 | }
|
---|
769 |
|
---|
770 | public void LoadFromXml(string fileName, DeserializeDockContent deserializeContent)
|
---|
771 | {
|
---|
772 | Persistor.LoadFromXml(this, fileName, deserializeContent);
|
---|
773 | }
|
---|
774 |
|
---|
775 | public void LoadFromXml(Stream stream, DeserializeDockContent deserializeContent)
|
---|
776 | {
|
---|
777 | Persistor.LoadFromXml(this, stream, deserializeContent);
|
---|
778 | }
|
---|
779 |
|
---|
780 | public void LoadFromXml(Stream stream, DeserializeDockContent deserializeContent, bool closeStream)
|
---|
781 | {
|
---|
782 | Persistor.LoadFromXml(this, stream, deserializeContent, closeStream);
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|