Mercurial > projects > dwt-win
annotate dwt/printing/PrintDialog.d @ 212:ab60f3309436
reverted the char[] to String and use the an alias.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:12:38 +0200 |
parents | cc00a2772bf4 |
children | 36f5cb12e1a2 |
rev | line source |
---|---|
152 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwt.printing.PrintDialog; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.internal.win32.OS; | |
19 import dwt.widgets.Control; | |
20 import dwt.widgets.Dialog; | |
21 import dwt.widgets.Display; | |
22 import dwt.widgets.Shell; | |
23 import dwt.widgets.Widget; | |
24 | |
25 import dwt.printing.Printer; | |
26 import dwt.printing.PrinterData; | |
27 | |
28 import dwt.dwthelper.utils; | |
29 | |
30 /** | |
31 * Instances of this class allow the user to select | |
32 * a printer and various print-related parameters | |
33 * prior to starting a print job. | |
34 * <p> | |
35 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
36 * within the DWT implementation. | |
37 * </p> | |
38 */ | |
39 | |
40 public class PrintDialog : Dialog { | |
41 int scope_ = PrinterData.ALL_PAGES; | |
42 int startPage = 1, endPage = 1; | |
43 bool printToFile = false; | |
44 | |
45 /** | |
46 * Constructs a new instance of this class given only its parent. | |
47 * | |
48 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
49 * | |
50 * @exception IllegalArgumentException <ul> | |
51 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
52 * </ul> | |
53 * @exception DWTException <ul> | |
54 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
55 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
56 * </ul> | |
57 * | |
58 * @see DWT | |
59 * @see Widget#checkSubclass | |
60 * @see Widget#getStyle | |
61 */ | |
62 public this (Shell parent) { | |
63 this (parent, DWT.PRIMARY_MODAL); | |
64 } | |
65 | |
66 /** | |
67 * Constructs a new instance of this class given its parent | |
68 * and a style value describing its behavior and appearance. | |
69 * <p> | |
70 * The style value is either one of the style constants defined in | |
71 * class <code>DWT</code> which is applicable to instances of this | |
72 * class, or must be built by <em>bitwise OR</em>'ing together | |
73 * (that is, using the <code>int</code> "|" operator) two or more | |
74 * of those <code>DWT</code> style constants. The class description | |
75 * lists the style constants that are applicable to the class. | |
76 * Style bits are also inherited from superclasses. | |
77 * </p> | |
78 * | |
79 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
80 * @param style the style of control to construct | |
81 * | |
82 * @exception IllegalArgumentException <ul> | |
83 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
84 * </ul> | |
85 * @exception DWTException <ul> | |
86 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
87 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
88 * </ul> | |
89 * | |
90 * @see DWT | |
91 * @see Widget#checkSubclass | |
92 * @see Widget#getStyle | |
93 */ | |
94 public this (Shell parent, int style) { | |
95 super (parent, style); | |
96 checkSubclass (); | |
97 } | |
98 | |
99 /** | |
100 * Returns the print job scope that the user selected | |
101 * before pressing OK in the dialog. This will be one | |
102 * of the following values: | |
103 * <dl> | |
104 * <dt><code>ALL_PAGES</code></dt> | |
105 * <dd>Print all pages in the current document</dd> | |
106 * <dt><code>PAGE_RANGE</code></dt> | |
107 * <dd>Print the range of pages specified by startPage and endPage</dd> | |
108 * <dt><code>SELECTION</code></dt> | |
109 * <dd>Print the current selection</dd> | |
110 * </dl> | |
111 * | |
112 * @return the scope setting that the user selected | |
113 */ | |
114 public int getScope() { | |
115 return scope_; | |
116 } | |
117 | |
118 /** | |
119 * Sets the scope of the print job. The user will see this | |
120 * setting when the dialog is opened. This can have one of | |
121 * the following values: | |
122 * <dl> | |
123 * <dt><code>ALL_PAGES</code></dt> | |
124 * <dd>Print all pages in the current document</dd> | |
125 * <dt><code>PAGE_RANGE</code></dt> | |
126 * <dd>Print the range of pages specified by startPage and endPage</dd> | |
127 * <dt><code>SELECTION</code></dt> | |
128 * <dd>Print the current selection</dd> | |
129 * </dl> | |
130 * | |
131 * @param scope the scope setting when the dialog is opened | |
132 */ | |
133 public void setScope(int scope_) { | |
134 this.scope_ = scope_; | |
135 } | |
136 | |
137 /** | |
138 * Returns the start page setting that the user selected | |
139 * before pressing OK in the dialog. | |
140 * <p> | |
141 * This value can be from 1 to the maximum number of pages for the platform. | |
142 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. | |
143 * </p> | |
144 * | |
145 * @return the start page setting that the user selected | |
146 */ | |
147 public int getStartPage() { | |
148 return startPage; | |
149 } | |
150 | |
151 /** | |
152 * Sets the start page that the user will see when the dialog | |
153 * is opened. | |
154 * <p> | |
155 * This value can be from 1 to the maximum number of pages for the platform. | |
156 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. | |
157 * </p> | |
158 * | |
159 * @param startPage the startPage setting when the dialog is opened | |
160 */ | |
161 public void setStartPage(int startPage) { | |
162 this.startPage = startPage; | |
163 } | |
164 | |
165 /** | |
166 * Returns the end page setting that the user selected | |
167 * before pressing OK in the dialog. | |
168 * <p> | |
169 * This value can be from 1 to the maximum number of pages for the platform. | |
170 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. | |
171 * </p> | |
172 * | |
173 * @return the end page setting that the user selected | |
174 */ | |
175 public int getEndPage() { | |
176 return endPage; | |
177 } | |
178 | |
179 /** | |
180 * Sets the end page that the user will see when the dialog | |
181 * is opened. | |
182 * <p> | |
183 * This value can be from 1 to the maximum number of pages for the platform. | |
184 * Note that it is only valid if the scope is <code>PAGE_RANGE</code>. | |
185 * </p> | |
186 * | |
187 * @param endPage the end page setting when the dialog is opened | |
188 */ | |
189 public void setEndPage(int endPage) { | |
190 this.endPage = endPage; | |
191 } | |
192 | |
193 /** | |
194 * Returns the 'Print to file' setting that the user selected | |
195 * before pressing OK in the dialog. | |
196 * | |
197 * @return the 'Print to file' setting that the user selected | |
198 */ | |
199 public bool getPrintToFile() { | |
200 return printToFile; | |
201 } | |
202 | |
203 /** | |
204 * Sets the 'Print to file' setting that the user will see | |
205 * when the dialog is opened. | |
206 * | |
207 * @param printToFile the 'Print to file' setting when the dialog is opened | |
208 */ | |
209 public void setPrintToFile(bool printToFile) { | |
210 this.printToFile = printToFile; | |
211 } | |
212 | |
213 protected void checkSubclass() { | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
214 String name = this.classinfo.name; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
215 String validName = PrintDialog.classinfo.name; |
152 | 216 if (validName!=/*eq*/name) { |
217 DWT.error(DWT.ERROR_INVALID_SUBCLASS); | |
218 } | |
219 } | |
220 | |
221 /** | |
222 * Makes the receiver visible and brings it to the front | |
223 * of the display. | |
224 * | |
225 * @return a printer data object describing the desired print job parameters | |
226 * | |
227 * @exception DWTException <ul> | |
228 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
229 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
230 * </ul> | |
231 */ | |
232 public PrinterData open() { | |
233 PRINTDLG pd; | |
234 pd.lStructSize = PRINTDLG.sizeof; | |
235 Control parent = getParent(); | |
236 if (parent !is null) pd.hwndOwner = parent.handle; | |
237 pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE; | |
238 if (printToFile) pd.Flags |= OS.PD_PRINTTOFILE; | |
239 switch (scope_) { | |
240 case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break; | |
241 case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break; | |
242 default: pd.Flags |= OS.PD_ALLPAGES; | |
243 } | |
244 pd.nMinPage = 1; | |
245 pd.nMaxPage = cast(ushort)-1; | |
246 pd.nFromPage = cast(short) Math.min (0xFFFF, Math.max (1, startPage)); | |
247 pd.nToPage = cast(short) Math.min (0xFFFF, Math.max (1, endPage)); | |
248 | |
249 Display display = parent.getDisplay(); | |
250 Shell [] shells = display.getShells(); | |
251 if ((getStyle() & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { | |
252 for (int i=0; i<shells.length; i++) { | |
253 if (shells[i].isEnabled() && shells[i] !is parent) { | |
254 shells[i].setEnabled(false); | |
255 } else { | |
256 shells[i] = null; | |
257 } | |
258 } | |
259 } | |
260 PrinterData data = null; | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
261 String key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; //$NON-NLS-1$ |
152 | 262 Object oldValue = display.getData(key); |
263 display.setData(key, new ValueWrapperBool(true)); | |
264 bool success = cast(bool)OS.PrintDlg(&pd); | |
265 display.setData(key, oldValue); | |
266 if ((getStyle() & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { | |
267 for (int i=0; i<shells.length; i++) { | |
268 if (shells[i] !is null && !shells[i].isDisposed ()) { | |
269 shells[i].setEnabled(true); | |
270 } | |
271 } | |
272 } | |
273 | |
274 if (success) { | |
275 /* Get driver and device from the DEVNAMES struct */ | |
276 auto hMem = pd.hDevNames; | |
277 /* Ensure size is a multiple of 2 bytes on UNICODE platforms */ | |
278 int size = OS.GlobalSize(hMem) / TCHAR.sizeof * TCHAR.sizeof; | |
279 auto ptr = OS.GlobalLock(hMem); | |
280 short[4] offsets; | |
281 OS.MoveMemory(offsets.ptr, ptr, 2 * offsets.length); | |
282 TCHAR[] buffer = NewTCHARs(0, size); | |
283 OS.MoveMemory(buffer.ptr, ptr, size); | |
284 OS.GlobalUnlock(hMem); | |
285 | |
286 int driverOffset = offsets[0]; | |
287 int i = 0; | |
288 while (driverOffset + i < size) { | |
289 if (buffer[driverOffset + i] is 0) break; | |
290 i++; | |
291 } | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
292 String driver = TCHARsToStr( buffer[driverOffset .. driverOffset+i] ); |
152 | 293 |
294 int deviceOffset = offsets[1]; | |
295 i = 0; | |
296 while (deviceOffset + i < size) { | |
297 if (buffer[deviceOffset + i] is 0) break; | |
298 i++; | |
299 } | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
300 String device = TCHARsToStr( buffer[deviceOffset .. deviceOffset+i] ); |
152 | 301 |
302 int outputOffset = offsets[2]; | |
303 i = 0; | |
304 while (outputOffset + i < size) { | |
305 if (buffer[outputOffset + i] is 0) break; | |
306 i++; | |
307 } | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
308 String output = TCHARsToStr( buffer[outputOffset .. outputOffset+i] ); |
152 | 309 |
310 /* Create PrinterData object and set fields from PRINTDLG */ | |
311 data = new PrinterData(driver, device); | |
312 if ((pd.Flags & OS.PD_PAGENUMS) !is 0) { | |
313 data.scope_ = PrinterData.PAGE_RANGE; | |
314 data.startPage = pd.nFromPage & 0xFFFF; | |
315 data.endPage = pd.nToPage & 0xFFFF; | |
316 } else if ((pd.Flags & OS.PD_SELECTION) !is 0) { | |
317 data.scope_ = PrinterData.SELECTION; | |
318 } | |
319 data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) !is 0; | |
320 if (data.printToFile) data.fileName = output; | |
321 data.copyCount = pd.nCopies; | |
322 data.collate = (pd.Flags & OS.PD_COLLATE) !is 0; | |
323 | |
324 /* Bulk-save the printer-specific settings in the DEVMODE struct */ | |
325 hMem = pd.hDevMode; | |
326 size = OS.GlobalSize(hMem); | |
327 ptr = OS.GlobalLock(hMem); | |
328 data.otherData = new byte[size]; | |
329 OS.MoveMemory(data.otherData.ptr, ptr, size); | |
330 OS.GlobalUnlock(hMem); | |
331 | |
332 endPage = data.endPage; | |
333 printToFile = data.printToFile; | |
334 scope_ = data.scope_; | |
335 startPage = data.startPage; | |
336 } | |
337 return data; | |
338 } | |
339 } |