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

Revision 20964, 9.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20963, 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#define GNOME_PRINT_UNSTABLE_API
32
33#include <libgnomeprint/gnome-print.h>
34#include <libgnomeprint/gnome-print-job.h>
35#include <libgnomeprint/gnome-print-config.h>
36#include <libgnomeprint/private/gpa-root.h>
37#include <libgnomeprintui/gnome-print-job-preview.h>
38#include <libgnomeprintui/gnome-print-dialog.h>
39#include <libgnomeprintui/gnome-print-paper-selector.h>
40#include <libgnomeprintui/gnome-font-dialog.h>
41#include <libgnomeprintui/gpaui/gpa-tree-viewer.h>
42
43#include <gtk/gtk.h>
44#include <glade/glade.h>
45
46typedef struct {
47        GtkWidget *view;
48        GnomePrintConfig *config;
49        gchar *name;
50} MyDoc;
51
52typedef struct {
53        GtkWidget *status_bar;
54
55        MyDoc *active_doc; /* The active document */
56       
57        MyDoc *doc1;
58        MyDoc *doc2;
59        MyDoc *doc3;
60} MyApp;
61
62static MyApp *app;
63
64static gboolean
65my_status_bar_pop (gpointer data)
66{
67        guint id = GPOINTER_TO_UINT (data);
68        gtk_statusbar_remove (GTK_STATUSBAR (app->status_bar), 0, id);
69        return FALSE;
70}
71
72static void
73my_status_bar_print (const gchar *message)
74{
75        guint num;
76        num = gtk_statusbar_push (GTK_STATUSBAR (app->status_bar), 0, message);
77        g_timeout_add (3000, my_status_bar_pop, GUINT_TO_POINTER (num));
78}
79
80static void
81my_print_image_from_pixbuf (GnomePrintContext *gpc, GdkPixbuf *pixbuf)
82{
83        guchar *raw_image;
84        gboolean has_alpha;
85        gint rowstride, height, width;
86       
87        raw_image = gdk_pixbuf_get_pixels (pixbuf);
88        has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
89        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
90        height    = gdk_pixbuf_get_height (pixbuf);
91        width     = gdk_pixbuf_get_width (pixbuf);
92       
93        if (has_alpha)
94                gnome_print_rgbaimage (gpc, (char *)raw_image, width, height, rowstride);
95        else
96                gnome_print_rgbimage (gpc, (char *)raw_image, width, height, rowstride);
97}
98
99static void
100my_print_image_from_disk (GnomePrintContext *gpc)
101{
102        GdkPixbuf *pixbuf;
103        GError *error;
104
105        error = NULL;
106        pixbuf = gdk_pixbuf_new_from_file ("sample-image.png", &error);
107        if (!pixbuf) {
108                g_print ("Could not load sample_image.png.\n");
109                return;
110        }
111
112        gnome_print_gsave (gpc);
113        gnome_print_scale (gpc, 144, 144);
114       
115        my_print_image_from_pixbuf (gpc, pixbuf);
116       
117        gnome_print_grestore (gpc);
118        g_object_unref (G_OBJECT (pixbuf));
119}
120
121static void
122my_draw (GnomePrintContext *gpc, gint page)
123{
124        GnomeFont *font;
125        gchar *t;
126        gchar *page_name;
127
128        font = gnome_font_find_closest ("Sans Regular", 18);
129        g_assert (font);
130
131        page_name = g_strdup_printf ("%d", page);
132        gnome_print_beginpage (gpc, page_name);
133        g_free (page_name);
134
135        gnome_print_moveto (gpc, 1, 1);
136        gnome_print_lineto (gpc, 200, 200);
137        gnome_print_stroke (gpc);
138
139        gnome_print_setfont (gpc, font);
140        gnome_print_moveto  (gpc, 200, 72);
141        t = g_strdup_printf ("Page: %d\n", page + 1);
142        gnome_print_show    (gpc, t);
143        g_free (t);
144
145        my_print_image_from_disk (gpc);
146        gnome_print_showpage (gpc);
147}
148
149
150static void
151my_print (GnomePrintJob *job, gboolean preview)
152{
153        GnomePrintContext *gpc;
154        gint i;
155
156        gpc = gnome_print_job_get_context (job);
157        for (i = 0; i < 4; i++)
158                my_draw (gpc, i);
159        g_object_unref (G_OBJECT (gpc));
160       
161        gnome_print_job_close (job);
162
163        if (!preview) {
164                my_status_bar_print ("Printing ...");
165                gnome_print_job_print (job);
166        } else {
167                my_status_bar_print ("Print previewing ...");
168                gtk_widget_show (gnome_print_job_preview_new (job, "Title goes here"));
169        }
170}
171
172static void
173my_print_cb (void)
174{
175        GnomePrintJob *job;
176        GtkWidget *dialog;
177        gint response;
178
179        /* Create the objects */
180        g_assert (app->active_doc);
181        job    = gnome_print_job_new (app->active_doc->config);
182        dialog = gnome_print_dialog_new (job, "Sample print dialog",
183                                         GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
184        gnome_print_dialog_construct_range_page (GNOME_PRINT_DIALOG (dialog),
185                                                 GNOME_PRINT_RANGE_ALL | GNOME_PRINT_RANGE_SELECTION,
186                                                 1, 2, "A", "Lines");
187
188        response = gnome_print_dialog_run (GNOME_PRINT_DIALOG (dialog));
189        switch (response) {
190        case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
191                my_print (job, FALSE);
192                break;
193        case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
194                my_print (job, TRUE);
195                break;
196        default:
197                return;
198        }
199
200        g_object_unref (G_OBJECT (job));
201}
202
203static void
204my_print_preview_cb (void)
205{
206        GnomePrintJob *job;
207
208        job = gnome_print_job_new (app->active_doc->config);
209        my_print (job, TRUE);
210        g_object_unref (G_OBJECT (job));
211}
212
213static void
214my_print_setup_cb (void)
215{
216        GtkWidget *dialog;
217        GtkWidget *paper_selector;
218        GtkWidget *notebook;
219        GtkWidget *label;
220        gint response;
221
222        notebook = gtk_notebook_new ();
223
224        /* GnomePrintPaperSelector */
225        paper_selector = gnome_paper_selector_new_with_flags (app->active_doc->config, 0);
226        gtk_widget_set_usize (paper_selector, 200, 400);
227        label = gtk_label_new_with_mnemonic ("P_aper");
228        gtk_notebook_append_page (GTK_NOTEBOOK (notebook), paper_selector, label);
229
230        /* GnomePrintPaperSelector */
231        paper_selector = gnome_paper_selector_new_with_flags (app->active_doc->config, 0);
232        gtk_widget_set_usize (paper_selector, 200, 400);
233        label = gtk_label_new_with_mnemonic ("_foo");
234        gtk_notebook_append_page (GTK_NOTEBOOK (notebook), paper_selector, label);
235
236        /* Dialog */
237        dialog = gtk_dialog_new_with_buttons ("Printer Setup", NULL, 0,
238                                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
239                                              GTK_STOCK_OK,     GTK_RESPONSE_OK,
240                                              NULL);
241        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
242                            notebook, TRUE, TRUE, 0);
243        gtk_widget_show_all (dialog);
244
245       
246        response = gtk_dialog_run (GTK_DIALOG (dialog));
247        gtk_widget_destroy (GTK_WIDGET (dialog));
248}
249
250static void
251my_font_dialog_cb (void)
252{
253        static GnomeFont *font = NULL;
254        GnomeFontSelection *fontsel;
255        GtkWidget *dialog;
256
257        dialog = gnome_font_dialog_new ("Sample Font dialog");
258        fontsel = (GnomeFontSelection *) gnome_font_dialog_get_fontsel (GNOME_FONT_DIALOG (dialog));
259        if (font)
260                gnome_font_selection_set_font (fontsel, font);
261
262        gtk_widget_show_all (dialog);
263        gtk_dialog_run (GTK_DIALOG (dialog));
264
265        if (font)
266                g_object_unref (G_OBJECT (font));
267        font = gnome_font_selection_get_font (fontsel);
268
269        gtk_widget_destroy (dialog);
270}
271
272
273static void
274my_tree_cb (void)
275{
276        GtkWidget *dialog;
277
278        dialog = gpa_tree_viewer_new (GPA_NODE (gpa_root));
279}
280
281
282/**
283 * my_document_activate_cb:
284 * @document:
285 * @data:
286 *
287 * This function is called when a document is activated. We set the active document here
288 * and update the GnomePrintWidgets with the active document config
289 **/
290static void
291my_document_activate_cb (GtkWidget *view, MyDoc *doc)
292{
293        gchar *message;
294
295        if (app->active_doc == doc)
296                return;
297        app->active_doc = doc;
298        message = g_strdup_printf ("The active document is now \"%s\"", doc->name);
299        my_status_bar_print (message);
300        g_free (message);
301}
302
303/**
304 * my_new_doc:
305 * @doc_name:
306 * @gui:
307 *
308 * Creates a new document
309 *
310 * Return Value: a pointer to the new document, aborts on error
311 **/
312static MyDoc *
313my_new_doc (const gchar *doc_name, GladeXML *gui)
314{
315        MyDoc *doc;
316
317        doc = g_new (MyDoc, 1);
318        doc->view = glade_xml_get_widget (gui, doc_name);
319        doc->name = g_strdup (doc_name);
320        doc->config = gnome_print_config_default ();
321        g_signal_connect (G_OBJECT (doc->view), "grab_focus",
322                          G_CALLBACK (my_document_activate_cb), doc);
323
324        g_assert (doc->view);
325        g_assert (doc->config);
326       
327        return doc;
328}
329
330static gboolean
331my_app_load (void)
332{
333        GladeXML *gui = glade_xml_new ("example_10.glade", NULL, NULL);
334        GtkWidget *item;
335
336        if (!gui) {
337                g_warning ("Could not load glade file\n");
338                return FALSE;
339        }
340
341        /* Connect menu items to callbacks */
342        item = glade_xml_get_widget (gui, "menu_quit_item");
343        g_return_val_if_fail (item, FALSE);
344        g_signal_connect (G_OBJECT (item), "activate",
345                          G_CALLBACK (gtk_main_quit), NULL);
346        item = glade_xml_get_widget (gui, "window1");
347        g_return_val_if_fail (item, FALSE);
348        g_signal_connect (G_OBJECT (item), "delete_event",
349                          G_CALLBACK (gtk_main_quit), NULL);
350        item = glade_xml_get_widget (gui, "menu_print_item");
351        g_return_val_if_fail (item, FALSE);
352        g_signal_connect (G_OBJECT (item), "activate",
353                          G_CALLBACK (my_print_cb), NULL);
354        item = glade_xml_get_widget (gui, "menu_print_preview_item");
355        g_return_val_if_fail (item, FALSE);
356        g_signal_connect (G_OBJECT (item), "activate",
357                          G_CALLBACK (my_print_preview_cb), NULL);
358        item = glade_xml_get_widget (gui, "menu_print_setup_item");
359        g_return_val_if_fail (item, FALSE);
360        g_signal_connect (G_OBJECT (item), "activate",
361                          G_CALLBACK (my_print_setup_cb), NULL);
362        item = glade_xml_get_widget (gui, "menu_tree_item");
363        g_return_val_if_fail (item, FALSE);
364        g_signal_connect (G_OBJECT (item), "activate",
365                          G_CALLBACK (my_tree_cb), NULL);
366        item = glade_xml_get_widget (gui, "menu_font_item");
367        g_return_val_if_fail (item, FALSE);
368        g_signal_connect (G_OBJECT (item), "activate",
369                          G_CALLBACK (my_font_dialog_cb), NULL);
370
371        app->status_bar = glade_xml_get_widget (gui, "statusbar");
372        app->doc1 = my_new_doc ("doc1", gui);
373#if 0   
374        app->doc2 = my_new_doc ("doc2", gui);
375        app->doc3 = my_new_doc ("doc3", gui);
376#endif
377        app->active_doc = NULL;
378        gtk_widget_grab_focus (app->doc1->view);
379       
380        return TRUE;
381}
382
383int
384main (int argc, char * argv[])
385{
386        gtk_init (&argc, &argv);
387
388        app = g_new (MyApp, 1);
389       
390        if (!my_app_load ())
391                return -1;
392
393        gtk_main ();
394
395        g_free (app);
396       
397        return 0;
398}
Note: See TracBrowser for help on using the repository browser.