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