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

📄 cursorkeys.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
// CursorKeys.cs 
// Copyright (C) 2000 Mike Krueger
// Copyright (C) 2000 Andrea Paatz
//
// 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.Drawing;
using System.Windows.Forms;
using System;

using SharpDevelop.Gui.Edit.Text;
using SharpDevelop.Actions;

namespace SharpDevelop.Actions.Edit {
	
	public class CaretLeft : ISdEditAction
	{
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			if (caretpos.X > 0) {
				--caretpos.X;
			} else {
				if (caretpos.Y > 0) {
					caretpos.Y = executor.TextArea.Caret.GetFirstValidPosLower(caretpos.Y);
					caretpos.X = executor.TextArea.Buffer[caretpos.Y].Text.Length;
				}
			}
			if (caretpos.Y != -1) {
				executor.TextArea.Caret.CaretPos = caretpos;
				executor.TextArea.Caret.SetUpDownPos();
			}
		}
	} 
	
	public class CaretRight : ISdEditAction
	{
		
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			if (executor.TextArea.Options.CursorBehindEOL || caretpos.X < executor.TextArea.Buffer[caretpos.Y].Text.Length) {
				++caretpos.X;
			} else {
				if (caretpos.Y < executor.TextArea.Buffer.Length - 1) {
					caretpos.Y = executor.TextArea.Caret.GetFirstValidPosGreater(caretpos.Y);
					caretpos.X = 0;
				}
			}
			if (caretpos.Y != -1) {
				executor.TextArea.Caret.CaretPos = caretpos;
				executor.TextArea.Caret.SetUpDownPos();
			}
		}
	}
	
	
	public class CaretUp : ISdEditAction
	{
		
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			caretpos.Y = Math.Max(0, executor.TextArea.Caret.GetFirstValidPosLower(caretpos.Y));
			caretpos.X = executor.TextArea.Caret.UpDownPos;
			
//			if (caretpos != executor.TextArea.Caret.PhysicalCaretPos)
			if (caretpos.Y != -1) 
				executor.TextArea.Caret.PhysicalCaretPos = caretpos;
		}
		
	}
	
	public class CaretDown : ISdEditAction
	{
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			caretpos.Y = Math.Min(executor.TextArea.Caret.GetFirstValidPosGreater(caretpos.Y), 
				                  executor.TextArea.Caret.GetFirstValidPosLower(executor.TextArea.Buffer.Length));
			caretpos.X = executor.TextArea.Caret.UpDownPos;
			
			if (caretpos.Y != -1) 
			if (caretpos != executor.TextArea.Caret.PhysicalCaretPos)
				executor.TextArea.Caret.PhysicalCaretPos = caretpos;
		}
	}
	
	
	public class ShiftCaretRight : CaretRight
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
	
	public class ShiftCaretLeft : CaretLeft
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
	
	public class ShiftCaretUp : CaretUp
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
	
	public class ShiftCaretDown : CaretDown
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
	
	
	public class ScrollLineUp : ISdEditAction
	{
		public void Execute(ISdEditActionExecutor executor)
		{
			executor.TextArea.SetScrollTo(Math.Max(0, executor.TextArea.GetFirstLine() - 1));
		}
	}
	
	public class ScrollLineDown : ISdEditAction
	{
		public void Execute(ISdEditActionExecutor executor)
		{
			executor.TextArea.SetScrollTo(Math.Min(executor.TextArea.GetMaxScroll(), executor.TextArea.GetFirstLine() + 1));
		}
	}
	
	public class WordRight : ISdEditAction
	{
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			if (caretpos.X == executor.TextArea.Buffer[caretpos.Y].Text.Length && 
				caretpos.Y == executor.TextArea.Buffer.Length - 1)
				return;
			if (caretpos.X == executor.TextArea.Buffer[caretpos.Y].Text.Length) {
				caretpos.X = 0;
				caretpos.Y = Math.Min(executor.TextArea.Caret.GetFirstValidPosGreater(caretpos.Y), 
					                  executor.TextArea.Caret.GetFirstValidPosLower(executor.TextArea.Buffer.Length));
			} else {
				int i = caretpos.X;
				
				while (i < executor.TextArea.Buffer[caretpos.Y].Text.Length && Char.IsLetterOrDigit(executor.TextArea.Buffer[caretpos.Y].Text[i]))
					++i;
				
				while (i < executor.TextArea.Buffer[caretpos.Y].Text.Length && !Char.IsLetterOrDigit(executor.TextArea.Buffer[caretpos.Y].Text[i]))
					++i;
				
				caretpos.X = i;
			}
			executor.TextArea.Caret.CaretPos = caretpos;
			executor.TextArea.Caret.SetUpDownPos();
		}
	}
	
	public class WordLeft : ISdEditAction
	{
		public virtual void Execute(ISdEditActionExecutor executor)
		{
			Point caretpos = executor.TextArea.Caret.CaretPos;
			if (caretpos.X == 0 && caretpos.Y == 0)
				return;
			if (caretpos.X == 0){
				caretpos.Y = executor.TextArea.Caret.GetFirstValidPosLower(caretpos.Y);
				caretpos.X = executor.TextArea.Buffer[caretpos.Y].Text.Length;
			} else {
				int i = caretpos.X;
				
				while (i > 0 && !Char.IsLetterOrDigit(executor.TextArea.Buffer[caretpos.Y].Text[i - 1]))
					--i;
				
				while (i > 0 && Char.IsLetterOrDigit(executor.TextArea.Buffer[caretpos.Y].Text[i - 1]))
					--i;
				
				caretpos.X = i;
			}
			executor.TextArea.Caret.CaretPos = caretpos;
			executor.TextArea.Caret.SetUpDownPos();
		}
	}
	
	public class ShiftWordRight : WordRight
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
	
	public class ShiftWordLeft : WordLeft
	{
		public override void Execute(ISdEditActionExecutor executor)
		{
			Point oldpos = executor.TextArea.Caret.CaretPos;
			executor.TextArea.ClearSelectionMove = false;
			base.Execute(executor);
			executor.TextArea.ClearSelectionMove = true;
			executor.TextArea.Selection.SetSelectionEnd(oldpos, executor.TextArea.Caret.CaretPos);
		}
	}
}

⌨️ 快捷键说明

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