📄 textarea.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krger" email="mike@icsharpcode.net"/>
// <version value="$version"/>
// </file>
using System;
using System.Collections;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using ICSharpCode.TextEditor.Actions;
using ICSharpCode.TextEditor.Document;
namespace ICSharpCode.TextEditor
{
public delegate bool KeyEventHandler(char ch);
public delegate bool DialogKeyProcessor(Keys keyData);
/// <summary>
/// This class paints the textarea.
/// </summary>
[ToolboxItem(false)]
public class TextArea : UserControl
{
public static bool HiddenMouseCursor = false;
Point virtualTop = new Point(0, 0);
TextAreaControl motherTextAreaControl;
TextEditorControl motherTextEditorControl;
ArrayList bracketshemes = new ArrayList();
TextAreaClipboardHandler textAreaClipboardHandler;
bool autoClearSelection = false;
ArrayList leftMargins = new ArrayList();
ArrayList topMargins = new ArrayList();
TextView textView;
GutterMargin gutterMargin;
FoldMargin foldMargin;
IconBarMargin iconBarMargin;
SelectionManager selectionManager;
Caret caret;
ToolTip toolTip = new ToolTip();
bool toolTipSet = false;
public TextEditorControl MotherTextEditorControl {
get {
return motherTextEditorControl;
}
}
public TextAreaControl MotherTextAreaControl {
get {
return motherTextAreaControl;
}
}
public SelectionManager SelectionManager {
get {
return selectionManager;
}
}
public Caret Caret {
get {
return caret;
}
}
public TextView TextView {
get {
return textView;
}
}
public GutterMargin GutterMargin {
get {
return gutterMargin;
}
}
public FoldMargin FoldMargin {
get {
return foldMargin;
}
}
public IconBarMargin IconBarMargin {
get {
return iconBarMargin;
}
}
public Encoding Encoding {
get {
return motherTextEditorControl.Encoding;
}
}
public int MaxVScrollValue {
get {
return (Document.GetVisibleLine(Document.TotalNumberOfLines - 1) + 1 + TextView.VisibleLineCount * 2 / 3) * TextView.FontHeight;
}
}
public Point VirtualTop {
get {
return virtualTop;
}
set {
Point newVirtualTop = new Point(value.X, Math.Min(MaxVScrollValue, Math.Max(0, value.Y)));
if (virtualTop != newVirtualTop) {
virtualTop = newVirtualTop;
motherTextAreaControl.VScrollBar.Value = virtualTop.Y;
Invalidate();
}
}
}
public bool AutoClearSelection {
get {
return autoClearSelection;
}
set {
autoClearSelection = value;
}
}
[Browsable(false)]
public IDocument Document {
get {
return motherTextEditorControl.Document;
}
}
public TextAreaClipboardHandler ClipboardHandler {
get {
return textAreaClipboardHandler;
}
}
public ITextEditorProperties TextEditorProperties {
get {
return motherTextEditorControl.TextEditorProperties;
}
}
public TextArea(TextEditorControl motherTextEditorControl, TextAreaControl motherTextAreaControl)
{
this.motherTextAreaControl = motherTextAreaControl;
this.motherTextEditorControl = motherTextEditorControl;
caret = new Caret(this);
selectionManager = new SelectionManager(Document);
this.textAreaClipboardHandler = new TextAreaClipboardHandler(this);
ResizeRedraw = true;
SetStyle(ControlStyles.DoubleBuffer, false);
// SetStyle(ControlStyles.AllPaintingInWmPaint, true);
// SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.Opaque, false);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, true);
textView = new TextView(this);
gutterMargin = new GutterMargin(this);
foldMargin = new FoldMargin(this);
iconBarMargin = new IconBarMargin(this);
leftMargins.AddRange(new AbstractMargin[] { iconBarMargin, gutterMargin, foldMargin });
OptionsChanged();
new TextAreaMouseHandler(this).Attach();
new TextAreaDragDropHandler().Attach(this);
bracketshemes.Add(new BracketHighlightingSheme('{', '}'));
bracketshemes.Add(new BracketHighlightingSheme('(', ')'));
bracketshemes.Add(new BracketHighlightingSheme('[', ']'));
caret.PositionChanged += new EventHandler(SearchMatchingBracket);
Document.TextContentChanged += new EventHandler(TextContentChanged);
Document.FoldingManager.FoldingsChanged += new EventHandler(DocumentFoldingsChanged);
}
public void UpdateMatchingBracket()
{
SearchMatchingBracket(null, null);
}
void TextContentChanged(object sender, EventArgs e)
{
Caret.Position = new Point(0, 0);
SelectionManager.SelectionCollection.Clear();
}
void SearchMatchingBracket(object sender, EventArgs e)
{
if (!TextEditorProperties.ShowMatchingBracket) {
textView.Highlight = null;
return;
}
bool changed = false;
if (caret.Offset == 0) {
if (textView.Highlight != null) {
int line = textView.Highlight.OpenBrace.Y;
int line2 = textView.Highlight.CloseBrace.Y;
textView.Highlight = null;
UpdateLine(line);
UpdateLine(line2);
}
return;
}
foreach (BracketHighlightingSheme bracketsheme in bracketshemes) {
// if (bracketsheme.IsInside(textareapainter.Document, textareapainter.Document.Caret.Offset)) {
Highlight highlight = bracketsheme.GetHighlight(Document, Caret.Offset - 1);
if (textView.Highlight != null && textView.Highlight.OpenBrace.Y >=0 && textView.Highlight.OpenBrace.Y < Document.TotalNumberOfLines) {
UpdateLine(textView.Highlight.OpenBrace.Y);
}
if (textView.Highlight != null && textView.Highlight.CloseBrace.Y >=0 && textView.Highlight.CloseBrace.Y < Document.TotalNumberOfLines) {
UpdateLine(textView.Highlight.CloseBrace.Y);
}
textView.Highlight = highlight;
if (highlight != null) {
changed = true;
break;
}
// }
}
if (changed || textView.Highlight != null) {
int line = textView.Highlight.OpenBrace.Y;
int line2 = textView.Highlight.CloseBrace.Y;
if (!changed) {
textView.Highlight = null;
}
UpdateLine(line);
UpdateLine(line2);
}
}
public void SetDesiredColumn()
{
Caret.DesiredColumn = TextView.GetDrawingXPos(Caret.Line, Caret.Column) + (int)(VirtualTop.X * textView.GetWidth(' '));
// Console.WriteLine("SetDesiredColumn : " + Caret.DesiredColumn);
}
public void SetCaretToDesiredColumn(int caretLine)
{
// Console.WriteLine("Calling GetLogicalColumn, DesiredColumn = " + Caret.DesiredColumn);
Caret.Position = textView.GetLogicalColumn(Caret.Line, Caret.DesiredColumn + (int)(VirtualTop.X * textView.GetWidth(' ')));
// Console.WriteLine("SetCaretToDesiredColumn : " + Caret.Position);
}
public void OptionsChanged()
{
UpdateMatchingBracket();
textView.OptionsChanged();
caret.RecreateCaret();
caret.UpdateCaretPosition();
Refresh();
}
AbstractMargin lastMouseInMargin;
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
this.Cursor = Cursors.Default;
if (lastMouseInMargin != null) {
lastMouseInMargin.HandleMouseLeave(EventArgs.Empty);
lastMouseInMargin = null;
}
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseDown(e);
foreach (AbstractMargin margin in leftMargins) {
if (margin.DrawingPosition.Contains(e.X, e.Y)) {
margin.HandleMouseDown(new Point(e.X, e.Y), e.Button);
}
}
}
string oldToolTip;
public void SetToolTip(string text)
{
toolTipSet = (text != null);
if (oldToolTip == text)
return;
//ToolTip toolTip = this.toolTip;
if (text == null) {
//Console.WriteLine("Tooltip disabled");
toolTip.SetToolTip(this, null);
} else {
//Console.WriteLine("Tooltip set to " + text);
//Point p = PointToClient(Control.MousePosition);
//p.Offset(3, 3);
toolTip.SetToolTip(this, text);
}
oldToolTip = text;
}
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
toolTipSet = false;
base.OnMouseMove(e);
if (!toolTipSet)
SetToolTip(null);
foreach (AbstractMargin margin in leftMargins) {
if (margin.DrawingPosition.Contains(e.X, e.Y)) {
this.Cursor = margin.Cursor;
margin.HandleMouseMove(new Point(e.X, e.Y), e.Button);
if (lastMouseInMargin != margin) {
if (lastMouseInMargin != null) {
lastMouseInMargin.HandleMouseLeave(EventArgs.Empty);
}
lastMouseInMargin = margin;
}
return;
}
}
if (lastMouseInMargin != null) {
lastMouseInMargin.HandleMouseLeave(EventArgs.Empty);
lastMouseInMargin = null;
}
if (textView.DrawingPosition.Contains(e.X, e.Y)) {
this.Cursor = textView.Cursor;
return;
}
this.Cursor = Cursors.Default;
}
AbstractMargin updateMargin = null;
public void Refresh(AbstractMargin margin)
{
updateMargin = margin;
Invalidate(updateMargin.DrawingPosition);
Update();
updateMargin = null;
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
int currentXPos = 0;
int currentYPos = 0;
bool adjustScrollBars = false;
Graphics g = e.Graphics;
Rectangle clipRectangle = e.ClipRectangle;
if (updateMargin != null) {
try {
updateMargin.Paint(g, updateMargin.DrawingPosition);
} catch (Exception ex) {
Console.WriteLine("Got exception : " + ex);
}
// clipRectangle.Intersect(updateMargin.DrawingPosition);
}
if (clipRectangle.Width <= 0 || clipRectangle.Height <= 0) {
return;
}
if (this.TextEditorProperties.UseAntiAliasedFont) {
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
} else {
g.TextRenderingHint = TextRenderingHint.SystemDefault;
}
foreach (AbstractMargin margin in leftMargins) {
if (margin.IsVisible) {
Rectangle marginRectangle = new Rectangle(currentXPos , currentYPos, margin.Size.Width, Height - currentYPos);
if (marginRectangle != margin.DrawingPosition) {
adjustScrollBars = true;
margin.DrawingPosition = marginRectangle;
}
currentXPos += margin.DrawingPosition.Width;
if (clipRectangle.IntersectsWith(marginRectangle)) {
marginRectangle.Intersect(clipRectangle);
if (!marginRectangle.IsEmpty) {
try {
margin.Paint(g, marginRectangle);
} catch (Exception ex) {
Console.WriteLine("Got exception : " + ex);
}
}
}
}
}
Rectangle textViewArea = new Rectangle(currentXPos, currentYPos, Width - currentXPos, Height - currentYPos);
if (textViewArea != textView.DrawingPosition) {
adjustScrollBars = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -