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