summaryrefslogtreecommitdiff
path: root/glib
diff options
context:
space:
mode:
authorGermán Poo-Caamaño <gpoo@gnome.org>2013-11-18 00:57:53 -0800
committerCarlos Garcia Campos <carlosgc@gnome.org>2013-11-19 18:05:10 +0100
commitc784c4c3a582aaa4e10c223665cb876e12b7c16f (patch)
tree68dfed0e1cacb1494fe346af970b8e3f132c2f91 /glib
parent2d164e06b8a84ade6689d85bba2d606c66bf62a9 (diff)
glib-demo: Add support for simple line annotations
https://bugs.freedesktop.org/show_bug.cgi?id=70981
Diffstat (limited to 'glib')
-rw-r--r--glib/demo/annots.c99
1 files changed, 91 insertions, 8 deletions
diff --git a/glib/demo/annots.c b/glib/demo/annots.c
index 9ca320a2..0dc311a2 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;
@@ -641,12 +648,13 @@ 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;
+ GdkPixbuf *pixbuf;
+ PopplerAnnotFlag flags;
+ gchar *x1, *y1, *x2, *y2;
x1 = g_strdup_printf ("%.2f", area.x1);
y1 = g_strdup_printf ("%.2f", area.y1);
@@ -670,6 +678,14 @@ pgd_annots_add_annot_to_model (PgdAnnotsDemo *demo,
ANNOTS_COLUMN, annot,
-1);
+ if (selected) {
+ GtkTreePath *path;
+
+ 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);
@@ -720,7 +736,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);
@@ -842,13 +859,28 @@ pgd_annots_add_annot (PgdAnnotsDemo *demo)
annot = poppler_annot_text_new (demo->doc, &rect);
break;
+ case POPPLER_ANNOT_LINE: {
+ PopplerPoint start, end;
+
+ start.x = rect.x1;
+ start.y = rect.y1;
+ end.x = rect.x2;
+ end.y = rect.y2;
+
+ 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, &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);
}
@@ -858,6 +890,7 @@ pgd_annots_finish_add_annot (PgdAnnotsDemo *demo)
g_assert (demo->mode == MODE_ADD || demo->mode == MODE_DRAWING);
demo->mode = MODE_NORMAL;
+ demo->start.x = -1;
pgd_annots_update_cursor (demo, GDK_LAST_CURSOR);
pgd_annots_viewer_queue_redraw (demo);
@@ -928,13 +961,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
@@ -968,6 +1005,44 @@ pgd_annots_drawing_area_button_press (GtkWidget *area,
}
static gboolean
+pgd_annots_drawing_area_motion_notify (GtkWidget *area,
+ GdkEventMotion *event,
+ PgdAnnotsDemo *demo)
+{
+ PopplerRectangle rect;
+ PopplerPoint start, end;
+ gdouble width, height;
+
+ if (!demo->page || demo->mode != MODE_DRAWING || demo->start.x == -1)
+ return FALSE;
+
+ demo->stop.x = event->x;
+ demo->stop.y = event->y;
+
+ 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)
@@ -1054,6 +1129,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);
@@ -1203,6 +1283,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);