~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/glib/demo/links.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Carlos Garcia Campos  <carlosgc@gnome.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 */
 
18
 
 
19
#include <gtk/gtk.h>
 
20
 
 
21
#include "links.h"
 
22
#include "utils.h"
 
23
 
 
24
enum {
 
25
        LINKS_ACTION_TYPE_COLUMN,
 
26
        LINKS_X1_COLUMN,
 
27
        LINKS_Y1_COLUMN,
 
28
        LINKS_X2_COLUMN,
 
29
        LINKS_Y2_COLUMN,
 
30
        LINKS_ACTION_COLUMN,
 
31
        N_COLUMNS
 
32
};
 
33
 
 
34
typedef struct {
 
35
        PopplerDocument *doc;
 
36
 
 
37
        GtkListStore    *model;
 
38
        GtkWidget       *action_view;
 
39
        GtkWidget       *timer_label;
 
40
        
 
41
        gint             page;
 
42
} PgdLinksDemo;
 
43
 
 
44
static void
 
45
pgd_links_free (PgdLinksDemo *demo)
 
46
{
 
47
        if (!demo)
 
48
                return;
 
49
 
 
50
        if (demo->doc) {
 
51
                g_object_unref (demo->doc);
 
52
                demo->doc = NULL;
 
53
        }
 
54
 
 
55
        if (demo->model) {
 
56
                g_object_unref (demo->model);
 
57
                demo->model = NULL;
 
58
        }
 
59
 
 
60
        g_free (demo);
 
61
}
 
62
 
 
63
static void
 
64
pgd_links_get_links (GtkWidget    *button,
 
65
                     PgdLinksDemo *demo)
 
66
{
 
67
        PopplerPage *page;
 
68
        GList       *mapping, *l;
 
69
        gint         n_links;
 
70
        GTimer      *timer;
 
71
        
 
72
        gtk_list_store_clear (demo->model);
 
73
        pgd_action_view_set_action (demo->action_view, NULL);
 
74
 
 
75
        page = poppler_document_get_page (demo->doc, demo->page);
 
76
        if (!page)
 
77
                return;
 
78
 
 
79
        timer = g_timer_new ();
 
80
        mapping = poppler_page_get_link_mapping (page);
 
81
        g_timer_stop (timer);
 
82
 
 
83
        n_links = g_list_length (mapping);
 
84
        if (n_links > 0) {
 
85
                gchar *str;
 
86
                
 
87
                str = g_strdup_printf ("<i>%d links found in %.4f seconds</i>",
 
88
                                       n_links, g_timer_elapsed (timer, NULL));
 
89
                gtk_label_set_markup (GTK_LABEL (demo->timer_label), str);
 
90
                g_free (str);
 
91
        } else {
 
92
                gtk_label_set_markup (GTK_LABEL (demo->timer_label), "<i>No links found</i>");
 
93
        }
 
94
 
 
95
        g_timer_destroy (timer);
 
96
 
 
97
        for (l = mapping; l; l = g_list_next (l)) {
 
98
                PopplerLinkMapping *lmapping;
 
99
                PopplerAction      *action;
 
100
                GEnumValue         *enum_value;
 
101
                GtkTreeIter         iter;
 
102
                gchar              *x1, *y1, *x2, *y2;
 
103
 
 
104
                lmapping = (PopplerLinkMapping *)l->data;
 
105
                action = poppler_action_copy (lmapping->action);
 
106
                enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ACTION_TYPE), action->type);
 
107
 
 
108
                x1 = g_strdup_printf ("%.2f", lmapping->area.x1);
 
109
                y1 = g_strdup_printf ("%.2f", lmapping->area.y1);
 
110
                x2 = g_strdup_printf ("%.2f", lmapping->area.x2);
 
111
                y2 = g_strdup_printf ("%.2f", lmapping->area.y2);
 
112
 
 
113
                gtk_list_store_append (demo->model, &iter);
 
114
                gtk_list_store_set (demo->model, &iter,
 
115
                                    LINKS_ACTION_TYPE_COLUMN, enum_value->value_name,
 
116
                                    LINKS_X1_COLUMN, x1, 
 
117
                                    LINKS_Y1_COLUMN, y1,
 
118
                                    LINKS_X2_COLUMN, x2,
 
119
                                    LINKS_Y2_COLUMN, y2,
 
120
                                    LINKS_ACTION_COLUMN, action,
 
121
                                    -1);
 
122
                g_free (x1);
 
123
                g_free (y1);
 
124
                g_free (x2);
 
125
                g_free (y2);
 
126
                
 
127
                g_object_weak_ref (G_OBJECT (demo->model),
 
128
                                   (GWeakNotify)poppler_action_free,
 
129
                                   action);
 
130
        }
 
131
 
 
132
        poppler_page_free_link_mapping (mapping);
 
133
        g_object_unref (page);
 
134
}
 
135
 
 
136
static void
 
137
pgd_links_page_selector_value_changed (GtkSpinButton *spinbutton,
 
138
                                       PgdLinksDemo  *demo)
 
139
{
 
140
        demo->page = (gint)gtk_spin_button_get_value (spinbutton) - 1;
 
141
}
 
142
 
 
143
static void
 
144
pgd_links_selection_changed (GtkTreeSelection *treeselection,
 
145
                             PgdLinksDemo     *demo)
 
146
{
 
147
        GtkTreeModel *model;
 
148
        GtkTreeIter   iter;
 
149
 
 
150
        if (gtk_tree_selection_get_selected (treeselection, &model, &iter)) {
 
151
                PopplerAction *action;
 
152
 
 
153
                gtk_tree_model_get (model, &iter,
 
154
                                    LINKS_ACTION_COLUMN, &action,
 
155
                                    -1);
 
156
                pgd_action_view_set_action (demo->action_view, action);
 
157
        }
 
158
}
 
159
 
 
160
GtkWidget *
 
161
pgd_links_create_widget (PopplerDocument *document)
 
162
{
 
163
        PgdLinksDemo     *demo;
 
164
        GtkWidget        *label;
 
165
        GtkWidget        *vbox;
 
166
        GtkWidget        *hbox, *page_selector;
 
167
        GtkWidget        *button;
 
168
        GtkWidget        *hpaned;
 
169
        GtkWidget        *swindow, *treeview;
 
170
        GtkTreeSelection *selection;
 
171
        GtkCellRenderer  *renderer;
 
172
        gchar            *str;
 
173
        gint              n_pages;
 
174
 
 
175
        demo = g_new0 (PgdLinksDemo, 1);
 
176
        
 
177
        demo->doc = g_object_ref (document);
 
178
        
 
179
        n_pages = poppler_document_get_n_pages (document);
 
180
 
 
181
        vbox = gtk_vbox_new (FALSE, 12);
 
182
 
 
183
        hbox = gtk_hbox_new (FALSE, 6);
 
184
 
 
185
        label = gtk_label_new ("Page:");
 
186
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
 
187
        gtk_widget_show (label);
 
188
 
 
189
        page_selector = gtk_spin_button_new_with_range (1, n_pages, 1);
 
190
        g_signal_connect (G_OBJECT (page_selector), "value-changed",
 
191
                          G_CALLBACK (pgd_links_page_selector_value_changed),
 
192
                          (gpointer)demo);
 
193
        gtk_box_pack_start (GTK_BOX (hbox), page_selector, FALSE, TRUE, 0);
 
194
        gtk_widget_show (page_selector);
 
195
 
 
196
        str = g_strdup_printf ("of %d", n_pages);
 
197
        label = gtk_label_new (str);
 
198
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
 
199
        gtk_widget_show (label);
 
200
        g_free (str);
 
201
 
 
202
        button = gtk_button_new_with_label ("Get Links");
 
203
        g_signal_connect (G_OBJECT (button), "clicked",
 
204
                          G_CALLBACK (pgd_links_get_links),
 
205
                          (gpointer)demo);
 
206
        gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
207
        gtk_widget_show (button);
 
208
 
 
209
        gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
 
210
        gtk_widget_show (hbox);
 
211
 
 
212
        demo->timer_label = gtk_label_new (NULL);
 
213
        gtk_label_set_markup (GTK_LABEL (demo->timer_label), "<i>No links found</i>");
 
214
        g_object_set (G_OBJECT (demo->timer_label), "xalign", 1.0, NULL);
 
215
        gtk_box_pack_start (GTK_BOX (vbox), demo->timer_label, FALSE, TRUE, 0);
 
216
        gtk_widget_show (demo->timer_label);
 
217
 
 
218
        hpaned = gtk_hpaned_new ();
 
219
 
 
220
        demo->action_view = pgd_action_view_new (document);
 
221
 
 
222
        swindow = gtk_scrolled_window_new (NULL, NULL);
 
223
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
 
224
                                        GTK_POLICY_AUTOMATIC,
 
225
                                        GTK_POLICY_AUTOMATIC);
 
226
        
 
227
        demo->model = gtk_list_store_new (N_COLUMNS,
 
228
                                          G_TYPE_STRING, G_TYPE_STRING,
 
229
                                          G_TYPE_STRING, G_TYPE_STRING,
 
230
                                          G_TYPE_STRING, G_TYPE_POINTER);
 
231
        treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (demo->model));
 
232
 
 
233
        renderer = gtk_cell_renderer_text_new ();
 
234
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
235
                                                     0, "Action Type",
 
236
                                                     renderer,
 
237
                                                     "text", LINKS_ACTION_TYPE_COLUMN,
 
238
                                                     NULL);
 
239
 
 
240
        renderer = gtk_cell_renderer_text_new ();
 
241
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
242
                                                     1, "X1",
 
243
                                                     renderer,
 
244
                                                     "text", LINKS_X1_COLUMN,
 
245
                                                     NULL);
 
246
        renderer = gtk_cell_renderer_text_new ();
 
247
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
248
                                                     2, "Y1",
 
249
                                                     renderer,
 
250
                                                     "text", LINKS_Y1_COLUMN,
 
251
                                                     NULL);
 
252
        renderer = gtk_cell_renderer_text_new ();
 
253
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
254
                                                     3, "X2",
 
255
                                                     renderer,
 
256
                                                     "text", LINKS_X2_COLUMN,
 
257
                                                     NULL);
 
258
        renderer = gtk_cell_renderer_text_new ();
 
259
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
260
                                                     4, "Y2",
 
261
                                                     renderer,
 
262
                                                     "text", LINKS_Y2_COLUMN,
 
263
                                                     NULL);
 
264
        
 
265
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
 
266
        g_signal_connect (G_OBJECT (selection), "changed",
 
267
                          G_CALLBACK (pgd_links_selection_changed),
 
268
                          (gpointer)demo);
 
269
 
 
270
        gtk_container_add (GTK_CONTAINER (swindow), treeview);
 
271
        gtk_widget_show (treeview);
 
272
 
 
273
        gtk_paned_add1 (GTK_PANED (hpaned), swindow);
 
274
        gtk_widget_show (swindow);
 
275
 
 
276
        gtk_paned_add2 (GTK_PANED (hpaned), demo->action_view);
 
277
        gtk_widget_show (demo->action_view);
 
278
 
 
279
        gtk_paned_set_position (GTK_PANED (hpaned), 300);
 
280
 
 
281
        gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
 
282
        gtk_widget_show (hpaned);
 
283
 
 
284
        g_object_weak_ref (G_OBJECT (vbox),
 
285
                           (GWeakNotify)pgd_links_free,
 
286
                           demo);
 
287
        
 
288
        return vbox;
 
289
}