diff options
author | Antia Puentes <[email protected]> | 2013-06-17 18:26:41 +0200 |
---|---|---|
committer | raveit65 <[email protected]> | 2017-09-06 18:25:34 +0200 |
commit | 84264408cc7733e8b2cc4febd95d8309efca25b7 (patch) | |
tree | ec832a599134d1b936b18361b6841e3ca3c0e8ec | |
parent | 4f4be3b6e7bb69e2d1b15256e4bf801791c9b957 (diff) | |
download | atril-84264408cc7733e8b2cc4febd95d8309efca25b7.tar.bz2 atril-84264408cc7733e8b2cc4febd95d8309efca25b7.tar.xz |
libview: Position the caret cursor by clicking
https://bugzilla.gnome.org/show_bug.cgi?id=702071
origin commit:
https://git.gnome.org/browse/evince/commit/?h=gnome-3-10&id=153e67d
-rw-r--r-- | libview/ev-view.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/libview/ev-view.c b/libview/ev-view.c index f91efe84..c68c5172 100644 --- a/libview/ev-view.c +++ b/libview/ev-view.c @@ -4230,6 +4230,51 @@ start_selection_for_event (EvView *view, } static gboolean +get_caret_cursor_offset_at_location (EvView *view, + gdouble x, + gdouble y, + gint *page, + gint *offset) +{ + gint doc_x, doc_y; + EvRectangle *areas; + EvRectangle *rect; + guint n_areas = 0; + guint i; + + if (!view->caret_enabled || view->rotation != 0) + return FALSE; + + if (!view->page_cache) + return FALSE; + + /* Get the offset from the doc point */ + if (!get_doc_point_from_location (view, x, y, page, &doc_x, &doc_y)) + return FALSE; + + ev_page_cache_get_text_layout (view->page_cache, *page, &areas, &n_areas); + if (!areas) + return FALSE; + + 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; + + return TRUE; + } + } + + return FALSE; +} + +static gboolean ev_view_button_press_event (GtkWidget *widget, GdkEventButton *event) { @@ -4302,6 +4347,8 @@ ev_view_button_press_event (GtkWidget *widget, view->image_dnd_info.start.x = event->x + view->scroll_x; view->image_dnd_info.start.y = event->y + view->scroll_y; } else { + gint page, offset; + ev_view_remove_all (view); _ev_view_set_focused_element (view, NULL, -1); @@ -4313,6 +4360,12 @@ ev_view_button_press_event (GtkWidget *widget, if (EV_IS_SELECTION (view->document)) start_selection_for_event (view, event); + + if (get_caret_cursor_offset_at_location (view, event->x, event->y, &page, &offset)) { + view->cursor_offset = offset; + view->cursor_page = page; + gtk_widget_queue_draw (widget); + } } } return TRUE; |