autostartcommands.cs

来自「SharpDevelop2.0.0 c#开发免费工具」· CS 代码 · 共 154 行

CS
154
字号
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version>$Revision: 1331 $</version>
// </file>

using System;
using System.IO;
using System.Collections;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
using System.Runtime.Remoting;
using System.Security.Policy;

using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;

using ICSharpCode.SharpDevelop.Dom;

namespace ICSharpCode.SharpDevelop.Commands
{
	public class StartWorkbenchCommand // : AbstractCommand
	{
		const string workbenchMemento = "WorkbenchMemento";
		
		/// <remarks>
		/// The worst workaround in the whole project
		/// </remarks>
		void ShowTipOfTheDay(object sender, EventArgs e)
		{
			Application.Idle -= ShowTipOfTheDay;
			
			// show tip of the day
			if (PropertyService.Get("ShowTipsAtStartup", true)) {
				ViewTipOfTheDay dview = new ViewTipOfTheDay();
				dview.Run();
			}
		}
		
		class FormKeyHandler : IMessageFilter
		{
			const int keyPressedMessage          = 0x100;
			
			void SelectActiveWorkbenchWindow()
			{
				if (WorkbenchSingleton.Workbench.ActiveWorkbenchWindow != null) {
					if (!WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ActiveViewContent.Control.ContainsFocus) {
						if (Form.ActiveForm == WorkbenchSingleton.MainForm) {
							WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ActiveViewContent.Control.Focus();
						}
					}
				}
			}
			
			bool PadHasFocus()
			{
				foreach (PadDescriptor padContent in WorkbenchSingleton.Workbench.PadContentCollection) {
					if (padContent.HasFocus) {
						return true;
						
					}
				}
				return false;
			}
			string oldLayout = "Default";
			public bool PreFilterMessage(ref Message m)
			{
				if (m.Msg != keyPressedMessage) {
					return false;
				}
				Keys keyPressed = (Keys)m.WParam.ToInt32() | Control.ModifierKeys;
				
				if (keyPressed == Keys.Escape) {
					if (PadHasFocus() && !MenuService.IsContextMenuOpen) {
						SelectActiveWorkbenchWindow();
						return true;
					}
					return false;
				}
				
				if (keyPressed == (Keys.Escape | Keys.Shift)) {
					if (LayoutConfiguration.CurrentLayoutName == "Plain") {
						LayoutConfiguration.CurrentLayoutName = oldLayout;
					} else {
						WorkbenchSingleton.Workbench.WorkbenchLayout.StoreConfiguration();
						oldLayout = LayoutConfiguration.CurrentLayoutName;
						LayoutConfiguration.CurrentLayoutName = "Plain";
					}
					SelectActiveWorkbenchWindow();
					return true;
				}
				return false;
			}
		}
		
		public void Run(string[] fileList)
		{
			Form f = (Form)WorkbenchSingleton.Workbench;
			f.Show();
			
			Application.Idle += ShowTipOfTheDay;
			
			bool didLoadCombineOrFile = false;
			
			foreach (string file in fileList) {
				didLoadCombineOrFile = true;
				try {
					IProjectLoader loader = ProjectService.GetProjectLoader(file);
					if (loader != null) {
						FileUtility.ObservedLoad(new NamedFileOperationDelegate(loader.Load), file);
					} else {
						FileService.OpenFile(file);
					}
				} catch (Exception e) {
					MessageService.ShowError(e, "unable to open file " + file);
				}
			}
			
			// load previous combine
			if (!didLoadCombineOrFile && PropertyService.Get("SharpDevelop.LoadPrevProjectOnStartup", false)) {
				if (FileService.RecentOpen.RecentProject.Count > 0) {
					ProjectService.LoadSolution(FileService.RecentOpen.RecentProject[0].ToString());
					didLoadCombineOrFile = true;
				}
			}
			
			if (!didLoadCombineOrFile) {
				foreach (ICommand command in AddInTree.BuildItems("/Workspace/AutostartNothingLoaded", null, false)) {
					command.Run();
				}
			}
			
			f.Focus(); // windows.forms focus workaround
			
			ParserService.StartParserThread();
			
			// finally run the workbench window ...
			Application.AddMessageFilter(new FormKeyHandler());
			Application.Run(f);
			
			// save the workbench memento in the ide properties
			try {
				PropertyService.Set(workbenchMemento, WorkbenchSingleton.Workbench.CreateMemento());
			} catch (Exception e) {
				MessageService.ShowError(e, "Exception while saving workbench state.");
			}
		}
	}
}

⌨️ 快捷键说明

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