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

📄 errordrawer.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  ErrorDrawer.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.Collections;
using System.Drawing;
using System.Threading;
using System.Drawing.Text;

using SharpDevelop.Internal.Text;

namespace SharpDevelop.Gui.Edit.Text {
	
	/// <summary>
	/// reperesents a visual error, this class is needed by the errordrawer.
	/// </summary>
	public class VisualError
	{
		int line;
		int xpos;
		int length;
		string description;
		
		public int Line {
			get {
				return line;
			}
			set {
				line = value;
			}
		}
		
		public int Xpos {
			get {
				return xpos;
			}
		}
		
		public int Length {
			get {
				return length;
			}
		}
		
		public string Description {
			get {
				return description;
			}
		}
		
		public VisualError(int line, int xpos, int length, string description)
		{
			this.line        = line;
			this.xpos        = xpos;
			this.length      = length;
			this.description = description;
		}
	}
	
	/// <summary>
	/// This class draws error underlines.
	/// </summary>
	public class ErrorDrawer
	{
		ArrayList       errors = new ArrayList();
		TextAreaControl textarea;
		
		public ErrorDrawer(TextAreaControl textarea)
		{
			this.textarea = textarea;
			textarea.Buffer.MoveIndices += new MoveIndicesHandler(MoveIndices);
			textarea.TextAreaPainter.ToolTipEvent += new ToolTipEvent(ToolTip);
			textarea.TextAreaPainter.LinePainter += new LinePainter(ErrorPainter);
		}
		
		public void MoveIndices(int index, int count)
		{
			ArrayList newerrors = new ArrayList();
			bool redraw = false;
			foreach (VisualError error in errors) {
				if (count < 0 && error.Line >= index + count && error.Line < index) {// delete cleared error
					redraw = true;
					continue;
				}
				if (error.Line >= index) {
					error.Line += count;
				}
				newerrors.Add(error);
			}
			textarea.Buffer.UpdateRequested |= redraw;
			errors = newerrors;
		}
		
		public void ClearErrors()
		{
			Monitor.Enter(this);
			ArrayList lines = new ArrayList();
			foreach (VisualError error in errors) 
				lines.Add(error.Line);
			errors.Clear();
			foreach (int line in lines) 
				textarea.UpdateLines(line, line);
			Monitor.Exit(this);
		}
		
		public bool AddError(VisualError newerror)
		{
			Monitor.Enter(this);
			foreach (VisualError error in errors)
				if (error.Line == newerror.Line && error.Xpos == newerror.Xpos && error.Length == newerror.Length) {
					Monitor.Exit(this);
					return false;
				}
			textarea.UpdateLines(newerror.Line, newerror.Line);
			errors.Add(newerror);
			Monitor.Exit(this);
			return true;
		}
		
		void ToolTip(int xpos, int ypos)
		{
			foreach (VisualError error in errors) {
				Point errorpos  = textarea.Caret.GetPhysicalPos(new Point(error.Xpos, error.Line));
				Rectangle r = new Rectangle((int)(errorpos.X * textarea.TextAreaPainter.FontWidth), 
											(int)(errorpos.Y * textarea.TextAreaPainter.FontHeight), 
						                    (int)(error.Length * textarea.TextAreaPainter.FontWidth), 
                    						(int)textarea.TextAreaPainter.FontHeight);
				if (r.Contains(xpos, ypos)) {
					textarea.TextAreaPainter.ToolTip.SetToolTip(textarea.TextAreaPainter, error.Description);
					return;
				}
			}
			textarea.TextAreaPainter.ToolTip.SetToolTip(textarea.TextAreaPainter, null);
		}
		
		void DrawWaveLine(Graphics g, int from, int to, int ypos)
		{
			Pen pen = new Pen(Color.Red);
			for (int i = from; i < to; i+= 6) {
				g.DrawLine(pen, i,     ypos + 3, i + 3, ypos + 1);
				g.DrawLine(pen, i + 3, ypos + 1, i + 6, ypos + 3);
			}
		}
		
		void ErrorPainter(Graphics g, int line, RectangleF rect, PointF pos)
		{
			if (textarea.Options.ShowErrors)
			foreach (VisualError error in errors) {
				Point errorpos  = textarea.Caret.GetPhysicalPos(new Point(error.Xpos, line));
				if (error.Line == line) {
					DrawWaveLine(g, 
						(int)(pos.X + errorpos.X * textarea.TextAreaPainter.FontWidth), 
						(int)(pos.X + (errorpos.X + error.Length) * textarea.TextAreaPainter.FontWidth), 
						(int)(pos.Y + textarea.TextAreaPainter.FontHeight - 3));
				} 
			}
		}	
	}
}

⌨️ 快捷键说明

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