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

« back to all changes in this revision

Viewing changes to tests/poppler/glib/demo/print.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) 2009 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 "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
#include "print.h"
 
23
 
 
24
typedef enum {
 
25
        PRINT_DOCUMENT,
 
26
        PRINT_DOCUMENT_MARKUPS,
 
27
        PRINT_DOCUMENT_STAMPS
 
28
} PgdPrintOptions;
 
29
 
 
30
typedef struct {
 
31
        PopplerDocument *doc;
 
32
        GtkWidget       *options_combo;
 
33
        PgdPrintOptions  options;
 
34
} PgdPrintDemo;
 
35
 
 
36
#define PGD_PRINT_OPTIONS "pgd-print-options"
 
37
 
 
38
static void
 
39
pgd_print_free (PgdPrintDemo *demo)
 
40
{
 
41
        if (!demo)
 
42
                return;
 
43
 
 
44
        if (demo->doc) {
 
45
                g_object_unref (demo->doc);
 
46
                demo->doc = NULL;
 
47
        }
 
48
 
 
49
        g_free (demo);
 
50
}
 
51
 
 
52
static void
 
53
pgd_print_begin_print (GtkPrintOperation *op,
 
54
                       GtkPrintContext   *context,
 
55
                       PgdPrintDemo      *demo)
 
56
{
 
57
        gtk_print_operation_set_n_pages (op, poppler_document_get_n_pages (demo->doc));
 
58
}
 
59
 
 
60
static void
 
61
pgd_print_draw_page (GtkPrintOperation *op,
 
62
                     GtkPrintContext   *context,
 
63
                     gint               page_nr,
 
64
                     PgdPrintDemo      *demo)
 
65
{
 
66
        PopplerPage      *page;
 
67
        cairo_t          *cr;
 
68
        GtkPrintSettings *settings;
 
69
        PgdPrintOptions   options;
 
70
        PopplerPrintFlags flags = 0;
 
71
 
 
72
        page = poppler_document_get_page (demo->doc, page_nr);
 
73
        if (!page)
 
74
                return;
 
75
 
 
76
        settings = gtk_print_operation_get_print_settings (op);
 
77
        /* Workaround for gtk+ bug, we need to save the options ourselves */
 
78
        options = demo->options;
 
79
#if 0
 
80
        options = gtk_print_settings_get_int_with_default (settings,
 
81
                                                           PGD_PRINT_OPTIONS,
 
82
                                                           PRINT_DOCUMENT_MARKUPS);
 
83
#endif
 
84
        switch (options) {
 
85
        case PRINT_DOCUMENT:
 
86
                flags |= POPPLER_PRINT_DOCUMENT;
 
87
                break;
 
88
        case PRINT_DOCUMENT_MARKUPS:
 
89
                flags |= POPPLER_PRINT_MARKUP_ANNOTS;
 
90
                break;
 
91
        case PRINT_DOCUMENT_STAMPS:
 
92
                flags |= POPPLER_PRINT_STAMP_ANNOTS_ONLY;
 
93
                break;
 
94
        default:
 
95
                g_assert_not_reached ();
 
96
        }
 
97
 
 
98
        cr = gtk_print_context_get_cairo_context (context);
 
99
        poppler_page_render_for_printing_with_options (page, cr, flags);
 
100
        g_object_unref (page);
 
101
}
 
102
 
 
103
static GObject *
 
104
pgd_print_create_custom_widget (GtkPrintOperation *op,
 
105
                                PgdPrintDemo      *demo)
 
106
{
 
107
        GtkWidget        *hbox;
 
108
        GtkWidget        *label, *combo;
 
109
        GtkPrintSettings *settings;
 
110
        PgdPrintOptions   options;
 
111
 
 
112
        settings = gtk_print_operation_get_print_settings (op);
 
113
        options = gtk_print_settings_get_int_with_default (settings,
 
114
                                                           PGD_PRINT_OPTIONS,
 
115
                                                           PRINT_DOCUMENT_MARKUPS);
 
116
 
 
117
        hbox = gtk_hbox_new (FALSE, 0);
 
118
        gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
 
119
 
 
120
        label = gtk_label_new ("Print: ");
 
121
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
122
        gtk_widget_show (label);
 
123
 
 
124
        combo = gtk_combo_box_new_text ();
 
125
        demo->options_combo = combo;
 
126
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Document");
 
127
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Document and markup");
 
128
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Document and stamps");
 
129
        gtk_combo_box_set_active (GTK_COMBO_BOX (combo), options);
 
130
        gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
 
131
        gtk_widget_show (combo);
 
132
 
 
133
        return G_OBJECT (hbox);
 
134
}
 
135
 
 
136
static void
 
137
pgd_print_custom_widget_apply (GtkPrintOperation *op,
 
138
                               GtkWidget         *widget,
 
139
                               PgdPrintDemo      *demo)
 
140
{
 
141
        GtkPrintSettings *settings;
 
142
        PgdPrintOptions   options;
 
143
 
 
144
        settings = gtk_print_operation_get_print_settings (op);
 
145
        options = gtk_combo_box_get_active (GTK_COMBO_BOX (demo->options_combo));
 
146
        /* Workaround for gtk+ bug, we need to save the options ourselves */
 
147
        demo->options = options;
 
148
        gtk_print_settings_set_int (settings, PGD_PRINT_OPTIONS, options);
 
149
}
 
150
 
 
151
static void
 
152
pgd_print_print (GtkWidget    *button,
 
153
                 PgdPrintDemo *demo)
 
154
{
 
155
        GtkPrintOperation *op;
 
156
        GError            *error = NULL;
 
157
 
 
158
        op = gtk_print_operation_new ();
 
159
        gtk_print_operation_set_custom_tab_label (op, "PDF Options");
 
160
        g_signal_connect (op, "begin-print",
 
161
                          G_CALLBACK (pgd_print_begin_print),
 
162
                          demo);
 
163
        g_signal_connect (op, "draw-page",
 
164
                          G_CALLBACK (pgd_print_draw_page),
 
165
                          demo);
 
166
        g_signal_connect (op, "create_custom_widget",
 
167
                          G_CALLBACK (pgd_print_create_custom_widget),
 
168
                          demo);
 
169
        g_signal_connect (op, "custom_widget_apply",
 
170
                          G_CALLBACK (pgd_print_custom_widget_apply),
 
171
                          demo);
 
172
        gtk_print_operation_run (op,
 
173
                                 GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 
174
                                 GTK_WINDOW (gtk_widget_get_toplevel (button)),
 
175
                                 &error);
 
176
        if (error) {
 
177
                GtkWidget *dialog;
 
178
 
 
179
                dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (button)),
 
180
                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
 
181
                                                 GTK_MESSAGE_ERROR,
 
182
                                                 GTK_BUTTONS_CLOSE,
 
183
                                                 "%s", error->message);
 
184
                g_error_free (error);
 
185
 
 
186
                g_signal_connect (dialog, "response",
 
187
                                  G_CALLBACK (gtk_widget_destroy), NULL);
 
188
 
 
189
                gtk_widget_show (dialog);
 
190
        }
 
191
        g_object_unref (op);
 
192
}
 
193
 
 
194
GtkWidget *
 
195
pgd_print_create_widget (PopplerDocument *document)
 
196
{
 
197
        PgdPrintDemo *demo;
 
198
        GtkWidget    *vbox;
 
199
        GtkWidget    *hbox;
 
200
        GtkWidget    *button;
 
201
 
 
202
        demo = g_new0 (PgdPrintDemo, 1);
 
203
 
 
204
        demo->doc = g_object_ref (document);
 
205
 
 
206
        vbox = gtk_vbox_new (FALSE, 12);
 
207
 
 
208
        hbox = gtk_hbox_new (FALSE, 6);
 
209
 
 
210
        button = gtk_button_new_with_label ("Print...");
 
211
        g_signal_connect (G_OBJECT (button), "clicked",
 
212
                          G_CALLBACK (pgd_print_print),
 
213
                          (gpointer)demo);
 
214
        gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
215
        gtk_widget_show (button);
 
216
 
 
217
        gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
 
218
        gtk_widget_show (hbox);
 
219
 
 
220
        g_object_weak_ref (G_OBJECT (vbox),
 
221
                           (GWeakNotify)pgd_print_free,
 
222
                           (gpointer)demo);
 
223
 
 
224
        return vbox;
 
225
}