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.HeightChanged += new EventHandler(document_HeightChanged);
92 //document.CaretMoved += new EventHandler(CaretMoved);
93 document.Wrap = false;
94 requested_height = -1;
96 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
97 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
98 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
99 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
100 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
101 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
103 scrollbars = RichTextBoxScrollBars.None;
105 hscroll = new HScrollBar();
106 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
107 hscroll.control_style &= ~ControlStyles.Selectable;
108 hscroll.Enabled = false;
109 hscroll.Visible = false;
111 vscroll = new VScrollBar();
112 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
113 vscroll.control_style &= ~ControlStyles.Selectable;
114 vscroll.Enabled = false;
115 vscroll.Visible = false;
118 this.Controls.AddImplicit (hscroll);
119 this.Controls.AddImplicit (vscroll);
122 //SetStyle(ControlStyles.ResizeRedraw, true);
123 SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
125 canvas_width = ClientSize.Width;
126 canvas_height = ClientSize.Height;
128 CalculateScrollBars();
130 #endregion // Internal Constructor
132 #region Private and Internal Methods
133 internal string CaseAdjust(string s) {
134 if (character_casing == CharacterCasing.Normal) {
137 if (character_casing == CharacterCasing.Lower) {
143 #endregion // Private and Internal Methods
145 #region Public Instance Properties
146 [DefaultValue(false)]
147 public bool AcceptsTab {
153 if (value != accepts_tab) {
155 OnAcceptsTabChanged(EventArgs.Empty);
162 [RefreshProperties(RefreshProperties.Repaint)]
163 public virtual bool AutoSize {
169 if (value != auto_size) {
172 if (PreferredHeight != ClientSize.Height) {
173 ClientSize = new Size(ClientSize.Width, PreferredHeight);
176 OnAutoSizeChanged(EventArgs.Empty);
182 public override System.Drawing.Color BackColor {
184 return base.BackColor;
187 base.BackColor = value;
192 [EditorBrowsable(EditorBrowsableState.Never)]
193 public override System.Drawing.Image BackgroundImage {
195 return base.BackgroundImage;
198 base.BackgroundImage = value;
202 [DefaultValue(BorderStyle.Fixed3D)]
204 public BorderStyle BorderStyle {
205 get { return InternalBorderStyle; }
207 InternalBorderStyle = value;
208 OnBorderStyleChanged(EventArgs.Empty);
213 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
214 public bool CanUndo {
221 public override System.Drawing.Color ForeColor {
223 return base.ForeColor;
226 base.ForeColor = value;
231 public bool HideSelection {
233 return hide_selection;
237 if (value != hide_selection) {
238 hide_selection = value;
239 OnHideSelectionChanged(EventArgs.Empty);
241 if (hide_selection) {
242 document.selection_visible = false;
244 document.selection_visible = true;
246 document.InvalidateSelectionArea();
251 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
252 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
254 public string[] Lines {
261 lines = new string[l];
263 for (i = 1; i <= l; i++) {
264 lines[i - 1] = document.GetLine(i).text.ToString();
278 brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
280 for (i = 0; i < l; i++) {
281 document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
284 OnTextChanged(EventArgs.Empty);
288 [DefaultValue(32767)]
290 public virtual int MaxLength {
296 if (value != max_length) {
303 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
304 public bool Modified {
310 if (value != modified) {
312 OnModifiedChanged(EventArgs.Empty);
317 [DefaultValue(false)]
319 [RefreshProperties(RefreshProperties.All)]
320 public virtual bool Multiline {
326 if (value != multiline) {
328 // Make sure we update our size; the user may have already set the size before going to multiline
329 if (multiline && requested_height != -1) {
330 Height = requested_height;
331 requested_height = -1;
334 OnMultilineChanged(EventArgs.Empty);
337 document.multiline = multiline;
340 document.Wrap = word_wrap;
342 document.Wrap = false;
348 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
349 [EditorBrowsable(EditorBrowsableState.Advanced)]
350 public int PreferredHeight {
352 return this.Font.Height + 7; // FIXME - consider border style as well
356 [DefaultValue(false)]
357 public bool ReadOnly {
363 if (value != read_only) {
365 OnReadOnlyChanged(EventArgs.Empty);
371 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
372 public virtual string SelectedText {
374 return document.GetSelection();
379 document.ReplaceSelection(CaseAdjust(value));
380 OnTextChanged(EventArgs.Empty);
386 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
387 public virtual int SelectionLength {
389 return document.SelectionLength();
399 start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
401 document.CharIndexToLineTag(start + value, out line, out tag, out pos);
402 document.SetSelectionEnd(line, pos);
403 document.PositionCaret(line, pos);
405 document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
406 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
412 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
413 public int SelectionStart {
417 index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
427 document.CharIndexToLineTag(value, out line, out tag, out pos);
428 document.SetSelectionStart(line, pos);
433 public override string Text {
435 if (document == null || document.Root == null || document.Root.text == null) {
440 return document.Root.text.ToString();
445 sb = new StringBuilder();
447 for (i = 1; i < document.Lines; i++) {
448 sb.Append(document.GetLine(i).text.ToString() + Environment.NewLine);
450 sb.Append(document.GetLine(i).text.ToString());
451 return sb.ToString();
456 if (value == base.Text) {
466 lines = value.Split(new char[] {'\n'});
468 for (int i = 0; i < lines.Length; i++) {
469 if (lines[i].EndsWith("\r")) {
470 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
475 line = document.GetLine(1);
476 document.SetSelectionStart(line, 0);
478 line = document.GetLine(document.Lines);
479 document.SetSelectionEnd(line, line.text.Length);
480 document.PositionCaret(line, line.text.Length);
483 document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
485 line = document.GetLine(1);
486 document.SetSelectionStart(line, 0);
487 document.SetSelectionEnd(line, value.Length);
488 document.PositionCaret(line, value.Length);
492 OnTextChanged(EventArgs.Empty);
497 public virtual int TextLength {
499 if (document == null || document.Root == null || document.Root.text == null) {
504 return document.Root.text.Length;
510 for (i = 1; i < document.Lines; i++) {
511 total += document.GetLine(i).text.Length + Environment.NewLine.Length;
513 total += document.GetLine(i).text.Length;
522 public bool WordWrap {
528 if (value != word_wrap) {
531 document.Wrap = value;
536 #endregion // Public Instance Properties
538 #region Protected Instance Properties
539 protected override CreateParams CreateParams {
541 return base.CreateParams;
545 protected override System.Drawing.Size DefaultSize {
547 return base.DefaultSize;
550 #endregion // Protected Instance Properties
552 #region Public Instance Methods
553 public void AppendText(string text) {
558 // Break the string into separate lines
559 lines = text.Split(new char[] {'\n'});
560 linecount = lines.Length;
561 for (int i = 0; i < linecount; i++) {
562 if (lines[i].EndsWith("\r")) {
563 lines[i] = lines[i].Substring(0, lines[i].Length - 1);
567 // Grab the formatting for the last element
568 document.MoveCaret(CaretDirection.CtrlEnd);
570 // Insert the first line
571 document.InsertString(document.CaretLine, document.CaretPosition, lines[0]);
573 for (int i = 1; i < linecount; i++) {
574 document.Add(document.CaretLine.LineNo+i, CaseAdjust(lines[i]), alignment, document.CaretTag.font, document.CaretTag.color);
578 document.MoveCaret(CaretDirection.CtrlEnd);
580 document.MoveCaret(CaretDirection.CtrlEnd);
581 document.InsertStringAtCaret(text, true);
582 //blah Console.WriteLine("TextBox.cs(582) Invalidate called in AppendText");
585 OnTextChanged(EventArgs.Empty);
588 public void Clear() {
592 public void ClearUndo() {
594 throw new NotImplementedException();
599 throw new NotImplementedException();
604 throw new NotImplementedException();
607 public void Paste() {
609 throw new NotImplementedException();
612 public void ScrollToCaret() {
614 throw new NotImplementedException();
617 public void Select(int start, int length) {
618 SelectionStart = start;
619 SelectionLength = length;
623 public void SelectAll() {
626 last = document.GetLine(document.Lines);
627 document.SetSelectionStart(document.GetLine(1), 0);
628 document.SetSelectionEnd(last, last.text.Length);
631 public override string ToString() {
638 #endregion // Public Instance Methods
640 #region Protected Instance Methods
641 protected override void CreateHandle() {
642 base.CreateHandle ();
645 protected override bool IsInputKey(Keys keyData) {
648 // We handle Enter in ProcessDialogKey
650 if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
658 if (accepts_tab && multiline) {
668 protected virtual void OnAcceptsTabChanged(EventArgs e) {
669 if (AcceptsTabChanged != null) {
670 AcceptsTabChanged(this, e);
674 protected virtual void OnAutoSizeChanged(EventArgs e) {
675 if (AutoSizeChanged != null) {
676 AutoSizeChanged(this, e);
680 protected virtual void OnBorderStyleChanged(EventArgs e) {
681 if (BorderStyleChanged != null) {
682 BorderStyleChanged(this, e);
686 protected override void OnFontChanged(EventArgs e) {
687 base.OnFontChanged (e);
690 if (PreferredHeight != ClientSize.Height) {
691 Height = PreferredHeight;
696 protected override void OnHandleCreated(EventArgs e) {
697 base.OnHandleCreated (e);
700 protected override void OnHandleDestroyed(EventArgs e) {
701 base.OnHandleDestroyed (e);
704 protected virtual void OnHideSelectionChanged(EventArgs e) {
705 if (HideSelectionChanged != null) {
706 HideSelectionChanged(this, e);
710 protected virtual void OnModifiedChanged(EventArgs e) {
711 if (ModifiedChanged != null) {
712 ModifiedChanged(this, e);
716 protected virtual void OnMultilineChanged(EventArgs e) {
717 if (MultilineChanged != null) {
718 MultilineChanged(this, e);
722 protected virtual void OnReadOnlyChanged(EventArgs e) {
723 if (ReadOnlyChanged != null) {
724 ReadOnlyChanged(this, e);
728 protected override bool ProcessDialogKey(Keys keyData) {
729 switch (keyData & Keys.KeyCode) {
731 document.SetSelectionToCaret(true);
733 if ((Control.ModifierKeys & Keys.Control) != 0) {
734 document.MoveCaret(CaretDirection.WordBack);
736 document.MoveCaret(CaretDirection.CharBack);
738 CaretMoved(this, null);
743 document.SetSelectionToCaret(true);
745 if ((Control.ModifierKeys & Keys.Control) != 0) {
746 document.MoveCaret(CaretDirection.WordForward);
748 document.MoveCaret(CaretDirection.CharForward);
750 CaretMoved(this, null);
755 document.SetSelectionToCaret(true);
756 document.MoveCaret(CaretDirection.LineUp);
757 CaretMoved(this, null);
762 document.SetSelectionToCaret(true);
763 document.MoveCaret(CaretDirection.LineDown);
764 CaretMoved(this, null);
769 document.SetSelectionToCaret(true);
771 if ((Control.ModifierKeys & Keys.Control) != 0) {
772 document.MoveCaret(CaretDirection.CtrlHome);
774 document.MoveCaret(CaretDirection.Home);
776 CaretMoved(this, null);
781 document.SetSelectionToCaret(true);
783 if ((Control.ModifierKeys & Keys.Control) != 0) {
784 document.MoveCaret(CaretDirection.CtrlEnd);
786 document.MoveCaret(CaretDirection.End);
788 CaretMoved(this, null);
793 // ignoring accepts_return, fixes bug #76355
794 if (!read_only && multiline && (accepts_return || (FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
797 if (document.selection_visible) {
798 document.ReplaceSelection("");
800 document.SetSelectionToCaret(true);
802 line = document.CaretLine;
804 document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
805 OnTextChanged(EventArgs.Empty);
806 document.UpdateView(line, 2, 0);
807 document.MoveCaret(CaretDirection.CharForward);
808 CaretMoved(this, null);
815 if (!read_only && accepts_tab && multiline) {
816 document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
817 if (document.selection_visible) {
818 document.ReplaceSelection("");
820 document.SetSelectionToCaret(true);
822 OnTextChanged(EventArgs.Empty);
823 CaretMoved(this, null);
835 // delete only deletes on the line, doesn't do the combine
836 if (document.selection_visible) {
837 document.ReplaceSelection("");
839 document.SetSelectionToCaret(true);
840 if (document.CaretPosition == 0) {
841 if (document.CaretLine.LineNo > 1) {
845 line = document.GetLine(document.CaretLine.LineNo - 1);
846 new_caret_pos = line.text.Length;
848 document.Combine(line, document.CaretLine);
849 document.UpdateView(line, 1, 0);
850 document.PositionCaret(line, new_caret_pos);
851 document.MoveCaret(CaretDirection.CharForward);
852 document.UpdateCaret();
853 OnTextChanged(EventArgs.Empty);
856 document.DeleteChar(document.CaretTag, document.CaretPosition, false);
857 document.MoveCaret(CaretDirection.CharBack);
858 OnTextChanged(EventArgs.Empty);
860 CaretMoved(this, null);
869 // delete only deletes on the line, doesn't do the combine
870 if (document.CaretPosition == document.CaretLine.text.Length) {
871 if (document.CaretLine.LineNo < document.Lines) {
874 line = document.GetLine(document.CaretLine.LineNo + 1);
875 document.Combine(document.CaretLine, line);
876 document.UpdateView(document.CaretLine, 2, 0);
877 OnTextChanged(EventArgs.Empty);
883 check_first = document.GetLine(document.CaretLine.LineNo);
884 check_second = document.GetLine(check_first.line_no + 1);
886 Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
889 // Caret doesn't move
892 document.DeleteChar(document.CaretTag, document.CaretPosition, true);
893 OnTextChanged(EventArgs.Empty);
895 document.AlignCaret();
896 document.UpdateCaret();
897 CaretMoved(this, null);
901 return base.ProcessDialogKey (keyData);
904 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
905 // Make sure we don't get sized bigger than we want to be
908 if (height > PreferredHeight) {
909 requested_height = height;
910 height = PreferredHeight;
911 specified |= BoundsSpecified.Height;
916 document.ViewPortWidth = width;
917 document.ViewPortHeight = height;
921 base.SetBoundsCore (x, y, width, height, specified);
924 protected override void WndProc(ref Message m) {
925 switch ((Msg)m.Msg) {
927 PaintEventArgs paint_event;
929 paint_event = XplatUI.PaintEventStart(Handle, true);
931 PaintControl(paint_event);
932 XplatUI.PaintEventEnd(Handle, true);
937 case Msg.WM_SETFOCUS: {
939 document.CaretHasFocus();
940 Console.WriteLine("Creating caret");
945 case Msg.WM_KILLFOCUS: {
947 document.CaretLostFocus();
948 Console.WriteLine("Destroying caret");
954 if (ProcessKeyEventArgs(ref m)) {
958 if (PreProcessMessage(ref m)) {
962 if (ProcessKeyMessage(ref m)) {
966 if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
967 if (document.selection_visible) {
968 document.ReplaceSelection("");
971 switch (character_casing) {
972 case CharacterCasing.Normal: {
973 document.InsertCharAtCaret((char)m.WParam, true);
974 OnTextChanged(EventArgs.Empty);
975 CaretMoved(this, null);
979 case CharacterCasing.Lower: {
980 document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
981 OnTextChanged(EventArgs.Empty);
982 CaretMoved(this, null);
986 case CharacterCasing.Upper: {
987 document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
988 OnTextChanged(EventArgs.Empty);
989 CaretMoved(this, null);
1005 #endregion // Protected Instance Methods
1008 public event EventHandler AcceptsTabChanged;
1009 public event EventHandler AutoSizeChanged;
1011 [EditorBrowsable(EditorBrowsableState.Never)]
1012 public new event EventHandler BackgroundImageChanged {
1013 add { base.BackgroundImageChanged += value; }
1014 remove { base.BackgroundImageChanged -= value; }
1016 public event EventHandler BorderStyleChanged;
1018 [EditorBrowsable(EditorBrowsableState.Advanced)]
1019 public event EventHandler Click;
1020 public event EventHandler HideSelectionChanged;
1021 public event EventHandler ModifiedChanged;
1022 public event EventHandler MultilineChanged;
1024 [EditorBrowsable(EditorBrowsableState.Never)]
1025 public event PaintEventHandler Paint;
1026 public event EventHandler ReadOnlyChanged;
1027 #endregion // Events
1029 #region Private Methods
1030 internal Document Document {
1042 private void PaintControl(PaintEventArgs pevent) {
1044 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
1045 pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
1046 //blah Console.WriteLine("Redrawing {0}", pevent.ClipRectangle);
1047 // Draw the viewable document
1048 document.Draw(pevent.Graphics, pevent.ClipRectangle);
1050 Rectangle rect = ClientRectangle;
1053 //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
1062 p = new Pen(Color.Red, 1);
1064 // First, figure out from what line to what line we need to draw
1065 start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
1066 end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
1068 //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
1069 //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
1072 while (line_no <= end) {
1073 line = document.GetLine(line_no);
1076 for (int i = 0; i < line.text.Length; i++) {
1077 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);
1086 private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
1087 if (e.Button == MouseButtons.Left) {
1088 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1089 document.SetSelectionToCaret(true);
1090 this.grabbed = true;
1091 this.Capture = true;
1100 if (e.Button == MouseButtons.Right) {
1101 draw_lines = !draw_lines;
1103 Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
1104 Console.WriteLine("Selection start: {0}", this.SelectionStart);
1106 this.SelectionStart = 10;
1107 this.SelectionLength = 5;
1112 tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
1114 Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
1117 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;
1118 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;
1119 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;
1120 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;
1121 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;
1122 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;
1129 // Update/Recalculate what we see
1130 document.UpdateView(line, 0);
1132 // Make sure our caret is properly positioned and sized
1133 document.AlignCaret();
1137 private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
1138 this.Capture = false;
1139 this.grabbed = false;
1140 if (e.Button == MouseButtons.Left) {
1141 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1142 document.SetSelectionToCaret(false);
1143 document.DisplayCaret();
1147 #endregion // Private Methods
1150 private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
1151 canvas_width = ClientSize.Width;
1152 canvas_height = ClientSize.Height;
1154 // We always move them, they just might not be displayed
1155 hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, ClientSize.Width, hscroll.Height);
1156 vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, ClientSize.Height);
1160 internal void CalculateDocument() {
1161 if (!IsHandleCreated) {
1164 document.RecalculateDocument(CreateGraphics());
1165 CalculateScrollBars();
1166 //blah Console.WriteLine("TextBox.cs(1175) Invalidate called in CalculateDocument");
1167 Invalidate(); // FIXME - do we need this?
1170 internal void CalculateScrollBars() {
1171 // FIXME - need separate calculations for center and right alignment
1172 // No scrollbars for a single line
1173 if (document.Width >= ClientSize.Width) {
1174 hscroll.Enabled = true;
1175 hscroll.Minimum = 0;
1176 hscroll.Maximum = document.Width - ClientSize.Width;
1178 hscroll.Maximum = document.ViewPortWidth;
1179 hscroll.Enabled = false;
1182 if (document.Height >= ClientSize.Height) {
1183 vscroll.Enabled = true;
1184 vscroll.Minimum = 0;
1185 vscroll.Maximum = document.Height - ClientSize.Height;
1187 vscroll.Maximum = document.ViewPortHeight;
1188 vscroll.Enabled = false;
1197 if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
1198 if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
1199 hscroll.Visible = true;
1204 if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
1205 if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
1206 vscroll.Visible = true;
1210 if (hscroll.Visible) {
1211 vscroll.Maximum += hscroll.Height;
1212 canvas_height = ClientSize.Height - hscroll.Height;
1215 if (vscroll.Visible) {
1216 hscroll.Maximum += vscroll.Width * 2;
1217 canvas_width = ClientSize.Width - vscroll.Width * 2;
1221 private void document_WidthChanged(object sender, EventArgs e) {
1222 CalculateScrollBars();
1225 private void document_HeightChanged(object sender, EventArgs e) {
1226 CalculateScrollBars();
1229 private void hscroll_ValueChanged(object sender, EventArgs e) {
1230 XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
1231 document.ViewPortX = this.hscroll.Value;
1232 document.UpdateCaret();
1233 //Console.WriteLine("Dude scrolled horizontal");
1236 private void vscroll_ValueChanged(object sender, EventArgs e) {
1237 XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
1238 document.ViewPortY = this.vscroll.Value;
1239 document.UpdateCaret();
1240 //Console.WriteLine("Dude scrolled vertical");
1243 private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
1244 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
1246 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1247 document.SetSelectionToCaret(false);
1248 document.DisplayCaret();
1252 private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
1256 // Font changes apply to the whole document
1257 for (int i = 1; i <= document.Lines; i++) {
1258 line = document.GetLine(i);
1259 LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
1260 document.UpdateView(line, 0);
1262 // Make sure the caret height is matching the new font height
1263 document.AlignCaret();
1267 /// <summary>Ensure the caret is always visible</summary>
1268 internal void CaretMoved(object sender, EventArgs e) {
1272 pos = document.Caret;
1273 //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);
1275 // Handle horizontal scrolling
1276 if (document.CaretLine.alignment == HorizontalAlignment.Left) {
1277 if (pos.X < (document.ViewPortX + track_width)) {
1279 if ((hscroll.Value - track_width) >= hscroll.Minimum) {
1280 hscroll.Value -= track_width;
1282 hscroll.Value = hscroll.Minimum;
1284 } while (hscroll.Value > pos.X);
1287 if ((pos.X > (this.canvas_width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
1289 if ((hscroll.Value + track_width) <= hscroll.Maximum) {
1290 hscroll.Value += track_width;
1292 hscroll.Value = hscroll.Maximum;
1294 } while (pos.X > (hscroll.Value + this.canvas_width));
1296 } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
1297 if (pos.X < document.ViewPortX) {
1298 if (pos.X > hscroll.Minimum) {
1299 hscroll.Value = pos.X;
1301 hscroll.Value = hscroll.Minimum;
1305 if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
1306 hscroll.Value = hscroll.Maximum;
1315 // Handle vertical scrolling
1316 height = document.CaretLine.Height;
1318 if (pos.Y < document.ViewPortY) {
1319 vscroll.Value = pos.Y;
1322 if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
1323 vscroll.Value = pos.Y - canvas_height + height;