comparison dwt/printing/PrintDialog.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
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
10 *******************************************************************************/
11 module dwt.printing.PrintDialog;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.internal.cocoa.NSPrintInfo;
18 import dwt.internal.cocoa.NSPrintPanel;
19 import dwt.widgets.Dialog;
20 import dwt.widgets.Shell;
21 import dwt.widgets.Widget;
22
23 /**
24 * Instances of this class allow the user to select
25 * a printer and various print-related parameters
26 * prior to starting a print job.
27 * <p>
28 * IMPORTANT: This class is intended to be subclassed <em>only</em>
29 * within the DWT implementation.
30 * </p>
31 */
32 public class PrintDialog extends Dialog {
33 PrinterData printerData;
34 int scope = PrinterData.ALL_PAGES;
35 int startPage = 1, endPage = 1;
36 bool printToFile = false;
37
38 /**
39 * Constructs a new instance of this class given only its parent.
40 *
41 * @param parent a composite control which will be the parent of the new instance (cannot be null)
42 *
43 * @exception IllegalArgumentException <ul>
44 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
45 * </ul>
46 * @exception DWTException <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>
49 * </ul>
50 *
51 * @see DWT
52 * @see Widget#checkSubclass
53 * @see Widget#getStyle
54 */
55 public PrintDialog (Shell parent) {
56 this (parent, DWT.PRIMARY_MODAL);
57 }
58
59 /**
60 * Constructs a new instance of this class given its parent
61 * and a style value describing its behavior and appearance.
62 * <p>
63 * The style value is either one of the style constants defined in
64 * class <code>DWT</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>DWT</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.
70 * </p>
71 *
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
74 *
75 * @exception IllegalArgumentException <ul>
76 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
77 * </ul>
78 * @exception DWTException <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>
81 * </ul>
82 *
83 * @see DWT
84 * @see Widget#checkSubclass
85 * @see Widget#getStyle
86 */
87 public PrintDialog (Shell parent, int style) {
88 super (parent, style);
89 checkSubclass ();
90 }
91
92 /**
93 * Sets the printer data that will be used when the dialog
94 * is opened.
95 *
96 * @param data the data that will be used when the dialog is opened
97 *
98 * @since 3.4
99 */
100 public void setPrinterData(PrinterData data) {
101 this.printerData = data;
102 }
103
104 /**
105 * Returns the printer data that will be used when the dialog
106 * is opened.
107 *
108 * @return the data that will be used when the dialog is opened
109 *
110 * @since 3.4
111 */
112 public PrinterData getPrinterData() {
113 return printerData;
114 }
115
116 /**
117 * Makes the receiver visible and brings it to the front
118 * of the display.
119 *
120 * @return a printer data object describing the desired print job parameters
121 *
122 * @exception DWTException <ul>
123 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
124 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
125 * </ul>
126 */
127 public PrinterData open() {
128 NSPrintPanel panel = NSPrintPanel.printPanel();
129 NSPrintInfo printInfo =(NSPrintInfo)new NSPrintInfo().alloc();
130 printInfo.initWithDictionary(null);
131 panel.runModalWithPrintInfo(printInfo);
132 printInfo.release();
133 // int[] buffer = new int[1];
134 // if (OS.PMCreateSession(buffer) is OS.noErr) {
135 // int printSession = buffer[0];
136 // if (OS.PMCreatePrintSettings(buffer) is OS.noErr) {
137 // int printSettings = buffer[0];
138 // OS.PMSessionDefaultPrintSettings(printSession, printSettings);
139 // if (OS.PMCreatePageFormat(buffer) is OS.noErr) {
140 // int pageFormat = buffer[0];
141 // OS.PMSessionDefaultPageFormat(printSession, pageFormat);
142 // OS.PMSessionSetDestination(printSession, printSettings, (short) (printToFile ? OS.kPMDestinationFile : OS.kPMDestinationPrinter), 0, 0);
143 // if (scope is PrinterData.PAGE_RANGE) {
144 // OS.PMSetFirstPage(printSettings, startPage, false);
145 // OS.PMSetLastPage(printSettings, endPage, false);
146 // OS.PMSetPageRange(printSettings, startPage, endPage);
147 // } else {
148 // OS.PMSetPageRange(printSettings, 1, OS.kPMPrintAllPages);
149 // }
150 // bool[] accepted = new bool [1];
151 // OS.PMSessionPageSetupDialog(printSession, pageFormat, accepted);
152 // if (accepted[0]) {
153 // OS.PMSessionPrintDialog(printSession, printSettings, pageFormat, accepted);
154 // if (accepted[0]) {
155 // short[] destType = new short[1];
156 // OS.PMSessionGetDestinationType(printSession, printSettings, destType);
157 // String name = Printer.getCurrentPrinterName(printSession);
158 // String driver = Printer.DRIVER;
159 // switch (destType[0]) {
160 // case OS.kPMDestinationFax: driver = Printer.FAX_DRIVER; break;
161 // case OS.kPMDestinationFile: driver = Printer.FILE_DRIVER; break;
162 // case OS.kPMDestinationPreview: driver = Printer.PREVIEW_DRIVER; break;
163 // case OS.kPMDestinationPrinter: driver = Printer.PRINTER_DRIVER; break;
164 // }
165 // PrinterData data = new PrinterData(driver, name);
166 // if (destType[0] is OS.kPMDestinationFile) {
167 // data.printToFile = true;
168 // OS.PMSessionCopyDestinationLocation(printSession, printSettings, buffer);
169 // int fileName = OS.CFURLCopyFileSystemPath(buffer[0],OS.kCFURLPOSIXPathStyle);
170 // OS.CFRelease(buffer[0]);
171 // data.fileName = Printer.getString(fileName);
172 // OS.CFRelease(fileName);
173 // }
174 // OS.PMGetCopies(printSettings, buffer);
175 // data.copyCount = buffer[0];
176 // OS.PMGetFirstPage(printSettings, buffer);
177 // data.startPage = buffer[0];
178 // OS.PMGetLastPage(printSettings, buffer);
179 // data.endPage = buffer[0];
180 // OS.PMGetPageRange(printSettings, null, buffer);
181 // if (data.startPage is 1 && data.endPage is OS.kPMPrintAllPages) {
182 // data.scope = PrinterData.ALL_PAGES;
183 // } else {
184 // data.scope = PrinterData.PAGE_RANGE;
185 // }
186 // bool[] collate = new bool[1];
187 // OS.PMGetCollate(printSettings, collate);
188 // data.collate = collate[0];
189 //
190 // /* Serialize settings */
191 // int[] flatSettings = new int[1];
192 // OS.PMFlattenPrintSettings(printSettings, flatSettings);
193 // int[] flatFormat = new int[1];
194 // OS.PMFlattenPageFormat(pageFormat, flatFormat);
195 // int settingsLength = OS.GetHandleSize (flatSettings[0]);
196 // int formatLength = OS.GetHandleSize (flatFormat[0]);
197 // byte[] otherData = data.otherData = new byte[settingsLength + formatLength + 8];
198 // int offset = 0;
199 // offset = Printer.packData(flatSettings[0], otherData, offset);
200 // offset = Printer.packData(flatFormat[0], otherData, offset);
201 // OS.DisposeHandle(flatSettings[0]);
202 // OS.DisposeHandle(flatFormat[0]);
203 //
204 // scope = data.scope;
205 // startPage = data.startPage;
206 // endPage = data.endPage;
207 // printToFile = data.printToFile;
208 // return data;
209 // }
210 // }
211 // OS.PMRelease(pageFormat);
212 // }
213 // OS.PMRelease(printSettings);
214 // }
215 // OS.PMRelease(printSession);
216 // }
217 return null;
218 }
219
220 /**
221 * Returns the print job scope that the user selected
222 * before pressing OK in the dialog. This will be one
223 * of the following values:
224 * <dl>
225 * <dt><code>ALL_PAGES</code></dt>
226 * <dd>Print all pages in the current document</dd>
227 * <dt><code>PAGE_RANGE</code></dt>
228 * <dd>Print the range of pages specified by startPage and endPage</dd>
229 * <dt><code>SELECTION</code></dt>
230 * <dd>Print the current selection</dd>
231 * </dl>
232 *
233 * @return the scope setting that the user selected
234 */
235 public int getScope() {
236 return scope;
237 }
238
239 /**
240 * Sets the scope of the print job. The user will see this
241 * setting when the dialog is opened. This can have one of
242 * the following values:
243 * <dl>
244 * <dt><code>ALL_PAGES</code></dt>
245 * <dd>Print all pages in the current document</dd>
246 * <dt><code>PAGE_RANGE</code></dt>
247 * <dd>Print the range of pages specified by startPage and endPage</dd>
248 * <dt><code>SELECTION</code></dt>
249 * <dd>Print the current selection</dd>
250 * </dl>
251 *
252 * @param scope the scope setting when the dialog is opened
253 */
254 public void setScope(int scope) {
255 this.scope = scope;
256 }
257
258 /**
259 * Returns the start page setting that the user selected
260 * before pressing OK in the dialog.
261 * <p>
262 * This value can be from 1 to the maximum number of pages for the platform.
263 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
264 * </p>
265 *
266 * @return the start page setting that the user selected
267 */
268 public int getStartPage() {
269 return startPage;
270 }
271
272 /**
273 * Sets the start page that the user will see when the dialog
274 * is opened.
275 * <p>
276 * This value can be from 1 to the maximum number of pages for the platform.
277 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
278 * </p>
279 *
280 * @param startPage the startPage setting when the dialog is opened
281 */
282 public void setStartPage(int startPage) {
283 this.startPage = startPage;
284 }
285
286 /**
287 * Returns the end page setting that the user selected
288 * before pressing OK in the dialog.
289 * <p>
290 * This value can be from 1 to the maximum number of pages for the platform.
291 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
292 * </p>
293 *
294 * @return the end page setting that the user selected
295 */
296 public int getEndPage() {
297 return endPage;
298 }
299
300 /**
301 * Sets the end page that the user will see when the dialog
302 * is opened.
303 * <p>
304 * This value can be from 1 to the maximum number of pages for the platform.
305 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>.
306 * </p>
307 *
308 * @param endPage the end page setting when the dialog is opened
309 */
310 public void setEndPage(int endPage) {
311 this.endPage = endPage;
312 }
313
314 /**
315 * Returns the 'Print to file' setting that the user selected
316 * before pressing OK in the dialog.
317 *
318 * @return the 'Print to file' setting that the user selected
319 */
320 public bool getPrintToFile() {
321 return printToFile;
322 }
323
324 /**
325 * Sets the 'Print to file' setting that the user will see
326 * when the dialog is opened.
327 *
328 * @param printToFile the 'Print to file' setting when the dialog is opened
329 */
330 public void setPrintToFile(bool printToFile) {
331 this.printToFile = printToFile;
332 }
333
334 protected void checkSubclass() {
335 String name = getClass().getName();
336 String validName = PrintDialog.class.getName();
337 if (!validName.equals(name)) {
338 DWT.error(DWT.ERROR_INVALID_SUBCLASS);
339 }
340 }
341 }