1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * example_09.c: sample gnome-print code. This dialog saves the GnomePrintConfig |
---|
4 | * to disk after printing and loads it before creating the dialog |
---|
5 | * This shows how to implement persistent print configuration. |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU Library General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 of |
---|
10 | * the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU Library General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU Library General Public |
---|
18 | * License along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
20 | * |
---|
21 | * Authors: |
---|
22 | * Chema Celorio <chema@ximian.com> |
---|
23 | * |
---|
24 | * Copyright (C) 2002 Ximian Inc. and authors |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /* |
---|
29 | * See README |
---|
30 | */ |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <string.h> |
---|
34 | #include <libgnomeprint/gnome-print.h> |
---|
35 | #include <gtk/gtk.h> |
---|
36 | #include <libgnomeprintui/gnome-print-dialog.h> |
---|
37 | |
---|
38 | #define CONFIG_FILE "print_config" |
---|
39 | |
---|
40 | static GnomePrintConfig * |
---|
41 | my_config_load_from_file (void) |
---|
42 | { |
---|
43 | GnomePrintConfig *config; |
---|
44 | FILE *file; |
---|
45 | gchar *str; |
---|
46 | gint read, allocated; |
---|
47 | |
---|
48 | file = fopen (CONFIG_FILE, "r"); |
---|
49 | if (!file) { |
---|
50 | g_print ("Config not found\n"); |
---|
51 | return gnome_print_config_default (); |
---|
52 | } |
---|
53 | |
---|
54 | #define BLOCK_SIZE 10 |
---|
55 | read = 0; |
---|
56 | allocated = BLOCK_SIZE; |
---|
57 | str = g_malloc (allocated); |
---|
58 | while (TRUE) { |
---|
59 | gint this; |
---|
60 | this = fread (str + read, sizeof (gchar), allocated - read, file); |
---|
61 | read += this; |
---|
62 | if (this < 1) |
---|
63 | break; |
---|
64 | if ((read + BLOCK_SIZE + 1) > allocated) { |
---|
65 | allocated += BLOCK_SIZE; |
---|
66 | str = g_realloc (str, allocated); |
---|
67 | } |
---|
68 | } |
---|
69 | str[read]=0; |
---|
70 | config = gnome_print_config_from_string (str, 0); |
---|
71 | |
---|
72 | return config; |
---|
73 | } |
---|
74 | |
---|
75 | static void |
---|
76 | my_config_save_to_file (GnomePrintConfig *config) |
---|
77 | { |
---|
78 | FILE *file; |
---|
79 | gchar *str; |
---|
80 | gint bytes; |
---|
81 | |
---|
82 | g_return_if_fail (config); |
---|
83 | |
---|
84 | str = gnome_print_config_to_string (config, 0); |
---|
85 | g_assert (str); |
---|
86 | |
---|
87 | file = fopen (CONFIG_FILE, "w"); |
---|
88 | g_assert (file); |
---|
89 | |
---|
90 | bytes = strlen (str); |
---|
91 | str += bytes; |
---|
92 | while (bytes > 0) |
---|
93 | bytes -= fwrite (str - bytes, sizeof (gchar), bytes < 1024 ? bytes : 1024, file); |
---|
94 | fclose (file); |
---|
95 | } |
---|
96 | |
---|
97 | static void |
---|
98 | my_print (void) |
---|
99 | { |
---|
100 | GnomePrintConfig *config; |
---|
101 | GnomePrintJob *job; |
---|
102 | GtkWidget *dialog; |
---|
103 | gint response; |
---|
104 | |
---|
105 | config = my_config_load_from_file (); |
---|
106 | job = gnome_print_job_new (config); |
---|
107 | dialog = gnome_print_dialog_new (job, "Example 09 print dialog", 0); |
---|
108 | gtk_widget_show (dialog); |
---|
109 | response = gtk_dialog_run (GTK_DIALOG (dialog)); |
---|
110 | if (response == GNOME_PRINT_DIALOG_RESPONSE_CANCEL) { |
---|
111 | g_print ("Printing was canceled, config not saved\n"); |
---|
112 | return; |
---|
113 | } |
---|
114 | |
---|
115 | g_assert (config); |
---|
116 | g_print ("Config saved to \"%s\"\n", CONFIG_FILE); |
---|
117 | my_config_save_to_file (config); |
---|
118 | |
---|
119 | g_object_unref (job); |
---|
120 | g_object_unref (config); |
---|
121 | } |
---|
122 | |
---|
123 | int |
---|
124 | main (int argc, char * argv[]) |
---|
125 | { |
---|
126 | gtk_init (&argc, &argv); |
---|
127 | |
---|
128 | my_print (); |
---|
129 | |
---|
130 | g_print ("Done...\n"); |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|