From 502b35b6de941c3aa6643a5bf9ef37f1b985ec01 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Wed, 26 Jun 2013 12:58:28 +0200 Subject: libview: Position the caret cursor at beginning/end of the line when clicking outside the line Position the caret cursor also when not clicking over text if the line contains text. origin commit: https://git.gnome.org/browse/evince/commit/?h=gnome-3-10&id=040a42d --- libview/ev-view.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/libview/ev-view.c b/libview/ev-view.c index 3f90d610..118d51d3 100644 --- a/libview/ev-view.c +++ b/libview/ev-view.c @@ -4232,7 +4232,7 @@ position_caret_cursor_at_location (EvView *view, gint offset = -1 ; gint doc_x, doc_y; EvRectangle *rect; - guint i; + guint i, j; if (!view->caret_enabled || view->rotation != 0) return FALSE; @@ -4248,24 +4248,50 @@ position_caret_cursor_at_location (EvView *view, if (!areas) return FALSE; + /* First look for the line of text at location */ for (i = 0; i < n_areas; i++) { rect = areas + i; - if (doc_x >= rect->x1 && doc_x <= rect->x2 && - doc_y >= rect->y1 && doc_y <= rect->y2) { - /* Position the caret before or after the character, depending on whether - the point falls within the left or right half of the bounding box. */ - if (doc_x <= rect->x1 + (rect->x2 - rect->x1) / 2) - offset = i; - else - offset = i + 1; + if (doc_y >= rect->y1 && doc_y <= rect->y2) break; - } } - if (offset == -1) + if (i == n_areas) return FALSE; + if (doc_x <= rect->x1) { + /* Location is before the start of the line */ + offset = i; + } else { + for (j = i; j < n_areas; j++) { + rect = areas + j; + + if (doc_y < rect->y1) { + /* Location is after the end of the line */ + offset = j; + break; + } + + if (doc_x >= rect->x1 && doc_x <= rect->x2) { + /* Location is inside the line. Position the caret before + * or after the character, depending on whether the point + * falls within the left or right half of the bounding box. + */ + if (doc_x <= rect->x1 + (rect->x2 - rect->x1) / 2) + offset = j; + else + offset = j + 1; + break; + } + + } + } + + if (offset == -1) { + /* This is the last line and loocation is after the end of the line */ + offset = n_areas; + } + if (view->cursor_offset != offset || view->cursor_page != page) { view->cursor_offset = offset; view->cursor_page = page; -- cgit v1.2.1