From 84264408cc7733e8b2cc4febd95d8309efca25b7 Mon Sep 17 00:00:00 2001 From: Antia Puentes Date: Mon, 17 Jun 2013 18:26:41 +0200 Subject: 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 --- libview/ev-view.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) 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 @@ -4229,6 +4229,51 @@ start_selection_for_event (EvView *view, &(view->selection_info.start)); } +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; -- cgit v1.2.1