+/* Annotations */
+static EvViewWindowChild *
+ev_view_get_window_child (EvView *view,
+ GtkWidget *window)
+{
+ GList *children = view->window_children;
+
+ while (children) {
+ EvViewWindowChild *child;
+
+ child = (EvViewWindowChild *)children->data;
+ children = children->next;
+
+ if (child->window == window)
+ return child;
+ }
+
+ return NULL;
+}
+
+static void
+ev_view_window_child_move (EvView *view,
+ EvViewWindowChild *child,
+ gint x,
+ gint y)
+{
+ GtkAllocation allocation;
+ gint width, height;
+
+ gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
+ gtk_window_get_size (GTK_WINDOW (child->window), &width, &height);
+
+ child->x = x;
+ child->y = y;
+ gtk_window_move (GTK_WINDOW (child->window),
+ CLAMP (x, child->parent_x,
+ child->parent_x + allocation.width - width),
+ CLAMP (y, child->parent_y,
+ child->parent_y + allocation.height - height));
+}
+
+static void
+ev_view_window_child_move_with_parent (EvView *view,
+ GtkWidget *window)
+{
+ EvViewWindowChild *child;
+ gint root_x, root_y;
+
+ child = ev_view_get_window_child (view, window);
+ gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
+ &root_x, &root_y);
+ if (root_x != child->parent_x || root_y != child->parent_y) {
+ gint dest_x, dest_y;
+
+ dest_x = child->x + (root_x - child->parent_x);
+ dest_y = child->y + (root_y - child->parent_y);
+ child->parent_x = root_x;
+ child->parent_y = root_y;
+ ev_view_window_child_move (view, child, dest_x, dest_y);
+ }
+
+ if (child->visible && !gtk_widget_get_visible (window))
+ gtk_widget_show (window);
+}
+
+static void
+ev_view_window_child_put (EvView *view,
+ GtkWidget *window,
+ guint page,
+ gint x,
+ gint y,
+ gdouble orig_x,
+ gdouble orig_y)
+{
+ EvViewWindowChild *child;
+ gint root_x, root_y;
+
+ gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)),
+ &root_x, &root_y);
+
+ child = g_new0 (EvViewWindowChild, 1);
+ child->window = window;
+ child->page = page;
+ child->orig_x = orig_x;
+ child->orig_y = orig_y;
+ child->parent_x = root_x;
+ child->parent_y = root_y;
+ child->visible = ev_annotation_window_is_open (EV_ANNOTATION_WINDOW (window));
+ ev_view_window_child_move (view, child, x + root_x, y + root_y);
+
+ if (child->visible)
+ gtk_widget_show (window);
+ else
+ gtk_widget_hide (window);
+
+ view->window_children = g_list_append (view->window_children, child);
+}
+
+static EvViewWindowChild *
+ev_view_find_window_child_for_annot (EvView *view,
+ guint page,
+ EvAnnotation *annot)
+{
+ GList *children = view->window_children;
+
+ while (children) {
+ EvViewWindowChild *child;
+ EvAnnotation *wannot;
+
+ child = (EvViewWindowChild *)children->data;
+ children = children->next;
+
+ if (child->page != page)
+ continue;
+
+ wannot = ev_annotation_window_get_annotation (EV_ANNOTATION_WINDOW (child->window));
+ if (ev_annotation_equal (wannot, annot))
+ return child;
+ }
+
+ return NULL;
+}
+
+static void
+ev_view_window_children_free (EvView *view)
+{
+ GList *l;
+
+ if (!view->window_children)
+ return;
+
+ for (l = view->window_children; l && l->data; l = g_list_next (l)) {
+ EvViewWindowChild *child;
+
+ child = (EvViewWindowChild *)l->data;
+ gtk_widget_destroy (GTK_WIDGET (child->window));
+ g_free (child);
+ }
+ g_list_free (view->window_children);
+ view->window_children = NULL;
+ view->window_child_focus = NULL;
+}
+
+static void
+annotation_window_grab_focus (GtkWidget *widget,
+ EvView *view)
+{
+ if (view->window_child_focus)
+ ev_annotation_window_ungrab_focus (EV_ANNOTATION_WINDOW (view->window_child_focus->window));
+ view->window_child_focus = ev_view_get_window_child (view, widget);
+}
+
+static void
+annotation_window_closed (EvAnnotationWindow *window,
+ EvView *view)
+{
+ EvViewWindowChild *child;
+
+ child = ev_view_get_window_child (view, GTK_WIDGET (window));
+ child->visible = FALSE;
+}
+
+static void
+annotation_window_moved (EvAnnotationWindow *window,
+ gint x,
+ gint y,
+ EvView *view)
+{
+ EvViewWindowChild *child;
+ GdkRectangle page_area;
+ GtkBorder border;
+ GdkRectangle view_rect;
+ EvRectangle doc_rect;
+ gint width, height;
+
+ child = ev_view_get_window_child (view, GTK_WIDGET (window));
+ if (child->x == x && child->y == y)
+ return;
+
+ child->moved = TRUE;
+ child->x = x;
+ child->y = y;
+
+ /* Window has been moved by the user,
+ * we have to set a new origin in doc coords
+ */
+ gtk_window_get_size (GTK_WINDOW (window), &width, &height);
+ view_rect.x = (x - child->parent_x) + view->scroll_x;
+ view_rect.y = (y - child->parent_y) + view->scroll_y;
+ view_rect.width = width;
+ view_rect.height = height;
+
+ ev_view_get_page_extents (view, child->page, &page_area, &border);
+ view_rect_to_doc_rect (view, &view_rect, &page_area, &doc_rect);
+ child->orig_x = doc_rect.x1;
+ child->orig_y = doc_rect.y1;
+}
+
+static void
+ev_view_annotation_save_contents (EvView *view,
+ GParamSpec *pspec,
+ EvAnnotation *annot)
+{
+ if (!view->document)
+ return;
+
+ ev_document_doc_mutex_lock ();
+ ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (view->document),
+ annot, EV_ANNOTATIONS_SAVE_CONTENTS);
+ ev_document_doc_mutex_unlock ();
+}
+
+static GtkWidget *
+ev_view_create_annotation_window (EvView *view,
+ EvAnnotation *annot,
+ GtkWindow *parent)
+{
+ GtkWidget *window;
+ EvRectangle doc_rect;
+ GdkRectangle view_rect;
+ guint page;
+
+ window = ev_annotation_window_new (annot, parent);
+ g_signal_connect (window, "grab_focus",
+ G_CALLBACK (annotation_window_grab_focus),
+ view);
+ g_signal_connect (window, "closed",
+ G_CALLBACK (annotation_window_closed),
+ view);
+ g_signal_connect (window, "moved",
+ G_CALLBACK (annotation_window_moved),
+ view);
+ g_signal_connect_swapped (annot, "notify::contents",
+ G_CALLBACK (ev_view_annotation_save_contents),
+ view);
+ g_object_set_data (G_OBJECT (annot), "popup", window);
+
+ page = ev_annotation_get_page_index (annot);
+ ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (window), &doc_rect);
+ doc_rect_to_view_rect (view, page, &doc_rect, &view_rect);
+ view_rect.x -= view->scroll_x;
+ view_rect.y -= view->scroll_y;
+
+ ev_view_window_child_put (view, window, page,
+ view_rect.x, view_rect.y,
+ doc_rect.x1, doc_rect.y1);
+
+ return window;
+}
+
+static void
+show_annotation_windows (EvView *view,
+ gint page)
+{
+ EvMappingList *annots;
+ GList *l;
+ GtkWindow *parent;
+
+ parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
+
+ annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
+
+ for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) {
+ EvAnnotation *annot;
+ EvViewWindowChild *child;
+ GtkWidget *window;
+
+ annot = ((EvMapping *)(l->data))->data;
+
+ if (!EV_IS_ANNOTATION_MARKUP (annot))
+ continue;
+
+ if (!ev_annotation_markup_has_popup (EV_ANNOTATION_MARKUP (annot)))
+ continue;
+
+ window = g_object_get_data (G_OBJECT (annot), "popup");
+ if (window) {
+ ev_view_window_child_move_with_parent (view, window);
+ continue;
+ }
+
+ /* Look if we already have a popup for this annot */
+ child = ev_view_find_window_child_for_annot (view, page, annot);
+ window = child ? child->window : NULL;
+ if (window) {
+ ev_annotation_window_set_annotation (EV_ANNOTATION_WINDOW (window), annot);
+ g_object_set_data (G_OBJECT (annot), "popup", window);
+ ev_view_window_child_move_with_parent (view, window);
+ } else {
+ ev_view_create_annotation_window (view, annot, parent);
+ }
+ }
+}
+
+static void
+hide_annotation_windows (EvView *view,
+ gint page)
+{
+ EvMappingList *annots;
+ GList *l;
+
+ annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
+
+ for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) {
+ EvAnnotation *annot;
+ GtkWidget *window;
+
+ annot = ((EvMapping *)(l->data))->data;
+
+ if (!EV_IS_ANNOTATION_MARKUP (annot))
+ continue;
+
+ window = g_object_get_data (G_OBJECT (annot), "popup");
+ if (window)
+ gtk_widget_hide (window);
+ }
+}
+
+static EvAnnotation *
+ev_view_get_annotation_at_location (EvView *view,
+ gdouble x,
+ gdouble y)
+{
+ gint page = -1;
+ gint x_new = 0, y_new = 0;
+ EvMappingList *annotations_mapping;
+
+ if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document))
+ return NULL;
+
+ if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
+ return NULL;
+
+ annotations_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page);
+
+ if (annotations_mapping)
+ return ev_mapping_list_get_data (annotations_mapping, x_new, y_new);
+ else
+ return NULL;
+}
+
+static void
+ev_view_annotation_show_popup_window (EvView *view,
+ GtkWidget *window)
+{
+ EvViewWindowChild *child;
+
+ if (!window)
+ return;
+
+ child = ev_view_get_window_child (view, window);
+ if (!child->visible) {
+ child->visible = TRUE;
+ ev_view_window_child_move (view, child, child->x, child->y);
+ gtk_widget_show (window);
+ }
+}
+
+static void
+ev_view_handle_annotation (EvView *view,
+ EvAnnotation *annot,
+ gdouble x,
+ gdouble y,
+ guint32 timestamp)
+{
+ if (EV_IS_ANNOTATION_MARKUP (annot)) {
+ GtkWidget *window;
+
+ window = g_object_get_data (G_OBJECT (annot), "popup");
+ ev_view_annotation_show_popup_window (view, window);
+ }
+
+ if (EV_IS_ANNOTATION_ATTACHMENT (annot)) {
+ EvAttachment *attachment;
+
+ attachment = ev_annotation_attachment_get_attachment (EV_ANNOTATION_ATTACHMENT (annot));
+ if (attachment) {
+ GError *error = NULL;
+
+ ev_attachment_open (attachment,
+ gtk_widget_get_screen (GTK_WIDGET (view)),
+ timestamp,
+ &error);
+
+ if (error) {
+ g_warning ("%s", error->message);
+ g_error_free (error);
+ }
+ }
+ }
+}
+
+static void
+ev_view_create_annotation (EvView *view,
+ EvAnnotationType annot_type,
+ gint x,
+ gint y)
+{
+ EvAnnotation *annot;
+ GdkPoint point;
+ GdkRectangle page_area;
+ GtkBorder border;
+ EvRectangle doc_rect, popup_rect;
+ EvPage *page;
+ GdkColor color = { 0, 65535, 65535, 0 };
+ GdkRectangle view_rect;
+ cairo_region_t *region;
+
+ point.x = x;
+ point.y = y;
+ ev_view_get_page_extents (view, view->current_page, &page_area, &border);
+ view_point_to_doc_point (view, &point, &page_area,
+ &doc_rect.x1, &doc_rect.y1);
+ doc_rect.x2 = doc_rect.x1 + 24;
+ doc_rect.y2 = doc_rect.y1 + 24;
+
+ ev_document_doc_mutex_lock ();
+ page = ev_document_get_page (view->document, view->current_page);
+ switch (annot_type) {
+ case EV_ANNOTATION_TYPE_TEXT:
+ annot = ev_annotation_text_new (page);
+ break;
+ case EV_ANNOTATION_TYPE_ATTACHMENT:
+ /* TODO */
+ g_object_unref (page);
+ ev_document_doc_mutex_unlock ();
+ return;
+ default:
+ g_assert_not_reached ();
+ }
+ g_object_unref (page);
+
+ ev_annotation_set_color (annot, &color);
+
+ if (EV_IS_ANNOTATION_MARKUP (annot)) {
+ popup_rect.x1 = doc_rect.x2;
+ popup_rect.x2 = popup_rect.x1 + 200;
+ popup_rect.y1 = doc_rect.y2;
+ popup_rect.y2 = popup_rect.y1 + 150;
+ g_object_set (annot,
+ "rectangle", &popup_rect,
+ "has_popup", TRUE,
+ "popup_is_open", FALSE,
+ "label", g_get_real_name (),
+ "opacity", 1.0,
+ NULL);
+ }
+ ev_document_annotations_add_annotation (EV_DOCUMENT_ANNOTATIONS (view->document),
+ annot, &doc_rect);
+ ev_document_doc_mutex_unlock ();
+
+ /* If the page didn't have annots, mark the cache as dirty */
+ if (!ev_page_cache_get_annot_mapping (view->page_cache, view->current_page))
+ ev_page_cache_mark_dirty (view->page_cache, view->current_page);
+
+ if (EV_IS_ANNOTATION_MARKUP (annot)) {
+ GtkWindow *parent;
+ GtkWidget *window;
+
+ parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));
+ window = ev_view_create_annotation_window (view, annot, parent);
+
+ /* Show the annot window the first time */
+ ev_view_annotation_show_popup_window (view, window);
+ }
+
+ doc_rect_to_view_rect (view, view->current_page, &doc_rect, &view_rect);
+ view_rect.x -= view->scroll_x;
+ view_rect.y -= view->scroll_y;
+ region = cairo_region_create_rectangle (&view_rect);
+ ev_view_reload_page (view, view->current_page, region);
+ cairo_region_destroy (region);
+
+ g_signal_emit (view, signals[SIGNAL_ANNOT_ADDED], 0, annot);
+}
+
+void
+ev_view_focus_annotation (EvView *view,
+ EvMapping *annot_mapping)
+{
+ GdkRectangle view_rect;
+ EvAnnotation *annot;
+ guint page;
+
+ if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document))
+ return;
+
+ if (view->focus_annotation == annot_mapping)
+ return;
+
+ view->focus_annotation = annot_mapping;
+ annot = (EvAnnotation *)annot_mapping->data;
+
+ page = ev_annotation_get_page_index (annot);
+ ev_document_model_set_page (view->model, page);
+
+ doc_rect_to_view_rect (view, page,
+ &annot_mapping->area, &view_rect);
+ ensure_rectangle_is_visible (view, &view_rect);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+}
+
+void
+ev_view_begin_add_annotation (EvView *view,
+ EvAnnotationType annot_type)
+{
+ if (annot_type == EV_ANNOTATION_TYPE_UNKNOWN)
+ return;
+
+ if (view->adding_annot)
+ return;
+
+ view->adding_annot = TRUE;
+ view->adding_annot_type = annot_type;
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_ADD);
+}
+
+void
+ev_view_cancel_add_annotation (EvView *view)
+{
+ gint x, y;
+
+ if (!view->adding_annot)
+ return;
+
+ view->adding_annot = FALSE;
+ gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
+ ev_view_handle_cursor_over_xy (view, x, y);
+}
+
+static gboolean
+ev_view_synctex_backward_search (EvView *view,
+ gdouble x,
+ gdouble y)
+{
+ gint page = -1;
+ gint x_new = 0, y_new = 0;
+ EvSourceLink *link;
+
+ if (!ev_document_has_synctex (view->document))
+ return FALSE;
+
+ if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new))
+ return FALSE;
+
+ link = ev_document_synctex_backward_search (view->document, page, x_new, y_new);
+ if (link) {
+ g_signal_emit (view, signals[SIGNAL_SYNC_SOURCE], 0, link);
+ g_free (link);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+