1 /*******************************************************************************
2 * Copyright (c) 2000, 2014 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.printing;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.win32.*;
19 import org.eclipse.swt.widgets.*;
22 * Instances of this class allow the user to select
23 * a printer and various print-related parameters
24 * prior to starting a print job.
26 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
29 * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a>
30 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
31 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
32 * @noextend This class is not intended to be subclassed by clients.
34 public class PrintDialog extends Dialog {
35 static final TCHAR DialogClass = new TCHAR (0, "#32770", true);
36 PrinterData printerData = new PrinterData();
39 * Constructs a new instance of this class given only its parent.
41 * @param parent a composite control which will be the parent of the new instance (cannot be null)
43 * @exception IllegalArgumentException <ul>
44 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
46 * @exception SWTException <ul>
47 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
48 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
52 * @see Widget#checkSubclass
53 * @see Widget#getStyle
55 public PrintDialog (Shell parent) {
56 this (parent, SWT.PRIMARY_MODAL);
60 * Constructs a new instance of this class given its parent
61 * and a style value describing its behavior and appearance.
63 * The style value is either one of the style constants defined in
64 * class <code>SWT</code> which is applicable to instances of this
65 * class, or must be built by <em>bitwise OR</em>'ing together
66 * (that is, using the <code>int</code> "|" operator) two or more
67 * of those <code>SWT</code> style constants. The class description
68 * lists the style constants that are applicable to the class.
69 * Style bits are also inherited from superclasses.
72 * @param parent a composite control which will be the parent of the new instance (cannot be null)
73 * @param style the style of control to construct
75 * @exception IllegalArgumentException <ul>
76 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
78 * @exception SWTException <ul>
79 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
80 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
84 * @see Widget#checkSubclass
85 * @see Widget#getStyle
87 public PrintDialog (Shell parent, int style) {
88 super (parent, checkStyle(parent, style));
92 static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) {
93 int mask = int0 | int1 | int2 | int3 | int4 | int5;
94 if ((style & mask) == 0) style |= int0;
95 if ((style & int0) != 0) style = (style & ~mask) | int0;
96 if ((style & int1) != 0) style = (style & ~mask) | int1;
97 if ((style & int2) != 0) style = (style & ~mask) | int2;
98 if ((style & int3) != 0) style = (style & ~mask) | int3;
99 if ((style & int4) != 0) style = (style & ~mask) | int4;
100 if ((style & int5) != 0) style = (style & ~mask) | int5;
104 static int checkStyle (Shell parent, int style) {
105 int mask = SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL;
106 if ((style & SWT.SHEET) != 0) {
108 if ((style & mask) == 0) {
109 style |= parent == null ? SWT.APPLICATION_MODAL : SWT.PRIMARY_MODAL;
112 if ((style & mask) == 0) {
113 style |= SWT.APPLICATION_MODAL;
115 style &= ~SWT.MIRRORED;
116 if ((style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT)) == 0) {
117 if (parent != null) {
118 if ((parent.getStyle () & SWT.LEFT_TO_RIGHT) != 0) style |= SWT.LEFT_TO_RIGHT;
119 if ((parent.getStyle () & SWT.RIGHT_TO_LEFT) != 0) style |= SWT.RIGHT_TO_LEFT;
122 return checkBits (style, SWT.LEFT_TO_RIGHT, SWT.RIGHT_TO_LEFT, 0, 0, 0, 0);
126 * Sets the printer data that will be used when the dialog
129 * Setting the printer data to null is equivalent to
130 * resetting all data fields to their default values.
133 * @param data the data that will be used when the dialog is opened or null to use default data
137 public void setPrinterData(PrinterData data) {
138 if (data == null) data = new PrinterData();
139 this.printerData = data;
143 * Returns the printer data that will be used when the dialog
146 * @return the data that will be used when the dialog is opened
150 public PrinterData getPrinterData() {
155 * Returns the print job scope that the user selected
156 * before pressing OK in the dialog. This will be one
157 * of the following values:
159 * <dt><code>PrinterData.ALL_PAGES</code></dt>
160 * <dd>Print all pages in the current document</dd>
161 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
162 * <dd>Print the range of pages specified by startPage and endPage</dd>
163 * <dt><code>PrinterData.SELECTION</code></dt>
164 * <dd>Print the current selection</dd>
167 * @return the scope setting that the user selected
169 public int getScope() {
170 return printerData.scope;
174 * Sets the scope of the print job. The user will see this
175 * setting when the dialog is opened. This can have one of
176 * the following values:
178 * <dt><code>PrinterData.ALL_PAGES</code></dt>
179 * <dd>Print all pages in the current document</dd>
180 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
181 * <dd>Print the range of pages specified by startPage and endPage</dd>
182 * <dt><code>PrinterData.SELECTION</code></dt>
183 * <dd>Print the current selection</dd>
186 * @param scope the scope setting when the dialog is opened
188 public void setScope(int scope) {
189 printerData.scope = scope;
193 * Returns the start page setting that the user selected
194 * before pressing OK in the dialog.
196 * This value can be from 1 to the maximum number of pages for the platform.
197 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
200 * @return the start page setting that the user selected
202 public int getStartPage() {
203 return printerData.startPage;
207 * Sets the start page that the user will see when the dialog
210 * This value can be from 1 to the maximum number of pages for the platform.
211 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
214 * @param startPage the startPage setting when the dialog is opened
216 public void setStartPage(int startPage) {
217 printerData.startPage = startPage;
221 * Returns the end page setting that the user selected
222 * before pressing OK in the dialog.
224 * This value can be from 1 to the maximum number of pages for the platform.
225 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
228 * @return the end page setting that the user selected
230 public int getEndPage() {
231 return printerData.endPage;
235 * Sets the end page that the user will see when the dialog
238 * This value can be from 1 to the maximum number of pages for the platform.
239 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
242 * @param endPage the end page setting when the dialog is opened
244 public void setEndPage(int endPage) {
245 printerData.endPage = endPage;
249 * Returns the 'Print to file' setting that the user selected
250 * before pressing OK in the dialog.
252 * @return the 'Print to file' setting that the user selected
254 public boolean getPrintToFile() {
255 return printerData.printToFile;
259 * Sets the 'Print to file' setting that the user will see
260 * when the dialog is opened.
262 * @param printToFile the 'Print to file' setting when the dialog is opened
264 public void setPrintToFile(boolean printToFile) {
265 printerData.printToFile = printToFile;
269 protected void checkSubclass() {
270 String name = getClass().getName();
271 String validName = PrintDialog.class.getName();
272 if (!validName.equals(name)) {
273 SWT.error(SWT.ERROR_INVALID_SUBCLASS);
278 * Makes the receiver visible and brings it to the front
281 * @return a printer data object describing the desired print job parameters,
282 * or null if the dialog was canceled, no printers were found, or an error occurred
284 * @exception SWTException <ul>
285 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
286 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
289 public PrinterData open() {
290 /* Get the owner HWND for the dialog */
291 Control parent = getParent();
292 int style = getStyle();
293 long hwndOwner = parent.handle;
294 long hwndParent = parent.handle;
297 * Feature in Windows. There is no API to set the BIDI orientation
298 * of a print dialog. It is always inherited from the parent. The fix
299 * is to create a hidden parent and set the orientation in the hidden
300 * parent for the dialog to inherit.
302 boolean enabled = false;
303 int dialogOrientation = style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
304 int parentOrientation = parent.getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
305 if (dialogOrientation != parentOrientation) {
306 int exStyle = OS.WS_EX_NOINHERITLAYOUT;
307 if (dialogOrientation == SWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL;
308 hwndOwner = OS.CreateWindowEx (
313 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
316 OS.GetModuleHandle (null),
318 enabled = OS.IsWindowEnabled (hwndParent);
319 if (enabled) OS.EnableWindow (hwndParent, false);
322 PrinterData data = null;
323 PRINTDLG pd = new PRINTDLG();
324 pd.lStructSize = PRINTDLG.sizeof;
325 pd.hwndOwner = hwndOwner;
327 boolean success = false;
328 if (printerData.name != null) {
329 /* Ensure that the printer name is in the current list of printers. */
330 PrinterData printerList[] = Printer.getPrinterList();
331 if (printerList.length > 0) {
332 for (int p = 0; p < printerList.length; p++) {
333 if (printerList[p].name.equals(printerData.name)) {
340 /* Initialize PRINTDLG DEVNAMES for the specified printer. */
341 TCHAR buffer = new TCHAR(0, printerData.name, true);
342 int size = buffer.length() * TCHAR.sizeof;
343 short[] offsets = new short[4]; // DEVNAMES (4 offsets)
344 int offsetsSize = offsets.length * 2; // 2 bytes each
345 offsets[1] = (short) offsets.length; // offset 1 points to wDeviceOffset
346 long hMem = OS.GlobalAlloc(OS.GMEM_MOVEABLE | OS.GMEM_ZEROINIT, offsetsSize + size);
347 long ptr = OS.GlobalLock(hMem);
348 OS.MoveMemory(ptr, offsets, offsetsSize);
349 OS.MoveMemory(ptr + offsetsSize, buffer, size);
350 OS.GlobalUnlock(hMem);
354 Display display = parent.getDisplay();
355 String externalLoopKey = "org.eclipse.swt.internal.win32.externalEventLoop";
357 /* Initialize PRINTDLG fields, including DEVMODE, for the default printer. */
358 pd.Flags = OS.PD_RETURNDEFAULT;
359 display.setData(externalLoopKey, Boolean.TRUE);
360 display.sendPreExternalEventDispatchEvent ();
361 success = OS.PrintDlg(pd);
362 display.setData(externalLoopKey, Boolean.FALSE);
363 display.sendPostExternalEventDispatchEvent ();
365 if (pd.hDevNames != 0) {
366 OS.GlobalFree(pd.hDevNames);
374 * If user setup info from a previous print dialog was specified,
375 * then restore the previous DEVMODE struct.
377 byte devmodeData [] = printerData.otherData;
378 if (devmodeData != null && devmodeData.length != 0) {
379 long hMem = OS.GlobalAlloc(OS.GMEM_MOVEABLE | OS.GMEM_ZEROINIT, devmodeData.length);
380 long ptr = OS.GlobalLock(hMem);
381 OS.MoveMemory(ptr, devmodeData, devmodeData.length);
382 OS.GlobalUnlock(hMem);
383 if (pd.hDevMode != 0) OS.GlobalFree(pd.hDevMode);
387 /* Initialize the DEVMODE struct's fields from the printerData. */
388 long hMem = pd.hDevMode;
390 hMem = OS.GlobalAlloc(OS.GMEM_MOVEABLE | OS.GMEM_ZEROINIT, DEVMODE.sizeof);
393 long ptr = OS.GlobalLock(hMem);
394 DEVMODE devmode = new DEVMODE ();
395 OS.MoveMemory(devmode, ptr, DEVMODE.sizeof);
396 if (printerData.name != null) {
397 /* Copy PRINTDLG DEVNAMES into DEVMODE dmDeviceName (truncate if necessary). */
398 int max = Math.min(printerData.name.length(), OS.CCHDEVICENAME - 1);
399 for (int i = 0; i < max; i++) {
400 devmode.dmDeviceName[i] = printerData.name.charAt(i);
403 devmode.dmFields |= OS.DM_ORIENTATION;
404 devmode.dmOrientation = printerData.orientation == PrinterData.PORTRAIT ? OS.DMORIENT_PORTRAIT : OS.DMORIENT_LANDSCAPE;
405 if (printerData.copyCount != 1) {
406 devmode.dmFields |= OS.DM_COPIES;
407 devmode.dmCopies = (short)printerData.copyCount;
409 if (printerData.collate != false) {
410 devmode.dmFields |= OS.DM_COLLATE;
411 devmode.dmCollate = OS.DMCOLLATE_TRUE;
413 if (printerData.duplex != SWT.DEFAULT) {
414 devmode.dmFields |= OS.DM_DUPLEX;
415 switch (printerData.duplex) {
416 case PrinterData.DUPLEX_SHORT_EDGE: devmode.dmDuplex = OS.DMDUP_HORIZONTAL; break;
417 case PrinterData.DUPLEX_LONG_EDGE: devmode.dmDuplex = OS.DMDUP_VERTICAL; break;
418 default: devmode.dmDuplex = OS.DMDUP_SIMPLEX;
421 OS.MoveMemory(ptr, devmode, DEVMODE.sizeof);
422 OS.GlobalUnlock(hMem);
424 pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE;
425 if (printerData.printToFile) pd.Flags |= OS.PD_PRINTTOFILE;
426 switch (printerData.scope) {
427 case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break;
428 case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break;
429 default: pd.Flags |= OS.PD_ALLPAGES;
433 pd.nFromPage = (short) Math.min (0xFFFF, Math.max (1, printerData.startPage));
434 pd.nToPage = (short) Math.min (0xFFFF, Math.max (1, printerData.endPage));
436 Shell [] shells = display.getShells();
437 if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
438 for (int i=0; i<shells.length; i++) {
439 if (shells[i].isEnabled() && shells[i] != parent) {
440 shells[i].setEnabled(false);
446 String key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; //$NON-NLS-1$
447 Object oldValue = display.getData(key);
448 display.setData(key, Boolean.TRUE);
449 display.setData(externalLoopKey, Boolean.TRUE);
450 display.sendPreExternalEventDispatchEvent ();
451 success = OS.PrintDlg(pd);
452 display.setData(externalLoopKey, Boolean.FALSE);
453 display.sendPostExternalEventDispatchEvent ();
454 display.setData(key, oldValue);
455 if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
456 for (int i=0; i<shells.length; i++) {
457 if (shells[i] != null && !shells[i].isDisposed ()) {
458 shells[i].setEnabled(true);
464 /* Get driver and device from the DEVNAMES struct */
466 /* Ensure size is a multiple of 2 bytes on UNICODE platforms */
467 int size = OS.GlobalSize(hMem) / TCHAR.sizeof * TCHAR.sizeof;
468 ptr = OS.GlobalLock(hMem);
469 short[] offsets = new short[4];
470 OS.MoveMemory(offsets, ptr, 2 * offsets.length);
471 char [] buffer = new char [size];
472 OS.MoveMemory(buffer, ptr, size);
473 OS.GlobalUnlock(hMem);
475 int driverOffset = offsets[0];
477 while (driverOffset + i < size) {
478 if (buffer [driverOffset + i] == 0) break;
481 String driver = new String(buffer, driverOffset, i);
483 int deviceOffset = offsets[1];
485 while (deviceOffset + i < size) {
486 if (buffer [deviceOffset + i] == 0) break;
489 String device = new String(buffer, deviceOffset, i);
491 /* Create PrinterData object and set fields from PRINTDLG */
492 data = new PrinterData(driver, device);
493 if ((pd.Flags & OS.PD_PAGENUMS) != 0) {
494 data.scope = PrinterData.PAGE_RANGE;
495 data.startPage = pd.nFromPage & 0xFFFF;
496 data.endPage = pd.nToPage & 0xFFFF;
497 } else if ((pd.Flags & OS.PD_SELECTION) != 0) {
498 data.scope = PrinterData.SELECTION;
500 data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) != 0;
501 if (data.printToFile) data.fileName = printerData.fileName;
502 data.copyCount = pd.nCopies;
503 data.collate = (pd.Flags & OS.PD_COLLATE) != 0;
505 /* Bulk-save the printer-specific settings in the DEVMODE struct */
507 size = OS.GlobalSize(hMem);
508 ptr = OS.GlobalLock(hMem);
509 data.otherData = new byte[size];
510 OS.MoveMemory(data.otherData, ptr, size);
512 /* Set PrinterData fields from DEVMODE */
513 devmode = new DEVMODE ();
514 OS.MoveMemory(devmode, ptr, DEVMODE.sizeof);
515 if ((devmode.dmFields & OS.DM_ORIENTATION) != 0) {
516 int dmOrientation = devmode.dmOrientation;
517 data.orientation = dmOrientation == OS.DMORIENT_LANDSCAPE ? PrinterData.LANDSCAPE : PrinterData.PORTRAIT;
519 if ((devmode.dmFields & OS.DM_DUPLEX) != 0) {
520 short dmDuplex = devmode.dmDuplex;
521 data.duplex = dmDuplex == OS.DMDUP_SIMPLEX ? PrinterData.DUPLEX_NONE : dmDuplex == OS.DMDUP_HORIZONTAL ? PrinterData.DUPLEX_SHORT_EDGE : PrinterData.DUPLEX_LONG_EDGE;
524 OS.GlobalUnlock(hMem);
528 if (pd.hDevNames != 0) {
529 OS.GlobalFree(pd.hDevNames);
532 if (pd.hDevMode != 0) {
533 OS.GlobalFree(pd.hDevMode);
536 /* Destroy the BIDI orientation window */
537 if (hwndParent != hwndOwner) {
538 if (enabled) OS.EnableWindow (hwndParent, true);
539 OS.SetActiveWindow (hwndParent);
540 OS.DestroyWindow (hwndOwner);