context: /*
   
   * Author:	Bob Limnor (info@limnor.com)
   * Project: Limnor Studio
   * Item:	Visual Expression Display and Edit
   * License: GNU General Public License v3.0
   
   */
  /*
   * All rights reserved by Longflow Enterprises Ltd
   * 
   * Formula Editor
   */
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Text;
  using System.Windows.Forms;
  using System.ComponentModel.Design;
  using System.ComponentModel.Design.Serialization;
  using System.Collections;
  using System.Xml;
  using System.CodeDom;
  using System.Reflection;
  using MathExp.RaisTypes;
  using VPL;
  
  namespace MathExp
  {
  	/// <summary>
  	/// the control holding the DesignerFrame for the root component (DigramViewer).
  	/// propery grid is used to assist the design.
  	/// it is supposed to be a self-contained designer for a group of linked math expressions.
  	/// </summary>
  	public partial class DiagramDesignerHolder : UserControl, IUnitTestOwner, IUndoHost
  	{
  		#region fields and constructors
  		private DesignSurface dsf;
  		private DiagramViewer root;
  		private DesignMessageFilter _msgFilter;
  		private ISelectionService selectionService;
  		private bool bLoading = false;
  		private ITestData _testData;
  		private System.Threading.Thread thTest;
  		private MathExpGroup result;
  		private bool _disableUndo;
  		private UndoEngine2 _undoEngine;
  		private IXmlCodeReader _reader;
  		private IXmlCodeWriter _writer;
  		//
  		const int IMG_Delete_Enable = 8;
  		const int IMG_Delete_Disable = 7;
  		const int IMG_Copy_Enable = 4;
  		const int IMG_Copy_Disable = 3;
  		const int IMG_Cut_Enable = 6;
  		const int IMG_Cut_Disable = 5;
  		const int IMG_Paste_Enable = 10;
  		const int IMG_Paste_Disable = 9;
  		const int IMG_UndoEnable = 13;
  		const int IMG_UndoDisable = 2;
  		const int IMG_RedoEnable = 11;
  		const int IMG_RedoDisable = 12;
  		//
  		public DiagramDesignerHolder()
  		{
  			InitializeComponent();
  			btOK.BringToFront();
  			btCancel.BringToFront();
  			_undoEngine = new UndoEngine2();
  			//
  			dsf = new DesignSurface(typeof(DiagramViewer));
  			Control control = dsf.View as Control; //DesignerFrame
  			splitContainer1.Panel2.Controls.Add(control);
  			control.Dock = DockStyle.Fill;
  			control.Visible = true;
  			splitContainer1.SplitterMoved += new SplitterEventHandler(splitContainer1_SplitterMoved);
  			this.Resize += new EventHandler(designview_Resize);
  			splitContainer1.SplitterWidth = 3;
  			//
  			IDesignerHost host = (IDesignerHost)dsf.GetService(typeof(IDesignerHost));
  			root = (DiagramViewer)host.RootComponent;
  			root.Dock = DockStyle.Fill;
  			root.AssignHolder(this);
  			//
  			host.AddService(typeof(PropertyGrid), propertyGrid1);
  			host.AddService(typeof(INameCreationService), new NameCreation());
  			//
  			selectionService = (ISelectionService)dsf.GetService(typeof(ISelectionService));
  			if (selectionService != null)
  			{
  				selectionService.SelectionChanged += new EventHandler(selectionService_SelectionChanged);
  			}
  			IComponentChangeService componentChangeService = (IComponentChangeService)dsf.GetService(typeof(IComponentChangeService));
  			componentChangeService.ComponentAdded += new ComponentEventHandler(componentChangeService_ComponentAdded);
  			componentChangeService.ComponentRemoving += new ComponentEventHandler(componentChangeService_ComponentRemoving);
  			componentChangeService.ComponentRemoved += new ComponentEventHandler(componentChangeService_ComponentRemoved);
  			componentChangeService.ComponentChanged += new ComponentChangedEventHandler(componentChangeService_ComponentChanged);
  			//
  			root.ControlRemoved += new ControlEventHandler(root_ControlRemoved);
  			//
  			_msgFilter = new DesignMessageFilter(dsf);
  			Application.AddMessageFilter(_msgFilter);
  			//
  			propertyGrid1.PropertyValueChanged += new PropertyValueChangedEventHandler(propertyGrid1_PropertyValueChanged);
  			propertyGrid1.HelpVisible = false;
  			propertyGrid1.PropertySort = PropertySort.Alphabetical;
  			propertyGrid1.SelectedObject = root;
  			//
  		}
  
  		void componentChangeService_ComponentChanged(object sender, ComponentChangedEventArgs e)
  		{
  			if (!_disableUndo)
  			{
  
  				if (e.OldValue != null)
  				{
  					Point pOld = (Point)e.OldValue;
  
  					if (e.NewValue != null)
  					{
  						Point pNew = (Point)e.NewValue;
  						IActiveDrawing c = e.Component as IActiveDrawing;
  						if (c != null)
  						{
  							UInt32 key = c.ActiveDrawingID;
  							UndoEntity entity = new UndoEntity(new PositionUndo(this, key, pOld), new PositionUndo(this, key, pNew));
  							AddUndoEntity(entity);
  						}
  					}
  				}
  			}
  		}
  
  		#endregion
  		#region form event handlers
  		void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
  		{
  			timerAdjustFrame.Enabled = false;
  			timerAdjustFrame.Enabled = true;
  		}
  		void designview_Resize(object sender, EventArgs e)
  		{
  			timerAdjustFrame.Enabled = false;
  			timerAdjustFrame.Enabled = true;
  		}
  		void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  		{
  			root.Changed = true;
  			bool iconChanged = false;
  			bool nameChanged = (e.ChangedItem.PropertyDescriptor.Name == RaisDataType.PROP_NAME);
  			bool isRoot = (propertyGrid1.SelectedObject is DiagramViewer);
  			bool isMathViewer = (propertyGrid1.SelectedObject is MathExpViewer);
  			GridItem gi = e.ChangedItem;
  			while (gi != null)
  			{
  				if (gi.Value is Image)
  				{
  				}
  				else if (gi.Value is EnumIconType)
  				{
  				}
  				else if (gi.Value is ObjectIconData)
  				{
  					iconChanged = true;
  					break;
  				}
  				gi = gi.Parent;
  			}
  			if (isRoot && nameChanged)
  			{
  				root.Name = (string)e.ChangedItem.Value;
  			}
  			if (iconChanged || (isRoot && nameChanged))
  			{
  				loadIconImage();
  			}
  			else if (isMathViewer)
  			{
  				if (e.ChangedItem.Value is Point || e.ChangedItem.Value is Font)
  				{
  					((MathExpViewer)propertyGrid1.SelectedObject).RefreshImage();
  				}
  			}
  			if (nameChanged)
  			{
  				root.Refresh();
  			}
  		}
  
  		void selectionService_SelectionChanged(object sender, EventArgs e)
  		{
  			if (selectionService != null)
  			{
  				ICollection selectedComponents = selectionService.GetSelectedComponents();
  				object[] comps = new object[selectedComponents.Count];
  				int i = 0;
  
  				foreach (Object o in selectedComponents)
  				{
  					comps[i] = o;
  					i++;
  				}
  				propertyGrid1.SelectedObjects = comps;
  				if (propertyGrid1.SelectedObject == null || propertyGrid1.SelectedObject == root)
  				{
  					btCopy.Enabled = false;
  					btCopy.ImageIndex = IMG_Copy_Disable;
  					btCut.Enabled = false;
  					btCut.ImageIndex = IMG_Cut_Disable;
  					btDelete.Enabled = false;
  					btDelete.ImageIndex = IMG_Delete_Disable;
  				}
  				else
  				{
  					btCopy.Enabled = true;
  					btCopy.ImageIndex = IMG_Copy_Enable;
  					btCut.Enabled = true;
  					btCut.ImageIndex = IMG_Cut_Enable;
  					btDelete.Enabled = true;
  					btDelete.ImageIndex = IMG_Delete_Enable;
  				}
  			}
  		}
  		public void OnClosing()
  		{
  			Application.RemoveMessageFilter(_msgFilter);
  		}
  		public void OnOK()
  		{
  
gt: 			result = root.Export();