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