private void TextBoxBase_MouseDown (object sender, MouseEventArgs e) { bool dbliclick = false; if (e.Button == MouseButtons.Left) { // Special case when shift key is pressed and // left mouse is clicked.. set selection from // current cursor to mouse if ((Control.ModifierKeys & Keys.Shift) > 0) { document.PositionCaret (e.X + document.ViewPortX, e.Y + document.ViewPortY); document.SetSelectionToCaret (false); document.DisplayCaret (); return; } dbliclick = IsDoubleClick (e); if (current_link != null) { HandleLinkClicked (current_link); return; } //ensure nothing is selected anymore BEFORE we //position the caret, so the caret is recreated //(caret is only visible when nothing is selected) if (document.selection_visible && dbliclick == false) { document.SetSelectionToCaret (true); click_mode = CaretSelection.Position; } document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY); if (dbliclick) { switch (click_mode) { case CaretSelection.Position: SelectWord (); click_mode = CaretSelection.Word; break; case CaretSelection.Word: if (this is TextBox) { document.SetSelectionToCaret (true); click_mode = CaretSelection.Position; break; } document.ExpandSelection (CaretSelection.Line, false); click_mode = CaretSelection.Line; break; case CaretSelection.Line: // Gotta do this first because Exanding to a word // from a line doesn't really work document.SetSelectionToCaret (true); SelectWord (); click_mode = CaretSelection.Word; break; } } else { document.SetSelectionToCaret (true); click_mode = CaretSelection.Position; } click_point_x = e.X; click_point_y = e.Y; click_last = DateTime.Now; } if ((e.Button == MouseButtons.Middle) && XplatUI.RunningOnUnix) { Document.Marker marker; marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos); marker.line = marker.tag.Line; marker.height = marker.tag.Height; document.SetSelection(marker.line, marker.pos, marker.line, marker.pos); Paste (Clipboard.GetDataObject (true), null, true); } }
// Constructor will go when complete, only for testing - pdb internal TextBoxBase () { alignment = HorizontalAlignment.Left; accepts_return = false; accepts_tab = false; auto_size = true; InternalBorderStyle = BorderStyle.Fixed3D; actual_border_style = BorderStyle.Fixed3D; character_casing = CharacterCasing.Normal; hide_selection = true; max_length = short.MaxValue; password_char = '\0'; read_only = false; word_wrap = true; richtext = false; show_selection = false; enable_links = false; list_links = new ArrayList (); current_link = null; show_caret_w_selection = (this is TextBox); document = new Document(this); document.WidthChanged += new EventHandler(document_WidthChanged); document.HeightChanged += new EventHandler(document_HeightChanged); //document.CaretMoved += new EventHandler(CaretMoved); document.Wrap = false; click_last = DateTime.Now; click_mode = CaretSelection.Position; MouseDown += new MouseEventHandler(TextBoxBase_MouseDown); MouseUp += new MouseEventHandler(TextBoxBase_MouseUp); MouseMove += new MouseEventHandler(TextBoxBase_MouseMove); SizeChanged += new EventHandler(TextBoxBase_SizeChanged); FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged); ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged); MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel); RightToLeftChanged += new EventHandler (TextBoxBase_RightToLeftChanged); scrollbars = RichTextBoxScrollBars.None; hscroll = new ImplicitHScrollBar(); hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged); hscroll.SetStyle (ControlStyles.Selectable, false); hscroll.Enabled = false; hscroll.Visible = false; hscroll.Maximum = Int32.MaxValue; vscroll = new ImplicitVScrollBar(); vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged); vscroll.SetStyle (ControlStyles.Selectable, false); vscroll.Enabled = false; vscroll.Visible = false; vscroll.Maximum = Int32.MaxValue; SuspendLayout (); this.Controls.AddImplicit (hscroll); this.Controls.AddImplicit (vscroll); ResumeLayout (); SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false); #if NET_2_0 SetStyle(ControlStyles.UseTextForAccessibility, false); base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink); #endif canvas_width = ClientSize.Width; canvas_height = ClientSize.Height; document.ViewPortWidth = canvas_width; document.ViewPortHeight = canvas_height; Cursor = Cursors.IBeam; }
/// <summary>Select text around caret</summary> internal void ExpandSelection(CaretSelection mode, bool to_caret) { if (to_caret) { // We're expanding the selection to the caret position switch(mode) { case CaretSelection.Line: { // Invalidate the selection delta if (caret > selection_prev) { Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length); } else { Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0); } if (caret.line.line_no <= selection_anchor.line.line_no) { selection_start.line = caret.line; selection_start.tag = caret.line.tags; selection_start.pos = 0; selection_end.line = selection_anchor.line; selection_end.tag = selection_anchor.tag; selection_end.pos = selection_anchor.pos; selection_end_anchor = true; } else { selection_start.line = selection_anchor.line; selection_start.pos = selection_anchor.height; selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1); selection_end.line = caret.line; selection_end.tag = caret.line.tags; selection_end.pos = caret.line.text.Length; selection_end_anchor = false; } selection_prev.line = caret.line; selection_prev.tag = caret.tag; selection_prev.pos = caret.pos; break; } case CaretSelection.Word: { int start_pos; int end_pos; start_pos = FindWordSeparator(caret.line, caret.pos, false); end_pos = FindWordSeparator(caret.line, caret.pos, true); // Invalidate the selection delta if (caret > selection_prev) { Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos); } else { Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos); } if (caret < selection_anchor) { selection_start.line = caret.line; selection_start.tag = caret.line.FindTag(start_pos + 1); selection_start.pos = start_pos; selection_end.line = selection_anchor.line; selection_end.tag = selection_anchor.tag; selection_end.pos = selection_anchor.pos; selection_prev.line = caret.line; selection_prev.tag = caret.tag; selection_prev.pos = start_pos; selection_end_anchor = true; } else { selection_start.line = selection_anchor.line; selection_start.pos = selection_anchor.height; selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1); selection_end.line = caret.line; selection_end.tag = caret.line.FindTag(end_pos); selection_end.pos = end_pos; selection_prev.line = caret.line; selection_prev.tag = caret.tag; selection_prev.pos = end_pos; selection_end_anchor = false; } break; } case CaretSelection.Position: { SetSelectionToCaret(false); return; } } } else { // We're setting the selection 'around' the caret position switch(mode) { case CaretSelection.Line: { this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length); selection_start.line = caret.line; selection_start.tag = caret.line.tags; selection_start.pos = 0; selection_end.line = caret.line; selection_end.pos = caret.line.text.Length; selection_end.tag = caret.line.FindTag(selection_end.pos); selection_anchor.line = selection_end.line; selection_anchor.tag = selection_end.tag; selection_anchor.pos = selection_end.pos; selection_anchor.height = 0; selection_prev.line = caret.line; selection_prev.tag = caret.tag; selection_prev.pos = caret.pos; this.selection_end_anchor = true; break; } case CaretSelection.Word: { int start_pos; int end_pos; start_pos = FindWordSeparator(caret.line, caret.pos, false); end_pos = FindWordSeparator(caret.line, caret.pos, true); this.Invalidate(selection_start.line, start_pos, caret.line, end_pos); selection_start.line = caret.line; selection_start.tag = caret.line.FindTag(start_pos + 1); selection_start.pos = start_pos; selection_end.line = caret.line; selection_end.tag = caret.line.FindTag(end_pos); selection_end.pos = end_pos; selection_anchor.line = selection_end.line; selection_anchor.tag = selection_end.tag; selection_anchor.pos = selection_end.pos; selection_anchor.height = start_pos; selection_prev.line = caret.line; selection_prev.tag = caret.tag; selection_prev.pos = caret.pos; this.selection_end_anchor = true; break; } } } SetSelectionVisible (!(selection_start == selection_end)); }