PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/swt4ruby/src/mingw32-x86_32/org/eclipse/swt/printing/PrintDialog.java

https://github.com/neelance/swt4ruby
Java | 413 lines | 190 code | 30 blank | 193 comment | 63 complexity | 5b8345a59576f1d5367fc634355fb105 MD5 | raw file
Possible License(s): EPL-1.0
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2009 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. import org.eclipse.swt.*;
  13. import org.eclipse.swt.widgets.*;
  14. import org.eclipse.swt.internal.win32.*;
  15. /**
  16. * Instances of this class allow the user to select
  17. * a printer and various print-related parameters
  18. * prior to starting a print job.
  19. * <p>
  20. * IMPORTANT: This class is intended to be subclassed <em>only</em>
  21. * within the SWT implementation.
  22. * </p>
  23. *
  24. * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a>
  25. * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
  26. * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  27. * @noextend This class is not intended to be subclassed by clients.
  28. */
  29. public class PrintDialog extends Dialog {
  30. PrinterData printerData = new PrinterData();
  31. /**
  32. * Constructs a new instance of this class given only its parent.
  33. *
  34. * @param parent a composite control which will be the parent of the new instance (cannot be null)
  35. *
  36. * @exception IllegalArgumentException <ul>
  37. * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
  38. * </ul>
  39. * @exception SWTException <ul>
  40. * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
  41. * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
  42. * </ul>
  43. *
  44. * @see SWT
  45. * @see Widget#checkSubclass
  46. * @see Widget#getStyle
  47. */
  48. public PrintDialog (Shell parent) {
  49. this (parent, SWT.PRIMARY_MODAL);
  50. }
  51. /**
  52. * Constructs a new instance of this class given its parent
  53. * and a style value describing its behavior and appearance.
  54. * <p>
  55. * The style value is either one of the style constants defined in
  56. * class <code>SWT</code> which is applicable to instances of this
  57. * class, or must be built by <em>bitwise OR</em>'ing together
  58. * (that is, using the <code>int</code> "|" operator) two or more
  59. * of those <code>SWT</code> style constants. The class description
  60. * lists the style constants that are applicable to the class.
  61. * Style bits are also inherited from superclasses.
  62. * </p>
  63. *
  64. * @param parent a composite control which will be the parent of the new instance (cannot be null)
  65. * @param style the style of control to construct
  66. *
  67. * @exception IllegalArgumentException <ul>
  68. * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
  69. * </ul>
  70. * @exception SWTException <ul>
  71. * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
  72. * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
  73. * </ul>
  74. *
  75. * @see SWT
  76. * @see Widget#checkSubclass
  77. * @see Widget#getStyle
  78. */
  79. public PrintDialog (Shell parent, int style) {
  80. super (parent, checkStyle(parent, style));
  81. checkSubclass ();
  82. }
  83. static int checkStyle (Shell parent, int style) {
  84. int mask = SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL;
  85. if ((style & SWT.SHEET) != 0) {
  86. style &= ~SWT.SHEET;
  87. if ((style & mask) == 0) {
  88. style |= parent == null ? SWT.APPLICATION_MODAL : SWT.PRIMARY_MODAL;
  89. }
  90. }
  91. return style;
  92. }
  93. /**
  94. * Sets the printer data that will be used when the dialog
  95. * is opened.
  96. * <p>
  97. * Setting the printer data to null is equivalent to
  98. * resetting all data fields to their default values.
  99. * </p>
  100. *
  101. * @param data the data that will be used when the dialog is opened or null to use default data
  102. *
  103. * @since 3.4
  104. */
  105. public void setPrinterData(PrinterData data) {
  106. if (data == null) data = new PrinterData();
  107. this.printerData = data;
  108. }
  109. /**
  110. * Returns the printer data that will be used when the dialog
  111. * is opened.
  112. *
  113. * @return the data that will be used when the dialog is opened
  114. *
  115. * @since 3.4
  116. */
  117. public PrinterData getPrinterData() {
  118. return printerData;
  119. }
  120. /**
  121. * Returns the print job scope that the user selected
  122. * before pressing OK in the dialog. This will be one
  123. * of the following values:
  124. * <dl>
  125. * <dt><code>PrinterData.ALL_PAGES</code></dt>
  126. * <dd>Print all pages in the current document</dd>
  127. * <dt><code>PrinterData.PAGE_RANGE</code></dt>
  128. * <dd>Print the range of pages specified by startPage and endPage</dd>
  129. * <dt><code>PrinterData.SELECTION</code></dt>
  130. * <dd>Print the current selection</dd>
  131. * </dl>
  132. *
  133. * @return the scope setting that the user selected
  134. */
  135. public int getScope() {
  136. return printerData.scope;
  137. }
  138. /**
  139. * Sets the scope of the print job. The user will see this
  140. * setting when the dialog is opened. This can have one of
  141. * the following values:
  142. * <dl>
  143. * <dt><code>PrinterData.ALL_PAGES</code></dt>
  144. * <dd>Print all pages in the current document</dd>
  145. * <dt><code>PrinterData.PAGE_RANGE</code></dt>
  146. * <dd>Print the range of pages specified by startPage and endPage</dd>
  147. * <dt><code>PrinterData.SELECTION</code></dt>
  148. * <dd>Print the current selection</dd>
  149. * </dl>
  150. *
  151. * @param scope the scope setting when the dialog is opened
  152. */
  153. public void setScope(int scope) {
  154. printerData.scope = scope;
  155. }
  156. /**
  157. * Returns the start page setting that the user selected
  158. * before pressing OK in the dialog.
  159. * <p>
  160. * This value can be from 1 to the maximum number of pages for the platform.
  161. * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
  162. * </p>
  163. *
  164. * @return the start page setting that the user selected
  165. */
  166. public int getStartPage() {
  167. return printerData.startPage;
  168. }
  169. /**
  170. * Sets the start page that the user will see when the dialog
  171. * is opened.
  172. * <p>
  173. * This value can be from 1 to the maximum number of pages for the platform.
  174. * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
  175. * </p>
  176. *
  177. * @param startPage the startPage setting when the dialog is opened
  178. */
  179. public void setStartPage(int startPage) {
  180. printerData.startPage = startPage;
  181. }
  182. /**
  183. * Returns the end page setting that the user selected
  184. * before pressing OK in the dialog.
  185. * <p>
  186. * This value can be from 1 to the maximum number of pages for the platform.
  187. * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
  188. * </p>
  189. *
  190. * @return the end page setting that the user selected
  191. */
  192. public int getEndPage() {
  193. return printerData.endPage;
  194. }
  195. /**
  196. * Sets the end page that the user will see when the dialog
  197. * is opened.
  198. * <p>
  199. * This value can be from 1 to the maximum number of pages for the platform.
  200. * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
  201. * </p>
  202. *
  203. * @param endPage the end page setting when the dialog is opened
  204. */
  205. public void setEndPage(int endPage) {
  206. printerData.endPage = endPage;
  207. }
  208. /**
  209. * Returns the 'Print to file' setting that the user selected
  210. * before pressing OK in the dialog.
  211. *
  212. * @return the 'Print to file' setting that the user selected
  213. */
  214. public boolean getPrintToFile() {
  215. return printerData.printToFile;
  216. }
  217. /**
  218. * Sets the 'Print to file' setting that the user will see
  219. * when the dialog is opened.
  220. *
  221. * @param printToFile the 'Print to file' setting when the dialog is opened
  222. */
  223. public void setPrintToFile(boolean printToFile) {
  224. printerData.printToFile = printToFile;
  225. }
  226. protected void checkSubclass() {
  227. String name = getClass().getName();
  228. String validName = PrintDialog.class.getName();
  229. if (!validName.equals(name)) {
  230. SWT.error(SWT.ERROR_INVALID_SUBCLASS);
  231. }
  232. }
  233. /**
  234. * Makes the receiver visible and brings it to the front
  235. * of the display.
  236. *
  237. * @return a printer data object describing the desired print job parameters,
  238. * or null if the dialog was canceled, no printers were found, or an error occurred
  239. *
  240. * @exception SWTException <ul>
  241. * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
  242. * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
  243. * </ul>
  244. */
  245. public PrinterData open() {
  246. PRINTDLG pd = new PRINTDLG();
  247. pd.lStructSize = PRINTDLG.sizeof;
  248. Control parent = getParent();
  249. if (parent != null) pd.hwndOwner = parent.handle;
  250. /* Initialize PRINTDLG fields, including DEVMODE. */
  251. pd.Flags = OS.PD_RETURNDEFAULT;
  252. OS.PrintDlg(pd);
  253. /*
  254. * If user setup info from a previous print dialog was specified,
  255. * then restore the previous DEVMODE struct.
  256. */
  257. int /*long*/ lpInitData = 0;
  258. int /*long*/ hHeap = OS.GetProcessHeap();
  259. byte devmodeData [] = printerData.otherData;
  260. if (devmodeData != null && devmodeData.length != 0) {
  261. lpInitData = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, devmodeData.length);
  262. OS.MoveMemory(lpInitData, devmodeData, devmodeData.length);
  263. if (pd.hDevMode != 0) OS.GlobalFree(pd.hDevMode);
  264. pd.hDevMode = lpInitData;
  265. }
  266. /* Initialize the DEVMODE struct's fields from the printerData. */
  267. int /*long*/ hMem = pd.hDevMode;
  268. int /*long*/ ptr = OS.GlobalLock(hMem);
  269. DEVMODE devmode = OS.IsUnicode ? (DEVMODE)new DEVMODEW () : new DEVMODEA ();
  270. OS.MoveMemory(devmode, ptr, OS.IsUnicode ? OS.DEVMODEW_sizeof() : OS.DEVMODEA_sizeof());
  271. devmode.dmFields |= OS.DM_ORIENTATION;
  272. devmode.dmOrientation = printerData.orientation == PrinterData.PORTRAIT ? OS.DMORIENT_PORTRAIT : OS.DMORIENT_LANDSCAPE;
  273. if (printerData.copyCount != 1) {
  274. devmode.dmFields |= OS.DM_COPIES;
  275. devmode.dmCopies = (short)printerData.copyCount;
  276. }
  277. if (printerData.collate != false) {
  278. devmode.dmFields |= OS.DM_COLLATE;
  279. devmode.dmCollate = OS.DMCOLLATE_TRUE;
  280. }
  281. OS.MoveMemory(ptr, devmode, OS.IsUnicode ? OS.DEVMODEW_sizeof() : OS.DEVMODEA_sizeof());
  282. OS.GlobalUnlock(hMem);
  283. pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE;
  284. if (printerData.printToFile) pd.Flags |= OS.PD_PRINTTOFILE;
  285. switch (printerData.scope) {
  286. case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break;
  287. case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break;
  288. default: pd.Flags |= OS.PD_ALLPAGES;
  289. }
  290. pd.nMinPage = 1;
  291. pd.nMaxPage = -1;
  292. pd.nFromPage = (short) Math.min (0xFFFF, Math.max (1, printerData.startPage));
  293. pd.nToPage = (short) Math.min (0xFFFF, Math.max (1, printerData.endPage));
  294. Display display = parent.getDisplay();
  295. Shell [] shells = display.getShells();
  296. if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
  297. for (int i=0; i<shells.length; i++) {
  298. if (shells[i].isEnabled() && shells[i] != parent) {
  299. shells[i].setEnabled(false);
  300. } else {
  301. shells[i] = null;
  302. }
  303. }
  304. }
  305. PrinterData data = null;
  306. String key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; //$NON-NLS-1$
  307. Object oldValue = display.getData(key);
  308. display.setData(key, new Boolean(true));
  309. boolean success = OS.PrintDlg(pd);
  310. display.setData(key, oldValue);
  311. if ((getStyle() & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
  312. for (int i=0; i<shells.length; i++) {
  313. if (shells[i] != null && !shells[i].isDisposed ()) {
  314. shells[i].setEnabled(true);
  315. }
  316. }
  317. }
  318. if (success) {
  319. /* Get driver and device from the DEVNAMES struct */
  320. hMem = pd.hDevNames;
  321. /* Ensure size is a multiple of 2 bytes on UNICODE platforms */
  322. int size = OS.GlobalSize(hMem) / TCHAR.sizeof * TCHAR.sizeof;
  323. ptr = OS.GlobalLock(hMem);
  324. short[] offsets = new short[4];
  325. OS.MoveMemory(offsets, ptr, 2 * offsets.length);
  326. TCHAR buffer = new TCHAR(0, size);
  327. OS.MoveMemory(buffer, ptr, size);
  328. OS.GlobalUnlock(hMem);
  329. if (pd.hDevNames != 0) OS.GlobalFree(pd.hDevNames);
  330. int driverOffset = offsets[0];
  331. int i = 0;
  332. while (driverOffset + i < size) {
  333. if (buffer.tcharAt(driverOffset + i) == 0) break;
  334. i++;
  335. }
  336. String driver = buffer.toString(driverOffset, i);
  337. int deviceOffset = offsets[1];
  338. i = 0;
  339. while (deviceOffset + i < size) {
  340. if (buffer.tcharAt(deviceOffset + i) == 0) break;
  341. i++;
  342. }
  343. String device = buffer.toString(deviceOffset, i);
  344. int outputOffset = offsets[2];
  345. i = 0;
  346. while (outputOffset + i < size) {
  347. if (buffer.tcharAt(outputOffset + i) == 0) break;
  348. i++;
  349. }
  350. String output = buffer.toString(outputOffset, i);
  351. /* Create PrinterData object and set fields from PRINTDLG */
  352. data = new PrinterData(driver, device);
  353. if ((pd.Flags & OS.PD_PAGENUMS) != 0) {
  354. data.scope = PrinterData.PAGE_RANGE;
  355. data.startPage = pd.nFromPage & 0xFFFF;
  356. data.endPage = pd.nToPage & 0xFFFF;
  357. } else if ((pd.Flags & OS.PD_SELECTION) != 0) {
  358. data.scope = PrinterData.SELECTION;
  359. }
  360. data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) != 0;
  361. if (data.printToFile) data.fileName = output;
  362. data.copyCount = pd.nCopies;
  363. data.collate = (pd.Flags & OS.PD_COLLATE) != 0;
  364. /* Bulk-save the printer-specific settings in the DEVMODE struct */
  365. hMem = pd.hDevMode;
  366. size = OS.GlobalSize(hMem);
  367. ptr = OS.GlobalLock(hMem);
  368. data.otherData = new byte[size];
  369. OS.MoveMemory(data.otherData, ptr, size);
  370. devmode = OS.IsUnicode ? (DEVMODE)new DEVMODEW () : new DEVMODEA ();
  371. OS.MoveMemory(devmode, ptr, OS.IsUnicode ? OS.DEVMODEW_sizeof() : OS.DEVMODEA_sizeof());
  372. if ((devmode.dmFields & OS.DM_ORIENTATION) != 0) {
  373. int dmOrientation = devmode.dmOrientation;
  374. data.orientation = dmOrientation == OS.DMORIENT_LANDSCAPE ? PrinterData.LANDSCAPE : PrinterData.PORTRAIT;
  375. }
  376. OS.GlobalUnlock(hMem);
  377. if (pd.hDevMode != 0) OS.GlobalFree(pd.hDevMode);
  378. if (lpInitData != 0) OS.HeapFree(hHeap, 0, lpInitData);
  379. printerData = data;
  380. }
  381. return data;
  382. }
  383. }