⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restore.cs

📁 Magic Library 1.7,有说明文档
💻 CS
📖 第 1 页 / 共 2 页
字号:
// *****************************************************************************
// 
//  (c) Crownwood Consulting Limited 2002 
//  All rights reserved. The software and associated documentation 
//  supplied hereunder are the proprietary information of Crownwood Consulting 
//	Limited, Haxey, North Lincolnshire, England and are supplied subject to 
//	licence terms.
// 
//  Magic Version 1.7 	www.dotnetmagic.com
// *****************************************************************************

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using Crownwood.Magic.Common;
using Crownwood.Magic.Docking;
using Crownwood.Magic.Collections;

namespace Crownwood.Magic.Docking
{
    public class Restore
    {
		// Instance fields
		protected Restore _child;

		public Restore()
		{
			// Default state
			_child = null;
		}

		public Restore(Restore child)
		{
			// Remember parameter
			_child = child;
		}

        public Restore Child
        {
            get { return _child; }
            set { _child = value; }
        }

        public virtual void PerformRestore(DockingManager dm) {}
		public virtual void PerformRestore(Window w) {}
        public virtual void PerformRestore(Zone z) {}
        public virtual void PerformRestore() {}

		public virtual void Reconnect(DockingManager dm)
		{
			if (_child != null)
				_child.Reconnect(dm);
		}

		public virtual void SaveToXml(XmlTextWriter xmlOut)
		{
			// Must define my type so loading can recreate my instance
			xmlOut.WriteAttributeString("Type", this.GetType().ToString());

			SaveInternalToXml(xmlOut);

			// Output the child object			
			xmlOut.WriteStartElement("Child");

			if (_child == null)
				xmlOut.WriteAttributeString("Type", "null");
			else
				_child.SaveToXml(xmlOut);

			xmlOut.WriteEndElement();
		}

		public virtual void LoadFromXml(XmlTextReader xmlIn, int formatVersion)
		{
			LoadInternalFromXml(xmlIn, formatVersion);

			// Move to next xml node
			if (!xmlIn.Read())
				throw new ArgumentException("Could not read in next expected node");

			// Check it has the expected name
			if (xmlIn.Name != "Child")
				throw new ArgumentException("Node 'Child' expected but not found");

			string type = xmlIn.GetAttribute(0);
			
			if (type != "null")
				_child = CreateFromXml(xmlIn, false, formatVersion);

			// Move past the end element
			if (!xmlIn.Read())
				throw new ArgumentException("Could not read in next expected node");
		
			// Check it has the expected name
			if (xmlIn.NodeType != XmlNodeType.EndElement)
				throw new ArgumentException("EndElement expected but not found");
		}

		public virtual void SaveInternalToXml(XmlTextWriter xmlOut) {}
		public virtual void LoadInternalFromXml(XmlTextReader xmlIn, int formatVersion) {}

		public static Restore CreateFromXml(XmlTextReader xmlIn, bool readIn, int formatVersion)
		{
			if (readIn)
			{
				// Move to next xml node
				if (!xmlIn.Read())
					throw new ArgumentException("Could not read in next expected node");
			}
			
			// Grab type name of the object to create
			string attrType = xmlIn.GetAttribute(0);

			// Convert from string to a Type description object
			Type newType = Type.GetType(attrType);

			// Create an instance of this object which must derive from Restore base class
			Restore newRestore = newType.Assembly.CreateInstance(attrType) as Restore;

			// Ask the object to load itself
			newRestore.LoadFromXml(xmlIn, formatVersion);

			return newRestore;
		}
	}

	public class RestoreContent : Restore
	{
		// Instance fields
		protected String _title;
		protected Content _content;

		public RestoreContent()
			: base()
		{
			// Default state
			_title = "";
			_content = null;
		}

		public RestoreContent(Content content)
			: base()
		{
			// Remember parameter
			_title = content.Title;
			_content = content;
		}

		public RestoreContent(Restore child, Content content)
			: base(child)
		{
			// Remember parameter
			_title = content.Title;
			_content = content;
		}

		public override void Reconnect(DockingManager dm)
		{
			// Connect to the current instance of required content object
			_content = dm.Contents[_title];

			base.Reconnect(dm);
		}

		public override void SaveInternalToXml(XmlTextWriter xmlOut)
		{
			base.SaveInternalToXml(xmlOut);
			xmlOut.WriteStartElement("Content");
			xmlOut.WriteAttributeString("Name", _content.Title);
			xmlOut.WriteEndElement();				
		}

		public override void LoadInternalFromXml(XmlTextReader xmlIn, int formatVersion)
		{
			base.LoadInternalFromXml(xmlIn, formatVersion);

			// Move to next xml node
			if (!xmlIn.Read())
				throw new ArgumentException("Could not read in next expected node");

			// Check it has the expected name
			if (xmlIn.Name != "Content")
				throw new ArgumentException("Node 'Content' expected but not found");

			// Grab type name of the object to create
			_title = xmlIn.GetAttribute(0);
		}
	}
	
	public class RestoreContentState : RestoreContent
	{
		// Instance fields
		protected State _state;

		public RestoreContentState()
			: base()
		{
		}

		public RestoreContentState(State state, Content content)
			: base(content)
		{
			// Remember parameter
			_state = state;
		}

		public RestoreContentState(Restore child, State state, Content content)
			: base(child, content)
		{
			// Remember parameter
			_state = state;
		}

		public override void PerformRestore(DockingManager dm)
		{
			// Use the existing DockingManager method that will create a Window appropriate for 
			// this Content and then add a new Zone for hosting the Window. It will always place
			// the Zone at the inner most level
			dm.AddContentWithState(_content, _state);				
		}

		public override void SaveInternalToXml(XmlTextWriter xmlOut)
		{
			base.SaveInternalToXml(xmlOut);
			xmlOut.WriteStartElement("State");
			xmlOut.WriteAttributeString("Value", _state.ToString());
			xmlOut.WriteEndElement();				
		}

		public override void LoadInternalFromXml(XmlTextReader xmlIn, int formatVersion)
		{
			base.LoadInternalFromXml(xmlIn, formatVersion);

			// Move to next xml node
			if (!xmlIn.Read())
				throw new ArgumentException("Could not read in next expected node");

			// Check it has the expected name
			if (xmlIn.Name != "State")
				throw new ArgumentException("Node 'State' expected but not found");

			// Grab type state of the object to create
			string attrState = xmlIn.GetAttribute(0);

			// Convert from string to enumeration value
			_state = (State)Enum.Parse(typeof(State), attrState);
		}
	}
	
	public class RestoreAutoHideState : RestoreContentState
	{
	    // Instance fields
	    
	    public RestoreAutoHideState()
	        : base()
	    {
	    }
        
        public RestoreAutoHideState(State state, Content content)
            : base(state, content)
        {
        }

        public RestoreAutoHideState(Restore child, State state, Content content)
            : base(child, state, content)
        {
        }
    
        public override void PerformRestore(DockingManager dm)
        {
            // Create collection of Contents to auto hide
            ContentCollection cc = new ContentCollection();
            
            // In this case, there is only one
            cc.Add(_content);
        
            // Add to appropriate AutoHidePanel based on _state
            dm.AutoHideContents(cc, _state);
        }
    }

    public class RestoreAutoHideAffinity : RestoreAutoHideState
    {
        // Instance fields
        protected StringCollection _next;
        protected StringCollection _previous;
        protected StringCollection _nextAll;
        protected StringCollection _previousAll;

        public RestoreAutoHideAffinity()
            : base()
        {
            // Must always point to valid reference
            _next = new StringCollection();
            _previous = new StringCollection();
            _nextAll = new StringCollection();
            _previousAll = new StringCollection();
        }

        public RestoreAutoHideAffinity(Restore child, 
                                       State state,
                                       Content content, 
                                       StringCollection next,
                                       StringCollection previous,
                                       StringCollection nextAll,
                                       StringCollection previousAll)
        : base(child, state, content)
        {
            // Remember parameters
            _next = next;				
            _previous = previous;	
            _nextAll = nextAll;				
            _previousAll = previousAll;	
        }

        public override void PerformRestore(DockingManager dm)
        {   
            // Get the correct target panel from state
            AutoHidePanel ahp = dm.AutoHidePanelForState(_state);
            
            ahp.AddContent(_content, _next, _previous, _nextAll, _previousAll);
        }

        public override void SaveInternalToXml(XmlTextWriter xmlOut)
        {
            base.SaveInternalToXml(xmlOut);
            _next.SaveToXml("Next", xmlOut);
            _previous.SaveToXml("Previous", xmlOut);
            _nextAll.SaveToXml("NextAll", xmlOut);
            _previousAll.SaveToXml("PreviousAll", xmlOut);
        }

        public override void LoadInternalFromXml(XmlTextReader xmlIn, int formatVersion)
        {
            base.LoadInternalFromXml(xmlIn, formatVersion);
            _next.LoadFromXml("Next", xmlIn);
            _previous.LoadFromXml("Previous", xmlIn);
            _nextAll.LoadFromXml("NextAll", xmlIn);
            _previousAll.LoadFromXml("PreviousAll", xmlIn);
        }
    }

	public class RestoreContentDockingAffinity : RestoreContentState
	{
		// Instance fields
		protected Size _size;
		protected Point _location;
		protected StringCollection _best;
		protected StringCollection _next;
		protected StringCollection _previous;
		protected StringCollection _nextAll;
		protected StringCollection _previousAll;

		public RestoreContentDockingAffinity()
			: base()
		{
			// Must always point to valid reference
			_best = new StringCollection();
			_next = new StringCollection();
			_previous = new StringCollection();
			_nextAll = new StringCollection();
			_previousAll = new StringCollection();
		}

		public RestoreContentDockingAffinity(Restore child, 
										     State state, 
											 Content content, 
											 StringCollection best,
											 StringCollection next,
											 StringCollection previous,
											 StringCollection nextAll,
											 StringCollection previousAll)
			: base(child, state, content)
		{
			// Remember parameters
			_best = best;
			_next = next;
			_previous = previous;
			_nextAll = nextAll;
			_previousAll = previousAll;
			_size = content.DisplaySize;
			_location = content.DisplayLocation;
		}

		public override void PerformRestore(DockingManager dm)
		{
			int count = dm.Container.Controls.Count;

			int min = -1;
			int max = count;

			if (dm.InnerControl != null)
				min = dm.Container.Controls.IndexOf(dm.InnerControl);

			if (dm.OuterControl != null)
				max = dm.OuterControlIndex();

			int beforeIndex = -1;
			int afterIndex = max;
			int beforeAllIndex = -1;
			int afterAllIndex = max;

			// Create a collection of the Zones in the appropriate direction
			for(int index=0; index<count; index++)
			{
				Zone z = dm.Container.Controls[index] as Zone;

				if (z != null)
				{
					StringCollection sc = ZoneHelper.ContentNames(z);
					
					if (_state == z.State)
					{
						if (sc.Contains(_best))
						{
							// Can we delegate to a child Restore object
							if (_child != null)
								_child.PerformRestore(z);
							else
							{
								// Just add an appropriate Window to start of the Zone
								dm.AddContentToZone(_content, z, 0);
							}
							return;
						}

						// If the WindowContent contains a Content previous to the target
						if (sc.Contains(_previous))
						{
							if (index > beforeIndex)
								beforeIndex = index;
						}
						
						// If the WindowContent contains a Content next to the target
						if (sc.Contains(_next))
						{
							if (index < afterIndex)
								afterIndex = index;
						}
					}
					else
					{
						// If the WindowContent contains a Content previous to the target
						if (sc.Contains(_previousAll))
						{
							if (index > beforeAllIndex)
								beforeAllIndex = index;
						}
						
						// If the WindowContent contains a Content next to the target
						if (sc.Contains(_nextAll))
						{
							if (index < afterAllIndex)
								afterAllIndex = index;
						}
					}
				}
			}

			dm.Container.SuspendLayout();

			// Create a new Zone with correct State
			Zone newZ = dm.CreateZoneForContent(_state);

			// Restore the correct content size/location values
			_content.DisplaySize = _size;
			_content.DisplayLocation = _location;

			// Add an appropriate Window to start of the Zone
			dm.AddContentToZone(_content, newZ, 0);

			// Did we find a valid 'before' Zone?
			if (beforeIndex != -1)
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -