Swap portrait/landscape icon when button clicked.
[wine/testsucceed.git] / dlls / commdlg / printdlg.c
blob9a9ab9cf7f45a2072374ef90cbfd6e9488cb31ad
1 /*
2 * COMMDLG - Print Dialog
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1999 Klaas van Gend
7 * Copyright 2000 Huw D M Davies
8 */
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wingdi.h"
16 #include "wine/wingdi16.h"
17 #include "winuser.h"
18 #include "wine/winuser16.h"
19 #include "commdlg.h"
20 #include "dlgs.h"
21 #include "debugtools.h"
22 #include "cderr.h"
23 #include "winspool.h"
24 #include "winerror.h"
26 DEFAULT_DEBUG_CHANNEL(commdlg);
28 #include "cdlg.h"
30 /* This PRINTDLGA internal structure stores
31 * pointers to several throughout useful structures.
34 typedef struct
36 LPDEVMODEA lpDevMode;
37 struct {
38 LPPRINTDLGA lpPrintDlg;
39 LPPRINTDLG16 lpPrintDlg16;
40 } dlg;
41 LPPRINTER_INFO_2A lpPrinterInfo;
42 LPDRIVER_INFO_3A lpDriverInfo;
43 UINT HelpMessageID;
44 HICON hCollateIcon; /* PrintDlg only */
45 HICON hNoCollateIcon; /* PrintDlg only */
46 HICON hPortraitIcon; /* PrintSetupDlg only */
47 HICON hLandscapeIcon; /* PrintSetupDlg only */
48 HWND hwndUpDown;
49 } PRINT_PTRA;
51 /* Debugging info */
52 static struct pd_flags {
53 DWORD flag;
54 LPSTR name;
55 } pd_flags[] = {
56 {PD_SELECTION, "PD_SELECTION "},
57 {PD_PAGENUMS, "PD_PAGENUMS "},
58 {PD_NOSELECTION, "PD_NOSELECTION "},
59 {PD_NOPAGENUMS, "PD_NOPAGENUMS "},
60 {PD_COLLATE, "PD_COLLATE "},
61 {PD_PRINTTOFILE, "PD_PRINTTOFILE "},
62 {PD_PRINTSETUP, "PD_PRINTSETUP "},
63 {PD_NOWARNING, "PD_NOWARNING "},
64 {PD_RETURNDC, "PD_RETURNDC "},
65 {PD_RETURNIC, "PD_RETURNIC "},
66 {PD_RETURNDEFAULT, "PD_RETURNDEFAULT "},
67 {PD_SHOWHELP, "PD_SHOWHELP "},
68 {PD_ENABLEPRINTHOOK, "PD_ENABLEPRINTHOOK "},
69 {PD_ENABLESETUPHOOK, "PD_ENABLESETUPHOOK "},
70 {PD_ENABLEPRINTTEMPLATE, "PD_ENABLEPRINTTEMPLATE "},
71 {PD_ENABLESETUPTEMPLATE, "PD_ENABLESETUPTEMPLATE "},
72 {PD_ENABLEPRINTTEMPLATEHANDLE, "PD_ENABLEPRINTTEMPLATEHANDLE "},
73 {PD_ENABLESETUPTEMPLATEHANDLE, "PD_ENABLESETUPTEMPLATEHANDLE "},
74 {PD_USEDEVMODECOPIES, "PD_USEDEVMODECOPIES[ANDCOLLATE] "},
75 {PD_DISABLEPRINTTOFILE, "PD_DISABLEPRINTTOFILE "},
76 {PD_HIDEPRINTTOFILE, "PD_HIDEPRINTTOFILE "},
77 {PD_NONETWORKBUTTON, "PD_NONETWORKBUTTON "},
78 {-1, NULL}
81 /* Debugging info */
82 static struct pd_flags psd_flags[] = {
83 {PSD_MINMARGINS,"PSD_MINMARGINS"},
84 {PSD_MARGINS,"PSD_MARGINS"},
85 {PSD_INTHOUSANDTHSOFINCHES,"PSD_INTHOUSANDTHSOFINCHES"},
86 {PSD_INHUNDREDTHSOFMILLIMETERS,"PSD_INHUNDREDTHSOFMILLIMETERS"},
87 {PSD_DISABLEMARGINS,"PSD_DISABLEMARGINS"},
88 {PSD_DISABLEPRINTER,"PSD_DISABLEPRINTER"},
89 {PSD_NOWARNING,"PSD_NOWARNING"},
90 {PSD_DISABLEORIENTATION,"PSD_DISABLEORIENTATION"},
91 {PSD_RETURNDEFAULT,"PSD_RETURNDEFAULT"},
92 {PSD_DISABLEPAPER,"PSD_DISABLEPAPER"},
93 {PSD_SHOWHELP,"PSD_SHOWHELP"},
94 {PSD_ENABLEPAGESETUPHOOK,"PSD_ENABLEPAGESETUPHOOK"},
95 {PSD_ENABLEPAGESETUPTEMPLATE,"PSD_ENABLEPAGESETUPTEMPLATE"},
96 {PSD_ENABLEPAGESETUPTEMPLATEHANDLE,"PSD_ENABLEPAGESETUPTEMPLATEHANDLE"},
97 {PSD_ENABLEPAGEPAINTHOOK,"PSD_ENABLEPAGEPAINTHOOK"},
98 {PSD_DISABLEPAGEPAINTING,"PSD_DISABLEPAGEPAINTING"},
99 {-1, NULL}
102 /* Yes these constants are the same, but we're just copying win98 */
103 #define UPDOWN_ID 0x270f
104 #define MAX_COPIES 9999
106 /***********************************************************************
107 * PRINTDLG_GetDefaultPrinterName
109 * Returns the default printer name in buf.
110 * Even under WinNT/2000 default printer is retrieved via GetProfileString -
111 * these entries are mapped somewhere in the registry rather than win.ini.
113 * Returns TRUE on success else FALSE
115 static BOOL PRINTDLG_GetDefaultPrinterName(LPSTR buf, DWORD len)
117 char *ptr;
119 if(!GetProfileStringA("windows", "device", "", buf, len)) {
120 TRACE("No profile entry for default printer found.\n");
121 return FALSE;
123 if((ptr = strchr(buf, ',')) == NULL) {
124 FIXME("bad format for default printer (%s)!\n",buf);
125 return FALSE;
127 *ptr = '\0';
128 return TRUE;
131 /***********************************************************************
132 * PRINTDLG_OpenDefaultPrinter
134 * Returns a winspool printer handle to the default printer in *hprn
135 * Caller must call ClosePrinter on the handle
137 * Returns TRUE on success else FALSE
139 static BOOL PRINTDLG_OpenDefaultPrinter(HANDLE *hprn)
141 char buf[260];
142 BOOL res;
143 if(!PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf)))
144 return FALSE;
145 res = OpenPrinterA(buf, hprn, NULL);
146 if (!res)
147 FIXME("Could not open printer %s?!\n",buf);
148 return res;
151 /***********************************************************************
152 * PRINTDLG_SetUpPrinterListCombo
154 * Initializes printer list combox.
155 * hDlg: HWND of dialog
156 * id: Control id of combo
157 * name: Name of printer to select
159 * Initializes combo with list of available printers. Selects printer 'name'
160 * If name is NULL or does not exist select the default printer.
162 * Returns number of printers added to list.
164 static INT PRINTDLG_SetUpPrinterListCombo(HWND hDlg, UINT id, LPCSTR name)
166 DWORD needed, num;
167 INT i;
168 LPPRINTER_INFO_2A pi;
169 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &needed, &num);
170 pi = HeapAlloc(GetProcessHeap(), 0, needed);
171 EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pi, needed, &needed,
172 &num);
174 for(i = 0; i < num; i++) {
175 SendDlgItemMessageA(hDlg, id, CB_ADDSTRING, 0,
176 (LPARAM)pi[i].pPrinterName );
178 HeapFree(GetProcessHeap(), 0, pi);
179 if(!name ||
180 (i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1,
181 (LPARAM)name)) == CB_ERR) {
183 char buf[260];
184 FIXME("Can't find '%s' in printer list so trying to find default\n",
185 name);
186 if(!PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf)))
187 return num;
188 i = SendDlgItemMessageA(hDlg, id, CB_FINDSTRINGEXACT, -1, (LPARAM)buf);
189 if(i == CB_ERR)
190 FIXME("Can't find default printer in printer list\n");
192 SendDlgItemMessageA(hDlg, id, CB_SETCURSEL, i, 0);
193 return num;
196 /***********************************************************************
197 * PRINTDLG_CreateDevNames [internal]
200 * creates a DevNames structure.
202 * (NB. when we handle unicode the offsets will be in wchars).
204 static BOOL PRINTDLG_CreateDevNames(HGLOBAL *hmem, char* DeviceDriverName,
205 char* DeviceName, char* OutputPort)
207 long size;
208 char* pDevNamesSpace;
209 char* pTempPtr;
210 LPDEVNAMES lpDevNames;
211 char buf[260];
213 size = strlen(DeviceDriverName) + 1
214 + strlen(DeviceName) + 1
215 + strlen(OutputPort) + 1
216 + sizeof(DEVNAMES);
218 if(*hmem)
219 *hmem = GlobalReAlloc(*hmem, size, GMEM_MOVEABLE);
220 else
221 *hmem = GlobalAlloc(GMEM_MOVEABLE, size);
222 if (*hmem == 0)
223 return FALSE;
225 pDevNamesSpace = GlobalLock(*hmem);
226 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
228 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
229 strcpy(pTempPtr, DeviceDriverName);
230 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
232 pTempPtr += strlen(DeviceDriverName) + 1;
233 strcpy(pTempPtr, DeviceName);
234 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
236 pTempPtr += strlen(DeviceName) + 1;
237 strcpy(pTempPtr, OutputPort);
238 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
240 PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf));
241 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
242 GlobalUnlock(*hmem);
243 return TRUE;
246 static BOOL PRINTDLG_CreateDevNames16(HGLOBAL16 *hmem, char* DeviceDriverName,
247 char* DeviceName, char* OutputPort)
249 long size;
250 char* pDevNamesSpace;
251 char* pTempPtr;
252 LPDEVNAMES lpDevNames;
253 char buf[260];
255 size = strlen(DeviceDriverName) + 1
256 + strlen(DeviceName) + 1
257 + strlen(OutputPort) + 1
258 + sizeof(DEVNAMES);
260 if(*hmem)
261 *hmem = GlobalReAlloc16(*hmem, size, GMEM_MOVEABLE);
262 else
263 *hmem = GlobalAlloc16(GMEM_MOVEABLE, size);
264 if (*hmem == 0)
265 return FALSE;
267 pDevNamesSpace = GlobalLock16(*hmem);
268 lpDevNames = (LPDEVNAMES) pDevNamesSpace;
270 pTempPtr = pDevNamesSpace + sizeof(DEVNAMES);
271 strcpy(pTempPtr, DeviceDriverName);
272 lpDevNames->wDriverOffset = pTempPtr - pDevNamesSpace;
274 pTempPtr += strlen(DeviceDriverName) + 1;
275 strcpy(pTempPtr, DeviceName);
276 lpDevNames->wDeviceOffset = pTempPtr - pDevNamesSpace;
278 pTempPtr += strlen(DeviceName) + 1;
279 strcpy(pTempPtr, OutputPort);
280 lpDevNames->wOutputOffset = pTempPtr - pDevNamesSpace;
282 PRINTDLG_GetDefaultPrinterName(buf, sizeof(buf));
283 lpDevNames->wDefault = (strcmp(buf, DeviceName) == 0) ? 1 : 0;
284 GlobalUnlock16(*hmem);
285 return TRUE;
289 /***********************************************************************
290 * PRINTDLG_UpdatePrintDlg [internal]
293 * updates the PrintDlg structure for returnvalues.
295 * RETURNS
296 * FALSE if user is not allowed to close (i.e. wrong nTo or nFrom values)
297 * TRUE if succesful.
299 static BOOL PRINTDLG_UpdatePrintDlg(HWND hDlg,
300 PRINT_PTRA* PrintStructures)
302 LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
303 PDEVMODEA lpdm = PrintStructures->lpDevMode;
304 LPPRINTER_INFO_2A pi = PrintStructures->lpPrinterInfo;
307 if(!lpdm) {
308 FIXME("No lpdm ptr?\n");
309 return FALSE;
313 if(!(lppd->Flags & PD_PRINTSETUP)) {
314 /* check whether nFromPage and nToPage are within range defined by
315 * nMinPage and nMaxPage
317 if (IsDlgButtonChecked(hDlg, rad3) == BST_CHECKED) { /* Pages */
318 WORD nToPage;
319 WORD nFromPage;
320 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
321 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
322 if (nFromPage < lppd->nMinPage || nFromPage > lppd->nMaxPage ||
323 nToPage < lppd->nMinPage || nToPage > lppd->nMaxPage) {
324 char resourcestr[256];
325 char resultstr[256];
326 LoadStringA(COMDLG32_hInstance, PD32_INVALID_PAGE_RANGE,
327 resourcestr, 255);
328 sprintf(resultstr,resourcestr, lppd->nMinPage, lppd->nMaxPage);
329 LoadStringA(COMDLG32_hInstance, PD32_PRINT_TITLE,
330 resourcestr, 255);
331 MessageBoxA(hDlg, resultstr, resourcestr,
332 MB_OK | MB_ICONWARNING);
333 return FALSE;
335 lppd->nFromPage = nFromPage;
336 lppd->nToPage = nToPage;
339 if (IsDlgButtonChecked(hDlg, chx1) == BST_CHECKED) {/* Print to file */
340 lppd->Flags |= PD_PRINTTOFILE;
341 pi->pPortName = "FILE:";
344 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED) { /* Collate */
345 FIXME("Collate lppd not yet implemented as output\n");
348 /* set PD_Collate and nCopies */
349 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
350 /* The application doesn't support multiple copies or collate...
352 lppd->Flags &= ~PD_COLLATE;
353 lppd->nCopies = 1;
354 /* if the printer driver supports it... store info there
355 * otherwise no collate & multiple copies !
357 if (lpdm->dmFields & DM_COLLATE)
358 lpdm->dmCollate =
359 (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED);
360 if (lpdm->dmFields & DM_COPIES)
361 lpdm->dmCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
362 } else {
363 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
364 lppd->Flags |= PD_COLLATE;
365 else
366 lppd->Flags &= ~PD_COLLATE;
367 lppd->nCopies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
370 return TRUE;
373 static BOOL PRINTDLG_PaperSize(
374 PRINTDLGA *pdlga,const char *PaperSize,LPPOINT size
376 DEVNAMES *dn;
377 DEVMODEA *dm;
378 LPSTR devname,portname;
379 int i;
380 DWORD NrOfEntries,ret;
381 char *Names = NULL;
382 POINT *points = NULL;
383 BOOL retval = FALSE;
385 dn = GlobalLock(pdlga->hDevNames);
386 dm = GlobalLock(pdlga->hDevMode);
387 devname = ((char*)dn)+dn->wDeviceOffset;
388 portname = ((char*)dn)+dn->wOutputOffset;
391 NrOfEntries = DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,NULL,dm);
392 if (!NrOfEntries) {
393 FIXME("No papernames found for %s/%s\n",devname,portname);
394 goto out;
396 Names = (char*)HeapAlloc(GetProcessHeap(),0,NrOfEntries*64);
397 if (NrOfEntries != (ret=DeviceCapabilitiesA(devname,portname,DC_PAPERNAMES,Names,dm))) {
398 FIXME("Number of returned vals %ld is not %ld\n",NrOfEntries,ret);
399 goto out;
401 for (i=0;i<NrOfEntries;i++)
402 if (!strcmp(PaperSize,Names+(64*i)))
403 break;
404 HeapFree(GetProcessHeap(),0,Names);
405 if (i==NrOfEntries) {
406 FIXME("Papersize %s not found in list?\n",PaperSize);
407 goto out;
409 points = HeapAlloc(GetProcessHeap(),0,sizeof(points[0])*NrOfEntries);
410 if (NrOfEntries!=(ret=DeviceCapabilitiesA(devname,portname,DC_PAPERSIZE,(LPBYTE)points,dm))) {
411 FIXME("Number of returned sizes %ld is not %ld?\n",NrOfEntries,ret);
412 goto out;
414 /* this is _10ths_ of a millimeter */
415 size->x=points[i].x;
416 size->y=points[i].y;
417 FIXME("papersize is %ld x %ld\n",size->x,size->y);
418 retval = TRUE;
419 out:
420 GlobalUnlock(pdlga->hDevNames);
421 GlobalUnlock(pdlga->hDevMode);
422 if (Names) HeapFree(GetProcessHeap(),0,Names);
423 if (points) HeapFree(GetProcessHeap(),0,points);
424 return retval;
428 /************************************************************************
429 * PRINTDLG_SetUpPaperComboBox
431 * Initialize either the papersize or inputslot combos of the Printer Setup
432 * dialog. We store the associated word (eg DMPAPER_A4) as the item data.
433 * We also try to re-select the old selection.
435 static BOOL PRINTDLG_SetUpPaperComboBox(HWND hDlg,
436 int nIDComboBox,
437 char* PrinterName,
438 char* PortName,
439 LPDEVMODEA dm)
441 int i;
442 DWORD NrOfEntries;
443 char* Names;
444 WORD* Words;
445 DWORD Sel;
446 WORD oldWord = 0;
447 int NamesSize;
448 int fwCapability_Names;
449 int fwCapability_Words;
451 TRACE(" Printer: %s, ComboID: %d\n",PrinterName,nIDComboBox);
453 /* query the dialog box for the current selected value */
454 Sel = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETCURSEL, 0, 0);
455 if(Sel != CB_ERR) {
456 /* we enter here only if a different printer is selected after
457 * the Print Setup dialog is opened. The current settings are
458 * stored into the newly selected printer.
460 oldWord = SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA,
461 Sel, 0);
462 if (dm) {
463 if (nIDComboBox == cmb2)
464 dm->u1.s1.dmPaperSize = oldWord;
465 else
466 dm->dmDefaultSource = oldWord;
469 else {
470 /* we enter here only when the Print setup dialog is initially
471 * opened. In this case the settings are restored from when
472 * the dialog was last closed.
474 if (dm) {
475 if (nIDComboBox == cmb2)
476 oldWord = dm->u1.s1.dmPaperSize;
477 else
478 oldWord = dm->dmDefaultSource;
482 if (nIDComboBox == cmb2) {
483 NamesSize = 64;
484 fwCapability_Names = DC_PAPERNAMES;
485 fwCapability_Words = DC_PAPERS;
486 } else {
487 nIDComboBox = cmb3;
488 NamesSize = 24;
489 fwCapability_Names = DC_BINNAMES;
490 fwCapability_Words = DC_BINS;
493 /* for some printer drivers, DeviceCapabilities calls a VXD to obtain the
494 * paper settings. As Wine doesn't allow VXDs, this results in a crash.
496 WARN(" if your printer driver uses VXDs, expect a crash now!\n");
497 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
498 fwCapability_Names, NULL, dm);
499 if (NrOfEntries == 0)
500 WARN("no Name Entries found!\n");
502 if(DeviceCapabilitiesA(PrinterName, PortName, fwCapability_Words, NULL, dm)
503 != NrOfEntries) {
504 ERR("Number of caps is different\n");
505 NrOfEntries = 0;
508 Names = HeapAlloc(GetProcessHeap(),0, NrOfEntries*NamesSize);
509 Words = HeapAlloc(GetProcessHeap(),0, NrOfEntries*sizeof(WORD));
510 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
511 fwCapability_Names, Names, dm);
512 NrOfEntries = DeviceCapabilitiesA(PrinterName, PortName,
513 fwCapability_Words, (LPSTR)Words, dm);
515 /* reset any current content in the combobox */
516 SendDlgItemMessageA(hDlg, nIDComboBox, CB_RESETCONTENT, 0, 0);
518 /* store new content */
519 for (i = 0; i < NrOfEntries; i++) {
520 DWORD pos = SendDlgItemMessageA(hDlg, nIDComboBox, CB_ADDSTRING, 0,
521 (LPARAM)(&Names[i*NamesSize]) );
522 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETITEMDATA, pos,
523 Words[i]);
526 /* Look for old selection - can't do this is previous loop since
527 item order will change as more items are added */
528 Sel = 0;
529 for (i = 0; i < NrOfEntries; i++) {
530 if(SendDlgItemMessageA(hDlg, nIDComboBox, CB_GETITEMDATA, i, 0) ==
531 oldWord) {
532 Sel = i;
533 break;
536 SendDlgItemMessageA(hDlg, nIDComboBox, CB_SETCURSEL, Sel, 0);
538 HeapFree(GetProcessHeap(),0,Words);
539 HeapFree(GetProcessHeap(),0,Names);
540 return TRUE;
543 /***********************************************************************
544 * PRINTDLG_UpdatePrinterInfoTexts [internal]
546 static void PRINTDLG_UpdatePrinterInfoTexts(HWND hDlg, LPPRINTER_INFO_2A pi)
548 char StatusMsg[256];
549 char ResourceString[256];
550 int i;
552 /* Status Message */
553 StatusMsg[0]='\0';
555 /* add all status messages */
556 for (i = 0; i < 25; i++) {
557 if (pi->Status & (1<<i)) {
558 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_PAUSED+i,
559 ResourceString, 255);
560 strcat(StatusMsg,ResourceString);
563 /* append "ready" */
564 /* FIXME: status==ready must only be appended if really so.
565 but how to detect? */
566 LoadStringA(COMDLG32_hInstance, PD32_PRINTER_STATUS_READY,
567 ResourceString, 255);
568 strcat(StatusMsg,ResourceString);
570 SendDlgItemMessageA(hDlg, stc12, WM_SETTEXT, 0, (LPARAM)StatusMsg);
572 /* set all other printer info texts */
573 SendDlgItemMessageA(hDlg, stc11, WM_SETTEXT, 0, (LPARAM)pi->pDriverName);
574 if (pi->pLocation != NULL && pi->pLocation[0] != '\0')
575 SendDlgItemMessageA(hDlg, stc14, WM_SETTEXT, 0,(LPARAM)pi->pLocation);
576 else
577 SendDlgItemMessageA(hDlg, stc14, WM_SETTEXT, 0,(LPARAM)pi->pPortName);
578 SendDlgItemMessageA(hDlg, stc13, WM_SETTEXT, 0, (LPARAM)(pi->pComment ?
579 pi->pComment : ""));
580 return;
584 /*******************************************************************
586 * PRINTDLG_ChangePrinter
589 static BOOL PRINTDLG_ChangePrinter(HWND hDlg, char *name,
590 PRINT_PTRA *PrintStructures)
592 LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
593 LPDEVMODEA lpdm = NULL;
594 LONG dmSize;
595 DWORD needed;
596 HANDLE hprn;
598 if(PrintStructures->lpPrinterInfo)
599 HeapFree(GetProcessHeap(),0, PrintStructures->lpPrinterInfo);
600 if(PrintStructures->lpDriverInfo)
601 HeapFree(GetProcessHeap(),0, PrintStructures->lpDriverInfo);
602 if(!OpenPrinterA(name, &hprn, NULL)) {
603 ERR("Can't open printer %s\n", name);
604 return FALSE;
606 GetPrinterA(hprn, 2, NULL, 0, &needed);
607 PrintStructures->lpPrinterInfo = HeapAlloc(GetProcessHeap(),0,needed);
608 GetPrinterA(hprn, 2, (LPBYTE)PrintStructures->lpPrinterInfo, needed,
609 &needed);
610 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
611 PrintStructures->lpDriverInfo = HeapAlloc(GetProcessHeap(),0,needed);
612 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)PrintStructures->lpDriverInfo,
613 needed, &needed)) {
614 ERR("GetPrinterDriverA failed for %s, fix your config!\n",PrintStructures->lpPrinterInfo->pPrinterName);
615 return FALSE;
617 ClosePrinter(hprn);
619 PRINTDLG_UpdatePrinterInfoTexts(hDlg, PrintStructures->lpPrinterInfo);
621 if(PrintStructures->lpDevMode) {
622 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
623 PrintStructures->lpDevMode = NULL;
626 dmSize = DocumentPropertiesA(0, 0, name, NULL, NULL, 0);
627 if(dmSize == -1) {
628 ERR("DocumentProperties fails on %s\n", debugstr_a(name));
629 return FALSE;
631 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(), 0, dmSize);
632 dmSize = DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, NULL,
633 DM_OUT_BUFFER);
634 if(lppd->hDevMode && (lpdm = GlobalLock(lppd->hDevMode)) &&
635 !strcmp(lpdm->dmDeviceName,
636 PrintStructures->lpDevMode->dmDeviceName)) {
637 /* Supplied devicemode matches current printer so try to use it */
638 DocumentPropertiesA(0, 0, name, PrintStructures->lpDevMode, lpdm,
639 DM_OUT_BUFFER | DM_IN_BUFFER);
641 if(lpdm)
642 GlobalUnlock(lppd->hDevMode);
644 lpdm = PrintStructures->lpDevMode; /* use this as a shortcut */
646 if(!(lppd->Flags & PD_PRINTSETUP)) {
647 /* Print range (All/Range/Selection) */
648 SetDlgItemInt(hDlg, edt1, lppd->nFromPage, FALSE);
649 SetDlgItemInt(hDlg, edt2, lppd->nToPage, FALSE);
650 CheckRadioButton(hDlg, rad1, rad3, rad1); /* default */
651 if (lppd->Flags & PD_NOSELECTION)
652 EnableWindow(GetDlgItem(hDlg, rad2), FALSE);
653 else
654 if (lppd->Flags & PD_SELECTION)
655 CheckRadioButton(hDlg, rad1, rad3, rad2);
656 if (lppd->Flags & PD_NOPAGENUMS) {
657 EnableWindow(GetDlgItem(hDlg, rad3), FALSE);
658 EnableWindow(GetDlgItem(hDlg, stc2),FALSE);
659 EnableWindow(GetDlgItem(hDlg, edt1), FALSE);
660 EnableWindow(GetDlgItem(hDlg, stc3),FALSE);
661 EnableWindow(GetDlgItem(hDlg, edt2), FALSE);
662 } else {
663 if (lppd->Flags & PD_PAGENUMS)
664 CheckRadioButton(hDlg, rad1, rad3, rad3);
666 /* "All xxx pages"... */
668 char resourcestr[64];
669 char result[64];
670 LoadStringA(COMDLG32_hInstance, PD32_PRINT_ALL_X_PAGES,
671 resourcestr, 49);
672 sprintf(result,resourcestr,lppd->nMaxPage - lppd->nMinPage + 1);
673 SendDlgItemMessageA(hDlg, rad1, WM_SETTEXT, 0, (LPARAM) result);
676 /* Collate pages
678 * FIXME: The ico3 is not displayed for some reason. I don't know why.
680 if (lppd->Flags & PD_COLLATE) {
681 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
682 (LPARAM)PrintStructures->hCollateIcon);
683 CheckDlgButton(hDlg, chx2, 1);
684 } else {
685 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
686 (LPARAM)PrintStructures->hNoCollateIcon);
687 CheckDlgButton(hDlg, chx2, 0);
690 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
691 /* if printer doesn't support it: no Collate */
692 if (!(lpdm->dmFields & DM_COLLATE)) {
693 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
694 EnableWindow(GetDlgItem(hDlg, ico3), FALSE);
698 /* nCopies */
700 INT copies;
701 if (lppd->hDevMode == 0)
702 copies = lppd->nCopies;
703 else
704 copies = lpdm->dmCopies;
705 if(copies == 0) copies = 1;
706 else if(copies < 0) copies = MAX_COPIES;
707 SetDlgItemInt(hDlg, edt3, copies, FALSE);
710 if (lppd->Flags & PD_USEDEVMODECOPIESANDCOLLATE) {
711 /* if printer doesn't support it: no nCopies */
712 if (!(lpdm->dmFields & DM_COPIES)) {
713 EnableWindow(GetDlgItem(hDlg, edt3), FALSE);
714 EnableWindow(GetDlgItem(hDlg, stc5), FALSE);
718 /* print to file */
719 CheckDlgButton(hDlg, chx1, (lppd->Flags & PD_PRINTTOFILE) ? 1 : 0);
720 if (lppd->Flags & PD_DISABLEPRINTTOFILE)
721 EnableWindow(GetDlgItem(hDlg, chx1), FALSE);
722 if (lppd->Flags & PD_HIDEPRINTTOFILE)
723 ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE);
725 } else { /* PD_PRINTSETUP */
726 BOOL bPortrait = (lpdm->u1.s1.dmOrientation == DMORIENT_PORTRAIT);
728 PRINTDLG_SetUpPaperComboBox(hDlg, cmb2,
729 PrintStructures->lpPrinterInfo->pPrinterName,
730 PrintStructures->lpPrinterInfo->pPortName,
731 lpdm);
732 PRINTDLG_SetUpPaperComboBox(hDlg, cmb3,
733 PrintStructures->lpPrinterInfo->pPrinterName,
734 PrintStructures->lpPrinterInfo->pPortName,
735 lpdm);
736 CheckRadioButton(hDlg, rad1, rad2, bPortrait ? rad1: rad2);
737 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
738 (LPARAM)(bPortrait ? PrintStructures->hPortraitIcon :
739 PrintStructures->hLandscapeIcon));
743 /* help button */
744 if ((lppd->Flags & PD_SHOWHELP)==0) {
745 /* hide if PD_SHOWHELP not specified */
746 ShowWindow(GetDlgItem(hDlg, pshHelp), SW_HIDE);
748 return TRUE;
751 /***********************************************************************
752 * PRINTDLG_WMInitDialog [internal]
754 static LRESULT PRINTDLG_WMInitDialog(HWND hDlg, WPARAM wParam,
755 PRINT_PTRA* PrintStructures)
757 LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
758 DEVNAMES *pdn;
759 DEVMODEA *pdm;
760 char *name = NULL;
761 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
763 /* load Collate ICONs */
764 /* We load these with LoadImage becasue they are not a standard
765 size and we don't want them rescaled */
766 PrintStructures->hCollateIcon =
767 LoadImageA(COMDLG32_hInstance, "PD32_COLLATE", IMAGE_ICON, 0, 0, 0);
768 PrintStructures->hNoCollateIcon =
769 LoadImageA(COMDLG32_hInstance, "PD32_NOCOLLATE", IMAGE_ICON, 0, 0, 0);
771 /* These can be done with LoadIcon */
772 PrintStructures->hPortraitIcon =
773 LoadIconA(COMDLG32_hInstance, "PD32_PORTRAIT");
774 PrintStructures->hLandscapeIcon =
775 LoadIconA(COMDLG32_hInstance, "PD32_LANDSCAPE");
777 if(PrintStructures->hCollateIcon == 0 ||
778 PrintStructures->hNoCollateIcon == 0 ||
779 PrintStructures->hPortraitIcon == 0 ||
780 PrintStructures->hLandscapeIcon == 0) {
781 ERR("no icon in resourcefile\n");
782 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
783 EndDialog(hDlg, FALSE);
787 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
788 * must be registered and the Help button must be shown.
790 if (lppd->Flags & PD_SHOWHELP) {
791 if((PrintStructures->HelpMessageID =
792 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
793 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
794 return FALSE;
796 } else
797 PrintStructures->HelpMessageID = 0;
799 if(!(lppd->Flags &PD_PRINTSETUP)) {
800 PrintStructures->hwndUpDown =
801 CreateUpDownControl(WS_CHILD | WS_VISIBLE | WS_BORDER |
802 UDS_NOTHOUSANDS | UDS_ARROWKEYS |
803 UDS_ALIGNRIGHT | UDS_SETBUDDYINT, 0, 0, 0, 0,
804 hDlg, UPDOWN_ID, COMDLG32_hInstance,
805 GetDlgItem(hDlg, edt3), MAX_COPIES, 1, 1);
808 /* FIXME: I allow more freedom than either Win95 or WinNT,
809 * which do not agree to what errors should be thrown or not
810 * in case nToPage or nFromPage is out-of-range.
812 if (lppd->nMaxPage < lppd->nMinPage)
813 lppd->nMaxPage = lppd->nMinPage;
814 if (lppd->nMinPage == lppd->nMaxPage)
815 lppd->Flags |= PD_NOPAGENUMS;
816 if (lppd->nToPage < lppd->nMinPage)
817 lppd->nToPage = lppd->nMinPage;
818 if (lppd->nToPage > lppd->nMaxPage)
819 lppd->nToPage = lppd->nMaxPage;
820 if (lppd->nFromPage < lppd->nMinPage)
821 lppd->nFromPage = lppd->nMinPage;
822 if (lppd->nFromPage > lppd->nMaxPage)
823 lppd->nFromPage = lppd->nMaxPage;
825 /* if we have the combo box, fill it */
826 if (GetDlgItem(hDlg,comboID)) {
827 /* Fill Combobox
829 pdn = GlobalLock(lppd->hDevNames);
830 pdm = GlobalLock(lppd->hDevMode);
831 if(pdn)
832 name = (char*)pdn + pdn->wDeviceOffset;
833 else if(pdm)
834 name = pdm->dmDeviceName;
835 PRINTDLG_SetUpPrinterListCombo(hDlg, comboID, name);
836 if(pdm) GlobalUnlock(lppd->hDevMode);
837 if(pdn) GlobalUnlock(lppd->hDevNames);
839 /* Now find selected printer and update rest of dlg */
840 name = HeapAlloc(GetProcessHeap(),0,256);
841 if (GetDlgItemTextA(hDlg, comboID, name, 255))
842 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
843 HeapFree(GetProcessHeap(),0,name);
844 } else {
845 /* else use default printer */
846 char name[200];
847 BOOL ret = PRINTDLG_GetDefaultPrinterName(name, sizeof(name));
849 if (ret)
850 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
851 else
852 FIXME("No default printer found, expect problems!\n");
854 return TRUE;
857 /***********************************************************************
858 * PRINTDLG_WMInitDialog [internal]
860 static LRESULT PRINTDLG_WMInitDialog16(HWND hDlg, WPARAM wParam,
861 PRINT_PTRA* PrintStructures)
863 LPPRINTDLG16 lppd = PrintStructures->dlg.lpPrintDlg16;
864 DEVNAMES *pdn;
865 DEVMODEA *pdm;
866 char *name = NULL;
867 UINT comboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
869 /* load Collate ICONs */
870 PrintStructures->hCollateIcon =
871 LoadIconA(COMDLG32_hInstance, "PD32_COLLATE");
872 PrintStructures->hNoCollateIcon =
873 LoadIconA(COMDLG32_hInstance, "PD32_NOCOLLATE");
874 if(PrintStructures->hCollateIcon == 0 ||
875 PrintStructures->hNoCollateIcon == 0) {
876 ERR("no icon in resourcefile\n");
877 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
878 EndDialog(hDlg, FALSE);
881 /* load Paper Orientation ICON */
882 /* FIXME: not implemented yet */
885 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
886 * must be registered and the Help button must be shown.
888 if (lppd->Flags & PD_SHOWHELP) {
889 if((PrintStructures->HelpMessageID =
890 RegisterWindowMessageA(HELPMSGSTRINGA)) == 0) {
891 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL);
892 return FALSE;
894 } else
895 PrintStructures->HelpMessageID = 0;
897 /* FIXME: I allow more freedom than either Win95 or WinNT,
898 * which do not agree to what errors should be thrown or not
899 * in case nToPage or nFromPage is out-of-range.
901 if (lppd->nMaxPage < lppd->nMinPage)
902 lppd->nMaxPage = lppd->nMinPage;
903 if (lppd->nMinPage == lppd->nMaxPage)
904 lppd->Flags |= PD_NOPAGENUMS;
905 if (lppd->nToPage < lppd->nMinPage)
906 lppd->nToPage = lppd->nMinPage;
907 if (lppd->nToPage > lppd->nMaxPage)
908 lppd->nToPage = lppd->nMaxPage;
909 if (lppd->nFromPage < lppd->nMinPage)
910 lppd->nFromPage = lppd->nMinPage;
911 if (lppd->nFromPage > lppd->nMaxPage)
912 lppd->nFromPage = lppd->nMaxPage;
914 /* If the printer combo box is in the dialog, fill it */
915 if (GetDlgItem(hDlg,comboID)) {
916 /* Fill Combobox
918 pdn = GlobalLock16(lppd->hDevNames);
919 pdm = GlobalLock16(lppd->hDevMode);
920 if(pdn)
921 name = (char*)pdn + pdn->wDeviceOffset;
922 else if(pdm)
923 name = pdm->dmDeviceName;
924 PRINTDLG_SetUpPrinterListCombo(hDlg, comboID, name);
925 if(pdm) GlobalUnlock16(lppd->hDevMode);
926 if(pdn) GlobalUnlock16(lppd->hDevNames);
928 /* Now find selected printer and update rest of dlg */
929 name = HeapAlloc(GetProcessHeap(),0,256);
930 if (GetDlgItemTextA(hDlg, comboID, name, 255))
931 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
932 } else {
933 /* else just use default printer */
934 char name[200];
935 BOOL ret = PRINTDLG_GetDefaultPrinterName(name, sizeof(name));
937 if (ret)
938 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
939 else
940 FIXME("No default printer found, expect problems!\n");
942 HeapFree(GetProcessHeap(),0,name);
944 return TRUE;
947 /***********************************************************************
948 * PRINTDLG_WMCommand [internal]
950 static LRESULT PRINTDLG_WMCommand(HWND hDlg, WPARAM wParam,
951 LPARAM lParam, PRINT_PTRA* PrintStructures)
953 LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
954 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
955 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
957 switch (LOWORD(wParam)) {
958 case IDOK:
959 TRACE(" OK button was hit\n");
960 if (PRINTDLG_UpdatePrintDlg(hDlg, PrintStructures)!=TRUE) {
961 FIXME("Update printdlg was not successful!\n");
962 return(FALSE);
964 EndDialog(hDlg, TRUE);
965 return(TRUE);
967 case IDCANCEL:
968 TRACE(" CANCEL button was hit\n");
969 EndDialog(hDlg, FALSE);
970 return(FALSE);
972 case pshHelp:
973 TRACE(" HELP button was hit\n");
974 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
975 (WPARAM) hDlg, (LPARAM) lppd);
976 break;
978 case chx2: /* collate pages checkbox */
979 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
980 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
981 (LPARAM)PrintStructures->hCollateIcon);
982 else
983 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
984 (LPARAM)PrintStructures->hNoCollateIcon);
985 break;
986 case edt1: /* from page nr editbox */
987 case edt2: /* to page nr editbox */
988 if (HIWORD(wParam)==EN_CHANGE) {
989 WORD nToPage;
990 WORD nFromPage;
991 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
992 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
993 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
994 CheckRadioButton(hDlg, rad1, rad3, rad3);
996 break;
998 case edt3:
999 if(HIWORD(wParam) == EN_CHANGE) {
1000 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1001 if(copies <= 1)
1002 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1003 else
1004 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1006 break;
1008 case psh2: /* Properties button */
1010 HANDLE hPrinter;
1011 char PrinterName[256];
1013 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1014 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1015 FIXME(" Call to OpenPrinter did not succeed!\n");
1016 break;
1018 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1019 PrintStructures->lpDevMode,
1020 PrintStructures->lpDevMode,
1021 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1022 ClosePrinter(hPrinter);
1023 break;
1026 case rad1: /* Paperorientation */
1027 if (lppd->Flags & PD_PRINTSETUP)
1029 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1030 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1031 (LPARAM)(PrintStructures->hPortraitIcon));
1033 break;
1035 case rad2: /* Paperorientation */
1036 if (lppd->Flags & PD_PRINTSETUP)
1038 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1039 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1040 (LPARAM)(PrintStructures->hLandscapeIcon));
1042 break;
1044 case cmb1:
1045 case cmb4: /* Printer combobox */
1046 if (HIWORD(wParam)==CBN_SELCHANGE) {
1047 char PrinterName[256];
1048 GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1049 PRINTDLG_ChangePrinter(hDlg, PrinterName, PrintStructures);
1051 break;
1053 case cmb2: /* Papersize */
1055 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1056 if(Sel != CB_ERR)
1057 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1058 CB_GETITEMDATA,
1059 Sel, 0);
1061 break;
1063 case cmb3: /* Bin */
1065 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1066 if(Sel != CB_ERR)
1067 lpdm->dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1068 CB_GETITEMDATA, Sel,
1071 break;
1073 if(lppd->Flags & PD_PRINTSETUP) {
1074 switch (LOWORD(wParam)) {
1075 case rad1: /* orientation */
1076 case rad2:
1077 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1078 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1079 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1080 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1081 (WPARAM)IMAGE_ICON,
1082 (LPARAM)PrintStructures->hPortraitIcon);
1084 } else {
1085 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1086 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1087 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1088 (WPARAM)IMAGE_ICON,
1089 (LPARAM)PrintStructures->hLandscapeIcon);
1092 break;
1095 return FALSE;
1098 /***********************************************************************
1099 * PrintDlgProcA [internal]
1101 BOOL WINAPI PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1102 LPARAM lParam)
1104 PRINT_PTRA* PrintStructures;
1105 LRESULT res=FALSE;
1107 if (uMsg!=WM_INITDIALOG) {
1108 PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);
1109 if (!PrintStructures)
1110 return FALSE;
1111 } else {
1112 PrintStructures = (PRINT_PTRA*) lParam;
1113 SetWindowLongA(hDlg, DWL_USER, lParam);
1114 res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1116 if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1117 res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(
1118 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg
1120 return res;
1123 if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1124 res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1125 lParam);
1126 if(res) return res;
1129 switch (uMsg) {
1130 case WM_COMMAND:
1131 return PRINTDLG_WMCommand(hDlg, wParam, lParam, PrintStructures);
1133 case WM_DESTROY:
1134 DestroyIcon(PrintStructures->hCollateIcon);
1135 DestroyIcon(PrintStructures->hNoCollateIcon);
1136 DestroyIcon(PrintStructures->hPortraitIcon);
1137 DestroyIcon(PrintStructures->hLandscapeIcon);
1138 if(PrintStructures->hwndUpDown)
1139 DestroyWindow(PrintStructures->hwndUpDown);
1140 return FALSE;
1142 return res;
1146 /************************************************************
1148 * PRINTDLG_Get16TemplateFrom32 [Internal]
1149 * Generates a 16 bits template from the Wine 32 bits resource
1152 static HGLOBAL16 PRINTDLG_Get16TemplateFrom32(char *PrintResourceName)
1154 HANDLE hResInfo, hDlgTmpl32;
1155 LPCVOID template32;
1156 DWORD size;
1157 HGLOBAL16 hGlobal16;
1158 LPVOID template;
1160 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
1161 PrintResourceName, RT_DIALOGA)))
1163 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1164 return 0;
1166 if (!(hDlgTmpl32 = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
1167 !(template32 = LockResource( hDlgTmpl32 )))
1169 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1170 return 0;
1172 size = SizeofResource(COMMDLG_hInstance32, hResInfo);
1173 hGlobal16 = GlobalAlloc16(0, size);
1174 if (!hGlobal16)
1176 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
1177 ERR("alloc failure for %ld bytes\n", size);
1178 return 0;
1180 template = GlobalLock16(hGlobal16);
1181 if (!template)
1183 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
1184 ERR("global lock failure for %x handle\n", hGlobal16);
1185 GlobalFree16(hGlobal16);
1186 return 0;
1188 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
1189 GlobalUnlock16(hGlobal16);
1190 return hGlobal16;
1193 /************************************************************
1195 * PRINTDLG_GetDlgTemplate
1198 static HGLOBAL PRINTDLG_GetDlgTemplate(PRINTDLGA *lppd)
1200 HGLOBAL hDlgTmpl, hResInfo;
1202 if (lppd->Flags & PD_PRINTSETUP) {
1203 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1204 hDlgTmpl = lppd->hSetupTemplate;
1205 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1206 hResInfo = FindResourceA(lppd->hInstance,
1207 lppd->lpSetupTemplateName, RT_DIALOGA);
1208 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1209 } else {
1210 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1211 RT_DIALOGA);
1212 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1214 } else {
1215 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1216 hDlgTmpl = lppd->hPrintTemplate;
1217 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1218 hResInfo = FindResourceA(lppd->hInstance,
1219 lppd->lpPrintTemplateName,
1220 RT_DIALOGA);
1221 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1222 } else {
1223 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1224 RT_DIALOGA);
1225 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1228 return hDlgTmpl;
1232 /************************************************************
1234 * PRINTDLG_GetDlgTemplate
1237 static HGLOBAL16 PRINTDLG_GetDlgTemplate16(PRINTDLG16 *lppd)
1239 HGLOBAL16 hDlgTmpl, hResInfo;
1241 if (lppd->Flags & PD_PRINTSETUP) {
1242 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1243 hDlgTmpl = lppd->hSetupTemplate;
1244 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1245 hResInfo = FindResource16(lppd->hInstance,
1246 MapSL(lppd->lpSetupTemplateName), RT_DIALOGA);
1247 hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1248 } else {
1249 hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32_SETUP");
1251 } else {
1252 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1253 hDlgTmpl = lppd->hPrintTemplate;
1254 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1255 hResInfo = FindResource16(lppd->hInstance,
1256 MapSL(lppd->lpPrintTemplateName),
1257 RT_DIALOGA);
1258 hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1259 } else {
1260 hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32");
1263 return hDlgTmpl;
1266 /***********************************************************************
1268 * PRINTDLG_CreateDC
1271 static BOOL PRINTDLG_CreateDC(LPPRINTDLGA lppd)
1273 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1274 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1276 if(lppd->Flags & PD_RETURNDC) {
1277 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1278 (char*)pdn + pdn->wDeviceOffset,
1279 (char*)pdn + pdn->wOutputOffset,
1280 pdm );
1281 } else if(lppd->Flags & PD_RETURNIC) {
1282 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1283 (char*)pdn + pdn->wDeviceOffset,
1284 (char*)pdn + pdn->wOutputOffset,
1285 pdm );
1287 GlobalUnlock(lppd->hDevNames);
1288 GlobalUnlock(lppd->hDevMode);
1289 return lppd->hDC ? TRUE : FALSE;
1292 static BOOL PRINTDLG_CreateDC16(LPPRINTDLG16 lppd)
1294 DEVNAMES *pdn = GlobalLock16(lppd->hDevNames);
1295 DEVMODEA *pdm = GlobalLock16(lppd->hDevMode);
1297 if(lppd->Flags & PD_RETURNDC) {
1298 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1299 (char*)pdn + pdn->wDeviceOffset,
1300 (char*)pdn + pdn->wOutputOffset,
1301 pdm );
1302 } else if(lppd->Flags & PD_RETURNIC) {
1303 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1304 (char*)pdn + pdn->wDeviceOffset,
1305 (char*)pdn + pdn->wOutputOffset,
1306 pdm );
1308 GlobalUnlock16(lppd->hDevNames);
1309 GlobalUnlock16(lppd->hDevMode);
1310 return lppd->hDC ? TRUE : FALSE;
1313 /***********************************************************************
1314 * PrintDlgA (COMDLG32.17)
1316 * Displays the the PRINT dialog box, which enables the user to specify
1317 * specific properties of the print job.
1319 * RETURNS
1320 * nonzero if the user pressed the OK button
1321 * zero if the user cancelled the window or an error occurred
1323 * BUGS
1324 * PrintDlg:
1325 * * The Collate Icons do not display, even though they are in the code.
1326 * * The Properties Button(s) should call DocumentPropertiesA().
1327 * PrintSetupDlg:
1328 * * The Paper Orientation Icons are not implemented yet.
1329 * * The Properties Button(s) should call DocumentPropertiesA().
1330 * * Settings are not yet taken from a provided DevMode or
1331 * default printer settings.
1333 BOOL WINAPI PrintDlgA(
1334 LPPRINTDLGA lppd /* [in/out] ptr to PRINTDLG32 struct */
1337 BOOL bRet = FALSE;
1338 LPVOID ptr;
1339 HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1341 if(TRACE_ON(commdlg)) {
1342 char flagstr[1000] = "";
1343 struct pd_flags *pflag = pd_flags;
1344 for( ; pflag->name; pflag++) {
1345 if(lppd->Flags & pflag->flag)
1346 strcat(flagstr, pflag->name);
1348 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1349 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1350 "flags %08lx (%s)\n",
1351 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1352 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1353 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1356 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
1357 WARN("structure size failure !!!\n");
1358 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1359 return FALSE;
1362 if(lppd->Flags & PD_RETURNDEFAULT) {
1363 PRINTER_INFO_2A *pbuf;
1364 DRIVER_INFO_3A *dbuf;
1365 HANDLE hprn;
1366 DWORD needed;
1368 if(lppd->hDevMode || lppd->hDevNames) {
1369 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1370 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1371 return FALSE;
1373 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1374 WARN("Can't find default printer\n");
1375 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
1376 return FALSE;
1379 GetPrinterA(hprn, 2, NULL, 0, &needed);
1380 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1381 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1383 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1384 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1385 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1386 ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",GetLastError(),pbuf->pPrinterName);
1387 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1388 return FALSE;
1390 ClosePrinter(hprn);
1392 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1393 dbuf->pDriverPath,
1394 pbuf->pPrinterName,
1395 pbuf->pPortName);
1396 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
1397 pbuf->pDevMode->dmDriverExtra);
1398 ptr = GlobalLock(lppd->hDevMode);
1399 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1400 pbuf->pDevMode->dmDriverExtra);
1401 GlobalUnlock(lppd->hDevMode);
1402 HeapFree(GetProcessHeap(), 0, pbuf);
1403 HeapFree(GetProcessHeap(), 0, dbuf);
1404 bRet = TRUE;
1405 } else {
1406 HGLOBAL hDlgTmpl;
1407 PRINT_PTRA *PrintStructures;
1409 /* load Dialog resources,
1410 * depending on Flags indicates Print32 or Print32_setup dialog
1412 hDlgTmpl = PRINTDLG_GetDlgTemplate(lppd);
1413 if (!hDlgTmpl) {
1414 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1415 return FALSE;
1417 ptr = LockResource( hDlgTmpl );
1418 if (!ptr) {
1419 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1420 return FALSE;
1423 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1424 sizeof(PRINT_PTRA));
1425 PrintStructures->dlg.lpPrintDlg = lppd;
1427 /* and create & process the dialog .
1428 * -1 is failure, 0 is broken hwnd, everything else is ok.
1430 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
1431 PrintDlgProcA,
1432 (LPARAM)PrintStructures));
1434 if(bRet) {
1435 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1436 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1437 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1439 if (lppd->hDevMode == 0) {
1440 TRACE(" No hDevMode yet... Need to create my own\n");
1441 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
1442 lpdm->dmSize + lpdm->dmDriverExtra);
1443 } else {
1444 WORD locks;
1445 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
1446 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1447 while(locks--) {
1448 GlobalUnlock(lppd->hDevMode);
1449 TRACE("Now got %d locks\n", locks);
1452 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
1453 lpdm->dmSize + lpdm->dmDriverExtra,
1454 GMEM_MOVEABLE);
1456 lpdmReturn = GlobalLock(lppd->hDevMode);
1457 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1459 if (lppd->hDevNames != 0) {
1460 WORD locks;
1461 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
1462 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1463 while(locks--)
1464 GlobalUnlock(lppd->hDevNames);
1467 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1468 di->pDriverPath,
1469 pi->pPrinterName,
1470 pi->pPortName
1472 GlobalUnlock(lppd->hDevMode);
1474 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1475 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1476 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1477 HeapFree(GetProcessHeap(), 0, PrintStructures);
1479 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1480 bRet = PRINTDLG_CreateDC(lppd);
1482 TRACE("exit! (%d)\n", bRet);
1483 return bRet;
1486 /***********************************************************************
1487 * PrintDlg16 (COMMDLG.20)
1489 * Displays the the PRINT dialog box, which enables the user to specify
1490 * specific properties of the print job.
1492 * RETURNS
1493 * nonzero if the user pressed the OK button
1494 * zero if the user cancelled the window or an error occurred
1496 * BUGS
1497 * * calls up to the 32-bit versions of the Dialogs, which look different
1498 * * Customizing is *not* implemented.
1501 BOOL16 WINAPI PrintDlg16(
1502 LPPRINTDLG16 lppd /* [in/out] ptr to PRINTDLG struct */
1504 BOOL bRet = FALSE;
1505 LPVOID ptr;
1506 HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1508 if(TRACE_ON(commdlg)) {
1509 char flagstr[1000] = "";
1510 struct pd_flags *pflag = pd_flags;
1511 for( ; pflag->name; pflag++) {
1512 if(lppd->Flags & pflag->flag)
1513 strcat(flagstr, pflag->name);
1515 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1516 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1517 "flags %08lx (%s)\n",
1518 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1519 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1520 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1523 if(lppd->lStructSize != sizeof(PRINTDLG16)) {
1524 ERR("structure size (%ld/%d)\n",lppd->lStructSize,sizeof(PRINTDLG16));
1525 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1526 return FALSE;
1529 if(lppd->Flags & PD_RETURNDEFAULT) {
1530 PRINTER_INFO_2A *pbuf;
1531 DRIVER_INFO_3A *dbuf;
1532 HANDLE hprn;
1533 DWORD needed;
1535 if(lppd->hDevMode || lppd->hDevNames) {
1536 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1537 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1538 return FALSE;
1540 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1541 WARN("Can't find default printer\n");
1542 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
1543 return FALSE;
1546 GetPrinterA(hprn, 2, NULL, 0, &needed);
1547 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1548 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1549 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1550 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1551 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1552 ERR("GetPrinterDriverA failed for %s, le %ld, fix your config!\n",
1553 pbuf->pPrinterName,GetLastError());
1554 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1555 return FALSE;
1557 ClosePrinter(hprn);
1558 PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1559 dbuf->pDriverPath,
1560 pbuf->pPrinterName,
1561 pbuf->pPortName);
1562 lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,pbuf->pDevMode->dmSize+
1563 pbuf->pDevMode->dmDriverExtra);
1564 ptr = GlobalLock16(lppd->hDevMode);
1565 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1566 pbuf->pDevMode->dmDriverExtra);
1567 GlobalUnlock16(lppd->hDevMode);
1568 HeapFree(GetProcessHeap(), 0, pbuf);
1569 HeapFree(GetProcessHeap(), 0, dbuf);
1570 bRet = TRUE;
1571 } else {
1572 HGLOBAL hDlgTmpl;
1573 PRINT_PTRA *PrintStructures;
1575 /* load Dialog resources,
1576 * depending on Flags indicates Print32 or Print32_setup dialog
1578 hDlgTmpl = PRINTDLG_GetDlgTemplate16(lppd);
1579 if (!hDlgTmpl) {
1580 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1581 return FALSE;
1583 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1584 sizeof(PRINT_PTRA));
1585 PrintStructures->dlg.lpPrintDlg16 = lppd;
1586 PrintStructures->dlg.lpPrintDlg = (LPPRINTDLGA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(PRINTDLGA));
1587 #define CVAL(x) PrintStructures->dlg.lpPrintDlg->x = lppd->x;
1588 #define MVAL(x) PrintStructures->dlg.lpPrintDlg->x = MapSL(lppd->x);
1589 CVAL(Flags);CVAL(hwndOwner);CVAL(hDC);
1590 CVAL(nFromPage);CVAL(nToPage);CVAL(nMinPage);CVAL(nMaxPage);
1591 CVAL(nCopies);CVAL(hInstance);CVAL(lCustData);
1592 MVAL(lpPrintTemplateName);MVAL(lpSetupTemplateName);
1593 /* Don't copy rest, it is 16 bit specific */
1594 #undef MVAL
1595 #undef CVAL
1597 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(),0,sizeof(DEVMODEA));
1599 /* and create & process the dialog .
1600 * -1 is failure, 0 is broken hwnd, everything else is ok.
1602 bRet = (0<DialogBoxIndirectParam16(
1603 hInst, hDlgTmpl, lppd->hwndOwner,
1604 (DLGPROC16)GetProcAddress16(GetModuleHandle16("COMMDLG"),(LPCSTR)21),
1605 (LPARAM)PrintStructures
1608 if (!PrintStructures->lpPrinterInfo) bRet = FALSE;
1609 if(bRet) {
1610 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1611 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1612 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1614 if (lppd->hDevMode == 0) {
1615 TRACE(" No hDevMode yet... Need to create my own\n");
1616 lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,
1617 lpdm->dmSize + lpdm->dmDriverExtra);
1618 } else {
1619 WORD locks;
1620 if((locks = (GlobalFlags16(lppd->hDevMode)&GMEM_LOCKCOUNT))) {
1621 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1622 while(locks--) {
1623 GlobalUnlock16(lppd->hDevMode);
1624 TRACE("Now got %d locks\n", locks);
1627 lppd->hDevMode = GlobalReAlloc16(lppd->hDevMode,
1628 lpdm->dmSize + lpdm->dmDriverExtra,
1629 GMEM_MOVEABLE);
1631 lpdmReturn = GlobalLock16(lppd->hDevMode);
1632 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1634 if (lppd->hDevNames != 0) {
1635 WORD locks;
1636 if((locks = (GlobalFlags16(lppd->hDevNames)&GMEM_LOCKCOUNT))) {
1637 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1638 while(locks--)
1639 GlobalUnlock16(lppd->hDevNames);
1642 PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1643 di->pDriverPath,
1644 pi->pPrinterName,
1645 pi->pPortName
1647 GlobalUnlock16(lppd->hDevMode);
1649 if (!(lppd->Flags & (PD_ENABLESETUPTEMPLATEHANDLE | PD_ENABLESETUPTEMPLATE)))
1650 GlobalFree16(hDlgTmpl); /* created from the 32 bits resource */
1651 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1652 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1653 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1654 HeapFree(GetProcessHeap(), 0, PrintStructures);
1656 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1657 bRet = PRINTDLG_CreateDC16(lppd);
1659 TRACE("exit! (%d)\n", bRet);
1660 return bRet;
1665 /***********************************************************************
1666 * PrintDlgW (COMDLG32.18)
1668 BOOL WINAPI PrintDlgW( LPPRINTDLGW printdlg )
1670 FIXME("A really empty stub\n" );
1671 return FALSE;
1674 /***********************************************************************
1676 * PageSetupDlg
1677 * rad1 - portrait
1678 * rad2 - landscape
1679 * cmb2 - paper size
1680 * cmb3 - source (tray?)
1681 * edt4 - border left
1682 * edt5 - border top
1683 * edt6 - border right
1684 * edt7 - border bottom
1685 * psh3 - "Printer..."
1688 typedef struct {
1689 LPPAGESETUPDLGA dlga;
1691 PRINTDLGA pdlg;
1692 } PageSetupData;
1694 static HGLOBAL PRINTDLG_GetPGSTemplate(PAGESETUPDLGA *lppd)
1696 HGLOBAL hDlgTmpl, hResInfo;
1698 if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATEHANDLE) {
1699 hDlgTmpl = lppd->hPageSetupTemplate;
1700 } else if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATE) {
1701 hResInfo = FindResourceA(lppd->hInstance,
1702 lppd->lpPageSetupTemplateName, RT_DIALOGA);
1703 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1704 } else {
1705 hResInfo = FindResourceA(COMDLG32_hInstance,(LPCSTR)PAGESETUPDLGORD,RT_DIALOGA);
1706 hDlgTmpl = LoadResource(COMDLG32_hInstance,hResInfo);
1708 return hDlgTmpl;
1711 static DWORD
1712 _c_10mm2size(PAGESETUPDLGA *dlga,DWORD size) {
1713 if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1714 return 10*size*10/25.4;
1715 /* If we don't have a flag, we can choose one. Use millimeters
1716 * to avoid confusing me
1718 dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1719 FIXME("returning %ld/100 mm \n",size);
1720 return 10*size;
1724 static DWORD
1725 _c_inch2size(PAGESETUPDLGA *dlga,DWORD size) {
1726 if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1727 return size;
1728 if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1729 return (size*254)/10;
1730 /* if we don't have a flag, we can choose one. Use millimeters
1731 * to avoid confusing me
1733 dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1734 return (size*254)/10;
1737 static void
1738 _c_size2str(PageSetupData *pda,DWORD size,LPSTR strout) {
1739 strcpy(strout,"<undef>");
1740 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1741 sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1742 return;
1744 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1745 sprintf(strout,"%.2fin",(size*1.0)/1000.0);
1746 return;
1748 pda->dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1749 sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1750 return;
1753 static DWORD
1754 _c_str2size(PageSetupData *pda,LPCSTR strin) {
1755 float val;
1756 char rest[200];
1758 rest[0]='\0';
1759 if (!sscanf(strin,"%f%s",&val,rest))
1760 return 0;
1762 if (!strcmp(rest,"in") || !strcmp(rest,"inch")) {
1763 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1764 return 1000*val;
1765 else
1766 return val*25.4*100;
1768 if (!strcmp(rest,"cm")) { rest[0]='m'; val = val*10.0; }
1769 if (!strcmp(rest,"m")) { strcpy(rest,"mm"); val = val*1000.0; }
1771 if (!strcmp(rest,"mm")) {
1772 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1773 return 100*val;
1774 else
1775 return 1000.0*val/25.4;
1777 if (rest[0]=='\0') {
1778 /* use application supplied default */
1779 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1780 /* 100*mm */
1781 return 100.0*val;
1783 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1784 /* 1000*inch */
1785 return 1000.0*val;
1788 ERR("Did not find a conversion for type '%s'!\n",rest);
1789 return 0;
1794 * This is called on finish and will update the output fields of the
1795 * struct.
1797 static BOOL
1798 PRINTDLG_PS_UpdateDlgStruct(HWND hDlg, PageSetupData *pda) {
1799 DEVNAMES *dn;
1800 DEVMODEA *dm;
1801 LPSTR devname,portname;
1802 char papername[64];
1803 char buf[200];
1805 dn = GlobalLock(pda->pdlg.hDevNames);
1806 dm = GlobalLock(pda->pdlg.hDevMode);
1807 devname = ((char*)dn)+dn->wDeviceOffset;
1808 portname = ((char*)dn)+dn->wOutputOffset;
1809 PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1810 PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1812 if (GetDlgItemTextA(hDlg,cmb2,papername,sizeof(papername))>0) {
1813 PRINTDLG_PaperSize(&(pda->pdlg),papername,&(pda->dlga->ptPaperSize));
1814 pda->dlga->ptPaperSize.x = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.x);
1815 pda->dlga->ptPaperSize.y = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.y);
1816 } else
1817 FIXME("could not get dialog text for papersize cmbbox?\n");
1818 #define GETVAL(id,val) if (GetDlgItemTextA(hDlg,id,buf,sizeof(buf))>0) { val = _c_str2size(pda,buf); } else { FIXME("could not get dlgitemtexta for %x\n",id); }
1819 GETVAL(edt4,pda->dlga->rtMargin.left);
1820 GETVAL(edt5,pda->dlga->rtMargin.top);
1821 GETVAL(edt6,pda->dlga->rtMargin.right);
1822 GETVAL(edt7,pda->dlga->rtMargin.bottom);
1823 #undef GETVAL
1825 /* If we are in landscape, swap x and y of page size */
1826 if (IsDlgButtonChecked(hDlg, rad2)) {
1827 DWORD tmp;
1828 tmp = pda->dlga->ptPaperSize.x;
1829 pda->dlga->ptPaperSize.x = pda->dlga->ptPaperSize.y;
1830 pda->dlga->ptPaperSize.y = tmp;
1832 GlobalUnlock(pda->pdlg.hDevNames);
1833 GlobalUnlock(pda->pdlg.hDevMode);
1834 return TRUE;
1838 * This is called after returning from PrintDlg().
1840 static BOOL
1841 PRINTDLG_PS_ChangePrinter(HWND hDlg, PageSetupData *pda) {
1842 DEVNAMES *dn;
1843 DEVMODEA *dm;
1844 LPSTR devname,portname;
1846 dn = GlobalLock(pda->pdlg.hDevNames);
1847 dm = GlobalLock(pda->pdlg.hDevMode);
1848 devname = ((char*)dn)+dn->wDeviceOffset;
1849 portname = ((char*)dn)+dn->wOutputOffset;
1850 PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1851 PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1852 GlobalUnlock(pda->pdlg.hDevNames);
1853 GlobalUnlock(pda->pdlg.hDevMode);
1854 return TRUE;
1857 static BOOL
1858 PRINTDLG_PS_WMCommand(
1859 HWND hDlg, WPARAM wParam, LPARAM lParam, PageSetupData *pda
1861 switch (LOWORD(wParam)) {
1862 case IDOK:
1863 if (!PRINTDLG_PS_UpdateDlgStruct(hDlg, pda))
1864 return(FALSE);
1865 EndDialog(hDlg, TRUE);
1866 return TRUE ;
1868 case IDCANCEL:
1869 EndDialog(hDlg, FALSE);
1870 return FALSE ;
1872 case psh3: {
1873 pda->pdlg.Flags = 0;
1874 pda->pdlg.hwndOwner = hDlg;
1875 if (PrintDlgA(&(pda->pdlg)))
1876 PRINTDLG_PS_ChangePrinter(hDlg,pda);
1877 return TRUE;
1880 FIXME("loword (lparam) %d, wparam 0x%x, lparam %08lx, STUB mostly.\n",
1881 LOWORD(lParam),wParam,lParam
1883 return FALSE;
1887 static BOOL WINAPI
1888 PageDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1890 PageSetupData *pda;
1891 BOOL res = FALSE;
1893 if (uMsg==WM_INITDIALOG) {
1894 res = TRUE;
1895 pda = (PageSetupData*)lParam;
1896 SetPropA(hDlg,"__WINE_PAGESETUPDLGDATA",lParam);
1897 if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
1898 res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
1899 if (!res) {
1900 FIXME("Setup page hook failed?\n");
1901 res = TRUE;
1904 if (pda->dlga->Flags & PSD_ENABLEPAGEPAINTHOOK) {
1905 FIXME("PagePaintHook not yet implemented!\n");
1907 if (pda->dlga->Flags & PSD_DISABLEPRINTER)
1908 EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
1909 if (pda->dlga->Flags & PSD_DISABLEMARGINS) {
1910 EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
1911 EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
1912 EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
1913 EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
1915 /* width larger as height -> landscape */
1916 if (pda->dlga->ptPaperSize.x > pda->dlga->ptPaperSize.y)
1917 CheckRadioButton(hDlg, rad1, rad2, rad2);
1918 else /* this is default if papersize is not set */
1919 CheckRadioButton(hDlg, rad1, rad2, rad1);
1920 if (pda->dlga->Flags & PSD_DISABLEORIENTATION) {
1921 EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
1922 EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
1924 /* We fill them out enabled or not */
1925 if (pda->dlga->Flags & PSD_MARGINS) {
1926 char str[100];
1927 _c_size2str(pda,pda->dlga->rtMargin.left,str);
1928 SetDlgItemTextA(hDlg,edt4,str);
1929 _c_size2str(pda,pda->dlga->rtMargin.top,str);
1930 SetDlgItemTextA(hDlg,edt5,str);
1931 _c_size2str(pda,pda->dlga->rtMargin.right,str);
1932 SetDlgItemTextA(hDlg,edt6,str);
1933 _c_size2str(pda,pda->dlga->rtMargin.bottom,str);
1934 SetDlgItemTextA(hDlg,edt7,str);
1935 } else {
1936 /* default is 1 inch */
1937 DWORD size = _c_inch2size(pda->dlga,1000);
1938 char str[20];
1939 _c_size2str(pda,size,str);
1940 SetDlgItemTextA(hDlg,edt4,str);
1941 SetDlgItemTextA(hDlg,edt5,str);
1942 SetDlgItemTextA(hDlg,edt6,str);
1943 SetDlgItemTextA(hDlg,edt7,str);
1945 PRINTDLG_PS_ChangePrinter(hDlg,pda);
1946 if (pda->dlga->Flags & PSD_DISABLEPAPER) {
1947 EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
1948 EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
1950 return TRUE;
1951 } else {
1952 pda = (PageSetupData*)GetPropA(hDlg,"__WINE_PAGESETUPDLGDATA");
1953 if (!pda) {
1954 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
1955 return FALSE;
1957 if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
1958 res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
1959 if (res) return res;
1962 switch (uMsg) {
1963 case WM_COMMAND:
1964 return PRINTDLG_PS_WMCommand(hDlg, wParam, lParam, pda);
1966 return FALSE;
1969 /***********************************************************************
1970 * PageSetupDlgA (COMDLG32.15)
1972 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg) {
1973 HGLOBAL hDlgTmpl;
1974 LPVOID ptr;
1975 BOOL bRet;
1976 PageSetupData *pda;
1977 PRINTDLGA pdlg;
1979 if(TRACE_ON(commdlg)) {
1980 char flagstr[1000] = "";
1981 struct pd_flags *pflag = psd_flags;
1982 for( ; pflag->name; pflag++) {
1983 if(setupdlg->Flags & pflag->flag) {
1984 strcat(flagstr, pflag->name);
1985 strcat(flagstr, "|");
1988 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1989 "hinst %08x, flags %08lx (%s)\n",
1990 setupdlg, setupdlg->hwndOwner, setupdlg->hDevMode,
1991 setupdlg->hDevNames,
1992 setupdlg->hInstance, setupdlg->Flags, flagstr);
1995 /* First get default printer data, we need it right after that. */
1996 memset(&pdlg,0,sizeof(pdlg));
1997 pdlg.lStructSize = sizeof(pdlg);
1998 pdlg.Flags = PD_RETURNDEFAULT;
1999 bRet = PrintDlgA(&pdlg);
2000 if (!bRet) return FALSE;
2002 /* short cut exit, just return default values */
2003 if (setupdlg->Flags & PSD_RETURNDEFAULT) {
2004 setupdlg->hDevMode = pdlg.hDevMode;
2005 setupdlg->hDevNames = pdlg.hDevNames;
2006 /* FIXME: Just return "A4" for now. */
2007 PRINTDLG_PaperSize(&pdlg,"A4",&setupdlg->ptPaperSize);
2008 setupdlg->ptPaperSize.x=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.x);
2009 setupdlg->ptPaperSize.y=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.y);
2010 return TRUE;
2012 hDlgTmpl = PRINTDLG_GetPGSTemplate(setupdlg);
2013 if (!hDlgTmpl) {
2014 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2015 return FALSE;
2017 ptr = LockResource( hDlgTmpl );
2018 if (!ptr) {
2019 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2020 return FALSE;
2022 pda = HeapAlloc(GetProcessHeap(),0,sizeof(*pda));
2023 pda->dlga = setupdlg;
2024 memcpy(&pda->pdlg,&pdlg,sizeof(pdlg));
2026 bRet = (0<DialogBoxIndirectParamA(
2027 setupdlg->hInstance,
2028 ptr,
2029 setupdlg->hwndOwner,
2030 PageDlgProcA,
2031 (LPARAM)pda)
2033 return bRet;
2035 /***********************************************************************
2036 * PageSetupDlgW (COMDLG32.16)
2038 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg) {
2039 FIXME("(%p), stub!\n",setupdlg);
2040 return FALSE;
2043 /**********************************************************************
2045 * 16 bit commdlg
2048 /***********************************************************************
2049 * PrintDlgProc16 (COMMDLG.21)
2051 LRESULT WINAPI PrintDlgProc16(HWND16 hDlg, UINT16 uMsg, WPARAM16 wParam,
2052 LPARAM lParam)
2054 PRINT_PTRA* PrintStructures;
2055 LRESULT res=FALSE;
2057 if (uMsg!=WM_INITDIALOG) {
2058 PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);
2059 if (!PrintStructures)
2060 return FALSE;
2061 } else {
2062 PrintStructures = (PRINT_PTRA*) lParam;
2063 SetWindowLongA(hDlg, DWL_USER, lParam);
2064 res = PRINTDLG_WMInitDialog16(hDlg, wParam, PrintStructures);
2066 if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2067 res = CallWindowProc16(
2068 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2069 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg16
2072 return res;
2075 if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2076 res = CallWindowProc16(
2077 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2078 hDlg,uMsg, wParam, lParam
2080 if(LOWORD(res)) return res;
2083 switch (uMsg) {
2084 case WM_COMMAND: {
2085 /* We need to map those for the 32bit window procedure, compare
2086 * with 32Ato16 mapper in winproc.c
2088 return PRINTDLG_WMCommand(
2089 hDlg,
2090 MAKEWPARAM(wParam,HIWORD(lParam)),
2091 LOWORD(lParam),
2092 PrintStructures
2095 case WM_DESTROY:
2096 DestroyIcon(PrintStructures->hCollateIcon);
2097 DestroyIcon(PrintStructures->hNoCollateIcon);
2098 /* FIXME: don't forget to delete the paper orientation icons here! */
2100 return FALSE;
2102 return res;
2106 /***********************************************************************
2107 * PrintSetupDlgProc16 (COMMDLG.22)
2109 LRESULT WINAPI PrintSetupDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
2110 LPARAM lParam)
2112 switch (wMsg)
2114 case WM_INITDIALOG:
2115 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
2116 ShowWindow(hWnd, SW_SHOWNORMAL);
2117 return (TRUE);
2118 case WM_COMMAND:
2119 switch (wParam) {
2120 case IDOK:
2121 EndDialog(hWnd, TRUE);
2122 return(TRUE);
2123 case IDCANCEL:
2124 EndDialog(hWnd, FALSE);
2125 return(TRUE);
2127 return(FALSE);
2129 return FALSE;
2133 /***********************************************************************
2134 * PrintDlgExA
2136 HRESULT WINAPI PrintDlgExA(LPVOID lpPrintDlgExA) /* [???] FIXME: LPPRINTDLGEXA */
2138 FIXME("stub\n");
2139 return E_NOTIMPL;
2141 /***********************************************************************
2142 * PrintDlgExW
2144 HRESULT WINAPI PrintDlgExW(LPVOID lpPrintDlgExW) /* [???] FIXME: LPPRINTDLGEXW */
2146 FIXME("stub\n");
2147 return E_NOTIMPL;