DockPanel.Persistor.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:38k
源码类别:

C#编程

开发平台:

C#

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