1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
33 using System.Drawing.Text;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
38 [DefaultEvent("TextChanged")]
39 [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
40 public abstract class TextBoxBase : Control {
41 #region Local Variables
42 internal HorizontalAlignment alignment;
43 internal bool accepts_tab;
44 internal bool accepts_return;
45 internal bool auto_size;
46 internal CharacterCasing character_casing;
48 internal bool hide_selection;
49 internal int max_length;
50 internal bool modified;
51 internal bool multiline;
52 internal bool read_only;
53 internal bool word_wrap;
54 internal Document document;
55 internal LineTag caret_tag; // tag our cursor is in
56 internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
57 internal HScrollBar hscroll;
58 internal VScrollBar vscroll;
59 internal RichTextBoxScrollBars scrollbars;
60 internal bool grabbed;
61 internal bool richtext;
62 internal int requested_height;
63 internal int canvas_width;
64 internal int canvas_height;
65 internal int track_width = 20;
67 internal static bool draw_lines = false;
70 #endregion // Local Variables
72 #region Internal Constructor
73 // Constructor will go when complete, only for testing - pdb
74 internal TextBoxBase() {
75 alignment = HorizontalAlignment.Left;
76 accepts_return = false;
79 border_style = BorderStyle.Fixed3D;
80 character_casing = CharacterCasing.Normal;
82 hide_selection = true;
89 document = new Document(this);
90 //document.CaretMoved += new EventHandler(CaretMoved);
92 requested_height = -1;
94 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
95 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
96 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
97 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
98 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
99 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
101 scrollbars = RichTextBoxScrollBars.None;
103 hscroll = new HScrollBar();
104 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
105 hscroll.control_style &= ~ControlStyles.Selectable;
106 hscroll.Enabled = false;
107 hscroll.Visible = false;
109 vscroll = new VScrollBar();
110 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
111 vscroll.control_style &= ~ControlStyles.Selectable;
112 vscroll.Enabled = false;
113 vscroll.Visible = false;
115 this.Controls.Add(hscroll);
116 this.Controls.Add(vscroll);
118 //SetStyle(ControlStyles.ResizeRedraw, true);
119 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
120 SetStyle(ControlStyles.UserPaint, true);
122 canvas_width = this.Width;
123 canvas_height = this.Height;
125 CalculateScrollBars();
127 #endregion // Internal Constructor
129 #region Private and Internal Methods
130 internal string CaseAdjust(string s) {
131 if (character_casing == CharacterCasing.Normal) {
134 if (character_casing == CharacterCasing.Lower) {
140 #endregion // Private and Internal Methods
142 #region Public Instance Properties
143 [DefaultValue(false)]
144 public bool AcceptsTab {
150 if (value != accepts_tab) {
152 OnAcceptsTabChanged(EventArgs.Empty);
159 [RefreshProperties(RefreshProperties.Repaint)]
160 public virtual bool AutoSize {
166 if (value != auto_size) {
169 if (PreferredHeight != Height) {
170 Height = PreferredHeight;
173 OnAutoSizeChanged(EventArgs.Empty);
179 public override System.Drawing.Color BackColor {
181 return base.BackColor;
184 base.BackColor = value;
189 [EditorBrowsable(EditorBrowsableState.Never)]
190 public override System.Drawing.Image BackgroundImage {
192 return base.BackgroundImage;
195 base.BackgroundImage = value;
199 [DefaultValue(BorderStyle.Fixed3D)]
201 public BorderStyle BorderStyle {
207 if (value != border_style) {
208 border_style = value;
209 OnBorderStyleChanged(EventArgs.Empty);
215 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
216 public bool CanUndo {
223 public override System.Drawing.Color ForeColor {
225 return base.ForeColor;
228 base.ForeColor = value;
233 public bool HideSelection {
235 return hide_selection;
239 if (value != hide_selection) {
240 hide_selection = value;
241 OnHideSelectionChanged(EventArgs.Empty);
243 if (hide_selection) {
244 document.selection_visible = false;
246 document.selection_visible = true;
248 document.InvalidateSelectionArea();
253 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
254 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
256 public string[] Lines {
263 lines = new string[l];
265 for (i = 1; i <= l; i++) {
266 lines[i - 1] = document.GetLine(i).text.ToString();
280 brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
282 for (i = 0; i < l; i++) {
283 document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
286 OnTextChanged(EventArgs.Empty);
290 [DefaultValue(32767)]
292 public virtual int MaxLength {
298 if (value != max_length) {
305 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
306 public bool Modified {
312 if (value != modified) {
314 OnModifiedChanged(EventArgs.Empty);
319 [DefaultValue(false)]
321 [RefreshProperties(RefreshProperties.All)]
322 public virtual bool Multiline {
328 if (value != multiline) {
331 // Make sure we update our size; the user may have already set the size before going to multiline
332 if (multiline && requested_height != -1) {
333 Height = requested_height;
334 requested_height = -1;
337 OnMultilineChanged(EventArgs.Empty);
340 document.multiline = multiline;
345 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
346 [EditorBrowsable(EditorBrowsableState.Advanced)]
347 public int PreferredHeight {
349 return this.Font.Height + 7; // FIXME - consider border style as well
353 [DefaultValue(false)]
354 public bool ReadOnly {
360 if (value != read_only) {
362 OnReadOnlyChanged(EventArgs.Empty);
368 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
369 public virtual string SelectedText {
371 return document.GetSelection();
376 document.ReplaceSelection(CaseAdjust(value));
377 OnTextChanged(EventArgs.Empty);
383 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
384 public virtual int SelectionLength {
386 return document.SelectionLength();
396 start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
398 document.CharIndexToLineTag(start + value, out line, out tag, out pos);
399 document.SetSelectionEnd(line, pos);
400 document.PositionCaret(line, pos);
402 document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
403 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
409 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
410 public int SelectionStart {
414 index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
424 document.CharIndexToLineTag(value, out line, out tag, out pos);
425 document.SetSelectionStart(line, pos);
430 public override string Text {
432 if (document == null || document.Root == null || document.Root.text == null) {
437 return document.Root.text.ToString();
442 sb = new StringBuilder();
444 for (i = 1; i < document.Lines; i++) {
445 sb.Append(document.GetLine(i).text.ToString() + Environment.NewLine);
447 return sb.ToString();
452 if (value == base.Text) {
462 lines = value.Split(new char[] {'\n'});
463 for (int i = 0; i < lines.Length; i++) {
464 if (lines[i].EndsWith("\r")) {
465 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
470 line = document.GetLine(1);
471 document.SetSelectionStart(line, 0);
473 line = document.GetLine(document.Lines);
474 document.SetSelectionEnd(line, line.text.Length);
475 document.PositionCaret(line, line.text.Length);
478 document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
480 line = document.GetLine(1);
481 document.SetSelectionStart(line, 0);
482 document.SetSelectionEnd(line, value.Length);
483 document.PositionCaret(line, value.Length);
487 OnTextChanged(EventArgs.Empty);
492 public virtual int TextLength {
494 if (document == null || document.Root == null || document.Root.text == null) {
499 return document.Root.text.Length;
505 for (i = 1; i < document.Lines; i++) {
506 total += document.GetLine(i).text.Length + Environment.NewLine.Length;
516 public bool WordWrap {
522 if (value != word_wrap) {
524 document.Wrap = value;
528 #endregion // Public Instance Properties
530 #region Protected Instance Properties
531 protected override CreateParams CreateParams {
533 return base.CreateParams;
537 protected override System.Drawing.Size DefaultSize {
539 return base.DefaultSize;
542 #endregion // Protected Instance Properties
544 #region Public Instance Methods
545 public void AppendText(string text) {
550 // Break the string into separate lines
551 lines = text.Split(new char[] {'\n'});
552 linecount = lines.Length;
553 for (int i = 0; i < linecount; i++) {
554 if (lines[i].EndsWith("\r")) {
555 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
559 // Grab the formatting for the last element
560 document.MoveCaret(CaretDirection.CtrlEnd);
562 // Insert the first line
563 document.InsertString(document.CaretLine, document.CaretPosition, lines[0]);
565 for (int i = 1; i < linecount; i++) {
566 document.Add(document.CaretLine.LineNo+i, CaseAdjust(lines[i]), alignment, document.CaretTag.font, document.CaretTag.color);
570 document.MoveCaret(CaretDirection.CtrlEnd);
572 document.MoveCaret(CaretDirection.CtrlEnd);
573 document.InsertStringAtCaret(text, true);
576 OnTextChanged(EventArgs.Empty);
579 public void Clear() {
583 public void ClearUndo() {
585 throw new NotImplementedException();
590 throw new NotImplementedException();
595 throw new NotImplementedException();
598 public void Paste() {
600 throw new NotImplementedException();
603 public void ScrollToCaret() {
605 throw new NotImplementedException();
608 public void Select(int start, int length) {
609 SelectionStart = start;
610 SelectionLength = length;
614 public void SelectAll() {
617 last = document.GetLine(document.Lines);
618 document.SetSelectionStart(document.GetLine(1), 0);
619 document.SetSelectionEnd(last, last.text.Length);
622 public override string ToString() {
627 if (document == null) {
631 sb = new StringBuilder();
633 end = document.Lines;
635 for (i = 1; i < end; i++) {
636 sb.Append(document.GetLine(i).text.ToString() + "\n");
639 return sb.ToString();
645 #endregion // Public Instance Methods
647 #region Protected Instance Methods
648 protected override void CreateHandle() {
649 base.CreateHandle ();
652 protected override bool IsInputKey(Keys keyData) {
655 // We handle Enter in ProcessDialogKey
657 if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
675 protected virtual void OnAcceptsTabChanged(EventArgs e) {
676 if (AcceptsTabChanged != null) {
677 AcceptsTabChanged(this, e);
681 protected virtual void OnAutoSizeChanged(EventArgs e) {
682 if (AutoSizeChanged != null) {
683 AutoSizeChanged(this, e);
687 protected virtual void OnBorderStyleChanged(EventArgs e) {
688 if (BorderStyleChanged != null) {
689 BorderStyleChanged(this, e);
693 protected override void OnFontChanged(EventArgs e) {
694 base.OnFontChanged (e);
697 if (PreferredHeight != Height) {
698 Height = PreferredHeight;
703 protected override void OnHandleCreated(EventArgs e) {
704 base.OnHandleCreated (e);
707 protected override void OnHandleDestroyed(EventArgs e) {
708 base.OnHandleDestroyed (e);
711 protected virtual void OnHideSelectionChanged(EventArgs e) {
712 if (HideSelectionChanged != null) {
713 HideSelectionChanged(this, e);
717 protected virtual void OnModifiedChanged(EventArgs e) {
718 if (ModifiedChanged != null) {
719 ModifiedChanged(this, e);
723 protected virtual void OnMultilineChanged(EventArgs e) {
724 if (MultilineChanged != null) {
725 MultilineChanged(this, e);
729 protected virtual void OnReadOnlyChanged(EventArgs e) {
730 if (ReadOnlyChanged != null) {
731 ReadOnlyChanged(this, e);
735 protected override bool ProcessDialogKey(Keys keyData) {
736 switch (keyData & Keys.KeyCode) {
738 document.SetSelectionToCaret(true);
740 if ((Control.ModifierKeys & Keys.Control) != 0) {
741 document.MoveCaret(CaretDirection.WordBack);
743 document.MoveCaret(CaretDirection.CharBack);
745 CaretMoved(this, null);
750 document.SetSelectionToCaret(true);
752 if ((Control.ModifierKeys & Keys.Control) != 0) {
753 document.MoveCaret(CaretDirection.WordForward);
755 document.MoveCaret(CaretDirection.CharForward);
757 CaretMoved(this, null);
762 document.SetSelectionToCaret(true);
763 document.MoveCaret(CaretDirection.LineUp);
764 CaretMoved(this, null);
769 document.SetSelectionToCaret(true);
770 document.MoveCaret(CaretDirection.LineDown);
771 CaretMoved(this, null);
776 document.SetSelectionToCaret(true);
778 if ((Control.ModifierKeys & Keys.Control) != 0) {
779 document.MoveCaret(CaretDirection.CtrlHome);
781 document.MoveCaret(CaretDirection.Home);
783 CaretMoved(this, null);
788 document.SetSelectionToCaret(true);
790 if ((Control.ModifierKeys & Keys.Control) != 0) {
791 document.MoveCaret(CaretDirection.CtrlEnd);
793 document.MoveCaret(CaretDirection.End);
795 CaretMoved(this, null);
800 if (!read_only && multiline && (accepts_return || ((Control.ModifierKeys & Keys.Control) != 0))) {
803 if (document.selection_visible) {
804 document.ReplaceSelection("");
806 document.SetSelectionToCaret(true);
808 line = document.CaretLine;
810 document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
811 OnTextChanged(EventArgs.Empty);
812 document.UpdateView(line, 2, 0);
813 document.MoveCaret(CaretDirection.CharForward);
814 CaretMoved(this, null);
821 if (!read_only && accepts_tab) {
822 document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
823 if (document.selection_visible) {
824 document.ReplaceSelection("");
826 document.SetSelectionToCaret(true);
828 OnTextChanged(EventArgs.Empty);
829 CaretMoved(this, null);
841 // delete only deletes on the line, doesn't do the combine
842 if (document.selection_visible) {
843 document.ReplaceSelection("");
845 document.SetSelectionToCaret(true);
846 if (document.CaretPosition == 0) {
847 if (document.CaretLine.LineNo > 1) {
851 line = document.GetLine(document.CaretLine.LineNo - 1);
852 new_caret_pos = line.text.Length;
854 document.Combine(line, document.CaretLine);
855 document.UpdateView(line, 1, 0);
856 document.PositionCaret(line, new_caret_pos);
857 document.UpdateCaret();
858 OnTextChanged(EventArgs.Empty);
861 document.DeleteChar(document.CaretTag, document.CaretPosition, false);
862 document.MoveCaret(CaretDirection.CharBack);
863 OnTextChanged(EventArgs.Empty);
865 CaretMoved(this, null);
874 // delete only deletes on the line, doesn't do the combine
875 if (document.CaretPosition == document.CaretLine.text.Length) {
876 if (document.CaretLine.LineNo < document.Lines) {
879 line = document.GetLine(document.CaretLine.LineNo + 1);
880 document.Combine(document.CaretLine, line);
881 document.UpdateView(document.CaretLine, 2, 0);
882 OnTextChanged(EventArgs.Empty);
888 check_first = document.GetLine(document.CaretLine.LineNo);
889 check_second = document.GetLine(check_first.line_no + 1);
891 Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
894 // Caret doesn't move
897 document.DeleteChar(document.CaretTag, document.CaretPosition, true);
898 OnTextChanged(EventArgs.Empty);
900 CaretMoved(this, null);
904 return base.ProcessDialogKey (keyData);
907 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
908 // Make sure we don't get sized bigger than we want to be
911 if (height > PreferredHeight) {
912 requested_height = height;
913 height = PreferredHeight;
914 specified |= BoundsSpecified.Height;
919 document.ViewPortWidth = this.Width;
920 document.ViewPortHeight = this.Height;
924 base.SetBoundsCore (x, y, width, height, specified);
927 protected override void WndProc(ref Message m) {
928 switch ((Msg)m.Msg) {
930 PaintEventArgs paint_event;
932 paint_event = XplatUI.PaintEventStart(Handle);
934 PaintControl(paint_event);
935 XplatUI.PaintEventEnd(Handle);
940 case Msg.WM_SETFOCUS: {
942 document.CaretHasFocus();
943 Console.WriteLine("Creating caret");
948 case Msg.WM_KILLFOCUS: {
950 document.CaretLostFocus();
951 Console.WriteLine("Destroying caret");
957 if (ProcessKeyEventArgs(ref m)) {
961 if (PreProcessMessage(ref m)) {
965 if (ProcessKeyMessage(ref m)) {
969 if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
970 if (document.selection_visible) {
971 document.ReplaceSelection("");
974 switch (character_casing) {
975 case CharacterCasing.Normal: {
976 document.InsertCharAtCaret((char)m.WParam, true);
977 OnTextChanged(EventArgs.Empty);
978 CaretMoved(this, null);
982 case CharacterCasing.Lower: {
983 document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
984 OnTextChanged(EventArgs.Empty);
985 CaretMoved(this, null);
989 case CharacterCasing.Upper: {
990 document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
991 OnTextChanged(EventArgs.Empty);
992 CaretMoved(this, null);
1002 base.WndProc(ref m);
1008 #endregion // Protected Instance Methods
1011 public event EventHandler AcceptsTabChanged;
1012 public event EventHandler AutoSizeChanged;
1014 [EditorBrowsable(EditorBrowsableState.Never)]
1015 public new event EventHandler BackgroundImageChanged {
1016 add { base.BackgroundImageChanged += value; }
1017 remove { base.BackgroundImageChanged -= value; }
1019 public event EventHandler BorderStyleChanged;
1021 [EditorBrowsable(EditorBrowsableState.Advanced)]
1022 public event EventHandler Click;
1023 public event EventHandler HideSelectionChanged;
1024 public event EventHandler ModifiedChanged;
1025 public event EventHandler MultilineChanged;
1027 [EditorBrowsable(EditorBrowsableState.Never)]
1028 public event PaintEventHandler Paint;
1029 public event EventHandler ReadOnlyChanged;
1030 #endregion // Events
1032 #region Private Methods
1033 internal Document Document {
1045 private void PaintControl(PaintEventArgs pevent) {
1047 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
1048 pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
1050 // Draw the viewable document
1051 document.Draw(pevent.Graphics, pevent.ClipRectangle);
1053 Rectangle rect = ClientRectangle;
1056 //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorButtonShadow), rect);
1065 p = new Pen(Color.Red, 1);
1067 // First, figure out from what line to what line we need to draw
1068 start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
1069 end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
1071 //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
1072 //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
1075 while (line_no <= end) {
1076 line = document.GetLine(line_no);
1079 for (int i = 0; i < line.text.Length; i++) {
1080 pevent.Graphics.DrawLine(p, (int)line.widths[i] - document.ViewPortX, line.Y - document.ViewPortY, (int)line.widths[i] - document.ViewPortX, line.Y + line.height - document.ViewPortY);
1089 private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
1094 if (e.Button == MouseButtons.Left) {
1095 document.PositionCaret(e.X, e.Y);
1096 document.SetSelectionToCaret(true);
1097 this.grabbed = true;
1098 this.Capture = true;
1103 if (e.Button == MouseButtons.Right) {
1104 draw_lines = !draw_lines;
1106 Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
1107 Console.WriteLine("Selection start: {0}", this.SelectionStart);
1109 this.SelectionStart = 10;
1110 this.SelectionLength = 5;
1115 tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
1117 Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
1120 case 4: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("impact", 20, FontStyle.Bold, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Red)); break;
1121 case 1: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("arial unicode ms", 24, FontStyle.Italic, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.DarkGoldenrod)); break;
1122 case 2: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Aquamarine)); break;
1123 case 3: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("times roman", 16, FontStyle.Underline, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Turquoise)); break;
1124 case 0: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("times roman", 64, FontStyle.Italic | FontStyle.Bold, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.LightSeaGreen)); break;
1125 case 5: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, ((TextBoxBase)sender).Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor)); break;
1132 // Update/Recalculate what we see
1133 document.UpdateView(line, 0);
1135 // Make sure our caret is properly positioned and sized
1136 document.AlignCaret();
1140 private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
1141 this.Capture = false;
1142 this.grabbed = false;
1143 if (e.Button == MouseButtons.Left) {
1144 document.PositionCaret(e.X, e.Y);
1145 document.SetSelectionToCaret(false);
1146 document.DisplayCaret();
1150 #endregion // Private Methods
1153 private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
1154 canvas_width = this.Width;
1155 canvas_height = this.Height;
1156 // We always move them, they just might not be displayed
1157 hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, Width, hscroll.Height);
1158 vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, Height);
1162 private void CalculateDocument() {
1163 document.RecalculateDocument(CreateGraphics());
1164 CalculateScrollBars();
1165 Invalidate(); // FIXME - do we need this?
1168 protected void CalculateScrollBars() {
1169 // No scrollbars for a single line
1170 if (document.Width >= this.Width) {
1171 hscroll.Enabled = true;
1172 hscroll.Minimum = 0;
1173 hscroll.Maximum = document.Width - this.Width;
1175 hscroll.Enabled = false;
1178 if (document.Height >= this.Height) {
1179 vscroll.Enabled = true;
1180 vscroll.Minimum = 0;
1181 vscroll.Maximum = document.Height - this.Height;
1183 vscroll.Enabled = false;
1191 if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
1192 if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
1193 hscroll.Visible = true;
1197 if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
1198 if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
1199 vscroll.Visible = true;
1203 if (hscroll.Visible) {
1204 vscroll.Maximum += hscroll.Height * 2;
1205 canvas_height = this.Height - hscroll.Height * 2;
1208 if (vscroll.Visible) {
1209 hscroll.Maximum += vscroll.Width * 2;
1210 canvas_width = this.Width - vscroll.Width * 2;
1214 private void hscroll_ValueChanged(object sender, EventArgs e) {
1215 XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
1216 document.ViewPortX = this.hscroll.Value;
1217 document.UpdateCaret();
1218 Console.WriteLine("Dude scrolled horizontal");
1221 private void vscroll_ValueChanged(object sender, EventArgs e) {
\r
1222 XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
1223 document.ViewPortX = this.vscroll.Value;
1224 document.UpdateCaret();
1225 Console.WriteLine("Dude scrolled vertical");
1228 private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
1229 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
1231 document.PositionCaret(e.X, e.Y);
1232 document.SetSelectionToCaret(false);
1233 document.DisplayCaret();
1237 private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
1241 // Font changes apply to the whole document
1242 for (int i = 1; i <= document.Lines; i++) {
1243 line = document.GetLine(i);
1244 LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
1245 document.UpdateView(line, 0);
1247 // Make sure the caret height is matching the new font height
1248 document.AlignCaret();
1252 /// <summary>Ensure the caret is always visible</summary>
\r
1253 internal void CaretMoved(object sender, EventArgs e) {
\r
1256 pos = document.Caret;
\r
1257 Console.WriteLine("Caret now at {0} (Thumb: {1}x{2}, Canvas: {3}x{4}, Document {5}x{6})", pos, hscroll.Value, vscroll.Value, canvas_width, canvas_height, document.Width, document.Height);
\r
1259 // Handle horizontal scrolling
\r
1260 if (pos.X < (document.ViewPortX + track_width)) {
\r
1261 if ((hscroll.Value - track_width) >= hscroll.Minimum) {
\r
1262 hscroll.Value -= track_width;
\r
1264 hscroll.Value = hscroll.Minimum;
\r
1268 if ((pos.X > (this.Width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
\r
1269 if ((hscroll.Value + track_width) <= hscroll.Maximum) {
\r
1270 hscroll.Value += track_width;
\r
1272 hscroll.Value = hscroll.Maximum;
\r
1280 // Handle vertical scrolling
\r
1281 if (pos.Y < (document.ViewPortY + track_width)) {
\r
1282 if ((hscroll.Value - track_width) >= hscroll.Minimum) {
\r
1283 hscroll.Value -= track_width;
\r
1285 hscroll.Value = hscroll.Minimum;
\r
1288 if (pos.X > this.Width + document.ViewPortX) {
\r
1289 hscroll.Value = hscroll.Minimum;
\r
1293 if (pos.X > (this.Width + document.ViewPortX - track_width)) {
\r
1294 if ((hscroll.Value + track_width) <= hscroll.Maximum) {
\r
1295 hscroll.Value += track_width;
\r
1297 hscroll.Value = hscroll.Maximum;
\r
1301 if (pos.X < document.ViewPortX) {
\r
1302 hscroll.Value = hscroll.Minimum;
\r