public void SelectAll() { this.SetPosition(0, 0, true, false); DiffViewPosition endPos = this.GetDocumentEndPosition(); this.SetSelectionEnd(endPos.Line, endPos.Column, false); }
protected override void OnMouseDown(MouseEventArgs e) { if (!this.Focused && this.CanFocus) { this.Focus(); } if (e.X >= this.gutterWidth) { DiffViewPosition pos = this.GetPosFromPoint(e.X, e.Y); // Only change pos if non-right-click or right-click not in selection if (e.Button != MouseButtons.Right || !this.InSelection(pos)) { this.SetPosition(pos.Line, pos.Column); } if (e.Button == MouseButtons.Left) { this.Capture = true; this.capturedMouse = true; } } base.OnMouseDown(e); }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); // Update the mouse cursor to be an IBeam if we're not in the gutter. this.Cursor = (e.X < this.gutterWidth) ? Cursors.Default : Cursors.IBeam; if (this.capturedMouse && e.Button == MouseButtons.Left) { // Determine if we're at or above the first visible line // or at or below the last visible line. If so, then // auto-scroll. Similarly, if we're on the first or last // character or beyond, then auto-scroll. Rectangle r = new(this.gutterWidth, 0, this.ClientSize.Width, this.ClientSize.Height); r.Inflate(-this.charWidth, -this.lineHeight); if (!r.Contains(e.X, e.Y)) { this.verticalAutoScrollAmount = 0; if (e.Y < r.Y) { this.verticalAutoScrollAmount = -1; } else if (e.Y > r.Bottom) { this.verticalAutoScrollAmount = 1; } this.horizontalAutoScrollAmount = 0; if (e.X < r.X) { this.horizontalAutoScrollAmount = -1; } else if (e.X > r.Right) { this.horizontalAutoScrollAmount = 1; } this.autoScrollTimer.Enabled = true; } else { this.autoScrollTimer.Enabled = false; } // Set the selection end to the current mouse position // if the new position is different from the caret position. DiffViewPosition pos = this.GetPosFromPoint(e.X, e.Y); if (pos != this.position) { this.SetSelectionEnd(pos.Line, pos.Column); } } }
protected override void OnDoubleClick(EventArgs e) { // Select the identifier at the double-clicked position. DiffViewPosition pos = this.Position; int startColumn = this.FindCurrentTokenStart(pos, true); int endColumn = this.FindCurrentTokenEnd(pos, false); this.SetPosition(pos.Line, startColumn); if (endColumn != startColumn) { this.SetSelectionEnd(pos.Line, endColumn); } base.OnDoubleClick(e); }
public void ScrollToCaret() { if (this.caret != null) { // Assume the caret is always at this.Position. // It would be nice if we had: // Debug.Assert(this.Position.Line == CaretPos.Line && this.Position.Column == CaretPos.Column); // but that fails on occasion because of rounding problems // between calling GetPointFromPos and then GetPosFromPoint. DiffViewPosition caretPos = this.position; Point caretPoint = this.GetPointFromPos(caretPos.Line, caretPos.Column); // Make sure that position is on the screen by // scrolling the minimal number of lines and characters. int firstVisibleLine = this.FirstVisibleLine; int lastVisibleLine = firstVisibleLine + this.VisibleLineCount - 1; if (caretPos.Line < firstVisibleLine) { this.VScrollPos -= firstVisibleLine - caretPos.Line; } else if (caretPos.Line > lastVisibleLine) { this.VScrollPos += caretPos.Line - lastVisibleLine; } // This is tricky because we might not have a monospaced font. // We have to figure out the number of pixels we need to scroll // and then translate that into characters (i.e. CharWidths). int firstVisibleX = this.gutterWidth - GutterSeparatorWidth; int lastVisibleX = this.ClientSize.Width - this.caret.Size.Width; if (caretPoint.X < firstVisibleX) { int scrollPixels = caretPoint.X - firstVisibleX; this.HScrollPos += (int)Math.Floor(scrollPixels / (double)this.charWidth); } else if (caretPoint.X > lastVisibleX) { int scrollPixels = caretPoint.X - lastVisibleX; this.HScrollPos += (int)Math.Ceiling(scrollPixels / (double)this.charWidth); } } }
protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); bool ctrlShiftPressed = e.Modifiers == (Keys.Control | Keys.Shift); bool ctrlPressed = e.Modifiers == Keys.Control; bool shiftPressed = e.Modifiers == Keys.Shift; bool noModifierPressed = e.Modifiers == 0; if (ctrlShiftPressed || ctrlPressed || shiftPressed || noModifierPressed) { switch (e.KeyCode) { case Keys.A: if (ctrlPressed) { this.SelectAll(); } break; case Keys.C: if (ctrlPressed && this.HasSelection) { Clipboard.SetDataObject(this.SelectedText, true); } break; case Keys.Up: this.HandleArrowUpDown(-1, ctrlPressed, shiftPressed, noModifierPressed); break; case Keys.Down: this.HandleArrowUpDown(+1, ctrlPressed, shiftPressed, noModifierPressed); break; case Keys.Left: this.HandleArrowLeft(ctrlShiftPressed, ctrlPressed, shiftPressed, noModifierPressed); break; case Keys.Right: this.HandleArrowRight(ctrlShiftPressed, ctrlPressed, shiftPressed, noModifierPressed); break; case Keys.PageUp: this.HandlePageUpDown(-1, shiftPressed, noModifierPressed); break; case Keys.PageDown: this.HandlePageUpDown(+1, shiftPressed, noModifierPressed); break; case Keys.Home: if (ctrlShiftPressed) { this.SetSelectionEnd(0, 0); } else if (ctrlPressed) { this.SetPosition(0, 0); } else if (shiftPressed) { this.ExtendSelection(0, -this.Position.Column); } else if (noModifierPressed) { this.SetPosition(this.Position.Line, 0); } break; case Keys.End: if (ctrlShiftPressed) { DiffViewPosition endPos = this.GetDocumentEndPosition(); this.SetSelectionEnd(endPos.Line, endPos.Column); } else if (ctrlPressed) { DiffViewPosition endPos = this.GetDocumentEndPosition(); this.SetPosition(endPos.Line, endPos.Column); } else if (shiftPressed) { this.ExtendSelection(0, this.GetDisplayLineLength(this.Position.Line) - this.Position.Column); } else if (noModifierPressed) { int line = this.Position.Line; this.SetPosition(line, this.GetDisplayLineLength(line)); } break; } } }
public bool FindPrevious(FindData data) { bool result = false; if (string.IsNullOrEmpty(data.Text)) { data.SearchUp = true; result = this.Find(data); } else { int numLines = this.LineCount; if (numLines > 0) { string text = data.Text; if (!data.MatchCase) { text = text.ToUpper(); } DiffViewPosition startPosition = this.GetFindStartPosition(true); int lastLineLastColumn = startPosition.Column; // Use <= so we check the start line again from the end for (int i = 0; i <= numLines; i++) { // Use % so we wrap around at the end. int lineNumber = (startPosition.Line - i + numLines) % numLines; // This needs to search the original text. DisplayLine displayLine = this.GetDisplayLine(lineNumber); string line = displayLine.OriginalText; if (!data.MatchCase) { line = line.ToUpper(); } const int StartAtEndColumn = -1; if (startPosition.Column == StartAtEndColumn) { startPosition = new DiffViewPosition(startPosition.Line, Math.Max(0, displayLine.GetDisplayTextLength())); } int index; if (i == numLines) { // We're rechecking the start line from the end. int startIndex = displayLine.GetTextIndexFromDisplayColumn(startPosition.Column); int lastIndex = displayLine.GetTextIndexFromDisplayColumn(lastLineLastColumn); index = line.LastIndexOf(text, startIndex, startIndex - lastIndex + 1); } else { index = line.LastIndexOf(text, displayLine.GetTextIndexFromDisplayColumn(startPosition.Column)); } if (index >= 0) { this.GoToPosition(lineNumber, displayLine.GetDisplayColumnFromTextIndex(index)); this.ExtendSelection(0, text.Length); result = true; break; } // On all lines but the first, we need to start at the end startPosition = new DiffViewPosition(startPosition.Line, StartAtEndColumn); } } if (!result) { string message = string.Format("'{0}' was not found.", data.Text); MessageBox.Show(this, message, nameof(this.Find), MessageBoxButtons.OK, MessageBoxIcon.Information); } } return(result); }