File: example_09.c

package info (click to toggle)
libgnomeprintui 2.18.5-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,288 kB
  • ctags: 1,978
  • sloc: ansic: 14,824; sh: 12,444; makefile: 281; xml: 27
file content (133 lines) | stat: -rw-r--r-- 3,224 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  example_09.c: sample gnome-print code. This dialog saves the GnomePrintConfig
 *                to disk after printing and loads it before creating the dialog
 *                This shows how to implement persistent print configuration.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors:
 *    Chema Celorio <chema@ximian.com>
 *
 *  Copyright (C) 2002 Ximian Inc. and authors
 *
 */

/*
 * See README
 */

#include <stdio.h>
#include <string.h>
#include <libgnomeprint/gnome-print.h>
#include <gtk/gtk.h>
#include <libgnomeprintui/gnome-print-dialog.h>

#define CONFIG_FILE "print_config"

static GnomePrintConfig *
my_config_load_from_file (void)
{
	GnomePrintConfig *config;
	FILE *file;
	gchar *str;
	gint read, allocated;

	file = fopen (CONFIG_FILE, "r");
	if (!file) {
		g_print ("Config not found\n");
		return gnome_print_config_default ();
	}

#define BLOCK_SIZE 10
	read = 0;
	allocated = BLOCK_SIZE;
	str = g_malloc (allocated);
	while (TRUE) {
		gint this;
		this = fread (str + read, sizeof (gchar), allocated - read, file);
		read += this;
		if (this < 1)
			break;
		if ((read + BLOCK_SIZE + 1) > allocated) {
			allocated += BLOCK_SIZE;
			str = g_realloc (str, allocated);
		}
	}
	str[read]=0;
	config = gnome_print_config_from_string (str, 0);

	return config;
}

static void
my_config_save_to_file (GnomePrintConfig *config)
{
	FILE *file;
	gchar *str;
	gint bytes;

	g_return_if_fail (config);

	str = gnome_print_config_to_string (config, 0);
	g_assert (str);
	
	file = fopen (CONFIG_FILE, "w");
	g_assert (file);
	
	bytes = strlen (str);
	str += bytes;
	while (bytes > 0)
		bytes -=  fwrite (str - bytes, sizeof (gchar), bytes < 1024 ? bytes : 1024, file);
	fclose (file);
}
	
static void
my_print (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	GtkWidget *dialog;
	gint response;

	config = my_config_load_from_file ();
	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, "Example 09 print dialog", 0);
	gtk_widget_show (dialog);
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	if (response == GNOME_PRINT_DIALOG_RESPONSE_CANCEL) {
		g_print ("Printing was canceled, config not saved\n");
		return;
	}

	g_assert (config);
	g_print ("Config saved to \"%s\"\n", CONFIG_FILE);
	my_config_save_to_file (config);

	g_object_unref (job);
	g_object_unref (config);
}

int
main (int argc, char * argv[])
{
	gtk_init (&argc, &argv);
	
	my_print ();

	g_print ("Done...\n");

	return 0;
}