1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-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
{
40 [DefaultEvent("TextChanged")]
41 [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts
.AssemblySystem_Design
)]
42 public abstract class TextBoxBase
: Control
{
43 #region Local Variables
44 internal HorizontalAlignment alignment
;
45 internal bool accepts_tab
;
46 internal bool accepts_return
;
47 internal bool auto_size
;
48 internal bool backcolor_set
;
49 internal CharacterCasing character_casing
;
50 internal bool hide_selection
;
51 internal int max_length
;
52 internal bool modified
;
53 internal bool multiline
;
54 internal char password_char
;
55 internal bool read_only
;
56 internal bool word_wrap
;
57 internal Document document
;
58 internal LineTag caret_tag
; // tag our cursor is in
59 internal int caret_pos
; // position on the line our cursor is in (can be 0 = beginning of line)
60 internal ImplicitHScrollBar hscroll
;
61 internal ImplicitVScrollBar vscroll
;
62 internal RichTextBoxScrollBars scrollbars
;
63 internal Timer scroll_timer
;
64 internal bool richtext
;
65 internal bool show_selection
; // set to true to always show selection, even if no focus is set
66 internal int selection_length
= -1; // set to the user-specified selection length, or -1 if none
67 internal bool show_caret_w_selection
; // TextBox shows the caret when the selection is visible
68 internal int requested_height
;
69 internal int canvas_width
;
70 internal int canvas_height
;
71 static internal int track_width
= 2; //
72 static internal int track_border
= 5; //
73 internal DateTime click_last
;
74 internal int click_point_x
;
75 internal int click_point_y
;
76 internal CaretSelection click_mode
;
79 internal static bool draw_lines
= false;
82 #endregion // Local Variables
84 #region Internal Constructor
85 // Constructor will go when complete, only for testing - pdb
86 internal TextBoxBase() {
87 alignment
= HorizontalAlignment
.Left
;
88 accepts_return
= false;
91 border_style
= BorderStyle
.Fixed3D
;
92 character_casing
= CharacterCasing
.Normal
;
93 hide_selection
= true;
101 show_selection
= false;
102 show_caret_w_selection
= (this is TextBox
);
103 document
= new Document(this);
104 document
.WidthChanged
+= new EventHandler(document_WidthChanged
);
105 document
.HeightChanged
+= new EventHandler(document_HeightChanged
);
106 //document.CaretMoved += new EventHandler(CaretMoved);
107 document
.Wrap
= false;
108 requested_height
= -1;
109 click_last
= DateTime
.Now
;
110 click_mode
= CaretSelection
.Position
;
111 bmp
= new Bitmap(1, 1, System
.Drawing
.Imaging
.PixelFormat
.Format32bppArgb
);
113 MouseDown
+= new MouseEventHandler(TextBoxBase_MouseDown
);
114 MouseUp
+= new MouseEventHandler(TextBoxBase_MouseUp
);
115 MouseMove
+= new MouseEventHandler(TextBoxBase_MouseMove
);
116 SizeChanged
+= new EventHandler(TextBoxBase_SizeChanged
);
117 FontChanged
+= new EventHandler(TextBoxBase_FontOrColorChanged
);
118 ForeColorChanged
+= new EventHandler(TextBoxBase_FontOrColorChanged
);
119 MouseWheel
+= new MouseEventHandler(TextBoxBase_MouseWheel
);
121 scrollbars
= RichTextBoxScrollBars
.None
;
123 hscroll
= new ImplicitHScrollBar();
124 hscroll
.ValueChanged
+= new EventHandler(hscroll_ValueChanged
);
125 hscroll
.SetStyle (ControlStyles
.Selectable
, false);
126 hscroll
.Enabled
= false;
127 hscroll
.Visible
= false;
128 hscroll
.Maximum
= Int32
.MaxValue
;
130 vscroll
= new ImplicitVScrollBar();
131 vscroll
.ValueChanged
+= new EventHandler(vscroll_ValueChanged
);
132 vscroll
.SetStyle (ControlStyles
.Selectable
, false);
133 vscroll
.Enabled
= false;
134 vscroll
.Visible
= false;
135 vscroll
.Maximum
= Int32
.MaxValue
;
138 this.Controls
.AddImplicit (hscroll
);
139 this.Controls
.AddImplicit (vscroll
);
142 SetStyle(ControlStyles
.UserPaint
| ControlStyles
.StandardClick
, false);
144 SetStyle(ControlStyles
.UseTextForAccessibility
, false);
147 canvas_width
= ClientSize
.Width
;
148 canvas_height
= ClientSize
.Height
;
149 document
.ViewPortWidth
= canvas_width
;
150 document
.ViewPortHeight
= canvas_height
;
152 Cursor
= Cursors
.IBeam
;
154 CalculateScrollBars();
156 #endregion // Internal Constructor
158 #region Private and Internal Methods
159 internal string CaseAdjust(string s
) {
160 if (character_casing
== CharacterCasing
.Normal
) {
163 if (character_casing
== CharacterCasing
.Lower
) {
170 internal override void HandleClick(int clicks
, MouseEventArgs me
) {
171 // MS seems to fire the click event in spite of the styles they set
172 bool click_set
= GetStyle (ControlStyles
.StandardClick
);
173 bool doubleclick_set
= GetStyle (ControlStyles
.StandardDoubleClick
);
175 // so explicitly set them to true first
176 SetStyle (ControlStyles
.StandardClick
| ControlStyles
.StandardDoubleClick
, true);
178 base.HandleClick (clicks
, me
);
180 // then revert to our previous state
182 SetStyle (ControlStyles
.StandardClick
, false);
183 if (!doubleclick_set
)
184 SetStyle (ControlStyles
.StandardDoubleClick
, false);
187 #endregion // Private and Internal Methods
189 #region Public Instance Properties
190 [DefaultValue(false)]
191 [MWFCategory("Behavior")]
192 public bool AcceptsTab
{
198 if (value != accepts_tab
) {
200 OnAcceptsTabChanged(EventArgs
.Empty
);
207 [RefreshProperties(RefreshProperties
.Repaint
)]
208 [MWFCategory("Behavior")]
221 if (value != auto_size
) {
224 if (PreferredHeight
!= ClientSize
.Height
) {
225 ClientSize
= new Size(ClientSize
.Width
, PreferredHeight
);
228 OnAutoSizeChanged(EventArgs
.Empty
);
234 public override System
.Drawing
.Color BackColor
{
236 return base.BackColor
;
239 if (value != ThemeEngine
.Current
.ColorWindow
) {
240 backcolor_set
= true;
242 backcolor_set
= false;
244 base.BackColor
= value;
249 [EditorBrowsable(EditorBrowsableState
.Never
)]
250 public override System
.Drawing
.Image BackgroundImage
{
252 return base.BackgroundImage
;
255 base.BackgroundImage
= value;
259 [DefaultValue(BorderStyle
.Fixed3D
)]
261 [MWFCategory("Appearance")]
262 public BorderStyle BorderStyle
{
263 get { return InternalBorderStyle; }
265 InternalBorderStyle
= value;
266 OnBorderStyleChanged(EventArgs
.Empty
);
271 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
272 public bool CanUndo
{
274 return document
.undo
.UndoLevels
!= 0;
279 public override System
.Drawing
.Color ForeColor
{
281 return base.ForeColor
;
284 base.ForeColor
= value;
289 [MWFCategory("Behavior")]
290 public bool HideSelection
{
292 return hide_selection
;
296 if (value != hide_selection
) {
297 hide_selection
= value;
298 OnHideSelectionChanged(EventArgs
.Empty
);
300 if (hide_selection
) {
301 document
.selection_visible
= false;
303 document
.selection_visible
= true;
305 document
.InvalidateSelectionArea();
310 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
311 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts
.AssemblySystem_Design
, typeof(System
.Drawing
.Design
.UITypeEditor
))]
313 [MWFCategory("Appearance")]
314 public string[] Lines
{
319 count
= document
.Lines
;
321 // Handle empty document
322 if ((count
== 1) && (document
.GetLine (1).text
.Length
== 0)) {
323 return new string [0];
326 lines
= new ArrayList ();
331 StringBuilder lt
= new StringBuilder ();
334 line
= document
.GetLine (i
++);
335 lt
.Append (line
.text
.ToString ());
336 } while (line
.soft_break
&& i
<= count
);
338 lines
.Add (lt
.ToString ());
341 return (string []) lines
.ToArray (typeof (string));
352 brush
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(this.ForeColor
);
354 document
.NoRecalc
= true;
355 for (i
= 0; i
< l
; i
++) {
357 // Don't add the last line if it is just an empty line feed
358 // the line feed is reflected in the previous line's soft_break = false
359 if (i
== l
- 1 && value [i
].Length
== 0)
362 bool carriage_return
= false;
363 if (value [i
].EndsWith ("\r")) {
364 value [i
] = value [i
].Substring (0, value [i
].Length
- 1);
365 carriage_return
= true;
368 document
.Add(i
+1, CaseAdjust(value[i
]), alignment
, Font
, brush
);
369 if (carriage_return
) {
370 Line line
= document
.GetLine (i
+ 1);
371 line
.carriage_return
= true;
375 document
.NoRecalc
= false;
377 // CalculateDocument();
378 OnTextChanged(EventArgs
.Empty
);
382 [DefaultValue(32767)]
384 [MWFCategory("Behavior")]
385 public virtual int MaxLength
{
387 if (max_length
== 2147483646) { // We don't distinguish between single and multi-line limits
394 if (value != max_length
) {
401 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
402 public bool Modified
{
408 if (value != modified
) {
410 OnModifiedChanged(EventArgs
.Empty
);
415 [DefaultValue(false)]
417 [RefreshProperties(RefreshProperties
.All
)]
418 [MWFCategory("Behavior")]
419 public virtual bool Multiline
{
425 if (value != multiline
) {
427 // Make sure we update our size; the user may have already set the size before going to multiline
428 if (multiline
&& requested_height
!= -1) {
429 Height
= requested_height
;
430 requested_height
= -1;
434 Parent
.PerformLayout ();
436 OnMultilineChanged(EventArgs
.Empty
);
439 document
.multiline
= multiline
;
442 document
.Wrap
= word_wrap
;
443 document
.PasswordChar
= "";
446 document
.Wrap
= false;
447 if (this.password_char
!= '\0') {
448 document
.PasswordChar
= password_char
.ToString();
450 document
.PasswordChar
= "";
454 CalculateDocument ();
459 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
460 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
461 public int PreferredHeight
{
463 return (BorderStyle
== BorderStyle
.None
? 0 : Font
.Height
+ 7);
467 [DefaultValue(false)]
468 [MWFCategory("Behavior")]
469 public bool ReadOnly
{
475 if (value != read_only
) {
477 OnReadOnlyChanged(EventArgs
.Empty
);
483 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
484 public virtual string SelectedText
{
486 return document
.GetSelection();
490 document
.ReplaceSelection(CaseAdjust(value), false);
493 OnTextChanged(EventArgs
.Empty
);
498 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
499 public virtual int SelectionLength
{
501 int res
= document
.SelectionLength ();
509 throw new ArgumentException(String
.Format("{0} is not a valid value", value), "value");
518 selection_length
= value;
520 start
= document
.LineTagToCharIndex(document
.selection_start
.line
, document
.selection_start
.pos
);
522 document
.CharIndexToLineTag(start
+ value, out line
, out tag
, out pos
);
523 document
.SetSelectionEnd(line
, pos
);
524 document
.PositionCaret(line
, pos
);
526 selection_length
= -1;
528 document
.SetSelectionEnd(document
.selection_start
.line
, document
.selection_start
.pos
);
529 document
.PositionCaret(document
.selection_start
.line
, document
.selection_start
.pos
);
535 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
536 public int SelectionStart
{
540 index
= document
.LineTagToCharIndex(document
.selection_start
.line
, document
.selection_start
.pos
);
546 document
.SetSelectionStart(value);
547 if (selection_length
> -1 ) {
548 document
.SetSelectionEnd(value + selection_length
);
550 document
.SetSelectionEnd(value);
552 document
.PositionCaret(document
.selection_start
.line
, document
.selection_start
.pos
);
558 public override string Text
{
560 if (document
== null || document
.Root
== null || document
.Root
.text
== null) {
564 StringBuilder sb
= new StringBuilder();
567 for (int i
= 1; i
<= document
.Lines
; i
++) {
568 if (line
!= null && line
.carriage_return
)
570 if (line
!= null && !line
.soft_break
)
571 sb
.Append (Environment
.NewLine
);
572 line
= document
.GetLine (i
);
573 sb
.Append(line
.text
.ToString());
576 return sb
.ToString();
583 if ((value != null) && (value != "")) {
587 document
.Insert (document
.GetLine (1), 0, false, value);
589 document
.PositionCaret (document
.GetLine (1), 0);
590 document
.SetSelectionToCaret (true);
598 // set the var so OnModifiedChanged is not raised
600 OnTextChanged(EventArgs
.Empty
);
605 public virtual int TextLength
{
607 if (document
== null || document
.Root
== null || document
.Root
.text
== null) {
612 return document
.Root
.text
.Length
;
618 for (i
= 1; i
< document
.Lines
; i
++) {
619 total
+= document
.GetLine(i
).text
.Length
+ Environment
.NewLine
.Length
;
621 total
+= document
.GetLine(i
).text
.Length
;
630 [MWFCategory("Behavior")]
631 public bool WordWrap
{
637 if (value != word_wrap
) {
640 document
.Wrap
= value;
645 #endregion // Public Instance Properties
647 #region Protected Instance Properties
648 protected override CreateParams CreateParams
{
650 return base.CreateParams
;
654 protected override System
.Drawing
.Size DefaultSize
{
656 return new Size(100, 20);
659 #endregion // Protected Instance Properties
661 #region Public Instance Methods
662 public void AppendText(string text
) {
665 document
.MoveCaret(CaretDirection
.CtrlEnd
);
666 document
.Insert (document
.caret
.line
, /* document.caret.tag, */ document
.caret
.pos
, false, text
);
667 // CalculateDocument();
669 // document.MoveCaret(CaretDirection.CtrlEnd);
670 // document.InsertStringAtCaret(text, true);
675 document
.MoveCaret(CaretDirection
.CtrlEnd
);
676 document
.SetSelectionToCaret (true);
678 OnTextChanged(EventArgs
.Empty
);
681 public void Clear() {
685 public void ClearUndo() {
686 document
.undo
.Clear();
692 o
= new DataObject(DataFormats
.Text
, SelectedText
);
693 if (this is RichTextBox
) {
694 o
.SetData(DataFormats
.Rtf
, ((RichTextBox
)this).SelectedRtf
);
696 Clipboard
.SetDataObject(o
);
702 o
= new DataObject(DataFormats
.Text
, SelectedText
);
703 if (this is RichTextBox
) {
704 o
.SetData(DataFormats
.Rtf
, ((RichTextBox
)this).SelectedRtf
);
706 Clipboard
.SetDataObject(o
);
707 document
.ReplaceSelection("", false);
710 public void Paste() {
711 Paste(Clipboard
.GetDataObject(), null, false);
714 public void ScrollToCaret() {
715 if (IsHandleCreated
) {
716 CaretMoved(this, EventArgs
.Empty
);
720 public void Select(int start
, int length
) {
721 SelectionStart
= start
;
722 SelectionLength
= length
;
726 public void SelectAll() {
729 last
= document
.GetLine(document
.Lines
);
730 document
.SetSelectionStart(document
.GetLine(1), 0);
731 document
.SetSelectionEnd(last
, last
.text
.Length
);
732 document
.PositionCaret (document
.selection_end
.line
, document
.selection_end
.pos
);
733 selection_length
= -1;
736 public override string ToString() {
737 return String
.Concat (base.ToString (), ", Text: ", Text
);
741 document
.undo
.Undo();
743 #endregion // Public Instance Methods
745 #region Protected Instance Methods
746 protected override void CreateHandle() {
747 base.CreateHandle ();
748 document
.AlignCaret();
752 protected override bool IsInputKey(Keys keyData
) {
753 if ((keyData
& Keys
.Alt
) != 0) {
754 return base.IsInputKey(keyData
);
757 switch (keyData
& Keys
.KeyCode
) {
759 if (multiline
&& accepts_return
) {
766 if (accepts_tab
&& multiline
) {
767 if ((keyData
& Keys
.Control
) == 0) {
789 protected virtual void OnAcceptsTabChanged(EventArgs e
) {
790 EventHandler eh
= (EventHandler
)(Events
[AcceptsTabChangedEvent
]);
795 protected virtual void OnAutoSizeChanged(EventArgs e
) {
796 EventHandler eh
= (EventHandler
)(Events
[AutoSizeChangedEvent
]);
801 protected virtual void OnBorderStyleChanged(EventArgs e
) {
802 EventHandler eh
= (EventHandler
)(Events
[BorderStyleChangedEvent
]);
807 protected override void OnFontChanged(EventArgs e
) {
808 base.OnFontChanged (e
);
810 if (auto_size
&& !multiline
) {
811 if (PreferredHeight
!= ClientSize
.Height
) {
812 Height
= PreferredHeight
;
817 protected override void OnHandleCreated(EventArgs e
) {
818 base.OnHandleCreated (e
);
821 protected override void OnHandleDestroyed(EventArgs e
) {
822 base.OnHandleDestroyed (e
);
825 protected virtual void OnHideSelectionChanged(EventArgs e
) {
826 EventHandler eh
= (EventHandler
)(Events
[HideSelectionChangedEvent
]);
831 protected virtual void OnModifiedChanged(EventArgs e
) {
832 EventHandler eh
= (EventHandler
)(Events
[ModifiedChangedEvent
]);
837 protected virtual void OnMultilineChanged(EventArgs e
) {
838 EventHandler eh
= (EventHandler
)(Events
[MultilineChangedEvent
]);
843 protected virtual void OnReadOnlyChanged(EventArgs e
) {
844 EventHandler eh
= (EventHandler
)(Events
[ReadOnlyChangedEvent
]);
849 protected override bool ProcessDialogKey(Keys keyData
) {
850 return base.ProcessDialogKey(keyData
);
853 private bool ProcessKey(Keys keyData
) {
857 control
= (Control
.ModifierKeys
& Keys
.Control
) != 0;
858 shift
= (Control
.ModifierKeys
& Keys
.Shift
) != 0;
860 switch (keyData
& Keys
.KeyCode
) {
861 case Keys
.X
: { // Cut (Ctrl-X)
869 case Keys
.C
: { // Copy (Ctrl-C)
877 case Keys
.V
: { // Paste (Ctrl-V)
879 return Paste(Clipboard
.GetDataObject(), null, true);
884 case Keys
.Z
: { // Undo (Ctrl-Z)
892 case Keys
.A
: { // Select All (Ctrl-A)
902 document
.MoveCaret(CaretDirection
.WordBack
);
904 if (!document
.selection_visible
|| shift
) {
905 document
.MoveCaret(CaretDirection
.CharBack
);
907 document
.MoveCaret(CaretDirection
.SelectionStart
);
912 document
.SetSelectionToCaret(true);
914 document
.SetSelectionToCaret(false);
917 CaretMoved(this, null);
923 document
.MoveCaret(CaretDirection
.WordForward
);
925 if (!document
.selection_visible
|| shift
) {
926 document
.MoveCaret(CaretDirection
.CharForward
);
928 document
.MoveCaret(CaretDirection
.SelectionEnd
);
932 document
.SetSelectionToCaret(true);
934 document
.SetSelectionToCaret(false);
937 CaretMoved(this, null);
943 if (document
.CaretPosition
== 0) {
944 document
.MoveCaret(CaretDirection
.LineUp
);
946 document
.MoveCaret(CaretDirection
.Home
);
949 document
.MoveCaret(CaretDirection
.LineUp
);
952 if ((Control
.ModifierKeys
& Keys
.Shift
) == 0) {
953 document
.SetSelectionToCaret(true);
955 document
.SetSelectionToCaret(false);
958 CaretMoved(this, null);
964 if (document
.CaretPosition
== document
.CaretLine
.Text
.Length
) {
965 document
.MoveCaret(CaretDirection
.LineDown
);
967 document
.MoveCaret(CaretDirection
.End
);
970 document
.MoveCaret(CaretDirection
.LineDown
);
973 if ((Control
.ModifierKeys
& Keys
.Shift
) == 0) {
974 document
.SetSelectionToCaret(true);
976 document
.SetSelectionToCaret(false);
979 CaretMoved(this, null);
984 if ((Control
.ModifierKeys
& Keys
.Control
) != 0) {
985 document
.MoveCaret(CaretDirection
.CtrlHome
);
987 document
.MoveCaret(CaretDirection
.Home
);
990 if ((Control
.ModifierKeys
& Keys
.Shift
) == 0) {
991 document
.SetSelectionToCaret(true);
993 document
.SetSelectionToCaret(false);
996 CaretMoved(this, null);
1001 if ((Control
.ModifierKeys
& Keys
.Control
) != 0) {
1002 document
.MoveCaret(CaretDirection
.CtrlEnd
);
1004 document
.MoveCaret(CaretDirection
.End
);
1007 if ((Control
.ModifierKeys
& Keys
.Shift
) == 0) {
1008 document
.SetSelectionToCaret(true);
1010 document
.SetSelectionToCaret(false);
1013 CaretMoved(this, null);
1018 // ignoring accepts_return, fixes bug #76355
1019 if (!read_only
&& multiline
&& (accepts_return
|| (FindForm() != null && FindForm().AcceptButton
== null) || ((Control
.ModifierKeys
& Keys
.Control
) != 0))) {
1022 if (document
.selection_visible
) {
1023 document
.ReplaceSelection("", false);
1026 line
= document
.CaretLine
;
1028 document
.Split(document
.CaretLine
, document
.CaretTag
, document
.CaretPosition
, false);
1029 OnTextChanged(EventArgs
.Empty
);
1030 document
.UpdateView(line
, 2, 0);
1031 document
.MoveCaret(CaretDirection
.CharForward
);
1032 document
.SetSelectionToCaret(true);
1033 CaretMoved(this, null);
1040 if (!read_only
&& accepts_tab
&& multiline
) {
1041 document
.InsertChar(document
.CaretLine
, document
.CaretPosition
, '\t');
1042 if (document
.selection_visible
) {
1043 document
.ReplaceSelection("", false);
1045 document
.SetSelectionToCaret(true);
1047 OnTextChanged(EventArgs
.Empty
);
1048 CaretMoved(this, null);
1056 Paste(Clipboard
.GetDataObject(), null, true);
1065 // FIXME - need overwrite/insert toggle?
1070 if ((Control
.ModifierKeys
& Keys
.Control
) != 0) {
1071 document
.MoveCaret(CaretDirection
.CtrlPgUp
);
1073 document
.MoveCaret(CaretDirection
.PgUp
);
1078 case Keys
.PageDown
: {
1079 if ((Control
.ModifierKeys
& Keys
.Control
) != 0) {
1080 document
.MoveCaret(CaretDirection
.CtrlPgDn
);
1082 document
.MoveCaret(CaretDirection
.PgDn
);
1097 if (document
.selection_visible
) {
1098 document
.ReplaceSelection("", false);
1100 // DeleteChar only deletes on the line, doesn't do the combine
1101 if (document
.CaretPosition
== document
.CaretLine
.Text
.Length
) {
1102 if (document
.CaretLine
.LineNo
< document
.Lines
) {
1105 line
= document
.GetLine(document
.CaretLine
.LineNo
+ 1);
1106 document
.Combine(document
.CaretLine
, line
);
1107 document
.UpdateView(document
.CaretLine
, 2, 0);
1113 check_first
= document
.GetLine(document
.CaretLine
.LineNo
);
1114 check_second
= document
.GetLine(check_first
.line_no
+ 1);
1116 Console
.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first
.Y
, check_second
.Y
);
1119 // Caret doesn't move
1123 document
.DeleteChar(document
.CaretTag
, document
.CaretPosition
, true);
1127 end_pos
= document
.CaretPosition
;
1129 while ((end_pos
< document
.CaretLine
.Text
.Length
) && !Document
.IsWordSeparator(document
.CaretLine
.Text
[end_pos
])) {
1133 if (end_pos
< document
.CaretLine
.Text
.Length
) {
1136 document
.DeleteChars(document
.CaretTag
, document
.CaretPosition
, end_pos
- document
.CaretPosition
);
1141 OnTextChanged(EventArgs
.Empty
);
1142 document
.AlignCaret();
1143 document
.UpdateCaret();
1144 CaretMoved(this, null);
1151 private void HandleBackspace(bool control
) {
1154 fire_changed
= false;
1156 // delete only deletes on the line, doesn't do the combine
1157 if (document
.selection_visible
) {
1158 document
.ReplaceSelection("", false);
1159 fire_changed
= true;
1161 document
.SetSelectionToCaret(true);
1163 if (document
.CaretPosition
== 0) {
1164 if (document
.CaretLine
.LineNo
> 1) {
1168 line
= document
.GetLine(document
.CaretLine
.LineNo
- 1);
1169 new_caret_pos
= line
.text
.Length
;
1171 document
.Combine(line
, document
.CaretLine
);
1172 document
.UpdateView(line
, 1, 0);
1173 document
.PositionCaret(line
, new_caret_pos
);
1174 //document.MoveCaret(CaretDirection.CharForward);
1175 document
.UpdateCaret();
1176 fire_changed
= true;
1179 if (!control
|| document
.CaretPosition
== 0) {
1180 document
.DeleteChar(document
.CaretTag
, document
.CaretPosition
, false);
1181 document
.MoveCaret(CaretDirection
.CharBack
);
1185 start_pos
= document
.CaretPosition
- 1;
1187 while ((start_pos
> 0) && !Document
.IsWordSeparator(document
.CaretLine
.Text
[start_pos
- 1])) {
1190 document
.DeleteChars(document
.CaretTag
, start_pos
, document
.CaretPosition
- start_pos
);
1191 document
.PositionCaret(document
.CaretLine
, start_pos
);
1193 document
.UpdateCaret();
1194 fire_changed
= true;
1197 OnTextChanged(EventArgs
.Empty
);
1199 CaretMoved(this, null);
1202 protected override void SetBoundsCore(int x
, int y
, int width
, int height
, BoundsSpecified specified
) {
1203 // Make sure we don't get sized bigger than we want to be
1206 if (height
!= PreferredHeight
) {
1207 requested_height
= height
;
1208 height
= PreferredHeight
;
1209 specified
|= BoundsSpecified
.Height
;
1214 base.SetBoundsCore (x
, y
, width
, height
, specified
);
1217 protected override void WndProc(ref Message m
) {
1218 switch ((Msg
)m
.Msg
) {
1219 case Msg
.WM_KEYDOWN
: {
1220 if (ProcessKeyMessage(ref m
) || ProcessKey((Keys
)m
.WParam
.ToInt32() | XplatUI
.State
.ModifierKeys
)) {
1221 m
.Result
= IntPtr
.Zero
;
1231 if (ProcessKeyMessage(ref m
)) {
1232 m
.Result
= IntPtr
.Zero
;
1240 m
.Result
= IntPtr
.Zero
;
1242 ch
= m
.WParam
.ToInt32();
1245 HandleBackspace(true);
1246 } else if (ch
>= 32) {
1247 if (document
.selection_visible
) {
1248 document
.ReplaceSelection("", false);
1251 char c
= (char)m
.WParam
;
1252 switch (character_casing
) {
1253 case CharacterCasing
.Upper
:
1254 c
= Char
.ToUpper((char) m
.WParam
);
1256 case CharacterCasing
.Lower
:
1257 c
= Char
.ToLower((char) m
.WParam
);
1261 if (document
.Length
< max_length
) {
1262 document
.InsertCharAtCaret(c
, true);
1263 OnTextChanged(EventArgs
.Empty
);
1264 CaretMoved(this, null);
1266 XplatUI
.AudibleAlert();
1269 } else if (ch
== 8) {
1270 HandleBackspace(false);
1277 base.WndProc(ref m
);
1283 #endregion // Protected Instance Methods
1286 static object AcceptsTabChangedEvent
= new object ();
1287 static object AutoSizeChangedEvent
= new object ();
1288 static object BorderStyleChangedEvent
= new object ();
1289 static object HideSelectionChangedEvent
= new object ();
1290 static object ModifiedChangedEvent
= new object ();
1291 static object MultilineChangedEvent
= new object ();
1292 static object ReadOnlyChangedEvent
= new object ();
1293 static object HScrolledEvent
= new object ();
1294 static object VScrolledEvent
= new object ();
1296 public event EventHandler AcceptsTabChanged
{
1297 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
1298 remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
1301 public event EventHandler AutoSizeChanged
{
1302 add { Events.AddHandler (AutoSizeChangedEvent, value); }
1303 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
1306 public event EventHandler BorderStyleChanged
{
1307 add { Events.AddHandler (BorderStyleChangedEvent, value); }
1308 remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
1311 public event EventHandler HideSelectionChanged
{
1312 add { Events.AddHandler (HideSelectionChangedEvent, value); }
1313 remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
1316 public event EventHandler ModifiedChanged
{
1317 add { Events.AddHandler (ModifiedChangedEvent, value); }
1318 remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
1321 public event EventHandler MultilineChanged
{
1322 add { Events.AddHandler (MultilineChangedEvent, value); }
1323 remove { Events.RemoveHandler (MultilineChangedEvent, value); }
1326 public event EventHandler ReadOnlyChanged
{
1327 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
1328 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
1331 internal event EventHandler HScrolled
{
1332 add { Events.AddHandler (HScrolledEvent, value); }
1333 remove { Events.RemoveHandler (HScrolledEvent, value); }
1336 internal event EventHandler VScrolled
{
1337 add { Events.AddHandler (VScrolledEvent, value); }
1338 remove { Events.RemoveHandler (VScrolledEvent, value); }
1342 [EditorBrowsable(EditorBrowsableState
.Never
)]
1343 public new event EventHandler BackgroundImageChanged
{
1344 add { base.BackgroundImageChanged += value; }
1345 remove { base.BackgroundImageChanged -= value; }
1348 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
1349 public new event EventHandler Click
{
1350 add { base.Click += value; }
1351 remove { base.Click -= value; }
1354 // XXX should this not manipulate base.Paint?
1356 [EditorBrowsable(EditorBrowsableState
.Never
)]
1357 public new event PaintEventHandler Paint
;
1358 #endregion // Events
1360 #region Private Methods
1361 internal Document Document
{
1371 internal bool ShowSelection
{
1373 if (show_selection
|| !hide_selection
) {
1381 if (show_selection
== value)
1384 show_selection
= value;
1385 // Currently InvalidateSelectionArea is commented out so do a full invalidate
1386 document
.InvalidateSelectionArea();
1390 internal Graphics
CreateGraphicsInternal() {
1391 if (IsHandleCreated
) {
1392 return base.CreateGraphics();
1395 return Graphics
.FromImage(bmp
);
1402 internal override void OnPaintInternal (PaintEventArgs pevent
) {
1404 if (backcolor_set
|| (Enabled
&& !read_only
)) {
1405 pevent
.Graphics
.FillRectangle(ThemeEngine
.Current
.ResPool
.GetSolidBrush(BackColor
), pevent
.ClipRectangle
);
1407 pevent
.Graphics
.FillRectangle(ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorControl
), pevent
.ClipRectangle
);
1409 pevent
.Graphics
.TextRenderingHint
=TextRenderingHint
.AntiAlias
;
1411 // Draw the viewable document
1412 document
.Draw(pevent
.Graphics
, pevent
.ClipRectangle
);
1414 Rectangle rect
= ClientRectangle
;
1417 //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
1426 p
= new Pen(Color
.Red
, 1);
1428 // First, figure out from what line to what line we need to draw
1429 start
= document
.GetLineByPixel(pevent
.ClipRectangle
.Top
- document
.ViewPortY
, false).line_no
;
1430 end
= document
.GetLineByPixel(pevent
.ClipRectangle
.Bottom
- document
.ViewPortY
, false).line_no
;
1432 //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
1433 //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
1436 while (line_no
<= end
) {
1437 line
= document
.GetLine(line_no
);
1440 for (int i
= 0; i
< line
.text
.Length
; i
++) {
1441 pevent
.Graphics
.DrawLine(p
, (int)line
.widths
[i
] - document
.ViewPortX
, line
.Y
- document
.ViewPortY
, (int)line
.widths
[i
] - document
.ViewPortX
, line
.Y
+ line
.height
- document
.ViewPortY
);
1450 internal override void OnGotFocusInternal (EventArgs e
)
1452 document
.CaretHasFocus ();
1453 base.OnGotFocusInternal (e
);
1456 internal override void OnLostFocusInternal (EventArgs e
)
1458 document
.CaretLostFocus ();
1459 base.OnLostFocusInternal (e
);
1462 private bool IsDoubleClick (MouseEventArgs e
)
1464 TimeSpan interval
= DateTime
.Now
- click_last
;
1465 if (interval
.TotalMilliseconds
> SystemInformation
.DoubleClickTime
)
1467 Size dcs
= SystemInformation
.DoubleClickSize
;
1468 if (e
.X
< click_point_x
- dcs
.Width
/ 2 || e
.X
> click_point_x
+ dcs
.Width
/ 2)
1470 if (e
.Y
< click_point_y
- dcs
.Height
/ 2 || e
.Y
> click_point_y
+ dcs
.Height
/ 2)
1475 private void TextBoxBase_MouseDown (object sender
, MouseEventArgs e
)
1477 if (e
.Button
== MouseButtons
.Left
) {
1479 document
.PositionCaret(e
.X
+ document
.ViewPortX
, e
.Y
+ document
.ViewPortY
);
1481 if (IsDoubleClick (e
)) {
1482 switch (click_mode
) {
1483 case CaretSelection
.Position
:
1485 click_mode
= CaretSelection
.Word
;
1487 case CaretSelection
.Word
:
1489 if (this is TextBox
) {
1490 document
.SetSelectionToCaret (true);
1491 click_mode
= CaretSelection
.Position
;
1495 document
.ExpandSelection (CaretSelection
.Line
, false);
1496 click_mode
= CaretSelection
.Line
;
1498 case CaretSelection
.Line
:
1500 // Gotta do this first because Exanding to a word
1501 // from a line doesn't really work
1502 document
.SetSelectionToCaret (true);
1505 click_mode
= CaretSelection
.Word
;
1509 document
.SetSelectionToCaret (true);
1510 click_mode
= CaretSelection
.Position
;
1513 click_point_x
= e
.X
;
1514 click_point_y
= e
.Y
;
1515 click_last
= DateTime
.Now
;
1518 if ((e
.Button
== MouseButtons
.Middle
) && (((int)Environment
.OSVersion
.Platform
== 4) || ((int)Environment
.OSVersion
.Platform
== 128))) {
1519 Document
.Marker marker
;
1521 marker
.tag
= document
.FindCursor(e
.X
+ document
.ViewPortX
, e
.Y
+ document
.ViewPortY
, out marker
.pos
);
1522 marker
.line
= marker
.tag
.line
;
1523 marker
.height
= marker
.tag
.height
;
1525 document
.SetSelection(marker
.line
, marker
.pos
, marker
.line
, marker
.pos
);
1526 Paste (Clipboard
.GetDataObject (true), null, true);
1531 private void TextBoxBase_MouseUp(object sender
, MouseEventArgs e
) {
1532 if (e
.Button
== MouseButtons
.Left
) {
1533 if (click_mode
== CaretSelection
.Position
) {
1534 document
.SetSelectionToCaret(false);
1535 document
.DisplayCaret();
1538 if (scroll_timer
!= null) {
1539 scroll_timer
.Enabled
= false;
1545 private void PositionControls ()
1547 if (hscroll
.Visible
) {
1548 //vscroll.Maximum += hscroll.Height;
1549 canvas_height
= ClientSize
.Height
- hscroll
.Height
;
1551 canvas_height
= ClientSize
.Height
;
1554 if (vscroll
.Visible
) {
1555 //hscroll.Maximum += vscroll.Width;
1556 canvas_width
= ClientSize
.Width
- vscroll
.Width
;
1558 canvas_width
= ClientSize
.Width
;
1562 document
.ViewPortWidth
= canvas_width
;
1563 document
.ViewPortHeight
= canvas_height
;
1565 if (canvas_height
< 1 || canvas_width
< 1)
1568 // We always move them, they just might not be displayed
1569 hscroll
.Bounds
= new Rectangle (ClientRectangle
.Left
,
1570 Math
.Max (0, ClientRectangle
.Height
- hscroll
.Height
),
1571 Math
.Max (0, ClientSize
.Width
- (vscroll
.Visible
? vscroll
.Width
: 0)),
1574 vscroll
.Bounds
= new Rectangle (Math
.Max (0, ClientRectangle
.Right
- vscroll
.Width
),
1575 ClientRectangle
.Top
, vscroll
.Width
,
1576 Math
.Max (0, ClientSize
.Height
- (hscroll
.Visible
? hscroll
.Height
: 0)));
1580 private void TextBoxBase_SizeChanged(object sender
, EventArgs e
) {
1581 CalculateDocument();
1584 private void TextBoxBase_MouseWheel(object sender
, MouseEventArgs e
) {
1586 if (!vscroll
.Enabled
) {
1591 vscroll
.Value
= Math
.Min (vscroll
.Value
+ SystemInformation
.MouseWheelScrollLines
,
1592 Math
.Max (0, vscroll
.Maximum
- document
.ViewPortHeight
+ 1));
1594 vscroll
.Value
= Math
.Max (0, vscroll
.Value
- SystemInformation
.MouseWheelScrollLines
);
1597 internal virtual void SelectWord ()
1599 StringBuilder s
= document
.caret
.line
.text
;
1600 int start
= document
.caret
.pos
;
1601 int end
= document
.caret
.pos
;
1604 if (document
.caret
.line
.line_no
>= document
.Lines
)
1606 Line line
= document
.GetLine (document
.caret
.line
.line_no
+ 1);
1607 document
.PositionCaret (line
, 0);
1616 // skip whitespace until we hit a word
1617 while (start
> 0 && s
[start
] == ' ')
1620 while (start
> 0 && (s
[start
] != ' '))
1622 if (s
[start
] == ' ')
1626 if (s
[end
] == ' ') {
1627 while (end
< s
.Length
&& s
[end
] == ' ')
1630 while (end
< s
.Length
&& s
[end
] != ' ')
1632 while (end
< s
.Length
&& s
[end
] == ' ')
1636 document
.SetSelection (document
.caret
.line
, start
, document
.caret
.line
, end
);
1637 document
.PositionCaret (document
.selection_end
.line
, document
.selection_end
.pos
);
1640 internal void CalculateDocument() {
1641 if (!IsHandleCreated
) {
1644 document
.RecalculateDocument(CreateGraphicsInternal());
1645 CalculateScrollBars();
1647 if (document
.caret
.line
!= null && document
.caret
.line
.Y
< document
.ViewPortHeight
) {
1648 // The window has probably been resized, making the entire thing visible, so
1649 // we need to set the scroll position back to zero.
1656 internal void CalculateScrollBars () {
1657 // FIXME - need separate calculations for center and right alignment
1660 PositionControls ();
1664 if (document
.Width
>= document
.ViewPortWidth
) {
1665 hscroll
.SetValues (0, Math
.Max (1, document
.Width
), -1,
1666 document
.ViewPortWidth
< 0 ? 0 : document
.ViewPortWidth
);
1667 hscroll
.Enabled
= true;
1669 hscroll
.Enabled
= false;
1670 hscroll
.Maximum
= document
.ViewPortWidth
;
1673 if (document
.Height
>= document
.ViewPortHeight
) {
1674 vscroll
.SetValues (0, Math
.Max (1, document
.Height
), -1,
1675 document
.ViewPortHeight
< 0 ? 0 : document
.ViewPortHeight
);
1676 vscroll
.Enabled
= true;
1678 vscroll
.Enabled
= false;
1679 vscroll
.Maximum
= document
.ViewPortHeight
;
1684 if ((scrollbars
& RichTextBoxScrollBars
.Horizontal
) != 0) {
1685 if (((scrollbars
& RichTextBoxScrollBars
.ForcedHorizontal
) != 0) || hscroll
.Enabled
) {
1686 hscroll
.Visible
= true;
1688 hscroll
.Visible
= false;
1691 hscroll
.Visible
= false;
1695 if ((scrollbars
& RichTextBoxScrollBars
.Vertical
) != 0) {
1696 if (((scrollbars
& RichTextBoxScrollBars
.ForcedVertical
) != 0) || vscroll
.Enabled
) {
1697 vscroll
.Visible
= true;
1699 vscroll
.Visible
= false;
1702 vscroll
.Visible
= false;
1705 PositionControls ();
1708 private void document_WidthChanged(object sender
, EventArgs e
) {
1709 CalculateScrollBars();
1712 private void document_HeightChanged(object sender
, EventArgs e
) {
1713 CalculateScrollBars();
1716 private void hscroll_ValueChanged(object sender
, EventArgs e
) {
1719 old_viewport_x
= document
.ViewPortX
;
1720 document
.ViewPortX
= this.hscroll
.Value
;
1722 if (vscroll
.Visible
) {
1723 XplatUI
.ScrollWindow(this.Handle
, new Rectangle(0, 0, ClientSize
.Width
- vscroll
.Width
, ClientSize
.Height
), old_viewport_x
- this.hscroll
.Value
, 0, false);
1725 XplatUI
.ScrollWindow(this.Handle
, ClientRectangle
, old_viewport_x
- this.hscroll
.Value
, 0, false);
1727 document
.UpdateCaret();
1729 EventHandler eh
= (EventHandler
)(Events
[HScrolledEvent
]);
1731 eh (this, EventArgs
.Empty
);
1734 private void vscroll_ValueChanged(object sender
, EventArgs e
) {
1737 old_viewport_y
= document
.ViewPortY
;
1738 document
.ViewPortY
= this.vscroll
.Value
;
1740 if (hscroll
.Visible
) {
1741 XplatUI
.ScrollWindow(this.Handle
, new Rectangle(0, 0, ClientSize
.Width
, ClientSize
.Height
- hscroll
.Height
), 0, old_viewport_y
- this.vscroll
.Value
, false);
1743 XplatUI
.ScrollWindow(this.Handle
, ClientRectangle
, 0, old_viewport_y
- this.vscroll
.Value
, false);
1745 document
.UpdateCaret();
1747 EventHandler eh
= (EventHandler
)(Events
[VScrolledEvent
]);
1749 eh (this, EventArgs
.Empty
);
1752 private void TextBoxBase_MouseMove(object sender
, MouseEventArgs e
) {
1753 // FIXME - handle auto-scrolling if mouse is to the right/left of the window
1754 if (e
.Button
== MouseButtons
.Left
&& Capture
) {
1755 if (!ClientRectangle
.Contains (e
.X
, e
.Y
)) {
1756 if (scroll_timer
== null) {
1757 scroll_timer
= new Timer ();
1758 scroll_timer
.Interval
= 100;
1759 scroll_timer
.Tick
+= new EventHandler (ScrollTimerTickHandler
);
1762 if (!scroll_timer
.Enabled
) {
1763 scroll_timer
.Start ();
1765 // Force the first tick
1766 ScrollTimerTickHandler (null, EventArgs
.Empty
);
1772 document
.PositionCaret(e
.X
+ document
.ViewPortX
, e
.Y
+ document
.ViewPortY
);
1773 if (click_mode
== CaretSelection
.Position
) {
1774 document
.SetSelectionToCaret(false);
1775 document
.DisplayCaret();
1780 private void TextBoxBase_FontOrColorChanged(object sender
, EventArgs e
) {
1784 document
.NoRecalc
= true;
1785 // Font changes apply to the whole document
1786 for (int i
= 1; i
<= document
.Lines
; i
++) {
1787 line
= document
.GetLine(i
);
1788 LineTag
.FormatText(line
, 1, line
.text
.Length
, Font
,
1789 ThemeEngine
.Current
.ResPool
.GetSolidBrush(ForeColor
),
1790 null, FormatSpecified
.Font
| FormatSpecified
.Color
);
1792 document
.UpdateView (document
.GetLine (1), 0);
1793 document
.NoRecalc
= false;
1794 // Make sure the caret height is matching the new font height
1795 document
.AlignCaret();
1799 private void ScrollTimerTickHandler (object sender
, EventArgs e
)
1801 Point pt
= Cursor
.Position
;
1803 pt
= PointToClient (pt
);
1805 if (pt
.X
< ClientRectangle
.Left
) {
1806 document
.MoveCaret(CaretDirection
.CharBackNoWrap
);
1807 document
.SetSelectionToCaret(false);
1809 CaretMoved(this, null);
1810 } else if (pt
.X
> ClientRectangle
.Right
) {
1811 document
.MoveCaret(CaretDirection
.CharForwardNoWrap
);
1812 document
.SetSelectionToCaret(false);
1814 CaretMoved(this, null);
1815 } else if (pt
.Y
> ClientRectangle
.Bottom
) {
1816 document
.MoveCaret(CaretDirection
.LineDown
);
1817 document
.SetSelectionToCaret(false);
1819 CaretMoved(this, null);
1820 } else if (pt
.Y
< ClientRectangle
.Top
) {
1821 document
.MoveCaret(CaretDirection
.LineUp
);
1822 document
.SetSelectionToCaret(false);
1824 CaretMoved(this, null);
1828 /// <summary>Ensure the caret is always visible</summary>
1829 internal void CaretMoved(object sender
, EventArgs e
) {
1833 if (canvas_width
< 1 || canvas_height
< 1)
1836 pos
= document
.Caret
;
1838 //Console.WriteLine("Caret now at {0} (Thumb: {1}x{2}, Canvas: {3}x{4}, Document {5}x{6})", pos, hscroll.Value, vscroll.Value, canvas_width, canvas_height, document.Width, document.Height);
1841 // Horizontal scrolling:
1842 // If the caret moves to the left outside the visible area, we jump the document into view, not just one
1843 // character, but 1/3 of the width of the document
1844 // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
1846 // Handle horizontal scrolling
1847 if (document
.CaretLine
.alignment
== HorizontalAlignment
.Left
) {
1848 // Check if we moved out of view to the left
1849 if (pos
.X
< (document
.ViewPortX
)) {
1851 if ((hscroll
.Value
- document
.ViewPortWidth
/ 3) >= hscroll
.Minimum
) {
1852 hscroll
.Value
-= document
.ViewPortWidth
/ 3;
1854 hscroll
.Value
= hscroll
.Minimum
;
1856 } while (hscroll
.Value
> pos
.X
);
1859 // Check if we moved out of view to the right
1860 if ((pos
.X
>= (document
.ViewPortWidth
+ document
.ViewPortX
)) && (hscroll
.Value
!= hscroll
.Maximum
)) {
1861 if ((pos
.X
- document
.ViewPortWidth
+ 1) <= hscroll
.Maximum
) {
1862 if (pos
.X
- document
.ViewPortWidth
>= 0) {
1863 hscroll
.Value
= pos
.X
- document
.ViewPortWidth
+ 1;
1868 hscroll
.Value
= hscroll
.Maximum
;
1871 } else if (document
.CaretLine
.alignment
== HorizontalAlignment
.Right
) {
1872 // hscroll.Value = pos.X;
1874 // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
1875 // hscroll.Value = hscroll.Maximum;
1878 // FIXME - implement center cursor alignment
1885 // Handle vertical scrolling
1886 height
= document
.CaretLine
.Height
+ 1;
1888 if (pos
.Y
< document
.ViewPortY
) {
1889 vscroll
.Value
= pos
.Y
;
1892 if ((pos
.Y
+ height
) > (document
.ViewPortY
+ canvas_height
)) {
1893 vscroll
.Value
= Math
.Max (0, pos
.Y
- canvas_height
+ height
);
1897 internal bool Paste(IDataObject clip
, DataFormats
.Format format
, bool obey_length
) {
1903 if (format
== null) {
1904 if ((this is RichTextBox
) && clip
.GetDataPresent(DataFormats
.Rtf
)) {
1905 format
= DataFormats
.GetFormat(DataFormats
.Rtf
);
1906 } else if (clip
.GetDataPresent(DataFormats
.UnicodeText
)) {
1907 format
= DataFormats
.GetFormat(DataFormats
.UnicodeText
);
1908 } else if (clip
.GetDataPresent(DataFormats
.Text
)) {
1909 format
= DataFormats
.GetFormat(DataFormats
.Text
);
1914 if ((format
.Name
== DataFormats
.Rtf
) && !(this is RichTextBox
)) {
1918 if (!clip
.GetDataPresent(format
.Name
)) {
1923 if (format
.Name
== DataFormats
.Rtf
) {
1924 ((RichTextBox
)this).SelectedRtf
= (string)clip
.GetData(DataFormats
.Rtf
);
1926 } else if (format
.Name
== DataFormats
.UnicodeText
) {
1927 s
= (string)clip
.GetData(DataFormats
.UnicodeText
);
1928 } else if (format
.Name
== DataFormats
.Text
) {
1929 s
= (string)clip
.GetData(DataFormats
.Text
);
1935 this.SelectedText
= s
;
1937 if ((s
.Length
+ document
.Length
) < max_length
) {
1938 this.SelectedText
= s
;
1939 } else if (document
.Length
< max_length
) {
1940 this.SelectedText
= s
.Substring(0, max_length
- document
.Length
);
1946 #endregion // Private Methods
1949 protected override void OnTextChanged (EventArgs e
)
1951 base.OnTextChanged (e
);