📄 textareacontrol.cs
字号:
++x;
Point p = Caret.CaretPos;
p.X -= Caret.CaretPos.X - x;
Buffer.Delete(p, Caret.CaretPos);
Caret.CaretPos = p;
}
public void InsertTemplate(CodeTemplate template)
{
DeleteWordBeforeCaret();
Point newCursorPos = TextArea.Caret.CaretPos;
for (int i =0; i < template.Text.Length; ++i) {
switch (template.Text[i]) {
case '|':
newCursorPos = Caret.CaretPos;
break;
case '\t':
ProcessDialogKey(Keys.Tab);
break;
case '\n':
ProcessDialogKey(Keys.Return);
break;
default:
KeyPressed(null, new KeyPressEventArgs(template.Text[i]));
break;
}
}
Caret.CaretPos = newCursorPos;
}
public void KeyPressed(object sender, KeyPressEventArgs e)
{
if (Buffer.ReadOnly)
return;
if (!hidden) {
TextAreaPainter.Cursor = null;
hidden = true;
}
char ch = e.KeyChar;
if (Options.AutoInsertTemplates && ch == ' ') {
string word = GetWordBeforeCaret();
if (word != null)
foreach (CodeTemplate template in CodeTemplateLoader.Template) {
if (template.Shortcut == word) {
InsertTemplate(template);
return;
}
}
}
if (ch >= ' ' && (int)ch < 256) {
clipboardhandler.Delete(null, null);
bool overwritten = false;
if (!Caret.InsertMode && Caret.CaretPos.X < Buffer[Caret.CaretPos.Y].Text.Length) {
Buffer.Delete(Caret.CaretPos, new Point(Caret.CaretPos.X + 1, Caret.CaretPos.Y));
overwritten = true;
}
Point pos = Buffer.Insert(Caret.CaretPos, ch.ToString());
if (ch == '{' || ch == '}') {
Indent.IndentLine(Buffer, pos.Y, Options.IndentStyle);
pos.X = Math.Min(pos.X, Buffer[pos.Y].Text.Length);
}
Caret.CaretPos = pos;
UpdateLine(pos.Y, 0, 0);
if (overwritten)
UndoStack.UndoLast(2);
}
ScrollToCaret();
}
public bool ProcessDialogKey2(Keys keyData)
{
return ProcessDialogKey(keyData);
}
protected override bool ProcessDialogKey(Keys keyData)
{
// Hack : two different / key codes !?!
if ((int)keyData == 131263)
keyData = Keys.Divide | Keys.Control;
foreach (EditAction action in EditActionLoader.EditAction) {
foreach (Keys shortcut in action.ShortCut) {
if (shortcut == keyData) {
action.ActionPerformed(this);
return true;
}
}
}
return base.ProcessDialogKey(keyData);
}
void NewCaretPos(object sender, EventArgs e)
{
switch (Options.LineViewerStyle) {
case LineViewerStyle.None:
textarea.InvalidatePos(Caret.PhysicalOldCaretPos);
textarea.InvalidatePos(Caret.PhysicalCaretPos);
break;
case LineViewerStyle.FullRow:
if (Caret.OldCaretPos.Y == Caret.CaretPos.Y)
goto case LineViewerStyle.None;
InvalidateLines(0, Caret.OldCaretPos.Y, Caret.OldCaretPos.Y);
InvalidateLines(0, Caret.CaretPos.Y, Caret.CaretPos.Y);
break;
}
textarea.Update();
if (ClearSelectionMove)
Selection.SetNoSelection();
ScrollToCaret();
}
void TextAreaRefreshEvt(object sender, EventArgs e)
{
textarea.Refresh();
}
public void BufferChange(object sender, EventArgs e)
{
int max = textarea.Buffer[0].Text.Length;
for (int i = 1; i < textarea.Buffer.Length; ++i)
max = Math.Max(max, textarea.Buffer[i].Text.Length);
max += lastVisibleChar - 1;
int newwidth = (int)(15 + (Options.ShowLineNumbers ? (Math.Max(3, (int)(Math.Log10(Buffer.Length) + 1))) * textarea.FontWidth : 0));
if (newwidth != lineview.Width) {
lineview.Width = newwidth;
panel_resize(null, null);
}
int newWidth = Math.Max(textareapanel.Width, (int)(max * textarea.FontWidth));
if (textarea.Width < newWidth)
textarea.Width = newWidth + (int)(20 * textarea.FontWidth);
int newHeight = Math.Max(textareapanel.Height, (int)(textarea.Buffer.Length * textarea.FontHeight) + Height);
if (textarea.Height < newHeight) {
textarea.Height = newHeight + (int)(20 * textarea.FontHeight);
lineview.Height = textarea.Height;
}
// do buffer refresh (SPAN insertion etc.)
if (textarea.Buffer.UpdateRequested) {
textarea.Buffer.UpdateRequested = false;
textarea.Refresh();
lineview.Refresh();
}
}
private void ScrollVScrollBar(object sender, EventArgs e)
{
int vAbsPos = (int)((vscrollbar.Value - vscrollbar.Minimum) * textarea.FontHeight);
lineview.Top = textarea.Top = - vAbsPos;
///
}
private void ScrollHScrollBar(object sender, EventArgs e)
{
int hAbsPos = (int)((hscrollbar.Value - hscrollbar.Minimum) * textarea.FontWidth);
textarea.Left = -hAbsPos;
}
public void panel_resize(object sender, EventArgs e)
{
vscrollbar.Height = mainpanel.ClientSize.Height - SystemInformation.VerticalScrollBarWidth;
vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
vscrollbar.Location = new Point (mainpanel.ClientSize.Width - SystemInformation.VerticalScrollBarWidth, 0);
hscrollbar.Width = mainpanel.ClientSize.Width - SystemInformation.HorizontalScrollBarHeight;
hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
hscrollbar.Location = new Point (0, mainpanel.ClientSize.Height - SystemInformation.HorizontalScrollBarHeight);
rowpanel.Location = new Point(0, 0);
rowpanel.Size = new Size(mainpanel.ClientSize.Width - SystemInformation.VerticalScrollBarWidth,
SystemInformation.HorizontalScrollBarHeight);
linenumberpanel.Location = new Point(0, rowpanel.Visible ? rowpanel.Height : 0);
linenumberpanel.Size = new Size(lineview.Width,
mainpanel.ClientSize.Height - SystemInformation.HorizontalScrollBarHeight - (rowpanel.Visible ? rowpanel.Height : 0));
textareapanel.Top = 0;
textareapanel.Left = linenumberpanel.Right;
textareapanel.Size = new Size(mainpanel.ClientSize.Width - SystemInformation.VerticalScrollBarWidth - lineview.Width,
mainpanel.ClientSize.Height - SystemInformation.HorizontalScrollBarHeight - (rowpanel.Visible ? rowpanel.Height : 0));
ruler.Size = new Size(Math.Max(textareapanel.Width, textarea.Width) + lineview.Width, rowpanel.Height);
}
void MyResize(object sender, EventArgs e)
{
if (Height == 0 || Width == 0)
return;
BufferChange(null, null);
vscrollbar.Maximum = (int)((textarea.Height - textareapanel.Height) / textarea.FontHeight);
hscrollbar.Maximum = (int)((textarea.Width - textareapanel.Width) / textarea.FontWidth);
lastVisibleChar = (int)((textareapanel.Width) / textarea.FontWidth) - 3;
lastVisibleLine = (int)(textareapanel.Height / textarea.FontHeight);
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
int newvalue = vscrollbar.Value - Math.Sign(e.Delta) * System.Windows.Forms.SystemInformation.MouseWheelScrollLines;
vscrollbar.Value = Math.Max(vscrollbar.Minimum, Math.Min(vscrollbar.Maximum, newvalue));
}
public void CenterCaret()
{
int vscroll = Caret.PhysicalCaretPos.Y - lastVisibleLine / 2;
if (vscroll < vscrollbar.Minimum)
vscroll = vscrollbar.Minimum;
if (vscroll > vscrollbar.Maximum)
vscroll = vscrollbar.Maximum;
vscrollbar.Value = vscroll;
}
public void ScrollToCaret()
{
int curCharMin = hscrollbar.Value - hscrollbar.Minimum;
int curCharMax = curCharMin + lastVisibleChar;
int pos = Caret.PhysicalCaretPos.X;
int scrollwidth = 2;
if (pos < curCharMin) {
hscrollbar.Value = hscrollbar.Minimum + Math.Max(0, pos - scrollwidth);
} else {
if (pos > curCharMax)
hscrollbar.Value = Math.Min(hscrollbar.Maximum,
pos - lastVisibleChar + scrollwidth + hscrollbar.Minimum);
}
ScrollTo(Caret.PhysicalCaretPos.Y);
}
public int GetFirstLine()
{
return vscrollbar.Value;
}
public int GetMaxScroll()
{
return vscrollbar.Maximum;
}
public void SetScrollTo(int line)
{
vscrollbar.Value = line;
}
public void ScrollTo(int line)
{
int curLineMin = vscrollbar.Value - vscrollbar.Minimum;
if (line - 2 < curLineMin) {
vscrollbar.Value = Math.Max(vscrollbar.Minimum, line - 2 + vscrollbar.Minimum);
} else {
int curLineMax = curLineMin + lastVisibleLine;
if (line + 2 > curLineMax)
vscrollbar.Value = Math.Min(vscrollbar.Maximum, line - lastVisibleLine + 2 + vscrollbar.Minimum);
}
}
public TextAreaPainter TextAreaPainter
{
get {
return textarea;
}
}
Point OldSelectionStart, OldSelectionEnd;
public void UpdateSelection(object sender, EventArgs e)
{
int min = Math.Min(Selection.SelectionStart.Y, Selection.SelectionEnd.Y);
int max = Math.Max(Selection.SelectionStart.Y, Selection.SelectionEnd.Y);
int oldmin = Math.Min(OldSelectionStart.Y, OldSelectionEnd.Y);
int oldmax = Math.Max(OldSelectionStart.Y, OldSelectionEnd.Y);
int xPos = 0;
if (Selection.SelectionStart == Selection.SelectionEnd) { // clear old selection
UpdateLines(xPos, oldmin, oldmax);
} else {
if (OldSelectionStart == OldSelectionEnd){ // new selection
UpdateLines(xPos, min, max);
} else { // update old selection
UpdateLines(xPos, Math.Min(Selection.SelectionEnd.Y, OldSelectionEnd.Y), Math.Max(Selection.SelectionEnd.Y, OldSelectionEnd.Y));
}
}
OldSelectionStart = Selection.SelectionStart;
OldSelectionEnd = Selection.SelectionEnd;
}
public void UpdateLines(int lineBegin, int lineEnd)
{
UpdateLines(0, lineBegin, lineEnd);
}
public void UpdateToEnd(int lineBegin)
{
int firstLine = vscrollbar.Value;
lineBegin = Buffer.Foldings.GetLogicalLine(lineBegin);
lineBegin = Math.Max(lineBegin, vscrollbar.Value);
int y = Math.Max( 0, (int)(lineBegin * textarea.FontHeight));
textarea.Invalidate(new Rectangle(0, y - 1,
(int)textarea.Width, textarea.Height));
}
public void UpdateLine(int line, int begin, int end)
{
UpdateLines(line, line);
}
public void UpdateLines(int xPos, int lineBegin, int lineEnd)
{
lineBegin = lineBegin;
lineEnd = lineEnd;
InvalidateLines(0, lineBegin, lineEnd);
}
public void InvalidateLines(int xPos, int lineBegin, int lineEnd)
{
int firstLine = vscrollbar.Value;
lineBegin = Buffer.Foldings.GetLogicalLine(lineBegin);
lineEnd = Buffer.Foldings.GetLogicalLine(lineEnd);
lineBegin = Math.Max(lineBegin, vscrollbar.Value);
lineEnd = Math.Min(lineEnd, vscrollbar.Value + lastVisibleLine);
int y = Math.Max( 0, (int)(lineBegin * textarea.FontHeight));
int height = Math.Min(textarea.Height, (int)((1 + lineEnd - lineBegin) * (textarea.FontHeight + 1)));
textarea.Invalidate(new Rectangle(xPos, y - 1, textarea.Width, height + 2));
}
public void InvalidateLines(int from, int length, int lineBegin, int lineEnd)
{
int firstLine = vscrollbar.Value;
lineBegin = Buffer.Foldings.GetLogicalLine(lineBegin);
lineEnd = Buffer.Foldings.GetLogicalLine(lineEnd);
lineBegin = Math.Max(lineBegin, vscrollbar.Value);
lineEnd = Math.Min(lineEnd, vscrollbar.Value + lastVisibleLine);
int y = Math.Max( 0, (int)(lineBegin * textarea.FontHeight));
int height = Math.Min(textarea.Height, (int)((1 + lineEnd - lineBegin) * (textarea.FontHeight + 1)));
textarea.Invalidate(new Rectangle((int)(textarea.FontWidth * from), y - 1, (int)(textarea.FontWidth * length), height + 2));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -