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

📄 sdmenucommand.cs

📁 c#源代码
💻 CS
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using ICSharpCode.Core.AddIns.Conditions;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;

using Reflector.UserInterface;

namespace ICSharpCode.SharpDevelop.Gui.Components
{
	public class SdMenuCommand : CommandBarButton, IStatusUpdate
	{
		static StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
			
		object caller;
		ConditionCollection conditionCollection;
		string description   = String.Empty;
		string localizedText = String.Empty;
		ICommand menuCommand = null;
		
		public ICommand Command {
			get {
				return menuCommand;
			}
			set {
				menuCommand = value;
				UpdateStatus();
			}
		}
		
		public string Description {
			get {
				return description;
			}
			set {
				description = value;
			}
		}
		
		public SdMenuCommand(ConditionCollection conditionCollection, object caller, string label) : base(stringParserService.Parse(label))
		{
			this.caller              = caller;
			this.conditionCollection = conditionCollection;
			this.localizedText       = label;
			UpdateStatus();
			
		}
		
		public SdMenuCommand(ConditionCollection conditionCollection, object caller, string label, ICommand menuCommand) : base(stringParserService.Parse(label))
		{
			this.caller = caller;
			this.conditionCollection = conditionCollection;
			this.localizedText       = label;
			this.menuCommand = menuCommand;
			UpdateStatus();
		}
		
		public SdMenuCommand(ConditionCollection conditionCollection, object caller, string label, EventHandler handler) : base(stringParserService.Parse(label), handler)
		{
			this.caller = caller;
			this.conditionCollection = conditionCollection;
			this.localizedText       = label;
			UpdateStatus();
		}
		
		public SdMenuCommand(object caller, string label, EventHandler handler) : base(stringParserService.Parse(label), handler)
		{
			this.caller = caller;
			this.localizedText       = label;
			UpdateStatus();
		}
		
		protected override void OnClick(System.EventArgs e)
		{
			base.OnClick(e);
			if (menuCommand != null) {
				menuCommand.Run();
			}
		}
		
		public override bool IsVisible {
			get {
				bool isVisible = base.IsVisible;
				if (conditionCollection != null) {
					ConditionFailedAction failedAction = conditionCollection.GetCurrentConditionFailedAction(caller);
					isVisible &= failedAction != ConditionFailedAction.Exclude;
				}
				return isVisible;
			}
			set {
				base.IsVisible = value;
			}
		}
		
		public override bool IsEnabled {
			get {
				bool isEnabled = true; //base.IsEnabled;
				if (conditionCollection != null) {
					ConditionFailedAction failedAction = conditionCollection.GetCurrentConditionFailedAction(caller);
					isEnabled &= failedAction != ConditionFailedAction.Disable;
				}
				if (menuCommand != null && menuCommand is IMenuCommand) {
					isEnabled &= ((IMenuCommand)menuCommand).IsEnabled;
				}
				return isEnabled;
			}
			set {
				base.IsEnabled = value;
			}
		}
		
//		protected override void OnSelect(System.EventArgs e)
//		{
//			base.OnSelect(e);
//			IStatusBarService statusBarService = (IStatusBarService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IStatusBarService));
//			statusBarService.SetMessage(description);
//		}
		
		public virtual void UpdateStatus()
		{
			bool isEnabled = true;
			if (conditionCollection != null) {
				ConditionFailedAction failedAction = conditionCollection.GetCurrentConditionFailedAction(caller);
				bool isVisible = failedAction != ConditionFailedAction.Exclude;
				if (base.IsVisible != isVisible) {
					base.IsVisible = isVisible;
				}
				isEnabled = failedAction != ConditionFailedAction.Disable;
			}
			if (menuCommand != null && menuCommand is IMenuCommand) {
				isEnabled &= ((IMenuCommand)menuCommand).IsEnabled;
			}
			if (base.IsEnabled != isEnabled) {
				base.IsEnabled = isEnabled;
			}
			Text = stringParserService.Parse(localizedText);
		}
	}
}

⌨️ 快捷键说明

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