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

📄 sharpdeveloptextareacontrol.cs

📁 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 value="$version"/>
// </file>

using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using Reflector.UserInterface;

using ICSharpCode.Core.Services;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.Properties;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.DefaultEditor.Actions;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Actions;
using ICSharpCode.SharpDevelop.Internal.Templates;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.SharpDevelop.Gui.Components;
using ICSharpCode.TextEditor.Gui.InsightWindow;
using ICSharpCode.TextEditor.Gui.CompletionWindow;

using System.Threading;

namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor
{
	public class SharpDevelopTextAreaControl : TextEditorControl
	{
		readonly static string contextMenuPath       = "/SharpDevelop/ViewContent/DefaultTextEditor/ContextMenu";
		readonly static string editActionsPath       = "/AddIns/DefaultTextEditor/EditActions";
		readonly static string formatingStrategyPath = "/AddIns/DefaultTextEditor/Formater";
		
		QuickClassBrowserPanel quickClassBrowserPanel = null;
		ErrorDrawer errorDrawer;
		
		public QuickClassBrowserPanel QuickClassBrowserPanel {
			get {
				return quickClassBrowserPanel;
			}
		}
		
		public SharpDevelopTextAreaControl()
		{
			errorDrawer = new ErrorDrawer(this);
			Document.FoldingManager.FoldingStrategy = new ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ParserFoldingStrategy();
			GenerateEditActions();
			
			TextAreaDragDropHandler dragDropHandler = new TextAreaDragDropHandler();
			TextEditorProperties = new SharpDevelopTextEditorProperties();
		}
		
		public virtual ICompletionDataProvider CreateCodeCompletionDataProvider(bool ctrlSpace)
		{
			//ivoko: please do not touch or discuss with me: we use another CCDP
			return new CodeCompletionDataProvider(ctrlSpace, false);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (ActiveTextAreaControl != null) {
					TextAreaControl newControl = ActiveTextAreaControl;
					if (newControl.ContextMenu != null) {
						newControl.ContextMenu.Dispose();
						newControl.ContextMenu = null;
					}
					newControl.TextArea.KeyEventHandler -= new ICSharpCode.TextEditor.KeyEventHandler(HandleKeyPress);
					if (newControl.SelectionManager != null)
						newControl.SelectionManager.SelectionChanged -= new EventHandler(SelectionChanged);
					if (newControl.Document != null)
						newControl.Document.DocumentChanged -= new DocumentEventHandler(DocumentChanged);
					if (newControl.Caret != null)
						newControl.Caret.PositionChanged -= new EventHandler(CaretPositionChanged);
					newControl.TextArea.ClipboardHandler.CopyText -= new CopyTextEventHandler(ClipboardHandlerCopyText);
			
					newControl.TextArea.IconBarMargin.Painted   -= new MarginPaintEventHandler(PaintIconBarBreakPoints);
					newControl.TextArea.IconBarMargin.MouseDown -= new MarginMouseEventHandler(IconBarMouseDown);
					newControl.MouseWheel                       -= new MouseEventHandler(TextAreaMouseWheel);
					
				}
				if (errorDrawer != null) {
					errorDrawer.Dispose();
					errorDrawer = null;
				}
				if (quickClassBrowserPanel != null) {
					quickClassBrowserPanel.Dispose();
					quickClassBrowserPanel = null;
				}
			}
			base.Dispose(disposing);
		}

		protected override void InitializeTextAreaControl(TextAreaControl newControl)
		{
			base.InitializeTextAreaControl(newControl);
			MenuService menuService = (MenuService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(MenuService));
			newControl.ContextMenu = menuService.CreateContextMenu(this, contextMenuPath);
			newControl.TextArea.KeyEventHandler += new ICSharpCode.TextEditor.KeyEventHandler(HandleKeyPress);
			newControl.SelectionManager.SelectionChanged += new EventHandler(SelectionChanged);
			newControl.Document.DocumentChanged += new DocumentEventHandler(DocumentChanged);
			newControl.Caret.PositionChanged += new EventHandler(CaretPositionChanged);
			newControl.TextArea.ClipboardHandler.CopyText += new CopyTextEventHandler(ClipboardHandlerCopyText);
			
			newControl.TextArea.IconBarMargin.Painted   += new MarginPaintEventHandler(PaintIconBarBreakPoints);
			newControl.TextArea.IconBarMargin.MouseDown += new MarginMouseEventHandler(IconBarMouseDown);
			newControl.MouseWheel                       += new MouseEventHandler(TextAreaMouseWheel);
			newControl.DoHandleMousewheel = false;
		}
		void CloseCodeCompletionWindow(object sender, EventArgs e)
		{
			Form senderForm = (Form)sender;
			senderForm.Closed -= new EventHandler(CloseCodeCompletionWindow);
			senderForm.Dispose();
			if (senderForm == codeCompletionWindow) {
				codeCompletionWindow = null;
			}
		}
		void CloseInsightWindow(object sender, EventArgs e)
		{
			Form senderForm = (Form)sender;
			senderForm.Closed -= new EventHandler(CloseInsightWindow);
			senderForm.Dispose();
			if (senderForm == insightWindow) {
				insightWindow = null;
			}
		}
		void TextAreaMouseWheel(object sender, MouseEventArgs e)
		{
			TextAreaControl textAreaControl = (TextAreaControl)sender;
			if (insightWindow != null && !insightWindow.IsDisposed && insightWindow.Visible) {
				insightWindow.HandleMouseWheel(e);
			} else if (codeCompletionWindow != null && !codeCompletionWindow.IsDisposed && codeCompletionWindow.Visible) {
				codeCompletionWindow.HandleMouseWheel(e);
			} else {
				textAreaControl.HandleMouseWheel(e);
			}
		}
		
		void ClipboardHandlerCopyText(object sender, CopyTextEventArgs e)
		{
			ICSharpCode.SharpDevelop.Gui.Pads.SideBarView.PutInClipboardRing(e.Text);
		}
		public override void OptionsChanged()
		{
			base.OptionsChanged();
			SharpDevelopTextEditorProperties sdtep = base.TextEditorProperties as SharpDevelopTextEditorProperties;
			
			if (sdtep != null) {
				if (!sdtep.ShowQuickClassBrowserPanel) {
 					RemoveQuickClassBrowserPanel();
				} else {
					ActivateQuickClassBrowserOnDemand();
				}
			}
		}

		protected virtual void IconBarMouseDown(AbstractMargin iconBar, Point mousepos, MouseButtons mouseButtons)
		{
			int realline = iconBar.TextArea.TextView.GetLogicalLine(mousepos);
			if (realline >= 0 && realline < iconBar.TextArea.Document.TotalNumberOfLines) {
				DebuggerService debuggerService = (DebuggerService)ServiceManager.Services.GetService(typeof(DebuggerService));
				if (debuggerService != null && debuggerService.CurrentDebugger.SupportsExecutionControl) {
					debuggerService.ToggleBreakpointAt(FileName, realline + 1, 0);
					iconBar.TextArea.Refresh(iconBar);
				}
			}
		}
		
		protected virtual void PaintIconBarBreakPoints(AbstractMargin iconBar, Graphics g, Rectangle rect)
		{
			DebuggerService debuggerService = (DebuggerService)ServiceManager.Services.GetService(typeof(DebuggerService));
			if (debuggerService == null)
				return;
			lock (debuggerService.Breakpoints) {
				foreach (Breakpoint breakpoint in debuggerService.Breakpoints) {
					try {
						if (Path.GetFullPath(breakpoint.FileName) == Path.GetFullPath(FileName)) {
							int lineNumber = iconBar.TextArea.Document.GetVisibleLine(breakpoint.LineNumber - 1);
							int yPos = (int)(lineNumber * iconBar.TextArea.TextView.FontHeight) - iconBar.TextArea.VirtualTop.Y;
							if (yPos >= rect.Y && yPos <= rect.Bottom) {
								((IconBarMargin)iconBar).DrawBreakpoint(g, yPos, breakpoint.IsEnabled);
							}
						}
					} catch (Exception) {}
				}
			}
		}
		
		void CaretPositionChanged(object sender, EventArgs e)
		{
			IStatusBarService statusBarService = (IStatusBarService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IStatusBarService));
			statusBarService.SetCaretPosition(ActiveTextAreaControl.TextArea.TextView.GetVisualColumn(ActiveTextAreaControl.Caret.Line, ActiveTextAreaControl.Caret.Column), ActiveTextAreaControl.Caret.Line, ActiveTextAreaControl.Caret.Column);
		}
		
		void DocumentChanged(object sender, DocumentEventArgs e)
		{
		}
		
		void SelectionChanged(object sender, EventArgs e)
		{
		}
		
		void GenerateEditActions()
		{
			try {
				IEditAction[] actions = (IEditAction[])(AddInTreeSingleton.AddInTree.GetTreeNode(editActionsPath).BuildChildItems(this)).ToArray(typeof(IEditAction));
				
				foreach (IEditAction action in actions) {
					foreach (Keys key in action.Keys) {
						editactions[key] = action;
					}
				}
			} catch (TreePathNotFoundException) {
				Console.WriteLine(editActionsPath + " doesn't exists in the AddInTree");
			}
		}
		
		void RemoveQuickClassBrowserPanel()
		{
			if (quickClassBrowserPanel != null) {
				Controls.Remove(quickClassBrowserPanel);
				quickClassBrowserPanel.Dispose();
				quickClassBrowserPanel = null;
				textAreaPanel.BorderStyle = System.Windows.Forms.BorderStyle.None;
			}
		}
		void ShowQuickClassBrowserPanel()
		{
			if (quickClassBrowserPanel == null) {
				quickClassBrowserPanel = new QuickClassBrowserPanel(this);
				Controls.Add(quickClassBrowserPanel);
				textAreaPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			}
		}
		public void ActivateQuickClassBrowserOnDemand()
		{
			SharpDevelopTextEditorProperties sdtep = base.TextEditorProperties as SharpDevelopTextEditorProperties;
			if (sdtep != null && sdtep.ShowQuickClassBrowserPanel && FileName != null) {
				IParserService parserService = (IParserService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IParserService));
				bool quickClassPanelActive = parserService.GetParser(FileName) != null;
				if (quickClassPanelActive) {
					ShowQuickClassBrowserPanel();
				} else {
					RemoveQuickClassBrowserPanel();
				}
			}
		}

⌨️ 快捷键说明

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