File: bonobo-print-client.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (356 lines) | stat: -rw-r--r-- 8,731 bytes parent folder | download | duplicates (2)
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
/**
 * bonobo-print-client.c: a print client interface for compound documents.
 *
 * Author:
 *    Michael Meeks (mmeeks@gnu.org)
 *
 * Copyright 2000, Helix Code, Inc.
 */
#include <config.h>
#include <stdarg.h>
#include <libart_lgpl/art_affine.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-print-client.h>
#include <libgnomeprint/gnome-print-meta.h>
#include <bonobo/bonobo-stream-client.h>

#undef PRINT_DEBUG

/**
 * bonobo_print_client_render:
 * @client: the print client.
 * @pd: the intialized print data
 * 
 * This routine is used to encourage a remote print client
 * to print itself. The BonoboPrintData specifies the size
 * information for the remote client to render itself to.
 * After render the BonoboPrintData contains the meta data
 * for the rendered page. This interface is baroque.
 **/
void
bonobo_print_client_render (BonoboPrintClient   *client,
			    BonoboPrintData     *pd)
{
	Bonobo_Stream           stream;
	Bonobo_PrintDimensions *p_dim;
	Bonobo_PrintScissor    *p_scissor;
	CORBA_Environment       ev;
	CORBA_long              len;
	guint8                 *data;

	g_return_if_fail (pd != NULL);
	g_return_if_fail (client != NULL);
	g_return_if_fail (BONOBO_IS_PRINT_CLIENT (client));

	CORBA_exception_init (&ev);

	p_scissor = Bonobo_PrintScissor__alloc ();
	p_scissor->width_first_page  = pd->width_first_page;
	p_scissor->width_per_page    = pd->width_per_page;
	p_scissor->height_first_page = pd->height_first_page;
	p_scissor->height_per_page   = pd->height_per_page;

	p_dim = Bonobo_PrintDimensions__alloc ();
	p_dim->width  = pd->width;
	p_dim->height = pd->height;

	stream = Bonobo_Print_render (client->corba_print, p_dim,
				      p_scissor, &ev);

#ifdef PRINT_DEBUG
	{
		int i;

		printf ("Read data %d %d\n", data->_length, data->_maximum);
		for (i = 0; i < data->_length; i++)
			printf ("0x%x ", data->_buffer [i]);
		printf ("\n");
	}
#endif

	CORBA_free (p_dim);
	CORBA_free (p_scissor);

	if (BONOBO_EX (&ev)) {
		g_warning ("Component print exception");
		return;
	}
	if (stream == CORBA_OBJECT_NIL) {
		g_warning ("Component print returns no data");
		return;
	}

	data = bonobo_stream_client_read (stream, -1, &len, &ev);
	if (BONOBO_EX (&ev) || !data)
		g_warning ("Failed to read print data from stream");
	else {
		pd->meta_data = gnome_print_meta_new_from (data);

		g_free (data);
	}

	bonobo_object_release_unref (stream, &ev);

	CORBA_exception_free (&ev);
}

/**
 * bonobo_print_data_get_meta:
 * @pd: the print data
 * 
 * Return value: the meta_data from @pd
 **/
GnomePrintMeta *
bonobo_print_data_get_meta (BonoboPrintData *pd)
{
	g_return_val_if_fail (pd != NULL, NULL);

	if (!pd->meta_data)
		pd->meta_data = gnome_print_meta_new ();

	return pd->meta_data;
}

/**
 * bonobo_print_data_new_full:
 * @width: the width in pts of the component to render
 * @height: the height in pts of the component to render
 * @width_first_page: the clear width available on the first page
 * @width_per_page: the width available on subsequent pages
 * @height_first_page: the clear height available on the first page
 * @height_per_page: the height available on subsequent pages
 * 
 * This initializes a BonoboPrintData to contain the above
 * parameters so that it can be used by #bonobo_print_client_render
 * 
 * Return value: a new BonoboPrintData object.
 **/
BonoboPrintData *
bonobo_print_data_new_full (double               width,
			    double               height,
			    double               width_first_page,
			    double               width_per_page,
			    double               height_first_page,
			    double               height_per_page)
{
	BonoboPrintData *pd = g_new (BonoboPrintData, 1);

	pd->width  = width;
	pd->height = height;
	
	pd->width_first_page  = width_first_page;
	pd->width_per_page    = width_per_page;
	pd->height_first_page = height_first_page;
	pd->height_per_page   = height_per_page;

	pd->meta_data = NULL;

	return pd;
}

/**
 * bonobo_print_data_new:
 * @width: the width in pts of the component to render
 * @height: the height in pts of the component to render
 * 
 * This constructs a BonoboPrintData with default scissor data.
 * 
 * Return value: a new BonoboPrintData object.
 **/
BonoboPrintData *
bonobo_print_data_new (double               width,
		       double               height)
{
	return bonobo_print_data_new_full (width, height, width, 0.0, height, 0.0);
}

/**
 * bonobo_print_data_render:
 * @ctx: the context
 * @x: the tlc bbox x
 * @y: the ltc bbox y
 * @pd: the print data to render
 * @meta_x: the offset into the print data x
 * @meta_y: the offset into the print data y
 * 
 * This is used to render the print data in @pd
 * onto a GnomePrintContext in @ctx.
 **/
void
bonobo_print_data_render (GnomePrintContext   *ctx,
			  double               x,
			  double               y,
			  BonoboPrintData     *pd,
			  double               meta_x,
			  double               meta_y)
{
	double matrix[6];
	double w, h;

	g_return_if_fail (pd != NULL);
	g_return_if_fail (GNOME_IS_PRINT_CONTEXT (ctx));

	if (!pd->meta_data)
		return;

	g_return_if_fail (GNOME_IS_PRINT_META (pd->meta_data));

	if (meta_x == 0.0)
		w = pd->width_first_page;
	else
		w = pd->width_per_page;

	if (meta_y == 0.0)
		h = pd->height_first_page;
	else
		h = pd->height_per_page;

	gnome_print_gsave (ctx);
	/* FIXME: we need a clip region & a different translation ! */
	gnome_print_moveto (ctx, x, y);
	gnome_print_lineto (ctx, x + w, y);
	gnome_print_lineto (ctx, x + w, y + h);
	gnome_print_lineto (ctx, x, y + h);
	gnome_print_lineto (ctx, x, y);
	gnome_print_clip   (ctx);

	art_affine_translate (matrix, x - meta_x, y - meta_y);
	gnome_print_concat (ctx, matrix);

	if (!gnome_print_meta_render_from_object (ctx, pd->meta_data))
		g_warning ("Failed to meta render");

	gnome_print_grestore (ctx);

#if 0
	{
		gnome_print_gsave (ctx);
		gnome_print_setlinewidth (ctx, 0);
		gnome_print_setrgbcolor (ctx, 1.0, 0.0, 0.0);

		gnome_print_moveto (ctx, x, y);
		gnome_print_lineto (ctx, x + w, y);
		gnome_print_lineto (ctx, x + w, y + h);
		gnome_print_lineto (ctx, x, y + h);
		gnome_print_lineto (ctx, x, y);
		gnome_print_stroke (ctx);

		gnome_print_grestore (ctx);
	}
#endif
}

/**
 * bonobo_print_data_free:
 * @pd: the print data
 * 
 * Releases all resources associated with the print data.
 **/
void
bonobo_print_data_free (BonoboPrintData *pd)
{
	if (pd) {
		if (pd->meta_data)
			gtk_object_unref (GTK_OBJECT (pd->meta_data));
		pd->meta_data = NULL;
		g_free (pd);
	}
}

/**
 * bonobo_print_client_new:
 * @corba_print: a remote Bonobo_Print interface handle
 * 
 * Return value: a newly constructed BonoboPrintClient
 **/
BonoboPrintClient *
bonobo_print_client_new (Bonobo_Print corba_print)
{
	BonoboPrintClient *pbc;
	static gboolean warned = FALSE;

	g_return_val_if_fail (corba_print != CORBA_OBJECT_NIL, NULL);

	if (!warned) {
/* Still true, but to help Jon we'll keep quiet */
/*		g_warning ("The print client interface is horribly immature, use at your own risk");*/
		warned = TRUE;
	}

	pbc = gtk_type_new (bonobo_print_client_get_type ());

	pbc->corba_print = corba_print;

	return pbc;
}

/**
 * bonobo_print_client_get:
 * @object: the print client
 * 
 * This does a QI on a remote BonoboObjectClient object, and
 * if it supports the interface returns a newly constructed
 * BonoboPrintClient handle, otherwise NULL.
 * 
 * Return value: a new handle or NULL.
 **/
BonoboPrintClient *
bonobo_print_client_get (BonoboObjectClient *object)
{
	CORBA_Environment  ev;
	Bonobo_Print       interf;
	BonoboPrintClient *client = NULL;

	g_return_val_if_fail (object != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_OBJECT_CLIENT (object), NULL);

	CORBA_exception_init (&ev);

	interf = bonobo_object_client_query_interface (object, "IDL:Bonobo/Print:1.0", &ev);

	if (BONOBO_EX (&ev))
		g_warning ("Exception getting print interface");
	else if (interf == CORBA_OBJECT_NIL)
		g_warning ("No printing interface");
	else
		client = bonobo_print_client_new (interf);

	CORBA_exception_free (&ev);

	return client;
}

static void
bonobo_print_client_gtk_init (BonoboPrintClient *pbc)
{
	pbc->corba_print = CORBA_OBJECT_NIL;
}

/**
 * bonobo_print_client_get_type:
 *
 * Returns: The GtkType corresponding to the BonoboPrintClient
 * class.
 */
GtkType
bonobo_print_client_get_type (void)
{
	static GtkType type = 0;

	if (!type) {
		GtkTypeInfo info = {
			"BonoboPrintClient",
			sizeof (BonoboPrintClient),
			sizeof (BonoboPrintClientClass),
			(GtkClassInitFunc) NULL,
			(GtkObjectInitFunc) bonobo_print_client_gtk_init,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

		type = gtk_type_unique (gtk_object_get_type (), &info);
	}

	return type;
}