- PreferredHeight is a little different than i thought.
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextBoxBase.cs
blob011e1061775a19847430d966412b92621c40f02a
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:
8 //
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-2006 Novell, Inc. (http://www.novell.com)
22 // Authors:
23 // Peter Bartok pbartok@novell.com
27 // NOT COMPLETE
28 #undef Debug
29 #undef DebugClick
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.Drawing;
34 using System.Drawing.Text;
35 using System.Text;
36 using System.Runtime.InteropServices;
37 using System.Collections;
39 namespace System.Windows.Forms {
40 [DefaultEvent("TextChanged")]
41 [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
42 public abstract class TextBoxBase : Control {
43 #region Local Variables
44 internal HorizontalAlignment alignment;
45 internal bool accepts_tab;
46 internal bool accepts_return;
47 internal bool auto_size;
48 internal bool backcolor_set;
49 internal CharacterCasing character_casing;
50 internal bool hide_selection;
51 internal int max_length;
52 internal bool modified;
53 internal bool multiline;
54 internal char password_char;
55 internal bool read_only;
56 internal bool word_wrap;
57 internal Document document;
58 internal LineTag caret_tag; // tag our cursor is in
59 internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
60 internal ImplicitHScrollBar hscroll;
61 internal ImplicitVScrollBar vscroll;
62 internal RichTextBoxScrollBars scrollbars;
63 internal Timer scroll_timer;
64 internal bool richtext;
65 internal bool show_selection; // set to true to always show selection, even if no focus is set
66 internal int selection_length = -1; // set to the user-specified selection length, or -1 if none
67 internal bool show_caret_w_selection; // TextBox shows the caret when the selection is visible
68 internal int requested_height;
69 internal int canvas_width;
70 internal int canvas_height;
71 static internal int track_width = 2; //
72 static internal int track_border = 5; //
73 internal DateTime click_last;
74 internal int click_point_x;
75 internal int click_point_y;
76 internal CaretSelection click_mode;
77 internal Bitmap bmp;
78 #if Debug
79 internal static bool draw_lines = false;
80 #endif
82 #endregion // Local Variables
84 #region Internal Constructor
85 // Constructor will go when complete, only for testing - pdb
86 internal TextBoxBase() {
87 alignment = HorizontalAlignment.Left;
88 accepts_return = false;
89 accepts_tab = false;
90 auto_size = true;
91 border_style = BorderStyle.Fixed3D;
92 character_casing = CharacterCasing.Normal;
93 hide_selection = true;
94 max_length = 32767;
95 modified = true;
96 multiline = false;
97 password_char = '\0';
98 read_only = false;
99 word_wrap = true;
100 richtext = false;
101 show_selection = false;
102 show_caret_w_selection = (this is TextBox);
103 document = new Document(this);
104 document.WidthChanged += new EventHandler(document_WidthChanged);
105 document.HeightChanged += new EventHandler(document_HeightChanged);
106 //document.CaretMoved += new EventHandler(CaretMoved);
107 document.Wrap = false;
108 requested_height = -1;
109 click_last = DateTime.Now;
110 click_mode = CaretSelection.Position;
111 bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
113 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
114 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
115 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
116 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
117 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
118 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
119 MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
121 scrollbars = RichTextBoxScrollBars.None;
123 hscroll = new ImplicitHScrollBar();
124 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
125 hscroll.SetStyle (ControlStyles.Selectable, false);
126 hscroll.Enabled = false;
127 hscroll.Visible = false;
128 hscroll.Maximum = Int32.MaxValue;
130 vscroll = new ImplicitVScrollBar();
131 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
132 vscroll.SetStyle (ControlStyles.Selectable, false);
133 vscroll.Enabled = false;
134 vscroll.Visible = false;
135 vscroll.Maximum = Int32.MaxValue;
137 SuspendLayout ();
138 this.Controls.AddImplicit (hscroll);
139 this.Controls.AddImplicit (vscroll);
140 ResumeLayout ();
142 SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
143 #if NET_2_0
144 SetStyle(ControlStyles.UseTextForAccessibility, false);
145 #endif
147 canvas_width = ClientSize.Width;
148 canvas_height = ClientSize.Height;
149 document.ViewPortWidth = canvas_width;
150 document.ViewPortHeight = canvas_height;
152 Cursor = Cursors.IBeam;
154 CalculateScrollBars();
156 #endregion // Internal Constructor
158 #region Private and Internal Methods
159 internal string CaseAdjust(string s) {
160 if (character_casing == CharacterCasing.Normal) {
161 return s;
163 if (character_casing == CharacterCasing.Lower) {
164 return s.ToLower();
165 } else {
166 return s.ToUpper();
170 internal override void HandleClick(int clicks, MouseEventArgs me) {
171 // MS seems to fire the click event in spite of the styles they set
172 bool click_set = GetStyle (ControlStyles.StandardClick);
173 bool doubleclick_set = GetStyle (ControlStyles.StandardDoubleClick);
175 // so explicitly set them to true first
176 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
178 base.HandleClick (clicks, me);
180 // then revert to our previous state
181 if (!click_set)
182 SetStyle (ControlStyles.StandardClick, false);
183 if (!doubleclick_set)
184 SetStyle (ControlStyles.StandardDoubleClick, false);
187 #endregion // Private and Internal Methods
189 #region Public Instance Properties
190 [DefaultValue(false)]
191 [MWFCategory("Behavior")]
192 public bool AcceptsTab {
193 get {
194 return accepts_tab;
197 set {
198 if (value != accepts_tab) {
199 accepts_tab = value;
200 OnAcceptsTabChanged(EventArgs.Empty);
205 [DefaultValue(true)]
206 [Localizable(true)]
207 [RefreshProperties(RefreshProperties.Repaint)]
208 [MWFCategory("Behavior")]
209 public
210 #if NET_2_0
211 override
212 #else
213 virtual
214 #endif
215 bool AutoSize {
216 get {
217 return auto_size;
220 set {
221 if (value != auto_size) {
222 auto_size = value;
223 if (auto_size) {
224 if (PreferredHeight != ClientSize.Height) {
225 ClientSize = new Size(ClientSize.Width, PreferredHeight);
228 OnAutoSizeChanged(EventArgs.Empty);
233 [DispId(-501)]
234 public override System.Drawing.Color BackColor {
235 get {
236 return base.BackColor;
238 set {
239 if (value != ThemeEngine.Current.ColorWindow) {
240 backcolor_set = true;
241 } else {
242 backcolor_set = false;
244 base.BackColor = value;
248 [Browsable(false)]
249 [EditorBrowsable(EditorBrowsableState.Never)]
250 public override System.Drawing.Image BackgroundImage {
251 get {
252 return base.BackgroundImage;
254 set {
255 base.BackgroundImage = value;
259 [DefaultValue(BorderStyle.Fixed3D)]
260 [DispId(-504)]
261 [MWFCategory("Appearance")]
262 public BorderStyle BorderStyle {
263 get { return InternalBorderStyle; }
264 set {
265 InternalBorderStyle = value;
266 OnBorderStyleChanged(EventArgs.Empty);
270 [Browsable(false)]
271 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
272 public bool CanUndo {
273 get {
274 return document.undo.UndoLevels != 0;
278 [DispId(-513)]
279 public override System.Drawing.Color ForeColor {
280 get {
281 return base.ForeColor;
283 set {
284 base.ForeColor = value;
288 [DefaultValue(true)]
289 [MWFCategory("Behavior")]
290 public bool HideSelection {
291 get {
292 return hide_selection;
295 set {
296 if (value != hide_selection) {
297 hide_selection = value;
298 OnHideSelectionChanged(EventArgs.Empty);
300 if (hide_selection) {
301 document.selection_visible = false;
302 } else {
303 document.selection_visible = true;
305 document.InvalidateSelectionArea();
310 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
311 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
312 [Localizable(true)]
313 [MWFCategory("Appearance")]
314 public string[] Lines {
315 get {
316 int count;
317 ArrayList lines;
319 count = document.Lines;
321 // Handle empty document
322 if ((count == 1) && (document.GetLine (1).text.Length == 0)) {
323 return new string [0];
326 lines = new ArrayList ();
328 int i = 1;
329 while (i <= count) {
330 Line line;
331 StringBuilder lt = new StringBuilder ();
333 do {
334 line = document.GetLine (i++);
335 lt.Append (line.text.ToString ());
336 } while (line.soft_break && i <= count);
338 lines.Add (lt.ToString ());
341 return (string []) lines.ToArray (typeof (string));
344 set {
345 int i;
346 int l;
347 SolidBrush brush;
349 document.Empty();
351 l = value.Length;
352 brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
354 document.NoRecalc = true;
355 for (i = 0; i < l; i++) {
357 // Don't add the last line if it is just an empty line feed
358 // the line feed is reflected in the previous line's soft_break = false
359 if (i == l - 1 && value [i].Length == 0)
360 break;
362 bool carriage_return = false;
363 if (value [i].EndsWith ("\r")) {
364 value [i] = value [i].Substring (0, value [i].Length - 1);
365 carriage_return = true;
368 document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
369 if (carriage_return) {
370 Line line = document.GetLine (i + 1);
371 line.carriage_return = true;
375 document.NoRecalc = false;
377 // CalculateDocument();
378 OnTextChanged(EventArgs.Empty);
382 [DefaultValue(32767)]
383 [Localizable(true)]
384 [MWFCategory("Behavior")]
385 public virtual int MaxLength {
386 get {
387 if (max_length == 2147483646) { // We don't distinguish between single and multi-line limits
388 return 0;
390 return max_length;
393 set {
394 if (value != max_length) {
395 max_length = value;
400 [Browsable(false)]
401 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
402 public bool Modified {
403 get {
404 return modified;
407 set {
408 if (value != modified) {
409 modified = value;
410 OnModifiedChanged(EventArgs.Empty);
415 [DefaultValue(false)]
416 [Localizable(true)]
417 [RefreshProperties(RefreshProperties.All)]
418 [MWFCategory("Behavior")]
419 public virtual bool Multiline {
420 get {
421 return multiline;
424 set {
425 if (value != multiline) {
426 multiline = value;
427 // Make sure we update our size; the user may have already set the size before going to multiline
428 if (multiline && requested_height != -1) {
429 Height = requested_height;
430 requested_height = -1;
433 if (Parent != null)
434 Parent.PerformLayout ();
436 OnMultilineChanged(EventArgs.Empty);
439 document.multiline = multiline;
441 if (multiline) {
442 document.Wrap = word_wrap;
443 document.PasswordChar = "";
445 } else {
446 document.Wrap = false;
447 if (this.password_char != '\0') {
448 document.PasswordChar = password_char.ToString();
449 } else {
450 document.PasswordChar = "";
454 CalculateDocument ();
458 [Browsable(false)]
459 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
460 [EditorBrowsable(EditorBrowsableState.Advanced)]
461 public int PreferredHeight {
462 get {
463 return (BorderStyle == BorderStyle.None ? 0 : Font.Height + 7);
467 [DefaultValue(false)]
468 [MWFCategory("Behavior")]
469 public bool ReadOnly {
470 get {
471 return read_only;
474 set {
475 if (value != read_only) {
476 read_only = value;
477 OnReadOnlyChanged(EventArgs.Empty);
482 [Browsable(false)]
483 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
484 public virtual string SelectedText {
485 get {
486 return document.GetSelection();
489 set {
490 document.ReplaceSelection(CaseAdjust(value), false);
492 ScrollToCaret();
493 OnTextChanged(EventArgs.Empty);
497 [Browsable(false)]
498 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
499 public virtual int SelectionLength {
500 get {
501 int res = document.SelectionLength ();
502 if (res == 0)
503 res = -1;
504 return res;
507 set {
508 if (value < 0) {
509 throw new ArgumentException(String.Format("{0} is not a valid value", value), "value");
512 if (value != 0) {
513 int start;
514 Line line;
515 LineTag tag;
516 int pos;
518 selection_length = value;
520 start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
522 document.CharIndexToLineTag(start + value, out line, out tag, out pos);
523 document.SetSelectionEnd(line, pos);
524 document.PositionCaret(line, pos);
525 } else {
526 selection_length = -1;
528 document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
529 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
534 [Browsable(false)]
535 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
536 public int SelectionStart {
537 get {
538 int index;
540 index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
542 return index;
545 set {
546 document.SetSelectionStart(value);
547 if (selection_length > -1 ) {
548 document.SetSelectionEnd(value + selection_length);
549 } else {
550 document.SetSelectionEnd(value);
552 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
553 ScrollToCaret();
557 [Localizable(true)]
558 public override string Text {
559 get {
560 if (document == null || document.Root == null || document.Root.text == null) {
561 return string.Empty;
564 StringBuilder sb = new StringBuilder();
566 Line line = null;
567 for (int i = 1; i <= document.Lines; i++) {
568 if (line != null && line.carriage_return)
569 sb.Append ("\r");
570 if (line != null && !line.soft_break)
571 sb.Append (Environment.NewLine);
572 line = document.GetLine (i);
573 sb.Append(line.text.ToString());
576 return sb.ToString();
579 set {
580 if (value == Text)
581 return;
583 if ((value != null) && (value != "")) {
585 document.Empty ();
587 document.Insert (document.GetLine (1), 0, false, value);
589 document.PositionCaret (document.GetLine (1), 0);
590 document.SetSelectionToCaret (true);
592 ScrollToCaret ();
593 } else {
594 document.Empty();
595 CalculateDocument();
598 // set the var so OnModifiedChanged is not raised
599 modified = false;
600 OnTextChanged(EventArgs.Empty);
604 [Browsable(false)]
605 public virtual int TextLength {
606 get {
607 if (document == null || document.Root == null || document.Root.text == null) {
608 return 0;
611 if (!multiline) {
612 return document.Root.text.Length;
613 } else {
614 int total;
615 int i;
617 total = 0;
618 for (i = 1; i < document.Lines; i++) {
619 total += document.GetLine(i).text.Length + Environment.NewLine.Length;
621 total += document.GetLine(i).text.Length;
623 return total;
628 [DefaultValue(true)]
629 [Localizable(true)]
630 [MWFCategory("Behavior")]
631 public bool WordWrap {
632 get {
633 return word_wrap;
636 set {
637 if (value != word_wrap) {
638 if (multiline) {
639 word_wrap = value;
640 document.Wrap = value;
645 #endregion // Public Instance Properties
647 #region Protected Instance Properties
648 protected override CreateParams CreateParams {
649 get {
650 return base.CreateParams;
654 protected override System.Drawing.Size DefaultSize {
655 get {
656 return new Size(100, 20);
659 #endregion // Protected Instance Properties
661 #region Public Instance Methods
662 public void AppendText(string text) {
663 //if (multiline) {
665 document.MoveCaret(CaretDirection.CtrlEnd);
666 document.Insert (document.caret.line, /* document.caret.tag, */ document.caret.pos, false, text);
667 // CalculateDocument();
668 //} else {
669 // document.MoveCaret(CaretDirection.CtrlEnd);
670 // document.InsertStringAtCaret(text, true);
672 // Invalidate();
673 // }
675 document.MoveCaret(CaretDirection.CtrlEnd);
676 document.SetSelectionToCaret (true);
678 OnTextChanged(EventArgs.Empty);
681 public void Clear() {
682 Text = null;
685 public void ClearUndo() {
686 document.undo.Clear();
689 public void Copy() {
690 DataObject o;
692 o = new DataObject(DataFormats.Text, SelectedText);
693 if (this is RichTextBox) {
694 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
696 Clipboard.SetDataObject(o);
699 public void Cut() {
700 DataObject o;
702 o = new DataObject(DataFormats.Text, SelectedText);
703 if (this is RichTextBox) {
704 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
706 Clipboard.SetDataObject(o);
707 document.ReplaceSelection("", false);
710 public void Paste() {
711 Paste(Clipboard.GetDataObject(), null, false);
714 public void ScrollToCaret() {
715 if (IsHandleCreated) {
716 CaretMoved(this, EventArgs.Empty);
720 public void Select(int start, int length) {
721 SelectionStart = start;
722 SelectionLength = length;
726 public void SelectAll() {
727 Line last;
729 last = document.GetLine(document.Lines);
730 document.SetSelectionStart(document.GetLine(1), 0);
731 document.SetSelectionEnd(last, last.text.Length);
732 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
733 selection_length = -1;
736 public override string ToString() {
737 return String.Concat (base.ToString (), ", Text: ", Text);
740 public void Undo() {
741 document.undo.Undo();
743 #endregion // Public Instance Methods
745 #region Protected Instance Methods
746 protected override void CreateHandle() {
747 base.CreateHandle ();
748 document.AlignCaret();
749 ScrollToCaret();
752 protected override bool IsInputKey(Keys keyData) {
753 if ((keyData & Keys.Alt) != 0) {
754 return base.IsInputKey(keyData);
757 switch (keyData & Keys.KeyCode) {
758 case Keys.Enter: {
759 if (multiline && accepts_return) {
760 return true;
762 return false;
765 case Keys.Tab: {
766 if (accepts_tab && multiline) {
767 if ((keyData & Keys.Control) == 0) {
768 return true;
771 return false;
774 case Keys.Left:
775 case Keys.Right:
776 case Keys.Up:
777 case Keys.Down:
778 case Keys.PageUp:
779 case Keys.PageDown:
780 case Keys.Home:
781 case Keys.End: {
782 return true;
785 return false;
789 protected virtual void OnAcceptsTabChanged(EventArgs e) {
790 EventHandler eh = (EventHandler)(Events [AcceptsTabChangedEvent]);
791 if (eh != null)
792 eh (this, e);
795 protected virtual void OnAutoSizeChanged(EventArgs e) {
796 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
797 if (eh != null)
798 eh (this, e);
801 protected virtual void OnBorderStyleChanged(EventArgs e) {
802 EventHandler eh = (EventHandler)(Events [BorderStyleChangedEvent]);
803 if (eh != null)
804 eh (this, e);
807 protected override void OnFontChanged(EventArgs e) {
808 base.OnFontChanged (e);
810 if (auto_size && !multiline) {
811 if (PreferredHeight != ClientSize.Height) {
812 Height = PreferredHeight;
817 protected override void OnHandleCreated(EventArgs e) {
818 base.OnHandleCreated (e);
821 protected override void OnHandleDestroyed(EventArgs e) {
822 base.OnHandleDestroyed (e);
825 protected virtual void OnHideSelectionChanged(EventArgs e) {
826 EventHandler eh = (EventHandler)(Events [HideSelectionChangedEvent]);
827 if (eh != null)
828 eh (this, e);
831 protected virtual void OnModifiedChanged(EventArgs e) {
832 EventHandler eh = (EventHandler)(Events [ModifiedChangedEvent]);
833 if (eh != null)
834 eh (this, e);
837 protected virtual void OnMultilineChanged(EventArgs e) {
838 EventHandler eh = (EventHandler)(Events [MultilineChangedEvent]);
839 if (eh != null)
840 eh (this, e);
843 protected virtual void OnReadOnlyChanged(EventArgs e) {
844 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
845 if (eh != null)
846 eh (this, e);
849 protected override bool ProcessDialogKey(Keys keyData) {
850 return base.ProcessDialogKey(keyData);
853 private bool ProcessKey(Keys keyData) {
854 bool control;
855 bool shift;
857 control = (Control.ModifierKeys & Keys.Control) != 0;
858 shift = (Control.ModifierKeys & Keys.Shift) != 0;
860 switch (keyData & Keys.KeyCode) {
861 case Keys.X: { // Cut (Ctrl-X)
862 if (control) {
863 Cut();
864 return true;
866 return false;
869 case Keys.C: { // Copy (Ctrl-C)
870 if (control) {
871 Copy();
872 return true;
874 return false;
877 case Keys.V: { // Paste (Ctrl-V)
878 if (control) {
879 return Paste(Clipboard.GetDataObject(), null, true);
881 return false;
884 case Keys.Z: { // Undo (Ctrl-Z)
885 if (control) {
886 Undo();
887 return true;
889 return false;
892 case Keys.A: { // Select All (Ctrl-A)
893 if (control) {
894 SelectAll();
895 return true;
897 return false;
900 case Keys.Left: {
901 if (control) {
902 document.MoveCaret(CaretDirection.WordBack);
903 } else {
904 if (!document.selection_visible || shift) {
905 document.MoveCaret(CaretDirection.CharBack);
906 } else {
907 document.MoveCaret(CaretDirection.SelectionStart);
911 if (!shift) {
912 document.SetSelectionToCaret(true);
913 } else {
914 document.SetSelectionToCaret(false);
917 CaretMoved(this, null);
918 return true;
921 case Keys.Right: {
922 if (control) {
923 document.MoveCaret(CaretDirection.WordForward);
924 } else {
925 if (!document.selection_visible || shift) {
926 document.MoveCaret(CaretDirection.CharForward);
927 } else {
928 document.MoveCaret(CaretDirection.SelectionEnd);
931 if (!shift) {
932 document.SetSelectionToCaret(true);
933 } else {
934 document.SetSelectionToCaret(false);
937 CaretMoved(this, null);
938 return true;
941 case Keys.Up: {
942 if (control) {
943 if (document.CaretPosition == 0) {
944 document.MoveCaret(CaretDirection.LineUp);
945 } else {
946 document.MoveCaret(CaretDirection.Home);
948 } else {
949 document.MoveCaret(CaretDirection.LineUp);
952 if ((Control.ModifierKeys & Keys.Shift) == 0) {
953 document.SetSelectionToCaret(true);
954 } else {
955 document.SetSelectionToCaret(false);
958 CaretMoved(this, null);
959 return true;
962 case Keys.Down: {
963 if (control) {
964 if (document.CaretPosition == document.CaretLine.Text.Length) {
965 document.MoveCaret(CaretDirection.LineDown);
966 } else {
967 document.MoveCaret(CaretDirection.End);
969 } else {
970 document.MoveCaret(CaretDirection.LineDown);
973 if ((Control.ModifierKeys & Keys.Shift) == 0) {
974 document.SetSelectionToCaret(true);
975 } else {
976 document.SetSelectionToCaret(false);
979 CaretMoved(this, null);
980 return true;
983 case Keys.Home: {
984 if ((Control.ModifierKeys & Keys.Control) != 0) {
985 document.MoveCaret(CaretDirection.CtrlHome);
986 } else {
987 document.MoveCaret(CaretDirection.Home);
990 if ((Control.ModifierKeys & Keys.Shift) == 0) {
991 document.SetSelectionToCaret(true);
992 } else {
993 document.SetSelectionToCaret(false);
996 CaretMoved(this, null);
997 return true;
1000 case Keys.End: {
1001 if ((Control.ModifierKeys & Keys.Control) != 0) {
1002 document.MoveCaret(CaretDirection.CtrlEnd);
1003 } else {
1004 document.MoveCaret(CaretDirection.End);
1007 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1008 document.SetSelectionToCaret(true);
1009 } else {
1010 document.SetSelectionToCaret(false);
1013 CaretMoved(this, null);
1014 return true;
1017 case Keys.Enter: {
1018 // ignoring accepts_return, fixes bug #76355
1019 if (!read_only && multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
1020 Line line;
1022 if (document.selection_visible) {
1023 document.ReplaceSelection("", false);
1026 line = document.CaretLine;
1028 document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
1029 OnTextChanged(EventArgs.Empty);
1030 document.UpdateView(line, 2, 0);
1031 document.MoveCaret(CaretDirection.CharForward);
1032 document.SetSelectionToCaret(true);
1033 CaretMoved(this, null);
1034 return true;
1036 break;
1039 case Keys.Tab: {
1040 if (!read_only && accepts_tab && multiline) {
1041 document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
1042 if (document.selection_visible) {
1043 document.ReplaceSelection("", false);
1045 document.SetSelectionToCaret(true);
1047 OnTextChanged(EventArgs.Empty);
1048 CaretMoved(this, null);
1049 return true;
1051 break;
1054 case Keys.Insert: {
1055 if (shift) {
1056 Paste(Clipboard.GetDataObject(), null, true);
1057 return true;
1060 if (control) {
1061 Copy();
1062 return true;
1065 // FIXME - need overwrite/insert toggle?
1066 return false;
1069 case Keys.PageUp: {
1070 if ((Control.ModifierKeys & Keys.Control) != 0) {
1071 document.MoveCaret(CaretDirection.CtrlPgUp);
1072 } else {
1073 document.MoveCaret(CaretDirection.PgUp);
1075 return true;
1078 case Keys.PageDown: {
1079 if ((Control.ModifierKeys & Keys.Control) != 0) {
1080 document.MoveCaret(CaretDirection.CtrlPgDn);
1081 } else {
1082 document.MoveCaret(CaretDirection.PgDn);
1084 return true;
1087 case Keys.Delete: {
1088 if (shift) {
1089 Cut();
1090 return true;
1093 if (read_only) {
1094 break;
1097 if (document.selection_visible) {
1098 document.ReplaceSelection("", false);
1099 } else {
1100 // DeleteChar only deletes on the line, doesn't do the combine
1101 if (document.CaretPosition == document.CaretLine.Text.Length) {
1102 if (document.CaretLine.LineNo < document.Lines) {
1103 Line line;
1105 line = document.GetLine(document.CaretLine.LineNo + 1);
1106 document.Combine(document.CaretLine, line);
1107 document.UpdateView(document.CaretLine, 2, 0);
1109 #if not_Debug
1110 Line check_first;
1111 Line check_second;
1113 check_first = document.GetLine(document.CaretLine.LineNo);
1114 check_second = document.GetLine(check_first.line_no + 1);
1116 Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
1117 #endif
1119 // Caret doesn't move
1121 } else {
1122 if (!control) {
1123 document.DeleteChar(document.CaretTag, document.CaretPosition, true);
1124 } else {
1125 int end_pos;
1127 end_pos = document.CaretPosition;
1129 while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
1130 end_pos++;
1133 if (end_pos < document.CaretLine.Text.Length) {
1134 end_pos++;
1136 document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
1141 OnTextChanged(EventArgs.Empty);
1142 document.AlignCaret();
1143 document.UpdateCaret();
1144 CaretMoved(this, null);
1145 return true;
1148 return false;
1151 private void HandleBackspace(bool control) {
1152 bool fire_changed;
1154 fire_changed = false;
1156 // delete only deletes on the line, doesn't do the combine
1157 if (document.selection_visible) {
1158 document.ReplaceSelection("", false);
1159 fire_changed = true;
1161 document.SetSelectionToCaret(true);
1163 if (document.CaretPosition == 0) {
1164 if (document.CaretLine.LineNo > 1) {
1165 Line line;
1166 int new_caret_pos;
1168 line = document.GetLine(document.CaretLine.LineNo - 1);
1169 new_caret_pos = line.text.Length;
1171 document.Combine(line, document.CaretLine);
1172 document.UpdateView(line, 1, 0);
1173 document.PositionCaret(line, new_caret_pos);
1174 //document.MoveCaret(CaretDirection.CharForward);
1175 document.UpdateCaret();
1176 fire_changed = true;
1178 } else {
1179 if (!control || document.CaretPosition == 0) {
1180 document.DeleteChar(document.CaretTag, document.CaretPosition, false);
1181 document.MoveCaret(CaretDirection.CharBack);
1182 } else {
1183 int start_pos;
1185 start_pos = document.CaretPosition - 1;
1187 while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
1188 start_pos--;
1190 document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
1191 document.PositionCaret(document.CaretLine, start_pos);
1193 document.UpdateCaret();
1194 fire_changed = true;
1196 if (fire_changed) {
1197 OnTextChanged(EventArgs.Empty);
1199 CaretMoved(this, null);
1202 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1203 // Make sure we don't get sized bigger than we want to be
1204 if (!richtext) {
1205 if (!multiline) {
1206 if (height != PreferredHeight) {
1207 requested_height = height;
1208 height = PreferredHeight;
1209 specified |= BoundsSpecified.Height;
1214 base.SetBoundsCore (x, y, width, height, specified);
1217 protected override void WndProc(ref Message m) {
1218 switch ((Msg)m.Msg) {
1219 case Msg.WM_KEYDOWN: {
1220 if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
1221 m.Result = IntPtr.Zero;
1222 return;
1224 DefWndProc (ref m);
1225 return;
1228 case Msg.WM_CHAR: {
1229 int ch;
1231 if (ProcessKeyMessage(ref m)) {
1232 m.Result = IntPtr.Zero;
1233 return;
1236 if (read_only) {
1237 return;
1240 m.Result = IntPtr.Zero;
1242 ch = m.WParam.ToInt32();
1244 if (ch == 127) {
1245 HandleBackspace(true);
1246 } else if (ch >= 32) {
1247 if (document.selection_visible) {
1248 document.ReplaceSelection("", false);
1251 char c = (char)m.WParam;
1252 switch (character_casing) {
1253 case CharacterCasing.Upper:
1254 c = Char.ToUpper((char) m.WParam);
1255 break;
1256 case CharacterCasing.Lower:
1257 c = Char.ToLower((char) m.WParam);
1258 break;
1261 if (document.Length < max_length) {
1262 document.InsertCharAtCaret(c, true);
1263 OnTextChanged(EventArgs.Empty);
1264 CaretMoved(this, null);
1265 } else {
1266 XplatUI.AudibleAlert();
1268 return;
1269 } else if (ch == 8) {
1270 HandleBackspace(false);
1273 return;
1276 default: {
1277 base.WndProc(ref m);
1278 return;
1283 #endregion // Protected Instance Methods
1285 #region Events
1286 static object AcceptsTabChangedEvent = new object ();
1287 static object AutoSizeChangedEvent = new object ();
1288 static object BorderStyleChangedEvent = new object ();
1289 static object HideSelectionChangedEvent = new object ();
1290 static object ModifiedChangedEvent = new object ();
1291 static object MultilineChangedEvent = new object ();
1292 static object ReadOnlyChangedEvent = new object ();
1293 static object HScrolledEvent = new object ();
1294 static object VScrolledEvent = new object ();
1296 public event EventHandler AcceptsTabChanged {
1297 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
1298 remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
1301 public event EventHandler AutoSizeChanged {
1302 add { Events.AddHandler (AutoSizeChangedEvent, value); }
1303 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
1306 public event EventHandler BorderStyleChanged {
1307 add { Events.AddHandler (BorderStyleChangedEvent, value); }
1308 remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
1311 public event EventHandler HideSelectionChanged {
1312 add { Events.AddHandler (HideSelectionChangedEvent, value); }
1313 remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
1316 public event EventHandler ModifiedChanged {
1317 add { Events.AddHandler (ModifiedChangedEvent, value); }
1318 remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
1321 public event EventHandler MultilineChanged {
1322 add { Events.AddHandler (MultilineChangedEvent, value); }
1323 remove { Events.RemoveHandler (MultilineChangedEvent, value); }
1326 public event EventHandler ReadOnlyChanged {
1327 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
1328 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
1331 internal event EventHandler HScrolled {
1332 add { Events.AddHandler (HScrolledEvent, value); }
1333 remove { Events.RemoveHandler (HScrolledEvent, value); }
1336 internal event EventHandler VScrolled {
1337 add { Events.AddHandler (VScrolledEvent, value); }
1338 remove { Events.RemoveHandler (VScrolledEvent, value); }
1341 [Browsable(false)]
1342 [EditorBrowsable(EditorBrowsableState.Never)]
1343 public new event EventHandler BackgroundImageChanged {
1344 add { base.BackgroundImageChanged += value; }
1345 remove { base.BackgroundImageChanged -= value; }
1347 [Browsable(false)]
1348 [EditorBrowsable(EditorBrowsableState.Advanced)]
1349 public new event EventHandler Click {
1350 add { base.Click += value; }
1351 remove { base.Click -= value; }
1354 // XXX should this not manipulate base.Paint?
1355 [Browsable(false)]
1356 [EditorBrowsable(EditorBrowsableState.Never)]
1357 public new event PaintEventHandler Paint;
1358 #endregion // Events
1360 #region Private Methods
1361 internal Document Document {
1362 get {
1363 return document;
1366 set {
1367 document = value;
1371 internal bool ShowSelection {
1372 get {
1373 if (show_selection || !hide_selection) {
1374 return true;
1377 return has_focus;
1380 set {
1381 if (show_selection == value)
1382 return;
1384 show_selection = value;
1385 // Currently InvalidateSelectionArea is commented out so do a full invalidate
1386 document.InvalidateSelectionArea();
1390 internal Graphics CreateGraphicsInternal() {
1391 if (IsHandleCreated) {
1392 return base.CreateGraphics();
1395 return Graphics.FromImage(bmp);
1398 #if Debug
1399 static int current;
1400 #endif
1402 internal override void OnPaintInternal (PaintEventArgs pevent) {
1403 // Fill background
1404 if (backcolor_set || (Enabled && !read_only)) {
1405 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
1406 } else {
1407 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
1409 pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
1411 // Draw the viewable document
1412 document.Draw(pevent.Graphics, pevent.ClipRectangle);
1414 Rectangle rect = ClientRectangle;
1415 rect.Width--;
1416 rect.Height--;
1417 //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
1419 #if Debug
1420 int start;
1421 int end;
1422 Line line;
1423 int line_no;
1424 Pen p;
1426 p = new Pen(Color.Red, 1);
1428 // First, figure out from what line to what line we need to draw
1429 start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
1430 end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
1432 //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
1433 //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
1435 line_no = start;
1436 while (line_no <= end) {
1437 line = document.GetLine(line_no);
1439 if (draw_lines) {
1440 for (int i = 0; i < line.text.Length; i++) {
1441 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);
1445 line_no++;
1447 #endif
1450 internal override void OnGotFocusInternal (EventArgs e)
1452 document.CaretHasFocus ();
1453 base.OnGotFocusInternal (e);
1456 internal override void OnLostFocusInternal (EventArgs e)
1458 document.CaretLostFocus ();
1459 base.OnLostFocusInternal (e);
1462 private bool IsDoubleClick (MouseEventArgs e)
1464 TimeSpan interval = DateTime.Now - click_last;
1465 if (interval.TotalMilliseconds > SystemInformation.DoubleClickTime)
1466 return false;
1467 Size dcs = SystemInformation.DoubleClickSize;
1468 if (e.X < click_point_x - dcs.Width / 2 || e.X > click_point_x + dcs.Width / 2)
1469 return false;
1470 if (e.Y < click_point_y - dcs.Height / 2 || e.Y > click_point_y + dcs.Height / 2)
1471 return false;
1472 return true;
1475 private void TextBoxBase_MouseDown (object sender, MouseEventArgs e)
1477 if (e.Button == MouseButtons.Left) {
1479 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1481 if (IsDoubleClick (e)) {
1482 switch (click_mode) {
1483 case CaretSelection.Position:
1484 SelectWord ();
1485 click_mode = CaretSelection.Word;
1486 break;
1487 case CaretSelection.Word:
1489 if (this is TextBox) {
1490 document.SetSelectionToCaret (true);
1491 click_mode = CaretSelection.Position;
1492 break;
1495 document.ExpandSelection (CaretSelection.Line, false);
1496 click_mode = CaretSelection.Line;
1497 break;
1498 case CaretSelection.Line:
1500 // Gotta do this first because Exanding to a word
1501 // from a line doesn't really work
1502 document.SetSelectionToCaret (true);
1504 SelectWord ();
1505 click_mode = CaretSelection.Word;
1506 break;
1508 } else {
1509 document.SetSelectionToCaret (true);
1510 click_mode = CaretSelection.Position;
1513 click_point_x = e.X;
1514 click_point_y = e.Y;
1515 click_last = DateTime.Now;
1518 if ((e.Button == MouseButtons.Middle) && (((int)Environment.OSVersion.Platform == 4) || ((int)Environment.OSVersion.Platform == 128))) {
1519 Document.Marker marker;
1521 marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
1522 marker.line = marker.tag.line;
1523 marker.height = marker.tag.height;
1525 document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
1526 Paste (Clipboard.GetDataObject (true), null, true);
1531 private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
1532 if (e.Button == MouseButtons.Left) {
1533 if (click_mode == CaretSelection.Position) {
1534 document.SetSelectionToCaret(false);
1535 document.DisplayCaret();
1538 if (scroll_timer != null) {
1539 scroll_timer.Enabled = false;
1541 return;
1545 private void PositionControls ()
1547 if (hscroll.Visible) {
1548 //vscroll.Maximum += hscroll.Height;
1549 canvas_height = ClientSize.Height - hscroll.Height;
1550 } else {
1551 canvas_height = ClientSize.Height;
1554 if (vscroll.Visible) {
1555 //hscroll.Maximum += vscroll.Width;
1556 canvas_width = ClientSize.Width - vscroll.Width;
1557 } else {
1558 canvas_width = ClientSize.Width;
1562 document.ViewPortWidth = canvas_width;
1563 document.ViewPortHeight = canvas_height;
1565 if (canvas_height < 1 || canvas_width < 1)
1566 return;
1568 // We always move them, they just might not be displayed
1569 hscroll.Bounds = new Rectangle (ClientRectangle.Left,
1570 Math.Max (0, ClientRectangle.Height - hscroll.Height),
1571 Math.Max (0, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0)),
1572 hscroll.Height);
1574 vscroll.Bounds = new Rectangle (Math.Max (0, ClientRectangle.Right - vscroll.Width),
1575 ClientRectangle.Top, vscroll.Width,
1576 Math.Max (0, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0)));
1580 private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
1581 CalculateDocument();
1584 private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
1586 if (!vscroll.Enabled) {
1587 return;
1590 if (e.Delta < 0)
1591 vscroll.Value = Math.Min (vscroll.Value + SystemInformation.MouseWheelScrollLines,
1592 Math.Max (0, vscroll.Maximum - document.ViewPortHeight + 1));
1593 else
1594 vscroll.Value = Math.Max (0, vscroll.Value - SystemInformation.MouseWheelScrollLines);
1597 internal virtual void SelectWord ()
1599 StringBuilder s = document.caret.line.text;
1600 int start = document.caret.pos;
1601 int end = document.caret.pos;
1603 if (s.Length < 1) {
1604 if (document.caret.line.line_no >= document.Lines)
1605 return;
1606 Line line = document.GetLine (document.caret.line.line_no + 1);
1607 document.PositionCaret (line, 0);
1608 return;
1611 if (start > 0) {
1612 start--;
1613 end--;
1616 // skip whitespace until we hit a word
1617 while (start > 0 && s [start] == ' ')
1618 start--;
1619 if (start > 0) {
1620 while (start > 0 && (s [start] != ' '))
1621 start--;
1622 if (s [start] == ' ')
1623 start++;
1626 if (s [end] == ' ') {
1627 while (end < s.Length && s [end] == ' ')
1628 end++;
1629 } else {
1630 while (end < s.Length && s [end] != ' ')
1631 end++;
1632 while (end < s.Length && s [end] == ' ')
1633 end++;
1636 document.SetSelection (document.caret.line, start, document.caret.line, end);
1637 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
1640 internal void CalculateDocument() {
1641 if (!IsHandleCreated) {
1642 return;
1644 document.RecalculateDocument(CreateGraphicsInternal());
1645 CalculateScrollBars();
1647 if (document.caret.line != null && document.caret.line.Y < document.ViewPortHeight) {
1648 // The window has probably been resized, making the entire thing visible, so
1649 // we need to set the scroll position back to zero.
1650 vscroll.Value = 0;
1653 Invalidate();
1656 internal void CalculateScrollBars () {
1657 // FIXME - need separate calculations for center and right alignment
1659 if (!multiline) {
1660 PositionControls ();
1661 return;
1664 if (document.Width >= document.ViewPortWidth) {
1665 hscroll.SetValues (0, Math.Max (1, document.Width), -1,
1666 document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth);
1667 hscroll.Enabled = true;
1668 } else {
1669 hscroll.Enabled = false;
1670 hscroll.Maximum = document.ViewPortWidth;
1673 if (document.Height >= document.ViewPortHeight) {
1674 vscroll.SetValues (0, Math.Max (1, document.Height), -1,
1675 document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight);
1676 vscroll.Enabled = true;
1677 } else {
1678 vscroll.Enabled = false;
1679 vscroll.Maximum = document.ViewPortHeight;
1683 if (!WordWrap) {
1684 if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
1685 if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
1686 hscroll.Visible = true;
1687 } else {
1688 hscroll.Visible = false;
1690 } else {
1691 hscroll.Visible = false;
1695 if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
1696 if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
1697 vscroll.Visible = true;
1698 } else {
1699 vscroll.Visible = false;
1701 } else {
1702 vscroll.Visible = false;
1705 PositionControls ();
1708 private void document_WidthChanged(object sender, EventArgs e) {
1709 CalculateScrollBars();
1712 private void document_HeightChanged(object sender, EventArgs e) {
1713 CalculateScrollBars();
1716 private void hscroll_ValueChanged(object sender, EventArgs e) {
1717 int old_viewport_x;
1719 old_viewport_x = document.ViewPortX;
1720 document.ViewPortX = this.hscroll.Value;
1722 if (vscroll.Visible) {
1723 XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
1724 } else {
1725 XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
1727 document.UpdateCaret();
1729 EventHandler eh = (EventHandler)(Events [HScrolledEvent]);
1730 if (eh != null)
1731 eh (this, EventArgs.Empty);
1734 private void vscroll_ValueChanged(object sender, EventArgs e) {
1735 int old_viewport_y;
1737 old_viewport_y = document.ViewPortY;
1738 document.ViewPortY = this.vscroll.Value;
1740 if (hscroll.Visible) {
1741 XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
1742 } else {
1743 XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
1745 document.UpdateCaret();
1747 EventHandler eh = (EventHandler)(Events [VScrolledEvent]);
1748 if (eh != null)
1749 eh (this, EventArgs.Empty);
1752 private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
1753 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
1754 if (e.Button == MouseButtons.Left && Capture) {
1755 if (!ClientRectangle.Contains (e.X, e.Y)) {
1756 if (scroll_timer == null) {
1757 scroll_timer = new Timer ();
1758 scroll_timer.Interval = 100;
1759 scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
1762 if (!scroll_timer.Enabled) {
1763 scroll_timer.Start ();
1765 // Force the first tick
1766 ScrollTimerTickHandler (null, EventArgs.Empty);
1769 return;
1772 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1773 if (click_mode == CaretSelection.Position) {
1774 document.SetSelectionToCaret(false);
1775 document.DisplayCaret();
1780 private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
1781 if (!richtext) {
1782 Line line;
1784 document.NoRecalc = true;
1785 // Font changes apply to the whole document
1786 for (int i = 1; i <= document.Lines; i++) {
1787 line = document.GetLine(i);
1788 LineTag.FormatText(line, 1, line.text.Length, Font,
1789 ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor),
1790 null, FormatSpecified.Font | FormatSpecified.Color);
1792 document.UpdateView (document.GetLine (1), 0);
1793 document.NoRecalc = false;
1794 // Make sure the caret height is matching the new font height
1795 document.AlignCaret();
1799 private void ScrollTimerTickHandler (object sender, EventArgs e)
1801 Point pt = Cursor.Position;
1803 pt = PointToClient (pt);
1805 if (pt.X < ClientRectangle.Left) {
1806 document.MoveCaret(CaretDirection.CharBackNoWrap);
1807 document.SetSelectionToCaret(false);
1809 CaretMoved(this, null);
1810 } else if (pt.X > ClientRectangle.Right) {
1811 document.MoveCaret(CaretDirection.CharForwardNoWrap);
1812 document.SetSelectionToCaret(false);
1814 CaretMoved(this, null);
1815 } else if (pt.Y > ClientRectangle.Bottom) {
1816 document.MoveCaret(CaretDirection.LineDown);
1817 document.SetSelectionToCaret(false);
1819 CaretMoved(this, null);
1820 } else if (pt.Y < ClientRectangle.Top) {
1821 document.MoveCaret(CaretDirection.LineUp);
1822 document.SetSelectionToCaret(false);
1824 CaretMoved(this, null);
1828 /// <summary>Ensure the caret is always visible</summary>
1829 internal void CaretMoved(object sender, EventArgs e) {
1830 Point pos;
1831 int height;
1833 if (canvas_width < 1 || canvas_height < 1)
1834 return;
1836 pos = document.Caret;
1838 //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);
1841 // Horizontal scrolling:
1842 // If the caret moves to the left outside the visible area, we jump the document into view, not just one
1843 // character, but 1/3 of the width of the document
1844 // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
1846 // Handle horizontal scrolling
1847 if (document.CaretLine.alignment == HorizontalAlignment.Left) {
1848 // Check if we moved out of view to the left
1849 if (pos.X < (document.ViewPortX)) {
1850 do {
1851 if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
1852 hscroll.Value -= document.ViewPortWidth / 3;
1853 } else {
1854 hscroll.Value = hscroll.Minimum;
1856 } while (hscroll.Value > pos.X);
1859 // Check if we moved out of view to the right
1860 if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
1861 if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
1862 if (pos.X - document.ViewPortWidth >= 0) {
1863 hscroll.Value = pos.X - document.ViewPortWidth + 1;
1864 } else {
1865 hscroll.Value = 0;
1867 } else {
1868 hscroll.Value = hscroll.Maximum;
1871 } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
1872 // hscroll.Value = pos.X;
1874 // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
1875 // hscroll.Value = hscroll.Maximum;
1876 // }
1877 } else {
1878 // FIXME - implement center cursor alignment
1881 if (!multiline) {
1882 return;
1885 // Handle vertical scrolling
1886 height = document.CaretLine.Height + 1;
1888 if (pos.Y < document.ViewPortY) {
1889 vscroll.Value = pos.Y;
1892 if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
1893 vscroll.Value = Math.Max (0, pos.Y - canvas_height + height);
1897 internal bool Paste(IDataObject clip, DataFormats.Format format, bool obey_length) {
1898 string s;
1900 if (clip == null)
1901 return false;
1903 if (format == null) {
1904 if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
1905 format = DataFormats.GetFormat(DataFormats.Rtf);
1906 } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
1907 format = DataFormats.GetFormat(DataFormats.UnicodeText);
1908 } else if (clip.GetDataPresent(DataFormats.Text)) {
1909 format = DataFormats.GetFormat(DataFormats.Text);
1910 } else {
1911 return false;
1913 } else {
1914 if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
1915 return false;
1918 if (!clip.GetDataPresent(format.Name)) {
1919 return false;
1923 if (format.Name == DataFormats.Rtf) {
1924 ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
1925 return true;
1926 } else if (format.Name == DataFormats.UnicodeText) {
1927 s = (string)clip.GetData(DataFormats.UnicodeText);
1928 } else if (format.Name == DataFormats.Text) {
1929 s = (string)clip.GetData(DataFormats.Text);
1930 } else {
1931 return false;
1934 if (!obey_length) {
1935 this.SelectedText = s;
1936 } else {
1937 if ((s.Length + document.Length) < max_length) {
1938 this.SelectedText = s;
1939 } else if (document.Length < max_length) {
1940 this.SelectedText = s.Substring(0, max_length - document.Length);
1944 return true;
1946 #endregion // Private Methods
1948 #if NET_2_0
1949 protected override void OnTextChanged (EventArgs e)
1951 base.OnTextChanged (e);
1953 #endif