public Point GetPointFromPos(int line, int column) { int y = (line - this.VScrollPos) * this.lineHeight; // Because we're not guaranteed to have a monospaced font, // this gets tricky. We have to measure the substring to // get the correct X. DisplayLine displayLine = this.GetDisplayLine(line); using (Graphics g = Graphics.FromHwnd(this.Handle)) { int x = this.GetXForColumn(g, displayLine, null, column); return(new Point(x, y)); } }
public DiffViewPosition GetPosFromPoint(int x, int y) { int line = (y / this.lineHeight) + this.VScrollPos; // Because we're not guaranteed to have a monospaced font, // this gets tricky. We have to make an initial guess at // the column, and then we'll converge to the best one. DisplayLine displayLine = this.GetDisplayLine(line); string text = displayLine.GetDisplayText(); // Make a starting guess. Because of tabs and variable width // fonts, this may be nowhere near the right place... int column = (int)((x - this.gutterWidth + (this.HScrollPos * this.charWidth)) / this.charWidth); using (Graphics g = Graphics.FromHwnd(this.Handle)) { int textLength = text.Length; int columnGreater = -1; int columnLess = -1; int columnX = this.GetXForColumn(g, displayLine, text, column); if (columnX != x) { if (columnX > x) { columnGreater = column; columnLess = 0; for (column = columnGreater - 1; column >= 0; column--) { columnX = this.GetXForColumn(g, displayLine, text, column); if (columnX > x) { columnGreater = column; } else { columnLess = column; break; } } } else { // columnX < X columnLess = column; columnGreater = textLength; for (column = columnLess + 1; column <= textLength; column++) { columnX = this.GetXForColumn(g, displayLine, text, column); if (columnX < x) { columnLess = column; } else { columnGreater = column; break; } } } columnGreater = EnsureInRange(0, columnGreater, textLength); columnLess = EnsureInRange(0, columnLess, textLength); int greaterX = this.GetXForColumn(g, displayLine, text, columnGreater); int lessX = this.GetXForColumn(g, displayLine, text, columnLess); if (Math.Abs(greaterX - x) < Math.Abs(lessX - x)) { column = columnGreater; } else { column = columnLess; } } } return(new DiffViewPosition(line, column)); }
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); }