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

📄 brackethighlighter.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  BracketHighlighter.cs
//  Copyright (C) 2000 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.Diagnostics;
using SharpDevelop.Internal.Text;

namespace SharpDevelop.Gui.Edit.Text {
	
	public class Highlight
	{
		Point begin;
		Point end;
		
		public Point Begin {
			get {
				return begin;
			}
		}
		
		public Point End {
			get {
				return end;
			}
		}
		
		public Highlight(Point begin, Point end)
		{
			this.begin = begin;
			this.end   = end;
		}
	}
	
	public class BracketHighlightingSheme
	{
		char opentag;
		char closingtag;
		
		public char OpenTag {
			get {
				return opentag;
			}
			set {
				opentag = value;
			}
		}
		
		public char ClosingTag {
			get {
				return closingtag;
			}
			set {
				closingtag = value;
			}
		}
		
		public BracketHighlightingSheme(char opentag, char closingtag)
		{
			this.opentag    = opentag;
			this.closingtag = closingtag;
		}
		
		public bool IsInside(TextBuffer buffer, Point pos)
		{
			Point wordcoordinate = new Point(pos.X - 1, pos.Y);
			if (wordcoordinate.X >= 0 && 
				wordcoordinate.Y < buffer.Length &&
				wordcoordinate.Y >= 0 && 
				wordcoordinate.X < buffer[wordcoordinate.Y].Text.Length) {
					
				char word = buffer[wordcoordinate.Y].Text[wordcoordinate.X];
				return word == opentag || word == closingtag;
			}
			return false;
		}
		
		public Highlight GetHighlight(TextBuffer buffer, Point pos)
		{
			Point wordcoordinate =  new Point(pos.X - 1, pos.Y);
			Debug.Assert(wordcoordinate.X >= 0 && 
			             wordcoordinate.Y >= 0 && 
			             wordcoordinate.Y < buffer.Length &&
			             wordcoordinate.X < buffer[wordcoordinate.Y].Text.Length 
			             , "SharpDevelop.Gui.Edit.Text.BracketHighlightingSheme.GetHighlight(TextBuffer buffer, Point pos) : pos not valid.");
					
			char word = buffer[wordcoordinate.Y].Text[wordcoordinate.X];
			
			if (word == opentag) {
				Point p = Indent.SearchBracketForward(wordcoordinate, buffer, opentag, closingtag);
				if (p.X >= 0) {
					return new Highlight(p, p);
				}
			} else
			if (word == closingtag) {
				Point p = Indent.SearchBracketBackward(wordcoordinate, buffer, opentag, closingtag);
				if (p.X >= 0) {
					return new Highlight(p, p);
				}
			}
			return null;
		}
	}
	
	public class BracketHighlighter
	{
		TextAreaPainter textareapainter;
		ArrayList       bracketshemes   = new ArrayList();
		
		public BracketHighlighter(TextAreaPainter textareapainter)
		{
			bracketshemes.Add(new BracketHighlightingSheme('{', '}'));
			bracketshemes.Add(new BracketHighlightingSheme('(', ')'));
			bracketshemes.Add(new BracketHighlightingSheme('[', ']'));
			
			this.textareapainter           = textareapainter;
			textareapainter.Buffer.Changed += new EventHandler(CaretPosChanged);
			textareapainter.Caret.Changed  += new EventHandler(CaretPosChanged);
		}
		
		void CaretPosChanged(object sender, EventArgs e)
		{
			Caret caret = textareapainter.Caret;
			
			bool changed = false;
			foreach (BracketHighlightingSheme bracketsheme in bracketshemes) {
				if (bracketsheme.IsInside(textareapainter.Buffer, caret.CaretPos)) {
					Highlight highlight = bracketsheme.GetHighlight(textareapainter.Buffer, caret.CaretPos);			
					if (textareapainter.Highlight != null) {
						textareapainter.InvalidatePos(textareapainter.Caret.GetPhysicalPos(textareapainter.Highlight.Begin));
					}
					textareapainter.Highlight = highlight;
					if (highlight != null)
						changed = true;
					break;
				}
			}
			if (changed || textareapainter.Highlight != null) {
				Point p = textareapainter.Caret.GetPhysicalPos(textareapainter.Highlight.Begin);
				if (!changed)
					textareapainter.Highlight = null;
				textareapainter.InvalidatePos(p);
			}
			
		}
	}
}

⌨️ 快捷键说明

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