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