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