Skip to content

Commit

Permalink
use wxSTC VerticalCentreCaret call to center the caret at a given line
Browse files Browse the repository at this point in the history
  • Loading branch information
eranif committed Mar 18, 2024
1 parent 6018c41 commit 077457c
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions Plugin/clSTCHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void DoSetCaretAt(wxStyledTextCtrl* ctrl, long pos)
ctrl->SetSelectionStart(pos);
ctrl->SetSelectionEnd(pos);
int line = ctrl->LineFromPosition(pos);
if(line >= 0) {
if (line >= 0) {
// This is needed to unfold the line if it were folded
// The various other 'EnsureVisible' things don't do this
ctrl->EnsureVisible(line);
Expand All @@ -20,7 +20,7 @@ void clSTCHelper::ScrollRange(wxStyledTextCtrl* ctrl, int selection_start, int s
{
#if wxCHECK_VERSION(3, 1, 0)
// ensure the selection is visible
if(selection_end != selection_start) {
if (selection_end != selection_start) {
ctrl->ScrollRange(selection_start, selection_end);
}
ctrl->EnsureCaretVisible(); // incase we are inside a folded area
Expand All @@ -35,7 +35,7 @@ void clSTCHelper::ScrollRange(wxStyledTextCtrl* ctrl, int selection_start, int s
void clSTCHelper::CenterLine(wxStyledTextCtrl* ctrl, int line, int col)
{
int caret_pos = ctrl->PositionFromLine(line);
if(col != wxNOT_FOUND) {
if (col != wxNOT_FOUND) {
// calculate the position
caret_pos += col;
}
Expand All @@ -44,18 +44,8 @@ void clSTCHelper::CenterLine(wxStyledTextCtrl* ctrl, int line, int col)
ctrl->EnsureVisible(line); // make sure the requested line is visible
SetCaretAt(ctrl, caret_pos);

// center that line
int linesOnScreen = ctrl->LinesOnScreen();
// To place our line in the middle, the first visible line should be
// the: line - (linesOnScreen / 2)
int firstVisibleLine = line - (linesOnScreen / 2);
if(firstVisibleLine < 0) {
firstVisibleLine = 0;
}

int real_visible_line = ctrl->VisibleFromDocLine(firstVisibleLine);
ctrl->EnsureVisible(real_visible_line);
ctrl->SetFirstVisibleLine(real_visible_line);
ctrl->VerticalCentreCaret();
ctrl->EnsureCaretVisible();
}

void clSTCHelper::SetCaretAt(wxStyledTextCtrl* ctrl, long pos)
Expand All @@ -73,31 +63,31 @@ void clSTCHelper::SetCaretAt(wxStyledTextCtrl* ctrl, long pos)
void clSTCHelper::UpdateScrollbarWidth(wxStyledTextCtrl* ctrl, size_t char_width)
{
// recalculate and set the length of horizontal scrollbar
if(char_width == wxString::npos) {
if (char_width == wxString::npos) {
char_width = ctrl->TextWidth(0, "X");
}

int maxPixel = 0;
int startLine = ctrl->GetFirstVisibleLine();
int endLine = startLine + ctrl->LinesOnScreen();
if(endLine >= (ctrl->GetLineCount() - 1)) {
if (endLine >= (ctrl->GetLineCount() - 1)) {
endLine--;
}

wxString text;
for(int i = startLine; i <= endLine; i++) {
for (int i = startLine; i <= endLine; i++) {
int visibleLine = (int)ctrl->DocLineFromVisible(i); // get actual visible line, folding may offset lines
wxString line_text = ctrl->GetLine(visibleLine);
text = line_text.length() > text.length() ? line_text : text;
}

maxPixel = char_width * text.length();
if(maxPixel == 0) {
if (maxPixel == 0) {
maxPixel++; // make sure maxPixel is valid
}

int currentLength = ctrl->GetScrollWidth(); // Get current scrollbar size
if(currentLength != maxPixel) {
if (currentLength != maxPixel) {
// And if it is not the same, update it
ctrl->SetScrollWidth(maxPixel);
}
Expand Down

0 comments on commit 077457c

Please sign in to comment.