5a4816dcacd431e125c4859ef43f79cec3e672bb
[reactos.git] / reactos / dll / win32 / comdlg32 / printdlg.c
1 /*
2 * COMMDLG - Print Dialog
3 *
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1999 Klaas van Gend
7 * Copyright 2000 Huw D M Davies
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winspool.h"
37 #include "winerror.h"
38
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 #include "commdlg.h"
43 #include "dlgs.h"
44 #include "cderr.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
47
48 #include "cdlg.h"
49 #include "printdlg.h"
50
51 /* Yes these constants are the same, but we're just copying win98 */
52 #define UPDOWN_ID 0x270f
53 #define MAX_COPIES 9999
54
55 DEVMODEW* WINAPI GdiConvertToDevmodeW(const DEVMODEA*);
56
57 /* Debugging info */
58 static const struct pd_flags psd_flags[] = {
59 {PSD_MINMARGINS,"PSD_MINMARGINS"},
60 {PSD_MARGINS,"PSD_MARGINS"},
61 {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
62 {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
63 {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
64 {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
65 {PSD_NOWARNING,"PSD_NOWARNING"},
66 {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
67 {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
68 {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
69 {PSD_SHOWHELP,"PSD_SHOWHELP"},
70 {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
71 {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
72 {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
73 {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
74 {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
75 {-1, NULL}
76 };
77
78 /* address of wndproc for subclassed Static control */
79 static WNDPROC lpfnStaticWndProc;
80 static WNDPROC edit_wndproc;
81 /* the text of the fake document to render for the Page Setup dialog */
82 static WCHAR wszFakeDocumentText[1024];
83 static const WCHAR pd32_collateW[] = { 'P', 'D', '3', '2', '_', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
84 static const WCHAR pd32_nocollateW[] = { 'P', 'D', '3', '2', '_', 'N', 'O', 'C', 'O', 'L', 'L', 'A', 'T', 'E', 0 };
85 static const WCHAR pd32_portraitW[] = { 'P', 'D', '3', '2', '_', 'P', 'O', 'R', 'T', 'R', 'A', 'I', 'T', 0 };
86 static const WCHAR pd32_landscapeW[] = { 'P', 'D', '3', '2', '_', 'L', 'A', 'N', 'D', 'S', 'C', 'A', 'P', 'E', 0 };
87 static const WCHAR printdlg_prop[] = {'_','_','W','I','N','E','_','P','R','I','N','T','D','L','G','D','A','T','A',0};
88 static const WCHAR pagesetupdlg_prop[] = { '_', '_', 'W', 'I', 'N', 'E', '_', 'P', 'A', 'G', 'E',
89 'S', 'E', 'T', 'U', 'P', 'D', 'L', 'G', 'D', 'A', 'T', 'A', 0 };
90
91
92 static LPWSTR strdupW(LPCWSTR p)
93 {
94 LPWSTR ret;
95 DWORD len;
96
97 if(!p) return NULL;
98 len = (strlenW(p) + 1) * sizeof(WCHAR);
99 ret = HeapAlloc(GetProcessHeap(), 0, len);
100 memcpy(ret, p, len);
101 return ret;
102 }
103
104 /***********************************************************
105 * convert_to_devmodeA
106 *
107 * Creates an ansi copy of supplied devmode
108 */
109 static DEVMODEA *convert_to_devmodeA(const DEVMODEW *dmW)
110 {
111 DEVMODEA *dmA;
112 DWORD size;
113
114 if (!dmW) return NULL;
115 size = dmW->dmSize - CCHDEVICENAME -
116 ((dmW->dmSize > FIELD_OFFSET(DEVMODEW, dmFormName)) ? CCHFORMNAME : 0);
117
118 dmA = HeapAlloc(GetProcessHeap(), 0, size + dmW->dmDriverExtra);
119 if (!dmA) return NULL;
120
121 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1,
122 (LPSTR)dmA->dmDeviceName, CCHDEVICENAME, NULL, NULL);
123
124 if (FIELD_OFFSET(DEVMODEW, dmFormName) >= dmW->dmSize)
125 {
126 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
127 dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
128 }
129 else
130 {
131 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
132 FIELD_OFFSET(DEVMODEW, dmFormName) - FIELD_OFFSET(DEVMODEW, dmSpecVersion));
133 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1,
134 (LPSTR)dmA->dmFormName, CCHFORMNAME, NULL, NULL);
135
136 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize - FIELD_OFFSET(DEVMODEW, dmLogPixels));
137 }
138
139 dmA->dmSize = size;
140 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize, dmW->dmDriverExtra);
141 return dmA;
142 }
143
144 /***********************************************************************
145 * PRINTDLG_OpenDefaultPrinter
146 *
147 * Returns a winspool printer handle to the default printer in *hprn
148 * Caller must call ClosePrinter on the handle
149 *
150 * Returns TRUE on success else FALSE
151 */
152 BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
153 {
154 WCHAR buf[260];
155 DWORD dwBufLen = sizeof(buf) / sizeof(buf[0]);
156 BOOL res;
157 if(!GetDefaultPrinterW(buf, &dwBufLen))
158 return FALSE;
159 res = OpenPrinterW(buf, hprn, NULL);
160 if (!res)
161 WARN("Could not open printer %s\n", debugstr_w(buf));
162 return res;
163 }
164
165 /***********************************************************************
166 * PRINTDLG_SetUpPrinterListCombo
167 *
168 * Initializes printer list combox.
169 * hDlg: HWND of dialog
170 * id: Control id of combo
171 * name: Name of printer to select
172 *
173 * Initializes combo with list of available printers. Selects printer 'name'
174 * If name is NULL or does not exist select the default printer.
175 *
176 * Returns number of printers added to list.
177 */
178 INT PRINTDLG_SetUpPrinterListComboA(HWND hDlg, UINT id, LPCSTR name)
179 {
180 DWORD needed, num;
181 INT i;
182 LPPRINTER_INFO_2A pi;
183 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
184 pi = HeapAlloc(GetProcessHeap(), 0, needed);
185 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
186 &num);
187
188 SendDlgItemMessageA(hDlg, id, CB_RESETCONTENT, 0, 0);
189
190 for(i = 0; i < num; i++) {
191 SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
192 (LPARAM)pi[i].pPrinterName );
193 }
194 HeapFree(GetProcessHeap(), 0, pi);
195 if(!name ||
196 (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
197 (LPARAM)name)) == CB_ERR) {
198
199 char buf[260];
200 DWORD dwBufLen = sizeof(buf);
201 if (name != NULL)
202 WARN("Can't find %s in printer list so trying to find default\n",
203 debugstr_a(name));
204 if(!GetDefaultPrinterA(buf, &dwBufLen))
205 return num;
206 i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
207 if(i == CB_ERR)
208 FIXME("Can't find default printer in printer list\n");
209 }
210 SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
211 return num;
212 }
213
214 static INT PRINTDLG_SetUpPrinterListComboW(HWND hDlg, UINT id, LPCWSTR name)
215 {
216 DWORD needed, num;
217 INT i;
218 LPPRINTER_INFO_2W pi;
219 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
220 pi = HeapAlloc(GetProcessHeap(), 0, needed);
221 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
222 &num);
223
224 for(i = 0; i < num; i++) {
225 SendDlgItemMessageW(hDlg, id, CB_ADDSTRING, 0,
226 (LPARAM)pi[i].pPrinterName );
227 }
228 HeapFree(GetProcessHeap(), 0, pi);
229 if(!name ||
230 (i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1,
231 (LPARAM)name)) == CB_ERR) {
232 WCHAR buf[260];
233 DWORD dwBufLen = sizeof(buf)/sizeof(buf[0]);
234 if (name != NULL)
235 WARN("Can't find %s in printer list so trying to find default\n",
236 debugstr_w(name));
237 if(!GetDefaultPrinterW(buf, &dwBufLen))
238 return num;
239 i = SendDlgItemMessageW(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
240 if(i == CB_ERR)
241 TRACE("Can't find default printer in printer list\n");
242 }
243 SendDlgItemMessageW(hDlg, id, CB_SETCURSEL, i, 0);
244 return num;
245 }
246
247 /***********************************************************************
248 * PRINTDLG_CreateDevNames [internal]
249 *
250 *
251 * creates a DevNames structure.
252 *
253 * (NB. when we handle unicode the offsets will be in wchars).
254 */
255 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, const char* DeviceDriverName,
256 const char* DeviceName, const char* OutputPort)
257 {
258 long size;
259 char* pDevNamesSpace;
260 char* pTempPtr;
261 LPDEVNAMES lpDevNames;
262 char buf[260];
263 DWORD dwBufLen = sizeof(buf);
264
265 size = strlen(DeviceDriverName) + 1
266 + strlen(DeviceName) + 1
267 + strlen(OutputPort) + 1
268 + sizeof(DEVNAMES);
269
270 if(*hmem)
271 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
272 else
273 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
274 if (*hmem == 0)
275 return FALSE;
276
277 pDevNamesSpace = GlobalLock(*hmem);
278 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
279
280 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
281 strcpy(pTempPtr, DeviceDriverName);
282 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
283
284 pTempPtr += strlen(DeviceDriverName) + 1;
285 strcpy(pTempPtr, DeviceName);
286 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
287
288 pTempPtr += strlen(DeviceName) + 1;
289 strcpy(pTempPtr, OutputPort);
290 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
291
292 GetDefaultPrinterA(buf, &dwBufLen);
293 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
294 GlobalUnlock(*hmem);
295 return TRUE;
296 }
297
298 static BOOL PRINTDLG_CreateDevNamesW(HGLOBAL *hmem, LPCWSTR DeviceDriverName,
299 LPCWSTR DeviceName, LPCWSTR OutputPort)
300 {
301 long size;
302 LPWSTR pDevNamesSpace;
303 LPWSTR pTempPtr;
304 LPDEVNAMES lpDevNames;
305 WCHAR bufW[260];
306 DWORD dwBufLen = sizeof(bufW) / sizeof(WCHAR);
307
308 size = sizeof(WCHAR)*lstrlenW(DeviceDriverName) + 2
309 + sizeof(WCHAR)*lstrlenW(DeviceName) + 2
310 + sizeof(WCHAR)*lstrlenW(OutputPort) + 2
311 + sizeof(DEVNAMES);
312
313 if(*hmem)
314 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
315 else
316 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
317 if (*hmem == 0)
318 return FALSE;
319
320 pDevNamesSpace = GlobalLock(*hmem);
321 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
322
323 pTempPtr = (LPWSTR)((LPDEVNAMES)pDevNamesSpace + 1);
324 lstrcpyW(pTempPtr, DeviceDriverName);
325 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
326
327 pTempPtr += lstrlenW(DeviceDriverName) + 1;
328 lstrcpyW(pTempPtr, DeviceName);
329 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
330
331 pTempPtr += lstrlenW(DeviceName) + 1;
332 lstrcpyW(pTempPtr, OutputPort);
333 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
334
335 GetDefaultPrinterW(bufW, &dwBufLen);
336 lpDevNames->wDefault = (lstrcmpW(bufW, DeviceName) == 0) ? 1 : 0;
337 GlobalUnlock(*hmem);
338 return TRUE;
339 }
340
341 /***********************************************************************
342 * PRINTDLG_UpdatePrintDlg [internal]
343 *
344 *
345 * updates the PrintDlg structure for return values.
346 *
347 * RETURNS
348 * FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
349 * TRUE if successful.
350 */
351 static BOOL PRINTDLG_UpdatePrintDlgA(HWND hDlg,
352 PRINT_PTRA* PrintStructures)
353 {
354 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
355 PDEVMODEA lpdm = PrintStructures->lpDevMode;
356 LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
357
358
359 if(!lpdm) {
360 FIXME("No lpdm ptr?\n");
361 return FALSE;
362 }
363
364
365 if(!(lppd->Flags & PD_PRINTSETUP)) {
366 /* check whether nFromPage and nToPage are within range defined by
367 * nMinPage and nMaxPage
368 */
369 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
370 WORD nToPage;
371 WORD nFromPage;
372 BOOL translated;
373 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
374 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
375
376 /* if no ToPage value is entered, use the FromPage value */
377 if(!translated) nToPage = nFromPage;
378
379 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
380 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
381 WCHAR resourcestr[256];
382 WCHAR resultstr[256];
383 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE, resourcestr, 255);
384 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
385 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE, resourcestr, 255);
386 MessageBoxW(hDlg, resultstr, resourcestr, MB_OK | MB_ICONWARNING);
387 return FALSE;
388 }
389 lppd->nFromPage = nFromPage;
390 lppd->nToPage = nToPage;
391 lppd->Flags |= PD_PAGENUMS;
392 }
393 else
394 lppd->Flags &= ~PD_PAGENUMS;
395
396 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
397 lppd->Flags |= PD_SELECTION;
398 else
399 lppd->Flags &= ~PD_SELECTION;
400
401 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
402 static char file[] = "FILE:";
403 lppd->Flags |= PD_PRINTTOFILE;
404 pi->pPortName = file;
405 }
406
407 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
408 FIXME("Collate lppd not yet implemented as output\n");
409 }
410
411 /* set PD_Collate and nCopies */
412 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
413 /* The application doesn't support multiple copies or collate...
414 */
415 lppd->Flags &= ~PD_COLLATE;
416 lppd->nCopies = 1;
417 /* if the printer driver supports it... store info there
418 * otherwise no collate & multiple copies !
419 */
420 if (lpdm->dmFields & DM_COLLATE)
421 lpdm->dmCollate =
422 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
423 if (lpdm->dmFields & DM_COPIES)
424 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
425 } else {
426 /* Application is responsible for multiple copies */
427 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
428 lppd->Flags |= PD_COLLATE;
429 else
430 lppd->Flags &= ~PD_COLLATE;
431 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
432 /* multiple copies already included in the document. Driver must print only one copy */
433 lpdm->u1.s1.dmCopies = 1;
434 }
435
436 /* Print quality, PrintDlg16 */
437 if(GetDlgItem(hDlg, cmb1))
438 {
439 HWND hQuality = GetDlgItem(hDlg, cmb1);
440 int Sel = SendMessageA(hQuality, CB_GETCURSEL, 0, 0);
441
442 if(Sel != CB_ERR)
443 {
444 LONG dpi = SendMessageA(hQuality, CB_GETITEMDATA, Sel, 0);
445 lpdm->dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
446 lpdm->u1.s1.dmPrintQuality = LOWORD(dpi);
447 lpdm->dmYResolution = HIWORD(dpi);
448 }
449 }
450 }
451 return TRUE;
452 }
453
454 static BOOL PRINTDLG_UpdatePrintDlgW(HWND hDlg,
455 PRINT_PTRW* PrintStructures)
456 {
457 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
458 PDEVMODEW lpdm = PrintStructures->lpDevMode;
459 LPPRINTER_INFO_2W pi = PrintStructures->lpPrinterInfo;
460
461
462 if(!lpdm) {
463 FIXME("No lpdm ptr?\n");
464 return FALSE;
465 }
466
467
468 if(!(lppd->Flags & PD_PRINTSETUP)) {
469 /* check whether nFromPage and nToPage are within range defined by
470 * nMinPage and nMaxPage
471 */
472 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
473 WORD nToPage;
474 WORD nFromPage;
475 BOOL translated;
476 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
477 nToPage = GetDlgItemInt(hDlg, edt2, &translated, FALSE);
478
479 /* if no ToPage value is entered, use the FromPage value */
480 if(!translated) nToPage = nFromPage;
481
482 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
483 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
484 WCHAR resourcestr[256];
485 WCHAR resultstr[256];
486 LoadStringW(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
487 resourcestr, 255);
488 wsprintfW(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
489 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,
490 resourcestr, 255);
491 MessageBoxW(hDlg, resultstr, resourcestr,
492 MB_OK | MB_ICONWARNING);
493 return FALSE;
494 }
495 lppd->nFromPage = nFromPage;
496 lppd->nToPage = nToPage;
497 lppd->Flags |= PD_PAGENUMS;
498 }
499 else
500 lppd->Flags &= ~PD_PAGENUMS;
501
502 if (IsDlgButtonChecked(hDlg, rad2) == BST_CHECKED) /* Selection */
503 lppd->Flags |= PD_SELECTION;
504 else
505 lppd->Flags &= ~PD_SELECTION;
506
507 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
508 static WCHAR file[] = {'F','I','L','E',':',0};
509 lppd->Flags |= PD_PRINTTOFILE;
510 pi->pPortName = file;
511 }
512
513 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
514 FIXME("Collate lppd not yet implemented as output\n");
515 }
516
517 /* set PD_Collate and nCopies */
518 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
519 /* The application doesn't support multiple copies or collate...
520 */
521 lppd->Flags &= ~PD_COLLATE;
522 lppd->nCopies = 1;
523 /* if the printer driver supports it... store info there
524 * otherwise no collate & multiple copies !
525 */
526 if (lpdm->dmFields & DM_COLLATE)
527 lpdm->dmCollate =
528 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
529 if (lpdm->dmFields & DM_COPIES)
530 lpdm->u1.s1.dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
531 } else {
532 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
533 lppd->Flags |= PD_COLLATE;
534 else
535 lppd->Flags &= ~PD_COLLATE;
536 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
537 }
538 }
539 return TRUE;
540 }
541
542 /************************************************************************
543 * PRINTDLG_SetUpPaperComboBox
544 *
545 * Initialize either the papersize or inputslot combos of the Printer Setup
546 * dialog. We store the associated word (eg DMPAPER_A4) as the item data.
547 * We also try to re-select the old selection.
548 */
549 static BOOL PRINTDLG_SetUpPaperComboBoxA(HWND hDlg,
550 int nIDComboBox,
551 char* PrinterName,
552 char* PortName,
553 LPDEVMODEA dm)
554 {
555 int i;
556 int NrOfEntries;
557 char* Names;
558 WORD* Words;
559 DWORD Sel;
560 WORD oldWord = 0;
561 int NamesSize;
562 int fwCapability_Names;
563 int fwCapability_Words;
564
565 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",PrinterName,PortName,nIDComboBox);
566
567 /* query the dialog box for the current selected value */
568 Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
569 if(Sel != CB_ERR) {
570 /* we enter here only if a different printer is selected after
571 * the Print Setup dialog is opened. The current settings are
572 * stored into the newly selected printer.
573 */
574 oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
575 Sel, 0);
576 if (dm) {
577 if (nIDComboBox == cmb2)
578 dm->u1.s1.dmPaperSize = oldWord;
579 else
580 dm->u1.s1.dmDefaultSource = oldWord;
581 }
582 }
583 else {
584 /* we enter here only when the Print setup dialog is initially
585 * opened. In this case the settings are restored from when
586 * the dialog was last closed.
587 */
588 if (dm) {
589 if (nIDComboBox == cmb2)
590 oldWord = dm->u1.s1.dmPaperSize;
591 else
592 oldWord = dm->u1.s1.dmDefaultSource;
593 }
594 }
595
596 if (nIDComboBox == cmb2) {
597 NamesSize = 64;
598 fwCapability_Names = DC_PAPERNAMES;
599 fwCapability_Words = DC_PAPERS;
600 } else {
601 nIDComboBox = cmb3;
602 NamesSize = 24;
603 fwCapability_Names = DC_BINNAMES;
604 fwCapability_Words = DC_BINS;
605 }
606
607 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
608 fwCapability_Names, NULL, dm);
609 if (NrOfEntries == 0)
610 WARN("no Name Entries found!\n");
611 else if (NrOfEntries < 0)
612 return FALSE;
613
614 if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
615 != NrOfEntries) {
616 ERR("Number of caps is different\n");
617 NrOfEntries = 0;
618 }
619
620 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(char)*NamesSize);
621 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
622 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
623 fwCapability_Names, Names, dm);
624 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
625 fwCapability_Words, (LPSTR)Words, dm);
626
627 /* reset any current content in the combobox */
628 SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
629
630 /* store new content */
631 for (i = 0; i < NrOfEntries; i++) {
632 DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
633 (LPARAM)(&Names[i*NamesSize]) );
634 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
635 Words[i]);
636 }
637
638 /* Look for old selection - can't do this is previous loop since
639 item order will change as more items are added */
640 Sel = 0;
641 for (i = 0; i < NrOfEntries; i++) {
642 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
643 oldWord) {
644 Sel = i;
645 break;
646 }
647 }
648 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
649
650 HeapFree(GetProcessHeap(),0,Words);
651 HeapFree(GetProcessHeap(),0,Names);
652 return TRUE;
653 }
654
655 static BOOL PRINTDLG_SetUpPaperComboBoxW(HWND hDlg,
656 int nIDComboBox,
657 const WCHAR* PrinterName,
658 const WCHAR* PortName,
659 LPDEVMODEW dm)
660 {
661 int i;
662 int NrOfEntries;
663 WCHAR* Names;
664 WORD* Words;
665 DWORD Sel;
666 WORD oldWord = 0;
667 int NamesSize;
668 int fwCapability_Names;
669 int fwCapability_Words;
670
671 TRACE(" Printer: %s, Port: %s, ComboID: %d\n",debugstr_w(PrinterName),debugstr_w(PortName),nIDComboBox);
672
673 /* query the dialog box for the current selected value */
674 Sel = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
675 if(Sel != CB_ERR) {
676 /* we enter here only if a different printer is selected after
677 * the Print Setup dialog is opened. The current settings are
678 * stored into the newly selected printer.
679 */
680 oldWord = SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA,
681 Sel, 0);
682 if (dm) {
683 if (nIDComboBox == cmb2)
684 dm->u1.s1.dmPaperSize = oldWord;
685 else
686 dm->u1.s1.dmDefaultSource = oldWord;
687 }
688 }
689 else {
690 /* we enter here only when the Print setup dialog is initially
691 * opened. In this case the settings are restored from when
692 * the dialog was last closed.
693 */
694 if (dm) {
695 if (nIDComboBox == cmb2)
696 oldWord = dm->u1.s1.dmPaperSize;
697 else
698 oldWord = dm->u1.s1.dmDefaultSource;
699 }
700 }
701
702 if (nIDComboBox == cmb2) {
703 NamesSize = 64;
704 fwCapability_Names = DC_PAPERNAMES;
705 fwCapability_Words = DC_PAPERS;
706 } else {
707 nIDComboBox = cmb3;
708 NamesSize = 24;
709 fwCapability_Names = DC_BINNAMES;
710 fwCapability_Words = DC_BINS;
711 }
712
713 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
714 fwCapability_Names, NULL, dm);
715 if (NrOfEntries == 0)
716 WARN("no Name Entries found!\n");
717 else if (NrOfEntries < 0)
718 return FALSE;
719
720 if(DeviceCapabilitiesW(PrinterName, PortName, fwCapability_Words, NULL, dm)
721 != NrOfEntries) {
722 ERR("Number of caps is different\n");
723 NrOfEntries = 0;
724 }
725
726 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WCHAR)*NamesSize);
727 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
728 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
729 fwCapability_Names, Names, dm);
730 NrOfEntries = DeviceCapabilitiesW(PrinterName, PortName,
731 fwCapability_Words, Words, dm);
732
733 /* reset any current content in the combobox */
734 SendDlgItemMessageW(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
735
736 /* store new content */
737 for (i = 0; i < NrOfEntries; i++) {
738 DWORD pos = SendDlgItemMessageW(hDlg, nIDComboBox, CB_ADDSTRING, 0,
739 (LPARAM)(&Names[i*NamesSize]) );
740 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
741 Words[i]);
742 }
743
744 /* Look for old selection - can't do this is previous loop since
745 item order will change as more items are added */
746 Sel = 0;
747 for (i = 0; i < NrOfEntries; i++) {
748 if(SendDlgItemMessageW(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
749 oldWord) {
750 Sel = i;
751 break;
752 }
753 }
754 SendDlgItemMessageW(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
755
756 HeapFree(GetProcessHeap(),0,Words);
757 HeapFree(GetProcessHeap(),0,Names);
758 return TRUE;
759 }
760
761
762 /***********************************************************************
763 * PRINTDLG_UpdatePrinterInfoTexts [internal]
764 */
765 static void PRINTDLG_UpdatePrinterInfoTextsA(HWND hDlg, const PRINTER_INFO_2A *pi)
766 {
767 char StatusMsg[256];
768 char ResourceString[256];
769 int i;
770
771 /* Status Message */
772 StatusMsg[0]='\0';
773
774 /* add all status messages */
775 for (i = 0; i < 25; i++) {
776 if (pi->Status & (1<<i)) {
777 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
778 ResourceString, 255);
779 strcat(StatusMsg,ResourceString);
780 }
781 }
782 /* append "ready" */
783 /* FIXME: status==ready must only be appended if really so.
784 but how to detect? */
785 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
786 ResourceString, 255);
787 strcat(StatusMsg,ResourceString);
788 SetDlgItemTextA(hDlg, stc12, StatusMsg);
789
790 /* set all other printer info texts */
791 SetDlgItemTextA(hDlg, stc11, pi->pDriverName);
792
793 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
794 SetDlgItemTextA(hDlg, stc14, pi->pLocation);
795 else
796 SetDlgItemTextA(hDlg, stc14, pi->pPortName);
797 SetDlgItemTextA(hDlg, stc13, pi->pComment ? pi->pComment : "");
798 return;
799 }
800
801 static void PRINTDLG_UpdatePrinterInfoTextsW(HWND hDlg, const PRINTER_INFO_2W *pi)
802 {
803 WCHAR StatusMsg[256];
804 WCHAR ResourceString[256];
805 static const WCHAR emptyW[] = {0};
806 int i;
807
808 /* Status Message */
809 StatusMsg[0]='\0';
810
811 /* add all status messages */
812 for (i = 0; i < 25; i++) {
813 if (pi->Status & (1<<i)) {
814 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
815 ResourceString, 255);
816 lstrcatW(StatusMsg,ResourceString);
817 }
818 }
819 /* append "ready" */
820 /* FIXME: status==ready must only be appended if really so.
821 but how to detect? */
822 LoadStringW(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
823 ResourceString, 255);
824 lstrcatW(StatusMsg,ResourceString);
825 SetDlgItemTextW(hDlg, stc12, StatusMsg);
826
827 /* set all other printer info texts */
828 SetDlgItemTextW(hDlg, stc11, pi->pDriverName);
829 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
830 SetDlgItemTextW(hDlg, stc14, pi->pLocation);
831 else
832 SetDlgItemTextW(hDlg, stc14, pi->pPortName);
833 SetDlgItemTextW(hDlg, stc13, pi->pComment ? pi->pComment : emptyW);
834 }
835
836
837 /*******************************************************************
838 *
839 * PRINTDLG_ChangePrinter
840 *
841 */
842 BOOL PRINTDLG_ChangePrinterA(HWND hDlg, char *name,
843 PRINT_PTRA *PrintStructures)
844 {
845 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
846 LPDEVMODEA lpdm = NULL;
847 LONG dmSize;
848 DWORD needed;
849 HANDLE hprn;
850
851 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
852 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
853 if(!OpenPrinterA(name, &hprn, NULL)) {
854 ERR("Can't open printer %s\n", name);
855 return FALSE;
856 }
857 GetPrinterA(hprn, 2, NULL, 0, &needed);
858 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
859 GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
860 &needed);
861 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
862 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
863 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
864 needed, &needed)) {
865 ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
866 return FALSE;
867 }
868 ClosePrinter(hprn);
869
870 PRINTDLG_UpdatePrinterInfoTextsA(hDlg, PrintStructures->lpPrinterInfo);
871
872 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
873 PrintStructures->lpDevMode = NULL;
874
875 dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
876 if(dmSize == -1) {
877 ERR("DocumentProperties fails on %s\n", debugstr_a(name));
878 return FALSE;
879 }
880 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
881 dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
882 DM_OUT_BUFFER);
883 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
884 !lstrcmpA( (LPSTR) lpdm->dmDeviceName,
885 (LPSTR) PrintStructures->lpDevMode->dmDeviceName)) {
886 /* Supplied devicemode matches current printer so try to use it */
887 DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
888 DM_OUT_BUFFER | DM_IN_BUFFER);
889 }
890 if(lpdm)
891 GlobalUnlock(lppd->hDevMode);
892
893 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
894
895 if(!(lppd->Flags & PD_PRINTSETUP)) {
896 /* Print range (All/Range/Selection) */
897 if(lppd->nFromPage != 0xffff)
898 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
899 if(lppd->nToPage != 0xffff)
900 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
901
902 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
903 if (lppd->Flags & PD_NOSELECTION)
904 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
905 else
906 if (lppd->Flags & PD_SELECTION)
907 CheckRadioButton(hDlg, rad1, rad3, rad2);
908 if (lppd->Flags & PD_NOPAGENUMS) {
909 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
910 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
911 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
912 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
913 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
914 } else {
915 if (lppd->Flags & PD_PAGENUMS)
916 CheckRadioButton(hDlg, rad1, rad3, rad3);
917 }
918
919 /* Collate pages
920 *
921 * FIXME: The ico3 is not displayed for some reason. I don't know why.
922 */
923 if (lppd->Flags & PD_COLLATE) {
924 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
925 (LPARAM)PrintStructures->hCollateIcon);
926 CheckDlgButton(hDlg, chx2, 1);
927 } else {
928 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
929 (LPARAM)PrintStructures->hNoCollateIcon);
930 CheckDlgButton(hDlg, chx2, 0);
931 }
932
933 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
934 /* if printer doesn't support it: no Collate */
935 if (!(lpdm->dmFields & DM_COLLATE)) {
936 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
937 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
938 }
939 }
940
941 /* nCopies */
942 {
943 INT copies;
944 if (lppd->hDevMode == 0)
945 copies = lppd->nCopies;
946 else
947 copies = lpdm->u1.s1.dmCopies;
948 if(copies == 0) copies = 1;
949 else if(copies < 0) copies = MAX_COPIES;
950 SetDlgItemInt(hDlg, edt3, copies, FALSE);
951 }
952
953 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
954 /* if printer doesn't support it: no nCopies */
955 if (!(lpdm->dmFields & DM_COPIES)) {
956 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
957 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
958 }
959 }
960
961 /* print to file */
962 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
963 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
964 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
965 if (lppd->Flags & PD_HIDEPRINTTOFILE)
966 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
967
968 /* Fill print quality combo, PrintDlg16 */
969 if(GetDlgItem(hDlg, cmb1))
970 {
971 DWORD numResolutions = DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
972 PrintStructures->lpPrinterInfo->pPortName,
973 DC_ENUMRESOLUTIONS, NULL, lpdm);
974
975 if(numResolutions != -1)
976 {
977 HWND hQuality = GetDlgItem(hDlg, cmb1);
978 LONG* Resolutions;
979 char buf[255];
980 DWORD i;
981 int dpiX, dpiY;
982 HDC hPrinterDC = CreateDCA(PrintStructures->lpPrinterInfo->pDriverName,
983 PrintStructures->lpPrinterInfo->pPrinterName,
984 0, lpdm);
985
986 Resolutions = HeapAlloc(GetProcessHeap(), 0, numResolutions*sizeof(LONG)*2);
987 DeviceCapabilitiesA(PrintStructures->lpPrinterInfo->pPrinterName,
988 PrintStructures->lpPrinterInfo->pPortName,
989 DC_ENUMRESOLUTIONS, (LPSTR)Resolutions, lpdm);
990
991 dpiX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
992 dpiY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
993 DeleteDC(hPrinterDC);
994
995 SendMessageA(hQuality, CB_RESETCONTENT, 0, 0);
996 for(i = 0; i < (numResolutions * 2); i += 2)
997 {
998 BOOL IsDefault = FALSE;
999 LRESULT Index;
1000
1001 if(Resolutions[i] == Resolutions[i+1])
1002 {
1003 if(dpiX == Resolutions[i])
1004 IsDefault = TRUE;
1005 sprintf(buf, "%d dpi", Resolutions[i]);
1006 } else
1007 {
1008 if(dpiX == Resolutions[i] && dpiY == Resolutions[i+1])
1009 IsDefault = TRUE;
1010 sprintf(buf, "%d dpi x %d dpi", Resolutions[i], Resolutions[i+1]);
1011 }
1012
1013 Index = SendMessageA(hQuality, CB_ADDSTRING, 0, (LPARAM)buf);
1014
1015 if(IsDefault)
1016 SendMessageA(hQuality, CB_SETCURSEL, Index, 0);
1017
1018 SendMessageA(hQuality, CB_SETITEMDATA, Index, MAKELONG(dpiX,dpiY));
1019 }
1020 HeapFree(GetProcessHeap(), 0, Resolutions);
1021 }
1022 }
1023 } else { /* PD_PRINTSETUP */
1024 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1025
1026 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb2,
1027 PrintStructures->lpPrinterInfo->pPrinterName,
1028 PrintStructures->lpPrinterInfo->pPortName,
1029 lpdm);
1030 PRINTDLG_SetUpPaperComboBoxA(hDlg, cmb3,
1031 PrintStructures->lpPrinterInfo->pPrinterName,
1032 PrintStructures->lpPrinterInfo->pPortName,
1033 lpdm);
1034 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1035 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1036 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1037 PrintStructures->hLandscapeIcon));
1038
1039 }
1040
1041 /* help button */
1042 if ((lppd->Flags & PD_SHOWHELP)==0) {
1043 /* hide if PD_SHOWHELP not specified */
1044 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1045 }
1046 return TRUE;
1047 }
1048
1049 static BOOL PRINTDLG_ChangePrinterW(HWND hDlg, WCHAR *name,
1050 PRINT_PTRW *PrintStructures)
1051 {
1052 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1053 LPDEVMODEW lpdm = NULL;
1054 LONG dmSize;
1055 DWORD needed;
1056 HANDLE hprn;
1057
1058 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
1059 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
1060 if(!OpenPrinterW(name, &hprn, NULL)) {
1061 ERR("Can't open printer %s\n", debugstr_w(name));
1062 return FALSE;
1063 }
1064 GetPrinterW(hprn, 2, NULL, 0, &needed);
1065 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
1066 GetPrinterW(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
1067 &needed);
1068 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
1069 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
1070 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
1071 needed, &needed)) {
1072 ERR("GetPrinterDriverA failed for %s, fix your config!\n",debugstr_w(PrintStructures->lpPrinterInfo->pPrinterName));
1073 return FALSE;
1074 }
1075 ClosePrinter(hprn);
1076
1077 PRINTDLG_UpdatePrinterInfoTextsW(hDlg, PrintStructures->lpPrinterInfo);
1078
1079 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1080 PrintStructures->lpDevMode = NULL;
1081
1082 dmSize = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
1083 if(dmSize == -1) {
1084 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
1085 return FALSE;
1086 }
1087 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
1088 dmSize = DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, NULL,
1089 DM_OUT_BUFFER);
1090 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
1091 !lstrcmpW(lpdm->dmDeviceName,
1092 PrintStructures->lpDevMode->dmDeviceName)) {
1093 /* Supplied devicemode matches current printer so try to use it */
1094 DocumentPropertiesW(0, 0, name, PrintStructures->lpDevMode, lpdm,
1095 DM_OUT_BUFFER | DM_IN_BUFFER);
1096 }
1097 if(lpdm)
1098 GlobalUnlock(lppd->hDevMode);
1099
1100 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
1101
1102 if(!(lppd->Flags & PD_PRINTSETUP)) {
1103 /* Print range (All/Range/Selection) */
1104 if(lppd->nFromPage != 0xffff)
1105 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
1106 if(lppd->nToPage != 0xffff)
1107 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
1108
1109 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
1110 if (lppd->Flags & PD_NOSELECTION)
1111 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
1112 else
1113 if (lppd->Flags & PD_SELECTION)
1114 CheckRadioButton(hDlg, rad1, rad3, rad2);
1115 if (lppd->Flags & PD_NOPAGENUMS) {
1116 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
1117 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
1118 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
1119 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
1120 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
1121 } else {
1122 if (lppd->Flags & PD_PAGENUMS)
1123 CheckRadioButton(hDlg, rad1, rad3, rad3);
1124 }
1125
1126 /* Collate pages
1127 *
1128 * FIXME: The ico3 is not displayed for some reason. I don't know why.
1129 */
1130 if (lppd->Flags & PD_COLLATE) {
1131 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1132 (LPARAM)PrintStructures->hCollateIcon);
1133 CheckDlgButton(hDlg, chx2, 1);
1134 } else {
1135 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1136 (LPARAM)PrintStructures->hNoCollateIcon);
1137 CheckDlgButton(hDlg, chx2, 0);
1138 }
1139
1140 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1141 /* if printer doesn't support it: no Collate */
1142 if (!(lpdm->dmFields & DM_COLLATE)) {
1143 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1144 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
1145 }
1146 }
1147
1148 /* nCopies */
1149 {
1150 INT copies;
1151 if (lppd->hDevMode == 0)
1152 copies = lppd->nCopies;
1153 else
1154 copies = lpdm->u1.s1.dmCopies;
1155 if(copies == 0) copies = 1;
1156 else if(copies < 0) copies = MAX_COPIES;
1157 SetDlgItemInt(hDlg, edt3, copies, FALSE);
1158 }
1159
1160 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
1161 /* if printer doesn't support it: no nCopies */
1162 if (!(lpdm->dmFields & DM_COPIES)) {
1163 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
1164 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
1165 }
1166 }
1167
1168 /* print to file */
1169 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
1170 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
1171 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
1172 if (lppd->Flags & PD_HIDEPRINTTOFILE)
1173 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
1174
1175 } else { /* PD_PRINTSETUP */
1176 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
1177
1178 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2,
1179 PrintStructures->lpPrinterInfo->pPrinterName,
1180 PrintStructures->lpPrinterInfo->pPortName,
1181 lpdm);
1182 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3,
1183 PrintStructures->lpPrinterInfo->pPrinterName,
1184 PrintStructures->lpPrinterInfo->pPortName,
1185 lpdm);
1186 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
1187 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1188 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
1189 PrintStructures->hLandscapeIcon));
1190
1191 }
1192
1193 /* help button */
1194 if ((lppd->Flags & PD_SHOWHELP)==0) {
1195 /* hide if PD_SHOWHELP not specified */
1196 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
1197 }
1198 return TRUE;
1199 }
1200
1201 /***********************************************************************
1202 * check_printer_setup [internal]
1203 */
1204 static LRESULT check_printer_setup(HWND hDlg)
1205 {
1206 DWORD needed,num;
1207 WCHAR resourcestr[256],resultstr[256];
1208
1209 EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
1210 if(needed == 0)
1211 {
1212 EnumPrintersW(PRINTER_ENUM_CONNECTIONS, NULL, 2, NULL, 0, &needed, &num);
1213 }
1214 if(needed > 0)
1215 return TRUE;
1216 else
1217 {
1218 LoadStringW(COMDLG32_hInstance, PD32_NO_DEVICES,resultstr, 255);
1219 LoadStringW(COMDLG32_hInstance, PD32_PRINT_TITLE,resourcestr, 255);
1220 MessageBoxW(hDlg, resultstr, resourcestr,MB_OK | MB_ICONWARNING);
1221 return FALSE;
1222 }
1223 }
1224
1225 /***********************************************************************
1226 * PRINTDLG_WMInitDialog [internal]
1227 */
1228 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
1229 PRINT_PTRA* PrintStructures)
1230 {
1231 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1232 DEVNAMES *pdn;
1233 DEVMODEA *pdm;
1234 char *name = NULL;
1235 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1236
1237 /* load Collate ICONs */
1238 /* We load these with LoadImage because they are not a standard
1239 size and we don't want them rescaled */
1240 PrintStructures->hCollateIcon =
1241 LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
1242 PrintStructures->hNoCollateIcon =
1243 LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
1244
1245 /* These can be done with LoadIcon */
1246 PrintStructures->hPortraitIcon =
1247 LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
1248 PrintStructures->hLandscapeIcon =
1249 LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
1250
1251 /* display the collate/no_collate icon */
1252 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1253 (LPARAM)PrintStructures->hNoCollateIcon);
1254
1255 if(PrintStructures->hCollateIcon == 0 ||
1256 PrintStructures->hNoCollateIcon == 0 ||
1257 PrintStructures->hPortraitIcon == 0 ||
1258 PrintStructures->hLandscapeIcon == 0) {
1259 ERR("no icon in resourcefile\n");
1260 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1261 EndDialog(hDlg, FALSE);
1262 }
1263
1264 /*
1265 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1266 * must be registered and the Help button must be shown.
1267 */
1268 if (lppd->Flags & PD_SHOWHELP) {
1269 if((PrintStructures->HelpMessageID =
1270 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
1271 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1272 return FALSE;
1273 }
1274 } else
1275 PrintStructures->HelpMessageID = 0;
1276
1277 if(!(lppd->Flags &PD_PRINTSETUP)) {
1278 PrintStructures->hwndUpDown =
1279 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1280 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1281 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1282 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1283 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1284 }
1285
1286 /* FIXME: I allow more freedom than either Win95 or WinNT,
1287 * which do not agree to what errors should be thrown or not
1288 * in case nToPage or nFromPage is out-of-range.
1289 */
1290 if (lppd->nMaxPage < lppd->nMinPage)
1291 lppd->nMaxPage = lppd->nMinPage;
1292 if (lppd->nMinPage == lppd->nMaxPage)
1293 lppd->Flags |= PD_NOPAGENUMS;
1294 if (lppd->nToPage < lppd->nMinPage)
1295 lppd->nToPage = lppd->nMinPage;
1296 if (lppd->nToPage > lppd->nMaxPage)
1297 lppd->nToPage = lppd->nMaxPage;
1298 if (lppd->nFromPage < lppd->nMinPage)
1299 lppd->nFromPage = lppd->nMinPage;
1300 if (lppd->nFromPage > lppd->nMaxPage)
1301 lppd->nFromPage = lppd->nMaxPage;
1302
1303 /* if we have the combo box, fill it */
1304 if (GetDlgItem(hDlg,comboID)) {
1305 /* Fill Combobox
1306 */
1307 pdn = GlobalLock(lppd->hDevNames);
1308 pdm = GlobalLock(lppd->hDevMode);
1309 if(pdn)
1310 name = (char*)pdn + pdn->wDeviceOffset;
1311 else if(pdm)
1312 name = (char*)pdm->dmDeviceName;
1313 PRINTDLG_SetUpPrinterListComboA(hDlg, comboID, name);
1314 if(pdm) GlobalUnlock(lppd->hDevMode);
1315 if(pdn) GlobalUnlock(lppd->hDevNames);
1316
1317 /* Now find selected printer and update rest of dlg */
1318 name = HeapAlloc(GetProcessHeap(),0,256);
1319 if (GetDlgItemTextA(hDlg, comboID, name, 255))
1320 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1321 HeapFree(GetProcessHeap(),0,name);
1322 } else {
1323 /* else use default printer */
1324 char name[200];
1325 DWORD dwBufLen = sizeof(name);
1326 BOOL ret = GetDefaultPrinterA(name, &dwBufLen);
1327
1328 if (ret)
1329 PRINTDLG_ChangePrinterA(hDlg, name, PrintStructures);
1330 else
1331 FIXME("No default printer found, expect problems!\n");
1332 }
1333 return TRUE;
1334 }
1335
1336 static LRESULT PRINTDLG_WMInitDialogW(HWND hDlg, WPARAM wParam,
1337 PRINT_PTRW* PrintStructures)
1338 {
1339 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1340 DEVNAMES *pdn;
1341 DEVMODEW *pdm;
1342 WCHAR *name = NULL;
1343 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1344
1345 /* load Collate ICONs */
1346 /* We load these with LoadImage because they are not a standard
1347 size and we don't want them rescaled */
1348 PrintStructures->hCollateIcon =
1349 LoadImageW(COMDLG32_hInstance, pd32_collateW, IMAGE_ICON, 0, 0, 0);
1350 PrintStructures->hNoCollateIcon =
1351 LoadImageW(COMDLG32_hInstance, pd32_nocollateW, IMAGE_ICON, 0, 0, 0);
1352
1353 /* These can be done with LoadIcon */
1354 PrintStructures->hPortraitIcon =
1355 LoadIconW(COMDLG32_hInstance, pd32_portraitW);
1356 PrintStructures->hLandscapeIcon =
1357 LoadIconW(COMDLG32_hInstance, pd32_landscapeW);
1358
1359 /* display the collate/no_collate icon */
1360 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1361 (LPARAM)PrintStructures->hNoCollateIcon);
1362
1363 if(PrintStructures->hCollateIcon == 0 ||
1364 PrintStructures->hNoCollateIcon == 0 ||
1365 PrintStructures->hPortraitIcon == 0 ||
1366 PrintStructures->hLandscapeIcon == 0) {
1367 ERR("no icon in resourcefile\n");
1368 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1369 EndDialog(hDlg, FALSE);
1370 }
1371
1372 /*
1373 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
1374 * must be registered and the Help button must be shown.
1375 */
1376 if (lppd->Flags & PD_SHOWHELP) {
1377 if((PrintStructures->HelpMessageID =
1378 RegisterWindowMessageW(HELPMSGSTRINGW)) == 0) {
1379 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
1380 return FALSE;
1381 }
1382 } else
1383 PrintStructures->HelpMessageID = 0;
1384
1385 if(!(lppd->Flags &PD_PRINTSETUP)) {
1386 PrintStructures->hwndUpDown =
1387 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
1388 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
1389 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
1390 hDlg, UPDOWN_ID, COMDLG32_hInstance,
1391 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
1392 }
1393
1394 /* FIXME: I allow more freedom than either Win95 or WinNT,
1395 * which do not agree to what errors should be thrown or not
1396 * in case nToPage or nFromPage is out-of-range.
1397 */
1398 if (lppd->nMaxPage < lppd->nMinPage)
1399 lppd->nMaxPage = lppd->nMinPage;
1400 if (lppd->nMinPage == lppd->nMaxPage)
1401 lppd->Flags |= PD_NOPAGENUMS;
1402 if (lppd->nToPage < lppd->nMinPage)
1403 lppd->nToPage = lppd->nMinPage;
1404 if (lppd->nToPage > lppd->nMaxPage)
1405 lppd->nToPage = lppd->nMaxPage;
1406 if (lppd->nFromPage < lppd->nMinPage)
1407 lppd->nFromPage = lppd->nMinPage;
1408 if (lppd->nFromPage > lppd->nMaxPage)
1409 lppd->nFromPage = lppd->nMaxPage;
1410
1411 /* if we have the combo box, fill it */
1412 if (GetDlgItem(hDlg,comboID)) {
1413 /* Fill Combobox
1414 */
1415 pdn = GlobalLock(lppd->hDevNames);
1416 pdm = GlobalLock(lppd->hDevMode);
1417 if(pdn)
1418 name = (WCHAR*)pdn + pdn->wDeviceOffset;
1419 else if(pdm)
1420 name = pdm->dmDeviceName;
1421 PRINTDLG_SetUpPrinterListComboW(hDlg, comboID, name);
1422 if(pdm) GlobalUnlock(lppd->hDevMode);
1423 if(pdn) GlobalUnlock(lppd->hDevNames);
1424
1425 /* Now find selected printer and update rest of dlg */
1426 /* ansi is ok here */
1427 name = HeapAlloc(GetProcessHeap(),0,256*sizeof(WCHAR));
1428 if (GetDlgItemTextW(hDlg, comboID, name, 255))
1429 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1430 HeapFree(GetProcessHeap(),0,name);
1431 } else {
1432 /* else use default printer */
1433 WCHAR name[200];
1434 DWORD dwBufLen = sizeof(name) / sizeof(WCHAR);
1435 BOOL ret = GetDefaultPrinterW(name, &dwBufLen);
1436
1437 if (ret)
1438 PRINTDLG_ChangePrinterW(hDlg, name, PrintStructures);
1439 else
1440 FIXME("No default printer found, expect problems!\n");
1441 }
1442 return TRUE;
1443 }
1444
1445 /***********************************************************************
1446 * PRINTDLG_WMCommand [internal]
1447 */
1448 LRESULT PRINTDLG_WMCommandA(HWND hDlg, WPARAM wParam,
1449 LPARAM lParam, PRINT_PTRA* PrintStructures)
1450 {
1451 LPPRINTDLGA lppd = PrintStructures->lpPrintDlg;
1452 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1453 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
1454
1455 switch (LOWORD(wParam)) {
1456 case IDOK:
1457 TRACE(" OK button was hit\n");
1458 if (!PRINTDLG_UpdatePrintDlgA(hDlg, PrintStructures)) {
1459 FIXME("Update printdlg was not successful!\n");
1460 return(FALSE);
1461 }
1462 EndDialog(hDlg, TRUE);
1463 return(TRUE);
1464
1465 case IDCANCEL:
1466 TRACE(" CANCEL button was hit\n");
1467 EndDialog(hDlg, FALSE);
1468 return(FALSE);
1469
1470 case pshHelp:
1471 TRACE(" HELP button was hit\n");
1472 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1473 (WPARAM) hDlg, (LPARAM) lppd);
1474 break;
1475
1476 case chx2: /* collate pages checkbox */
1477 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1478 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1479 (LPARAM)PrintStructures->hCollateIcon);
1480 else
1481 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1482 (LPARAM)PrintStructures->hNoCollateIcon);
1483 break;
1484 case edt1: /* from page nr editbox */
1485 case edt2: /* to page nr editbox */
1486 if (HIWORD(wParam)==EN_CHANGE) {
1487 WORD nToPage;
1488 WORD nFromPage;
1489 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1490 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1491 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1492 CheckRadioButton(hDlg, rad1, rad3, rad3);
1493 }
1494 break;
1495
1496 case edt3:
1497 if(HIWORD(wParam) == EN_CHANGE) {
1498 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1499 if(copies <= 1)
1500 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1501 else
1502 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1503 }
1504 break;
1505
1506 #if 0
1507 case psh1: /* Print Setup */
1508 {
1509 PRINTDLG16 pdlg;
1510
1511 if (!PrintStructures->dlg.lpPrintDlg16) {
1512 FIXME("The 32bit print dialog does not have this button!?\n");
1513 break;
1514 }
1515
1516 memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1517 pdlg.Flags |= PD_PRINTSETUP;
1518 pdlg.hwndOwner = HWND_16(hDlg);
1519 if (!PrintDlg16(&pdlg))
1520 break;
1521 }
1522 break;
1523 #endif
1524 case psh2: /* Properties button */
1525 {
1526 HANDLE hPrinter;
1527 char PrinterName[256];
1528
1529 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1530 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1531 FIXME(" Call to OpenPrinter did not succeed!\n");
1532 break;
1533 }
1534 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1535 PrintStructures->lpDevMode,
1536 PrintStructures->lpDevMode,
1537 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1538 ClosePrinter(hPrinter);
1539 break;
1540 }
1541
1542 case rad1: /* Paperorientation */
1543 if (lppd->Flags & PD_PRINTSETUP)
1544 {
1545 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1546 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1547 (LPARAM)(PrintStructures->hPortraitIcon));
1548 }
1549 break;
1550
1551 case rad2: /* Paperorientation */
1552 if (lppd->Flags & PD_PRINTSETUP)
1553 {
1554 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1555 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1556 (LPARAM)(PrintStructures->hLandscapeIcon));
1557 }
1558 break;
1559
1560 case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT16 */
1561 if (PrinterComboID != LOWORD(wParam)) {
1562 break;
1563 }
1564 /* FALLTHROUGH */
1565 case cmb4: /* Printer combobox */
1566 if (HIWORD(wParam)==CBN_SELCHANGE) {
1567 char PrinterName[256];
1568 GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1569 PRINTDLG_ChangePrinterA(hDlg, PrinterName, PrintStructures);
1570 }
1571 break;
1572
1573 case cmb2: /* Papersize */
1574 {
1575 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1576 if(Sel != CB_ERR)
1577 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1578 CB_GETITEMDATA,
1579 Sel, 0);
1580 }
1581 break;
1582
1583 case cmb3: /* Bin */
1584 {
1585 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1586 if(Sel != CB_ERR)
1587 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1588 CB_GETITEMDATA, Sel,
1589 0);
1590 }
1591 break;
1592 }
1593 if(lppd->Flags & PD_PRINTSETUP) {
1594 switch (LOWORD(wParam)) {
1595 case rad1: /* orientation */
1596 case rad2:
1597 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1598 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1599 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1600 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1601 (WPARAM)IMAGE_ICON,
1602 (LPARAM)PrintStructures->hPortraitIcon);
1603 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1604 (WPARAM)IMAGE_ICON,
1605 (LPARAM)PrintStructures->hPortraitIcon);
1606 }
1607 } else {
1608 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1609 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1610 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1611 (WPARAM)IMAGE_ICON,
1612 (LPARAM)PrintStructures->hLandscapeIcon);
1613 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1614 (WPARAM)IMAGE_ICON,
1615 (LPARAM)PrintStructures->hLandscapeIcon);
1616 }
1617 }
1618 break;
1619 }
1620 }
1621 return FALSE;
1622 }
1623
1624 static LRESULT PRINTDLG_WMCommandW(HWND hDlg, WPARAM wParam,
1625 LPARAM lParam, PRINT_PTRW* PrintStructures)
1626 {
1627 LPPRINTDLGW lppd = PrintStructures->lpPrintDlg;
1628 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
1629 LPDEVMODEW lpdm = PrintStructures->lpDevMode;
1630
1631 switch (LOWORD(wParam)) {
1632 case IDOK:
1633 TRACE(" OK button was hit\n");
1634 if (!PRINTDLG_UpdatePrintDlgW(hDlg, PrintStructures)) {
1635 FIXME("Update printdlg was not successful!\n");
1636 return(FALSE);
1637 }
1638 EndDialog(hDlg, TRUE);
1639 return(TRUE);
1640
1641 case IDCANCEL:
1642 TRACE(" CANCEL button was hit\n");
1643 EndDialog(hDlg, FALSE);
1644 return(FALSE);
1645
1646 case pshHelp:
1647 TRACE(" HELP button was hit\n");
1648 SendMessageW(lppd->hwndOwner, PrintStructures->HelpMessageID,
1649 (WPARAM) hDlg, (LPARAM) lppd);
1650 break;
1651
1652 case chx2: /* collate pages checkbox */
1653 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1654 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1655 (LPARAM)PrintStructures->hCollateIcon);
1656 else
1657 SendDlgItemMessageW(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1658 (LPARAM)PrintStructures->hNoCollateIcon);
1659 break;
1660 case edt1: /* from page nr editbox */
1661 case edt2: /* to page nr editbox */
1662 if (HIWORD(wParam)==EN_CHANGE) {
1663 WORD nToPage;
1664 WORD nFromPage;
1665 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1666 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1667 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1668 CheckRadioButton(hDlg, rad1, rad3, rad3);
1669 }
1670 break;
1671
1672 case edt3:
1673 if(HIWORD(wParam) == EN_CHANGE) {
1674 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1675 if(copies <= 1)
1676 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1677 else
1678 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1679 }
1680 break;
1681
1682 case psh1: /* Print Setup */
1683 {
1684 ERR("psh1 is called from 16bit code only, we should not get here.\n");
1685 }
1686 break;
1687 case psh2: /* Properties button */
1688 {
1689 HANDLE hPrinter;
1690 WCHAR PrinterName[256];
1691
1692 if (!GetDlgItemTextW(hDlg, PrinterComboID, PrinterName, 255)) break;
1693 if (!OpenPrinterW(PrinterName, &hPrinter, NULL)) {
1694 FIXME(" Call to OpenPrinter did not succeed!\n");
1695 break;
1696 }
1697 DocumentPropertiesW(hDlg, hPrinter, PrinterName,
1698 PrintStructures->lpDevMode,
1699 PrintStructures->lpDevMode,
1700 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1701 ClosePrinter(hPrinter);
1702 break;
1703 }
1704
1705 case rad1: /* Paperorientation */
1706 if (lppd->Flags & PD_PRINTSETUP)
1707 {
1708 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1709 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1710 (LPARAM)(PrintStructures->hPortraitIcon));
1711 }
1712 break;
1713
1714 case rad2: /* Paperorientation */
1715 if (lppd->Flags & PD_PRINTSETUP)
1716 {
1717 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1718 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1719 (LPARAM)(PrintStructures->hLandscapeIcon));
1720 }
1721 break;
1722
1723 case cmb1: /* Printer Combobox in PRINT SETUP */
1724 /* FALLTHROUGH */
1725 case cmb4: /* Printer combobox */
1726 if (HIWORD(wParam)==CBN_SELCHANGE) {
1727 WCHAR PrinterName[256];
1728 GetDlgItemTextW(hDlg, LOWORD(wParam), PrinterName, 255);
1729 PRINTDLG_ChangePrinterW(hDlg, PrinterName, PrintStructures);
1730 }
1731 break;
1732
1733 case cmb2: /* Papersize */
1734 {
1735 DWORD Sel = SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1736 if(Sel != CB_ERR)
1737 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageW(hDlg, cmb2,
1738 CB_GETITEMDATA,
1739 Sel, 0);
1740 }
1741 break;
1742
1743 case cmb3: /* Bin */
1744 {
1745 DWORD Sel = SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1746 if(Sel != CB_ERR)
1747 lpdm->u1.s1.dmDefaultSource = SendDlgItemMessageW(hDlg, cmb3,
1748 CB_GETITEMDATA, Sel,
1749 0);
1750 }
1751 break;
1752 }
1753 if(lppd->Flags & PD_PRINTSETUP) {
1754 switch (LOWORD(wParam)) {
1755 case rad1: /* orientation */
1756 case rad2:
1757 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1758 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1759 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1760 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1761 (WPARAM)IMAGE_ICON,
1762 (LPARAM)PrintStructures->hPortraitIcon);
1763 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1764 (WPARAM)IMAGE_ICON,
1765 (LPARAM)PrintStructures->hPortraitIcon);
1766 }
1767 } else {
1768 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1769 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1770 SendDlgItemMessageW(hDlg, stc10, STM_SETIMAGE,
1771 (WPARAM)IMAGE_ICON,
1772 (LPARAM)PrintStructures->hLandscapeIcon);
1773 SendDlgItemMessageW(hDlg, ico1, STM_SETIMAGE,
1774 (WPARAM)IMAGE_ICON,
1775 (LPARAM)PrintStructures->hLandscapeIcon);
1776 }
1777 }
1778 break;
1779 }
1780 }
1781 return FALSE;
1782 }
1783
1784 /***********************************************************************
1785 * PrintDlgProcA [internal]
1786 */
1787 static INT_PTR CALLBACK PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1788 LPARAM lParam)
1789 {
1790 PRINT_PTRA* PrintStructures;
1791 INT_PTR res = FALSE;
1792
1793 if (uMsg!=WM_INITDIALOG) {
1794 PrintStructures = GetPropW(hDlg, printdlg_prop);
1795 if (!PrintStructures)
1796 return FALSE;
1797 } else {
1798 PrintStructures = (PRINT_PTRA*) lParam;
1799 SetPropW(hDlg, printdlg_prop, PrintStructures);
1800 if(!check_printer_setup(hDlg))
1801 {
1802 EndDialog(hDlg,FALSE);
1803 return FALSE;
1804 }
1805 res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1806
1807 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1808 res = PrintStructures->lpPrintDlg->lpfnPrintHook(
1809 hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg
1810 );
1811 return res;
1812 }
1813
1814 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1815 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1816 lParam);
1817 if(res) return res;
1818 }
1819
1820 switch (uMsg) {
1821 case WM_COMMAND:
1822 return PRINTDLG_WMCommandA(hDlg, wParam, lParam, PrintStructures);
1823
1824 case WM_DESTROY:
1825 DestroyIcon(PrintStructures->hCollateIcon);
1826 DestroyIcon(PrintStructures->hNoCollateIcon);
1827 DestroyIcon(PrintStructures->hPortraitIcon);
1828 DestroyIcon(PrintStructures->hLandscapeIcon);
1829 if(PrintStructures->hwndUpDown)
1830 DestroyWindow(PrintStructures->hwndUpDown);
1831 return FALSE;
1832 }
1833 return res;
1834 }
1835
1836 static INT_PTR CALLBACK PrintDlgProcW(HWND hDlg, UINT uMsg, WPARAM wParam,
1837 LPARAM lParam)
1838 {
1839 PRINT_PTRW* PrintStructures;
1840 INT_PTR res = FALSE;
1841
1842 if (uMsg!=WM_INITDIALOG) {
1843 PrintStructures = GetPropW(hDlg, printdlg_prop);
1844 if (!PrintStructures)
1845 return FALSE;
1846 } else {
1847 PrintStructures = (PRINT_PTRW*) lParam;
1848 SetPropW(hDlg, printdlg_prop, PrintStructures);
1849 if(!check_printer_setup(hDlg))
1850 {
1851 EndDialog(hDlg,FALSE);
1852 return FALSE;
1853 }
1854 res = PRINTDLG_WMInitDialogW(hDlg, wParam, PrintStructures);
1855
1856 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1857 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg, uMsg, wParam, (LPARAM)PrintStructures->lpPrintDlg);
1858 return res;
1859 }
1860
1861 if(PrintStructures->lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1862 res = PrintStructures->lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam, lParam);
1863 if(res) return res;
1864 }
1865
1866 switch (uMsg) {
1867 case WM_COMMAND:
1868 return PRINTDLG_WMCommandW(hDlg, wParam, lParam, PrintStructures);
1869
1870 case WM_DESTROY:
1871 DestroyIcon(PrintStructures->hCollateIcon);
1872 DestroyIcon(PrintStructures->hNoCollateIcon);
1873 DestroyIcon(PrintStructures->hPortraitIcon);
1874 DestroyIcon(PrintStructures->hLandscapeIcon);
1875 if(PrintStructures->hwndUpDown)
1876 DestroyWindow(PrintStructures->hwndUpDown);
1877 return FALSE;
1878 }
1879 return res;
1880 }
1881
1882 /************************************************************
1883 *
1884 * PRINTDLG_GetDlgTemplate
1885 *
1886 */
1887 static HGLOBAL PRINTDLG_GetDlgTemplateA(const PRINTDLGA *lppd)
1888 {
1889 HRSRC hResInfo;
1890 HGLOBAL hDlgTmpl;
1891
1892 if (lppd->Flags & PD_PRINTSETUP) {
1893 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1894 hDlgTmpl = lppd->hSetupTemplate;
1895 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1896 hResInfo = FindResourceA(lppd->hInstance,
1897 lppd->lpSetupTemplateName, (LPSTR)RT_DIALOG);
1898 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1899 } else {
1900 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1901 (LPSTR)RT_DIALOG);
1902 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1903 }
1904 } else {
1905 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1906 hDlgTmpl = lppd->hPrintTemplate;
1907 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1908 hResInfo = FindResourceA(lppd->hInstance,
1909 lppd->lpPrintTemplateName,
1910 (LPSTR)RT_DIALOG);
1911 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1912 } else {
1913 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1914 (LPSTR)RT_DIALOG);
1915 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1916 }
1917 }
1918 return hDlgTmpl;
1919 }
1920
1921 static HGLOBAL PRINTDLG_GetDlgTemplateW(const PRINTDLGW *lppd)
1922 {
1923 HRSRC hResInfo;
1924 HGLOBAL hDlgTmpl;
1925 static const WCHAR xpsetup[] = { 'P','R','I','N','T','3','2','_','S','E','T','U','P',0};
1926 static const WCHAR xprint[] = { 'P','R','I','N','T','3','2',0};
1927
1928 if (lppd->Flags & PD_PRINTSETUP) {
1929 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1930 hDlgTmpl = lppd->hSetupTemplate;
1931 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1932 hResInfo = FindResourceW(lppd->hInstance,
1933 lppd->lpSetupTemplateName, (LPWSTR)RT_DIALOG);
1934 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1935 } else {
1936 hResInfo = FindResourceW(COMDLG32_hInstance, xpsetup, (LPWSTR)RT_DIALOG);
1937 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1938 }
1939 } else {
1940 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1941 hDlgTmpl = lppd->hPrintTemplate;
1942 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1943 hResInfo = FindResourceW(lppd->hInstance,
1944 lppd->lpPrintTemplateName,
1945 (LPWSTR)RT_DIALOG);
1946 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1947 } else {
1948 hResInfo = FindResourceW(COMDLG32_hInstance, xprint, (LPWSTR)RT_DIALOG);
1949 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1950 }
1951 }
1952 return hDlgTmpl;
1953 }
1954
1955 /***********************************************************************
1956 *
1957 * PRINTDLG_CreateDC
1958 *
1959 */
1960 static BOOL PRINTDLG_CreateDCA(LPPRINTDLGA lppd)
1961 {
1962 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1963 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1964
1965 if(lppd->Flags & PD_RETURNDC) {
1966 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1967 (char*)pdn + pdn->wDeviceOffset,
1968 (char*)pdn + pdn->wOutputOffset,
1969 pdm );
1970 } else if(lppd->Flags & PD_RETURNIC) {
1971 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1972 (char*)pdn + pdn->wDeviceOffset,
1973 (char*)pdn + pdn->wOutputOffset,
1974 pdm );
1975 }
1976 GlobalUnlock(lppd->hDevNames);
1977 GlobalUnlock(lppd->hDevMode);
1978 return lppd->hDC ? TRUE : FALSE;
1979 }
1980
1981 static BOOL PRINTDLG_CreateDCW(LPPRINTDLGW lppd)
1982 {
1983 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1984 DEVMODEW *pdm = GlobalLock(lppd->hDevMode);
1985
1986 if(lppd->Flags & PD_RETURNDC) {
1987 lppd->hDC = CreateDCW((WCHAR*)pdn + pdn->wDriverOffset,
1988 (WCHAR*)pdn + pdn->wDeviceOffset,
1989 (WCHAR*)pdn + pdn->wOutputOffset,
1990 pdm );
1991 } else if(lppd->Flags & PD_RETURNIC) {
1992 lppd->hDC = CreateICW((WCHAR*)pdn + pdn->wDriverOffset,
1993 (WCHAR*)pdn + pdn->wDeviceOffset,
1994 (WCHAR*)pdn + pdn->wOutputOffset,
1995 pdm );
1996 }
1997 GlobalUnlock(lppd->hDevNames);
1998 GlobalUnlock(lppd->hDevMode);
1999 return lppd->hDC ? TRUE : FALSE;
2000 }
2001
2002 /***********************************************************************
2003 * PrintDlgA (COMDLG32.@)
2004 *
2005 * Displays the PRINT dialog box, which enables the user to specify
2006 * specific properties of the print job.
2007 *
2008 * PARAMS
2009 * lppd [IO] ptr to PRINTDLG32 struct
2010 *
2011 * RETURNS
2012 * nonzero if the user pressed the OK button
2013 * zero if the user cancelled the window or an error occurred
2014 *
2015 * BUGS
2016 * PrintDlg:
2017 * * The Collate Icons do not display, even though they are in the code.
2018 * * The Properties Button(s) should call DocumentPropertiesA().
2019 */
2020
2021 BOOL WINAPI PrintDlgA(LPPRINTDLGA lppd)
2022 {
2023 BOOL bRet = FALSE;
2024 LPVOID ptr;
2025 HINSTANCE hInst;
2026
2027 if (!lppd)
2028 {
2029 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2030 return FALSE;
2031 }
2032
2033 hInst = (HINSTANCE)GetWindowLongPtrA( lppd->hwndOwner, GWLP_HINSTANCE );
2034 if(TRACE_ON(commdlg)) {
2035 char flagstr[1000] = "";
2036 const struct pd_flags *pflag = pd_flags;
2037 for( ; pflag->name; pflag++) {
2038 if(lppd->Flags & pflag->flag)
2039 strcat(flagstr, pflag->name);
2040 }
2041 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2042 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2043 "flags %08x (%s)\n",
2044 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2045 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2046 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2047 }
2048
2049 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
2050 WARN("structure size failure !!!\n");
2051 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2052 return FALSE;
2053 }
2054
2055 if(lppd->Flags & PD_RETURNDEFAULT) {
2056 PRINTER_INFO_2A *pbuf;
2057 DRIVER_INFO_3A *dbuf;
2058 HANDLE hprn;
2059 DWORD needed;
2060
2061 if(lppd->hDevMode || lppd->hDevNames) {
2062 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2063 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2064 return FALSE;
2065 }
2066 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2067 WARN("Can't find default printer\n");
2068 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2069 return FALSE;
2070 }
2071
2072 GetPrinterA(hprn, 2, NULL, 0, &needed);
2073 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2074 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2075
2076 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
2077 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2078 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2079 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2080 GetLastError(),pbuf->pPrinterName);
2081 HeapFree(GetProcessHeap(), 0, dbuf);
2082 HeapFree(GetProcessHeap(), 0, pbuf);
2083 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2084 return FALSE;
2085 }
2086 ClosePrinter(hprn);
2087
2088 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2089 dbuf->pDriverPath,
2090 pbuf->pPrinterName,
2091 pbuf->pPortName);
2092 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2093 pbuf->pDevMode->dmDriverExtra);
2094 ptr = GlobalLock(lppd->hDevMode);
2095 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2096 pbuf->pDevMode->dmDriverExtra);
2097 GlobalUnlock(lppd->hDevMode);
2098 HeapFree(GetProcessHeap(), 0, pbuf);
2099 HeapFree(GetProcessHeap(), 0, dbuf);
2100 bRet = TRUE;
2101 } else {
2102 HGLOBAL hDlgTmpl;
2103 PRINT_PTRA *PrintStructures;
2104
2105 /* load Dialog resources,
2106 * depending on Flags indicates Print32 or Print32_setup dialog
2107 */
2108 hDlgTmpl = PRINTDLG_GetDlgTemplateA(lppd);
2109 if (!hDlgTmpl) {
2110 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2111 return FALSE;
2112 }
2113 ptr = LockResource( hDlgTmpl );
2114 if (!ptr) {
2115 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2116 return FALSE;
2117 }
2118
2119 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2120 sizeof(PRINT_PTRA));
2121 PrintStructures->lpPrintDlg = lppd;
2122
2123 /* and create & process the dialog .
2124 * -1 is failure, 0 is broken hwnd, everything else is ok.
2125 */
2126 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
2127 PrintDlgProcA,
2128 (LPARAM)PrintStructures));
2129
2130 if(bRet) {
2131 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2132 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
2133 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
2134
2135 if (lppd->hDevMode == 0) {
2136 TRACE(" No hDevMode yet... Need to create my own\n");
2137 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2138 lpdm->dmSize + lpdm->dmDriverExtra);
2139 } else {
2140 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2141 lpdm->dmSize + lpdm->dmDriverExtra,
2142 GMEM_MOVEABLE);
2143 }
2144 lpdmReturn = GlobalLock(lppd->hDevMode);
2145 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2146
2147 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
2148 di->pDriverPath,
2149 pi->pPrinterName,
2150 pi->pPortName
2151 );
2152 GlobalUnlock(lppd->hDevMode);
2153 }
2154 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2155 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2156 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2157 HeapFree(GetProcessHeap(), 0, PrintStructures);
2158 }
2159 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2160 bRet = PRINTDLG_CreateDCA(lppd);
2161
2162 TRACE("exit! (%d)\n", bRet);
2163 return bRet;
2164 }
2165
2166 /***********************************************************************
2167 * PrintDlgW (COMDLG32.@)
2168 *
2169 * See PrintDlgA.
2170 */
2171 BOOL WINAPI PrintDlgW(LPPRINTDLGW lppd)
2172 {
2173 BOOL bRet = FALSE;
2174 LPVOID ptr;
2175 HINSTANCE hInst;
2176
2177 if (!lppd)
2178 {
2179 COMDLG32_SetCommDlgExtendedError(CDERR_INITIALIZATION);
2180 return FALSE;
2181 }
2182
2183 hInst = (HINSTANCE)GetWindowLongPtrW( lppd->hwndOwner, GWLP_HINSTANCE );
2184 if(TRACE_ON(commdlg)) {
2185 char flagstr[1000] = "";
2186 const struct pd_flags *pflag = pd_flags;
2187 for( ; pflag->name; pflag++) {
2188 if(lppd->Flags & pflag->flag)
2189 strcat(flagstr, pflag->name);
2190 }
2191 TRACE("(%p): hwndOwner = %p, hDevMode = %p, hDevNames = %p\n"
2192 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %p\n"
2193 "flags %08x (%s)\n",
2194 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
2195 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
2196 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
2197 }
2198
2199 if(lppd->lStructSize != sizeof(PRINTDLGW)) {
2200 WARN("structure size failure !!!\n");
2201 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
2202 return FALSE;
2203 }
2204
2205 if(lppd->Flags & PD_RETURNDEFAULT) {
2206 PRINTER_INFO_2W *pbuf;
2207 DRIVER_INFO_3W *dbuf;
2208 HANDLE hprn;
2209 DWORD needed;
2210
2211 if(lppd->hDevMode || lppd->hDevNames) {
2212 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
2213 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2214 return FALSE;
2215 }
2216 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
2217 WARN("Can't find default printer\n");
2218 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
2219 return FALSE;
2220 }
2221
2222 GetPrinterW(hprn, 2, NULL, 0, &needed);
2223 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
2224 GetPrinterW(hprn, 2, (LPBYTE)pbuf, needed, &needed);
2225
2226 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2227 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
2228 if (!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
2229 ERR("GetPrinterDriverA failed, le %d, fix your config for printer %s!\n",
2230 GetLastError(),debugstr_w(pbuf->pPrinterName));
2231 HeapFree(GetProcessHeap(), 0, dbuf);
2232 HeapFree(GetProcessHeap(), 0, pbuf);
2233 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
2234 return FALSE;
2235 }
2236 ClosePrinter(hprn);
2237
2238 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2239 dbuf->pDriverPath,
2240 pbuf->pPrinterName,
2241 pbuf->pPortName);
2242 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
2243 pbuf->pDevMode->dmDriverExtra);
2244 ptr = GlobalLock(lppd->hDevMode);
2245 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
2246 pbuf->pDevMode->dmDriverExtra);
2247 GlobalUnlock(lppd->hDevMode);
2248 HeapFree(GetProcessHeap(), 0, pbuf);
2249 HeapFree(GetProcessHeap(), 0, dbuf);
2250 bRet = TRUE;
2251 } else {
2252 HGLOBAL hDlgTmpl;
2253 PRINT_PTRW *PrintStructures;
2254
2255 /* load Dialog resources,
2256 * depending on Flags indicates Print32 or Print32_setup dialog
2257 */
2258 hDlgTmpl = PRINTDLG_GetDlgTemplateW(lppd);
2259 if (!hDlgTmpl) {
2260 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2261 return FALSE;
2262 }
2263 ptr = LockResource( hDlgTmpl );
2264 if (!ptr) {
2265 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2266 return FALSE;
2267 }
2268
2269 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2270 sizeof(PRINT_PTRW));
2271 PrintStructures->lpPrintDlg = lppd;
2272
2273 /* and create & process the dialog .
2274 * -1 is failure, 0 is broken hwnd, everything else is ok.
2275 */
2276 bRet = (0<DialogBoxIndirectParamW(hInst, ptr, lppd->hwndOwner,
2277 PrintDlgProcW,
2278 (LPARAM)PrintStructures));
2279
2280 if(bRet) {
2281 DEVMODEW *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
2282 PRINTER_INFO_2W *pi = PrintStructures->lpPrinterInfo;
2283 DRIVER_INFO_3W *di = PrintStructures->lpDriverInfo;
2284
2285 if (lppd->hDevMode == 0) {
2286 TRACE(" No hDevMode yet... Need to create my own\n");
2287 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
2288 lpdm->dmSize + lpdm->dmDriverExtra);
2289 } else {
2290 WORD locks;
2291 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
2292 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
2293 while(locks--) {
2294 GlobalUnlock(lppd->hDevMode);
2295 TRACE("Now got %d locks\n", locks);
2296 }
2297 }
2298 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
2299 lpdm->dmSize + lpdm->dmDriverExtra,
2300 GMEM_MOVEABLE);
2301 }
2302 lpdmReturn = GlobalLock(lppd->hDevMode);
2303 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
2304
2305 if (lppd->hDevNames != 0) {
2306 WORD locks;
2307 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
2308 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
2309 while(locks--)
2310 GlobalUnlock(lppd->hDevNames);
2311 }
2312 }
2313 PRINTDLG_CreateDevNamesW(&(lppd->hDevNames),
2314 di->pDriverPath,
2315 pi->pPrinterName,
2316 pi->pPortName
2317 );
2318 GlobalUnlock(lppd->hDevMode);
2319 }
2320 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
2321 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
2322 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
2323 HeapFree(GetProcessHeap(), 0, PrintStructures);
2324 }
2325 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
2326 bRet = PRINTDLG_CreateDCW(lppd);
2327
2328 TRACE("exit! (%d)\n", bRet);
2329 return bRet;
2330 }
2331
2332 /***********************************************************************
2333 *
2334 * PageSetupDlg
2335 * rad1 - portrait
2336 * rad2 - landscape
2337 * cmb1 - printer select (not in standard dialog template)
2338 * cmb2 - paper size
2339 * cmb3 - source (tray?)
2340 * edt4 - border left
2341 * edt5 - border top
2342 * edt6 - border right
2343 * edt7 - border bottom
2344 * psh3 - "Printer..."
2345 */
2346
2347 typedef struct
2348 {
2349 BOOL unicode;
2350 union
2351 {
2352 LPPAGESETUPDLGA dlga;
2353 LPPAGESETUPDLGW dlgw;
2354 } u;
2355 HWND hDlg; /* Page Setup dialog handle */
2356 RECT rtDrawRect; /* Drawing rect for page */
2357 } pagesetup_data;
2358
2359 static inline DWORD pagesetup_get_flags(const pagesetup_data *data)
2360 {
2361 return data->u.dlgw->Flags;
2362 }
2363
2364 static inline BOOL is_metric(const pagesetup_data *data)
2365 {
2366 return pagesetup_get_flags(data) & PSD_INHUNDREDTHSOFMILLIMETERS;
2367 }
2368
2369 static inline LONG tenths_mm_to_size(const pagesetup_data *data, LONG size)
2370 {
2371 if (is_metric(data))
2372 return 10 * size;
2373 else
2374 return 10 * size * 100 / 254;
2375 }
2376
2377 static inline LONG thousandths_inch_to_size(const pagesetup_data *data, LONG size)
2378 {
2379 if (is_metric(data))
2380 return size * 254 / 100;
2381 else
2382 return size;
2383 }
2384
2385 static WCHAR get_decimal_sep(void)
2386 {
2387 static WCHAR sep;
2388
2389 if(!sep)
2390 {
2391 WCHAR buf[2] = {'.',0};
2392 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buf, sizeof(buf) / sizeof(buf[0]));
2393 sep = buf[0];
2394 }
2395 return sep;
2396 }
2397
2398 static void size2str(const pagesetup_data *data, DWORD size, LPWSTR strout)
2399 {
2400 WCHAR integer_fmt[] = {'%','d',0};
2401 WCHAR hundredths_fmt[] = {'%','d','%','c','%','0','2','d',0};
2402 WCHAR thousandths_fmt[] = {'%','d','%','c','%','0','3','d',0};
2403
2404 /* FIXME use LOCALE_SDECIMAL when the edit parsing code can cope */
2405
2406 if (is_metric(data))
2407 {
2408 if(size % 100)
2409 wsprintfW(strout, hundredths_fmt, size / 100, get_decimal_sep(), size % 100);
2410 else
2411 wsprintfW(strout, integer_fmt, size / 100);
2412 }
2413 else
2414 {
2415 if(size % 1000)
2416 wsprintfW(strout, thousandths_fmt, size / 1000, get_decimal_sep(), size % 1000);
2417 else
2418 wsprintfW(strout, integer_fmt, size / 1000);
2419
2420 }
2421 }
2422
2423 static inline BOOL is_default_metric(void)
2424 {
2425 DWORD system;
2426 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
2427 (LPWSTR)&system, sizeof(system));
2428 return system == 0;
2429 }
2430
2431 /**********************************************
2432 * rotate_rect
2433 * Cyclically permute the four members of rc
2434 * If sense is TRUE l -> t -> r -> b
2435 * otherwise l <- t <- r <- b
2436 */
2437 static inline void rotate_rect(RECT *rc, BOOL sense)
2438 {
2439 INT tmp;
2440 if(sense)
2441 {
2442 tmp = rc->bottom;
2443 rc->bottom = rc->right;
2444 rc->right = rc->top;
2445 rc->top = rc->left;
2446 rc->left = tmp;
2447 }
2448 else
2449 {
2450 tmp = rc->left;
2451 rc->left = rc->top;
2452 rc->top = rc->right;
2453 rc->right = rc->bottom;
2454 rc->bottom = tmp;
2455 }
2456 }
2457
2458 static void pagesetup_set_orientation(pagesetup_data *data, WORD orient)
2459 {
2460 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2461
2462 assert(orient == DMORIENT_PORTRAIT || orient == DMORIENT_LANDSCAPE);
2463
2464 if(data->unicode)
2465 dm->u1.s1.dmOrientation = orient;
2466 else
2467 {
2468 DEVMODEA *dmA = (DEVMODEA *)dm;
2469 dmA->u1.s1.dmOrientation = orient;
2470 }
2471 GlobalUnlock(data->u.dlgw->hDevMode);
2472 }
2473
2474 static WORD pagesetup_get_orientation(const pagesetup_data *data)
2475 {
2476 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2477 WORD orient;
2478
2479 if(data->unicode)
2480 orient = dm->u1.s1.dmOrientation;
2481 else
2482 {
2483 DEVMODEA *dmA = (DEVMODEA *)dm;
2484 orient = dmA->u1.s1.dmOrientation;
2485 }
2486 GlobalUnlock(data->u.dlgw->hDevMode);
2487 return orient;
2488 }
2489
2490 static void pagesetup_set_papersize(pagesetup_data *data, WORD paper)
2491 {
2492 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2493
2494 if(data->unicode)
2495 dm->u1.s1.dmPaperSize = paper;
2496 else
2497 {
2498 DEVMODEA *dmA = (DEVMODEA *)dm;
2499 dmA->u1.s1.dmPaperSize = paper;
2500 }
2501 GlobalUnlock(data->u.dlgw->hDevMode);
2502 }
2503
2504 static WORD pagesetup_get_papersize(const pagesetup_data *data)
2505 {
2506 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2507 WORD paper;
2508
2509 if(data->unicode)
2510 paper = dm->u1.s1.dmPaperSize;
2511 else
2512 {
2513 DEVMODEA *dmA = (DEVMODEA *)dm;
2514 paper = dmA->u1.s1.dmPaperSize;
2515 }
2516 GlobalUnlock(data->u.dlgw->hDevMode);
2517 return paper;
2518 }
2519
2520 static void pagesetup_set_defaultsource(pagesetup_data *data, WORD source)
2521 {
2522 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2523
2524 if(data->unicode)
2525 dm->u1.s1.dmDefaultSource = source;
2526 else
2527 {
2528 DEVMODEA *dmA = (DEVMODEA *)dm;
2529 dmA->u1.s1.dmDefaultSource = source;
2530 }
2531 GlobalUnlock(data->u.dlgw->hDevMode);
2532 }
2533
2534 typedef enum
2535 {
2536 devnames_driver_name,
2537 devnames_device_name,
2538 devnames_output_name
2539 } devnames_name;
2540
2541
2542 static inline WORD get_devname_offset(const DEVNAMES *dn, devnames_name which)
2543 {
2544 switch(which)
2545 {
2546 case devnames_driver_name: return dn->wDriverOffset;
2547 case devnames_device_name: return dn->wDeviceOffset;
2548 case devnames_output_name: return dn->wOutputOffset;
2549 }
2550 ERR("Souldn't be here\n");
2551 return 0;
2552 }
2553
2554 static WCHAR *pagesetup_get_a_devname(const pagesetup_data *data, devnames_name which)
2555 {
2556 DEVNAMES *dn;
2557 WCHAR *name;
2558
2559 dn = GlobalLock(data->u.dlgw->hDevNames);
2560 if(data->unicode)
2561 name = strdupW((WCHAR *)dn + get_devname_offset(dn, which));
2562 else
2563 {
2564 int len = MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, NULL, 0);
2565 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2566 MultiByteToWideChar(CP_ACP, 0, (char*)dn + get_devname_offset(dn, which), -1, name, len);
2567 }
2568 GlobalUnlock(data->u.dlgw->hDevNames);
2569 return name;
2570 }
2571
2572 static WCHAR *pagesetup_get_drvname(const pagesetup_data *data)
2573 {
2574 return pagesetup_get_a_devname(data, devnames_driver_name);
2575 }
2576
2577 static WCHAR *pagesetup_get_devname(const pagesetup_data *data)
2578 {
2579 return pagesetup_get_a_devname(data, devnames_device_name);
2580 }
2581
2582 static WCHAR *pagesetup_get_portname(const pagesetup_data *data)
2583 {
2584 return pagesetup_get_a_devname(data, devnames_output_name);
2585 }
2586
2587 static void pagesetup_release_a_devname(const pagesetup_data *data, WCHAR *name)
2588 {
2589 HeapFree(GetProcessHeap(), 0, name);
2590 }
2591
2592 static void pagesetup_set_devnames(pagesetup_data *data, LPCWSTR drv, LPCWSTR devname, LPCWSTR port)
2593 {
2594 DEVNAMES *dn;
2595 WCHAR def[256];
2596 DWORD len = sizeof(DEVNAMES), drv_len, dev_len, port_len;
2597
2598 if(data->unicode)
2599 {
2600 drv_len = (strlenW(drv) + 1) * sizeof(WCHAR);
2601 dev_len = (strlenW(devname) + 1) * sizeof(WCHAR);
2602 port_len = (strlenW(port) + 1) * sizeof(WCHAR);
2603 }
2604 else
2605 {
2606 drv_len = WideCharToMultiByte(CP_ACP, 0, drv, -1, NULL, 0, NULL, NULL);
2607 dev_len = WideCharToMultiByte(CP_ACP, 0, devname, -1, NULL, 0, NULL, NULL);
2608 port_len = WideCharToMultiByte(CP_ACP, 0, port, -1, NULL, 0, NULL, NULL);
2609 }
2610 len += drv_len + dev_len + port_len;
2611
2612 if(data->u.dlgw->hDevNames)
2613 data->u.dlgw->hDevNames = GlobalReAlloc(data->u.dlgw->hDevNames, len, GMEM_MOVEABLE);
2614 else
2615 data->u.dlgw->hDevNames = GlobalAlloc(GMEM_MOVEABLE, len);
2616
2617 dn = GlobalLock(data->u.dlgw->hDevNames);
2618
2619 if(data->unicode)
2620 {
2621 WCHAR *ptr = (WCHAR *)(dn + 1);
2622 len = sizeof(DEVNAMES) / sizeof(WCHAR);
2623 dn->wDriverOffset = len;
2624 strcpyW(ptr, drv);
2625 ptr += drv_len / sizeof(WCHAR);
2626 len += drv_len / sizeof(WCHAR);
2627 dn->wDeviceOffset = len;
2628 strcpyW(ptr, devname);
2629 ptr += dev_len / sizeof(WCHAR);
2630 len += dev_len / sizeof(WCHAR);
2631 dn->wOutputOffset = len;
2632 strcpyW(ptr, port);
2633 }
2634 else
2635 {
2636 char *ptr = (char *)(dn + 1);
2637 len = sizeof(DEVNAMES);
2638 dn->wDriverOffset = len;
2639 WideCharToMultiByte(CP_ACP, 0, drv, -1, ptr, drv_len, NULL, NULL);
2640 ptr += drv_len;
2641 len += drv_len;
2642 dn->wDeviceOffset = len;
2643 WideCharToMultiByte(CP_ACP, 0, devname, -1, ptr, dev_len, NULL, NULL);
2644 ptr += dev_len;
2645 len += dev_len;
2646 dn->wOutputOffset = len;
2647 WideCharToMultiByte(CP_ACP, 0, port, -1, ptr, port_len, NULL, NULL);
2648 }
2649
2650 dn->wDefault = 0;
2651 len = sizeof(def) / sizeof(def[0]);
2652 GetDefaultPrinterW(def, &len);
2653 if(!lstrcmpW(def, devname))
2654 dn->wDefault = 1;
2655
2656 GlobalUnlock(data->u.dlgw->hDevNames);
2657 }
2658
2659 static DEVMODEW *pagesetup_get_devmode(const pagesetup_data *data)
2660 {
2661 DEVMODEW *dm = GlobalLock(data->u.dlgw->hDevMode);
2662 DEVMODEW *ret;
2663
2664 if(data->unicode)
2665 {
2666 /* We make a copy even in the unicode case because the ptr
2667 may get passed back to us in pagesetup_set_devmode. */
2668 ret = HeapAlloc(GetProcessHeap(), 0, dm->dmSize + dm->dmDriverExtra);
2669 memcpy(ret, dm, dm->dmSize + dm->dmDriverExtra);
2670 }
2671 else
2672 ret = GdiConvertToDevmodeW((DEVMODEA *)dm);
2673
2674 GlobalUnlock(data->u.dlgw->hDevMode);
2675 return ret;
2676 }
2677
2678 static void pagesetup_release_devmode(const pagesetup_data *data, DEVMODEW *dm)
2679 {
2680 HeapFree(GetProcessHeap(), 0, dm);
2681 }
2682
2683 static void pagesetup_set_devmode(pagesetup_data *data, DEVMODEW *dm)
2684 {
2685 DEVMODEA *dmA = NULL;
2686 void *src, *dst;
2687 DWORD size;
2688
2689 if(data->unicode)
2690 {
2691 size = dm->dmSize + dm->dmDriverExtra;
2692 src = dm;
2693 }
2694 else
2695 {
2696 dmA = convert_to_devmodeA(dm);
2697 size = dmA->dmSize + dmA->dmDriverExtra;
2698 src = dmA;
2699 }
2700
2701 if(data->u.dlgw->hDevMode)
2702 data->u.dlgw->hDevMode = GlobalReAlloc(data->u.dlgw->hDevMode, size,
2703 GMEM_MOVEABLE);
2704 else
2705 data->u.dlgw->hDevMode = GlobalAlloc(GMEM_MOVEABLE, size);
2706
2707 dst = GlobalLock(data->u.dlgw->hDevMode);
2708 memcpy(dst, src, size);
2709 GlobalUnlock(data->u.dlgw->hDevMode);
2710 HeapFree(GetProcessHeap(), 0, dmA);
2711 }
2712
2713 static inline POINT *pagesetup_get_papersize_pt(const pagesetup_data *data)
2714 {
2715 return &data->u.dlgw->ptPaperSize;
2716 }
2717
2718 static inline RECT *pagesetup_get_margin_rect(const pagesetup_data *data)
2719 {
2720 return &data->u.dlgw->rtMargin;
2721 }
2722
2723 typedef enum
2724 {
2725 page_setup_hook,
2726 page_paint_hook
2727 } hook_type;
2728
2729 static inline LPPAGESETUPHOOK pagesetup_get_hook(const pagesetup_data *data, hook_type which)
2730 {
2731 switch(which)
2732 {
2733 case page_setup_hook: return data->u.dlgw->lpfnPageSetupHook;
2734 case page_paint_hook: return data->u.dlgw->lpfnPagePaintHook;
2735 }
2736 return NULL;
2737 }
2738
2739 /* This should only be used in calls to hook procs so we return the ptr
2740 already cast to LPARAM */
2741 static inline LPARAM pagesetup_get_dlg_struct(const pagesetup_data *data)
2742 {
2743 return (LPARAM)data->u.dlgw;
2744 }
2745
2746 static inline void swap_point(POINT *pt)
2747 {
2748 LONG tmp = pt->x;
2749 pt->x = pt->y;
2750 pt->y = tmp;
2751 }
2752
2753 static BOOL pagesetup_update_papersize(pagesetup_data *data)
2754 {
2755 DEVMODEW *dm;
2756 LPWSTR devname, portname;
2757 int i, num;
2758 WORD *words = NULL, paperword;
2759 POINT *points = NULL;
2760 BOOL retval = FALSE;
2761
2762 dm = pagesetup_get_devmode(data);
2763 devname = pagesetup_get_devname(data);
2764 portname = pagesetup_get_portname(data);
2765
2766 num = DeviceCapabilitiesW(devname, portname, DC_PAPERS, NULL, dm);
2767 if (num <= 0)
2768 {
2769 FIXME("No papernames found for %s/%s\n", debugstr_w(devname), debugstr_w(portname));
2770 goto end;
2771 }
2772
2773 words = HeapAlloc(GetProcessHeap(), 0, num * sizeof(WORD));
2774 points = HeapAlloc(GetProcessHeap(), 0, num * sizeof(POINT));
2775
2776 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERS, (LPWSTR)words, dm))
2777 {
2778 FIXME("Number of returned words is not %d\n", num);
2779 goto end;
2780 }
2781
2782 if (num != DeviceCapabilitiesW(devname, portname, DC_PAPERSIZE, (LPWSTR)points, dm))
2783 {
2784 FIXME("Number of returned sizes is not %d\n", num);
2785 goto end;
2786 }
2787
2788 paperword = pagesetup_get_papersize(data);
2789
2790 for (i = 0; i < num; i++)
2791 if (words[i] == paperword)
2792 break;
2793
2794 if (i == num)
2795 {
2796 FIXME("Papersize %d not found in list?\n", paperword);
2797 goto end;
2798 }
2799
2800 /* this is _10ths_ of a millimeter */
2801 pagesetup_get_papersize_pt(data)->x = tenths_mm_to_size(data, points[i].x);
2802 pagesetup_get_papersize_pt(data)->y = tenths_mm_to_size(data, points[i].y);
2803
2804 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2805 swap_point(pagesetup_get_papersize_pt(data));
2806
2807 retval = TRUE;
2808
2809 end:
2810 HeapFree(GetProcessHeap(), 0, words);
2811 HeapFree(GetProcessHeap(), 0, points);
2812 pagesetup_release_a_devname(data, portname);
2813 pagesetup_release_a_devname(data, devname);
2814 pagesetup_release_devmode(data, dm);
2815
2816 return retval;
2817 }
2818
2819 /**********************************************************************************************
2820 * pagesetup_change_printer
2821 *
2822 * Redefines hDevMode and hDevNames HANDLES and initialises it.
2823 *
2824 */
2825 static BOOL pagesetup_change_printer(LPWSTR name, pagesetup_data *data)
2826 {
2827 HANDLE hprn;
2828 DWORD needed;
2829 PRINTER_INFO_2W *prn_info = NULL;
2830 DRIVER_INFO_3W *drv_info = NULL;
2831 DEVMODEW *dm = NULL;
2832 BOOL retval = FALSE;
2833
2834 if(!OpenPrinterW(name, &hprn, NULL))
2835 {
2836 ERR("Can't open printer %s\n", debugstr_w(name));
2837 goto end;
2838 }
2839
2840 GetPrinterW(hprn, 2, NULL, 0, &needed);
2841 prn_info = HeapAlloc(GetProcessHeap(), 0, needed);
2842 GetPrinterW(hprn, 2, (LPBYTE)prn_info, needed, &needed);
2843 GetPrinterDriverW(hprn, NULL, 3, NULL, 0, &needed);
2844 drv_info = HeapAlloc(GetProcessHeap(), 0, needed);
2845 if(!GetPrinterDriverW(hprn, NULL, 3, (LPBYTE)drv_info, needed, &needed))
2846 {
2847 ERR("GetPrinterDriverA failed for %s, fix your config!\n", debugstr_w(prn_info->pPrinterName));
2848 goto end;
2849 }
2850 ClosePrinter(hprn);
2851
2852 needed = DocumentPropertiesW(0, 0, name, NULL, NULL, 0);
2853 if(needed == -1)
2854 {
2855 ERR("DocumentProperties fails on %s\n", debugstr_w(name));
2856 goto end;
2857 }
2858
2859 dm = HeapAlloc(GetProcessHeap(), 0, needed);
2860 DocumentPropertiesW(0, 0, name, dm, NULL, DM_OUT_BUFFER);
2861
2862 pagesetup_set_devmode(data, dm);
2863 pagesetup_set_devnames(data, drv_info->pDriverPath, prn_info->pPrinterName,
2864 prn_info->pPortName);
2865
2866 retval = TRUE;
2867 end:
2868 HeapFree(GetProcessHeap(), 0, dm);
2869 HeapFree(GetProcessHeap(), 0, prn_info);
2870 HeapFree(GetProcessHeap(), 0, drv_info);
2871 return retval;
2872 }
2873
2874 /****************************************************************************************
2875 * pagesetup_init_combos
2876 *
2877 * Fills Printers, Paper and Source combos
2878 *
2879 */
2880 static void pagesetup_init_combos(HWND hDlg, pagesetup_data *data)
2881 {
2882 DEVMODEW *dm;
2883 LPWSTR devname, portname;
2884
2885 dm = pagesetup_get_devmode(data);
2886 devname = pagesetup_get_devname(data);
2887 portname = pagesetup_get_portname(data);
2888
2889 PRINTDLG_SetUpPrinterListComboW(hDlg, cmb1, devname);
2890 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb2, devname, portname, dm);
2891 PRINTDLG_SetUpPaperComboBoxW(hDlg, cmb3, devname, portname, dm);
2892
2893 pagesetup_release_a_devname(data, portname);
2894 pagesetup_release_a_devname(data, devname);
2895 pagesetup_release_devmode(data, dm);
2896 }
2897
2898
2899 /****************************************************************************************
2900 * pagesetup_change_printer_dialog
2901 *
2902 * Pops up another dialog that lets the user pick another printer.
2903 *
2904 * For now we display the PrintDlg, this should display a striped down version of it.
2905 */
2906 static void pagesetup_change_printer_dialog(HWND hDlg, pagesetup_data *data)
2907 {
2908 PRINTDLGW prnt;
2909 LPWSTR drvname, devname, portname;
2910 DEVMODEW *tmp_dm, *dm;
2911
2912 memset(&prnt, 0, sizeof(prnt));
2913 prnt.lStructSize = sizeof(prnt);
2914 prnt.Flags = 0;
2915 prnt.hwndOwner = hDlg;
2916
2917 drvname = pagesetup_get_drvname(data);
2918 devname = pagesetup_get_devname(data);
2919 portname = pagesetup_get_portname(data);
2920 prnt.hDevNames = 0;
2921 PRINTDLG_CreateDevNamesW(&prnt.hDevNames, drvname, devname, portname);
2922 pagesetup_release_a_devname(data, portname);
2923 pagesetup_release_a_devname(data, devname);
2924 pagesetup_release_a_devname(data, drvname);
2925
2926 tmp_dm = pagesetup_get_devmode(data);
2927 prnt.hDevMode = GlobalAlloc(GMEM_MOVEABLE, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2928 dm = GlobalLock(prnt.hDevMode);
2929 memcpy(dm, tmp_dm, tmp_dm->dmSize + tmp_dm->dmDriverExtra);
2930 GlobalUnlock(prnt.hDevMode);
2931 pagesetup_release_devmode(data, tmp_dm);
2932
2933 if (PrintDlgW(&prnt))
2934 {
2935 DEVMODEW *dm = GlobalLock(prnt.hDevMode);
2936 DEVNAMES *dn = GlobalLock(prnt.hDevNames);
2937
2938 pagesetup_set_devnames(data, (WCHAR*)dn + dn->wDriverOffset,
2939 (WCHAR*)dn + dn->wDeviceOffset, (WCHAR *)dn + dn->wOutputOffset);
2940 pagesetup_set_devmode(data, dm);
2941 GlobalUnlock(prnt.hDevNames);
2942 GlobalUnlock(prnt.hDevMode);
2943 pagesetup_init_combos(hDlg, data);
2944 }
2945
2946 GlobalFree(prnt.hDevMode);
2947 GlobalFree(prnt.hDevNames);
2948
2949 }
2950
2951 /******************************************************************************************
2952 * pagesetup_change_preview
2953 *
2954 * Changes paper preview size / position
2955 *
2956 */
2957 static void pagesetup_change_preview(const pagesetup_data *data)
2958 {
2959 LONG width, height, x, y;
2960 RECT tmp;
2961 const int shadow = 4;
2962
2963 if(pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
2964 {
2965 width = data->rtDrawRect.right - data->rtDrawRect.left;
2966 height = pagesetup_get_papersize_pt(data)->y * width / pagesetup_get_papersize_pt(data)->x;
2967 }
2968 else
2969 {
2970 height = data->rtDrawRect.bottom - data->rtDrawRect.top;
2971 width = pagesetup_get_papersize_pt(data)->x * height / pagesetup_get_papersize_pt(data)->y;
2972 }
2973 x = (data->rtDrawRect.right + data->rtDrawRect.left - width) / 2;
2974 y = (data->rtDrawRect.bottom + data->rtDrawRect.top - height) / 2;
2975 TRACE("draw rect %s x=%d, y=%d, w=%d, h=%d\n",
2976 wine_dbgstr_rect(&data->rtDrawRect), x, y, width, height);
2977
2978 MoveWindow(GetDlgItem(data->hDlg, rct2), x + width, y + shadow, shadow, height, FALSE);
2979 MoveWindow(GetDlgItem(data->hDlg, rct3), x + shadow, y + height, width, shadow, FALSE);
2980 MoveWindow(GetDlgItem(data->hDlg, rct1), x, y, width, height, FALSE);
2981
2982 tmp = data->rtDrawRect;
2983 tmp.right += shadow;
2984 tmp.bottom += shadow;
2985 InvalidateRect(data->hDlg, &tmp, TRUE);
2986 }
2987
2988 static inline LONG *element_from_margin_id(RECT *rc, WORD id)
2989 {
2990 switch(id)
2991 {
2992 case edt4: return &rc->left;
2993 case edt5: return &rc->top;
2994 case edt6: return &rc->right;
2995 case edt7: return &rc->bottom;
2996 }
2997 return NULL;
2998 }
2999
3000 static void update_margin_edits(HWND hDlg, const pagesetup_data *data, WORD id)
3001 {
3002 WCHAR str[100];
3003 WORD idx;
3004
3005 for(idx = edt4; idx <= edt7; idx++)
3006 {
3007 if(id == 0 || id == idx)
3008 {
3009 size2str(data, *element_from_margin_id(pagesetup_get_margin_rect(data), idx), str);
3010 SetDlgItemTextW(hDlg, idx, str);
3011 }
3012 }
3013 }
3014
3015 static void margin_edit_notification(HWND hDlg, const pagesetup_data *data, WORD msg, WORD id)
3016 {
3017 switch (msg)
3018 {
3019 case EN_CHANGE:
3020 {
3021 WCHAR buf[10];
3022 LONG val = 0;
3023 LONG *value = element_from_margin_id(pagesetup_get_margin_rect(data), id);
3024
3025 if (GetDlgItemTextW(hDlg, id, buf, sizeof(buf) / sizeof(buf[0])) != 0)
3026 {
3027 WCHAR *end;
3028 WCHAR decimal = get_decimal_sep();
3029
3030 val = strtolW(buf, &end, 10);
3031 if(end != buf || *end == decimal)
3032 {
3033 int mult = is_metric(data) ? 100 : 1000;
3034 val *= mult;
3035 if(*end == decimal)
3036 {
3037 while(mult > 1)
3038 {
3039 end++;
3040 mult /= 10;
3041 if(isdigitW(*end))
3042 val += (*end - '0') * mult;
3043 else
3044 break;
3045 }
3046 }
3047 }
3048 }
3049 *value = val;
3050 return;
3051 }
3052
3053 case EN_KILLFOCUS:
3054 update_margin_edits(hDlg, data, id);
3055 return;
3056 }
3057 }
3058
3059 static void set_margin_groupbox_title(HWND hDlg, const pagesetup_data *data)
3060 {
3061 WCHAR title[256];
3062
3063 if(LoadStringW(COMDLG32_hInstance, is_metric(data) ? PD32_MARGINS_IN_MILLIMETERS : PD32_MARGINS_IN_INCHES,
3064 title, sizeof(title)/sizeof(title[0])))
3065 SetDlgItemTextW(hDlg, grp4, title);
3066 }
3067
3068 static void pagesetup_update_orientation_buttons(HWND hDlg, const pagesetup_data *data)
3069 {
3070 if (pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE)
3071 CheckRadioButton(hDlg, rad1, rad2, rad2);
3072 else
3073 CheckRadioButton(hDlg, rad1, rad2, rad1);
3074 }
3075
3076 /****************************************************************************************
3077 * pagesetup_printer_properties
3078 *
3079 * Handle invocation of the 'Properties' button (not present in the default template).
3080 */
3081 static void pagesetup_printer_properties(HWND hDlg, pagesetup_data *data)
3082 {
3083 HANDLE hprn;
3084 LPWSTR devname;
3085 DEVMODEW *dm;
3086 LRESULT count;
3087 int i;
3088
3089 devname = pagesetup_get_devname(data);
3090
3091 if (!OpenPrinterW(devname, &hprn, NULL))
3092 {
3093 FIXME("Call to OpenPrinter did not succeed!\n");
3094 pagesetup_release_a_devname(data, devname);
3095 return;
3096 }
3097
3098 dm = pagesetup_get_devmode(data);
3099 DocumentPropertiesW(hDlg, hprn, devname, dm, dm, DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
3100 pagesetup_set_devmode(data, dm);
3101 pagesetup_release_devmode(data, dm);
3102 pagesetup_release_a_devname(data, devname);
3103 ClosePrinter(hprn);
3104
3105 /* Changing paper */
3106 pagesetup_update_papersize(data);
3107 pagesetup_update_orientation_buttons(hDlg, data);
3108
3109 /* Changing paper preview */
3110 pagesetup_change_preview(data);
3111
3112 /* Selecting paper in combo */
3113 count = SendDlgItemMessageW(hDlg, cmb2, CB_GETCOUNT, 0, 0);
3114 if(count != CB_ERR)
3115 {
3116 WORD paperword = pagesetup_get_papersize(data);
3117 for(i = 0; i < count; i++)
3118 {
3119 if(SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA, i, 0) == paperword) {
3120 SendDlgItemMessageW(hDlg, cmb2, CB_SETCURSEL, i, 0);
3121 break;
3122 }
3123 }
3124 }
3125 }
3126
3127 /********************************************************************************
3128 * pagesetup_wm_command
3129 * process WM_COMMAND message for PageSetupDlg
3130 *
3131 * PARAMS
3132 * hDlg [in] Main dialog HANDLE
3133 * wParam [in] WM_COMMAND wParam
3134 * lParam [in] WM_COMMAND lParam
3135 * pda [in/out] ptr to PageSetupDataA
3136 */
3137
3138 static BOOL pagesetup_wm_command(HWND hDlg, WPARAM wParam, LPARAM lParam, pagesetup_data *data)
3139 {
3140 WORD msg = HIWORD(wParam);
3141 WORD id = LOWORD(wParam);
3142
3143 TRACE("loword (lparam) %d, wparam 0x%lx, lparam %08lx\n",
3144 LOWORD(lParam),wParam,lParam);
3145 switch (id) {
3146 case IDOK:
3147 EndDialog(hDlg, TRUE);
3148 return TRUE ;
3149
3150 case IDCANCEL:
3151 EndDialog(hDlg, FALSE);
3152 return FALSE ;
3153
3154 case psh3: /* Printer... */
3155 pagesetup_change_printer_dialog(hDlg, data);
3156 return TRUE;
3157
3158 case rad1: /* Portrait */
3159 case rad2: /* Landscape */
3160 if((id == rad1 && pagesetup_get_orientation(data) == DMORIENT_LANDSCAPE) ||
3161 (id == rad2 && pagesetup_get_orientation(data) == DMORIENT_PORTRAIT))
3162 {
3163 pagesetup_set_orientation(data, (id == rad1) ? DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE);
3164 pagesetup_update_papersize(data);
3165 rotate_rect(pagesetup_get_margin_rect(data), (id == rad2));
3166 update_margin_edits(hDlg, data, 0);
3167 pagesetup_change_preview(data);
3168 }
3169 break;
3170 case cmb1: /* Printer combo */
3171 if(msg == CBN_SELCHANGE)
3172 {
3173 WCHAR name[256];
3174 GetDlgItemTextW(hDlg, id, name, sizeof(name) / sizeof(name[0]));
3175 pagesetup_change_printer(name, data);
3176 pagesetup_init_combos(hDlg, data);
3177 }
3178 break;
3179 case cmb2: /* Paper combo */
3180 if(msg == CBN_SELCHANGE)
3181 {
3182 DWORD paperword = SendDlgItemMessageW(hDlg, cmb2, CB_GETITEMDATA,
3183 SendDlgItemMessageW(hDlg, cmb2, CB_GETCURSEL, 0, 0), 0);
3184 if (paperword != CB_ERR)
3185 {
3186 pagesetup_set_papersize(data, paperword);
3187 pagesetup_update_papersize(data);
3188 pagesetup_change_preview(data);
3189 } else
3190 FIXME("could not get dialog text for papersize cmbbox?\n");
3191 }
3192 break;
3193 case cmb3: /* Paper Source */
3194 if(msg == CBN_SELCHANGE)
3195 {
3196 WORD source = SendDlgItemMessageW(hDlg, cmb3, CB_GETITEMDATA,
3197 SendDlgItemMessageW(hDlg, cmb3, CB_GETCURSEL, 0, 0), 0);
3198 pagesetup_set_defaultsource(data, source);
3199 }
3200 break;
3201 case psh2: /* Printer Properties button */
3202 pagesetup_printer_properties(hDlg, data);
3203 break;
3204 case edt4:
3205 case edt5:
3206 case edt6:
3207 case edt7:
3208 margin_edit_notification(hDlg, data, msg, id);
3209 break;
3210 }
3211 InvalidateRect(GetDlgItem(hDlg, rct1), NULL, TRUE);
3212 return FALSE;
3213 }
3214
3215 /***********************************************************************
3216 * default_page_paint_hook
3217 * Default hook paint procedure that receives WM_PSD_* messages from the dialog box
3218 * whenever the sample page is redrawn.
3219 */
3220 static UINT_PTR default_page_paint_hook(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam,
3221 const pagesetup_data *data)
3222 {
3223 LPRECT lprc = (LPRECT) lParam;
3224 HDC hdc = (HDC) wParam;
3225 HPEN hpen, holdpen;
3226 LOGFONTW lf;
3227 HFONT hfont, holdfont;
3228 INT oldbkmode;
3229 TRACE("uMsg: WM_USER+%d\n",uMsg-WM_USER);
3230 /* Call user paint hook if enable */
3231 if (pagesetup_get_flags(data) & PSD_ENABLEPAGEPAINTHOOK)
3232 if (pagesetup_get_hook(data, page_paint_hook)(hwndDlg, uMsg, wParam, lParam))
3233 return TRUE;
3234
3235 switch (uMsg) {
3236 /* LPPAGESETUPDLG in lParam */
3237 case WM_PSD_PAGESETUPDLG:
3238 /* Inform about the sample page rectangle */
3239 case WM_PSD_FULLPAGERECT:
3240 /* Inform about the margin rectangle */
3241 case WM_PSD_MINMARGINRECT:
3242 return FALSE;
3243
3244 /* Draw dashed rectangle showing margins */
3245 case WM_PSD_MARGINRECT:
3246 hpen = CreatePen(PS_DASH, 1, GetSysColor(COLOR_3DSHADOW));
3247 holdpen = SelectObject(hdc, hpen);
3248 Rectangle(hdc, lprc->left, lprc->top, lprc->right, lprc->bottom);
3249 DeleteObject(SelectObject(hdc, holdpen));
3250 return TRUE;
3251 /* Draw the fake document */
3252 case WM_PSD_GREEKTEXTRECT:
3253 /* select a nice scalable font, because we want the text really small */
3254 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
3255 lf.lfHeight = 6; /* value chosen based on visual effect */
3256 hfont = CreateFontIndirectW(&lf);
3257 holdfont = SelectObject(hdc, hfont);
3258
3259 /* if text not loaded, then do so now */
3260 if (wszFakeDocumentText[0] == '\0')
3261 LoadStringW(COMDLG32_hInstance,
3262 IDS_FAKEDOCTEXT,
3263 wszFakeDocumentText,
3264 sizeof(wszFakeDocumentText)/sizeof(wszFakeDocumentText[0]));
3265
3266 oldbkmode = SetBkMode(hdc, TRANSPARENT);
3267 DrawTextW(hdc, wszFakeDocumentText, -1, lprc, DT_TOP|DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
3268 SetBkMode(hdc, oldbkmode);
3269
3270 DeleteObject(SelectObject(hdc, holdfont));
3271 return TRUE;
3272
3273 /* Envelope stamp */
3274 case WM_PSD_ENVSTAMPRECT:
3275 /* Return address */
3276 case WM_PSD_YAFULLPAGERECT:
3277 FIXME("envelope/stamp is not implemented\n");
3278 return FALSE;
3279 default:
3280 FIXME("Unknown message %x\n",uMsg);
3281 return FALSE;
3282 }
3283 return TRUE;
3284 }
3285
3286 /***********************************************************************
3287 * PagePaintProc
3288 * The main paint procedure for the PageSetupDlg function.
3289 * The Page Setup dialog box includes an image of a sample page that shows how
3290 * the user's selections affect the appearance of the printed output.
3291 * The image consists of a rectangle that represents the selected paper
3292 * or envelope type, with a dotted-line rectangle representing
3293 * the current margins, and partial (Greek text) characters
3294 * to show how text looks on the printed page.
3295 *
3296 * The following messages in the order sends to user hook procedure:
3297 * WM_PSD_PAGESETUPDLG Draw the contents of the sample page
3298 * WM_PSD_FULLPAGERECT Inform about the bounding rectangle
3299 * WM_PSD_MINMARGINRECT Inform about the margin rectangle (min margin?)
3300 * WM_PSD_MARGINRECT Draw the margin rectangle
3301 * WM_PSD_GREEKTEXTRECT Draw the Greek text inside the margin rectangle
3302 * If any of first three messages returns TRUE, painting done.
3303 *
3304 * PARAMS:
3305 * hWnd [in] Handle to the Page Setup dialog box
3306 * uMsg [in] Received message
3307 *
3308 * TODO:
3309 * WM_PSD_ENVSTAMPRECT Draw in the envelope-stamp rectangle (for envelopes only)
3310 * WM_PSD_YAFULLPAGERECT Draw the return address portion (for envelopes and other paper sizes)
3311 *
3312 * RETURNS:
3313 * FALSE if all done correctly
3314 *
3315 */
3316
3317
3318 static LRESULT CALLBACK
3319 PRINTDLG_PagePaintProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3320 {
3321 PAINTSTRUCT ps;
3322 RECT rcClient, rcMargin;
3323 HPEN hpen, holdpen;
3324 HDC hdc;
3325 HBRUSH hbrush, holdbrush;
3326 pagesetup_data *data;
3327 int papersize=0, orientation=0; /* FIXME: set this values for user paint hook */
3328 double scalx, scaly;
3329
3330 if (uMsg != WM_PAINT)
3331 return CallWindowProcA(lpfnStaticWndProc, hWnd, uMsg, wParam, lParam);
3332
3333 /* Processing WM_PAINT message */
3334 data = GetPropW(hWnd, pagesetupdlg_prop);
3335 if (!data) {
3336 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
3337 return FALSE;
3338 }
3339 if (default_page_paint_hook(hWnd, WM_PSD_PAGESETUPDLG, MAKELONG(papersize, orientation),
3340 pagesetup_get_dlg_struct(data), data))
3341 return FALSE;
3342
3343 hdc = BeginPaint(hWnd, &ps);
3344 GetClientRect(hWnd, &rcClient);
3345
3346 scalx = rcClient.right / (double)pagesetup_get_papersize_pt(data)->x;
3347 scaly = rcClient.bottom / (double)pagesetup_get_papersize_pt(data)->y;
3348 rcMargin = rcClient;
3349
3350 rcMargin.left += pagesetup_get_margin_rect(data)->left * scalx;
3351 rcMargin.top += pagesetup_get_margin_rect(data)->top * scaly;
3352 rcMargin.right -= pagesetup_get_margin_rect(data)->right * scalx;
3353 rcMargin.bottom -= pagesetup_get_margin_rect(data)->bottom * scaly;
3354
3355 /* if the space is too small then we make sure to not draw anything */
3356 rcMargin.left = min(rcMargin.left, rcMargin.right);
3357 rcMargin.top = min(rcMargin.top, rcMargin.bottom);
3358
3359 if (!default_page_paint_hook(hWnd, WM_PSD_FULLPAGERECT, (WPARAM)hdc, (LPARAM)&rcClient, data) &&
3360 !default_page_paint_hook(hWnd, WM_PSD_MINMARGINRECT, (WPARAM)hdc, (LPARAM)&rcMargin, data) )
3361 {
3362 /* fill background */
3363 hbrush = GetSysColorBrush(COLOR_3DHIGHLIGHT);
3364 FillRect(hdc, &rcClient, hbrush);
3365 holdbrush = SelectObject(hdc, hbrush);
3366
3367 hpen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
3368 holdpen = SelectObject(hdc, hpen);
3369
3370 /* paint left edge */
3371 MoveToEx(hdc, rcClient.left, rcClient.top, NULL);
3372 LineTo(hdc, rcClient.left, rcClient.bottom-1);