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:c
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)
23 // Peter Bartok pbartok@novell.com
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
34 using System.Drawing.Text;
36 using System.Runtime.InteropServices;
37 using System.Collections;
39 namespace System.Windows.Forms {
42 [DefaultBindingProperty ("Text")]
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
45 [DefaultEvent("TextChanged")]
46 [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
47 public abstract class TextBoxBase : Control {
48 #region Local Variables
49 internal HorizontalAlignment alignment;
50 internal bool accepts_tab;
51 internal bool accepts_return;
52 internal bool auto_size;
53 internal bool backcolor_set;
54 internal CharacterCasing character_casing;
55 internal bool hide_selection;
56 internal int max_length;
57 internal bool modified;
58 internal char password_char;
59 internal bool read_only;
60 internal bool word_wrap;
61 internal Document document;
62 internal LineTag caret_tag; // tag our cursor is in
63 internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
64 internal ImplicitHScrollBar hscroll;
65 internal ImplicitVScrollBar vscroll;
66 internal RichTextBoxScrollBars scrollbars;
67 internal Timer scroll_timer;
68 internal bool richtext;
69 internal bool show_selection; // set to true to always show selection, even if no focus is set
71 internal bool has_been_focused;
73 internal int selection_length = -1; // set to the user-specified selection length, or -1 if none
74 internal bool show_caret_w_selection; // TextBox shows the caret when the selection is visible
75 internal int canvas_width;
76 internal int canvas_height;
77 static internal int track_width = 2; //
78 static internal int track_border = 5; //
79 internal DateTime click_last;
80 internal int click_point_x;
81 internal int click_point_y;
82 internal CaretSelection click_mode;
84 internal BorderStyle actual_border_style;
85 internal bool shortcuts_enabled = true;
87 internal static bool draw_lines = false;
90 #endregion // Local Variables
92 #region Internal Constructor
93 // Constructor will go when complete, only for testing - pdb
94 internal TextBoxBase() {
95 alignment = HorizontalAlignment.Left;
96 accepts_return = false;
99 InternalBorderStyle = BorderStyle.Fixed3D;
100 actual_border_style = BorderStyle.Fixed3D;
101 character_casing = CharacterCasing.Normal;
102 hide_selection = true;
104 password_char = '\0';
108 show_selection = false;
109 show_caret_w_selection = (this is TextBox);
110 document = new Document(this);
111 document.WidthChanged += new EventHandler(document_WidthChanged);
112 document.HeightChanged += new EventHandler(document_HeightChanged);
113 //document.CaretMoved += new EventHandler(CaretMoved);
114 document.Wrap = false;
115 click_last = DateTime.Now;
116 click_mode = CaretSelection.Position;
117 bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
119 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
120 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
121 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
122 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
123 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
124 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
125 MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
127 scrollbars = RichTextBoxScrollBars.None;
129 hscroll = new ImplicitHScrollBar();
130 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
131 hscroll.SetStyle (ControlStyles.Selectable, false);
132 hscroll.Enabled = false;
133 hscroll.Visible = false;
134 hscroll.Maximum = Int32.MaxValue;
136 vscroll = new ImplicitVScrollBar();
137 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
138 vscroll.SetStyle (ControlStyles.Selectable, false);
139 vscroll.Enabled = false;
140 vscroll.Visible = false;
141 vscroll.Maximum = Int32.MaxValue;
144 this.Controls.AddImplicit (hscroll);
145 this.Controls.AddImplicit (vscroll);
148 SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
150 SetStyle(ControlStyles.UseTextForAccessibility, false);
152 base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
155 canvas_width = ClientSize.Width;
156 canvas_height = ClientSize.Height;
157 document.ViewPortWidth = canvas_width;
158 document.ViewPortHeight = canvas_height;
160 Cursor = Cursors.IBeam;
162 #endregion // Internal Constructor
164 #region Private and Internal Methods
165 internal string CaseAdjust(string s) {
166 if (character_casing == CharacterCasing.Normal) {
169 if (character_casing == CharacterCasing.Lower) {
177 internal override Size GetPreferredSizeCore (Size proposedSize)
179 return new Size (Width, Height);
183 internal override void HandleClick(int clicks, MouseEventArgs me) {
184 // MS seems to fire the click event in spite of the styles they set
185 bool click_set = GetStyle (ControlStyles.StandardClick);
186 bool doubleclick_set = GetStyle (ControlStyles.StandardDoubleClick);
188 // so explicitly set them to true first
189 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
191 base.HandleClick (clicks, me);
193 // then revert to our previous state
195 SetStyle (ControlStyles.StandardClick, false);
196 if (!doubleclick_set)
197 SetStyle (ControlStyles.StandardDoubleClick, false);
200 #endregion // Private and Internal Methods
202 #region Public Instance Properties
203 [DefaultValue(false)]
204 [MWFCategory("Behavior")]
205 public bool AcceptsTab {
211 if (value != accepts_tab) {
213 OnAcceptsTabChanged(EventArgs.Empty);
220 [EditorBrowsable (EditorBrowsableState.Never)]
224 [RefreshProperties(RefreshProperties.Repaint)]
225 [MWFCategory("Behavior")]
238 if (value != auto_size) {
241 if (PreferredHeight != ClientSize.Height) {
242 ClientSize = new Size(ClientSize.Width, PreferredHeight);
246 OnAutoSizeChanged(EventArgs.Empty);
253 public override System.Drawing.Color BackColor {
255 return base.BackColor;
258 backcolor_set = true;
259 base.BackColor = ChangeBackColor (value);
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 public override System.Drawing.Image BackgroundImage {
267 return base.BackgroundImage;
270 base.BackgroundImage = value;
274 [DefaultValue(BorderStyle.Fixed3D)]
276 [MWFCategory("Appearance")]
277 public BorderStyle BorderStyle {
278 get { return actual_border_style; }
280 if (value == actual_border_style)
283 if (actual_border_style != BorderStyle.Fixed3D || value != BorderStyle.Fixed3D)
286 actual_border_style = value;
287 document.UpdateMargins ();
289 if (value != BorderStyle.Fixed3D)
290 value = BorderStyle.None;
292 InternalBorderStyle = value;
293 OnBorderStyleChanged(EventArgs.Empty);
298 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
299 public bool CanUndo {
301 return document.undo.CanUndo;
306 public override System.Drawing.Color ForeColor {
308 return base.ForeColor;
311 base.ForeColor = value;
316 [MWFCategory("Behavior")]
317 public bool HideSelection {
319 return hide_selection;
323 if (value != hide_selection) {
324 hide_selection = value;
325 OnHideSelectionChanged(EventArgs.Empty);
327 if (hide_selection) {
328 document.selection_visible = false;
330 document.selection_visible = true;
332 document.InvalidateSelectionArea();
338 [MergableProperty (false)]
340 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
341 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
343 [MWFCategory("Appearance")]
344 public string[] Lines {
349 count = document.Lines;
351 // Handle empty document
352 if ((count == 1) && (document.GetLine (1).text.Length == 0)) {
353 return new string [0];
356 lines = new ArrayList ();
361 StringBuilder lt = new StringBuilder ();
364 line = document.GetLine (i++);
365 lt.Append (line.TextWithoutEnding ());
366 } while (line.ending == LineEnding.Wrap && i <= count);
368 lines.Add (lt.ToString ());
371 return (string []) lines.ToArray (typeof (string));
382 brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
384 document.SuspendRecalc ();
385 for (i = 0; i < l; i++) {
387 // Don't add the last line if it is just an empty line feed
388 // the line feed is reflected in the previous line's ending
389 if (i == l - 1 && value [i].Length == 0)
392 LineEnding ending = LineEnding.Rich;
393 if (value [i].EndsWith ("\r"))
394 ending = LineEnding.Hard;
396 document.Add (i + 1, CaseAdjust (value [i]), alignment, Font, brush, ending);
399 document.ResumeRecalc (true);
402 CalculateDocument ();
404 OnTextChanged(EventArgs.Empty);
408 [DefaultValue(32767)]
410 [MWFCategory("Behavior")]
411 public virtual int MaxLength {
413 if (max_length == 2147483646) { // We don't distinguish between single and multi-line limits
420 if (value != max_length) {
427 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
428 public bool Modified {
434 if (value != modified) {
436 OnModifiedChanged(EventArgs.Empty);
441 [DefaultValue(false)]
443 [RefreshProperties(RefreshProperties.All)]
444 [MWFCategory("Behavior")]
445 public virtual bool Multiline {
447 return document.multiline;
451 if (value != document.multiline) {
452 document.multiline = value;
454 // SetBoundsCore overrides the Height for multiline if it needs to,
455 // so we don't need to worry about it here.
456 SetBoundsCore (Left, Top, Width, ExplicitBounds.Height, BoundsSpecified.None);
459 Parent.PerformLayout ();
461 OnMultilineChanged(EventArgs.Empty);
464 if (document.multiline) {
465 document.Wrap = word_wrap;
466 document.PasswordChar = "";
469 document.Wrap = false;
470 if (this.password_char != '\0') {
471 document.PasswordChar = password_char.ToString();
473 document.PasswordChar = "";
478 CalculateDocument ();
483 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
484 [EditorBrowsable(EditorBrowsableState.Advanced)]
485 public int PreferredHeight {
487 return Font.Height + (BorderStyle == BorderStyle.None ? 0 : 7);
492 [RefreshProperties (RefreshProperties.Repaint)]
494 [DefaultValue(false)]
495 [MWFCategory("Behavior")]
496 public bool ReadOnly {
502 if (value != read_only) {
505 if (!backcolor_set) {
507 background_color = SystemColors.Control;
509 background_color = SystemColors.Window;
512 OnReadOnlyChanged(EventArgs.Empty);
519 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
520 public virtual string SelectedText {
522 string retval = document.GetSelection ();
525 if (!IsHandleCreated && retval == string.Empty)
532 document.ReplaceSelection(CaseAdjust(value), false);
535 OnTextChanged(EventArgs.Empty);
540 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
541 public virtual int SelectionLength {
543 int res = document.SelectionLength ();
546 if (res == 0 && !IsHandleCreated)
555 throw new ArgumentException(String.Format("{0} is not a valid value", value), "value");
564 selection_length = value;
566 start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
568 document.CharIndexToLineTag(start + value, out line, out tag, out pos);
569 document.SetSelectionEnd(line, pos, true);
570 document.PositionCaret(line, pos);
572 selection_length = -1;
574 document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos, true);
575 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
581 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
582 public int SelectionStart {
586 index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
592 document.SetSelectionStart(value, false);
593 if (selection_length > -1 ) {
594 document.SetSelectionEnd(value + selection_length, true);
596 document.SetSelectionEnd(value, true);
598 document.PositionCaret(document.selection_start.line, document.selection_start.pos);
604 [DefaultValue (true)]
605 public virtual bool ShortcutsEnabled {
606 get { return shortcuts_enabled; }
607 set { shortcuts_enabled = value; }
610 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
611 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
614 public override string Text {
616 if (document == null || document.Root == null || document.Root.text == null) {
620 StringBuilder sb = new StringBuilder();
623 for (int i = 1; i <= document.Lines; i++) {
624 line = document.GetLine (i);
625 sb.Append(line.text.ToString ());
628 return sb.ToString();
632 // reset to force a select all next time the box gets focus
633 has_been_focused = false;
638 if ((value != null) && (value != "")) {
642 document.Insert (document.GetLine (1), 0, false, value);
644 document.PositionCaret (document.GetLine (1), 0);
645 document.SetSelectionToCaret (true);
651 CalculateDocument ();
654 // set the var so OnModifiedChanged is not raised
656 OnTextChanged(EventArgs.Empty);
661 public virtual int TextLength {
663 if (document == null || document.Root == null || document.Root.text == null) {
672 [MWFCategory("Behavior")]
673 public bool WordWrap {
679 if (value != word_wrap) {
680 if (document.multiline) {
682 document.Wrap = value;
684 CalculateDocument ();
690 [EditorBrowsable (EditorBrowsableState.Never)]
691 public override ImageLayout BackgroundImageLayout {
692 get { return base.BackgroundImageLayout; }
693 set { base.BackgroundImageLayout = value; }
697 [EditorBrowsable (EditorBrowsableState.Never)]
698 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
699 public new Padding Padding {
700 get { return base.Padding; }
701 set { base.Padding = value; }
704 protected override Cursor DefaultCursor {
705 get { return Cursors.IBeam; }
708 #endregion // Public Instance Properties
710 #region Protected Instance Properties
711 protected override CreateParams CreateParams {
713 return base.CreateParams;
717 protected override System.Drawing.Size DefaultSize {
719 return new Size(100, 20);
724 // Currently our double buffering breaks our scrolling, so don't let people enable this
725 [EditorBrowsable (EditorBrowsableState.Never)]
726 protected override bool DoubleBuffered {
727 get { return false; }
732 #endregion // Protected Instance Properties
734 #region Public Instance Methods
735 public void AppendText(string text)
737 // Save some cycles and only check the Text if we are one line
738 bool is_empty = document.Lines == 1 && Text == String.Empty;
740 document.MoveCaret (CaretDirection.CtrlEnd);
741 document.Insert (document.caret.line, document.caret.pos, false, text);
742 document.MoveCaret (CaretDirection.CtrlEnd);
743 document.SetSelectionToCaret (true);
749 // Avoid the initial focus selecting all when append text is used
751 has_been_focused = true;
753 OnTextChanged(EventArgs.Empty);
756 public void Clear() {
760 public void ClearUndo() {
761 document.undo.Clear();
767 o = new DataObject(DataFormats.Text, SelectedText);
768 if (this is RichTextBox) {
769 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
771 Clipboard.SetDataObject(o);
777 o = new DataObject(DataFormats.Text, SelectedText);
778 if (this is RichTextBox) {
779 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
781 Clipboard.SetDataObject (o);
783 document.undo.BeginUserAction (Locale.GetText ("Cut"));
784 document.ReplaceSelection (String.Empty, false);
785 document.undo.EndUserAction ();
787 OnTextChanged (EventArgs.Empty);
790 public void Paste() {
791 Paste(Clipboard.GetDataObject(), null, false);
794 public void ScrollToCaret() {
795 if (IsHandleCreated) {
796 CaretMoved(this, EventArgs.Empty);
800 public void Select(int start, int length) {
801 SelectionStart = start;
802 SelectionLength = length;
806 public void SelectAll() {
809 last = document.GetLine(document.Lines);
810 document.SetSelectionStart(document.GetLine(1), 0, false);
811 document.SetSelectionEnd(last, last.text.Length, true);
812 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
813 selection_length = -1;
815 CaretMoved (this, null);
817 document.DisplayCaret ();
820 internal void SelectAllNoScroll ()
824 last = document.GetLine(document.Lines);
825 document.SetSelectionStart(document.GetLine(1), 0, false);
826 document.SetSelectionEnd(last, last.text.Length, false);
827 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
828 selection_length = -1;
830 document.DisplayCaret ();
833 public override string ToString() {
834 return String.Concat (base.ToString (), ", Text: ", Text);
838 document.undo.Undo();
842 public void DeselectAll ()
847 public virtual char GetCharFromPosition (Point p)
849 return GetCharFromPositionInternal (p);
852 internal virtual char GetCharFromPositionInternal (Point p)
855 LineTag tag = document.FindCursor (p.X, p.Y, out index);
857 return (char) 0; // Shouldn't happen
859 if (index >= tag.line.text.Length) {
861 if (tag.line.ending == LineEnding.Wrap) {
862 // If we have wrapped text, we return the first char of the next line
863 Line line = document.GetLine (tag.line.line_no + 1);
865 return line.text [0];
869 if (tag.line.line_no == document.Lines) {
870 // Last line returns the last char
871 return tag.line.text [tag.line.text.Length - 1];
874 // This really shouldn't happen
877 return tag.line.text [index];
880 public virtual int GetCharIndexFromPosition (Point p)
883 LineTag tag = document.FindCursor (p.X, p.Y, out line_index);
887 if (line_index >= tag.line.text.Length) {
889 if (tag.line.ending == LineEnding.Wrap) {
890 // If we have wrapped text, we return the first char of the next line
891 Line line = document.GetLine (tag.line.line_no + 1);
893 return document.LineTagToCharIndex (line, 0);
896 if (tag.line.line_no == document.Lines) {
897 // Last line returns the last char
898 return document.LineTagToCharIndex (tag.line, tag.line.text.Length - 1);
904 return document.LineTagToCharIndex (tag.line, line_index);
907 public virtual Point GetPositionFromCharIndex (int index)
913 document.CharIndexToLineTag (index, out line, out tag, out pos);
915 return new Point ((int) (line.widths [pos] +
916 line.X + document.viewport_x),
917 line.Y + document.viewport_y + tag.shift);
920 public int GetFirstCharIndexFromLine (int line_number)
922 Line line = document.GetLine (line_number + 1);
926 return document.LineTagToCharIndex (line, 0);
929 public int GetFirstCharIndexOfCurrentLine ()
931 return document.LineTagToCharIndex (document.caret.line, 0);
934 #endregion // Public Instance Methods
936 #region Protected Instance Methods
937 protected override void CreateHandle() {
938 CalculateDocument ();
939 base.CreateHandle ();
940 document.AlignCaret();
944 protected override bool IsInputKey(Keys keyData) {
945 if ((keyData & Keys.Alt) != 0) {
946 return base.IsInputKey(keyData);
949 switch (keyData & Keys.KeyCode) {
951 if (accepts_return && document.multiline) {
958 if (accepts_tab && document.multiline) {
959 if ((keyData & Keys.Control) == 0) {
981 protected virtual void OnAcceptsTabChanged(EventArgs e) {
982 EventHandler eh = (EventHandler)(Events [AcceptsTabChangedEvent]);
987 protected virtual void OnAutoSizeChanged(EventArgs e) {
988 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
994 protected virtual void OnBorderStyleChanged(EventArgs e) {
995 EventHandler eh = (EventHandler)(Events [BorderStyleChangedEvent]);
1000 protected override void OnFontChanged(EventArgs e) {
1001 base.OnFontChanged (e);
1003 if (auto_size && !document.multiline) {
1004 if (PreferredHeight != ClientSize.Height) {
1005 Height = PreferredHeight;
1010 protected override void OnHandleCreated(EventArgs e) {
1011 base.OnHandleCreated (e);
1015 protected override void OnHandleDestroyed(EventArgs e) {
1016 base.OnHandleDestroyed (e);
1019 protected virtual void OnHideSelectionChanged(EventArgs e) {
1020 EventHandler eh = (EventHandler)(Events [HideSelectionChangedEvent]);
1025 protected virtual void OnModifiedChanged(EventArgs e) {
1026 EventHandler eh = (EventHandler)(Events [ModifiedChangedEvent]);
1031 protected virtual void OnMultilineChanged(EventArgs e) {
1032 EventHandler eh = (EventHandler)(Events [MultilineChangedEvent]);
1038 protected override void OnPaddingChanged (EventArgs e)
1040 base.OnPaddingChanged (e);
1044 protected virtual void OnReadOnlyChanged(EventArgs e) {
1045 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
1051 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
1053 return base.ProcessCmdKey (ref msg, keyData);
1056 protected override bool ProcessDialogKey(Keys keyData) {
1057 return base.ProcessDialogKey(keyData);
1060 private bool ProcessKey(Keys keyData) {
1064 control = (Control.ModifierKeys & Keys.Control) != 0;
1065 shift = (Control.ModifierKeys & Keys.Shift) != 0;
1067 if (shortcuts_enabled) {
1068 switch (keyData & Keys.KeyCode) {
1085 return Paste(Clipboard.GetDataObject(), null, true);
1105 Paste(Clipboard.GetDataObject(), null, true);
1125 if (document.selection_visible) {
1126 document.ReplaceSelection("", false);
1128 // DeleteChar only deletes on the line, doesn't do the combine
1129 if (document.CaretPosition >= document.CaretLine.TextLengthWithoutEnding ()) {
1130 if (document.CaretLine.LineNo < document.Lines) {
1133 line = document.GetLine(document.CaretLine.LineNo + 1);
1135 // this line needs to be invalidated before it is combined
1136 // because once it is combined, all it's coordinates will
1138 document.Invalidate (line, 0, line, line.text.Length);
1139 document.Combine(document.CaretLine, line);
1141 document.UpdateView(document.CaretLine,
1147 document.DeleteChar(document.CaretTag, document.CaretPosition, true);
1151 end_pos = document.CaretPosition;
1153 while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
1157 if (end_pos < document.CaretLine.Text.Length) {
1160 document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
1165 OnTextChanged(EventArgs.Empty);
1166 document.AlignCaret();
1167 document.UpdateCaret();
1168 CaretMoved(this, null);
1173 switch (keyData & Keys.KeyCode) {
1176 document.MoveCaret(CaretDirection.WordBack);
1178 if (!document.selection_visible || shift) {
1179 document.MoveCaret(CaretDirection.CharBack);
1181 document.MoveCaret(CaretDirection.SelectionStart);
1186 document.SetSelectionToCaret(true);
1188 document.SetSelectionToCaret(false);
1191 CaretMoved(this, null);
1197 document.MoveCaret(CaretDirection.WordForward);
1199 if (!document.selection_visible || shift) {
1200 document.MoveCaret(CaretDirection.CharForward);
1202 document.MoveCaret(CaretDirection.SelectionEnd);
1206 document.SetSelectionToCaret(true);
1208 document.SetSelectionToCaret(false);
1211 CaretMoved(this, null);
1217 if (document.CaretPosition == 0) {
1218 document.MoveCaret(CaretDirection.LineUp);
1220 document.MoveCaret(CaretDirection.Home);
1223 document.MoveCaret(CaretDirection.LineUp);
1226 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1227 document.SetSelectionToCaret(true);
1229 document.SetSelectionToCaret(false);
1232 CaretMoved(this, null);
1238 if (document.CaretPosition == document.CaretLine.Text.Length) {
1239 document.MoveCaret(CaretDirection.LineDown);
1241 document.MoveCaret(CaretDirection.End);
1244 document.MoveCaret(CaretDirection.LineDown);
1247 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1248 document.SetSelectionToCaret(true);
1250 document.SetSelectionToCaret(false);
1253 CaretMoved(this, null);
1258 if ((Control.ModifierKeys & Keys.Control) != 0) {
1259 document.MoveCaret(CaretDirection.CtrlHome);
1261 document.MoveCaret(CaretDirection.Home);
1264 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1265 document.SetSelectionToCaret(true);
1267 document.SetSelectionToCaret(false);
1270 CaretMoved(this, null);
1275 if ((Control.ModifierKeys & Keys.Control) != 0) {
1276 document.MoveCaret(CaretDirection.CtrlEnd);
1278 document.MoveCaret(CaretDirection.End);
1281 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1282 document.SetSelectionToCaret(true);
1284 document.SetSelectionToCaret(false);
1287 CaretMoved(this, null);
1292 // ignoring accepts_return, fixes bug #76355
1293 if (!read_only && document.multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
1296 if (document.selection_visible) {
1297 document.ReplaceSelection("\n", false);
1300 line = document.CaretLine;
1302 document.Split (document.CaretLine, document.CaretTag, document.CaretPosition);
1303 line.ending = LineEnding.Rich;
1304 document.InsertString (line, line.text.Length,
1305 document.LineEndingToString (line.ending));
1306 OnTextChanged(EventArgs.Empty);
1308 document.UpdateView (line, document.Lines - line.line_no, 0);
1309 CaretMoved(this, null);
1316 if (!read_only && accepts_tab && document.multiline) {
1317 document.InsertCharAtCaret ('\t', true);
1319 OnTextChanged(EventArgs.Empty);
1320 CaretMoved(this, null);
1327 if ((Control.ModifierKeys & Keys.Control) != 0) {
1328 document.MoveCaret(CaretDirection.CtrlPgUp);
1330 document.MoveCaret(CaretDirection.PgUp);
1332 document.DisplayCaret ();
1336 case Keys.PageDown: {
1337 if ((Control.ModifierKeys & Keys.Control) != 0) {
1338 document.MoveCaret(CaretDirection.CtrlPgDn);
1340 document.MoveCaret(CaretDirection.PgDn);
1342 document.DisplayCaret ();
1350 private void HandleBackspace(bool control) {
1353 fire_changed = false;
1355 // delete only deletes on the line, doesn't do the combine
1356 if (document.selection_visible) {
1357 document.undo.BeginUserAction (Locale.GetText ("Delete"));
1358 document.ReplaceSelection("", false);
1359 document.undo.EndUserAction ();
1360 fire_changed = true;
1362 document.SetSelectionToCaret(true);
1364 if (document.CaretPosition == 0) {
1365 if (document.CaretLine.LineNo > 1) {
1369 line = document.GetLine(document.CaretLine.LineNo - 1);
1370 new_caret_pos = line.TextLengthWithoutEnding ();
1372 // Invalidate the old line position before we do the combine
1373 document.Invalidate (line, 0, line, line.text.Length);
1374 document.Combine(line, document.CaretLine);
1376 document.UpdateView(line, document.Lines - line.LineNo, 0);
1377 document.PositionCaret(line, new_caret_pos);
1378 document.SetSelectionToCaret (true);
1379 document.UpdateCaret();
1380 fire_changed = true;
1383 if (!control || document.CaretPosition == 0) {
1385 // Move before we delete because the delete will change positions around
1386 // if we cross a wrap border
1387 LineTag tag = document.CaretTag;
1388 int pos = document.CaretPosition;
1389 document.MoveCaret (CaretDirection.CharBack);
1390 document.DeleteChar (tag, pos, false);
1391 document.SetSelectionToCaret (true);
1396 start_pos = document.CaretPosition - 1;
1397 while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
1401 document.undo.BeginUserAction (Locale.GetText ("Delete"));
1402 document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
1403 document.undo.EndUserAction ();
1404 document.PositionCaret(document.CaretLine, start_pos);
1405 document.SetSelectionToCaret (true);
1407 document.UpdateCaret();
1408 fire_changed = true;
1412 OnTextChanged(EventArgs.Empty);
1414 CaretMoved(this, null);
1417 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1418 // Make sure we don't get sized bigger than we want to be
1421 if (!document.multiline) {
1422 if (height != PreferredHeight) {
1423 // If the specified has Height, we need to store that in the
1424 // ExplicitBounds because we are going to override it
1425 if ((specified & BoundsSpecified.Height) != 0) {
1426 Rectangle r = ExplicitBounds;
1429 specified &= ~BoundsSpecified.Height;
1432 height = PreferredHeight;
1437 base.SetBoundsCore (x, y, width, height, specified);
1440 protected override void WndProc(ref Message m) {
1441 switch ((Msg)m.Msg) {
1442 case Msg.WM_KEYDOWN: {
1443 if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
1444 m.Result = IntPtr.Zero;
1454 if (ProcessKeyMessage(ref m)) {
1455 m.Result = IntPtr.Zero;
1463 m.Result = IntPtr.Zero;
1465 ch = m.WParam.ToInt32();
1468 HandleBackspace(true);
1469 } else if (ch >= 32) {
1470 if (document.selection_visible) {
1471 document.ReplaceSelection("", false);
1474 char c = (char)m.WParam;
1475 switch (character_casing) {
1476 case CharacterCasing.Upper:
1477 c = Char.ToUpper((char) m.WParam);
1479 case CharacterCasing.Lower:
1480 c = Char.ToLower((char) m.WParam);
1484 if (document.Length < max_length) {
1485 document.InsertCharAtCaret(c, true);
1489 OnTextChanged(EventArgs.Empty);
1490 CaretMoved(this, null);
1492 XplatUI.AudibleAlert();
1495 } else if (ch == 8) {
1496 HandleBackspace(false);
1502 case Msg.WM_SETFOCUS:
1503 base.WndProc(ref m);
1504 document.CaretHasFocus ();
1507 case Msg.WM_KILLFOCUS:
1508 base.WndProc(ref m);
1509 document.CaretLostFocus ();
1513 base.WndProc(ref m);
1518 #endregion // Protected Instance Methods
1521 static object AcceptsTabChangedEvent = new object ();
1522 static object AutoSizeChangedEvent = new object ();
1523 static object BorderStyleChangedEvent = new object ();
1524 static object HideSelectionChangedEvent = new object ();
1525 static object ModifiedChangedEvent = new object ();
1526 static object MultilineChangedEvent = new object ();
1527 static object ReadOnlyChangedEvent = new object ();
1528 static object HScrolledEvent = new object ();
1529 static object VScrolledEvent = new object ();
1531 public event EventHandler AcceptsTabChanged {
1532 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
1533 remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
1538 [EditorBrowsable (EditorBrowsableState.Never)]
1540 public new event EventHandler AutoSizeChanged {
1541 add { Events.AddHandler (AutoSizeChangedEvent, value); }
1542 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
1545 public event EventHandler BorderStyleChanged {
1546 add { Events.AddHandler (BorderStyleChangedEvent, value); }
1547 remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
1550 public event EventHandler HideSelectionChanged {
1551 add { Events.AddHandler (HideSelectionChangedEvent, value); }
1552 remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
1555 public event EventHandler ModifiedChanged {
1556 add { Events.AddHandler (ModifiedChangedEvent, value); }
1557 remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
1560 public event EventHandler MultilineChanged {
1561 add { Events.AddHandler (MultilineChangedEvent, value); }
1562 remove { Events.RemoveHandler (MultilineChangedEvent, value); }
1565 public event EventHandler ReadOnlyChanged {
1566 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
1567 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
1570 internal event EventHandler HScrolled {
1571 add { Events.AddHandler (HScrolledEvent, value); }
1572 remove { Events.RemoveHandler (HScrolledEvent, value); }
1575 internal event EventHandler VScrolled {
1576 add { Events.AddHandler (VScrolledEvent, value); }
1577 remove { Events.RemoveHandler (VScrolledEvent, value); }
1581 [EditorBrowsable(EditorBrowsableState.Never)]
1582 public new event EventHandler BackgroundImageChanged {
1583 add { base.BackgroundImageChanged += value; }
1584 remove { base.BackgroundImageChanged -= value; }
1589 [EditorBrowsable (EditorBrowsableState.Never)]
1590 public new event EventHandler BackgroundImageLayoutChanged {
1591 add { base.BackgroundImageLayoutChanged += value; }
1592 remove { base.BackgroundImageLayoutChanged -= value; }
1596 [EditorBrowsable (EditorBrowsableState.Always)]
1597 public new event MouseEventHandler MouseClick {
1598 add { base.MouseClick += value; }
1599 remove { base.MouseClick -= value; }
1603 [EditorBrowsable (EditorBrowsableState.Never)]
1604 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1605 public new event EventHandler PaddingChanged {
1606 add { base.PaddingChanged += value; }
1607 remove { base.PaddingChanged -= value; }
1611 [EditorBrowsable (EditorBrowsableState.Always)]
1614 [EditorBrowsable(EditorBrowsableState.Advanced)]
1616 public new event EventHandler Click {
1617 add { base.Click += value; }
1618 remove { base.Click -= value; }
1621 // XXX should this not manipulate base.Paint?
1623 [EditorBrowsable(EditorBrowsableState.Never)]
1624 public new event PaintEventHandler Paint;
1625 #endregion // Events
1627 #region Private Methods
1628 internal Document Document {
1638 internal bool ShowSelection {
1640 if (show_selection || !hide_selection) {
1648 if (show_selection == value)
1651 show_selection = value;
1652 // Currently InvalidateSelectionArea is commented out so do a full invalidate
1653 document.InvalidateSelectionArea();
1657 internal Graphics CreateGraphicsInternal() {
1658 if (IsHandleCreated) {
1659 return base.CreateGraphics();
1662 return Graphics.FromImage(bmp);
1665 internal override void OnPaintInternal (PaintEventArgs pevent) {
1667 if (backcolor_set || (Enabled && !read_only)) {
1668 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
1670 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
1673 // Draw the viewable document
1674 document.Draw(pevent.Graphics, pevent.ClipRectangle);
1677 // OnPaint does not get raised on MS (see bug #80639)
1679 pevent.Handled = true;
1682 private void FixupHeight ()
1685 if (!document.multiline) {
1686 if (PreferredHeight != ClientSize.Height) {
1687 ClientSize = new Size (ClientSize.Width, PreferredHeight);
1693 private bool IsDoubleClick (MouseEventArgs e)
1695 TimeSpan interval = DateTime.Now - click_last;
1696 if (interval.TotalMilliseconds > SystemInformation.DoubleClickTime)
1698 Size dcs = SystemInformation.DoubleClickSize;
1699 if (e.X < click_point_x - dcs.Width / 2 || e.X > click_point_x + dcs.Width / 2)
1701 if (e.Y < click_point_y - dcs.Height / 2 || e.Y > click_point_y + dcs.Height / 2)
1706 private void TextBoxBase_MouseDown (object sender, MouseEventArgs e)
1708 if (e.Button == MouseButtons.Left) {
1710 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1712 if (IsDoubleClick (e)) {
1713 switch (click_mode) {
1714 case CaretSelection.Position:
1716 click_mode = CaretSelection.Word;
1718 case CaretSelection.Word:
1720 if (this is TextBox) {
1721 document.SetSelectionToCaret (true);
1722 click_mode = CaretSelection.Position;
1726 document.ExpandSelection (CaretSelection.Line, false);
1727 click_mode = CaretSelection.Line;
1729 case CaretSelection.Line:
1731 // Gotta do this first because Exanding to a word
1732 // from a line doesn't really work
1733 document.SetSelectionToCaret (true);
1736 click_mode = CaretSelection.Word;
1740 document.SetSelectionToCaret (true);
1741 click_mode = CaretSelection.Position;
1744 click_point_x = e.X;
1745 click_point_y = e.Y;
1746 click_last = DateTime.Now;
1749 if ((e.Button == MouseButtons.Middle) && XplatUI.RunningOnUnix) {
1750 Document.Marker marker;
1752 marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
1753 marker.line = marker.tag.line;
1754 marker.height = marker.tag.height;
1756 document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
1757 Paste (Clipboard.GetDataObject (true), null, true);
1762 private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
1763 if (e.Button == MouseButtons.Left) {
1764 if (click_mode == CaretSelection.Position) {
1765 document.SetSelectionToCaret(false);
1766 document.DisplayCaret();
1769 if (scroll_timer != null) {
1770 scroll_timer.Enabled = false;
1776 private void PositionControls ()
1778 if (hscroll.Visible) {
1779 //vscroll.Maximum += hscroll.Height;
1780 canvas_height = ClientSize.Height - hscroll.Height;
1782 canvas_height = ClientSize.Height;
1785 if (vscroll.Visible) {
1786 //hscroll.Maximum += vscroll.Width;
1787 canvas_width = ClientSize.Width - vscroll.Width;
1789 canvas_width = ClientSize.Width;
1793 document.ViewPortWidth = canvas_width;
1794 document.ViewPortHeight = canvas_height;
1796 if (canvas_height < 1 || canvas_width < 1)
1799 // We always move them, they just might not be displayed
1800 hscroll.Bounds = new Rectangle (ClientRectangle.Left,
1801 Math.Max (0, ClientRectangle.Height - hscroll.Height),
1802 Math.Max (0, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0)),
1805 vscroll.Bounds = new Rectangle (Math.Max (0, ClientRectangle.Right - vscroll.Width),
1806 ClientRectangle.Top, vscroll.Width,
1807 Math.Max (0, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0)));
1811 private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
1812 if (IsHandleCreated)
1813 CalculateDocument ();
1816 private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
1818 if (!vscroll.Enabled) {
1823 vscroll.Value = Math.Min (vscroll.Value + SystemInformation.MouseWheelScrollLines * 5,
1824 Math.Max (0, vscroll.Maximum - document.ViewPortHeight + 1));
1826 vscroll.Value = Math.Max (0, vscroll.Value - SystemInformation.MouseWheelScrollLines * 5);
1829 internal virtual void SelectWord ()
1831 StringBuilder s = document.caret.line.text;
1832 int start = document.caret.pos;
1833 int end = document.caret.pos;
1836 if (document.caret.line.line_no >= document.Lines)
1838 Line line = document.GetLine (document.caret.line.line_no + 1);
1839 document.PositionCaret (line, 0);
1848 // skip whitespace until we hit a word
1849 while (start > 0 && s [start] == ' ')
1852 while (start > 0 && (s [start] != ' '))
1854 if (s [start] == ' ')
1858 if (s [end] == ' ') {
1859 while (end < s.Length && s [end] == ' ')
1862 while (end < s.Length && s [end] != ' ')
1864 while (end < s.Length && s [end] == ' ')
1868 document.SetSelection (document.caret.line, start, document.caret.line, end);
1869 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
1870 document.DisplayCaret();
1873 internal void CalculateDocument()
1875 CalculateScrollBars ();
1876 document.RecalculateDocument (CreateGraphicsInternal ());
1879 if (document.caret.line != null && document.caret.line.Y < document.ViewPortHeight) {
1880 // The window has probably been resized, making the entire thing visible, so
1881 // we need to set the scroll position back to zero.
1888 internal void CalculateScrollBars () {
1889 // FIXME - need separate calculations for center and right alignment
1893 if (document.Width >= document.ViewPortWidth) {
1894 hscroll.SetValues (0, Math.Max (1, document.Width), -1,
1895 document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth);
1896 if (document.multiline)
1897 hscroll.Enabled = true;
1899 hscroll.Enabled = false;
1900 hscroll.Maximum = document.ViewPortWidth;
1903 if (document.Height >= document.ViewPortHeight) {
1904 vscroll.SetValues (0, Math.Max (1, document.Height), -1,
1905 document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight);
1906 if (document.multiline)
1907 vscroll.Enabled = true;
1909 vscroll.Enabled = false;
1910 vscroll.Maximum = document.ViewPortHeight;
1914 switch (scrollbars) {
1915 case RichTextBoxScrollBars.Both:
1916 case RichTextBoxScrollBars.Horizontal:
1917 hscroll.Visible = hscroll.Enabled;
1919 case RichTextBoxScrollBars.ForcedBoth:
1920 case RichTextBoxScrollBars.ForcedHorizontal:
1921 hscroll.Visible = true;
1924 hscroll.Visible = false;
1928 hscroll.Visible = false;
1931 switch (scrollbars) {
1932 case RichTextBoxScrollBars.Both:
1933 case RichTextBoxScrollBars.Vertical:
1934 vscroll.Visible = vscroll.Enabled;
1936 case RichTextBoxScrollBars.ForcedBoth:
1937 case RichTextBoxScrollBars.ForcedVertical:
1938 vscroll.Visible = true;
1941 vscroll.Visible = false;
1945 PositionControls ();
1948 private void document_WidthChanged(object sender, EventArgs e) {
1949 CalculateScrollBars();
1952 private void document_HeightChanged(object sender, EventArgs e) {
1953 CalculateScrollBars();
1956 private void hscroll_ValueChanged(object sender, EventArgs e) {
1959 old_viewport_x = document.ViewPortX;
1960 document.ViewPortX = this.hscroll.Value;
1963 // Before scrolling we want to destroy the caret, then draw a new one after the scroll
1964 // the reason for this is that scrolling changes the coordinates of the caret, and we
1965 // will get tracers if we don't
1968 document.CaretLostFocus ();
1970 if (vscroll.Visible) {
1971 XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
1973 XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
1977 document.CaretHasFocus ();
1979 EventHandler eh = (EventHandler)(Events [HScrolledEvent]);
1981 eh (this, EventArgs.Empty);
1984 private void vscroll_ValueChanged(object sender, EventArgs e) {
1987 old_viewport_y = document.ViewPortY;
1988 document.ViewPortY = this.vscroll.Value;
1991 // Before scrolling we want to destroy the caret, then draw a new one after the scroll
1992 // the reason for this is that scrolling changes the coordinates of the caret, and we
1993 // will get tracers if we don't
1996 document.CaretLostFocus ();
1998 if (hscroll.Visible) {
1999 XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
2001 XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
2005 document.CaretHasFocus ();
2007 EventHandler eh = (EventHandler)(Events [VScrolledEvent]);
2009 eh (this, EventArgs.Empty);
2012 private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
2013 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
2014 if (e.Button == MouseButtons.Left && Capture) {
2015 if (!ClientRectangle.Contains (e.X, e.Y)) {
2016 if (scroll_timer == null) {
2017 scroll_timer = new Timer ();
2018 scroll_timer.Interval = 100;
2019 scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
2022 if (!scroll_timer.Enabled) {
2023 scroll_timer.Start ();
2025 // Force the first tick
2026 ScrollTimerTickHandler (null, EventArgs.Empty);
2030 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
2031 if (click_mode == CaretSelection.Position) {
2032 document.SetSelectionToCaret(false);
2033 document.DisplayCaret();
2038 private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
2042 document.SuspendRecalc ();
2043 // Font changes apply to the whole document
2044 for (int i = 1; i <= document.Lines; i++) {
2045 line = document.GetLine(i);
2046 LineTag.FormatText(line, 1, line.text.Length, Font,
2047 ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor),
2048 null, FormatSpecified.Font | FormatSpecified.Color);
2050 document.ResumeRecalc (false);
2052 // Make sure the caret height is matching the new font height
2053 document.AlignCaret();
2057 private void ScrollTimerTickHandler (object sender, EventArgs e)
2059 Point pt = Cursor.Position;
2061 pt = PointToClient (pt);
2063 if (pt.X < ClientRectangle.Left) {
2064 document.MoveCaret(CaretDirection.CharBackNoWrap);
2065 document.SetSelectionToCaret(false);
2067 CaretMoved(this, null);
2068 } else if (pt.X > ClientRectangle.Right) {
2069 document.MoveCaret(CaretDirection.CharForwardNoWrap);
2070 document.SetSelectionToCaret(false);
2072 CaretMoved(this, null);
2073 } else if (pt.Y > ClientRectangle.Bottom) {
2074 document.MoveCaret(CaretDirection.LineDown);
2075 document.SetSelectionToCaret(false);
2077 CaretMoved(this, null);
2078 } else if (pt.Y < ClientRectangle.Top) {
2079 document.MoveCaret(CaretDirection.LineUp);
2080 document.SetSelectionToCaret(false);
2082 CaretMoved(this, null);
2086 /// <summary>Ensure the caret is always visible</summary>
2087 internal void CaretMoved(object sender, EventArgs e) {
2091 if (!IsHandleCreated || canvas_width < 1 || canvas_height < 1)
2094 document.MoveCaretToTextTag ();
2095 pos = document.Caret;
2097 //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);
2100 // Horizontal scrolling:
2101 // If the caret moves to the left outside the visible area, we jump the document into view, not just one
2102 // character, but 1/3 of the width of the document
2103 // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
2105 // Handle horizontal scrolling
2106 if (document.CaretLine.alignment == HorizontalAlignment.Left) {
2107 // Check if we moved out of view to the left
2108 if (pos.X < (document.ViewPortX)) {
2110 if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
2111 hscroll.Value -= document.ViewPortWidth / 3;
2113 hscroll.Value = hscroll.Minimum;
2115 } while (hscroll.Value > pos.X);
2118 // Check if we moved out of view to the right
2119 if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
2120 if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
2121 if (pos.X - document.ViewPortWidth >= 0) {
2122 hscroll.Value = pos.X - document.ViewPortWidth + 1;
2127 hscroll.Value = hscroll.Maximum;
2130 } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
2131 // hscroll.Value = pos.X;
2133 // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
2134 // hscroll.Value = hscroll.Maximum;
2137 // FIXME - implement center cursor alignment
2140 if (!document.multiline) {
2144 // Handle vertical scrolling
2145 height = document.CaretLine.Height + 1;
2147 if (pos.Y < document.ViewPortY) {
2148 vscroll.Value = pos.Y;
2151 if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
2152 vscroll.Value = Math.Min (vscroll.Maximum, pos.Y - canvas_height + height);
2156 internal bool Paste(IDataObject clip, DataFormats.Format format, bool obey_length) {
2162 if (format == null) {
2163 if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
2164 format = DataFormats.GetFormat(DataFormats.Rtf);
2165 } else if ((this is RichTextBox) && clip.GetDataPresent (DataFormats.Bitmap)) {
2166 format = DataFormats.GetFormat (DataFormats.Bitmap);
2167 } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
2168 format = DataFormats.GetFormat(DataFormats.UnicodeText);
2169 } else if (clip.GetDataPresent(DataFormats.Text)) {
2170 format = DataFormats.GetFormat(DataFormats.Text);
2175 if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
2179 if (!clip.GetDataPresent(format.Name)) {
2184 if (format.Name == DataFormats.Rtf) {
2185 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2186 ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
2187 document.undo.EndUserAction ();
2189 } else if (format.Name == DataFormats.Bitmap) {
2190 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2191 // document.InsertImage (document.caret.line, document.caret.pos, (Image) clip.GetData (DataFormats.Bitmap));
2192 document.MoveCaret (CaretDirection.CharForward);
2193 document.undo.EndUserAction ();
2195 } else if (format.Name == DataFormats.UnicodeText) {
2196 s = (string)clip.GetData(DataFormats.UnicodeText);
2197 } else if (format.Name == DataFormats.Text) {
2198 s = (string)clip.GetData(DataFormats.Text);
2204 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2205 this.SelectedText = s;
2206 document.undo.EndUserAction ();
2208 if ((s.Length + document.Length) < max_length) {
2209 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2210 this.SelectedText = s;
2211 document.undo.EndUserAction ();
2212 } else if (document.Length < max_length) {
2213 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2214 this.SelectedText = s.Substring (0, max_length - document.Length);
2215 document.undo.EndUserAction ();
2222 internal abstract Color ChangeBackColor (Color backColor);
2224 internal override bool IsInputCharInternal (char charCode)
2228 #endregion // Private Methods
2231 // This is called just before OnTextChanged is called.
2232 internal virtual void OnTextUpdate ()
2236 protected override void OnTextChanged (EventArgs e)
2238 base.OnTextChanged (e);
2241 public virtual int GetLineFromCharIndex (int index)
2247 document.CharIndexToLineTag (index, out line_out, out tag_out, out pos);
2249 return line_out.LineNo;
2252 protected override void OnMouseUp (MouseEventArgs e)