source: trunk/third/libgnomeprintui/examples/example_10.c @ 18604

Revision 18604, 8.9 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18603, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/*
3 *  example_10.c: sample gnome-print code
4 *
5 *  This program is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Library General Public License
7 *  as published by the Free Software Foundation; either version 2 of
8 *  the License, or (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU Library General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Library General Public
16 *  License along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 *  Authors:
20 *    Chema Celorio <chema@ximian.com>
21 *
22 *  Copyright (C) 2002 Ximian Inc. and authors
23 *
24 */
25
26/*
27 * See README
28 */
29
30#define WE_ARE_LIBGNOMEPRINT_INTERNALS /* Needed for gpa-tree-viewer which is not public */
31
32#include <libgnomeprint/gnome-print.h>
33#include <libgnomeprint/gnome-print-job.h>
34#include <libgnomeprint/gnome-print-config.h>
35#include <libgnomeprintui/gnome-print-job-preview.h>
36#include <libgnomeprintui/gnome-print-dialog.h>
37#include <libgnomeprintui/gnome-print-paper-selector.h>
38#include <libgnomeprintui/gnome-font-dialog.h>
39#include <libgnomeprintui/gpaui/gpa-tree-viewer.h>
40
41#include <gtk/gtk.h>
42#include <glade/glade.h>
43
44typedef struct {
45        GtkWidget *view;
46        GnomePrintConfig *config;
47        gchar *name;
48} MyDoc;
49
50typedef struct {
51        GtkWidget *status_bar;
52
53        MyDoc *active_doc; /* The active document */
54       
55        MyDoc *doc1;
56        MyDoc *doc2;
57        MyDoc *doc3;
58} MyApp;
59
60static MyApp *app;
61
62static gboolean
63my_status_bar_pop (gpointer data)
64{
65        guint id = GPOINTER_TO_UINT (data);
66        gtk_statusbar_remove (GTK_STATUSBAR (app->status_bar), 0, id);
67        return FALSE;
68}
69
70static void
71my_status_bar_print (const gchar *message)
72{
73        guint num;
74        num = gtk_statusbar_push (GTK_STATUSBAR (app->status_bar), 0, message);
75        g_timeout_add (3000, my_status_bar_pop, GUINT_TO_POINTER (num));
76}
77
78static void
79my_print_image_from_pixbuf (GnomePrintContext *gpc, GdkPixbuf *pixbuf)
80{
81        guchar *raw_image;
82        gboolean has_alpha;
83        gint rowstride, height, width;
84       
85        raw_image = gdk_pixbuf_get_pixels (pixbuf);
86        has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
87        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
88        height    = gdk_pixbuf_get_height (pixbuf);
89        width     = gdk_pixbuf_get_width (pixbuf);
90       
91        if (has_alpha)
92                gnome_print_rgbaimage (gpc, (char *)raw_image, width, height, rowstride);
93        else
94                gnome_print_rgbimage (gpc, (char *)raw_image, width, height, rowstride);
95}
96
97static void
98my_print_image_from_disk (GnomePrintContext *gpc)
99{
100        GdkPixbuf *pixbuf;
101        GError *error;
102
103        error = NULL;
104        pixbuf = gdk_pixbuf_new_from_file ("sample-image.png", &error);
105        if (!pixbuf) {
106                g_print ("Could not load sample_image.png.\n");
107                return;
108        }
109
110        gnome_print_gsave (gpc);
111        gnome_print_scale (gpc, 144, 144);
112       
113        my_print_image_from_pixbuf (gpc, pixbuf);
114       
115        gnome_print_grestore (gpc);
116        g_object_unref (G_OBJECT (pixbuf));
117}
118
119static void
120my_draw (GnomePrintContext *gpc)
121{
122        gnome_print_beginpage (gpc, "1");
123
124        gnome_print_moveto (gpc, 1, 1);
125        gnome_print_lineto (gpc, 200, 200);
126        gnome_print_stroke (gpc);
127
128        my_print_image_from_disk (gpc);
129        gnome_print_showpage (gpc);
130}
131
132
133static void
134my_print (GnomePrintJob *job, gboolean preview)
135{
136        GnomePrintContext *gpc;
137
138        gpc = gnome_print_job_get_context (job);
139        my_draw (gpc);
140        g_object_unref (G_OBJECT (gpc));
141       
142        gnome_print_job_close (job);
143
144        if (!preview) {
145                my_status_bar_print ("Printing ...");
146                gnome_print_job_print (job);
147        } else {
148                my_status_bar_print ("Print previewing ...");
149                gtk_widget_show (gnome_print_job_preview_new (job, "Title goes here"));
150        }
151}
152
153static void
154my_print_cb (void)
155{
156        GnomePrintJob *job;
157        GtkWidget *dialog;
158        gint response;
159
160        /* Create the objects */
161        g_assert (app->active_doc);
162        job    = gnome_print_job_new (app->active_doc->config);
163        dialog = gnome_print_dialog_new (job, "Sample print dialog", 0);
164
165        /* Run the dialog */
166        response = gtk_dialog_run (GTK_DIALOG (dialog));
167        gtk_widget_destroy (dialog);
168        switch (response) {
169        case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
170                my_print (job, FALSE);
171                break;
172        case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
173                my_print (job, TRUE);
174                break;
175        default:
176                return;
177        }
178
179        g_object_unref (G_OBJECT (job));
180}
181
182static void
183my_print_preview_cb (void)
184{
185        GnomePrintJob *job;
186
187        job = gnome_print_job_new (app->active_doc->config);
188        my_print (job, TRUE);
189        g_object_unref (G_OBJECT (job));
190}
191
192static void
193my_print_setup_cb (void)
194{
195        GtkWidget *dialog;
196        GtkWidget *paper_selector;
197        GtkWidget *notebook;
198        GtkWidget *label;
199        gint response;
200
201        notebook = gtk_notebook_new ();
202
203        /* GnomePrintPaperSelector */
204        paper_selector = gnome_paper_selector_new_with_flags (app->active_doc->config, 0);
205        gtk_widget_set_usize (paper_selector, 200, 400);
206        label = gtk_label_new_with_mnemonic ("P_aper");
207        gtk_notebook_append_page (GTK_NOTEBOOK (notebook), paper_selector, label);
208
209        /* GnomePrintPaperSelector */
210        paper_selector = gnome_paper_selector_new_with_flags (app->active_doc->config, 0);
211        gtk_widget_set_usize (paper_selector, 200, 400);
212        label = gtk_label_new_with_mnemonic ("_foo");
213        gtk_notebook_append_page (GTK_NOTEBOOK (notebook), paper_selector, label);
214
215        /* Dialog */
216        dialog = gtk_dialog_new_with_buttons ("Printer Setup", NULL, 0,
217                                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
218                                              GTK_STOCK_OK,     GTK_RESPONSE_OK,
219                                              NULL);
220        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
221                            notebook, TRUE, TRUE, 0);
222        gtk_widget_show_all (dialog);
223
224       
225        response = gtk_dialog_run (GTK_DIALOG (dialog));
226        gtk_widget_destroy (GTK_WIDGET (dialog));
227}
228
229static void
230my_font_dialog_cb (void)
231{
232        GtkWidget *dialog;
233
234        dialog = gnome_font_dialog_new ("Sample Font dialog");
235
236        gtk_widget_show_all (dialog);
237        gtk_dialog_run (GTK_DIALOG (dialog));
238        gtk_widget_destroy (dialog);
239}
240
241
242static void
243my_tree_cb (void)
244{
245        GtkWidget *dialog;
246
247        dialog = gpa_tree_viewer_new (app->doc1->config);
248}
249
250
251/**
252 * my_document_activate_cb:
253 * @document:
254 * @data:
255 *
256 * This function is called when a document is activated. We set the active document here
257 * and update the GnomePrintWidgets with the active document config
258 **/
259static void
260my_document_activate_cb (GtkWidget *view, MyDoc *doc)
261{
262        gchar *message;
263
264        if (app->active_doc == doc)
265                return;
266        app->active_doc = doc;
267        message = g_strdup_printf ("The active document is now \"%s\"", doc->name);
268        my_status_bar_print (message);
269        g_free (message);
270}
271
272/**
273 * my_new_doc:
274 * @doc_name:
275 * @gui:
276 *
277 * Creates a new document
278 *
279 * Return Value: a pointer to the new document, aborts on error
280 **/
281static MyDoc *
282my_new_doc (const gchar *doc_name, GladeXML *gui)
283{
284        MyDoc *doc;
285
286        doc = g_new (MyDoc, 1);
287        doc->view = glade_xml_get_widget (gui, doc_name);
288        doc->name = g_strdup (doc_name);
289        doc->config = gnome_print_config_default ();
290        g_signal_connect (G_OBJECT (doc->view), "grab_focus",
291                          G_CALLBACK (my_document_activate_cb), doc);
292
293        g_assert (doc->view);
294        g_assert (doc->config);
295       
296        return doc;
297}
298
299static gboolean
300my_app_load (void)
301{
302        GladeXML *gui = glade_xml_new ("example_10.glade", NULL, NULL);
303        GtkWidget *item;
304
305        if (!gui) {
306                g_warning ("Could not load glade file\n");
307                return FALSE;
308        }
309
310        /* Connect menu items to callbacks */
311        item = glade_xml_get_widget (gui, "menu_quit_item");
312        g_return_val_if_fail (item, FALSE);
313        g_signal_connect (G_OBJECT (item), "activate",
314                          G_CALLBACK (gtk_main_quit), NULL);
315        item = glade_xml_get_widget (gui, "window1");
316        g_return_val_if_fail (item, FALSE);
317        g_signal_connect (G_OBJECT (item), "delete_event",
318                          G_CALLBACK (gtk_main_quit), NULL);
319        item = glade_xml_get_widget (gui, "menu_print_item");
320        g_return_val_if_fail (item, FALSE);
321        g_signal_connect (G_OBJECT (item), "activate",
322                          G_CALLBACK (my_print_cb), NULL);
323        item = glade_xml_get_widget (gui, "menu_print_preview_item");
324        g_return_val_if_fail (item, FALSE);
325        g_signal_connect (G_OBJECT (item), "activate",
326                          G_CALLBACK (my_print_preview_cb), NULL);
327        item = glade_xml_get_widget (gui, "menu_print_setup_item");
328        g_return_val_if_fail (item, FALSE);
329        g_signal_connect (G_OBJECT (item), "activate",
330                          G_CALLBACK (my_print_setup_cb), NULL);
331        item = glade_xml_get_widget (gui, "menu_tree_item");
332        g_return_val_if_fail (item, FALSE);
333        g_signal_connect (G_OBJECT (item), "activate",
334                          G_CALLBACK (my_tree_cb), NULL);
335        item = glade_xml_get_widget (gui, "menu_font_item");
336        g_return_val_if_fail (item, FALSE);
337        g_signal_connect (G_OBJECT (item), "activate",
338                          G_CALLBACK (my_font_dialog_cb), NULL);
339
340        app->status_bar = glade_xml_get_widget (gui, "statusbar");
341        app->doc1 = my_new_doc ("doc1", gui);
342        app->doc2 = my_new_doc ("doc2", gui);
343        app->doc3 = my_new_doc ("doc3", gui);
344        app->active_doc = NULL;
345        gtk_widget_grab_focus (app->doc1->view);
346       
347        return TRUE;
348}
349
350int
351main (int argc, char * argv[])
352{
353        gtk_init (&argc, &argv);
354
355        app = g_new (MyApp, 1);
356       
357        if (!my_app_load ())
358                return -1;
359
360        gtk_main ();
361
362        g_free (app);
363       
364        return 0;
365}
Note: See TracBrowser for help on using the repository browser.