comparison dwt/printing/PrintDialog.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents e831403a80a9
children 10760eb00d08
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
12 12
13 import dwt.dwthelper.utils; 13 import dwt.dwthelper.utils;
14 14
15 import dwt.DWT; 15 import dwt.DWT;
16 import dwt.DWTException; 16 import dwt.DWTException;
17 import dwt.internal.cocoa.NSData;
18 import dwt.internal.cocoa.NSKeyedArchiver;
19 import dwt.internal.cocoa.NSMutableDictionary;
20 import dwt.internal.cocoa.NSNumber;
17 import dwt.internal.cocoa.NSPrintInfo; 21 import dwt.internal.cocoa.NSPrintInfo;
18 import dwt.internal.cocoa.NSPrintPanel; 22 import dwt.internal.cocoa.NSPrintPanel;
23 import dwt.internal.cocoa.NSPrinter;
24 import dwt.internal.cocoa.NSString;
25 import dwt.internal.cocoa.OS;
19 import dwt.widgets.Dialog; 26 import dwt.widgets.Dialog;
20 import dwt.widgets.Shell; 27 import dwt.widgets.Shell;
21 import dwt.widgets.Widget; 28 import dwt.widgets.Widget;
22 29
23 /** 30 /**
26 * prior to starting a print job. 33 * prior to starting a print job.
27 * <p> 34 * <p>
28 * IMPORTANT: This class is intended to be subclassed <em>only</em> 35 * IMPORTANT: This class is intended to be subclassed <em>only</em>
29 * within the DWT implementation. 36 * within the DWT implementation.
30 * </p> 37 * </p>
31 */ 38 *
32 public class PrintDialog : Dialog { 39 * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a>
40 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a>
41 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
42 */
43 public class PrintDialog extends Dialog {
33 PrinterData printerData; 44 PrinterData printerData;
34 int scope = PrinterData.ALL_PAGES; 45 int scope = PrinterData.ALL_PAGES;
35 int startPage = 1, endPage = 1; 46 int startPage = 1, endPage = 1;
36 bool printToFile = false; 47 bool printToFile = false;
37 48
50 * 61 *
51 * @see DWT 62 * @see DWT
52 * @see Widget#checkSubclass 63 * @see Widget#checkSubclass
53 * @see Widget#getStyle 64 * @see Widget#getStyle
54 */ 65 */
55 public this (Shell parent) { 66 public PrintDialog (Shell parent) {
56 this (parent, DWT.PRIMARY_MODAL); 67 this (parent, DWT.PRIMARY_MODAL);
57 } 68 }
58 69
59 /** 70 /**
60 * Constructs a new instance of this class given its parent 71 * Constructs a new instance of this class given its parent
82 * 93 *
83 * @see DWT 94 * @see DWT
84 * @see Widget#checkSubclass 95 * @see Widget#checkSubclass
85 * @see Widget#getStyle 96 * @see Widget#getStyle
86 */ 97 */
87 public this (Shell parent, int style) { 98 public PrintDialog (Shell parent, int style) {
88 super (parent, style); 99 super (parent, style);
89 checkSubclass (); 100 checkSubclass ();
90 } 101 }
91 102
92 /** 103 /**
123 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 134 * <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> 135 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
125 * </ul> 136 * </ul>
126 */ 137 */
127 public PrinterData open() { 138 public PrinterData open() {
139 PrinterData data = null;
128 NSPrintPanel panel = NSPrintPanel.printPanel(); 140 NSPrintPanel panel = NSPrintPanel.printPanel();
129 NSPrintInfo printInfo =cast(NSPrintInfo)new NSPrintInfo().alloc(); 141 NSPrintInfo printInfo = new NSPrintInfo(NSPrintInfo.sharedPrintInfo().copy());
130 printInfo.initWithDictionary(null); 142 NSMutableDictionary dict = printInfo.dictionary();
131 panel.runModalWithPrintInfo(printInfo); 143 if (printToFile) {
144 dict.setValue(OS.NSPrintSaveJob, OS.NSPrintJobDisposition);
145 }
146 //TODO - setting range not work. why?
147 dict.setValue(NSNumber.numberWithBool(scope is PrinterData.ALL_PAGES), OS.NSPrintAllPages);
148 if (scope is PrinterData.PAGE_RANGE) {
149 dict.setValue(NSNumber.numberWithInt(startPage), OS.NSPrintFirstPage);
150 dict.setValue(NSNumber.numberWithInt(endPage), OS.NSPrintLastPage);
151 }
152 panel.setOptions(OS.NSPrintPanelShowsPageSetupAccessory | panel.options());
153 if (panel.runModalWithPrintInfo(printInfo) !is OS.NSCancelButton) {
154 NSPrinter printer = printInfo.printer();
155 NSString str = printer.name();
156 data = new PrinterData(Printer.DRIVER, str.getString());
157 data.printToFile = printInfo.jobDisposition().isEqual(OS.NSPrintSaveJob);
158 if (data.printToFile) {
159 NSString filename = new NSString(dict.objectForKey(OS.NSPrintSavePath));
160 data.fileName = filename.getString();
161 }
162 data.scope = new NSNumber(dict.objectForKey(OS.NSPrintAllPages)).intValue() !is 0 ? PrinterData.ALL_PAGES : PrinterData.PAGE_RANGE;
163 if (data.scope is PrinterData.PAGE_RANGE) {
164 data.startPage = new NSNumber(dict.objectForKey(OS.NSPrintFirstPage)).intValue();
165 data.endPage = new NSNumber(dict.objectForKey(OS.NSPrintLastPage)).intValue();
166 }
167 data.collate = new NSNumber(dict.objectForKey(OS.NSPrintMustCollate)).intValue() !is 0;
168 data.copyCount = new NSNumber(dict.objectForKey(OS.NSPrintCopies)).intValue();
169 NSData nsData = NSKeyedArchiver.archivedDataWithRootObject(printInfo);
170 data.otherData = new byte[(int)/*64*/nsData.length()];
171 OS.memmove(data.otherData, nsData.bytes(), data.otherData.length);
172
173 printToFile = data.printToFile;
174 scope = data.scope;
175 startPage = data.startPage;
176 endPage = data.endPage;
177 }
132 printInfo.release(); 178 printInfo.release();
133 // int[] buffer = new int[1]; 179 return data;
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, cast(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 } 180 }
219 181
220 /** 182 /**
221 * Returns the print job scope that the user selected 183 * Returns the print job scope that the user selected
222 * before pressing OK in the dialog. This will be one 184 * before pressing OK in the dialog. This will be one
223 * of the following values: 185 * of the following values:
224 * <dl> 186 * <dl>
225 * <dt><code>ALL_PAGES</code></dt> 187 * <dt><code>PrinterData.ALL_PAGES</code></dt>
226 * <dd>Print all pages in the current document</dd> 188 * <dd>Print all pages in the current document</dd>
227 * <dt><code>PAGE_RANGE</code></dt> 189 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
228 * <dd>Print the range of pages specified by startPage and endPage</dd> 190 * <dd>Print the range of pages specified by startPage and endPage</dd>
229 * <dt><code>SELECTION</code></dt> 191 * <dt><code>PrinterData.SELECTION</code></dt>
230 * <dd>Print the current selection</dd> 192 * <dd>Print the current selection</dd>
231 * </dl> 193 * </dl>
232 * 194 *
233 * @return the scope setting that the user selected 195 * @return the scope setting that the user selected
234 */ 196 */
239 /** 201 /**
240 * Sets the scope of the print job. The user will see this 202 * Sets the scope of the print job. The user will see this
241 * setting when the dialog is opened. This can have one of 203 * setting when the dialog is opened. This can have one of
242 * the following values: 204 * the following values:
243 * <dl> 205 * <dl>
244 * <dt><code>ALL_PAGES</code></dt> 206 * <dt><code>PrinterData.ALL_PAGES</code></dt>
245 * <dd>Print all pages in the current document</dd> 207 * <dd>Print all pages in the current document</dd>
246 * <dt><code>PAGE_RANGE</code></dt> 208 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
247 * <dd>Print the range of pages specified by startPage and endPage</dd> 209 * <dd>Print the range of pages specified by startPage and endPage</dd>
248 * <dt><code>SELECTION</code></dt> 210 * <dt><code>PrinterData.SELECTION</code></dt>
249 * <dd>Print the current selection</dd> 211 * <dd>Print the current selection</dd>
250 * </dl> 212 * </dl>
251 * 213 *
252 * @param scope the scope setting when the dialog is opened 214 * @param scope the scope setting when the dialog is opened
253 */ 215 */
258 /** 220 /**
259 * Returns the start page setting that the user selected 221 * Returns the start page setting that the user selected
260 * before pressing OK in the dialog. 222 * before pressing OK in the dialog.
261 * <p> 223 * <p>
262 * This value can be from 1 to the maximum number of pages for the platform. 224 * 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>. 225 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
264 * </p> 226 * </p>
265 * 227 *
266 * @return the start page setting that the user selected 228 * @return the start page setting that the user selected
267 */ 229 */
268 public int getStartPage() { 230 public int getStartPage() {
272 /** 234 /**
273 * Sets the start page that the user will see when the dialog 235 * Sets the start page that the user will see when the dialog
274 * is opened. 236 * is opened.
275 * <p> 237 * <p>
276 * This value can be from 1 to the maximum number of pages for the platform. 238 * 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>. 239 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
278 * </p> 240 * </p>
279 * 241 *
280 * @param startPage the startPage setting when the dialog is opened 242 * @param startPage the startPage setting when the dialog is opened
281 */ 243 */
282 public void setStartPage(int startPage) { 244 public void setStartPage(int startPage) {
286 /** 248 /**
287 * Returns the end page setting that the user selected 249 * Returns the end page setting that the user selected
288 * before pressing OK in the dialog. 250 * before pressing OK in the dialog.
289 * <p> 251 * <p>
290 * This value can be from 1 to the maximum number of pages for the platform. 252 * 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>. 253 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
292 * </p> 254 * </p>
293 * 255 *
294 * @return the end page setting that the user selected 256 * @return the end page setting that the user selected
295 */ 257 */
296 public int getEndPage() { 258 public int getEndPage() {
300 /** 262 /**
301 * Sets the end page that the user will see when the dialog 263 * Sets the end page that the user will see when the dialog
302 * is opened. 264 * is opened.
303 * <p> 265 * <p>
304 * This value can be from 1 to the maximum number of pages for the platform. 266 * 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>. 267 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
306 * </p> 268 * </p>
307 * 269 *
308 * @param endPage the end page setting when the dialog is opened 270 * @param endPage the end page setting when the dialog is opened
309 */ 271 */
310 public void setEndPage(int endPage) { 272 public void setEndPage(int endPage) {