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