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

📄 textareamousehandler.cs

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

namespace SharpDevelop.Gui.Edit.Text {

	/// <summary>
	/// This class handles all mouse stuff for a textarea.
	/// </summary>
	public class TextAreaMouseHandler
	{
		TextAreaControl textarea;
		bool  doubleclick = false;
	    Point mousepos = new Point(0, 0);
		Point selbegin, selend;
		
		public TextAreaMouseHandler(TextAreaControl textarea)
		{
			this.textarea = textarea;
		}
		
		public void OnMouseDown(object sender, MouseEventArgs e)
		{ // TODO : MOUSE _ CLICK - correct cursor position
			if (doubleclick) {
				doubleclick = false;
				return;
			}
			Console.WriteLine("OnMouseDown");
			if (e.Button == MouseButtons.Left) {
				Point realmousepos = new Point(0, 0);
				realmousepos.Y = textarea.Buffer.Foldings.GetPhysicalLine((int)(mousepos.Y / textarea.TextAreaPainter.FontHeight));
				realmousepos.X = (int)(mousepos.X / textarea.TextAreaPainter.FontWidth);
				selbegin = textarea.Selection.SelectionStart;
				selend   = textarea.Selection.SelectionEnd;
				textarea.Selection.HasSomethingSelected = false;
				first = true;
//				ClearSelection();
				if (mousepos.Y > 0 && mousepos.Y < textarea.TextAreaPainter.Height) 
				{
					Point pos = new Point();
					pos.Y = Math.Min(textarea.Buffer.Length - 1,  realmousepos.Y);
					pos.X = realmousepos.X;
					
					textarea.Caret.PhysicalCaretPos = pos;
					textarea.Caret.SetUpDownPos();
				}
			} 
			textarea.Focus();
		}
		
		bool first = true; // HACK WARNING !!! GOT OnMouseMove everytime I 
		                   // open a damn window 
		public void OnMouseMove(object sender, MouseEventArgs e)
		{
			if (textarea.hidden) {
				textarea.TextAreaPainter.Cursor = Cursors.IBeam;
				textarea.hidden = false;
			}
			textarea.TextAreaPainter.OnToolTipEvent(e.X, e.Y);
		    
			textarea.ClearSelectionMove = false;
			mousepos = new Point (e.X, e.Y);
			if (e.Button == MouseButtons.Left) {
				Console.WriteLine("OnMouseMove");
				if  (!first) {
					Point realmousepos = new Point(0, 0);
					realmousepos.Y = textarea.Buffer.Foldings.GetPhysicalLine((int)(mousepos.Y / textarea.TextAreaPainter.FontHeight));
					realmousepos.X = (int)(mousepos.X / textarea.TextAreaPainter.FontWidth);
					int y1 = textarea.Caret.CaretPos.Y;
					
					if (!textarea.Selection.HasSomethingSelected) {
						textarea.Selection.SelectionStart = textarea.Caret.CaretPos;
					}
					
					Point pos = textarea.Caret.CaretPos;
	//				if (e.Y > 0 && e.Y < textarea.Height) 
					pos.Y = Math.Min(textarea.Buffer.Length - 1, realmousepos.Y);
					pos.X = realmousepos.X;
					if (pos.Y >= 0 && pos.Y < textarea.Buffer.Length) 
	
					{
						for (int i = 0;i < textarea.Buffer[pos.Y].Text.Length && i < pos.X; ++i) {
							if (textarea.Buffer[pos.Y].Text[i] == '\t')
								pos.X -= 3; //Options.options.TabIndent - 1;
						}
						pos.X = Math.Max(0, Math.Min(textarea.Buffer[pos.Y].Text.Length, pos.X));
						textarea.Caret.CaretPos = pos;
						textarea.Caret.SetUpDownPos();
						int y2 = textarea.Caret.CaretPos.Y;
						textarea.Selection.SelectionEnd = textarea.Caret.CaretPos;
					}
	//				UpdateSelection();
				}
			} 
			first = false;
			textarea.ClearSelectionMove = true;
		}
		
		void SelectWord()
		{
			TextLine tl   = textarea.Buffer[textarea.Caret.CaretPos.Y];
			int      xPos1 = textarea.Caret.CaretPos.X;
			int      xPos2 = textarea.Caret.CaretPos.X;
			
			for (;xPos1 < tl.Text.Length; ++xPos1)
				if (!(Char.IsLetterOrDigit(tl.Text[xPos1]) || tl.Text[xPos1] == '_'))
					break;
			for (;xPos2 >= 0 ; --xPos2)
				if (xPos2 < tl.Text.Length && !(Char.IsLetterOrDigit(tl.Text[xPos2]) || tl.Text[xPos2] == '_') ) {
					++xPos2;
					break;
				}
			Point p1 = new Point (xPos2, textarea.Caret.CaretPos.Y);
			Point p2 = new Point (xPos1, textarea.Caret.CaretPos.Y);
			// old == newselection -> select line 
			if (selbegin == p1 && selend == p2) {
				p1.X = 0;
				p2.X = tl.Text.Length;
			}
			textarea.Selection.SelectionStart = p1;
			textarea.Selection.SelectionEnd   = p2;
//			UpdateSelection();
		}
		
	    public void OnDoubleClick(object sender, System.EventArgs e)
	    {
	    	doubleclick = true;
	    	SelectWord();
	 //   	first       = true;
	    }
	}
}

⌨️ 快捷键说明

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