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

📄 caret.cs

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

namespace SharpDevelop.Gui.Edit.Text {

	/// <summary>
	/// This class represents the text caret.
	/// </summary>
	public class Caret
	{
		TextBuffer buffer = null;
		
		Point  oldpos        = new Point(0, 0);
		Point  caretpos      = new Point(0, 0);
		Point  phys_oldpos   = new Point(0, 0);
		Point  phys_caretpos = new Point(0, 0);
		int    updownpos = 0;
		
		bool   insertmode = true;
		bool   visible    = true;
		
		public event EventHandler Changed;
		
		public Point OldCaretPos {
			get {
				return oldpos;
			}
		}
		
		public Point CaretPos {
			get {
				return caretpos;
			}
			set {
				oldpos = caretpos;
				caretpos = value;
				
				CheckCaretPos();
				
				phys_oldpos   = phys_caretpos;
				phys_caretpos = GetPhysicalPos(caretpos);
				
				OnChanged();
			}
		}
		
		public Point PhysicalOldCaretPos {
			get {
				return phys_oldpos;
			}
		}
		
		public Point PhysicalCaretPos {
			get {
				return phys_caretpos;
			}
			
			set {
				Point to = value; //physik
				
				int max = buffer[to.Y].Text.Length;
				
				int xpos    = 0; //logik
				int realpos = 0; //physik
				
				for (int i = 0; i < max; ++i) {
					if (realpos >= to.X)
						break;
					
					if (buffer[to.Y].Text[i] == '\t') {
						realpos += buffer.Options.TabIndent;
					} else 
						++realpos;
					
					++xpos;
				}
				
				if (to.X > realpos && buffer.Options.CursorBehindEOL)
					xpos += to.X - realpos;
				to.X = xpos;
				CaretPos = to;
			}
		}
		
		public int UpDownPos {
			get {
				return updownpos;
			}
			/*
			set {
				updownpos = value;
				OnChanged();
			}*/
		}
		
		public bool Visible {
			get {
				return visible;
			}
			set {
				visible = value;
			}
		}
		
		public bool InsertMode {
			get {
				return insertmode;
			}
			set {
				insertmode = value;
				OnChanged();
			}
		}
		
		public Caret(TextBuffer buffer)
		{
			this.buffer  = buffer;
		}
		
		void OnChanged()
		{
			if (Changed != null)
				Changed(this, null);
		}
		
		public void SetUpDownPos()
		{
			updownpos = phys_caretpos.X;
		}
		
		/// <summary>
		/// Gets the physical pos of the logical position p (tabs are x spaces long), p 
		/// may be outside of the textbuffer bounds. (in this case p is returned)
		/// </summary>
		public Point GetPhysicalPos(Point p)
		{
			if (p.Y >= buffer.Length) 
				return p;
			int max = Math.Min(buffer[p.Y].Text.Length, p.X);
			int xpos = 0;
			for (int i = 0; i < max; ++i) {
				if (buffer[p.Y].Text[i] == '\t') {
					xpos += buffer.Options.TabIndent;
				} else 
					++xpos;
			}
			if (p.X > max)
				xpos += p.X - max;
			p.X = xpos;
			p.Y = buffer.Foldings.GetLogicalLine(p.Y);
			return p;
		}
		
		public void CheckCaretPos()
		{
			Point pos = caretpos;
			
			if (pos.Y >= buffer.Length) 
				pos.Y = buffer.Length - 1;
			
			if (!buffer.Options.CursorBehindEOL && pos.X > buffer[pos.Y].Text.Length)
				pos.X = buffer[pos.Y].Text.Length;
			
			if (pos != CaretPos)
				CaretPos = pos;
		}
		public int GetFirstValidPosLower(int ypos)
		{
			for (int i = ypos - 1; i >= 0; --i)
				if (buffer[i].Visible)
					return i;
			return -1;
		}
		public int GetFirstValidPosGreater(int ypos)
		{
			for (int i = ypos + 1; i < buffer.Length; ++i)
				if (buffer[i].Visible)
					return i;
			return GetFirstValidPosLower(buffer.Length);
		}
		
	}
}

⌨️ 快捷键说明

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