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.WidthChanged += new EventHandler(document_WidthChanged);
91 //document.CaretMoved += new EventHandler(CaretMoved);
93 requested_height = -1;
95 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
96 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
97 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
98 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
99 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
100 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
102 scrollbars = RichTextBoxScrollBars.None;
104 hscroll = new HScrollBar();
105 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
106 hscroll.control_style &= ~ControlStyles.Selectable;
107 hscroll.Enabled = false;
108 hscroll.Visible = false;
110 vscroll = new VScrollBar();
111 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
112 vscroll.control_style &= ~ControlStyles.Selectable;
113 vscroll.Enabled = false;
114 vscroll.Visible = false;
116 this.Controls.Add(hscroll);
117 this.Controls.Add(vscroll);
119 //SetStyle(ControlStyles.ResizeRedraw, true);
120 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
121 SetStyle(ControlStyles.UserPaint, true);
123 canvas_width = this.Width;
124 canvas_height = this.Height;
126 CalculateScrollBars();
128 #endregion // Internal Constructor
130 #region Private and Internal Methods
131 internal string CaseAdjust(string s) {
132 if (character_casing == CharacterCasing.Normal) {
135 if (character_casing == CharacterCasing.Lower) {
141 #endregion // Private and Internal Methods
143 #region Public Instance Properties
144 [DefaultValue(false)]
145 public bool AcceptsTab {
151 if (value != accepts_tab) {
153 OnAcceptsTabChanged(EventArgs.Empty);
160 [RefreshProperties(RefreshProperties.Repaint)]
161 public virtual bool AutoSize {
167 if (value != auto_size) {
170 if (PreferredHeight != Height) {
171 Height = PreferredHeight;
174 OnAutoSizeChanged(EventArgs.Empty);
180 public override System.Drawing.Color BackColor {
182 return base.BackColor;
185 base.BackColor = value;
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 public override System.Drawing.Image BackgroundImage {
193 return base.BackgroundImage;
196 base.BackgroundImage = value;
200 [DefaultValue(BorderStyle.Fixed3D)]
202 public BorderStyle BorderStyle {
208 if (value != border_style) {
209 border_style = value;
210 OnBorderStyleChanged(EventArgs.Empty);
216 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
217 public bool CanUndo {
224 public override System.Drawing.Color ForeColor {
226 return base.ForeColor;
229 base.ForeColor = value;
234 public bool HideSelection {
236 return hide_selection;
240 if (value != hide_selection) {
241 hide_selection = value;
242 OnHideSelectionChanged(EventArgs.Empty);
244 if (hide_selection) {
245 document.selection_visible = false;
247 document.selection_visible = true;
249 document.InvalidateSelectionArea();
254 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
255 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
257 public string[] Lines {
264 lines = new string[l];
266 for (i = 1; i <= l; i++) {
267 lines[i - 1] = document.GetLine(i).text.ToString();
281 brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
283 for (i = 0; i < l; i++) {
284 document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
287 OnTextChanged(EventArgs.Empty);
291 [DefaultValue(32767)]
293 public virtual int MaxLength {
299 if (value != max_length) {
306 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
307 public bool Modified {
313 if (value != modified) {
315 OnModifiedChanged(EventArgs.Empty);
320 [DefaultValue(false)]
322 [RefreshProperties(RefreshProperties.All)]
323 public virtual bool Multiline {
329 if (value != multiline) {
332 // Make sure we update our size; the user may have already set the size before going to multiline
333 if (multiline && requested_height != -1) {
334 Height = requested_height;
335 requested_height = -1;
338 OnMultilineChanged(EventArgs.Empty);
341 document.multiline = multiline;
346 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
347 [EditorBrowsable(EditorBrowsableState.Advanced)]
348 public int PreferredHeight {
350 return this.Font.Height + 7; // FIXME - consider border style as well
354 [DefaultValue(false)]
355 public bool ReadOnly {
361 if (value != read_only) {
363 OnReadOnlyChanged(EventArgs.Empty);
369 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
370 public virtual string SelectedText {
372 return document.GetSelection();
377 document.ReplaceSelection(CaseAdjust(value));
378 OnTextChanged(EventArgs.Empty);
384 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
385 public virtual int SelectionLength {
387 return document.SelectionLength();
397 start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
399 document.CharIndexToLineTag(start + value, out line, out tag, out pos);
400 document.SetSelectionEnd(line, pos);
401 document.PositionCaret(line, pos);
403 document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
404 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
410 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
411 public int SelectionStart {
415 index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
425 document.CharIndexToLineTag(value, out line, out tag, out pos);
426 document.SetSelectionStart(line, pos);
431 public override string Text {
433 if (document == null || document.Root == null || document.Root.text == null) {
438 return document.Root.text.ToString();
443 sb = new StringBuilder();
445 for (i = 1; i < document.Lines; i++) {
446 sb.Append(document.GetLine(i).text.ToString() + Environment.NewLine);
448 return sb.ToString();
453 if (value == base.Text) {
463 lines = value.Split(new char[] {'\n'});
464 for (int i = 0; i < lines.Length; i++) {
465 if (lines[i].EndsWith("\r")) {
466 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
471 line = document.GetLine(1);
472 document.SetSelectionStart(line, 0);
474 line = document.GetLine(document.Lines);
475 document.SetSelectionEnd(line, line.text.Length);
476 document.PositionCaret(line, line.text.Length);
479 document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
481 line = document.GetLine(1);
482 document.SetSelectionStart(line, 0);
483 document.SetSelectionEnd(line, value.Length);
484 document.PositionCaret(line, value.Length);
488 OnTextChanged(EventArgs.Empty);
493 public virtual int TextLength {
495 if (document == null || document.Root == null || document.Root.text == null) {
500 return document.Root.text.Length;
506 for (i = 1; i < document.Lines; i++) {
507 total += document.GetLine(i).text.Length + Environment.NewLine.Length;
517 public bool WordWrap {
523 if (value != word_wrap) {
525 document.Wrap = value;
529 #endregion // Public Instance Properties
531 #region Protected Instance Properties
532 protected override CreateParams CreateParams {
534 return base.CreateParams;
538 protected override System.Drawing.Size DefaultSize {
540 return base.DefaultSize;
543 #endregion // Protected Instance Properties
545 #region Public Instance Methods
546 public void AppendText(string text) {
551 // Break the string into separate lines
552 lines = text.Split(new char[] {'\n'});
553 linecount = lines.Length;
554 for (int i = 0; i < linecount; i++) {
555 if (lines[i].EndsWith("\r")) {
556 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
560 // Grab the formatting for the last element
561 document.MoveCaret(CaretDirection.CtrlEnd);
563 // Insert the first line
564 document.InsertString(document.CaretLine, document.CaretPosition, lines[0]);
566 for (int i = 1; i < linecount; i++) {
567 document.Add(document.CaretLine.LineNo+i, CaseAdjust(lines[i]), alignment, document.CaretTag.font, document.CaretTag.color);
571 document.MoveCaret(CaretDirection.CtrlEnd);
573 document.MoveCaret(CaretDirection.CtrlEnd);
574 document.InsertStringAtCaret(text, true);
577 OnTextChanged(EventArgs.Empty);
580 public void Clear() {
584 public void ClearUndo() {
586 throw new NotImplementedException();
591 throw new NotImplementedException();
596 throw new NotImplementedException();
599 public void Paste() {
601 throw new NotImplementedException();
604 public void ScrollToCaret() {
606 throw new NotImplementedException();
609 public void Select(int start, int length) {
610 SelectionStart = start;
611 SelectionLength = length;
615 public void SelectAll() {
618 last = document.GetLine(document.Lines);
619 document.SetSelectionStart(document.GetLine(1), 0);
620 document.SetSelectionEnd(last, last.text.Length);
623 public override string ToString() {
628 if (document == null) {
632 sb = new StringBuilder();
634 end = document.Lines;
636 for (i = 1; i < end; i++) {
637 sb.Append(document.GetLine(i).text.ToString() + "\n");
640 return sb.ToString();
646 #endregion // Public Instance Methods
648 #region Protected Instance Methods
649 protected override void CreateHandle() {
650 base.CreateHandle ();
653 protected override bool IsInputKey(Keys keyData) {
656 // We handle Enter in ProcessDialogKey
658 if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
676 protected virtual void OnAcceptsTabChanged(EventArgs e) {
677 if (AcceptsTabChanged != null) {
678 AcceptsTabChanged(this, e);
682 protected virtual void OnAutoSizeChanged(EventArgs e) {
683 if (AutoSizeChanged != null) {
684 AutoSizeChanged(this, e);
688 protected virtual void OnBorderStyleChanged(EventArgs e) {
689 if (BorderStyleChanged != null) {
690 BorderStyleChanged(this, e);
694 protected override void OnFontChanged(EventArgs e) {
695 base.OnFontChanged (e);
698 if (PreferredHeight != Height) {
699 Height = PreferredHeight;
704 protected override void OnHandleCreated(EventArgs e) {
705 base.OnHandleCreated (e);
708 protected override void OnHandleDestroyed(EventArgs e) {
709 base.OnHandleDestroyed (e);
712 protected virtual void OnHideSelectionChanged(EventArgs e) {
713 if (HideSelectionChanged != null) {
714 HideSelectionChanged(this, e);
718 protected virtual void OnModifiedChanged(EventArgs e) {
719 if (ModifiedChanged != null) {
720 ModifiedChanged(this, e);
724 protected virtual void OnMultilineChanged(EventArgs e) {
725 if (MultilineChanged != null) {
726 MultilineChanged(this, e);
730 protected virtual void OnReadOnlyChanged(EventArgs e) {
731 if (ReadOnlyChanged != null) {
732 ReadOnlyChanged(this, e);
736 protected override bool ProcessDialogKey(Keys keyData) {
737 switch (keyData & Keys.KeyCode) {
739 document.SetSelectionToCaret(true);
741 if ((Control.ModifierKeys & Keys.Control) != 0) {
742 document.MoveCaret(CaretDirection.WordBack);
744 document.MoveCaret(CaretDirection.CharBack);
746 CaretMoved(this, null);
751 document.SetSelectionToCaret(true);
753 if ((Control.ModifierKeys & Keys.Control) != 0) {
754 document.MoveCaret(CaretDirection.WordForward);
756 document.MoveCaret(CaretDirection.CharForward);
758 CaretMoved(this, null);
763 document.SetSelectionToCaret(true);
764 document.MoveCaret(CaretDirection.LineUp);
765 CaretMoved(this, null);
770 document.SetSelectionToCaret(true);
771 document.MoveCaret(CaretDirection.LineDown);
772 CaretMoved(this, null);
777 document.SetSelectionToCaret(true);
779 if ((Control.ModifierKeys & Keys.Control) != 0) {
780 document.MoveCaret(CaretDirection.CtrlHome);
782 document.MoveCaret(CaretDirection.Home);
784 CaretMoved(this, null);
789 document.SetSelectionToCaret(true);
791 if ((Control.ModifierKeys & Keys.Control) != 0) {
792 document.MoveCaret(CaretDirection.CtrlEnd);
794 document.MoveCaret(CaretDirection.End);
796 CaretMoved(this, null);
801 if (!read_only && multiline && (accepts_return || ((Control.ModifierKeys & Keys.Control) != 0))) {
804 if (document.selection_visible) {
805 document.ReplaceSelection("");
807 document.SetSelectionToCaret(true);
809 line = document.CaretLine;
811 document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
812 OnTextChanged(EventArgs.Empty);
813 document.UpdateView(line, 2, 0);
814 document.MoveCaret(CaretDirection.CharForward);
815 CaretMoved(this, null);
822 if (!read_only && accepts_tab) {
823 document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
824 if (document.selection_visible) {
825 document.ReplaceSelection("");
827 document.SetSelectionToCaret(true);
829 OnTextChanged(EventArgs.Empty);
830 CaretMoved(this, null);
842 // delete only deletes on the line, doesn't do the combine
843 if (document.selection_visible) {
844 document.ReplaceSelection("");
846 document.SetSelectionToCaret(true);
847 if (document.CaretPosition == 0) {
848 if (document.CaretLine.LineNo > 1) {
852 line = document.GetLine(document.CaretLine.LineNo - 1);
853 new_caret_pos = line.text.Length;
855 document.Combine(line, document.CaretLine);
856 document.UpdateView(line, 1, 0);
857 document.PositionCaret(line, new_caret_pos);
858 document.UpdateCaret();
859 OnTextChanged(EventArgs.Empty);
862 document.DeleteChar(document.CaretTag, document.CaretPosition, false);
863 document.MoveCaret(CaretDirection.CharBack);
864 OnTextChanged(EventArgs.Empty);
866 CaretMoved(this, null);
875 // delete only deletes on the line, doesn't do the combine
876 if (document.CaretPosition == document.CaretLine.text.Length) {
877 if (document.CaretLine.LineNo < document.Lines) {
880 line = document.GetLine(document.CaretLine.LineNo + 1);
881 document.Combine(document.CaretLine, line);
882 document.UpdateView(document.CaretLine, 2, 0);
883 OnTextChanged(EventArgs.Empty);
889 check_first = document.GetLine(document.CaretLine.LineNo);
890 check_second = document.GetLine(check_first.line_no + 1);
892 Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
895 // Caret doesn't move
898 document.DeleteChar(document.CaretTag, document.CaretPosition, true);
899 OnTextChanged(EventArgs.Empty);
901 CaretMoved(this, null);
905 return base.ProcessDialogKey (keyData);
908 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
909 // Make sure we don't get sized bigger than we want to be
912 if (height > PreferredHeight) {
913 requested_height = height;
914 height = PreferredHeight;
915 specified |= BoundsSpecified.Height;
920 document.ViewPortWidth = this.Width;
921 document.ViewPortHeight = this.Height;
925 base.SetBoundsCore (x, y, width, height, specified);
928 protected override void WndProc(ref Message m) {
929 switch ((Msg)m.Msg) {
931 PaintEventArgs paint_event;
933 paint_event = XplatUI.PaintEventStart(Handle);
935 PaintControl(paint_event);
936 XplatUI.PaintEventEnd(Handle);
941 case Msg.WM_SETFOCUS: {
943 document.CaretHasFocus();
944 Console.WriteLine("Creating caret");
949 case Msg.WM_KILLFOCUS: {
951 document.CaretLostFocus();
952 Console.WriteLine("Destroying caret");
958 if (ProcessKeyEventArgs(ref m)) {
962 if (PreProcessMessage(ref m)) {
966 if (ProcessKeyMessage(ref m)) {
970 if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
971 if (document.selection_visible) {
972 document.ReplaceSelection("");
975 switch (character_casing) {
976 case CharacterCasing.Normal: {
977 document.InsertCharAtCaret((char)m.WParam, true);
978 OnTextChanged(EventArgs.Empty);
979 CaretMoved(this, null);
983 case CharacterCasing.Lower: {
984 document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
985 OnTextChanged(EventArgs.Empty);
986 CaretMoved(this, null);
990 case CharacterCasing.Upper: {
991 document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
992 OnTextChanged(EventArgs.Empty);
993 CaretMoved(this, null);
1003 base.WndProc(ref m);
1009 #endregion // Protected Instance Methods
1012 public event EventHandler AcceptsTabChanged;
1013 public event EventHandler AutoSizeChanged;
1015 [EditorBrowsable(EditorBrowsableState.Never)]
1016 public new event EventHandler BackgroundImageChanged {
1017 add { base.BackgroundImageChanged += value; }
1018 remove { base.BackgroundImageChanged -= value; }
1020 public event EventHandler BorderStyleChanged;
1022 [EditorBrowsable(EditorBrowsableState.Advanced)]
1023 public event EventHandler Click;
1024 public event EventHandler HideSelectionChanged;
1025 public event EventHandler ModifiedChanged;
1026 public event EventHandler MultilineChanged;
1028 [EditorBrowsable(EditorBrowsableState.Never)]
1029 public event PaintEventHandler Paint;
1030 public event EventHandler ReadOnlyChanged;
1031 #endregion // Events
1033 #region Private Methods
1034 internal Document Document {
1046 private void PaintControl(PaintEventArgs pevent) {
1048 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
1049 pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
1051 // Draw the viewable document
1052 document.Draw(pevent.Graphics, pevent.ClipRectangle);
1054 Rectangle rect = ClientRectangle;
1057 //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorButtonShadow), rect);
1066 p = new Pen(Color.Red, 1);
1068 // First, figure out from what line to what line we need to draw
1069 start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
1070 end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
1072 //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
1073 //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
1076 while (line_no <= end) {
1077 line = document.GetLine(line_no);
1080 for (int i = 0; i < line.text.Length; i++) {
1081 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);
1090 private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
1095 if (e.Button == MouseButtons.Left) {
1096 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1097 document.SetSelectionToCaret(true);
1098 this.grabbed = true;
1099 this.Capture = true;
1104 if (e.Button == MouseButtons.Right) {
1105 draw_lines = !draw_lines;
1107 Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
1108 Console.WriteLine("Selection start: {0}", this.SelectionStart);
1110 this.SelectionStart = 10;
1111 this.SelectionLength = 5;
1116 tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
1118 Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
1121 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;
1122 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;
1123 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;
1124 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;
1125 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;
1126 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;
1133 // Update/Recalculate what we see
1134 document.UpdateView(line, 0);
1136 // Make sure our caret is properly positioned and sized
1137 document.AlignCaret();
1141 private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
1142 this.Capture = false;
1143 this.grabbed = false;
1144 if (e.Button == MouseButtons.Left) {
1145 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1146 document.SetSelectionToCaret(false);
1147 document.DisplayCaret();
1151 #endregion // Private Methods
1154 private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
1155 canvas_width = this.Width;
1156 canvas_height = this.Height;
1157 // We always move them, they just might not be displayed
1158 hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, Width, hscroll.Height);
1159 vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, Height);
1163 private void CalculateDocument() {
1164 document.RecalculateDocument(CreateGraphics());
1165 CalculateScrollBars();
1166 Invalidate(); // FIXME - do we need this?
1169 protected void CalculateScrollBars() {
1170 // No scrollbars for a single line
1171 if (document.Width >= this.Width) {
1172 hscroll.Enabled = true;
1173 hscroll.Minimum = 0;
1174 hscroll.Maximum = document.Width - this.Width;
1176 hscroll.Enabled = false;
1179 if (document.Height >= this.Height) {
1180 vscroll.Enabled = true;
1181 vscroll.Minimum = 0;
1182 vscroll.Maximum = document.Height - this.Height;
1184 vscroll.Enabled = false;
1192 if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
1193 if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
1194 hscroll.Visible = true;
1198 if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
1199 if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
1200 vscroll.Visible = true;
1204 if (hscroll.Visible) {
1205 vscroll.Maximum += hscroll.Height;
1206 canvas_height = this.Height - hscroll.Height;
1209 if (vscroll.Visible) {
1210 hscroll.Maximum += vscroll.Width * 2;
1211 canvas_width = this.Width - vscroll.Width * 2;
1215 private void document_WidthChanged(object sender, EventArgs e) {
\r
1216 CalculateScrollBars();
\r
1219 private void hscroll_ValueChanged(object sender, EventArgs e) {
1220 XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
1221 document.ViewPortX = this.hscroll.Value;
1222 document.UpdateCaret();
1223 //Console.WriteLine("Dude scrolled horizontal");
1226 private void vscroll_ValueChanged(object sender, EventArgs e) {
\r
1227 XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
1228 document.ViewPortY = this.vscroll.Value;
1229 document.UpdateCaret();
1230 //Console.WriteLine("Dude scrolled vertical");
1233 private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
1234 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
1236 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1237 document.SetSelectionToCaret(false);
1238 document.DisplayCaret();
1242 private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
1246 // Font changes apply to the whole document
1247 for (int i = 1; i <= document.Lines; i++) {
1248 line = document.GetLine(i);
1249 LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
1250 document.UpdateView(line, 0);
1252 // Make sure the caret height is matching the new font height
1253 document.AlignCaret();
1257 /// <summary>Ensure the caret is always visible</summary>
\r
1258 internal void CaretMoved(object sender, EventArgs e) {
\r
1262 pos = document.Caret;
\r
1263 //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
1265 // Handle horizontal scrolling
\r
1266 if (pos.X < (document.ViewPortX + track_width)) {
\r
1268 if ((hscroll.Value - track_width) >= hscroll.Minimum) {
\r
1269 hscroll.Value -= track_width;
\r
1271 hscroll.Value = hscroll.Minimum;
\r
1273 } while (hscroll.Value > pos.X);
\r
1276 if ((pos.X > (this.canvas_width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
\r
1278 if ((hscroll.Value + track_width) <= hscroll.Maximum) {
\r
1279 hscroll.Value += track_width;
\r
1281 hscroll.Value = hscroll.Maximum;
\r
1283 } while (pos.X > (hscroll.Value + this.canvas_width));
\r
1290 // Handle vertical scrolling
\r
1291 height = document.CaretLine.Height;
\r
1293 if (pos.Y < document.ViewPortY) {
\r
1294 vscroll.Value = pos.Y;
\r
1297 if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
\r
1298 vscroll.Value = pos.Y - canvas_height + height;
\r