svg text alignment
[dia.git] / app / paginate_gnomeprint.c
blob8afe7d5ed95a832b4e75ef53ea5587bc9d440672
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998, 1999 Alexander Larsson
4 * paginate_gnomeprint.[ch] -- pagination code for the gnome-print backend
5 * Copyright (C) 1999 James Henstridge
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.
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.
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.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include "diagram.h"
27 #include "diagramdata.h"
28 #include "libgnome/libgnome.h"
29 #include "paginate_gnomeprint.h"
30 #include "message.h"
32 #include <gnome.h>
33 #include <libgnomeprint/gnome-printer-dialog.h>
35 static void
36 count_objs(Object *obj, Renderer *renderer, int active_layer, guint *nobjs)
38 (*nobjs)++;
42 static guint
43 print_page(DiagramData *data, RendererGPrint *rend, Rectangle *bounds,
44 gint xpos, gint ypos)
46 guint nobjs = 0;
47 gdouble tmargin = data->paper.tmargin, bmargin = data->paper.bmargin;
48 gdouble lmargin = data->paper.lmargin;
49 #ifdef THIS_IS_PROBABLY_DEAD_CODE
50 gdouble rmargin = data->paper.rmargin;
51 #endif
52 gdouble scale = data->paper.scaling;
53 char buf[256];
55 /* count the number of objects in this region */
56 data_render(data, (Renderer *)rend, bounds,
57 (ObjectRenderer) count_objs, &nobjs);
59 if (nobjs == 0)
60 return nobjs;
62 /* give a name to this page */
63 g_snprintf(buf, sizeof(buf), "%d,%d", xpos, ypos);
64 gnome_print_beginpage(rend->ctx, buf);
66 /* save print context */
67 gnome_print_gsave(rend->ctx);
69 /* transform coordinate system */
70 if (data->paper.is_portrait) {
71 gnome_print_scale(rend->ctx, 28.346457 * scale, -28.346457 * scale);
72 gnome_print_translate(rend->ctx, lmargin/scale - bounds->left,
73 -bmargin/scale - bounds->bottom);
74 } else {
75 gnome_print_rotate(rend->ctx, 90);
76 gnome_print_scale(rend->ctx, 28.346457 * scale, -28.346457 * scale);
77 gnome_print_translate(rend->ctx, lmargin/scale - bounds->left,
78 tmargin/scale - bounds->top);
81 /* set up clip mask */
82 gnome_print_newpath(rend->ctx);
83 gnome_print_moveto(rend->ctx, bounds->left, bounds->top);
84 gnome_print_lineto(rend->ctx, bounds->right, bounds->top);
85 gnome_print_lineto(rend->ctx, bounds->right, bounds->bottom);
86 gnome_print_lineto(rend->ctx, bounds->left, bounds->bottom);
87 gnome_print_lineto(rend->ctx, bounds->left, bounds->top);
88 gnome_print_clip(rend->ctx);
90 /* render the region */
91 data_render(data, (Renderer *)rend, bounds, NULL, NULL);
93 /* restore print context */
94 gnome_print_grestore(rend->ctx);
96 /* print the page */
97 gnome_print_showpage(rend->ctx);
99 return nobjs;
102 void
103 paginate_gnomeprint(Diagram *dia, GnomePrintContext *ctx)
105 RendererGPrint *rend;
106 Rectangle *extents;
107 gdouble width, height;
108 gdouble x, y, initx, inity;
109 gint xpos, ypos;
110 guint nobjs = 0;
112 rend = new_gnomeprint_renderer(dia, ctx);
114 /* the usable area of the page */
115 width = dia->data->paper.width;
116 height = dia->data->paper.height;
118 /* get extents, and make them multiples of width / height */
119 extents = &dia->data->extents;
120 initx = extents->left;
121 inity = extents->top;
122 /* make page boundaries align with origin */
123 if (!dia->data->paper.fitto) {
124 initx = floor(initx / width) * width;
125 inity = floor(inity / height) * height;
128 /* iterate through all the pages in the diagram */
129 for (y = inity, ypos = 0; y < extents->bottom; y += height, ypos++)
130 for (x = inity, xpos = 0; x < extents->right; x += width, xpos++) {
131 Rectangle page_bounds;
133 page_bounds.left = x;
134 page_bounds.right = x + width;
135 page_bounds.top = y;
136 page_bounds.bottom = y + height;
138 nobjs += print_page(dia->data, rend, &page_bounds, xpos, ypos);
141 g_free(rend);
143 gnome_print_context_close(ctx);
145 gtk_object_unref(GTK_OBJECT(ctx));
148 void
149 diagram_print_gnome(Diagram *dia)
151 GtkWidget *dialog, *printersel;
152 int btn;
153 GnomePrinter *printer;
154 GnomePrintContext *ctx;
156 dialog = gnome_dialog_new(_("Print Diagram"), GNOME_STOCK_BUTTON_OK,
157 GNOME_STOCK_BUTTON_CANCEL, NULL);
159 printersel = gnome_printer_widget_new();
160 gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), printersel,
161 TRUE, TRUE, 0);
162 gtk_widget_show(printersel);
164 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
165 btn = gnome_dialog_run(GNOME_DIALOG(dialog));
166 if (btn < 0)
167 return;
169 /* cancel was pressed */
170 if (btn == 1) {
171 gtk_widget_destroy(dialog);
172 return;
175 /* get the printer name */
176 printer = gnome_printer_widget_get_printer(GNOME_PRINTER_WIDGET(printersel));
178 ctx = gnome_print_context_new_with_paper_size(printer,dia->data->paper.name);
180 if (ctx != NULL)
181 paginate_gnomeprint(dia, ctx);
182 else
183 message_error(_("An error occured while creating the print context"));
185 gtk_object_unref(GTK_OBJECT(printer));
186 gtk_widget_destroy(dialog);