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

📄 toolboxservice.cs

📁 SharpDevelop2.0.0 c#开发免费工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version>$Revision: 915 $</version>
// </file>

using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.ComponentModel.Design;
using ICSharpCode.Core;

namespace ICSharpCode.FormsDesigner.Services
{
	public delegate void ToolboxEventHandler(object sender, ToolboxEventArgs tea);
	
	public class ToolboxEventArgs : EventArgs
	{
		ToolboxItem   item     = null;
		string        category = null;
		IDesignerHost host     = null;
		
		public ToolboxEventArgs(ToolboxItem item, string category, IDesignerHost host)
		{
			this.item     = item;
			this.category = category;
			this.host     = host;
		}
		
		public ToolboxItem Item {
			get {
				return item;
			}
		}
		
		public string Category {
			get {
				return category;
			}
		}
		
		public IDesignerHost Host {
			get {
				return host;
			}
		}
	}
	
	/// <summary>
	/// Provides access to the toolbox in the development environment.
	/// </summary>
	/// <remarks>
	/// Provides designers with the ability to configure what tools
	/// are available on the toolbox.
	/// </remarks>
	public class ToolboxService : IToolboxService
	{
		static readonly string ALL_HOSTS      = "_all_hosts_";
		static readonly string ALL_CATEGORIES = "_all_categories_";
		
		IDictionary toolboxByCategory = new ListDictionary();
		IDictionary toolboxByHost     = new ListDictionary();
		ArrayList toolboxItems        = new ArrayList();
		
		IDictionary creators          = new HybridDictionary();
		IDictionary creatorsByHost    = new ListDictionary();
		
		string selectedCategory  = null;
		ToolboxItem selectedItem = null;
		
		// Constructor
		public ToolboxService()
		{
			IList list = new ArrayList();
			toolboxByCategory.Add(ALL_CATEGORIES, list);
			
			list = new ArrayList();
			toolboxByHost.Add(ALL_HOSTS, list);
		}
		
		// Properties
		
		/// <summary>
		/// Gets the names of all the tool categories currently on the toolbox.
		/// </summary>
		/// <value>
		/// A <see cref="System.Drawing.Design.CategoryNameCollection">
		/// containing the tool categories.
		/// </value>
		public CategoryNameCollection CategoryNames {
			get {
				string[] names = new string[toolboxByCategory.Count];
				toolboxByCategory.Keys.CopyTo(names, 0);
				return new CategoryNameCollection(names);
			}
		}
		
		/// <summary>
		/// Gets or sets the name of the currently selected tool category
		/// from the toolbox.
		/// </summary>
		/// <value>
		/// The name of the currently selected category.
		/// </value>
		/// <remarks>
		/// The implementation of this property's "set" accessor fires the
		/// events <see cref="SelectedCategoryChanging"> and
		/// <see cref="SelectedCategoryChanged">.
		/// </remarks>
		public string SelectedCategory {
			get {
				return selectedCategory;
			}
			set {
				if (value != selectedCategory) {
					FireSelectedCategoryChanging();
					selectedCategory = value;
					FireSelectedCategoryChanged();
				}
			}
		}
		
		// Methods
		/// <summary>
		/// Adds a new toolbox item creator.
		/// </summary>
		/// <param name="creator">
		/// A <see cref="System.Drawing.Design.ToolboxItemCreatorCallback">
		/// that can create a component when the toolbox item
		/// is invoked. </param>
		/// <param name="format">
		/// The data format this creator responds to. If a creator responds
		/// to more than one format, call AddCreator more than once. It is
		/// an error to add more than one creator for the same format.
		/// </param>
		/// <remarks>
		/// A toolbox item creator is used to handle data formats other than
		/// the standard native format of the toolbox service. Typically, the
		/// standard toolbox service format should be used, because it provides
		/// ample opportunity for customization in an object-oriented way.
		/// However, there are times when a legacy data format may need to be
		/// supported. A toolbox item creator is the mechanism by which these
		/// legacy data formats may be converted into toolbox items.
		/// </remarks>
		public void AddCreator(ToolboxItemCreatorCallback creator, string format)
		{
			AddCreator(creator, format, null);
		}
		
		/// <summary>
		/// Adds a new toolbox item creator.
		/// </summary>
		/// <param name="creator">
		/// A <see cref="System.Drawing.Design.ToolboxItemCreatorCallback">
		/// that can create a component when the toolbox item
		/// is invoked. </param>
		/// <param name="format">
		/// The data format this creator responds to. If a creator responds
		/// to more than one format, call AddCreator more than once. It is
		/// an error to add more than one creator for the same format.
		/// </param>
		/// <param name="host">
		/// The designer host to associate with the creator. If this parameter
		/// is set to a null reference (Nothing in Visual Basic), this creator
		/// will be available to all designers. If a designer host is supplied,
		/// the creator will only be available to designers using the specified
		/// host.
		/// </param>
		/// <remarks>
		/// A toolbox item creator is used to handle data formats other than
		/// the standard native format of the toolbox service. Typically, the
		/// standard toolbox service format should be used, because it provides
		/// ample opportunity for customization in an object-oriented way.
		/// However, there are times when a legacy data format may need to be
		/// supported. A toolbox item creator is the mechanism by which these
		/// legacy data formats may be converted into toolbox items.
		/// <para>
		/// This implemetation does add the specified creator to a collection,
		/// but at this time I have no idea what to do with it!
		/// </para>
		/// </remarks>
		public void AddCreator(ToolboxItemCreatorCallback creator, string format, IDesignerHost host)
		{
			LoggingService.DebugFormatted("\tDefaultToolboxService:AddCreator({0}, {1}, {2})", creator, format, host);
			if (host == null) {
				creators.Add(format, creator);
			} else {
				IDictionary creatorsDict = (IDictionary)creatorsByHost[host];
				if (creatorsDict == null) {
					creatorsDict = new HybridDictionary();
					creatorsByHost.Add(host, creatorsDict);
				}
				creatorsDict[format] =creator;
			}
		}
		
		void AddItemToToolbox(ToolboxItem toolboxItem, string category, IDesignerHost host)
		{
			toolboxItems.Add(toolboxItem);
			
			if (host != null) {
				IList list = (IList)toolboxByHost[host];
				if (list == null) {
					list = new ArrayList();
					toolboxByHost.Add(host, list);
				}
				list.Add(toolboxItem);
			} else {
				IList list = (IList)toolboxByHost[ALL_HOSTS];
				list.Add(toolboxItem);
			}
			
			if (category != null) {
				IList list = (IList)toolboxByCategory[category];
				if (list == null) {
					list = new ArrayList();
					toolboxByCategory.Add(category, list);
				}
				list.Add(toolboxItem);
			} else {
				IList list = (IList)toolboxByCategory[ALL_CATEGORIES];
				list.Add(toolboxItem);
			}
			
			FireToolboxItemAdded(toolboxItem, category, host);
		}
		
		public void AddLinkedToolboxItem(ToolboxItem toolboxItem, string category, IDesignerHost host)
		{
			AddItemToToolbox(toolboxItem, category, host);
		}
		
		public void AddLinkedToolboxItem(ToolboxItem toolboxItem, IDesignerHost host)
		{
			this.AddLinkedToolboxItem(toolboxItem, null, host);
		}
		
		public void AddToolboxItem(ToolboxItem toolboxItem)
		{
			this.AddItemToToolbox(toolboxItem, null, null);
		}
		
		public void AddToolboxItem(ToolboxItem toolboxItem, string category)
		{
			this.AddItemToToolbox(toolboxItem, category, null);
		}
		
		public ToolboxItem DeserializeToolboxItem(object serializedObject)
		{
			return DeserializeToolboxItem(serializedObject, null);
		}
		
		public ToolboxItem DeserializeToolboxItem(object serializedObject, IDesignerHost host)
		{
			LoggingService.DebugFormatted("DeserializeToolboxItem {0} host {1}", serializedObject, host);
			if (serializedObject is System.Windows.Forms.IDataObject) {
				if (((System.Windows.Forms.IDataObject)serializedObject).GetDataPresent(typeof(ToolboxItem))) {
					ToolboxItem item = (ToolboxItem) ((System.Windows.Forms.IDataObject)serializedObject).GetData(typeof(ToolboxItem));
					
					ArrayList list;
					if (host != null) {
						list = (ArrayList)toolboxByHost[host];
						if (list != null && list.Contains(item)) {
							LoggingService.Warn(item.TypeName);
							return item;
						}
					}

⌨️ 快捷键说明

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