From 4cba3e154d2ccebfc9df88b9a4b0b4573ca3a563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Mon, 18 Nov 2013 00:57:53 -0800 Subject: [PATCH] glib-demo: Add support for simple line annotations https://bugs.freedesktop.org/show_bug.cgi?id=70981 --- glib/demo/annots.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 12 deletions(-) diff --git a/glib/demo/annots.c b/glib/demo/annots.c index af70225..b6c1c14 100644 --- a/glib/demo/annots.c +++ b/glib/demo/annots.c @@ -52,6 +52,7 @@ typedef enum { typedef struct { PopplerDocument *doc; PopplerPage *page; + PopplerAnnot *active_annot; GtkWidget *tree_view; GtkListStore *model; @@ -72,6 +73,7 @@ typedef struct { GdkPoint start; GdkPoint stop; GdkCursorType cursor; + guint annotations_idle; } PgdAnnotsDemo; static void pgd_annots_viewer_queue_redraw (PgdAnnotsDemo *demo); @@ -82,6 +84,11 @@ pgd_annots_free (PgdAnnotsDemo *demo) if (!demo) return; + if (demo->annotations_idle > 0) { + g_source_remove (demo->annotations_idle); + demo->annotations_idle = 0; + } + if (demo->doc) { g_object_unref (demo->doc); demo->doc = NULL; @@ -651,12 +658,14 @@ pgd_annot_view_set_annot (PgdAnnotsDemo *demo, static void pgd_annots_add_annot_to_model (PgdAnnotsDemo *demo, PopplerAnnot *annot, - PopplerRectangle area) + PopplerRectangle area, + gboolean selected) { - GtkTreeIter iter; - GdkPixbuf *pixbuf; - PopplerAnnotFlag flags; - gchar *x1, *y1, *x2, *y2; + GtkTreeIter iter; + GtkTreePath *path; + GdkPixbuf *pixbuf; + PopplerAnnotFlag flags; + gchar *x1, *y1, *x2, *y2; x1 = g_strdup_printf ("%.2f", area.x1); y1 = g_strdup_printf ("%.2f", area.y1); @@ -680,6 +689,12 @@ pgd_annots_add_annot_to_model (PgdAnnotsDemo *demo, ANNOTS_COLUMN, annot, -1); + if (selected) { + path = gtk_tree_model_get_path (GTK_TREE_MODEL (demo->model), &iter); + gtk_tree_view_set_cursor (GTK_TREE_VIEW (demo->tree_view), path, NULL, FALSE); + gtk_tree_path_free (path); + } + if (pixbuf) g_object_unref (pixbuf); @@ -730,7 +745,8 @@ pgd_annots_get_annots (PgdAnnotsDemo *demo) PopplerAnnotMapping *amapping; amapping = (PopplerAnnotMapping *) l->data; - pgd_annots_add_annot_to_model (demo, amapping->annot, amapping->area); + pgd_annots_add_annot_to_model (demo, amapping->annot, + amapping->area, FALSE); } poppler_page_free_annot_mapping (mapping); @@ -830,6 +846,7 @@ static void pgd_annots_add_annot (PgdAnnotsDemo *demo) { PopplerRectangle rect; + PopplerPoint start, end; PopplerAnnot *annot; gdouble height; @@ -837,23 +854,32 @@ pgd_annots_add_annot (PgdAnnotsDemo *demo) poppler_page_get_size (demo->page, NULL, &height); - rect.x1 = demo->start.x; - rect.y1 = height - demo->start.y; - rect.x2 = demo->stop.x; - rect.y2 = height - demo->stop.y; + rect.x1 = start.x = demo->start.x; + rect.y1 = start.y = height - demo->start.y; + rect.x2 = end.x = demo->stop.x; + rect.y2 = end.y = height - demo->stop.y; switch (demo->annot_type) { case POPPLER_ANNOT_TEXT: annot = poppler_annot_text_new (demo->doc, &rect); break; + case POPPLER_ANNOT_LINE: + annot = poppler_annot_line_new (demo->doc, &rect); + poppler_annot_line_set_vertices (POPPLER_ANNOT_LINE (annot), + &start, &end); + break; default: g_assert_not_reached (); } + demo->active_annot = annot; + poppler_annot_set_color (annot, demo->annot_color); poppler_page_add_annot (demo->page, annot); - pgd_annots_add_annot_to_model (demo, annot, rect); + pgd_annots_add_annot_to_model (demo, annot, rect, TRUE); g_object_unref (annot); + + pgd_annots_viewer_queue_redraw (demo); } static void @@ -932,13 +958,17 @@ pgd_annots_viewer_redraw (PgdAnnotsDemo *demo) gtk_widget_queue_draw (demo->darea); + demo->annotations_idle = 0; + return FALSE; } static void pgd_annots_viewer_queue_redraw (PgdAnnotsDemo *demo) { - g_idle_add ((GSourceFunc)pgd_annots_viewer_redraw, demo); + if (demo->annotations_idle == 0) + demo->annotations_idle = g_idle_add ((GSourceFunc)pgd_annots_viewer_redraw, + demo); } static void @@ -972,6 +1002,45 @@ pgd_annots_drawing_area_button_press (GtkWidget *area, } static gboolean +pgd_annots_drawing_area_motion_notify (GtkWidget *area, + GdkEventMotion *event, + PgdAnnotsDemo *demo) +{ + if (!demo->page || demo->mode != MODE_DRAWING || demo->start.x == -1) + return FALSE; + + demo->stop.x = event->x; + demo->stop.y = event->y; + + PopplerRectangle rect; + PopplerPoint start, end; + gdouble width, height; + + poppler_page_get_size (demo->page, &width, &height); + + /* Keep the drawing within the page */ + demo->stop.x = CLAMP (demo->stop.x, 0, width); + demo->stop.y = CLAMP (demo->stop.y, 0, height); + + rect.x1 = start.x = demo->start.x; + rect.y1 = start.y = height - demo->start.y; + rect.x2 = end.x = demo->stop.x; + rect.y2 = end.y = height - demo->stop.y; + + poppler_annot_set_rectangle (demo->active_annot, &rect); + + if (demo->annot_type == POPPLER_ANNOT_LINE) + poppler_annot_line_set_vertices (POPPLER_ANNOT_LINE (demo->active_annot), + &start, &end); + + pgd_annot_view_set_annot (demo, demo->active_annot); + + pgd_annots_viewer_queue_redraw (demo); + + return TRUE; +} + +static gboolean pgd_annots_drawing_area_button_release (GtkWidget *area, GdkEventButton *event, PgdAnnotsDemo *demo) @@ -983,6 +1052,11 @@ pgd_annots_drawing_area_button_release (GtkWidget *area, demo->start.x = -1; + if (demo->annotations_idle > 0) { + g_source_remove (demo->annotations_idle); + demo->annotations_idle = 0; + } + return TRUE; } @@ -1061,6 +1135,11 @@ pgd_annots_create_widget (PopplerDocument *document) SELECTED_LABEL_COLUMN, "Text", -1); + gtk_list_store_append (model, &iter); + gtk_list_store_set (model, &iter, + SELECTED_TYPE_COLUMN, POPPLER_ANNOT_LINE, + SELECTED_LABEL_COLUMN, "Line", + -1); demo->type_selector = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model)); g_object_unref (model); @@ -1217,6 +1296,9 @@ pgd_annots_create_widget (PopplerDocument *document) g_signal_connect (demo->darea, "button_press_event", G_CALLBACK (pgd_annots_drawing_area_button_press), (gpointer)demo); + g_signal_connect (demo->darea, "motion_notify_event", + G_CALLBACK (pgd_annots_drawing_area_motion_notify), + (gpointer)demo); g_signal_connect (demo->darea, "button_release_event", G_CALLBACK (pgd_annots_drawing_area_button_release), (gpointer)demo); -- 1.7.9.5