108
|
1 /*******************************************************************************
|
91
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
108
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
91
|
12 *******************************************************************************/
|
|
13 module dwt.printing.Printer;
|
|
14
|
|
15
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.DWTError;
|
|
19 import dwt.DWTException;
|
|
20 import dwt.graphics.Device;
|
|
21 import dwt.graphics.DeviceData;
|
|
22 import dwt.graphics.Font;
|
|
23 import dwt.graphics.GCData;
|
|
24 import dwt.graphics.Point;
|
|
25 import dwt.graphics.Rectangle;
|
|
26 import dwt.internal.cairo.Cairo;
|
|
27 import dwt.internal.gtk.OS;
|
|
28 import dwt.printing.PrinterData;
|
|
29
|
|
30 static import tango.stdc.stringz;
|
|
31 static import tango.io.Stdout;
|
|
32 import tango.util.Convert;
|
|
33
|
|
34
|
|
35 /**
|
|
36 * Instances of this class are used to print to a printer.
|
|
37 * Applications create a GC on a printer using <code>new GC(printer)</code>
|
|
38 * and then draw on the printer GC using the usual graphics calls.
|
|
39 * <p>
|
|
40 * A <code>Printer</code> object may be constructed by providing
|
|
41 * a <code>PrinterData</code> object which identifies the printer.
|
|
42 * A <code>PrintDialog</code> presents a print dialog to the user
|
|
43 * and returns an initialized instance of <code>PrinterData</code>.
|
|
44 * Alternatively, calling <code>new Printer()</code> will construct a
|
|
45 * printer object for the user's default printer.
|
|
46 * </p><p>
|
|
47 * Application code must explicitly invoke the <code>Printer.dispose()</code>
|
|
48 * method to release the operating system resources managed by each instance
|
|
49 * when those instances are no longer required.
|
|
50 * </p>
|
|
51 *
|
|
52 * @see PrinterData
|
|
53 * @see PrintDialog
|
|
54 */
|
|
55 public final class Printer : Device {
|
|
56 static PrinterData [] printerList;
|
|
57
|
|
58 PrinterData data;
|
|
59 GtkPrinter* printer;
|
|
60 GtkPrintJob* printJob;
|
|
61 GtkPrintSettings* settings;
|
|
62 void* pageSetup;
|
|
63 cairo_surface_t* surface;
|
|
64 cairo_t* cairo;
|
|
65
|
|
66 /**
|
|
67 * whether or not a GC was created for this printer
|
|
68 */
|
|
69 bool isGCCreated = false;
|
|
70 Font systemFont;
|
|
71
|
|
72 char[] settingsData;
|
|
73 int start, end;
|
|
74
|
|
75 static const char[] GTK_LPR_BACKEND = "GtkPrintBackendLpr"; //$NON-NLS-1$
|
|
76
|
|
77 /**
|
|
78 * Returns an array of <code>PrinterData</code> objects
|
|
79 * representing all available printers.
|
|
80 *
|
|
81 * @return the list of available printers
|
|
82 */
|
|
83 public static PrinterData[] getPrinterList() {
|
|
84 printerList = new PrinterData [0];
|
|
85 if (OS.GTK_VERSION < OS.buildVERSION (2, 10, 0)) {
|
|
86 return printerList;
|
|
87 }
|
|
88 OS.gtk_enumerate_printers(&GtkPrinterFunc_List, null, null, true);
|
|
89 return printerList;
|
|
90 }
|
|
91
|
|
92 private static extern(C) int GtkPrinterFunc_List (GtkPrinter* printer, void* user_data) {
|
|
93 int length_ = printerList.length;
|
|
94 PrinterData [] newList = new PrinterData [length_ + 1];
|
|
95 System.arraycopy (printerList, 0, newList, 0, length_);
|
|
96 printerList = newList;
|
|
97 printerList [length_] = printerDataFromGtkPrinter(printer);
|
|
98 /*
|
|
99 * Bug in GTK. While performing a gtk_enumerate_printers(), GTK finds all of the
|
|
100 * available printers from each backend and can hang. If a backend requires more
|
|
101 * time to gather printer info, GTK will start an event loop waiting for a done
|
|
102 * signal before continuing. For the Lpr backend, GTK does not send a done signal
|
|
103 * which means the event loop never ends. The fix is to check to see if the driver
|
|
104 * is of type Lpr, and stop the enumeration, which exits the event loop.
|
|
105 */
|
|
106 if (printerList[length_].driver ==/*eq*/ GTK_LPR_BACKEND) return 1;
|
|
107 return 0;
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Returns a <code>PrinterData</code> object representing
|
|
112 * the default printer or <code>null</code> if there is no
|
|
113 * printer available on the System.
|
|
114 *
|
|
115 * @return the default printer data or null
|
|
116 *
|
|
117 * @since 2.1
|
|
118 */
|
|
119 public static PrinterData getDefaultPrinterData() {
|
|
120 printerList = new PrinterData [1];
|
|
121 if (OS.GTK_VERSION < OS.buildVERSION (2, 10, 0)) {
|
|
122 return null;
|
|
123 }
|
|
124 OS.gtk_enumerate_printers(&GtkPrinterFunc_Default, null, null, true);
|
|
125 return printerList[0];
|
|
126 }
|
|
127
|
|
128 private static extern(C) int GtkPrinterFunc_Default (GtkPrinter* printer, void* user_data) {
|
|
129 if (OS.gtk_printer_is_default(printer)) {
|
|
130 printerList[0] = printerDataFromGtkPrinter(printer);
|
|
131 return 1;
|
|
132 }
|
|
133 return 0;
|
|
134 }
|
|
135
|
|
136 GtkPrinter* gtkPrinterFromPrinterData() {
|
|
137 printer = null;
|
|
138 OS.gtk_enumerate_printers(&GtkPrinterFunc_FindNamedPrinterFunc, cast(void*)this, null, true);
|
|
139 return printer;
|
|
140 }
|
|
141
|
|
142 private static extern(C) int GtkPrinterFunc_FindNamedPrinterFunc (GtkPrinter* printer, void* user_data) {
|
|
143 return (cast(Printer)user_data).GtkPrinterFunc_FindNamedPrinter( printer, null );
|
|
144 }
|
|
145 int GtkPrinterFunc_FindNamedPrinter (GtkPrinter* printer, void* user_data) {
|
|
146 PrinterData pd = printerDataFromGtkPrinter(printer);
|
|
147 if (pd.driver ==/*eq*/data.driver && pd.name ==/*eq*/ data.name ) {
|
|
148 this.printer = printer;
|
|
149 OS.g_object_ref(printer);
|
|
150 return 1;
|
|
151 }
|
|
152 return 0;
|
|
153 }
|
|
154
|
|
155 static PrinterData printerDataFromGtkPrinter(GtkPrinter* printer) {
|
|
156 auto backend = OS.gtk_printer_get_backend(printer);
|
|
157 auto address = OS.G_OBJECT_TYPE_NAME(backend);
|
|
158 char[] backendType =tango.stdc.stringz.fromUtf8z( address ).dup;
|
|
159
|
|
160 address = OS.gtk_printer_get_name (printer);
|
|
161 char[] name =tango.stdc.stringz.fromUtf8z( address ).dup;
|
|
162
|
|
163 return new PrinterData (backendType, name);
|
|
164 }
|
|
165
|
|
166 static void setScope(GtkPrintSettings* settings, int scope_, int startPage, int endPage) {
|
|
167 switch (scope_) {
|
|
168 case PrinterData.ALL_PAGES:
|
|
169 OS.gtk_print_settings_set_print_pages(settings, OS.GTK_PRINT_PAGES_ALL);
|
|
170 break;
|
|
171 case PrinterData.PAGE_RANGE:
|
|
172 OS.gtk_print_settings_set_print_pages(settings, OS.GTK_PRINT_PAGES_RANGES);
|
|
173 GtkPageRange pageRange;
|
|
174 pageRange.start = startPage - 1;
|
|
175 pageRange.end = endPage - 1;
|
|
176 OS.gtk_print_settings_set_page_ranges(settings, &pageRange, 1);
|
|
177 break;
|
|
178 case PrinterData.SELECTION:
|
|
179 //TODO: Not correctly implemented. May need new API. For now, set to ALL. (see gtk bug 344519)
|
|
180 OS.gtk_print_settings_set_print_pages(settings, OS.GTK_PRINT_PAGES_ALL);
|
|
181 break;
|
|
182 default:
|
|
183 }
|
|
184 }
|
|
185
|
|
186 static DeviceData checkNull (PrinterData data) {
|
|
187 if (data is null) data = new PrinterData();
|
|
188 if (data.driver is null || data.name is null) {
|
|
189 PrinterData defaultPrinter = getDefaultPrinterData();
|
|
190 if (defaultPrinter is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
191 data.driver = defaultPrinter.driver;
|
|
192 data.name = defaultPrinter.name;
|
|
193 }
|
|
194 return data;
|
|
195 }
|
|
196
|
|
197 /**
|
|
198 * Constructs a new printer representing the default printer.
|
|
199 * <p>
|
|
200 * You must dispose the printer when it is no longer required.
|
|
201 * </p>
|
|
202 *
|
|
203 * @exception DWTError <ul>
|
|
204 * <li>ERROR_NO_HANDLES - if there are no valid printers
|
|
205 * </ul>
|
|
206 *
|
|
207 * @see Device#dispose
|
|
208 */
|
|
209 public this() {
|
|
210 this(null);
|
|
211 }
|
|
212
|
|
213 /**
|
|
214 * Constructs a new printer given a <code>PrinterData</code>
|
|
215 * object representing the desired printer.
|
|
216 * <p>
|
|
217 * You must dispose the printer when it is no longer required.
|
|
218 * </p>
|
|
219 *
|
|
220 * @param data the printer data for the specified printer
|
|
221 *
|
|
222 * @exception IllegalArgumentException <ul>
|
|
223 * <li>ERROR_INVALID_ARGUMENT - if the specified printer data does not represent a valid printer
|
|
224 * </ul>
|
|
225 * @exception DWTError <ul>
|
|
226 * <li>ERROR_NO_HANDLES - if there are no valid printers
|
|
227 * </ul>
|
|
228 *
|
|
229 * @see Device#dispose
|
|
230 */
|
|
231 public this(PrinterData data) {
|
|
232 super(checkNull(data));
|
|
233 }
|
|
234
|
|
235 int restoreInt(char[] key) {
|
|
236 char [] value = restoreBytes(key, false);
|
|
237 return to!(int)( value );
|
|
238 }
|
|
239
|
|
240 double restoreDouble(char[] key) {
|
|
241 char [] value = restoreBytes(key, false);
|
|
242 return to!(double)( value );
|
|
243 }
|
|
244
|
|
245 bool restoreBoolean(char[] key) {
|
|
246 char [] value = restoreBytes(key, false);
|
|
247 return to!(bool)( value );
|
|
248 }
|
|
249
|
|
250 char[] restoreBytes(char[] key, bool nullTerminate) {
|
|
251 //get key
|
|
252 start = end;
|
|
253 while (end < settingsData.length && settingsData[end] !is 0) end++;
|
|
254 end++;
|
|
255 char [] keyBuffer = new char [end - start];
|
|
256 System.arraycopy (settingsData, start, keyBuffer, 0, keyBuffer.length);
|
|
257
|
|
258 //get value
|
|
259 start = end;
|
|
260 while (end < settingsData.length && settingsData[end] !is 0) end++;
|
|
261 int length_ = end - start;
|
|
262 end++;
|
|
263 if (nullTerminate) length_++;
|
|
264 char [] valueBuffer = new char [length_];
|
|
265 System.arraycopy (settingsData, start, valueBuffer, 0, length_);
|
|
266
|
|
267 if (DEBUG) tango.io.Stdout.Stdout.formatln( "{}: {}", keyBuffer, valueBuffer );
|
|
268
|
|
269 return valueBuffer;
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Returns a reasonable font for applications to use.
|
|
274 * On some platforms, this will match the "default font"
|
|
275 * or "system font" if such can be found. This font
|
|
276 * should not be free'd because it was allocated by the
|
|
277 * system, not the application.
|
|
278 * <p>
|
|
279 * Typically, applications which want the default look
|
|
280 * should simply not set the font on the widgets they
|
|
281 * create. Widgets are always created with the correct
|
|
282 * default font for the class of user-interface component
|
|
283 * they represent.
|
|
284 * </p>
|
|
285 *
|
|
286 * @return a font
|
|
287 *
|
|
288 * @exception DWTException <ul>
|
|
289 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
290 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
291 * </ul>
|
|
292 */
|
|
293 public Font getSystemFont () {
|
|
294 checkDevice ();
|
|
295 if (systemFont !is null) return systemFont;
|
|
296 auto style = OS.gtk_widget_get_default_style();
|
|
297 auto defaultFont = OS.pango_font_description_copy (OS.gtk_style_get_font_desc (style));
|
|
298 return systemFont = Font.gtk_new (this, defaultFont);
|
|
299 }
|
|
300
|
|
301 /**
|
|
302 * Invokes platform specific functionality to allocate a new GC handle.
|
|
303 * <p>
|
|
304 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
305 * API for <code>Printer</code>. It is marked public only so that it
|
|
306 * can be shared within the packages provided by DWT. It is not
|
|
307 * available on all platforms, and should never be called from
|
|
308 * application code.
|
|
309 * </p>
|
|
310 *
|
|
311 * @param data the platform specific GC data
|
|
312 * @return the platform specific GC handle
|
|
313 */
|
|
314 public GdkGC* internal_new_GC(GCData data) {
|
|
315 auto drawable = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), 1, 1, 1);
|
|
316 auto gdkGC = OS.gdk_gc_new (drawable);
|
|
317 if (gdkGC is null) DWT.error (DWT.ERROR_NO_HANDLES);
|
|
318 if (data !is null) {
|
|
319 if (isGCCreated) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
320 int mask = DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
|
|
321 if ((data.style & mask) is 0) {
|
|
322 data.style |= DWT.LEFT_TO_RIGHT;
|
|
323 }
|
|
324 data.device = this;
|
|
325 data.drawable = drawable;
|
|
326 data.background = getSystemColor (DWT.COLOR_WHITE).handle;
|
|
327 data.foreground = getSystemColor (DWT.COLOR_BLACK).handle;
|
|
328 data.font = getSystemFont ().handle;
|
|
329 if (cairo is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
330 data.cairo = cairo;
|
|
331 isGCCreated = true;
|
|
332 }
|
|
333 return gdkGC;
|
|
334 }
|
|
335
|
|
336 /**
|
|
337 * Invokes platform specific functionality to dispose a GC handle.
|
|
338 * <p>
|
|
339 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
340 * API for <code>Printer</code>. It is marked public only so that it
|
|
341 * can be shared within the packages provided by DWT. It is not
|
|
342 * available on all platforms, and should never be called from
|
|
343 * application code.
|
|
344 * </p>
|
|
345 *
|
|
346 * @param hDC the platform specific GC handle
|
|
347 * @param data the platform specific GC data
|
|
348 */
|
|
349 public void internal_dispose_GC(GdkGC* gdkGC, GCData data) {
|
|
350 if (data !is null) isGCCreated = false;
|
|
351 OS.g_object_unref (gdkGC);
|
|
352 if (data !is null) {
|
|
353 if (data.drawable !is null) OS.g_object_unref (data.drawable);
|
|
354 data.drawable = null;
|
|
355 data.cairo = null;
|
|
356 }
|
|
357 }
|
|
358
|
|
359 /**
|
|
360 * Releases any internal state prior to destroying this printer.
|
|
361 * This method is called internally by the dispose
|
|
362 * mechanism of the <code>Device</code> class.
|
|
363 */
|
|
364 protected void release () {
|
|
365 super.release();
|
|
366
|
|
367 /* Dispose the default font */
|
|
368 if (systemFont !is null) systemFont.dispose ();
|
|
369 systemFont = null;
|
|
370 }
|
|
371
|
|
372 /**
|
|
373 * Starts a print job and returns true if the job started successfully
|
|
374 * and false otherwise.
|
|
375 * <p>
|
|
376 * This must be the first method called to initiate a print job,
|
|
377 * followed by any number of startPage/endPage calls, followed by
|
|
378 * endJob. Calling startPage, endPage, or endJob before startJob
|
|
379 * will result in undefined behavior.
|
|
380 * </p>
|
|
381 *
|
|
382 * @param jobName the name of the print job to start
|
|
383 * @return true if the job started successfully and false otherwise.
|
|
384 *
|
|
385 * @exception DWTException <ul>
|
|
386 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
387 * </ul>
|
|
388 *
|
|
389 * @see #startPage
|
|
390 * @see #endPage
|
|
391 * @see #endJob
|
|
392 */
|
|
393 public bool startJob(char[] jobName) {
|
|
394 checkDevice();
|
|
395 char* buffer = tango.stdc.stringz.toStringz(jobName);
|
|
396 printJob = OS.gtk_print_job_new (buffer, printer, settings, pageSetup);
|
|
397 if (printJob is null) return false;
|
|
398 surface = OS.gtk_print_job_get_surface(printJob, null);
|
|
399 if (surface is null) {
|
|
400 OS.g_object_unref(printJob);
|
|
401 printJob = null;
|
|
402 return false;
|
|
403 }
|
|
404 cairo = Cairo.cairo_create(surface);
|
|
405 if (cairo is null) {
|
|
406 OS.g_object_unref(printJob);
|
|
407 printJob = null;
|
|
408 return false;
|
|
409 }
|
|
410 return true;
|
|
411 }
|
|
412
|
|
413 /**
|
|
414 * Destroys the printer handle.
|
|
415 * This method is called internally by the dispose
|
|
416 * mechanism of the <code>Device</code> class.
|
|
417 */
|
|
418 protected void destroy () {
|
|
419 if (printer !is null) OS.g_object_unref (printer);
|
|
420 if (settings !is null) OS.g_object_unref (settings);
|
|
421 if (pageSetup !is null) OS.g_object_unref (pageSetup);
|
|
422 if (cairo !is null) Cairo.cairo_destroy (cairo);
|
|
423 if (printJob !is null) OS.g_object_unref (printJob);
|
|
424 printer = null;
|
|
425 settings = null;
|
|
426 pageSetup = null;
|
|
427 cairo = null;
|
|
428 printJob = null;
|
|
429 }
|
|
430
|
|
431 /**
|
|
432 * Ends the current print job.
|
|
433 *
|
|
434 * @exception DWTException <ul>
|
|
435 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
436 * </ul>
|
|
437 *
|
|
438 * @see #startJob
|
|
439 * @see #startPage
|
|
440 * @see #endPage
|
|
441 */
|
|
442 public void endJob() {
|
|
443 checkDevice();
|
|
444 if (printJob is null) return;
|
|
445 Cairo.cairo_surface_finish(surface);
|
|
446 OS.gtk_print_job_send(printJob, null, null, null );
|
|
447 }
|
|
448
|
|
449 /**
|
|
450 * Cancels a print job in progress.
|
|
451 *
|
|
452 * @exception DWTException <ul>
|
|
453 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
454 * </ul>
|
|
455 */
|
|
456 public void cancelJob() {
|
|
457 checkDevice();
|
|
458 if (printJob is null) return;
|
|
459 //TODO: Need to implement (waiting on gtk bug 339323)
|
|
460 //OS.g_object_unref(printJob);
|
|
461 //printJob = 0;
|
|
462 }
|
|
463
|
|
464 /**
|
|
465 * Starts a page and returns true if the page started successfully
|
|
466 * and false otherwise.
|
|
467 * <p>
|
|
468 * After calling startJob, this method may be called any number of times
|
|
469 * along with a matching endPage.
|
|
470 * </p>
|
|
471 *
|
|
472 * @return true if the page started successfully and false otherwise.
|
|
473 *
|
|
474 * @exception DWTException <ul>
|
|
475 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
476 * </ul>
|
|
477 *
|
|
478 * @see #endPage
|
|
479 * @see #startJob
|
|
480 * @see #endJob
|
|
481 */
|
|
482 public bool startPage() {
|
|
483 checkDevice();
|
|
484 if (printJob is null) return false;
|
|
485 double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS);
|
|
486 double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS);
|
|
487 int type = Cairo.cairo_surface_get_type (surface);
|
|
488 switch (type) {
|
|
489 case Cairo.CAIRO_SURFACE_TYPE_PS:
|
|
490 Cairo.cairo_ps_surface_set_size (surface, width, height);
|
|
491 break;
|
|
492 case Cairo.CAIRO_SURFACE_TYPE_PDF:
|
|
493 Cairo.cairo_pdf_surface_set_size (surface, width, height);
|
|
494 break;
|
|
495 }
|
|
496 return true;
|
|
497 }
|
|
498
|
|
499 /**
|
|
500 * Ends the current page.
|
|
501 *
|
|
502 * @exception DWTException <ul>
|
|
503 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
504 * </ul>
|
|
505 *
|
|
506 * @see #startPage
|
|
507 * @see #startJob
|
|
508 * @see #endJob
|
|
509 */
|
|
510 public void endPage() {
|
|
511 checkDevice();
|
|
512 if (cairo !is null) Cairo.cairo_show_page(cairo);
|
|
513 }
|
|
514
|
|
515 /**
|
|
516 * Returns a point whose x coordinate is the horizontal
|
|
517 * dots per inch of the printer, and whose y coordinate
|
|
518 * is the vertical dots per inch of the printer.
|
|
519 *
|
|
520 * @return the horizontal and vertical DPI
|
|
521 *
|
|
522 * @exception DWTException <ul>
|
|
523 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
524 * </ul>
|
|
525 */
|
|
526 public Point getDPI() {
|
|
527 checkDevice();
|
|
528 int resolution = OS.gtk_print_settings_get_resolution(settings);
|
|
529 if (DEBUG) tango.io.Stdout.Stdout.formatln("print_settings.resolution={}", resolution);
|
|
530 //TODO: Return 72 (1/72 inch = 1 point) until gtk bug 346245 is fixed
|
|
531 //TODO: Fix this: gtk_print_settings_get_resolution returns 0? (see gtk bug 346252)
|
|
532 if (true || resolution is 0) return new Point(72, 72);
|
|
533 return new Point(resolution, resolution);
|
|
534 }
|
|
535
|
|
536 /**
|
|
537 * Returns a rectangle describing the receiver's size and location.
|
|
538 * For a printer, this is the size of a physical page, in pixels.
|
|
539 *
|
|
540 * @return the bounding rectangle
|
|
541 *
|
|
542 * @exception DWTException <ul>
|
|
543 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
544 * </ul>
|
|
545 *
|
|
546 * @see #getClientArea
|
|
547 * @see #computeTrim
|
|
548 */
|
|
549 public Rectangle getBounds() {
|
|
550 checkDevice();
|
|
551 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245)
|
|
552 double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS);
|
|
553 double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS);
|
|
554 return new Rectangle(0, 0, cast(int) width, cast(int) height);
|
|
555 }
|
|
556
|
|
557 /**
|
|
558 * Returns a rectangle which describes the area of the
|
|
559 * receiver which is capable of displaying data.
|
|
560 * For a printer, this is the size of the printable area
|
|
561 * of a page, in pixels.
|
|
562 *
|
|
563 * @return the client area
|
|
564 *
|
|
565 * @exception DWTException <ul>
|
|
566 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
567 * </ul>
|
|
568 *
|
|
569 * @see #getBounds
|
|
570 * @see #computeTrim
|
|
571 */
|
|
572 public Rectangle getClientArea() {
|
|
573 checkDevice();
|
|
574 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245)
|
|
575 double width = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS);
|
|
576 double height = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS);
|
|
577 return new Rectangle(0, 0, cast(int) width, cast(int) height);
|
|
578 }
|
|
579
|
|
580 /**
|
|
581 * Given a desired <em>client area</em> for the receiver
|
|
582 * (as described by the arguments), returns the bounding
|
|
583 * rectangle which would be required to produce that client
|
|
584 * area.
|
|
585 * <p>
|
|
586 * In other words, it returns a rectangle such that, if the
|
|
587 * receiver's bounds were set to that rectangle, the area
|
|
588 * of the receiver which is capable of displaying data
|
|
589 * (that is, not covered by the "trimmings") would be the
|
|
590 * rectangle described by the arguments (relative to the
|
|
591 * receiver's parent).
|
|
592 * </p><p>
|
|
593 * Note that there is no setBounds for a printer. This method
|
|
594 * is usually used by passing in the client area (the 'printable
|
|
595 * area') of the printer. It can also be useful to pass in 0, 0, 0, 0.
|
|
596 * </p>
|
|
597 *
|
|
598 * @param x the desired x coordinate of the client area
|
|
599 * @param y the desired y coordinate of the client area
|
|
600 * @param width the desired width of the client area
|
|
601 * @param height the desired height of the client area
|
|
602 * @return the required bounds to produce the given client area
|
|
603 *
|
|
604 * @exception DWTException <ul>
|
|
605 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
606 * </ul>
|
|
607 *
|
|
608 * @see #getBounds
|
|
609 * @see #getClientArea
|
|
610 */
|
|
611 public Rectangle computeTrim(int x, int y, int width, int height) {
|
|
612 checkDevice();
|
|
613 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245)
|
|
614 double printWidth = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS);
|
|
615 double printHeight = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS);
|
|
616 double paperWidth = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS);
|
|
617 double paperHeight = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS);
|
|
618 double printX = -OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS);
|
|
619 double printY = -OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS);
|
|
620 double hTrim = paperWidth - printWidth;
|
|
621 double vTrim = paperHeight - printHeight;
|
|
622 return new Rectangle(x + cast(int)printX, y + cast(int)printY, width + cast(int)hTrim, height + cast(int)vTrim);
|
|
623 }
|
|
624
|
|
625 /**
|
|
626 * Creates the printer handle.
|
|
627 * This method is called internally by the instance creation
|
|
628 * mechanism of the <code>Device</code> class.
|
|
629 * @param deviceData the device data
|
|
630 */
|
|
631 protected void create(DeviceData deviceData) {
|
|
632 this.data = cast(PrinterData)deviceData;
|
|
633 if (OS.GTK_VERSION < OS.buildVERSION (2, 10, 0)) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
634 printer = gtkPrinterFromPrinterData();
|
|
635 if (printer is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
636 }
|
|
637
|
|
638 /**
|
|
639 * Initializes any internal resources needed by the
|
|
640 * device.
|
|
641 * <p>
|
|
642 * This method is called after <code>create</code>.
|
|
643 * </p><p>
|
|
644 * If subclasses reimplement this method, they must
|
|
645 * call the <code>super</code> implementation.
|
|
646 * </p>
|
|
647 *
|
|
648 * @see #create
|
|
649 */
|
|
650 protected void init() {
|
|
651 super.init ();
|
|
652 settings = OS.gtk_print_settings_new();
|
|
653 pageSetup = OS.gtk_page_setup_new();
|
|
654 if (data.otherData !is null) {
|
|
655 /* Retreive stored printer_settings data. */
|
|
656 settingsData = data.otherData;
|
|
657 start = end = 0;
|
|
658 while (end < settingsData.length && settingsData[end] !is 0) {
|
|
659 start = end;
|
|
660 while (end < settingsData.length && settingsData[end] !is 0) end++;
|
|
661 end++;
|
|
662 char [] keyBuffer = new char [end - start];
|
|
663 System.arraycopy (settingsData, start, keyBuffer, 0, keyBuffer.length);
|
|
664 start = end;
|
|
665 while (end < settingsData.length && settingsData[end] !is 0) end++;
|
|
666 end++;
|
|
667 char [] valueBuffer = new char [end - start];
|
|
668 System.arraycopy (settingsData, start, valueBuffer, 0, valueBuffer.length);
|
|
669 OS.gtk_print_settings_set(settings, keyBuffer.ptr, valueBuffer.ptr);
|
|
670 if (DEBUG) tango.io.Stdout.Stdout.formatln("{}: {}", keyBuffer, valueBuffer);
|
|
671 }
|
|
672 end++; // skip extra null terminator
|
|
673
|
|
674 /* Retreive stored page_setup data.
|
|
675 * Note that page_setup properties must be stored (in PrintDialog) and restored (here) in the same order.
|
|
676 */
|
|
677 OS.gtk_page_setup_set_orientation(pageSetup, restoreInt("orientation")); //$NON-NLS-1$
|
|
678 OS.gtk_page_setup_set_top_margin(pageSetup, restoreDouble("top_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$
|
|
679 OS.gtk_page_setup_set_bottom_margin(pageSetup, restoreDouble("bottom_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$
|
|
680 OS.gtk_page_setup_set_left_margin(pageSetup, restoreDouble("left_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$
|
|
681 OS.gtk_page_setup_set_right_margin(pageSetup, restoreDouble("right_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$
|
|
682 char [] name = restoreBytes("paper_size_name", true); //$NON-NLS-1$
|
|
683 char [] display_name = restoreBytes("paper_size_display_name", true); //$NON-NLS-1$
|
|
684 char [] ppd_name = restoreBytes("paper_size_ppd_name", true); //$NON-NLS-1$
|
|
685 double width = restoreDouble("paper_size_width"); //$NON-NLS-1$
|
|
686 double height = restoreDouble("paper_size_height"); //$NON-NLS-1$
|
|
687 bool custom = restoreBoolean("paper_size_is_custom"); //$NON-NLS-1$
|
|
688 GtkPaperSize* paper_size;
|
|
689 if (custom) {
|
|
690 if (ppd_name.length > 0) {
|
|
691 paper_size = OS.gtk_paper_size_new_from_ppd(ppd_name.ptr, display_name.ptr, width, height);
|
|
692 } else {
|
|
693 paper_size = OS.gtk_paper_size_new_custom(name.ptr, display_name.ptr, width, height, OS.GTK_UNIT_MM);
|
|
694 }
|
|
695 } else {
|
|
696 paper_size = OS.gtk_paper_size_new(name.ptr);
|
|
697 }
|
|
698 OS.gtk_page_setup_set_paper_size(pageSetup, paper_size);
|
|
699 OS.g_free(paper_size);
|
|
700 }
|
|
701
|
|
702 /* Set values of settings from PrinterData. */
|
|
703 setScope(settings, data.scope_, data.startPage, data.endPage);
|
|
704 //TODO: Should we look at printToFile, or driver/name for "Print to File", or both? (see gtk bug 345590)
|
|
705 if (data.printToFile) {
|
|
706 char* buffer = tango.stdc.stringz.toStringz( data.fileName );
|
|
707 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer);
|
|
708 }
|
|
709 if (data.driver ==/*eq*/ "GtkPrintBackendFile" && data.name ==/*eq*/ "Print to File" ) { //$NON-NLS-1$ //$NON-NLS-2$
|
|
710 char* buffer = tango.stdc.stringz.toStringz( data.fileName );
|
|
711 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer);
|
|
712 }
|
|
713 OS.gtk_print_settings_set_n_copies(settings, data.copyCount);
|
|
714 OS.gtk_print_settings_set_collate(settings, data.collate);
|
|
715 }
|
|
716
|
|
717 /**
|
|
718 * Returns a <code>PrinterData</code> object representing the
|
|
719 * target printer for this print job.
|
|
720 *
|
|
721 * @return a PrinterData object describing the receiver
|
|
722 */
|
|
723 public PrinterData getPrinterData() {
|
|
724 checkDevice();
|
|
725 return data;
|
|
726 }
|
|
727
|
|
728 }
|