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

📄 iconmenuitem.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  IconMenuItem.cs
//  Copyright (c) 2001 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;

using SharpDevelop.Gui.Window;
using SharpDevelop.Tool.Data;
using SharpDevelop.Internal.ExternalTool;
using SharpDevelop.Internal.Text;

namespace SharpDevelop.Gui.Components {
	
	public enum IconEnabledStatus { 
		Always,
		Buffer,
		Project,
		Window,
		Something
	};
	
	public enum SpecialItemTag {
		None,
		LastFiles,
		LastProjects,
		ToolMenu,
		PluginMenu,
		FileModes,
		MdiList,
		Link,
		WebLink
	};
		
	public class IconMenuItem : RichMenuItem
	{
		string       description = null;
	    MainWindow   mainwindow = null;
		EventHandler myhandler; // needed for cloning
		
		IconEnabledStatus   iconenabled = IconEnabledStatus.Always;
		SpecialItemTag      itemtag     = SpecialItemTag.None;
		
		public IconEnabledStatus IconEnabled {
			get {
				return iconenabled;
			}
			set {
				iconenabled = value;
			}
		}
		
		public SpecialItemTag ItemTag {
			get {
				return itemtag;
			}
			set {
				itemtag = value;
				switch (itemtag) {
					case SpecialItemTag.FileModes:
						FileModeChange(null, null);
						break;
					case SpecialItemTag.PluginMenu:
						PluginMenuChange(null, null);
						break;
					case SpecialItemTag.ToolMenu:
						ToolMenuChange(null, null);
						break;
					case SpecialItemTag.LastFiles:
						mainwindow.RecentOpen.RecentFileChanged += new EventHandler(RecentFileChange);
						RecentFileChange(null, null);
						break;
					case SpecialItemTag.LastProjects:
						mainwindow.RecentOpen.RecentProjectChanged += new EventHandler(RecentProjectChange);
						RecentProjectChange(null, null);
						break;
					case SpecialItemTag.MdiList:
						MdiList = true;
						break;
				}
			}
		}
		
		public string Description {
			get {
				return description;
			}
			set {
				description = value;
			}
		}
		
		public IconMenuItem()
		{
		}
	
		public IconMenuItem(MainWindow mainwindow)
		{
			this.mainwindow = mainwindow;
		}
		
		public IconMenuItem(MainWindow mainwindow, string name, EventHandler handler, string description, Shortcut shortcut) : base((IconMenuStyle)Enum.Parse(typeof(IconMenuStyle),Option.GetProperty("IconMenuItem.IconMenuStyle", IconMenuStyle.VSNet).ToString()), name, handler, shortcut)
		{
		}
		
		public IconMenuItem(MainWindow mainwindow, string name, EventHandler handler, string description) : base((IconMenuStyle)Enum.Parse(typeof(IconMenuStyle),Option.GetProperty("IconMenuItem.IconMenuStyle", IconMenuStyle.VSNet).ToString()), name, handler)
		{
			myhandler = handler;
			this.mainwindow = mainwindow;
			this.Description = description;
			Select += new EventHandler(SelectMenuItem);
		}
			
		protected override void OnPopup(EventArgs e)
		{
			base.OnPopup(e);
			// update the current update status of the subitems
			foreach (MenuItem item in MenuItems) {
				if (item is IconMenuItem) {
					((IconMenuItem)item).UpdateOpenStatus(this, null);
				}
			}
		}
		
		void UpdateOpenStatus(object sender, EventArgs e)
		{
			switch (iconenabled) {
				case IconEnabledStatus.Always:
					Enabled = true;
					break;
				case IconEnabledStatus.Window:
					Enabled = mainwindow.ActiveContentWindow != null;
					break;
				case IconEnabledStatus.Buffer:
					Enabled = false;
					goto default;
				case IconEnabledStatus.Project:
					Enabled = mainwindow.ProjectMode;
					break;
				case IconEnabledStatus.Something:
					Enabled = mainwindow.ProjectMode;
					goto default;
				default:
					// openstatus for text buffer
					foreach (ContentWindow window in mainwindow.MdiChildren)
						if (window.HasTextArea) {
							Enabled = true;
							break;
						}
					break;
			}
		}
		
		void SelectMenuItem(object sender, EventArgs e)
		{
			if (typeof(IconMenuItem).IsInstanceOfType(sender)) {
				IconMenuItem item = (IconMenuItem)sender;
				if (item.Description != null)
					mainwindow.StatusBarText = item.Description;
				else
					mainwindow.StatusBarText = "";
			}
		}

		void LoadOldFile(object sender, System.EventArgs e)
		{
			MenuItem item = (MenuItem)sender;
			mainwindow.OpenWindow(item.Text);
		}
		
		void RecentFileChange(object sender, EventArgs e)
		{
			int recentfilelength = mainwindow.RecentOpen.RecentFile.Count;
			MenuItems.Clear();
			if (recentfilelength > 0) {
				for (int i = 0; i < recentfilelength; ++i) 
					MenuItems.Add(new IconMenuItem(mainwindow, mainwindow.RecentOpen.RecentFile[i].ToString(), new EventHandler(LoadOldFile), "load file " + mainwindow.RecentOpen.RecentFile[i].ToString()));
			} else {
				MenuItem tmp = new IconMenuItem(mainwindow, "last files", null, null);
				tmp.Enabled  = false;
				MenuItems.Add(tmp);
			}
		}
		
		void LoadOldProject(object sender, System.EventArgs e)
		{
			MenuItem item = (MenuItem)sender;
			mainwindow.ProjectBrowser.OpenProject(item.Text);
			mainwindow.ProjectMode = true;
		}
		void RecentProjectChange(object sender, EventArgs e)
		{
			int recentprojectlength = mainwindow.RecentOpen.RecentProject.Count;
			MenuItems.Clear();
			if (recentprojectlength > 0) {
				for (int i = 0; i < recentprojectlength; ++i) 
					MenuItems.Add(new IconMenuItem(mainwindow, mainwindow.RecentOpen.RecentProject[i].ToString(), new EventHandler(LoadOldProject), "load project " + mainwindow.RecentOpen.RecentProject[i].ToString()));
			} else {
				MenuItem tmp = new IconMenuItem(mainwindow, "last projects", null, null);
				tmp.Enabled  = false;
				MenuItems.Add(tmp);
			}
		}
		
		void ToolEvt(object sender, EventArgs e)
		{
			MenuItem item = (MenuItem)sender;
			
			for (int i = 0; i < ToolLoader.Tool.Count; ++i) 
				if (item.Text == ToolLoader.Tool[i].ToString()) { 
					ExternalTool tool = (ExternalTool)ToolLoader.Tool[i];
					ProcessStartInfo startinfo = new ProcessStartInfo(tool.Command, tool.Arguments);
					startinfo.WorkingDirectory = tool.InitialDirectory;
					Process.Start(startinfo);
					break;
				}
		}
		void ToolMenuChange(object sender, EventArgs e)
		{
			MenuItems.Clear();
			
			for (int i = 0; i < ToolLoader.Tool.Count; ++i) {
				IconMenuItem item = new IconMenuItem(mainwindow, ToolLoader.Tool[i].ToString(), new EventHandler(ToolEvt), "Open Tool");
//				item.Select += new EventHandler(SelectMenuItem);
				MenuItems.Add (item);
			}
		}
		
		void PluginMenuChange(object sender, EventArgs e)
		{
			foreach (MenuItem item in mainwindow.PluginManager.MenuItems)
				MenuItems.Add(item);
		}
		
		EventHandler syntaxhandler;
		void SetSyntaxCheckMarks(object sender, EventArgs e)
		{
			ContentWindow window = mainwindow.ActiveContentWindow;
			foreach (MenuItem i in MenuItems) {
				i.Checked = (window != null && window.HasTextArea && window.TextArea.Buffer.Syntax.Name == i.Text);
			}
			mainwindow.MdiChildActivate -= syntaxhandler;
		}
		
		void ChangeSyntax(object sender, EventArgs e)
		{
			ContentWindow window = mainwindow.ActiveContentWindow;
			if (window.HasTextArea) {
				IconMenuItem item = ((IconMenuItem)sender);
				foreach (MenuItem i in item.Parent.MenuItems) {
					i.Checked = false;
				}
				item.Checked = true;
				window.TextArea.Buffer.SetHighlightingTo(item.Text);
				window.TextArea.Refresh();
			}
		}
		
		void FileModeChange(object sender, EventArgs e)
		{
			foreach (Syntax syntax in Syntax.SyntaxDefinitions) {
				IconMenuItem item = new IconMenuItem(mainwindow, syntax.Name, new EventHandler(ChangeSyntax), "Change highlighting definition to " + syntax.Name);
				MenuItems.Add(item);
			}
			syntaxhandler = new EventHandler(SetSyntaxCheckMarks);
			mainwindow.MdiChildActivate += syntaxhandler;
		}
	}
}

⌨️ 快捷键说明

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