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

📄 formdesignerdisplaybindingbase.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krueger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Printing;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Xml;
using System.ComponentModel.Design.Serialization;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Internal.Undo;
using ICSharpCode.SharpDevelop.Gui.Components;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using SharpDevelop.Internal.Parser;

using ICSharpCode.Core.Properties;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;

using ICSharpCode.SharpDevelop.FormDesigner.Services;
using ICSharpCode.SharpDevelop.FormDesigner.Hosts;
using ICSharpCode.SharpDevelop.FormDesigner.Util;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.TextEditor;

using System.CodeDom;
using System.CodeDom.Compiler;

using Microsoft.CSharp;
using Microsoft.VisualBasic;
using ICSharpCode.SharpDevelop.Gui.Pads;

using ICSharpCode.SharpDevelop.Gui.ErrorDialogs;
using ICSharpCode.SharpDevelop.FormDesigner.Gui;

using ICSharpCode.SharpDevelop.Gui.Dialogs.OptionPanels;


namespace ICSharpCode.SharpDevelop.FormDesigner
{
	public abstract class FormDesignerDisplayBindingBase : AbstractViewContent, IEditable, IClipboardHandler, IHelpProvider, ITabOrder
	{
		protected DesignPanel         designPanel       = null;
		protected DefaultDesignerHost host;
		protected bool                isFormDesignerVisible;
		protected UndoHandler         undoHandler       = new UndoHandler();
		AmbientProperties             ambientProperties = new AmbientProperties();
		
		protected Hashtable           resources = null;

		public virtual TextAreaControl TextAreaControl {
			get {
				return null;
			}
		}
		
		public bool IsFormDesignerVisible {
			get {
				return isFormDesignerVisible;
			}
		}
		
		// IEditable
		public virtual IClipboardHandler ClipboardHandler {
			get {
				return this;
			}
		}
		
		public virtual string Text {
			get {
				return null;
			}
			set {
			}
		}
		
		public IDesignerHost DesignerHost {
			get {
				return host;
			}
		}
		
		public bool EnableUndo {
			get {
				return undoHandler.EnableUndo;
			}
		}
		public bool EnableRedo {
			get {
				return undoHandler.EnableRedo;
			}
		}
		
		public bool EnableCut {
			get {
				if (host == null) {
					return false;
				}
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				return selectionService.SelectionCount >= 0 && selectionService.PrimarySelection != host.RootComponent;
			}
		}
		
		public bool EnableCopy {
			get {
				if (host == null) {
					return false;
				}
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				return selectionService.SelectionCount >= 0 && selectionService.PrimarySelection != host.RootComponent;
			}
		}
		
		const string ComponentClipboardFormat = "CF_DESIGNERCOMPONENTS";
		public bool EnablePaste {
			get {
				if (host == null) {
					return false;
				}
				// Clipboard.GetDataObject may throw an exception...
				try {
					IDataObject data = Clipboard.GetDataObject();
					return data != null && data.GetDataPresent(ComponentClipboardFormat);
				} catch (Exception e) {
					Console.WriteLine("Got exception while enablepaste : " + e);
					return false;
				}
			}
		}
		
		public bool EnableDelete {
			get {
				if (host == null) {
					return false;
				}
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				return selectionService.SelectionCount >= 0 && selectionService.PrimarySelection != host.RootComponent;
			}
		}
		
		public bool EnableSelectAll {
			get {
				if (host == null) {
					return false;
				}
				return true;
			}
		}
		
		public override string TabPageText {
			get {
				return "${res:FormsDesigner.DesignTabPages.DesignTabPage}";
			}
		}
		
		// AbstractViewContent members
		protected override void OnWorkbenchWindowChanged(EventArgs e)
		{
			base.OnWorkbenchWindowChanged(e);
			if (WorkbenchWindow != null) {
				WorkbenchWindow.WindowSelected   += new EventHandler(SelectMe);
				WorkbenchWindow.WindowDeselected += new EventHandler(DeSelectMe);
			}
		}
		
		int lastSideTab    = Int32.MaxValue;
		int defaultSideTab = 0;
		protected virtual void SelectMe(object sender, EventArgs e)
		{
			if (!isFormDesignerVisible || host == null) {
				return;
			}
			defaultSideTab  = SharpDevelopSideBar.SideBar.Tabs.IndexOf(SharpDevelopSideBar.SideBar.ActiveTab);
			foreach(AxSideTab tab in ToolboxProvider.SideTabs) {
				if (!SharpDevelopSideBar.SideBar.Tabs.Contains(tab)) {
					SharpDevelopSideBar.SideBar.Tabs.Add(tab);
				}
			}
			if (ToolboxProvider.SideTabs.Count > 0) {
				lastSideTab = Math.Max(Math.Min(lastSideTab, SharpDevelopSideBar.SideBar.Tabs.Count - 1), 0);
				SharpDevelopSideBar.SideBar.ActiveTab = (AxSideTab)SharpDevelopSideBar.SideBar.Tabs[lastSideTab];
			}
//			SharpDevelopSideBar.SideBar.ActiveTab = tab;
			
			ICSharpCode.SharpDevelop.Gui.Pads.PropertyPad.SetDesignerHost(host);
			
			// set old selection
			ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
			if (oldSelectedComponents != null) {
				ArrayList newSelection = new ArrayList();
				foreach (string name in oldSelectedComponents) {
					newSelection.Add(host.Container.Components[name]);
				}
				selectionService.SetSelectedComponents(newSelection);
				oldSelectedComponents = null;
			}
			
			if (selectionService.SelectionCount == 0) {
				SelectRootComponent();
			}
			
			UpdateSelectableObjects();
			designPanel.Enable();
		}
		ArrayList oldSelectedComponents = null;
		
		protected virtual void DeSelectMe(object sender, EventArgs e)
		{
			// host == null is for the case when designer is disposed and deselect is called.
			if (oldSelectedComponents != null || host == null) {
				return;
			}
			try {
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				oldSelectedComponents = new ArrayList();
				foreach (IComponent component in selectionService.GetSelectedComponents()) {
					if (component.Site != null) {
						oldSelectedComponents.Add(component.Site.Name);
					}
				}
				
				ICSharpCode.SharpDevelop.Gui.Pads.PropertyPad.SetDesignableObject(null);
				ICSharpCode.SharpDevelop.Gui.Pads.PropertyPad.SetSelectableObjects(null);
				int oldTab = SharpDevelopSideBar.SideBar.Tabs.IndexOf(SharpDevelopSideBar.SideBar.ActiveTab);
				
				foreach(AxSideTab tab in ToolboxProvider.SideTabs) {
					if (!SharpDevelopSideBar.SideBar.Tabs.Contains(tab)) {
						return;
					}
					SharpDevelopSideBar.SideBar.Tabs.Remove(tab);
				}
				lastSideTab = oldTab;
				defaultSideTab = Math.Max(Math.Min(defaultSideTab, SharpDevelopSideBar.SideBar.Tabs.Count - 1), 0);
				SharpDevelopSideBar.SideBar.ActiveTab = (AxSideTab)SharpDevelopSideBar.SideBar.Tabs[defaultSideTab];
	//			SharpDevelopSideBar.SideBar.Tabs.Remove(tab);
				designPanel.Disable();
				ICSharpCode.SharpDevelop.Gui.Pads.PropertyPad.RemoveHost(host);
			} catch (Exception ex) {
				Console.WriteLine("Got exception while deselection:" + ex);
			}
		}
		
		public override bool IsReadOnly {
			get {
				return false; // FIXME
			}
		}
		
		public void ReloadAndSelect()
		{
			Reload();
 
			// Select only active window
			if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null && WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ActiveViewContent == this) {
				UpdateSelectableObjects();
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				if (selectionService.SelectionCount == 0) 
					SelectRootComponent();
			}			
		}


		public virtual void Reload()
		{
		}
		
		public override void RedrawContent()
		{
//			((DefaultDesignerHost)DesignerHost).Reload();
		}
		
		public override void Dispose()
		{
			if (designPanel != null) {
				designPanel.Dispose();
			}
			undoHandler.Reset();
			if (host != null) {
				ICSharpCode.SharpDevelop.Gui.Pads.PropertyPad.RemoveHost(host);
				host.TransactionClosed -= new DesignerTransactionCloseEventHandler(TransactionFinished);
				
				ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
				selectionService.SelectionChanged -= new EventHandler(SelectionServiceSelectionChanged);
				undoHandler.Detach();
				host.Deactivate();
				host.Dispose();
			}
			designPanel       = null;
			host              = null;
			if (this.resources != null) {
				foreach(DesignerResourceService.ResourceStorage storage in this.resources.Values){
					storage.Dispose();
				}
				resources.Clear();
			}
			resources         = null;
			ambientProperties = null;
		}
		
		protected string GetDataAs(string what)
		{
			StringWriter writer = new StringWriter();
			switch (what) {
				case "XML":
					XmlElement el = new XmlFormGenerator().GetElementFor(new XmlDocument(), host);
					
					XmlDocument doc = new XmlDocument();
					doc.LoadXml("<" + el.Name + " version=\"1.0\"/>");
					
					foreach (XmlNode node in el.ChildNodes) {
						doc.DocumentElement.AppendChild(doc.ImportNode(node, true));
					}
					
					doc.Save(writer);
					break;
				case "C#":
					new CodeDOMGenerator(host, new Microsoft.CSharp.CSharpCodeProvider()).ConvertContentDefinition(writer);
					break;
				case "VB.NET":
					new CodeDOMGenerator(host, new Microsoft.VisualBasic.VBCodeProvider()).ConvertContentDefinition(writer);
					break;
			}
			return writer.ToString();
		}
		
		public override void Save(string fileName)
		{
			FileName = fileName;
			TitleName = Path.GetFileName(fileName);
			XmlElement el = new XmlFormGenerator().GetElementFor(new XmlDocument(), host);
			
			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<" + el.Name + " version=\"1.0\"/>");
			
			foreach (XmlNode node in el.ChildNodes) {
				doc.DocumentElement.AppendChild(doc.ImportNode(node, true));

⌨️ 快捷键说明

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