Mercurial > projects > dwt-linux
annotate dwt/printing/Printer.d @ 150:f2e04420fd6c
reworked overrides and superclass aliases
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 31 Jan 2008 19:04:56 +0100 |
parents | 52b32f5cb1e0 |
children | 17f8449522fd |
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; | |
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 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
293 public override Font getSystemFont () { |
91 | 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 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
314 public override GdkGC* internal_new_GC(GCData data) { |
91 | 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 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
349 public override void internal_dispose_GC(GdkGC* gdkGC, GCData data) { |
91 | 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; | |
115
52b32f5cb1e0
many file checked for switch default
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
495 default: |
91 | 496 } |
497 return true; | |
498 } | |
499 | |
500 /** | |
501 * Ends the current page. | |
502 * | |
503 * @exception DWTException <ul> | |
504 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
505 * </ul> | |
506 * | |
507 * @see #startPage | |
508 * @see #startJob | |
509 * @see #endJob | |
510 */ | |
511 public void endPage() { | |
512 checkDevice(); | |
513 if (cairo !is null) Cairo.cairo_show_page(cairo); | |
514 } | |
515 | |
516 /** | |
517 * Returns a point whose x coordinate is the horizontal | |
518 * dots per inch of the printer, and whose y coordinate | |
519 * is the vertical dots per inch of the printer. | |
520 * | |
521 * @return the horizontal and vertical DPI | |
522 * | |
523 * @exception DWTException <ul> | |
524 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
525 * </ul> | |
526 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
527 public override Point getDPI() { |
91 | 528 checkDevice(); |
529 int resolution = OS.gtk_print_settings_get_resolution(settings); | |
530 if (DEBUG) tango.io.Stdout.Stdout.formatln("print_settings.resolution={}", resolution); | |
531 //TODO: Return 72 (1/72 inch = 1 point) until gtk bug 346245 is fixed | |
532 //TODO: Fix this: gtk_print_settings_get_resolution returns 0? (see gtk bug 346252) | |
533 if (true || resolution is 0) return new Point(72, 72); | |
534 return new Point(resolution, resolution); | |
535 } | |
536 | |
537 /** | |
538 * Returns a rectangle describing the receiver's size and location. | |
539 * For a printer, this is the size of a physical page, in pixels. | |
540 * | |
541 * @return the bounding rectangle | |
542 * | |
543 * @exception DWTException <ul> | |
544 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
545 * </ul> | |
546 * | |
547 * @see #getClientArea | |
548 * @see #computeTrim | |
549 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
550 public override Rectangle getBounds() { |
91 | 551 checkDevice(); |
552 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
553 double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); | |
554 double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); | |
555 return new Rectangle(0, 0, cast(int) width, cast(int) height); | |
556 } | |
557 | |
558 /** | |
559 * Returns a rectangle which describes the area of the | |
560 * receiver which is capable of displaying data. | |
561 * For a printer, this is the size of the printable area | |
562 * of a page, in pixels. | |
563 * | |
564 * @return the client area | |
565 * | |
566 * @exception DWTException <ul> | |
567 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
568 * </ul> | |
569 * | |
570 * @see #getBounds | |
571 * @see #computeTrim | |
572 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
573 public override Rectangle getClientArea() { |
91 | 574 checkDevice(); |
575 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
576 double width = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); | |
577 double height = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); | |
578 return new Rectangle(0, 0, cast(int) width, cast(int) height); | |
579 } | |
580 | |
581 /** | |
582 * Given a desired <em>client area</em> for the receiver | |
583 * (as described by the arguments), returns the bounding | |
584 * rectangle which would be required to produce that client | |
585 * area. | |
586 * <p> | |
587 * In other words, it returns a rectangle such that, if the | |
588 * receiver's bounds were set to that rectangle, the area | |
589 * of the receiver which is capable of displaying data | |
590 * (that is, not covered by the "trimmings") would be the | |
591 * rectangle described by the arguments (relative to the | |
592 * receiver's parent). | |
593 * </p><p> | |
594 * Note that there is no setBounds for a printer. This method | |
595 * is usually used by passing in the client area (the 'printable | |
596 * area') of the printer. It can also be useful to pass in 0, 0, 0, 0. | |
597 * </p> | |
598 * | |
599 * @param x the desired x coordinate of the client area | |
600 * @param y the desired y coordinate of the client area | |
601 * @param width the desired width of the client area | |
602 * @param height the desired height of the client area | |
603 * @return the required bounds to produce the given client area | |
604 * | |
605 * @exception DWTException <ul> | |
606 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
607 * </ul> | |
608 * | |
609 * @see #getBounds | |
610 * @see #getClientArea | |
611 */ | |
612 public Rectangle computeTrim(int x, int y, int width, int height) { | |
613 checkDevice(); | |
614 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
615 double printWidth = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); | |
616 double printHeight = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); | |
617 double paperWidth = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); | |
618 double paperHeight = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); | |
619 double printX = -OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS); | |
620 double printY = -OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS); | |
621 double hTrim = paperWidth - printWidth; | |
622 double vTrim = paperHeight - printHeight; | |
623 return new Rectangle(x + cast(int)printX, y + cast(int)printY, width + cast(int)hTrim, height + cast(int)vTrim); | |
624 } | |
625 | |
626 /** | |
627 * Creates the printer handle. | |
628 * This method is called internally by the instance creation | |
629 * mechanism of the <code>Device</code> class. | |
630 * @param deviceData the device data | |
631 */ | |
632 protected void create(DeviceData deviceData) { | |
633 this.data = cast(PrinterData)deviceData; | |
634 if (OS.GTK_VERSION < OS.buildVERSION (2, 10, 0)) DWT.error(DWT.ERROR_NO_HANDLES); | |
635 printer = gtkPrinterFromPrinterData(); | |
636 if (printer is null) DWT.error(DWT.ERROR_NO_HANDLES); | |
637 } | |
638 | |
639 /** | |
640 * Initializes any internal resources needed by the | |
641 * device. | |
642 * <p> | |
643 * This method is called after <code>create</code>. | |
644 * </p><p> | |
645 * If subclasses reimplement this method, they must | |
646 * call the <code>super</code> implementation. | |
647 * </p> | |
648 * | |
649 * @see #create | |
650 */ | |
651 protected void init() { | |
652 super.init (); | |
653 settings = OS.gtk_print_settings_new(); | |
654 pageSetup = OS.gtk_page_setup_new(); | |
655 if (data.otherData !is null) { | |
656 /* Retreive stored printer_settings data. */ | |
657 settingsData = data.otherData; | |
658 start = end = 0; | |
659 while (end < settingsData.length && settingsData[end] !is 0) { | |
660 start = end; | |
661 while (end < settingsData.length && settingsData[end] !is 0) end++; | |
662 end++; | |
663 char [] keyBuffer = new char [end - start]; | |
664 System.arraycopy (settingsData, start, keyBuffer, 0, keyBuffer.length); | |
665 start = end; | |
666 while (end < settingsData.length && settingsData[end] !is 0) end++; | |
667 end++; | |
668 char [] valueBuffer = new char [end - start]; | |
669 System.arraycopy (settingsData, start, valueBuffer, 0, valueBuffer.length); | |
670 OS.gtk_print_settings_set(settings, keyBuffer.ptr, valueBuffer.ptr); | |
671 if (DEBUG) tango.io.Stdout.Stdout.formatln("{}: {}", keyBuffer, valueBuffer); | |
672 } | |
673 end++; // skip extra null terminator | |
674 | |
675 /* Retreive stored page_setup data. | |
676 * Note that page_setup properties must be stored (in PrintDialog) and restored (here) in the same order. | |
677 */ | |
678 OS.gtk_page_setup_set_orientation(pageSetup, restoreInt("orientation")); //$NON-NLS-1$ | |
679 OS.gtk_page_setup_set_top_margin(pageSetup, restoreDouble("top_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$ | |
680 OS.gtk_page_setup_set_bottom_margin(pageSetup, restoreDouble("bottom_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$ | |
681 OS.gtk_page_setup_set_left_margin(pageSetup, restoreDouble("left_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$ | |
682 OS.gtk_page_setup_set_right_margin(pageSetup, restoreDouble("right_margin"), OS.GTK_UNIT_MM); //$NON-NLS-1$ | |
683 char [] name = restoreBytes("paper_size_name", true); //$NON-NLS-1$ | |
684 char [] display_name = restoreBytes("paper_size_display_name", true); //$NON-NLS-1$ | |
685 char [] ppd_name = restoreBytes("paper_size_ppd_name", true); //$NON-NLS-1$ | |
686 double width = restoreDouble("paper_size_width"); //$NON-NLS-1$ | |
687 double height = restoreDouble("paper_size_height"); //$NON-NLS-1$ | |
688 bool custom = restoreBoolean("paper_size_is_custom"); //$NON-NLS-1$ | |
689 GtkPaperSize* paper_size; | |
690 if (custom) { | |
691 if (ppd_name.length > 0) { | |
692 paper_size = OS.gtk_paper_size_new_from_ppd(ppd_name.ptr, display_name.ptr, width, height); | |
693 } else { | |
694 paper_size = OS.gtk_paper_size_new_custom(name.ptr, display_name.ptr, width, height, OS.GTK_UNIT_MM); | |
695 } | |
696 } else { | |
697 paper_size = OS.gtk_paper_size_new(name.ptr); | |
698 } | |
699 OS.gtk_page_setup_set_paper_size(pageSetup, paper_size); | |
700 OS.g_free(paper_size); | |
701 } | |
702 | |
703 /* Set values of settings from PrinterData. */ | |
704 setScope(settings, data.scope_, data.startPage, data.endPage); | |
705 //TODO: Should we look at printToFile, or driver/name for "Print to File", or both? (see gtk bug 345590) | |
706 if (data.printToFile) { | |
707 char* buffer = tango.stdc.stringz.toStringz( data.fileName ); | |
708 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer); | |
709 } | |
710 if (data.driver ==/*eq*/ "GtkPrintBackendFile" && data.name ==/*eq*/ "Print to File" ) { //$NON-NLS-1$ //$NON-NLS-2$ | |
711 char* buffer = tango.stdc.stringz.toStringz( data.fileName ); | |
712 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer); | |
713 } | |
714 OS.gtk_print_settings_set_n_copies(settings, data.copyCount); | |
715 OS.gtk_print_settings_set_collate(settings, data.collate); | |
716 } | |
717 | |
718 /** | |
719 * Returns a <code>PrinterData</code> object representing the | |
720 * target printer for this print job. | |
721 * | |
722 * @return a PrinterData object describing the receiver | |
723 */ | |
724 public PrinterData getPrinterData() { | |
725 checkDevice(); | |
726 return data; | |
727 } | |
728 | |
729 } |