Mercurial > projects > dwt-linux
annotate dwt/printing/Printer.d @ 361:4bffbf81e2d6
redirect direct prints to DwtLogger
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 20 Mar 2009 21:00:06 +0100 |
parents | c0d810de7093 |
children |
rev | line source |
---|---|
115
52b32f5cb1e0
many file checked for switch default
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
91 | 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 import tango.util.Convert; | |
32 | |
33 | |
34 /** | |
35 * Instances of this class are used to print to a printer. | |
36 * Applications create a GC on a printer using <code>new GC(printer)</code> | |
37 * and then draw on the printer GC using the usual graphics calls. | |
38 * <p> | |
39 * A <code>Printer</code> object may be constructed by providing | |
40 * a <code>PrinterData</code> object which identifies the printer. | |
41 * A <code>PrintDialog</code> presents a print dialog to the user | |
42 * and returns an initialized instance of <code>PrinterData</code>. | |
43 * Alternatively, calling <code>new Printer()</code> will construct a | |
44 * printer object for the user's default printer. | |
45 * </p><p> | |
46 * Application code must explicitly invoke the <code>Printer.dispose()</code> | |
47 * method to release the operating system resources managed by each instance | |
48 * when those instances are no longer required. | |
49 * </p> | |
50 * | |
51 * @see PrinterData | |
52 * @see PrintDialog | |
259 | 53 * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a> |
54 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
91 | 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); | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
181 String backendType = fromStringz( address ).dup; |
91 | 182 |
183 address = OS.gtk_printer_get_name (printer); | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
184 String name = 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); | |
361
4bffbf81e2d6
redirect direct prints to DwtLogger
Frank Benoit <benoit@tionex.de>
parents:
259
diff
changeset
|
207 if (DEBUG) getDwtLogger().info("{}: {}", keyBuffer, valueBuffer ); |
240 | 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 | |
361
4bffbf81e2d6
redirect direct prints to DwtLogger
Frank Benoit <benoit@tionex.de>
parents:
259
diff
changeset
|
340 if (DEBUG) getDwtLogger().info( "{}: {}", keyBuffer, valueBuffer ); |
91 | 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(); |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
471 char* buffer = toStringz(jobName); |
91 | 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); | |
361
4bffbf81e2d6
redirect direct prints to DwtLogger
Frank Benoit <benoit@tionex.de>
parents:
259
diff
changeset
|
606 if (DEBUG) getDwtLogger().info("print_settings.resolution={}", resolution); |
91 | 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. | |
259 | 615 * <p> |
616 * For a printer, this is the size of the physical page, in pixels. | |
617 * </p> | |
91 | 618 * |
619 * @return the bounding rectangle | |
620 * | |
621 * @exception DWTException <ul> | |
622 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
623 * </ul> | |
624 * | |
625 * @see #getClientArea | |
626 * @see #computeTrim | |
627 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
628 public override Rectangle getBounds() { |
91 | 629 checkDevice(); |
630 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
631 double width = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); | |
632 double height = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); | |
633 return new Rectangle(0, 0, cast(int) width, cast(int) height); | |
634 } | |
635 | |
636 /** | |
637 * Returns a rectangle which describes the area of the | |
638 * receiver which is capable of displaying data. | |
259 | 639 * <p> |
91 | 640 * For a printer, this is the size of the printable area |
259 | 641 * of the page, in pixels. |
642 * </p> | |
91 | 643 * |
644 * @return the client area | |
645 * | |
646 * @exception DWTException <ul> | |
647 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
648 * </ul> | |
649 * | |
650 * @see #getBounds | |
651 * @see #computeTrim | |
652 */ | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
653 public override Rectangle getClientArea() { |
91 | 654 checkDevice(); |
655 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
656 double width = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); | |
657 double height = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); | |
658 return new Rectangle(0, 0, cast(int) width, cast(int) height); | |
659 } | |
660 | |
661 /** | |
259 | 662 * Given a <em>client area</em> (as described by the arguments), |
663 * returns a rectangle, relative to the client area's coordinates, | |
664 * that is the client area expanded by the printer's trim (or minimum margins). | |
91 | 665 * <p> |
259 | 666 * Most printers have a minimum margin on each edge of the paper where the |
667 * printer device is unable to print. This margin is known as the "trim." | |
668 * This method can be used to calculate the printer's minimum margins | |
669 * by passing in a client area of 0, 0, 0, 0 and then using the resulting | |
670 * x and y coordinates (which will be <= 0) to determine the minimum margins | |
671 * for the top and left edges of the paper, and the resulting width and height | |
672 * (offset by the resulting x and y) to determine the minimum margins for the | |
673 * bottom and right edges of the paper, as follows: | |
674 * <ul> | |
675 * <li>The left trim width is -x pixels</li> | |
676 * <li>The top trim height is -y pixels</li> | |
677 * <li>The right trim width is (x + width) pixels</li> | |
678 * <li>The bottom trim height is (y + height) pixels</li> | |
679 * </ul> | |
91 | 680 * </p> |
681 * | |
259 | 682 * @param x the x coordinate of the client area |
683 * @param y the y coordinate of the client area | |
684 * @param width the width of the client area | |
685 * @param height the height of the client area | |
686 * @return a rectangle, relative to the client area's coordinates, that is | |
687 * the client area expanded by the printer's trim (or minimum margins) | |
91 | 688 * |
689 * @exception DWTException <ul> | |
690 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> | |
691 * </ul> | |
692 * | |
693 * @see #getBounds | |
694 * @see #getClientArea | |
695 */ | |
696 public Rectangle computeTrim(int x, int y, int width, int height) { | |
697 checkDevice(); | |
698 //TODO: We are supposed to return this in pixels, but GTK_UNIT_PIXELS is currently not implemented (gtk bug 346245) | |
699 double printWidth = OS.gtk_page_setup_get_page_width(pageSetup, OS.GTK_UNIT_POINTS); | |
700 double printHeight = OS.gtk_page_setup_get_page_height(pageSetup, OS.GTK_UNIT_POINTS); | |
701 double paperWidth = OS.gtk_page_setup_get_paper_width (pageSetup, OS.GTK_UNIT_POINTS); | |
702 double paperHeight = OS.gtk_page_setup_get_paper_height (pageSetup, OS.GTK_UNIT_POINTS); | |
703 double printX = -OS.gtk_page_setup_get_left_margin(pageSetup, OS.GTK_UNIT_POINTS); | |
704 double printY = -OS.gtk_page_setup_get_top_margin(pageSetup, OS.GTK_UNIT_POINTS); | |
705 double hTrim = paperWidth - printWidth; | |
706 double vTrim = paperHeight - printHeight; | |
707 return new Rectangle(x + cast(int)printX, y + cast(int)printY, width + cast(int)hTrim, height + cast(int)vTrim); | |
708 } | |
709 | |
710 /** | |
711 * Creates the printer handle. | |
712 * This method is called internally by the instance creation | |
713 * mechanism of the <code>Device</code> class. | |
714 * @param deviceData the device data | |
715 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
716 protected override void create(DeviceData deviceData) { |
91 | 717 this.data = cast(PrinterData)deviceData; |
211 | 718 if (OS.GTK_VERSION < OS.buildVERSION (2, 10, 0) || disablePrinting) DWT.error(DWT.ERROR_NO_HANDLES); |
91 | 719 printer = gtkPrinterFromPrinterData(); |
720 if (printer is null) DWT.error(DWT.ERROR_NO_HANDLES); | |
721 } | |
722 | |
723 /** | |
724 * Initializes any internal resources needed by the | |
725 * device. | |
726 * <p> | |
727 * This method is called after <code>create</code>. | |
728 * </p><p> | |
729 * If subclasses reimplement this method, they must | |
730 * call the <code>super</code> implementation. | |
731 * </p> | |
732 * | |
733 * @see #create | |
734 */ | |
216
997624cece6a
renames init() to init_() to avoid problems in tango collections in jface port
Frank Benoit <benoit@tionex.de>
parents:
211
diff
changeset
|
735 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
|
736 super.init_ (); |
91 | 737 settings = OS.gtk_print_settings_new(); |
738 pageSetup = OS.gtk_page_setup_new(); | |
739 if (data.otherData !is null) { | |
240 | 740 restore(data.otherData, settings, pageSetup); |
91 | 741 } |
742 | |
743 /* Set values of settings from PrinterData. */ | |
744 setScope(settings, data.scope_, data.startPage, data.endPage); | |
745 //TODO: Should we look at printToFile, or driver/name for "Print to File", or both? (see gtk bug 345590) | |
746 if (data.printToFile) { | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
747 char* buffer = toStringz( data.fileName ); |
91 | 748 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer); |
749 } | |
195 | 750 if (data.driver.equals("GtkPrintBackendFile") && data.name.equals("Print to File")) { //$NON-NLS-1$ //$NON-NLS-2$ |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
751 char* buffer = toStringz( data.fileName ); |
91 | 752 OS.gtk_print_settings_set(settings, OS.GTK_PRINT_SETTINGS_OUTPUT_URI.ptr, buffer); |
753 } | |
754 OS.gtk_print_settings_set_n_copies(settings, data.copyCount); | |
755 OS.gtk_print_settings_set_collate(settings, data.collate); | |
756 } | |
757 | |
758 /** | |
759 * Returns a <code>PrinterData</code> object representing the | |
760 * target printer for this print job. | |
761 * | |
762 * @return a PrinterData object describing the receiver | |
763 */ | |
764 public PrinterData getPrinterData() { | |
765 checkDevice(); | |
766 return data; | |
767 } | |
768 | |
769 } |