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