You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
426 lines
12 KiB
426 lines
12 KiB
// <file> |
|
// <copyright see="prj:///doc/copyright.txt"/> |
|
// <license see="prj:///doc/license.txt"/> |
|
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
// <version>$Revision$</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 |
|
{ |
|
/// <summary> |
|
/// This class paints the textarea. |
|
/// </summary> |
|
[ToolboxItem(false)] |
|
public class TextAreaControl : Panel |
|
{ |
|
TextEditorControl motherTextEditorControl; |
|
|
|
HRuler hRuler = null; |
|
|
|
VScrollBar vScrollBar = new VScrollBar(); |
|
HScrollBar hScrollBar = new HScrollBar(); |
|
TextArea textArea; |
|
bool doHandleMousewheel = true; |
|
bool disposed; |
|
|
|
public TextArea TextArea { |
|
get { |
|
return textArea; |
|
} |
|
} |
|
|
|
public SelectionManager SelectionManager { |
|
get { |
|
return textArea.SelectionManager; |
|
} |
|
} |
|
|
|
public Caret Caret { |
|
get { |
|
return textArea.Caret; |
|
} |
|
} |
|
|
|
[Browsable(false)] |
|
public IDocument Document { |
|
get { |
|
if (motherTextEditorControl != null) |
|
return motherTextEditorControl.Document; |
|
return null; |
|
} |
|
} |
|
|
|
public ITextEditorProperties TextEditorProperties { |
|
get { |
|
if (motherTextEditorControl != null) |
|
return motherTextEditorControl.TextEditorProperties; |
|
return null; |
|
} |
|
} |
|
|
|
public VScrollBar VScrollBar { |
|
get { |
|
return vScrollBar; |
|
} |
|
} |
|
|
|
public HScrollBar HScrollBar { |
|
get { |
|
return hScrollBar; |
|
} |
|
} |
|
|
|
public bool DoHandleMousewheel { |
|
get { |
|
return doHandleMousewheel; |
|
} |
|
set { |
|
doHandleMousewheel = value; |
|
} |
|
} |
|
|
|
public TextAreaControl(TextEditorControl motherTextEditorControl) |
|
{ |
|
this.motherTextEditorControl = motherTextEditorControl; |
|
|
|
this.textArea = new TextArea(motherTextEditorControl, this); |
|
Controls.Add(textArea); |
|
|
|
vScrollBar.ValueChanged += new EventHandler(VScrollBarValueChanged); |
|
Controls.Add(this.vScrollBar); |
|
|
|
hScrollBar.ValueChanged += new EventHandler(HScrollBarValueChanged); |
|
Controls.Add(this.hScrollBar); |
|
ResizeRedraw = true; |
|
|
|
Document.DocumentChanged += AdjustScrollBarsOnDocumentChange; |
|
Document.UpdateCommited += AdjustScrollBarsOnCommittedUpdate; |
|
} |
|
|
|
protected override void Dispose(bool disposing) |
|
{ |
|
if (disposing) { |
|
if (!disposed) { |
|
disposed = true; |
|
Document.DocumentChanged -= AdjustScrollBarsOnDocumentChange; |
|
Document.UpdateCommited -= AdjustScrollBarsOnCommittedUpdate; |
|
motherTextEditorControl = null; |
|
if (vScrollBar != null) { |
|
vScrollBar.Dispose(); |
|
vScrollBar = null; |
|
} |
|
if (hScrollBar != null) { |
|
hScrollBar.Dispose(); |
|
hScrollBar = null; |
|
} |
|
if (hRuler != null) { |
|
hRuler.Dispose(); |
|
hRuler = null; |
|
} |
|
} |
|
} |
|
base.Dispose(disposing); |
|
} |
|
|
|
protected override void OnResize(System.EventArgs e) |
|
{ |
|
base.OnResize(e); |
|
ResizeTextArea(); |
|
} |
|
|
|
public void ResizeTextArea() |
|
{ |
|
int y = 0; |
|
int h = 0; |
|
if (hRuler != null) { |
|
hRuler.Bounds = new Rectangle(0, |
|
0, |
|
Width - SystemInformation.HorizontalScrollBarArrowWidth, |
|
textArea.TextView.FontHeight); |
|
|
|
y = hRuler.Bounds.Bottom; |
|
h = hRuler.Bounds.Height; |
|
} |
|
|
|
textArea.Bounds = new Rectangle(0, y, |
|
Width - SystemInformation.HorizontalScrollBarArrowWidth, |
|
Height - SystemInformation.VerticalScrollBarArrowHeight - h); |
|
SetScrollBarBounds(); |
|
} |
|
|
|
public void SetScrollBarBounds() |
|
{ |
|
vScrollBar.Bounds = new Rectangle(textArea.Bounds.Right, 0, SystemInformation.HorizontalScrollBarArrowWidth, Height - SystemInformation.VerticalScrollBarArrowHeight); |
|
hScrollBar.Bounds = new Rectangle(0, |
|
textArea.Bounds.Bottom, |
|
Width - SystemInformation.HorizontalScrollBarArrowWidth, |
|
SystemInformation.VerticalScrollBarArrowHeight); |
|
} |
|
|
|
bool adjustScrollBarsOnNextUpdate; |
|
Point scrollToPosOnNextUpdate; |
|
|
|
void AdjustScrollBarsOnDocumentChange(object sender, DocumentEventArgs e) |
|
{ |
|
if (motherTextEditorControl.IsInUpdate == false) { |
|
AdjustScrollBarsClearCache(); |
|
AdjustScrollBars(); |
|
} else { |
|
adjustScrollBarsOnNextUpdate = true; |
|
} |
|
} |
|
|
|
void AdjustScrollBarsOnCommittedUpdate(object sender, EventArgs e) |
|
{ |
|
if (motherTextEditorControl.IsInUpdate == false) { |
|
if (!scrollToPosOnNextUpdate.IsEmpty) { |
|
ScrollTo(scrollToPosOnNextUpdate.Y, scrollToPosOnNextUpdate.X); |
|
} |
|
if (adjustScrollBarsOnNextUpdate) { |
|
AdjustScrollBarsClearCache(); |
|
AdjustScrollBars(); |
|
} |
|
} |
|
} |
|
|
|
int[] lineLengthCache; |
|
const int LineLengthCacheAdditionalSize = 100; |
|
|
|
void AdjustScrollBarsClearCache() |
|
{ |
|
if (lineLengthCache != null) { |
|
if (lineLengthCache.Length < this.Document.TotalNumberOfLines + 2 * LineLengthCacheAdditionalSize) { |
|
lineLengthCache = null; |
|
} else { |
|
Array.Clear(lineLengthCache, 0, lineLengthCache.Length); |
|
} |
|
} |
|
} |
|
|
|
public void AdjustScrollBars() |
|
{ |
|
adjustScrollBarsOnNextUpdate = false; |
|
vScrollBar.Minimum = 0; |
|
// number of visible lines in document (folding!) |
|
vScrollBar.Maximum = textArea.MaxVScrollValue; |
|
int max = 0; |
|
|
|
int firstLine = textArea.TextView.FirstVisibleLine; |
|
int lastLine = this.Document.GetFirstLogicalLine(textArea.TextView.FirstPhysicalLine + textArea.TextView.VisibleLineCount); |
|
if (lastLine >= this.Document.TotalNumberOfLines) |
|
lastLine = this.Document.TotalNumberOfLines - 1; |
|
|
|
if (lineLengthCache == null || lineLengthCache.Length <= lastLine) { |
|
lineLengthCache = new int[lastLine + LineLengthCacheAdditionalSize]; |
|
} |
|
|
|
for (int lineNumber = firstLine; lineNumber <= lastLine; lineNumber++) { |
|
LineSegment lineSegment = this.Document.GetLineSegment(lineNumber); |
|
if (Document.FoldingManager.IsLineVisible(lineNumber)) { |
|
if (lineLengthCache[lineNumber] > 0) { |
|
max = Math.Max(max, lineLengthCache[lineNumber]); |
|
} else { |
|
int visualLength = textArea.TextView.GetVisualColumnFast(lineSegment, lineSegment.Length); |
|
lineLengthCache[lineNumber] = Math.Max(1, visualLength); |
|
max = Math.Max(max, visualLength); |
|
} |
|
} |
|
} |
|
hScrollBar.Minimum = 0; |
|
hScrollBar.Maximum = (Math.Max(max + 20, textArea.TextView.VisibleColumnCount - 1)); |
|
|
|
vScrollBar.LargeChange = Math.Max(0, textArea.TextView.DrawingPosition.Height); |
|
vScrollBar.SmallChange = Math.Max(0, textArea.TextView.FontHeight); |
|
|
|
hScrollBar.LargeChange = Math.Max(0, textArea.TextView.VisibleColumnCount - 1); |
|
hScrollBar.SmallChange = Math.Max(0, (int)textArea.TextView.SpaceWidth); |
|
} |
|
|
|
public void OptionsChanged() |
|
{ |
|
textArea.OptionsChanged(); |
|
|
|
if (textArea.TextEditorProperties.ShowHorizontalRuler) { |
|
if (hRuler == null) { |
|
hRuler = new HRuler(textArea); |
|
Controls.Add(hRuler); |
|
ResizeTextArea(); |
|
} |
|
} else { |
|
if (hRuler != null) { |
|
Controls.Remove(hRuler); |
|
hRuler.Dispose(); |
|
hRuler = null; |
|
ResizeTextArea(); |
|
} |
|
} |
|
|
|
AdjustScrollBars(); |
|
} |
|
|
|
void VScrollBarValueChanged(object sender, EventArgs e) |
|
{ |
|
textArea.VirtualTop = new Point(textArea.VirtualTop.X, vScrollBar.Value); |
|
textArea.Invalidate(); |
|
AdjustScrollBars(); |
|
} |
|
|
|
void HScrollBarValueChanged(object sender, EventArgs e) |
|
{ |
|
textArea.VirtualTop = new Point(hScrollBar.Value, textArea.VirtualTop.Y); |
|
textArea.Invalidate(); |
|
} |
|
|
|
public void HandleMouseWheel(MouseEventArgs e) |
|
{ |
|
if ((Control.ModifierKeys & Keys.Control) != 0 && TextEditorProperties.MouseWheelTextZoom) { |
|
if (e.Delta > 0) { |
|
motherTextEditorControl.Font = new Font(motherTextEditorControl.Font.Name, |
|
motherTextEditorControl.Font.Size + 1); |
|
|
|
} else { |
|
motherTextEditorControl.Font = new Font(motherTextEditorControl.Font.Name, |
|
Math.Max(6, motherTextEditorControl.Font.Size - 1)); |
|
|
|
|
|
} |
|
} else { |
|
int MAX_DELTA = 120; // basically it's constant now, but could be changed later by MS |
|
int multiplier = Math.Abs(e.Delta) / MAX_DELTA; |
|
|
|
int newValue; |
|
if (System.Windows.Forms.SystemInformation.MouseWheelScrollLines > 0) { |
|
newValue = this.vScrollBar.Value - (TextEditorProperties.MouseWheelScrollDown ? 1 : -1) * Math.Sign(e.Delta) * System.Windows.Forms.SystemInformation.MouseWheelScrollLines * vScrollBar.SmallChange * multiplier; |
|
} else { |
|
newValue = this.vScrollBar.Value - (TextEditorProperties.MouseWheelScrollDown ? 1 : -1) * Math.Sign(e.Delta) * vScrollBar.LargeChange; |
|
} |
|
vScrollBar.Value = Math.Max(vScrollBar.Minimum, Math.Min(vScrollBar.Maximum, newValue)); |
|
} |
|
} |
|
|
|
protected override void OnMouseWheel(MouseEventArgs e) |
|
{ |
|
base.OnMouseWheel(e); |
|
if (DoHandleMousewheel) { |
|
HandleMouseWheel(e); |
|
} |
|
} |
|
|
|
public void ScrollToCaret() |
|
{ |
|
ScrollTo(textArea.Caret.Line, textArea.Caret.Column); |
|
} |
|
|
|
public void ScrollTo(int line, int column) |
|
{ |
|
if (motherTextEditorControl.IsInUpdate) { |
|
scrollToPosOnNextUpdate = new Point(column, line); |
|
return; |
|
} else { |
|
scrollToPosOnNextUpdate = Point.Empty; |
|
} |
|
|
|
ScrollTo(line); |
|
|
|
int curCharMin = (int)(this.hScrollBar.Value - this.hScrollBar.Minimum); |
|
int curCharMax = curCharMin + textArea.TextView.VisibleColumnCount; |
|
|
|
int pos = textArea.TextView.GetVisualColumn(line, column); |
|
|
|
if (textArea.TextView.VisibleColumnCount < 0) { |
|
hScrollBar.Value = 0; |
|
} else { |
|
if (pos < curCharMin) { |
|
hScrollBar.Value = (int)(Math.Max(0, pos - scrollMarginHeight)); |
|
} else { |
|
if (pos > curCharMax) { |
|
hScrollBar.Value = (int)Math.Max(0, Math.Min(hScrollBar.Maximum, (pos - textArea.TextView.VisibleColumnCount + scrollMarginHeight))); |
|
} |
|
} |
|
} |
|
} |
|
|
|
int scrollMarginHeight = 3; |
|
|
|
public void ScrollTo(int line) |
|
{ |
|
line = Math.Max(0, Math.Min(Document.TotalNumberOfLines - 1, line)); |
|
line = Document.GetVisibleLine(line); |
|
int curLineMin = textArea.TextView.FirstPhysicalLine; |
|
if (textArea.TextView.LineHeightRemainder > 0) { |
|
curLineMin ++; |
|
} |
|
|
|
if (line - scrollMarginHeight + 3 < curLineMin) { |
|
this.vScrollBar.Value = Math.Max(0, Math.Min(this.vScrollBar.Maximum, (line - scrollMarginHeight + 3) * textArea.TextView.FontHeight)) ; |
|
VScrollBarValueChanged(this, EventArgs.Empty); |
|
} else { |
|
int curLineMax = curLineMin + this.textArea.TextView.VisibleLineCount; |
|
if (line + scrollMarginHeight - 1 > curLineMax) { |
|
if (this.textArea.TextView.VisibleLineCount == 1) { |
|
this.vScrollBar.Value = Math.Max(0, Math.Min(this.vScrollBar.Maximum, (line - scrollMarginHeight - 1) * textArea.TextView.FontHeight)) ; |
|
} else { |
|
this.vScrollBar.Value = Math.Min(this.vScrollBar.Maximum, |
|
(line - this.textArea.TextView.VisibleLineCount + scrollMarginHeight - 1)* textArea.TextView.FontHeight) ; |
|
} |
|
VScrollBarValueChanged(this, EventArgs.Empty); |
|
} |
|
} |
|
} |
|
|
|
public void JumpTo(int line) |
|
{ |
|
line = Math.Min(line, Document.TotalNumberOfLines - 1); |
|
string text = Document.GetText(Document.GetLineSegment(line)); |
|
JumpTo(line, text.Length - text.TrimStart().Length); |
|
} |
|
|
|
public void JumpTo(int line, int column) |
|
{ |
|
textArea.Focus(); |
|
textArea.SelectionManager.ClearSelection(); |
|
textArea.Caret.Position = new Point(column, line); |
|
textArea.SetDesiredColumn(); |
|
ScrollToCaret(); |
|
} |
|
|
|
public event MouseEventHandler ShowContextMenu; |
|
|
|
protected override void WndProc(ref Message m) |
|
{ |
|
if (m.Msg == 0x007B) { // handle WM_CONTEXTMENU |
|
if (ShowContextMenu != null) { |
|
long lParam = m.LParam.ToInt64(); |
|
int x = unchecked((short)(lParam & 0xffff)); |
|
int y = unchecked((short)((lParam & 0xffff0000) >> 16)); |
|
if (x == -1 && y == -1) { |
|
Point pos = Caret.ScreenPosition; |
|
ShowContextMenu(this, new MouseEventArgs(MouseButtons.None, 0, pos.X, pos.Y + textArea.TextView.FontHeight, 0)); |
|
} else { |
|
Point pos = PointToClient(new Point(x, y)); |
|
ShowContextMenu(this, new MouseEventArgs(MouseButtons.Right, 1, pos.X, pos.Y, 0)); |
|
} |
|
} |
|
} |
|
base.WndProc(ref m); |
|
} |
|
} |
|
}
|
|
|