KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > printing > PrintDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.swt.printing;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.widgets.*;
16 import org.eclipse.swt.internal.win32.*;
17
18 /**
19  * Instances of this class allow the user to select
20  * a printer and various print-related parameters
21  * prior to starting a print job.
22  * <p>
23  * IMPORTANT: This class is intended to be subclassed <em>only</em>
24  * within the SWT implementation.
25  * </p>
26  */

27
28 public class PrintDialog extends Dialog {
29     int scope = PrinterData.ALL_PAGES;
30     int startPage = 1, endPage = 1;
31     boolean printToFile = false;
32     
33 /**
34  * Constructs a new instance of this class given only its parent.
35  *
36  * @param parent a composite control which will be the parent of the new instance (cannot be null)
37  *
38  * @exception IllegalArgumentException <ul>
39  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
40  * </ul>
41  * @exception SWTException <ul>
42  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
43  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
44  * </ul>
45  *
46  * @see SWT
47  * @see Widget#checkSubclass
48  * @see Widget#getStyle
49  */

50 public PrintDialog (Shell parent) {
51     this (parent, SWT.PRIMARY_MODAL);
52 }
53
54 /**
55  * Constructs a new instance of this class given its parent
56  * and a style value describing its behavior and appearance.
57  * <p>
58  * The style value is either one of the style constants defined in
59  * class <code>SWT</code> which is applicable to instances of this
60  * class, or must be built by <em>bitwise OR</em>'ing together
61  * (that is, using the <code>int</code> "|" operator) two or more
62  * of those <code>SWT</code> style constants. The class description
63  * lists the style constants that are applicable to the class.
64  * Style bits are also inherited from superclasses.
65  * </p>
66  *
67  * @param parent a composite control which will be the parent of the new instance (cannot be null)
68  * @param style the style of control to construct
69  *
70  * @exception IllegalArgumentException <ul>
71  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
72  * </ul>
73  * @exception SWTException <ul>
74  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
75  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
76  * </ul>
77  *
78  * @see SWT
79  * @see Widget#checkSubclass
80  * @see Widget#getStyle
81  */

82 public PrintDialog (Shell parent, int style) {
83     super (parent, style);
84     checkSubclass ();
85 }
86
87 /**
88  * Returns the print job scope that the user selected
89  * before pressing OK in the dialog. This will be one
90  * of the following values:
91  * <dl>
92  * <dt><code>ALL_PAGES</code></dt>
93  * <dd>Print all pages in the current document</dd>
94  * <dt><code>PAGE_RANGE</code></dt>
95  * <dd>Print the range of pages specified by startPage and endPage</dd>
96  * <dt><code>SELECTION</code></dt>
97  * <dd>Print the current selection</dd>
98  * </dl>
99  *
100  * @return the scope setting that the user selected
101  */

102 public int getScope() {
103     return scope;
104 }
105
106 /**
107  * Sets the scope of the print job. The user will see this
108  * setting when the dialog is opened. This can have one of
109  * the following values:
110  * <dl>
111  * <dt><code>ALL_PAGES</code></dt>
112  * <dd>Print all pages in the current document</dd>
113  * <dt><code>PAGE_RANGE</code></dt>
114  * <dd>Print the range of pages specified by startPage and endPage</dd>
115  * <dt><code>SELECTION</code></dt>
116  * <dd>Print the current selection</dd>
117  * </dl>
118  *
119  * @param scope the scope setting when the dialog is opened
120  */

121 public void setScope(int scope) {
122     this.scope = scope;
123 }
124
125 /**
126  * Returns the start page setting that the user selected
127  * before pressing OK in the dialog.
128  * <p>
129  * This value can be from 1 to the maximum number of pages for the platform.
130  * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
131  * </p>
132  *
133  * @return the start page setting that the user selected
134  */

135 public int getStartPage() {
136     return startPage;
137 }
138
139 /**
140  * Sets the start page that the user will see when the dialog
141  * is opened.
142  * <p>
143  * This value can be from 1 to the maximum number of pages for the platform.
144  * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
145  * </p>
146  *
147  * @param startPage the startPage setting when the dialog is opened
148  */

149 public void setStartPage(int startPage) {
150     this.startPage = startPage;
151 }
152
153 /**
154  * Returns the end page setting that the user selected
155  * before pressing OK in the dialog.
156  * <p>
157  * This value can be from 1 to the maximum number of pages for the platform.
158  * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
159  * </p>
160  *
161  * @return the end page setting that the user selected
162  */

163 public int getEndPage() {
164     return endPage;
165 }
166
167 /**
168  * Sets the end page that the user will see when the dialog
169  * is opened.
170  * <p>
171  * This value can be from 1 to the maximum number of pages for the platform.
172  * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
173  * </p>
174  *
175  * @param endPage the end page setting when the dialog is opened
176  */

177 public void setEndPage(int endPage) {
178     this.endPage = endPage;
179 }
180
181 /**
182  * Returns the 'Print to file' setting that the user selected
183  * before pressing OK in the dialog.
184  *
185  * @return the 'Print to file' setting that the user selected
186  */

187 public boolean getPrintToFile() {
188     return printToFile;
189 }
190
191 /**
192  * Sets the 'Print to file' setting that the user will see
193  * when the dialog is opened.
194  *
195  * @param printToFile the 'Print to file' setting when the dialog is opened
196  */

197 public void setPrintToFile(boolean printToFile) {
198     this.printToFile = printToFile;
199 }
200
201 protected void checkSubclass() {
202     String JavaDoc name = getClass().getName();
203     String JavaDoc validName = PrintDialog.class.getName();
204     if (!validName.equals(name)) {
205         SWT.error(SWT.ERROR_INVALID_SUBCLASS);
206     }
207 }
208
209 /**
210  * Makes the receiver visible and brings it to the front
211  * of the display.
212  *
213  * @return a printer data object describing the desired print job parameters
214  *
215  * @exception SWTException <ul>
216  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
217  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
218  * </ul>
219  */

220 public PrinterData open() {
221     PRINTDLG pd = new PRINTDLG();
222     pd.lStructSize = PRINTDLG.sizeof;
223     Control parent = getParent();
224     if (parent != null) pd.hwndOwner = parent.handle;
225     pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE;
226     if (printToFile) pd.Flags |= OS.PD_PRINTTOFILE;
227     switch (scope) {
228         case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break;
229         case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break;
230         default: pd.Flags |= OS.PD_ALLPAGES;
231     }
232     pd.nMinPage = 1;
233     pd.nMaxPage = -1;
234     pd.nFromPage = (short) Math.min (0xFFFF, Math.max (1, startPage));
235     pd.nToPage = (short) Math.min (0xFFFF, Math.max (1, endPage));
236     
237     Display display = parent.getDisplay();
238     Shell [] shells = display.getShells();
239     if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
240         for (int i=0; i<shells.length; i++) {
241             if (shells[i].isEnabled() && shells[i] != parent) {
242                 shells[i].setEnabled(false);
243             } else {
244                 shells[i] = null;
245             }
246         }
247     }
248     PrinterData data = null;
249     String JavaDoc key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; //$NON-NLS-1$
250
Object JavaDoc oldValue = display.getData(key);
251     display.setData(key, new Boolean JavaDoc(true));
252     boolean success = OS.PrintDlg(pd);
253     display.setData(key, oldValue);
254     if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
255         for (int i=0; i<shells.length; i++) {
256             if (shells[i] != null && !shells[i].isDisposed ()) {
257                 shells[i].setEnabled(true);
258             }
259         }
260     }
261     
262     if (success) {
263         /* Get driver and device from the DEVNAMES struct */
264         int hMem = pd.hDevNames;
265         /* Ensure size is a multiple of 2 bytes on UNICODE platforms */
266         int size = OS.GlobalSize(hMem) / TCHAR.sizeof * TCHAR.sizeof;
267         int ptr = OS.GlobalLock(hMem);
268         short[] offsets = new short[4];
269         OS.MoveMemory(offsets, ptr, 2 * offsets.length);
270         TCHAR buffer = new TCHAR(0, size);
271         OS.MoveMemory(buffer, ptr, size);
272         OS.GlobalUnlock(hMem);
273
274         int driverOffset = offsets[0];
275         int i = 0;
276         while (driverOffset + i < size) {
277             if (buffer.tcharAt(driverOffset + i) == 0) break;
278             i++;
279         }
280         String JavaDoc driver = buffer.toString(driverOffset, i);
281
282         int deviceOffset = offsets[1];
283         i = 0;
284         while (deviceOffset + i < size) {
285             if (buffer.tcharAt(deviceOffset + i) == 0) break;
286             i++;
287         }
288         String JavaDoc device = buffer.toString(deviceOffset, i);
289
290         int outputOffset = offsets[2];
291         i = 0;
292         while (outputOffset + i < size) {
293             if (buffer.tcharAt(outputOffset + i) == 0) break;
294             i++;
295         }
296         String JavaDoc output = buffer.toString(outputOffset, i);
297         
298         /* Create PrinterData object and set fields from PRINTDLG */
299         data = new PrinterData(driver, device);
300         if ((pd.Flags & OS.PD_PAGENUMS) != 0) {
301             data.scope = PrinterData.PAGE_RANGE;
302             data.startPage = pd.nFromPage & 0xFFFF;
303             data.endPage = pd.nToPage & 0xFFFF;
304         } else if ((pd.Flags & OS.PD_SELECTION) != 0) {
305             data.scope = PrinterData.SELECTION;
306         }
307         data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) != 0;
308         if (data.printToFile) data.fileName = output;
309         data.copyCount = pd.nCopies;
310         data.collate = (pd.Flags & OS.PD_COLLATE) != 0;
311
312         /* Bulk-save the printer-specific settings in the DEVMODE struct */
313         hMem = pd.hDevMode;
314         size = OS.GlobalSize(hMem);
315         ptr = OS.GlobalLock(hMem);
316         data.otherData = new byte[size];
317         OS.MoveMemory(data.otherData, ptr, size);
318         OS.GlobalUnlock(hMem);
319
320         endPage = data.endPage;
321         printToFile = data.printToFile;
322         scope = data.scope;
323         startPage = data.startPage;
324     }
325     return data;
326 }
327 }
328
Popular Tags