+/* 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)
+{
+ child->x = x;
+ child->y = y;
+ gtk_window_move (GTK_WINDOW (child->window),
+ MAX (child->parent_x, x),
+ MAX (child->parent_y, y));
+}
+
+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 (wannot == annot || strcmp (wannot->name, annot->name) == 0)
+ 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;
+
+ 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 (EvView *view,
+ EvAnnotation *annot)
+{
+ if (!view->document)
+ return;
+
+ if (!annot->changed)
+ return;
+
+ ev_document_annotations_annotation_set_contents (EV_DOCUMENT_ANNOTATIONS (view->document),
+ annot, annot->contents);
+ annot->changed = FALSE;
+}
+
+static void
+show_annotation_windows (EvView *view,
+ gint page)
+{
+ GList *annots, *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 = annots; l && l->data; l = g_list_next (l)) {
+ EvAnnotation *annot;
+ EvViewWindowChild *child;
+ GtkWidget *window;
+ EvRectangle *doc_rect;
+ GdkRectangle view_rect;
+
+ 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 {
+ 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_object_set_data (G_OBJECT (annot), "popup", window);
+
+ doc_rect = (EvRectangle *)ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (window));
+ 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);
+
+ g_object_weak_ref (G_OBJECT (annot),
+ (GWeakNotify)ev_view_annotation_save,
+ view);
+ }
+ }
+}
+
+static void
+hide_annotation_windows (EvView *view,
+ gint page)
+{
+ GList *annots, *l;
+
+ annots = ev_page_cache_get_annot_mapping (view->page_cache, page);
+
+ for (l = 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;
+ GList *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_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");
+ if (window) {
+ EvViewWindowChild *child;
+
+ 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);
+ }
+ }
+ }
+
+ if (EV_IS_ANNOTATION_ATTACHMENT (annot)) {
+ EvAttachment *attachment = EV_ANNOTATION_ATTACHMENT (annot)->attachment;
+
+ 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);
+ }
+ }
+ }
+}
+