[18337] | 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 | |
---|
[18603] | 30 | #define WE_ARE_LIBGNOMEPRINT_INTERNALS /* Needed for gpa-tree-viewer which is not public */ |
---|
[20963] | 31 | #define GNOME_PRINT_UNSTABLE_API |
---|
[18603] | 32 | |
---|
[18337] | 33 | #include <libgnomeprint/gnome-print.h> |
---|
[18603] | 34 | #include <libgnomeprint/gnome-print-job.h> |
---|
| 35 | #include <libgnomeprint/gnome-print-config.h> |
---|
[20963] | 36 | #include <libgnomeprint/private/gpa-root.h> |
---|
[18603] | 37 | #include <libgnomeprintui/gnome-print-job-preview.h> |
---|
[18337] | 38 | #include <libgnomeprintui/gnome-print-dialog.h> |
---|
[18603] | 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> |
---|
[18337] | 44 | #include <glade/glade.h> |
---|
| 45 | |
---|
[18603] | 46 | typedef struct { |
---|
| 47 | GtkWidget *view; |
---|
| 48 | GnomePrintConfig *config; |
---|
| 49 | gchar *name; |
---|
| 50 | } MyDoc; |
---|
| 51 | |
---|
| 52 | typedef 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 | |
---|
| 62 | static MyApp *app; |
---|
| 63 | |
---|
| 64 | static gboolean |
---|
| 65 | my_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 | |
---|
[18337] | 72 | static void |
---|
[18603] | 73 | my_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 | |
---|
| 80 | static void |
---|
| 81 | my_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 | |
---|
| 99 | static void |
---|
| 100 | my_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 | |
---|
| 121 | static void |
---|
[20963] | 122 | my_draw (GnomePrintContext *gpc, gint page) |
---|
[18337] | 123 | { |
---|
[20963] | 124 | GnomeFont *font; |
---|
| 125 | gchar *t; |
---|
| 126 | gchar *page_name; |
---|
[18337] | 127 | |
---|
[20963] | 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 | |
---|
[18603] | 135 | gnome_print_moveto (gpc, 1, 1); |
---|
[18337] | 136 | gnome_print_lineto (gpc, 200, 200); |
---|
[18603] | 137 | gnome_print_stroke (gpc); |
---|
[18337] | 138 | |
---|
[20963] | 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 | |
---|
[18603] | 145 | my_print_image_from_disk (gpc); |
---|
[18337] | 146 | gnome_print_showpage (gpc); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | static void |
---|
[18603] | 151 | my_print (GnomePrintJob *job, gboolean preview) |
---|
[18337] | 152 | { |
---|
| 153 | GnomePrintContext *gpc; |
---|
[20963] | 154 | gint i; |
---|
[18337] | 155 | |
---|
[18603] | 156 | gpc = gnome_print_job_get_context (job); |
---|
[20963] | 157 | for (i = 0; i < 4; i++) |
---|
| 158 | my_draw (gpc, i); |
---|
[18603] | 159 | g_object_unref (G_OBJECT (gpc)); |
---|
| 160 | |
---|
| 161 | gnome_print_job_close (job); |
---|
[18337] | 162 | |
---|
[18603] | 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 | } |
---|
[18337] | 170 | } |
---|
| 171 | |
---|
| 172 | static void |
---|
| 173 | my_print_cb (void) |
---|
| 174 | { |
---|
[18603] | 175 | GnomePrintJob *job; |
---|
[18337] | 176 | GtkWidget *dialog; |
---|
| 177 | gint response; |
---|
| 178 | |
---|
| 179 | /* Create the objects */ |
---|
[18603] | 180 | g_assert (app->active_doc); |
---|
| 181 | job = gnome_print_job_new (app->active_doc->config); |
---|
[20963] | 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"); |
---|
[18337] | 187 | |
---|
[20963] | 188 | response = gnome_print_dialog_run (GNOME_PRINT_DIALOG (dialog)); |
---|
[18337] | 189 | switch (response) { |
---|
| 190 | case GNOME_PRINT_DIALOG_RESPONSE_PRINT: |
---|
[18603] | 191 | my_print (job, FALSE); |
---|
[18337] | 192 | break; |
---|
| 193 | case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW: |
---|
[18603] | 194 | my_print (job, TRUE); |
---|
[18337] | 195 | break; |
---|
| 196 | default: |
---|
| 197 | return; |
---|
| 198 | } |
---|
[18603] | 199 | |
---|
| 200 | g_object_unref (G_OBJECT (job)); |
---|
[18337] | 201 | } |
---|
| 202 | |
---|
| 203 | static void |
---|
| 204 | my_print_preview_cb (void) |
---|
| 205 | { |
---|
[18603] | 206 | GnomePrintJob *job; |
---|
[18337] | 207 | |
---|
[18603] | 208 | job = gnome_print_job_new (app->active_doc->config); |
---|
| 209 | my_print (job, TRUE); |
---|
| 210 | g_object_unref (G_OBJECT (job)); |
---|
[18337] | 211 | } |
---|
| 212 | |
---|
| 213 | static void |
---|
| 214 | my_print_setup_cb (void) |
---|
| 215 | { |
---|
[18603] | 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)); |
---|
[18337] | 248 | } |
---|
| 249 | |
---|
[18603] | 250 | static void |
---|
| 251 | my_font_dialog_cb (void) |
---|
| 252 | { |
---|
[20963] | 253 | static GnomeFont *font = NULL; |
---|
| 254 | GnomeFontSelection *fontsel; |
---|
[18603] | 255 | GtkWidget *dialog; |
---|
| 256 | |
---|
| 257 | dialog = gnome_font_dialog_new ("Sample Font dialog"); |
---|
[20963] | 258 | fontsel = (GnomeFontSelection *) gnome_font_dialog_get_fontsel (GNOME_FONT_DIALOG (dialog)); |
---|
| 259 | if (font) |
---|
| 260 | gnome_font_selection_set_font (fontsel, font); |
---|
[18603] | 261 | |
---|
| 262 | gtk_widget_show_all (dialog); |
---|
| 263 | gtk_dialog_run (GTK_DIALOG (dialog)); |
---|
[20963] | 264 | |
---|
| 265 | if (font) |
---|
| 266 | g_object_unref (G_OBJECT (font)); |
---|
| 267 | font = gnome_font_selection_get_font (fontsel); |
---|
| 268 | |
---|
[18603] | 269 | gtk_widget_destroy (dialog); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | static void |
---|
| 274 | my_tree_cb (void) |
---|
| 275 | { |
---|
| 276 | GtkWidget *dialog; |
---|
| 277 | |
---|
[20963] | 278 | dialog = gpa_tree_viewer_new (GPA_NODE (gpa_root)); |
---|
[18603] | 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 | **/ |
---|
| 290 | static void |
---|
| 291 | my_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 | **/ |
---|
| 312 | static MyDoc * |
---|
| 313 | my_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 | |
---|
[18337] | 330 | static gboolean |
---|
| 331 | my_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 | |
---|
[18603] | 341 | /* Connect menu items to callbacks */ |
---|
[18337] | 342 | item = glade_xml_get_widget (gui, "menu_quit_item"); |
---|
| 343 | g_return_val_if_fail (item, FALSE); |
---|
[18603] | 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); |
---|
[18337] | 350 | item = glade_xml_get_widget (gui, "menu_print_item"); |
---|
| 351 | g_return_val_if_fail (item, FALSE); |
---|
[18603] | 352 | g_signal_connect (G_OBJECT (item), "activate", |
---|
| 353 | G_CALLBACK (my_print_cb), NULL); |
---|
[18337] | 354 | item = glade_xml_get_widget (gui, "menu_print_preview_item"); |
---|
| 355 | g_return_val_if_fail (item, FALSE); |
---|
[18603] | 356 | g_signal_connect (G_OBJECT (item), "activate", |
---|
| 357 | G_CALLBACK (my_print_preview_cb), NULL); |
---|
[18337] | 358 | item = glade_xml_get_widget (gui, "menu_print_setup_item"); |
---|
| 359 | g_return_val_if_fail (item, FALSE); |
---|
[18603] | 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); |
---|
[18337] | 370 | |
---|
[18603] | 371 | app->status_bar = glade_xml_get_widget (gui, "statusbar"); |
---|
| 372 | app->doc1 = my_new_doc ("doc1", gui); |
---|
[20963] | 373 | #if 0 |
---|
[18603] | 374 | app->doc2 = my_new_doc ("doc2", gui); |
---|
| 375 | app->doc3 = my_new_doc ("doc3", gui); |
---|
[20963] | 376 | #endif |
---|
[18603] | 377 | app->active_doc = NULL; |
---|
| 378 | gtk_widget_grab_focus (app->doc1->view); |
---|
[18337] | 379 | |
---|
| 380 | return TRUE; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | int |
---|
| 384 | main (int argc, char * argv[]) |
---|
| 385 | { |
---|
| 386 | gtk_init (&argc, &argv); |
---|
| 387 | |
---|
[18603] | 388 | app = g_new (MyApp, 1); |
---|
| 389 | |
---|
[18337] | 390 | if (!my_app_load ()) |
---|
| 391 | return -1; |
---|
| 392 | |
---|
| 393 | gtk_main (); |
---|
| 394 | |
---|
[18603] | 395 | g_free (app); |
---|
| 396 | |
---|
[18337] | 397 | return 0; |
---|
| 398 | } |
---|