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