File: planner-print-job.c

package info (click to toggle)
planner 0.14.6-7
  • links: PTS
  • area: main
  • in suites: buster
  • size: 14,600 kB
  • sloc: ansic: 67,239; sh: 10,242; xml: 3,928; sql: 886; makefile: 665; python: 3
file content (605 lines) | stat: -rw-r--r-- 14,415 bytes parent folder | download | duplicates (4)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2003 CodeFactory AB
 * Copyright (C) 2001-2003 Richard Hult <richard@imendio.com>
 * Copyright (C) 2001-2003 Mikael Hallendal <micke@imendio.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <string.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include "planner-print-job.h"
#include "planner-print-dialog.h"
#include "planner-view.h"

#define MARGIN 0

struct _PlannerPrintJobPriv {
        gchar         *header;
        gchar         *footer;

	gdouble        paper_width;
	gdouble        paper_height;

	gdouble        header_height;
	gdouble        footer_height;

	PangoFontDescription     *font;
	gdouble        font_height;

	PangoFontDescription     *bold_font;

	/* The font in use, one of the above. */
	PangoFontDescription     *current_font;

	gint           active_page;
	gint           total_pages;

	GList         *views;
};

static void     print_job_class_init          (PlannerPrintJobClass *klass);
static void     print_job_init                (PlannerPrintJob      *job);
static void     print_job_finalize            (GObject         *object);
static void     print_job_transform           (PlannerPrintJob      *job,
					       gdouble         *x,
					       gdouble         *y);
static void     print_job_update_size         (PlannerPrintJob      *job);


static GObjectClass *parent_class = NULL;

GType
planner_print_job_get_type (void)
{
        static GType type = 0;

        if (!type) {
                static const GTypeInfo info =  {
                        sizeof (PlannerPrintJobClass),
                        NULL,           /* base_init */
                        NULL,           /* base_finalize */
                        (GClassInitFunc) print_job_class_init,
                        NULL,           /* class_finalize */
                        NULL,           /* class_data */
                        sizeof (PlannerPrintJob),
                        0,              /* n_preallocs */
                        (GInstanceInitFunc) print_job_init
                };

                type = g_type_register_static (G_TYPE_OBJECT,
					       "PlannerPrintJob", &info, 0);
        }

        return type;
}

static void
print_job_class_init (PlannerPrintJobClass *klass)
{
        GObjectClass *o_class;

	parent_class = g_type_class_peek_parent (klass);

        o_class = G_OBJECT_CLASS (klass);
        o_class->finalize = print_job_finalize;
}

static void
print_job_init (PlannerPrintJob *job)
{
        PlannerPrintJobPriv   *priv;

        priv = g_new0 (PlannerPrintJobPriv, 1);
	job->priv = priv;
}

static void
print_job_finalize (GObject *object)
{
        PlannerPrintJob     *job = PLANNER_PRINT_JOB (object);
	PlannerPrintJobPriv *priv = job->priv;

	g_object_unref (job->operation);
	pango_font_description_free (priv->font);
	pango_font_description_free (priv->bold_font);

        g_free (job->priv);

	if (G_OBJECT_CLASS (parent_class)->finalize) {
		(* G_OBJECT_CLASS (parent_class)->finalize) (object);
	}
}

static void
print_job_transform (PlannerPrintJob *job, gdouble *x, gdouble *y)
{
	if (x) {
		*x += MARGIN;
	}

	if (y) {
		*y += MARGIN;
	}
}

static void
print_job_update_size (PlannerPrintJob *job)
{
	PlannerPrintJobPriv *priv;

	priv = job->priv;

	job->height = (priv->paper_height -
		       priv->header_height -
		       priv->footer_height -
		       2 * MARGIN);

	job->width = (priv->paper_width -
		      2 * MARGIN);
}

static GObject *
print_job_create_custom_widget(GtkPrintOperation *operation,
			       gpointer           user_data)
{
	PlannerPrintJob *job;
	PlannerPrintJobPriv *priv;

	job = (PlannerPrintJob *)user_data;
	priv = job->priv;

	gtk_print_operation_set_custom_tab_label(operation, _("Select views"));

	return G_OBJECT(print_dialog_create_page (NULL, NULL, priv->views));
}

static void
print_job_custom_widget_apply(GtkPrintOperation *operation,
			      GtkWidget         *widget,
			      gpointer           user_data)
{
	gboolean summary;
	PlannerPrintJob *job;
	PlannerPrintJobPriv *priv;

	job = (PlannerPrintJob *)user_data;
	priv = job->priv;

	priv->views = planner_print_dialog_get_print_selection(widget, &summary);
}

static void
print_job_begin_print(GtkPrintOperation *operation,
		      GtkPrintContext   *context,
		      gpointer           user_data)
{
	PlannerPrintJob *job;
	PlannerPrintJobPriv *priv;
	GList *l;
	PlannerView *view;
	int n_pages = 0;

	job = (PlannerPrintJob *)user_data;
	priv = job->priv;

	job->cr = gtk_print_context_get_cairo_context (context);
	job->pc = context;

	priv->paper_height = gtk_print_context_get_height (context);
	priv->paper_width = gtk_print_context_get_width (context);

	print_job_update_size (job);

	planner_print_job_set_font_regular (job);
	job->x_pad = planner_print_job_get_extents (job, "#") / 2;

	for (l = priv->views; l; l = l->next) {
		view = l->data;
		planner_view_print_init (view, job);

		n_pages += planner_view_print_get_n_pages (view);
	}
	gtk_print_operation_set_n_pages (operation, (n_pages > 0) ? n_pages : 1);
}

static void
print_job_draw_page (GtkPrintOperation *operation,
		     GtkPrintContext   *context,
		     gint               page_nr,
		     gpointer           user_data)
{
	PlannerPrintJob     *job;
	PlannerPrintJobPriv *priv;
	PlannerView         *v;
	GList               *l;
	gboolean             page_found = FALSE;
	gint                 pages_in_view;

	job = (PlannerPrintJob *)user_data;
	priv = job->priv;

	l = priv->views;

	while (! page_found && l != NULL) {
		v = PLANNER_VIEW (l->data);

		pages_in_view = planner_view_print_get_n_pages (v);

		if (page_nr < pages_in_view) {
			planner_view_print (v, page_nr);
			page_found = TRUE;
		} else {
			page_nr -= pages_in_view;
			l = l->next;
		}
	}
}

static void
print_job_end_print (GtkPrintOperation *operation,
		     GtkPrintContext   *context,
		     gpointer           user_data)
{
	PlannerPrintJob *job;
	PlannerPrintJobPriv *priv;
	GList *l;

	job = (PlannerPrintJob *)user_data;
	priv = job->priv;

	for (l = priv->views; l; l = l->next) {
		planner_view_print_cleanup (PLANNER_VIEW(l->data));
	}
}


PlannerPrintJob *
planner_print_job_new (GtkPrintOperation *gpo, GList *views)
{
        PlannerPrintJob     *job;
	PlannerPrintJobPriv *priv;

        job = g_object_new (PLANNER_TYPE_PRINT_JOB, NULL);

	priv = job->priv;

	job->operation = g_object_ref (gpo);
	gtk_print_operation_set_unit (gpo, GTK_UNIT_POINTS);

	priv->header = NULL;
	priv->footer = NULL;

	priv->font = pango_font_description_from_string ("Sans Regular 6");
	priv->font_height = pango_font_description_get_size (priv->font) / PANGO_SCALE;

	priv->bold_font = pango_font_description_from_string ("Sans Bold 6");

	priv->header_height = 0;
	priv->footer_height = 0;
	priv->views = views;

	g_signal_connect (G_OBJECT (gpo), "create-custom-widget",
			  G_CALLBACK (print_job_create_custom_widget), job);
	g_signal_connect (G_OBJECT (gpo), "custom-widget-apply",
			  G_CALLBACK (print_job_custom_widget_apply), job);
	g_signal_connect (G_OBJECT (gpo), "begin-print",
			  G_CALLBACK (print_job_begin_print), job);
	g_signal_connect (G_OBJECT (gpo), "draw-page",
			  G_CALLBACK (print_job_draw_page), job);
	g_signal_connect (G_OBJECT (gpo), "end-print",
			  G_CALLBACK (print_job_end_print), job);

        return job;
}

void
planner_print_job_set_header (PlannerPrintJob *job, const gchar *header)
{
        PlannerPrintJobPriv *priv;

        g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

        priv = job->priv;

	g_free (priv->header);
	priv->header = NULL;

        if (header) {
                priv->header = g_strdup (header);
		priv->header_height = priv->font_height * 2;
        } else {
		priv->header_height = 0;
	}

	print_job_update_size (job);
}

void
planner_print_job_set_footer (PlannerPrintJob *job, const gchar *footer)
{
        PlannerPrintJobPriv *priv;

        g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

        priv = job->priv;

	g_free (priv->footer);
	priv->footer = NULL;

        if (footer) {
                priv->footer = g_strdup (footer);
		priv->footer_height = priv->font_height * 2;
        } else {
		priv->footer_height = 0;
	}

	print_job_update_size (job);
}

void
planner_print_job_set_total_pages (PlannerPrintJob *job, gint total_pages)
{
        g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

        job->priv->total_pages = total_pages;
}

void
planner_print_job_moveto (PlannerPrintJob *job, gdouble x, gdouble y)
{
	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	print_job_transform (job, &x, &y);

	cairo_move_to (job->cr, x, y);
}

void
planner_print_job_lineto (PlannerPrintJob *job, gdouble x, gdouble y)
{
	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	print_job_transform (job, &x, &y);

	cairo_line_to (job->cr, x, y);
}

void
planner_print_job_text (PlannerPrintJob *job,
			gint x,
			gint y,
			const char      *str)
{
	g_return_if_fail (job != NULL);
	g_return_if_fail (str != NULL);

	PangoLayout *layout = gtk_print_context_create_pango_layout (job->pc);
	PlannerPrintJobPriv *priv;

	priv = job->priv;

	pango_layout_set_font_description (layout, priv->current_font);
	pango_layout_set_text (layout, str, -1);

	pango_layout_context_changed (layout);

	planner_print_job_moveto (job, x, y - priv->font_height);

	pango_cairo_show_layout (job->cr, layout);

	g_object_unref (layout);
}

void
planner_print_job_show_clipped (PlannerPrintJob *job,
				gdouble          x,
				gdouble          y,
				const gchar     *str,
				gdouble          x1,
				gdouble          y1,
				gdouble          x2,
				gdouble          y2)
{
	PlannerPrintJobPriv *priv;
	gint height;

	priv = job->priv;

	x1 = MAX (x1, 0);
	x2 = MIN (x2, job->width);
	y1 = MAX (y1, 0);
	y2 = MIN (y2, job->height);

	/* Don't try to print anything if the text starts outside the clip rect. */
	if (x < x1 || x > x2) {
		return;
	}

	cairo_save (job->cr);

	cairo_set_line_width (job->cr, 1);

	cairo_new_path (job->cr);
	planner_print_job_moveto (job, x1, y1);
	planner_print_job_lineto (job, x1, y2);
	planner_print_job_lineto (job, x2, y2);
	planner_print_job_lineto (job, x2, y1);

	cairo_close_path (job->cr);

	cairo_clip (job->cr);

	PangoLayout *layout = gtk_print_context_create_pango_layout (job->pc);

	pango_layout_set_text (layout, str, strlen(str));
	pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
	pango_layout_set_width (layout, (x2 - x1) * PANGO_SCALE);

	pango_layout_set_font_description (layout, priv->current_font);
	pango_layout_context_changed (layout);

	pango_layout_get_pixel_size (layout, NULL, &height);

	planner_print_job_moveto (job, x, y - priv->font_height);

	pango_cairo_show_layout (job->cr, layout);

	g_object_unref (layout);

	cairo_restore (job->cr);
}

gboolean
planner_print_job_begin_next_page (PlannerPrintJob *job)
{
	PlannerPrintJobPriv *priv;

	g_return_val_if_fail (PLANNER_IS_PRINT_JOB (job), FALSE);

	priv = job->priv;

	if ((priv->active_page + 1) > priv->total_pages) {
		return FALSE;
	}

	priv->active_page++;

	/* Draw header and footer, FIXME: implement. */
/*
	cairo_new_path (job->cr);

	planner_print_job_set_font_regular (job);

	cairo_set_line_width (job->cr, THIN_LINE_WIDTH);

	planner_print_job_moveto (job, 0, 0);
	planner_print_job_lineto (job, job->width, 0);
	planner_print_job_lineto (job, job->width, job->height);
	planner_print_job_lineto (job, 0, job->height);
	cairo_close_path (job->cr);

#if 0
	cairo_save (job->cr);
	cairo_set_line_width (job->cr, 3);
	cairo_stroke (job->cr);
	cairo_restore (job->cr);
#endif
	cairo_clip (job->cr);
	*/
	return TRUE;
}

void
planner_print_job_finish_page (PlannerPrintJob *job, gboolean draw_border)
{
	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	if (draw_border) {
		cairo_new_path (job->cr);

		cairo_set_line_width (job->cr, THIN_LINE_WIDTH);

		planner_print_job_moveto (job, 0, 0);
		planner_print_job_lineto (job, job->width, 0);
		planner_print_job_lineto (job, job->width, job->height);
		planner_print_job_lineto (job, 0, job->height);
		planner_print_job_lineto (job, 0, 0);
		cairo_close_path (job->cr);

		cairo_stroke (job->cr);
	}
}

PangoFontDescription *
planner_print_job_get_font (PlannerPrintJob *job)
{
	g_return_val_if_fail (PLANNER_IS_PRINT_JOB (job), NULL);

	return job->priv->font;
}

gdouble
planner_print_job_get_font_height (PlannerPrintJob *job)
{
	g_return_val_if_fail (PLANNER_IS_PRINT_JOB (job), 0);

	return job->priv->font_height;
}

void
planner_print_job_set_font_regular (PlannerPrintJob *job)
{
	PlannerPrintJobPriv *priv;

	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	priv = job->priv;

	priv->current_font = priv->font;
}

void
planner_print_job_set_font_bold (PlannerPrintJob *job)
{
	PlannerPrintJobPriv *priv;

	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	priv = job->priv;

	priv->current_font = priv->bold_font;
}

void
planner_print_job_set_font_italic (PlannerPrintJob *job)
{
	PlannerPrintJobPriv *priv;

	g_return_if_fail (PLANNER_IS_PRINT_JOB (job));

	priv = job->priv;

	/* FIXME: use italic. */

	priv->current_font = priv->bold_font;
}

gdouble
planner_print_job_get_extents (PlannerPrintJob *job, char *text)
{
	PangoLayout *layout = gtk_print_context_create_pango_layout (job->pc);
	PlannerPrintJobPriv *priv;
	PangoRectangle ink;

	priv = job->priv;

	pango_layout_set_font_description (layout, priv->current_font);
	pango_layout_set_text (layout, text, -1);

        pango_cairo_update_layout (gtk_print_context_get_cairo_context(job->pc), layout);
	pango_layout_context_changed (layout);

	pango_layout_get_extents (layout, &ink, NULL);

	g_object_unref (layout);

	return ((gdouble)ink.width / PANGO_SCALE);
}