~nik90/ubuntu/precise/eog/keywords

1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
1
/* Eye of Gnome - Print Operations
2
 *
3
 * Copyright (C) 2005-2008 The Free Software Foundation
4
 *
5
 * Author: Claudio Saavedra <csaavedra@gnome.org>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 */
21
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
22
#include "config.h"
23
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
24
#include <gtk/gtk.h>
25
#include <glib/gi18n.h>
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
26
#include "eog-image.h"
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
27
#include "eog-print.h"
28
#include "eog-print-image-setup.h"
29
#include "eog-util.h"
30
#include "eog-debug.h"
31
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
32
#ifdef HAVE_RSVG
33
#include <librsvg/rsvg-cairo.h>
34
#endif
35
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
36
#define EOG_PRINT_SETTINGS_FILE "eog-print-settings.ini"
37
#define EOG_PAGE_SETUP_GROUP "Page Setup"
38
#define EOG_PRINT_SETTINGS_GROUP "Print Settings"
39
40
typedef struct {
41
	EogImage *image;
42
	gdouble left_margin;
43
	gdouble top_margin;
44
	gdouble scale_factor;
45
	GtkUnit unit;
46
} EogPrintData;
47
48
static void
49
eog_print_draw_page (GtkPrintOperation *operation,
50
		     GtkPrintContext   *context,
51
		     gint               page_nr,
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
52
		     gpointer           user_data)
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
53
{
54
	cairo_t *cr;
55
	gdouble dpi_x, dpi_y;
56
	gdouble x0, y0;
57
	gdouble scale_factor;
58
	gdouble p_width, p_height;
59
	gint width, height;
60
	EogPrintData *data;
61
	GtkPageSetup *page_setup;
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
62
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
63
	eog_debug (DEBUG_PRINTING);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
64
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
65
	data = (EogPrintData *) user_data;
66
67
	scale_factor = data->scale_factor/100;
68
69
	dpi_x = gtk_print_context_get_dpi_x (context);
70
	dpi_y = gtk_print_context_get_dpi_y (context);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
71
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
72
	switch (data->unit) {
73
	case GTK_UNIT_INCH:
74
		x0 = data->left_margin * dpi_x;
75
		y0 = data->top_margin  * dpi_y;
76
		break;
77
	case GTK_UNIT_MM:
78
		x0 = data->left_margin * dpi_x/25.4;
79
		y0 = data->top_margin  * dpi_y/25.4;
80
		break;
81
	default:
82
		g_assert_not_reached ();
83
	}
84
85
	cr = gtk_print_context_get_cairo_context (context);
86
87
	cairo_translate (cr, x0, y0);
88
89
	page_setup = gtk_print_context_get_page_setup (context);
90
	p_width =  gtk_page_setup_get_page_width (page_setup, GTK_UNIT_POINTS);
91
	p_height = gtk_page_setup_get_page_height (page_setup, GTK_UNIT_POINTS);
92
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
93
	eog_image_get_size (data->image, &width, &height);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
94
95
	/* this is both a workaround for a bug in cairo's PDF backend, and
96
	   a way to ensure we are not printing outside the page margins */
97
	cairo_rectangle (cr, 0, 0, MIN (width*scale_factor, p_width), MIN (height*scale_factor, p_height));
98
	cairo_clip (cr);
99
100
	cairo_scale (cr, scale_factor, scale_factor);
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
101
102
#ifdef HAVE_RSVG
103
	if (eog_image_is_svg (data->image))
104
	{
105
		RsvgHandle *svg = eog_image_get_svg (data->image);
106
107
		rsvg_handle_render_cairo (svg, cr);
108
	} else
109
#endif
110
	{
111
		GdkPixbuf *pixbuf;
112
113
		pixbuf = eog_image_get_pixbuf (data->image);
114
		gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
115
 		cairo_paint (cr);
116
		g_object_unref (pixbuf);
117
	}
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
118
}
119
120
static GObject *
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
121
eog_print_create_custom_widget (GtkPrintOperation *operation,
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
122
				       gpointer user_data)
123
{
124
	GtkPageSetup *page_setup;
125
	EogPrintData *data;
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
126
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
127
	eog_debug (DEBUG_PRINTING);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
128
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
129
	data = (EogPrintData *)user_data;
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
130
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
131
	page_setup = gtk_print_operation_get_default_page_setup (operation);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
132
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
133
	if (page_setup == NULL)
134
		page_setup = gtk_page_setup_new ();
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
135
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
136
	return G_OBJECT (eog_print_image_setup_new (data->image, page_setup));
137
}
138
139
static void
140
eog_print_custom_widget_apply (GtkPrintOperation *operation,
141
			       GtkWidget         *widget,
142
			       gpointer           user_data)
143
{
144
	EogPrintData *data;
145
	gdouble left_margin, top_margin, scale_factor;
146
	GtkUnit unit;
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
147
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
148
	eog_debug (DEBUG_PRINTING);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
149
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
150
	data = (EogPrintData *)user_data;
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
151
152
	eog_print_image_setup_get_options (EOG_PRINT_IMAGE_SETUP (widget),
153
					   &left_margin, &top_margin,
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
154
					   &scale_factor, &unit);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
155
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
156
	data->left_margin = left_margin;
157
	data->top_margin = top_margin;
158
	data->scale_factor = scale_factor;
159
	data->unit = unit;
160
}
161
162
static void
163
eog_print_end_print (GtkPrintOperation *operation,
164
		     GtkPrintContext   *context,
165
		     gpointer           user_data)
166
{
167
	EogPrintData *data = (EogPrintData*) user_data;
168
169
	eog_debug (DEBUG_PRINTING);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
170
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
171
	g_object_unref (data->image);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
172
	g_slice_free (EogPrintData, data);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
173
}
174
175
GtkPrintOperation *
176
eog_print_operation_new (EogImage *image,
177
			 GtkPrintSettings *print_settings,
178
			 GtkPageSetup *page_setup)
179
{
180
	GtkPrintOperation *print;
181
	EogPrintData *data;
182
183
	eog_debug (DEBUG_PRINTING);
184
185
	print = gtk_print_operation_new ();
186
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
187
	data = g_slice_new0 (EogPrintData);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
188
189
	data->left_margin = 0;
190
	data->top_margin = 0;
191
	data->scale_factor = 100;
192
	data->image = g_object_ref (image);
193
	data->unit = GTK_UNIT_INCH;
194
195
	gtk_print_operation_set_print_settings (print, print_settings);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
196
	gtk_print_operation_set_default_page_setup (print,
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
197
						    page_setup);
198
	gtk_print_operation_set_n_pages (print, 1);
199
	gtk_print_operation_set_job_name (print,
200
					  eog_image_get_caption (image));
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
201
	gtk_print_operation_set_embed_page_setup (print, TRUE);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
202
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
203
	g_signal_connect (print, "draw_page",
204
			  G_CALLBACK (eog_print_draw_page),
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
205
			  data);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
206
	g_signal_connect (print, "create-custom-widget",
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
207
			  G_CALLBACK (eog_print_create_custom_widget),
208
			  data);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
209
	g_signal_connect (print, "custom-widget-apply",
210
			  G_CALLBACK (eog_print_custom_widget_apply),
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
211
			  data);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
212
	g_signal_connect (print, "end-print",
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
213
			  G_CALLBACK (eog_print_end_print),
214
			  data);
1.14.8 by Sebastien Bacher
Import upstream version 2.31.5
215
	g_signal_connect (print, "update-custom-widget",
216
			  G_CALLBACK (eog_print_image_setup_update),
217
			  data);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
218
219
	gtk_print_operation_set_custom_tab_label (print, _("Image Settings"));
220
221
	return print;
222
}
223
224
static GKeyFile *
225
eog_print_get_key_file (void)
226
{
227
	GKeyFile *key_file;
228
	GError *error = NULL;
229
	gchar *filename;
230
	GFile *file;
231
	const gchar *dot_dir = eog_util_dot_dir ();
232
233
	filename = g_build_filename (dot_dir, EOG_PRINT_SETTINGS_FILE, NULL);
234
235
	file = g_file_new_for_path (filename);
236
	key_file = g_key_file_new ();
237
238
	if (g_file_query_exists (file, NULL)) {
239
		g_key_file_load_from_file (key_file, filename,
240
					   G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
241
					   &error);
242
		if (error) {
1.1.46 by Sebastien Bacher
Import upstream version 2.23.5
243
			g_warning ("Error loading print settings file: %s", error->message);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
244
			g_error_free (error);
245
			g_object_unref (file);
246
			g_free (filename);
247
			g_key_file_free (key_file);
248
			return NULL;
249
		}
250
	}
251
252
	g_object_unref (file);
253
	g_free (filename);
254
255
	return key_file;
256
}
257
258
GtkPageSetup *
259
eog_print_get_page_setup (void)
260
{
261
	GtkPageSetup *page_setup;
262
	GKeyFile *key_file;
263
	GError *error = NULL;
264
265
	key_file = eog_print_get_key_file ();
266
267
	if (key_file && g_key_file_has_group (key_file, EOG_PAGE_SETUP_GROUP)) {
268
		page_setup = gtk_page_setup_new_from_key_file (key_file, EOG_PAGE_SETUP_GROUP, &error);
269
	} else {
270
		page_setup = gtk_page_setup_new ();
271
	}
272
273
	if (error) {
274
		page_setup = gtk_page_setup_new ();
275
1.1.46 by Sebastien Bacher
Import upstream version 2.23.5
276
		g_warning ("Error loading print settings file: %s", error->message);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
277
		g_error_free (error);
278
	}
279
280
	if (key_file)
281
		g_key_file_free (key_file);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
282
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
283
	return page_setup;
284
}
285
286
static void
287
eog_print_save_key_file (GKeyFile *key_file)
288
{
289
	gchar *filename;
290
	gchar *data;
291
	GError *error = NULL;
292
	const gchar *dot_dir = eog_util_dot_dir ();
293
294
	filename = g_build_filename (dot_dir, EOG_PRINT_SETTINGS_FILE, NULL);
295
296
	data = g_key_file_to_data (key_file, NULL, NULL);
297
298
	g_file_set_contents (filename, data, -1, &error);
299
300
	if (error) {
1.1.46 by Sebastien Bacher
Import upstream version 2.23.5
301
		g_warning ("Error saving print settings file: %s", error->message);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
302
		g_error_free (error);
303
	}
304
305
	g_free (filename);
306
	g_free (data);
307
}
308
309
void
310
eog_print_set_page_setup (GtkPageSetup *page_setup)
311
{
312
	GKeyFile *key_file;
313
314
	key_file = eog_print_get_key_file ();
315
316
	if (key_file == NULL) {
317
		key_file = g_key_file_new ();
318
	}
319
320
	gtk_page_setup_to_key_file (page_setup, key_file, EOG_PAGE_SETUP_GROUP);
321
	eog_print_save_key_file (key_file);
322
323
	g_key_file_free (key_file);
324
}
325
326
GtkPrintSettings *
327
eog_print_get_print_settings (void)
328
{
329
	GtkPrintSettings *print_settings;
330
	GError *error = NULL;
331
	GKeyFile *key_file;
332
333
	key_file = eog_print_get_key_file ();
334
335
	if (key_file && g_key_file_has_group (key_file, EOG_PRINT_SETTINGS_GROUP)) {
336
		print_settings = gtk_print_settings_new_from_key_file (key_file, EOG_PRINT_SETTINGS_GROUP, &error);
337
	} else {
338
		print_settings = gtk_print_settings_new ();
339
	}
340
341
	if (error) {
342
		print_settings = gtk_print_settings_new ();
343
1.1.46 by Sebastien Bacher
Import upstream version 2.23.5
344
		g_warning ("Error loading print settings file: %s", error->message);
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
345
		g_error_free (error);
346
	}
347
348
	if (key_file)
349
		g_key_file_free (key_file);
1.1.53 by Sebastien Bacher
Import upstream version 2.25.3
350
1.1.44 by Sebastien Bacher
Import upstream version 2.23.3
351
	return print_settings;
352
}
353
354
void
355
eog_print_set_print_settings (GtkPrintSettings *print_settings)
356
{
357
	GKeyFile *key_file;
358
359
	key_file = eog_print_get_key_file ();
360
361
	if (key_file == NULL) {
362
		key_file = g_key_file_new ();
363
	}
364
365
	gtk_print_settings_to_key_file (print_settings, key_file, EOG_PRINT_SETTINGS_GROUP);
366
	eog_print_save_key_file (key_file);
367
368
	g_key_file_free (key_file);
369
}