1 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 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 50 public PrintDialog (Shell parent) { 51 this (parent, SWT.PRIMARY_MODAL); 52 } 53 54 82 public PrintDialog (Shell parent, int style) { 83 super (parent, style); 84 checkSubclass (); 85 } 86 87 102 public int getScope() { 103 return scope; 104 } 105 106 121 public void setScope(int scope) { 122 this.scope = scope; 123 } 124 125 135 public int getStartPage() { 136 return startPage; 137 } 138 139 149 public void setStartPage(int startPage) { 150 this.startPage = startPage; 151 } 152 153 163 public int getEndPage() { 164 return endPage; 165 } 166 167 177 public void setEndPage(int endPage) { 178 this.endPage = endPage; 179 } 180 181 187 public boolean getPrintToFile() { 188 return printToFile; 189 } 190 191 197 public void setPrintToFile(boolean printToFile) { 198 this.printToFile = printToFile; 199 } 200 201 protected void checkSubclass() { 202 String name = getClass().getName(); 203 String validName = PrintDialog.class.getName(); 204 if (!validName.equals(name)) { 205 SWT.error(SWT.ERROR_INVALID_SUBCLASS); 206 } 207 } 208 209 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 key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; Object oldValue = display.getData(key); 251 display.setData(key, new Boolean (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 264 int hMem = pd.hDevNames; 265 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 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 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 output = buffer.toString(outputOffset, i); 297 298 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 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 |