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)]
44 [DefaultEvent("TextChanged")]
45 [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
46 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;
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
70 internal ArrayList list_links; // currently showing links
71 private LinkRectangle current_link; // currently hovering link
72 private bool enable_links; // whether links are enabled
74 internal bool has_been_focused;
76 internal int selection_length = -1; // set to the user-specified selection length, or -1 if none
77 internal bool show_caret_w_selection; // TextBox shows the caret when the selection is visible
78 internal int canvas_width;
79 internal int canvas_height;
80 static internal int track_width = 2; //
81 static internal int track_border = 5; //
82 internal DateTime click_last;
83 internal int click_point_x;
84 internal int click_point_y;
85 internal CaretSelection click_mode;
86 internal BorderStyle actual_border_style;
87 internal bool shortcuts_enabled = true;
89 internal static bool draw_lines = false;
92 #endregion // Local Variables
94 #region Internal Constructor
95 // Constructor will go when complete, only for testing - pdb
96 internal TextBoxBase ()
98 alignment = HorizontalAlignment.Left;
99 accepts_return = false;
102 InternalBorderStyle = BorderStyle.Fixed3D;
103 actual_border_style = BorderStyle.Fixed3D;
104 character_casing = CharacterCasing.Normal;
105 hide_selection = true;
106 max_length = short.MaxValue;
107 password_char = '\0';
111 show_selection = false;
112 enable_links = false;
113 list_links = new ArrayList ();
115 show_caret_w_selection = (this is TextBox);
116 document = new Document(this);
117 document.WidthChanged += new EventHandler(document_WidthChanged);
118 document.HeightChanged += new EventHandler(document_HeightChanged);
119 //document.CaretMoved += new EventHandler(CaretMoved);
120 document.Wrap = false;
121 click_last = DateTime.Now;
122 click_mode = CaretSelection.Position;
124 MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
125 MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
126 MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
127 SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
128 FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
129 ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
130 MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
131 RightToLeftChanged += new EventHandler (TextBoxBase_RightToLeftChanged);
133 scrollbars = RichTextBoxScrollBars.None;
135 hscroll = new ImplicitHScrollBar();
136 hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
137 hscroll.SetStyle (ControlStyles.Selectable, false);
138 hscroll.Enabled = false;
139 hscroll.Visible = false;
140 hscroll.Maximum = Int32.MaxValue;
142 vscroll = new ImplicitVScrollBar();
143 vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
144 vscroll.SetStyle (ControlStyles.Selectable, false);
145 vscroll.Enabled = false;
146 vscroll.Visible = false;
147 vscroll.Maximum = Int32.MaxValue;
150 this.Controls.AddImplicit (hscroll);
151 this.Controls.AddImplicit (vscroll);
154 SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
155 SetStyle(ControlStyles.UseTextForAccessibility, false);
157 base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
159 canvas_width = ClientSize.Width;
160 canvas_height = ClientSize.Height;
161 document.ViewPortWidth = canvas_width;
162 document.ViewPortHeight = canvas_height;
164 Cursor = Cursors.IBeam;
166 #endregion // Internal Constructor
168 #region Private and Internal Methods
169 internal string CaseAdjust (string s)
171 if (character_casing == CharacterCasing.Normal)
173 if (character_casing == CharacterCasing.Lower)
178 internal override Size GetPreferredSizeCore (Size proposedSize)
180 return new Size (Width, Height);
183 internal override void HandleClick (int clicks, MouseEventArgs me)
185 // MS seems to fire the click event in spite of the styles they set
186 bool click_set = GetStyle (ControlStyles.StandardClick);
187 bool doubleclick_set = GetStyle (ControlStyles.StandardDoubleClick);
189 // so explicitly set them to true first
190 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
192 base.HandleClick (clicks, me);
194 // then revert to our previous state
196 SetStyle (ControlStyles.StandardClick, false);
197 if (!doubleclick_set)
198 SetStyle (ControlStyles.StandardDoubleClick, false);
201 internal override void PaintControlBackground (PaintEventArgs pevent)
203 if (!ThemeEngine.Current.TextBoxBaseShouldPaintBackground (this))
205 base.PaintControlBackground (pevent);
207 #endregion // Private and Internal Methods
209 #region Public Instance Properties
210 [DefaultValue(false)]
211 [MWFCategory("Behavior")]
212 public bool AcceptsTab {
218 if (value != accepts_tab) {
220 OnAcceptsTabChanged(EventArgs.Empty);
226 [EditorBrowsable (EditorBrowsableState.Never)]
229 [RefreshProperties(RefreshProperties.Repaint)]
230 [MWFCategory("Behavior")]
231 public override bool AutoSize {
237 if (value != auto_size) {
240 if (PreferredHeight != Height) {
241 Height = PreferredHeight;
249 public override System.Drawing.Color BackColor {
251 return base.BackColor;
254 backcolor_set = true;
255 base.BackColor = ChangeBackColor (value);
260 [EditorBrowsable(EditorBrowsableState.Never)]
261 public override System.Drawing.Image BackgroundImage {
263 return base.BackgroundImage;
266 base.BackgroundImage = value;
270 [DefaultValue(BorderStyle.Fixed3D)]
272 [MWFCategory("Appearance")]
273 public BorderStyle BorderStyle {
274 get { return actual_border_style; }
276 if (value == actual_border_style)
279 if (actual_border_style != BorderStyle.Fixed3D || value != BorderStyle.Fixed3D)
282 actual_border_style = value;
283 document.UpdateMargins ();
285 if (value != BorderStyle.Fixed3D)
286 value = BorderStyle.None;
288 InternalBorderStyle = value;
289 OnBorderStyleChanged(EventArgs.Empty);
294 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
295 public bool CanUndo {
297 return document.undo.CanUndo;
302 public override System.Drawing.Color ForeColor {
304 return base.ForeColor;
307 base.ForeColor = value;
312 [MWFCategory("Behavior")]
313 public bool HideSelection {
315 return hide_selection;
319 if (value != hide_selection) {
320 hide_selection = value;
321 OnHideSelectionChanged(EventArgs.Empty);
323 document.selection_visible = !hide_selection;
324 document.InvalidateSelectionArea();
328 [MergableProperty (false)]
329 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
330 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
332 [MWFCategory("Appearance")]
333 public string[] Lines {
338 count = document.Lines;
340 // Handle empty document
341 if ((count == 1) && (document.GetLine (1).text.Length == 0)) {
342 return new string [0];
345 lines = new ArrayList ();
350 StringBuilder lt = new StringBuilder ();
353 line = document.GetLine (i++);
354 lt.Append (line.TextWithoutEnding ());
355 } while (line.ending == LineEnding.Wrap && i <= count);
357 lines.Add (lt.ToString ());
360 return (string []) lines.ToArray (typeof (string));
364 StringBuilder sb = new StringBuilder ();
366 for (int i = 0; i < value.Length; i++) {
367 // Don't add the last line if it is just an empty line feed
368 // the line feed is reflected in the previous line's ending
369 if (i == value.Length - 1 && value[i].Length == 0)
372 sb.Append (value[i] + Environment.NewLine);
375 int newline_length = Environment.NewLine.Length;
377 // We want to remove the final new line character
378 if (sb.Length >= newline_length)
379 sb.Remove (sb.Length - newline_length, newline_length);
381 Text = sb.ToString ();
385 [DefaultValue(32767)]
387 [MWFCategory("Behavior")]
388 public virtual int MaxLength {
390 if (max_length == (int.MaxValue - 1)) { // We don't distinguish between single and multi-line limits
397 if (value != max_length) {
399 value = int.MaxValue - 1;
407 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
408 public bool Modified {
414 if (value != modified) {
416 OnModifiedChanged(EventArgs.Empty);
421 [DefaultValue(false)]
423 [RefreshProperties(RefreshProperties.All)]
424 [MWFCategory("Behavior")]
425 public virtual bool Multiline {
427 return document.multiline;
431 if (value != document.multiline) {
432 document.multiline = value;
435 SetStyle (ControlStyles.FixedHeight, !value);
437 // SetBoundsCore overrides the Height for multiline if it needs to,
438 // so we don't need to worry about it here.
439 SetBoundsCore (Left, Top, Width, ExplicitBounds.Height, BoundsSpecified.None);
442 Parent.PerformLayout ();
444 OnMultilineChanged(EventArgs.Empty);
447 if (document.multiline) {
448 document.Wrap = word_wrap;
449 document.PasswordChar = "";
452 document.Wrap = false;
453 if (this.password_char != '\0') {
455 document.PasswordChar = (this as TextBox).PasswordChar.ToString ();
457 document.PasswordChar = "";
462 CalculateDocument ();
467 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
468 [EditorBrowsable(EditorBrowsableState.Advanced)]
469 // This returns the preferred outer height, not the client height.
470 public int PreferredHeight {
472 int clientDelta = Height - ClientSize.Height;
473 if (BorderStyle != BorderStyle.None)
474 return Font.Height + 7 + clientDelta;
476 // usually in borderless mode the top margin is 0, but
477 // try to access it, in case it was set manually, as ToolStrip* controls do
478 return Font.Height + TopMargin + clientDelta;
482 [RefreshProperties (RefreshProperties.Repaint)]
483 [DefaultValue(false)]
484 [MWFCategory("Behavior")]
485 public bool ReadOnly {
491 if (value != read_only) {
493 if (!backcolor_set) {
495 background_color = SystemColors.Control;
497 background_color = SystemColors.Window;
499 OnReadOnlyChanged(EventArgs.Empty);
506 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
507 public virtual string SelectedText {
509 string retval = document.GetSelection ();
516 value = String.Empty;
518 document.ReplaceSelection(CaseAdjust(value), false);
521 OnTextChanged(EventArgs.Empty);
526 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
527 public virtual int SelectionLength {
529 int res = document.SelectionLength ();
536 string msg = String.Format ("'{0}' is not a valid value for 'SelectionLength'", value);
537 throw new ArgumentOutOfRangeException ("SelectionLength", msg);
540 document.InvalidateSelectionArea ();
547 selection_length = value;
548 start = document.LineTagToCharIndex (document.selection_start.line, document.selection_start.pos);
549 document.CharIndexToLineTag (start + value, out line, out tag, out pos);
550 document.SetSelectionEnd (line, pos, true);
551 document.PositionCaret (line, pos);
553 selection_length = -1;
554 document.SetSelectionEnd (document.selection_start.line, document.selection_start.pos, true);
555 document.PositionCaret (document.selection_start.line, document.selection_start.pos);
561 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
562 public int SelectionStart {
564 return document.LineTagToCharIndex(document.selection_start.line,
565 document.selection_start.pos);
570 string msg = String.Format ("'{0}' is not a valid value for 'SelectionStart'", value);
571 throw new ArgumentOutOfRangeException ("SelectionStart", msg);
574 // If SelectionStart has been used, we don't highlight on focus
575 has_been_focused = true;
577 document.InvalidateSelectionArea ();
578 document.SetSelectionStart (value, false);
579 if (selection_length > -1)
580 document.SetSelectionEnd (value + selection_length, true);
582 document.SetSelectionEnd (value, true);
583 document.PositionCaret (document.selection_start.line, document.selection_start.pos);
588 [DefaultValue (true)]
589 public virtual bool ShortcutsEnabled {
590 get { return shortcuts_enabled; }
591 set { shortcuts_enabled = value; }
594 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
595 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
597 public override string Text {
599 if (document == null || document.Root == null || document.Root.text == null)
602 StringBuilder sb = new StringBuilder();
605 for (int i = 1; i <= document.Lines; i++) {
606 line = document.GetLine (i);
607 sb.Append(line.text.ToString ());
610 return sb.ToString();
614 // reset to force a select all next time the box gets focus
615 has_been_focused = false;
621 if ((value != null) && (value != "")) {
622 document.Insert (document.GetLine (1), 0, false, value);
624 if (IsHandleCreated) {
625 document.SetSelectionToCaret (true);
626 CalculateDocument ();
630 document.PositionCaret (document.GetLine (1), 0);
631 document.SetSelectionToCaret (true);
635 OnTextChanged(EventArgs.Empty);
640 public virtual int TextLength {
642 if (document == null || document.Root == null || document.Root.text == null)
650 [MWFCategory("Behavior")]
651 public bool WordWrap {
657 if (value != word_wrap) {
658 if (document.multiline) {
660 document.Wrap = value;
662 CalculateDocument ();
668 [EditorBrowsable (EditorBrowsableState.Never)]
669 public override ImageLayout BackgroundImageLayout {
670 get { return base.BackgroundImageLayout; }
671 set { base.BackgroundImageLayout = value; }
675 [EditorBrowsable (EditorBrowsableState.Never)]
676 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
677 public new Padding Padding {
678 get { return base.Padding; }
679 set { base.Padding = value; }
682 protected override Cursor DefaultCursor {
683 get { return Cursors.IBeam; }
685 #endregion // Public Instance Properties
687 #region Protected Instance Properties
688 protected override bool CanEnableIme {
690 if (ReadOnly || password_char != '\0')
697 protected override CreateParams CreateParams {
699 return base.CreateParams;
703 protected override System.Drawing.Size DefaultSize {
705 return new Size(100, 20);
709 // Currently our double buffering breaks our scrolling, so don't let people enable this
710 [EditorBrowsable (EditorBrowsableState.Never)]
711 protected override bool DoubleBuffered {
712 get { return false; }
716 #endregion // Protected Instance Properties
718 #region Public Instance Methods
719 public void AppendText (string text)
721 // Save some cycles and only check the Text if we are one line
722 bool is_empty = document.Lines == 1 && Text == String.Empty;
724 // make sure the caret begins at the end
725 if (document.caret.line.line_no != document.Lines ||
726 (document.caret.pos) != document.caret.line.TextLengthWithoutEnding ()) {
727 document.MoveCaret (CaretDirection.CtrlEnd);
729 document.Insert (document.caret.line, document.caret.pos, false, text, document.CaretTag);
730 document.MoveCaret (CaretDirection.CtrlEnd);
731 document.SetSelectionToCaret (true);
737 // Avoid the initial focus selecting all when append text is used
739 has_been_focused = true;
742 OnTextChanged(EventArgs.Empty);
751 public void ClearUndo ()
753 document.undo.Clear();
760 o = new DataObject(DataFormats.Text, SelectedText);
761 if (this is RichTextBox)
762 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
763 Clipboard.SetDataObject(o);
770 o = new DataObject(DataFormats.Text, SelectedText);
771 if (this is RichTextBox)
772 o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
773 Clipboard.SetDataObject (o);
775 document.undo.BeginUserAction (Locale.GetText ("Cut"));
776 document.ReplaceSelection (String.Empty, false);
777 document.undo.EndUserAction ();
780 OnTextChanged (EventArgs.Empty);
785 Paste (Clipboard.GetDataObject(), null, false);
788 public void ScrollToCaret ()
791 CaretMoved (this, EventArgs.Empty);
794 public void Select(int start, int length)
796 SelectionStart = start;
797 SelectionLength = length;
800 public void SelectAll ()
804 last = document.GetLine(document.Lines);
805 document.SetSelectionStart(document.GetLine(1), 0, false);
806 document.SetSelectionEnd(last, last.text.Length, true);
807 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
808 selection_length = -1;
810 CaretMoved (this, null);
812 document.DisplayCaret ();
815 internal void SelectAllNoScroll ()
819 last = document.GetLine(document.Lines);
820 document.SetSelectionStart(document.GetLine(1), 0, false);
821 document.SetSelectionEnd(last, last.text.Length, false);
822 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
823 selection_length = -1;
825 document.DisplayCaret ();
828 public override string ToString ()
830 return String.Concat (base.ToString (), ", Text: ", Text);
833 [MonoInternalNote ("Deleting is classed as Typing, instead of its own Undo event")]
836 if (document.undo.Undo ()) {
838 OnTextChanged (EventArgs.Empty);
842 public void DeselectAll ()
847 public virtual char GetCharFromPosition (Point pt)
849 return GetCharFromPositionInternal (pt);
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 pt)
883 LineTag tag = document.FindCursor (pt.X, pt.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 lineNumber)
922 Line line = document.GetLine (lineNumber + 1);
926 return document.LineTagToCharIndex (line, 0);
929 public int GetFirstCharIndexOfCurrentLine ()
931 return document.LineTagToCharIndex (document.caret.line, 0);
933 #endregion // Public Instance Methods
935 #region Protected Instance Methods
936 protected override void CreateHandle ()
938 CalculateDocument ();
939 base.CreateHandle ();
940 document.AlignCaret();
944 internal virtual void HandleLinkClicked (LinkRectangle link_clicked)
948 protected override bool IsInputKey (Keys keyData)
950 if ((keyData & Keys.Alt) != 0)
951 return base.IsInputKey(keyData);
953 switch (keyData & Keys.KeyCode) {
955 return (accepts_return && document.multiline);
959 if (accepts_tab && document.multiline)
960 if ((keyData & Keys.Control) == 0)
979 protected virtual void OnAcceptsTabChanged(EventArgs e)
981 EventHandler eh = (EventHandler)(Events [AcceptsTabChangedEvent]);
986 protected virtual void OnBorderStyleChanged (EventArgs e)
988 EventHandler eh = (EventHandler)(Events [BorderStyleChangedEvent]);
993 protected override void OnFontChanged (EventArgs e)
995 base.OnFontChanged (e);
997 if (auto_size && !document.multiline) {
998 if (PreferredHeight != Height) {
999 Height = PreferredHeight;
1004 protected override void OnHandleCreated (EventArgs e)
1006 base.OnHandleCreated (e);
1010 protected override void OnHandleDestroyed (EventArgs e)
1012 base.OnHandleDestroyed (e);
1015 protected virtual void OnHideSelectionChanged (EventArgs e)
1017 EventHandler eh = (EventHandler)(Events [HideSelectionChangedEvent]);
1022 protected virtual void OnModifiedChanged (EventArgs e)
1024 EventHandler eh = (EventHandler)(Events [ModifiedChangedEvent]);
1029 protected virtual void OnMultilineChanged (EventArgs e)
1031 EventHandler eh = (EventHandler)(Events [MultilineChangedEvent]);
1036 protected override void OnPaddingChanged (EventArgs e)
1038 base.OnPaddingChanged (e);
1041 protected virtual void OnReadOnlyChanged (EventArgs e)
1043 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
1048 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
1050 return base.ProcessCmdKey (ref msg, keyData);
1052 protected override bool ProcessDialogKey (Keys keyData)
1054 // The user can use Ctrl-Tab or Ctrl-Shift-Tab to move control focus
1055 // instead of inserting a Tab. However, the focus-moving-tab-stuffs
1056 // doesn't work if Ctrl is pushed, so we remove it before sending it.
1057 if (accepts_tab && (keyData & (Keys.Control | Keys.Tab)) == (Keys.Control | Keys.Tab))
1058 keyData ^= Keys.Control;
1060 return base.ProcessDialogKey(keyData);
1063 private bool ProcessKey (Keys keyData)
1068 control = (Control.ModifierKeys & Keys.Control) != 0;
1069 shift = (Control.ModifierKeys & Keys.Shift) != 0;
1071 if (shortcuts_enabled) {
1072 switch (keyData & Keys.KeyCode) {
1074 if (control && read_only == false) {
1088 if (control && read_only == false) {
1089 return Paste(Clipboard.GetDataObject(), null, true);
1094 if (control && read_only == false) {
1109 if (read_only == false) {
1111 Paste (Clipboard.GetDataObject (), null, true);
1128 if (shift && read_only == false) {
1133 if (document.selection_visible) {
1134 document.ReplaceSelection("", false);
1136 // DeleteChar only deletes on the line, doesn't do the combine
1137 if (document.CaretPosition >= document.CaretLine.TextLengthWithoutEnding ()) {
1138 if (document.CaretLine.LineNo < document.Lines) {
1141 line = document.GetLine(document.CaretLine.LineNo + 1);
1143 // this line needs to be invalidated before it is combined
1144 // because once it is combined, all it's coordinates will
1146 document.Invalidate (line, 0, line, line.text.Length);
1147 document.Combine(document.CaretLine, line);
1149 document.UpdateView(document.CaretLine,
1155 document.DeleteChar(document.CaretTag.Line, document.CaretPosition, true);
1159 end_pos = document.CaretPosition;
1161 while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
1165 if (end_pos < document.CaretLine.Text.Length) {
1168 document.DeleteChars(document.CaretTag.Line, document.CaretPosition, end_pos - document.CaretPosition);
1173 document.AlignCaret();
1174 document.UpdateCaret();
1175 CaretMoved(this, null);
1178 OnTextChanged (EventArgs.Empty);
1184 switch (keyData & Keys.KeyCode) {
1187 document.MoveCaret(CaretDirection.WordBack);
1189 if (!document.selection_visible || shift) {
1190 document.MoveCaret(CaretDirection.CharBack);
1192 document.MoveCaret(CaretDirection.SelectionStart);
1197 document.SetSelectionToCaret(true);
1199 document.SetSelectionToCaret(false);
1202 CaretMoved(this, null);
1208 document.MoveCaret(CaretDirection.WordForward);
1210 if (!document.selection_visible || shift) {
1211 document.MoveCaret(CaretDirection.CharForward);
1213 document.MoveCaret(CaretDirection.SelectionEnd);
1217 document.SetSelectionToCaret(true);
1219 document.SetSelectionToCaret(false);
1222 CaretMoved(this, null);
1228 if (document.CaretPosition == 0) {
1229 document.MoveCaret(CaretDirection.LineUp);
1231 document.MoveCaret(CaretDirection.Home);
1234 document.MoveCaret(CaretDirection.LineUp);
1237 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1238 document.SetSelectionToCaret(true);
1240 document.SetSelectionToCaret(false);
1243 CaretMoved(this, null);
1249 if (document.CaretPosition == document.CaretLine.Text.Length) {
1250 document.MoveCaret(CaretDirection.LineDown);
1252 document.MoveCaret(CaretDirection.End);
1255 document.MoveCaret(CaretDirection.LineDown);
1258 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1259 document.SetSelectionToCaret(true);
1261 document.SetSelectionToCaret(false);
1264 CaretMoved(this, null);
1269 if ((Control.ModifierKeys & Keys.Control) != 0) {
1270 document.MoveCaret(CaretDirection.CtrlHome);
1272 document.MoveCaret(CaretDirection.Home);
1275 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1276 document.SetSelectionToCaret(true);
1278 document.SetSelectionToCaret(false);
1281 CaretMoved(this, null);
1286 if ((Control.ModifierKeys & Keys.Control) != 0) {
1287 document.MoveCaret(CaretDirection.CtrlEnd);
1289 document.MoveCaret(CaretDirection.End);
1292 if ((Control.ModifierKeys & Keys.Shift) == 0) {
1293 document.SetSelectionToCaret(true);
1295 document.SetSelectionToCaret(false);
1298 CaretMoved(this, null);
1302 //case Keys.Enter: {
1303 // // ignoring accepts_return, fixes bug #76355
1304 // if (!read_only && document.multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
1307 // if (document.selection_visible) {
1308 // document.ReplaceSelection("\n", false);
1311 // line = document.CaretLine;
1313 // document.Split (document.CaretLine, document.CaretTag, document.CaretPosition);
1314 // line.ending = LineEnding.Rich;
1315 // document.InsertString (line, line.text.Length,
1316 // document.LineEndingToString (line.ending));
1317 // OnTextChanged(EventArgs.Empty);
1319 // document.UpdateView (line, document.Lines - line.line_no, 0);
1320 // CaretMoved(this, null);
1327 if (!read_only && accepts_tab && document.multiline) {
1328 document.InsertCharAtCaret ('\t', true);
1330 CaretMoved(this, null);
1332 OnTextChanged (EventArgs.Empty);
1340 if ((Control.ModifierKeys & Keys.Control) != 0) {
1341 document.MoveCaret(CaretDirection.CtrlPgUp);
1343 document.MoveCaret(CaretDirection.PgUp);
1345 document.DisplayCaret ();
1349 case Keys.PageDown: {
1350 if ((Control.ModifierKeys & Keys.Control) != 0) {
1351 document.MoveCaret(CaretDirection.CtrlPgDn);
1353 document.MoveCaret(CaretDirection.PgDn);
1355 document.DisplayCaret ();
1363 internal virtual void RaiseSelectionChanged ()
1365 // Do nothing, overridden in RTB
1368 private void HandleBackspace (bool control)
1372 fire_changed = false;
1374 // delete only deletes on the line, doesn't do the combine
1375 if (document.selection_visible) {
1376 document.undo.BeginUserAction (Locale.GetText ("Delete"));
1377 document.ReplaceSelection("", false);
1378 document.undo.EndUserAction ();
1379 fire_changed = true;
1380 document.SetSelectionToCaret (true);
1382 document.SetSelectionToCaret (true);
1384 if (document.CaretPosition == 0) {
1385 if (document.CaretLine.LineNo > 1) {
1389 line = document.GetLine(document.CaretLine.LineNo - 1);
1390 new_caret_pos = line.TextLengthWithoutEnding ();
1392 // Invalidate the old line position before we do the combine
1393 document.Invalidate (line, 0, line, line.text.Length);
1394 document.Combine(line, document.CaretLine);
1396 document.UpdateView(line, document.Lines - line.LineNo, 0);
1397 document.PositionCaret(line, new_caret_pos);
1398 document.SetSelectionToCaret (true);
1399 document.UpdateCaret();
1400 fire_changed = true;
1403 if (!control || document.CaretPosition == 0) {
1405 // Move before we delete because the delete will change positions around
1406 // if we cross a wrap border
1407 LineTag tag = document.CaretTag;
1408 int pos = document.CaretPosition;
1409 document.MoveCaret (CaretDirection.CharBack);
1410 document.DeleteChar (tag.Line, pos, false);
1411 document.SetSelectionToCaret (true);
1416 start_pos = document.CaretPosition - 1;
1417 while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
1421 document.undo.BeginUserAction (Locale.GetText ("Delete"));
1422 document.DeleteChars(document.CaretTag.Line, start_pos, document.CaretPosition - start_pos);
1423 document.undo.EndUserAction ();
1424 document.PositionCaret(document.CaretLine, start_pos);
1425 document.SetSelectionToCaret (true);
1427 document.UpdateCaret();
1428 fire_changed = true;
1432 CaretMoved (this, null);
1436 OnTextChanged(EventArgs.Empty);
1440 private void HandleEnter ()
1442 // ignoring accepts_return, fixes bug #76355
1443 if (!read_only && document.multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
1446 if (document.selection_visible)
1447 document.ReplaceSelection ("", false);
1449 line = document.CaretLine;
1451 document.Split (document.CaretLine, document.CaretTag, document.CaretPosition);
1452 line.ending = document.StringToLineEnding (Environment.NewLine);
1453 document.InsertString (line, line.text.Length, document.LineEndingToString (line.ending));
1455 document.UpdateView (line, document.Lines - line.line_no, 0);
1456 CaretMoved (this, null);
1458 OnTextChanged (EventArgs.Empty);
1462 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1464 // Make sure we don't get sized bigger than we want to be
1467 if (!document.multiline) {
1468 if (height != PreferredHeight) {
1469 // If the specified has Height, we need to store that in the
1470 // ExplicitBounds because we are going to override it
1471 if ((specified & BoundsSpecified.Height) != 0) {
1472 Rectangle r = ExplicitBounds;
1475 specified &= ~BoundsSpecified.Height;
1478 height = PreferredHeight;
1483 base.SetBoundsCore (x, y, width, height, specified);
1486 protected override void WndProc (ref Message m)
1488 switch ((Msg)m.Msg) {
1489 case Msg.WM_KEYDOWN: {
1490 if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
1491 m.Result = IntPtr.Zero;
1501 if (ProcessKeyMessage(ref m)) {
1502 m.Result = IntPtr.Zero;
1510 m.Result = IntPtr.Zero;
1512 ch = m.WParam.ToInt32();
1515 HandleBackspace(true);
1516 } else if (ch >= 32) {
1517 if (document.selection_visible) {
1518 document.ReplaceSelection("", false);
1521 char c = (char)m.WParam;
1522 switch (character_casing) {
1523 case CharacterCasing.Upper:
1524 c = Char.ToUpper((char) m.WParam);
1526 case CharacterCasing.Lower:
1527 c = Char.ToLower((char) m.WParam);
1531 if (document.Length < max_length) {
1532 document.InsertCharAtCaret(c, true);
1534 CaretMoved (this, null);
1536 OnTextChanged(EventArgs.Empty);
1539 XplatUI.AudibleAlert(AlertType.Default);
1542 } else if (ch == 8) {
1543 HandleBackspace(false);
1544 } else if (ch == 13)
1550 case Msg.WM_SETFOCUS:
1551 base.WndProc(ref m);
1552 document.CaretHasFocus ();
1555 case Msg.WM_KILLFOCUS:
1556 base.WndProc(ref m);
1557 document.CaretLostFocus ();
1560 case Msg.WM_NCPAINT:
1561 if (!ThemeEngine.Current.TextBoxBaseHandleWmNcPaint (this, ref m))
1562 base.WndProc(ref m);
1566 base.WndProc(ref m);
1571 #endregion // Protected Instance Methods
1574 static object AcceptsTabChangedEvent = new object ();
1575 static object AutoSizeChangedEvent = new object ();
1576 static object BorderStyleChangedEvent = new object ();
1577 static object HideSelectionChangedEvent = new object ();
1578 static object ModifiedChangedEvent = new object ();
1579 static object MultilineChangedEvent = new object ();
1580 static object ReadOnlyChangedEvent = new object ();
1581 static object HScrolledEvent = new object ();
1582 static object VScrolledEvent = new object ();
1584 public event EventHandler AcceptsTabChanged {
1585 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
1586 remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
1590 [EditorBrowsable (EditorBrowsableState.Never)]
1591 public new event EventHandler AutoSizeChanged {
1592 add { Events.AddHandler (AutoSizeChangedEvent, value); }
1593 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
1596 public event EventHandler BorderStyleChanged {
1597 add { Events.AddHandler (BorderStyleChangedEvent, value); }
1598 remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
1601 public event EventHandler HideSelectionChanged {
1602 add { Events.AddHandler (HideSelectionChangedEvent, value); }
1603 remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
1606 public event EventHandler ModifiedChanged {
1607 add { Events.AddHandler (ModifiedChangedEvent, value); }
1608 remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
1611 public event EventHandler MultilineChanged {
1612 add { Events.AddHandler (MultilineChangedEvent, value); }
1613 remove { Events.RemoveHandler (MultilineChangedEvent, value); }
1616 public event EventHandler ReadOnlyChanged {
1617 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
1618 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
1621 internal event EventHandler HScrolled {
1622 add { Events.AddHandler (HScrolledEvent, value); }
1623 remove { Events.RemoveHandler (HScrolledEvent, value); }
1626 internal event EventHandler VScrolled {
1627 add { Events.AddHandler (VScrolledEvent, value); }
1628 remove { Events.RemoveHandler (VScrolledEvent, value); }
1632 [EditorBrowsable(EditorBrowsableState.Never)]
1633 public new event EventHandler BackgroundImageChanged {
1634 add { base.BackgroundImageChanged += value; }
1635 remove { base.BackgroundImageChanged -= value; }
1639 [EditorBrowsable (EditorBrowsableState.Never)]
1640 public new event EventHandler BackgroundImageLayoutChanged {
1641 add { base.BackgroundImageLayoutChanged += value; }
1642 remove { base.BackgroundImageLayoutChanged -= value; }
1646 [EditorBrowsable (EditorBrowsableState.Always)]
1647 public new event MouseEventHandler MouseClick {
1648 add { base.MouseClick += value; }
1649 remove { base.MouseClick -= value; }
1653 [EditorBrowsable (EditorBrowsableState.Never)]
1654 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1655 public new event EventHandler PaddingChanged {
1656 add { base.PaddingChanged += value; }
1657 remove { base.PaddingChanged -= value; }
1661 [EditorBrowsable (EditorBrowsableState.Always)]
1662 public new event EventHandler Click {
1663 add { base.Click += value; }
1664 remove { base.Click -= value; }
1667 // XXX should this not manipulate base.Paint?
1668 #pragma warning disable 0067
1671 [EditorBrowsable(EditorBrowsableState.Never)]
1672 public new event PaintEventHandler Paint;
1673 #pragma warning restore 0067
1675 #endregion // Events
1677 #region Private Methods
1678 internal Document Document {
1688 internal bool EnableLinks {
1689 get { return enable_links; }
1691 enable_links = value;
1693 document.EnableLinks = value;
1697 internal override bool ScaleChildrenInternal {
1698 get { return false; }
1701 internal bool ShowSelection {
1703 if (show_selection || !hide_selection) {
1711 if (show_selection == value)
1714 show_selection = value;
1715 // Currently InvalidateSelectionArea is commented out so do a full invalidate
1716 document.InvalidateSelectionArea();
1720 internal int TopMargin {
1722 return document.top_margin;
1725 document.top_margin = value;
1729 #region UIA Framework Properties
1731 internal ScrollBar UIAHScrollBar {
1732 get { return hscroll; }
1735 internal ScrollBar UIAVScrollBar {
1736 get { return vscroll; }
1739 #endregion UIA Framework Properties
1741 internal Graphics CreateGraphicsInternal ()
1743 if (IsHandleCreated)
1744 return base.CreateGraphics();
1746 return DeviceContext;
1749 internal override void OnPaintInternal (PaintEventArgs pevent)
1751 Draw (pevent.Graphics, pevent.ClipRectangle);
1754 // OnPaint does not get raised on MS (see bug #80639)
1756 pevent.Handled = true;
1759 internal void Draw (Graphics g, Rectangle clippingArea)
1761 ThemeEngine.Current.TextBoxBaseFillBackground (this, g, clippingArea);
1763 // Draw the viewable document
1764 document.Draw(g, clippingArea);
1767 private void FixupHeight ()
1770 if (!document.multiline) {
1771 if (PreferredHeight != Height) {
1772 Height = PreferredHeight;
1778 private bool IsDoubleClick (MouseEventArgs e)
1780 TimeSpan interval = DateTime.Now - click_last;
1781 if (interval.TotalMilliseconds > SystemInformation.DoubleClickTime)
1783 Size dcs = SystemInformation.DoubleClickSize;
1784 if (e.X < click_point_x - dcs.Width / 2 || e.X > click_point_x + dcs.Width / 2)
1786 if (e.Y < click_point_y - dcs.Height / 2 || e.Y > click_point_y + dcs.Height / 2)
1791 private void TextBoxBase_MouseDown (object sender, MouseEventArgs e)
1793 bool dbliclick = false;
1795 if (e.Button == MouseButtons.Left) {
1797 // Special case when shift key is pressed and
1798 // left mouse is clicked.. set selection from
1799 // current cursor to mouse
1800 if ((Control.ModifierKeys & Keys.Shift) > 0) {
1801 document.PositionCaret (e.X + document.ViewPortX, e.Y + document.ViewPortY);
1802 document.SetSelectionToCaret (false);
1803 document.DisplayCaret ();
1807 dbliclick = IsDoubleClick (e);
1809 if (current_link != null) {
1810 HandleLinkClicked (current_link);
1814 //ensure nothing is selected anymore BEFORE we
1815 //position the caret, so the caret is recreated
1816 //(caret is only visible when nothing is selected)
1817 if (document.selection_visible && dbliclick == false) {
1818 document.SetSelectionToCaret (true);
1819 click_mode = CaretSelection.Position;
1822 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
1825 switch (click_mode) {
1826 case CaretSelection.Position:
1828 click_mode = CaretSelection.Word;
1830 case CaretSelection.Word:
1832 if (this is TextBox) {
1833 document.SetSelectionToCaret (true);
1834 click_mode = CaretSelection.Position;
1838 document.ExpandSelection (CaretSelection.Line, false);
1839 click_mode = CaretSelection.Line;
1841 case CaretSelection.Line:
1843 // Gotta do this first because Exanding to a word
1844 // from a line doesn't really work
1845 document.SetSelectionToCaret (true);
1848 click_mode = CaretSelection.Word;
1852 document.SetSelectionToCaret (true);
1853 click_mode = CaretSelection.Position;
1856 click_point_x = e.X;
1857 click_point_y = e.Y;
1858 click_last = DateTime.Now;
1861 if ((e.Button == MouseButtons.Middle) && XplatUI.RunningOnUnix) {
1862 Document.Marker marker;
1864 marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
1865 marker.line = marker.tag.Line;
1866 marker.height = marker.tag.Height;
1868 document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
1869 Paste (Clipboard.GetDataObject (true), null, true);
1873 private void TextBoxBase_MouseUp (object sender, MouseEventArgs e)
1875 if (e.Button == MouseButtons.Left) {
1876 if (click_mode == CaretSelection.Position) {
1877 document.SetSelectionToCaret(false);
1878 document.DisplayCaret();
1880 // Only raise if there is text.
1881 if (Text.Length > 0)
1882 RaiseSelectionChanged ();
1885 if (scroll_timer != null) {
1886 scroll_timer.Enabled = false;
1892 private void SizeControls ()
1894 if (hscroll.Visible) {
1895 //vscroll.Maximum += hscroll.Height;
1896 canvas_height = ClientSize.Height - hscroll.Height;
1898 canvas_height = ClientSize.Height;
1901 if (vscroll.Visible) {
1902 //hscroll.Maximum += vscroll.Width;
1903 canvas_width = ClientSize.Width - vscroll.Width;
1905 if (GetInheritedRtoL () == RightToLeft.Yes) {
1906 document.OffsetX = vscroll.Width;
1908 document.OffsetX = 0;
1912 canvas_width = ClientSize.Width;
1913 document.OffsetX = 0;
1916 document.ViewPortWidth = canvas_width;
1917 document.ViewPortHeight = canvas_height;
1920 private void PositionControls ()
1922 if (canvas_height < 1 || canvas_width < 1)
1925 int hmod = vscroll.Visible ? vscroll.Width : 0;
1926 int vmod = hscroll.Visible ? hscroll.Height : 0;
1928 if (GetInheritedRtoL () == RightToLeft.Yes) {
1929 hscroll.Bounds = new Rectangle (ClientRectangle.Left + hmod,
1930 Math.Max(0, ClientRectangle.Height - hscroll.Height),
1934 vscroll.Bounds = new Rectangle (ClientRectangle.Left,
1935 ClientRectangle.Top,
1937 Math.Max(0, ClientSize.Height - (vmod)));
1939 hscroll.Bounds = new Rectangle (ClientRectangle.Left,
1940 Math.Max(0, ClientRectangle.Height - hscroll.Height),
1941 Math.Max(0, ClientSize.Width - hmod),
1944 vscroll.Bounds = new Rectangle (
1945 Math.Max(0, ClientRectangle.Right - vscroll.Width),
1946 ClientRectangle.Top,
1948 Math.Max(0, ClientSize.Height - vmod));
1952 internal RightToLeft GetInheritedRtoL ()
1954 for (Control c = this; c != null; c = c.Parent)
1955 if (c.RightToLeft != RightToLeft.Inherit)
1956 return c.RightToLeft;
1957 return RightToLeft.No;
1960 private void TextBoxBase_SizeChanged (object sender, EventArgs e)
1962 if (IsHandleCreated)
1963 CalculateDocument ();
1966 private void TextBoxBase_RightToLeftChanged (object o, EventArgs e)
1968 if (IsHandleCreated)
1969 CalculateDocument ();
1972 private void TextBoxBase_MouseWheel (object sender, MouseEventArgs e)
1974 if (!vscroll.Enabled)
1978 vscroll.Value = Math.Min (vscroll.Value + SystemInformation.MouseWheelScrollLines * 5,
1979 Math.Max (0, vscroll.Maximum - document.ViewPortHeight + 1));
1981 vscroll.Value = Math.Max (0, vscroll.Value - SystemInformation.MouseWheelScrollLines * 5);
1984 internal virtual void SelectWord ()
1986 StringBuilder s = document.caret.line.text;
1987 int start = document.caret.pos;
1988 int end = document.caret.pos;
1991 if (document.caret.line.line_no >= document.Lines)
1993 Line line = document.GetLine (document.caret.line.line_no + 1);
1994 document.PositionCaret (line, 0);
2003 // skip whitespace until we hit a word
2004 while (start > 0 && s [start] == ' ')
2007 while (start > 0 && (s [start] != ' '))
2009 if (s [start] == ' ')
2013 if (s [end] == ' ') {
2014 while (end < s.Length && s [end] == ' ')
2017 while (end < s.Length && s [end] != ' ')
2019 while (end < s.Length && s [end] == ' ')
2023 document.SetSelection (document.caret.line, start, document.caret.line, end);
2024 document.PositionCaret (document.selection_end.line, document.selection_end.pos);
2025 document.DisplayCaret();
2028 internal void CalculateDocument()
2030 CalculateScrollBars ();
2031 document.RecalculateDocument (CreateGraphicsInternal ());
2034 if (document.caret.line != null && document.caret.line.Y < document.ViewPortHeight) {
2035 // The window has probably been resized, making the entire thing visible, so
2036 // we need to set the scroll position back to zero.
2043 internal void CalculateScrollBars ()
2045 // FIXME - need separate calculations for center and right alignment
2048 if (document.Width >= document.ViewPortWidth) {
2049 hscroll.SetValues (0, Math.Max (1, document.Width), -1,
2050 document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth);
2051 if (document.multiline)
2052 hscroll.Enabled = true;
2054 hscroll.Enabled = false;
2055 hscroll.Maximum = document.ViewPortWidth;
2058 if (document.Height >= document.ViewPortHeight) {
2059 vscroll.SetValues (0, Math.Max (1, document.Height), -1,
2060 document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight);
2061 if (document.multiline)
2062 vscroll.Enabled = true;
2064 vscroll.Enabled = false;
2065 vscroll.Maximum = document.ViewPortHeight;
2069 switch (scrollbars) {
2070 case RichTextBoxScrollBars.Both:
2071 case RichTextBoxScrollBars.Horizontal:
2073 hscroll.Visible = hscroll.Enabled;
2075 hscroll.Visible = this.Multiline;
2077 case RichTextBoxScrollBars.ForcedBoth:
2078 case RichTextBoxScrollBars.ForcedHorizontal:
2079 hscroll.Visible = true;
2082 hscroll.Visible = false;
2086 hscroll.Visible = false;
2089 switch (scrollbars) {
2090 case RichTextBoxScrollBars.Both:
2091 case RichTextBoxScrollBars.Vertical:
2093 vscroll.Visible = vscroll.Enabled;
2095 vscroll.Visible = this.Multiline;
2097 case RichTextBoxScrollBars.ForcedBoth:
2098 case RichTextBoxScrollBars.ForcedVertical:
2099 vscroll.Visible = true;
2102 vscroll.Visible = false;
2106 PositionControls ();
2108 SizeControls (); //Update sizings now we've decided whats visible
2111 private void document_WidthChanged (object sender, EventArgs e)
2113 CalculateScrollBars();
2116 private void document_HeightChanged (object sender, EventArgs e)
2118 CalculateScrollBars();
2121 private void ScrollLinks (int xChange, int yChange)
2123 foreach (LinkRectangle link in list_links)
2124 link.Scroll (xChange, yChange);
2127 private void hscroll_ValueChanged (object sender, EventArgs e)
2131 old_viewport_x = document.ViewPortX;
2132 document.ViewPortX = this.hscroll.Value;
2135 // Before scrolling we want to destroy the caret, then draw a new one after the scroll
2136 // the reason for this is that scrolling changes the coordinates of the caret, and we
2137 // will get tracers if we don't
2140 document.CaretLostFocus ();
2142 if (vscroll.Visible) {
2143 if (GetInheritedRtoL () == RightToLeft.Yes) {
2144 XplatUI.ScrollWindow (this.Handle, new Rectangle (vscroll.Width, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
2146 XplatUI.ScrollWindow (this.Handle, new Rectangle (0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
2149 XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
2152 ScrollLinks (old_viewport_x - this.hscroll.Value, 0);
2155 document.CaretHasFocus ();
2157 EventHandler eh = (EventHandler)(Events [HScrolledEvent]);
2159 eh (this, EventArgs.Empty);
2162 private void vscroll_ValueChanged (object sender, EventArgs e)
2166 old_viewport_y = document.ViewPortY;
2167 document.ViewPortY = this.vscroll.Value;
2170 // Before scrolling we want to destroy the caret, then draw a new one after the scroll
2171 // the reason for this is that scrolling changes the coordinates of the caret, and we
2172 // will get tracers if we don't
2175 document.CaretLostFocus ();
2177 if (hscroll.Visible) {
2178 XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
2180 XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
2183 ScrollLinks (0, old_viewport_y - this.vscroll.Value);
2186 document.CaretHasFocus ();
2188 EventHandler eh = (EventHandler)(Events [VScrolledEvent]);
2190 eh (this, EventArgs.Empty);
2193 private void TextBoxBase_MouseMove (object sender, MouseEventArgs e)
2195 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
2196 if (e.Button == MouseButtons.Left && Capture) {
2197 if (!ClientRectangle.Contains (e.X, e.Y)) {
2198 if (scroll_timer == null) {
2199 scroll_timer = new Timer ();
2200 scroll_timer.Interval = 100;
2201 scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
2204 if (!scroll_timer.Enabled) {
2205 scroll_timer.Start ();
2207 // Force the first tick
2208 ScrollTimerTickHandler (null, EventArgs.Empty);
2212 document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
2213 if (click_mode == CaretSelection.Position) {
2214 document.SetSelectionToCaret(false);
2215 document.DisplayCaret();
2219 //search through link boxes to see if the mouse is over one of them
2221 bool found_link = false;
2222 foreach (LinkRectangle link in list_links) {
2223 if (link.LinkAreaRectangle.Contains (e.X, e.Y)) {
2224 XplatUI.SetCursor (window.Handle, Cursors.Hand.handle);
2227 current_link = link;
2232 if (found_link == false) {
2233 XplatUI.SetCursor (window.Handle, DefaultCursor.handle);
2234 current_link = null;
2238 private void TextBoxBase_FontOrColorChanged (object sender, EventArgs e)
2242 document.SuspendRecalc ();
2243 // Font changes apply to the whole document
2244 for (int i = 1; i <= document.Lines; i++) {
2245 line = document.GetLine(i);
2246 if (LineTag.FormatText(line, 1, line.text.Length, Font, ForeColor,
2247 Color.Empty, FormatSpecified.Font | FormatSpecified.Color))
2248 document.RecalculateDocument (CreateGraphicsInternal (), line.LineNo, line.LineNo, false);
2250 document.ResumeRecalc (false);
2252 // Make sure the caret height is matching the new font height
2253 document.AlignCaret();
2256 private void ScrollTimerTickHandler (object sender, EventArgs e)
2258 Point pt = Cursor.Position;
2260 pt = PointToClient (pt);
2262 if (pt.X < ClientRectangle.Left) {
2263 document.MoveCaret(CaretDirection.CharBackNoWrap);
2264 document.SetSelectionToCaret(false);
2266 CaretMoved(this, null);
2267 } else if (pt.X > ClientRectangle.Right) {
2268 document.MoveCaret(CaretDirection.CharForwardNoWrap);
2269 document.SetSelectionToCaret(false);
2271 CaretMoved(this, null);
2272 } else if (pt.Y > ClientRectangle.Bottom) {
2273 document.MoveCaret(CaretDirection.LineDown);
2274 document.SetSelectionToCaret(false);
2276 CaretMoved(this, null);
2277 } else if (pt.Y < ClientRectangle.Top) {
2278 document.MoveCaret(CaretDirection.LineUp);
2279 document.SetSelectionToCaret(false);
2281 CaretMoved(this, null);
2285 /// <summary>Ensure the caret is always visible</summary>
2286 internal void CaretMoved (object sender, EventArgs e)
2291 if (!IsHandleCreated || canvas_width < 1 || canvas_height < 1)
2294 document.MoveCaretToTextTag ();
2295 pos = document.Caret;
2297 //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);
2300 // Horizontal scrolling:
2301 // If the caret moves to the left outside the visible area, we jump the document into view, not just one
2302 // character, but 1/3 of the width of the document
2303 // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
2305 // Handle horizontal scrolling
2306 if (document.CaretLine.alignment == HorizontalAlignment.Left) {
2307 // Check if we moved out of view to the left
2308 if (pos.X < (document.ViewPortX)) {
2310 if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
2311 hscroll.SafeValueSet (hscroll.Value - document.ViewPortWidth / 3);
2313 hscroll.Value = hscroll.Minimum;
2315 } while (hscroll.Value > pos.X);
2318 // Check if we moved out of view to the right
2319 if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
2320 if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
2321 if (pos.X - document.ViewPortWidth >= 0) {
2322 hscroll.SafeValueSet (pos.X - document.ViewPortWidth + 1);
2327 hscroll.Value = hscroll.Maximum;
2330 } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
2331 // hscroll.Value = pos.X;
2333 // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
2334 // hscroll.Value = hscroll.Maximum;
2337 // FIXME - implement center cursor alignment
2340 if (Text.Length > 0)
2341 RaiseSelectionChanged ();
2343 if (!document.multiline)
2346 // Handle vertical scrolling
2347 height = document.CaretLine.Height + 1;
2349 if (pos.Y < document.ViewPortY)
2350 vscroll.SafeValueSet (pos.Y);
2351 if ((pos.Y + height) > (document.ViewPortY + canvas_height))
2352 vscroll.Value = Math.Min (vscroll.Maximum, pos.Y - canvas_height + height);
2355 internal bool Paste (IDataObject clip, DataFormats.Format format, bool obey_length)
2362 if (format == null) {
2363 if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
2364 format = DataFormats.GetFormat(DataFormats.Rtf);
2365 } else if ((this is RichTextBox) && clip.GetDataPresent (DataFormats.Bitmap)) {
2366 format = DataFormats.GetFormat (DataFormats.Bitmap);
2367 } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
2368 format = DataFormats.GetFormat(DataFormats.UnicodeText);
2369 } else if (clip.GetDataPresent(DataFormats.Text)) {
2370 format = DataFormats.GetFormat(DataFormats.Text);
2375 if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
2379 if (!clip.GetDataPresent(format.Name)) {
2384 if (format.Name == DataFormats.Rtf) {
2385 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2386 ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
2387 document.undo.EndUserAction ();
2390 } else if (format.Name == DataFormats.Bitmap) {
2391 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2392 // document.InsertImage (document.caret.line, document.caret.pos, (Image) clip.GetData (DataFormats.Bitmap));
2393 document.MoveCaret (CaretDirection.CharForward);
2394 document.undo.EndUserAction ();
2396 } else if (format.Name == DataFormats.UnicodeText) {
2397 s = (string)clip.GetData(DataFormats.UnicodeText);
2398 } else if (format.Name == DataFormats.Text) {
2399 s = (string)clip.GetData(DataFormats.Text);
2405 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2406 this.SelectedText = s;
2407 document.undo.EndUserAction ();
2409 if ((s.Length + (document.Length - SelectedText.Length)) < max_length) {
2410 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2411 this.SelectedText = s;
2412 document.undo.EndUserAction ();
2413 } else if ((document.Length - SelectedText.Length) < max_length) {
2414 document.undo.BeginUserAction (Locale.GetText ("Paste"));
2415 this.SelectedText = s.Substring (0, max_length - (document.Length - SelectedText.Length));
2416 document.undo.EndUserAction ();
2424 internal virtual Color ChangeBackColor (Color backColor)
2429 internal override bool IsInputCharInternal (char charCode)
2433 #endregion // Private Methods
2435 #region Private Classes
2436 internal class LinkRectangle {
2437 private Rectangle link_area_rectangle;
2438 private LineTag link_tag;
2440 public LinkRectangle (Rectangle rect)
2443 link_area_rectangle = rect;
2446 public Rectangle LinkAreaRectangle {
2447 get { return link_area_rectangle; }
2448 set { link_area_rectangle = value; }
2451 public LineTag LinkTag {
2452 get { return link_tag; }
2453 set { link_tag = value; }
2456 public void Scroll (int x_change, int y_change)
2458 link_area_rectangle.X += x_change;
2459 link_area_rectangle.Y += y_change;
2464 // This is called just before OnTextChanged is called.
2465 internal virtual void OnTextUpdate ()
2469 protected override void OnTextChanged (EventArgs e)
2471 base.OnTextChanged (e);
2474 public virtual int GetLineFromCharIndex (int index)
2480 document.CharIndexToLineTag (index, out line_out, out tag_out, out pos);
2482 return line_out.LineNo;
2485 protected override void OnMouseUp (MouseEventArgs mevent)
2487 base.OnMouseUp (mevent);