Added Quality Box (readonly currently) for 16bit templated dialogs.
[wine.git] / dlls / commdlg / printdlg.c
blob5c504b1bbdba76e3e26ddb414d997fc76bcc5fc6
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 if (!(lppd->Flags & PD_PRINTSETUP)) {
898 /* We have a print quality combo box. What shall we do? */
899 if (GetDlgItem(hDlg,cmb1)) {
900 char buf [20];
902 FIXME("Print quality only displaying currently.\n");
904 pdm = GlobalLock16(lppd->hDevMode);
905 if(pdm) {
906 switch (pdm->dmPrintQuality) {
907 case DMRES_HIGH : strcpy(buf,"High");break;
908 case DMRES_MEDIUM : strcpy(buf,"Medium");break;
909 case DMRES_LOW : strcpy(buf,"Low");break;
910 case DMRES_DRAFT : strcpy(buf,"Draft");break;
911 case 0 : strcpy(buf,"Default");break;
912 default : sprintf(buf,"%ddpi",pdm->dmPrintQuality);break;
914 GlobalUnlock16(lppd->hDevMode);
915 } else
916 strcpy(buf,"Default");
917 SendDlgItemMessageA(hDlg,cmb1,CB_ADDSTRING,0,(LPARAM)buf);
918 SendDlgItemMessageA(hDlg,cmb1,CB_SETCURSEL,0,0);
919 EnableWindow(GetDlgItem(hDlg,cmb1),FALSE);
923 /* FIXME: I allow more freedom than either Win95 or WinNT,
924 * which do not agree to what errors should be thrown or not
925 * in case nToPage or nFromPage is out-of-range.
927 if (lppd->nMaxPage < lppd->nMinPage)
928 lppd->nMaxPage = lppd->nMinPage;
929 if (lppd->nMinPage == lppd->nMaxPage)
930 lppd->Flags |= PD_NOPAGENUMS;
931 if (lppd->nToPage < lppd->nMinPage)
932 lppd->nToPage = lppd->nMinPage;
933 if (lppd->nToPage > lppd->nMaxPage)
934 lppd->nToPage = lppd->nMaxPage;
935 if (lppd->nFromPage < lppd->nMinPage)
936 lppd->nFromPage = lppd->nMinPage;
937 if (lppd->nFromPage > lppd->nMaxPage)
938 lppd->nFromPage = lppd->nMaxPage;
940 /* If the printer combo box is in the dialog, fill it */
941 if (GetDlgItem(hDlg,comboID)) {
942 /* Fill Combobox
944 pdn = GlobalLock16(lppd->hDevNames);
945 pdm = GlobalLock16(lppd->hDevMode);
946 if(pdn)
947 name = (char*)pdn + pdn->wDeviceOffset;
948 else if(pdm)
949 name = pdm->dmDeviceName;
950 PRINTDLG_SetUpPrinterListCombo(hDlg, comboID, name);
951 if(pdm) GlobalUnlock16(lppd->hDevMode);
952 if(pdn) GlobalUnlock16(lppd->hDevNames);
954 /* Now find selected printer and update rest of dlg */
955 name = HeapAlloc(GetProcessHeap(),0,256);
956 if (GetDlgItemTextA(hDlg, comboID, name, 255))
957 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
958 } else {
959 /* else just use default printer */
960 char name[200];
961 BOOL ret = PRINTDLG_GetDefaultPrinterName(name, sizeof(name));
963 if (ret)
964 PRINTDLG_ChangePrinter(hDlg, name, PrintStructures);
965 else
966 FIXME("No default printer found, expect problems!\n");
968 HeapFree(GetProcessHeap(),0,name);
970 return TRUE;
973 /***********************************************************************
974 * PRINTDLG_WMCommand [internal]
976 static LRESULT PRINTDLG_WMCommand(HWND hDlg, WPARAM wParam,
977 LPARAM lParam, PRINT_PTRA* PrintStructures)
979 LPPRINTDLGA lppd = PrintStructures->dlg.lpPrintDlg;
980 UINT PrinterComboID = (lppd->Flags & PD_PRINTSETUP) ? cmb1 : cmb4;
981 LPDEVMODEA lpdm = PrintStructures->lpDevMode;
983 switch (LOWORD(wParam)) {
984 case IDOK:
985 TRACE(" OK button was hit\n");
986 if (PRINTDLG_UpdatePrintDlg(hDlg, PrintStructures)!=TRUE) {
987 FIXME("Update printdlg was not successful!\n");
988 return(FALSE);
990 EndDialog(hDlg, TRUE);
991 return(TRUE);
993 case IDCANCEL:
994 TRACE(" CANCEL button was hit\n");
995 EndDialog(hDlg, FALSE);
996 return(FALSE);
998 case pshHelp:
999 TRACE(" HELP button was hit\n");
1000 SendMessageA(lppd->hwndOwner, PrintStructures->HelpMessageID,
1001 (WPARAM) hDlg, (LPARAM) lppd);
1002 break;
1004 case chx2: /* collate pages checkbox */
1005 if (IsDlgButtonChecked(hDlg, chx2) == BST_CHECKED)
1006 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1007 (LPARAM)PrintStructures->hCollateIcon);
1008 else
1009 SendDlgItemMessageA(hDlg, ico3, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1010 (LPARAM)PrintStructures->hNoCollateIcon);
1011 break;
1012 case edt1: /* from page nr editbox */
1013 case edt2: /* to page nr editbox */
1014 if (HIWORD(wParam)==EN_CHANGE) {
1015 WORD nToPage;
1016 WORD nFromPage;
1017 nFromPage = GetDlgItemInt(hDlg, edt1, NULL, FALSE);
1018 nToPage = GetDlgItemInt(hDlg, edt2, NULL, FALSE);
1019 if (nFromPage != lppd->nFromPage || nToPage != lppd->nToPage)
1020 CheckRadioButton(hDlg, rad1, rad3, rad3);
1022 break;
1024 case edt3:
1025 if(HIWORD(wParam) == EN_CHANGE) {
1026 INT copies = GetDlgItemInt(hDlg, edt3, NULL, FALSE);
1027 if(copies <= 1)
1028 EnableWindow(GetDlgItem(hDlg, chx2), FALSE);
1029 else
1030 EnableWindow(GetDlgItem(hDlg, chx2), TRUE);
1032 break;
1034 case psh1: /* Print Setup */
1036 PRINTDLG16 pdlg;
1038 if (!PrintStructures->dlg.lpPrintDlg16) {
1039 FIXME("The 32bit print dialog does not have this button!?\n");
1040 break;
1043 memcpy(&pdlg,PrintStructures->dlg.lpPrintDlg16,sizeof(pdlg));
1044 pdlg.Flags |= PD_PRINTSETUP;
1045 pdlg.hwndOwner = hDlg;
1046 if (!PrintDlg16(&pdlg))
1047 break;
1049 break;
1050 case psh2: /* Properties button */
1052 HANDLE hPrinter;
1053 char PrinterName[256];
1055 GetDlgItemTextA(hDlg, PrinterComboID, PrinterName, 255);
1056 if (!OpenPrinterA(PrinterName, &hPrinter, NULL)) {
1057 FIXME(" Call to OpenPrinter did not succeed!\n");
1058 break;
1060 DocumentPropertiesA(hDlg, hPrinter, PrinterName,
1061 PrintStructures->lpDevMode,
1062 PrintStructures->lpDevMode,
1063 DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT);
1064 ClosePrinter(hPrinter);
1065 break;
1068 case rad1: /* Paperorientation */
1069 if (lppd->Flags & PD_PRINTSETUP)
1071 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1072 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1073 (LPARAM)(PrintStructures->hPortraitIcon));
1075 break;
1077 case rad2: /* Paperorientation */
1078 if (lppd->Flags & PD_PRINTSETUP)
1080 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1081 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE, (WPARAM) IMAGE_ICON,
1082 (LPARAM)(PrintStructures->hLandscapeIcon));
1084 break;
1086 case cmb1: /* Printer Combobox in PRINT SETUP, quality combobox in PRINT */
1087 if (PrinterComboID != wParam) {
1088 FIXME("No handling for print quality combo box yet.\n");
1089 break;
1091 /* FALLTHROUGH */
1092 case cmb4: /* Printer combobox */
1093 if (HIWORD(wParam)==CBN_SELCHANGE) {
1094 char PrinterName[256];
1095 GetDlgItemTextA(hDlg, LOWORD(wParam), PrinterName, 255);
1096 PRINTDLG_ChangePrinter(hDlg, PrinterName, PrintStructures);
1098 break;
1100 case cmb2: /* Papersize */
1102 DWORD Sel = SendDlgItemMessageA(hDlg, cmb2, CB_GETCURSEL, 0, 0);
1103 if(Sel != CB_ERR)
1104 lpdm->u1.s1.dmPaperSize = SendDlgItemMessageA(hDlg, cmb2,
1105 CB_GETITEMDATA,
1106 Sel, 0);
1108 break;
1110 case cmb3: /* Bin */
1112 DWORD Sel = SendDlgItemMessageA(hDlg, cmb3, CB_GETCURSEL, 0, 0);
1113 if(Sel != CB_ERR)
1114 lpdm->dmDefaultSource = SendDlgItemMessageA(hDlg, cmb3,
1115 CB_GETITEMDATA, Sel,
1118 break;
1120 if(lppd->Flags & PD_PRINTSETUP) {
1121 switch (LOWORD(wParam)) {
1122 case rad1: /* orientation */
1123 case rad2:
1124 if (IsDlgButtonChecked(hDlg, rad1) == BST_CHECKED) {
1125 if(lpdm->u1.s1.dmOrientation != DMORIENT_PORTRAIT) {
1126 lpdm->u1.s1.dmOrientation = DMORIENT_PORTRAIT;
1127 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1128 (WPARAM)IMAGE_ICON,
1129 (LPARAM)PrintStructures->hPortraitIcon);
1130 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1131 (WPARAM)IMAGE_ICON,
1132 (LPARAM)PrintStructures->hPortraitIcon);
1134 } else {
1135 if(lpdm->u1.s1.dmOrientation != DMORIENT_LANDSCAPE) {
1136 lpdm->u1.s1.dmOrientation = DMORIENT_LANDSCAPE;
1137 SendDlgItemMessageA(hDlg, stc10, STM_SETIMAGE,
1138 (WPARAM)IMAGE_ICON,
1139 (LPARAM)PrintStructures->hLandscapeIcon);
1140 SendDlgItemMessageA(hDlg, ico1, STM_SETIMAGE,
1141 (WPARAM)IMAGE_ICON,
1142 (LPARAM)PrintStructures->hLandscapeIcon);
1145 break;
1148 return FALSE;
1151 /***********************************************************************
1152 * PrintDlgProcA [internal]
1154 BOOL WINAPI PrintDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam,
1155 LPARAM lParam)
1157 PRINT_PTRA* PrintStructures;
1158 LRESULT res=FALSE;
1160 if (uMsg!=WM_INITDIALOG) {
1161 PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);
1162 if (!PrintStructures)
1163 return FALSE;
1164 } else {
1165 PrintStructures = (PRINT_PTRA*) lParam;
1166 SetWindowLongA(hDlg, DWL_USER, lParam);
1167 res = PRINTDLG_WMInitDialog(hDlg, wParam, PrintStructures);
1169 if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK)
1170 res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(
1171 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg
1173 return res;
1176 if(PrintStructures->dlg.lpPrintDlg->Flags & PD_ENABLEPRINTHOOK) {
1177 res = PrintStructures->dlg.lpPrintDlg->lpfnPrintHook(hDlg,uMsg,wParam,
1178 lParam);
1179 if(res) return res;
1182 switch (uMsg) {
1183 case WM_COMMAND:
1184 return PRINTDLG_WMCommand(hDlg, wParam, lParam, PrintStructures);
1186 case WM_DESTROY:
1187 DestroyIcon(PrintStructures->hCollateIcon);
1188 DestroyIcon(PrintStructures->hNoCollateIcon);
1189 DestroyIcon(PrintStructures->hPortraitIcon);
1190 DestroyIcon(PrintStructures->hLandscapeIcon);
1191 if(PrintStructures->hwndUpDown)
1192 DestroyWindow(PrintStructures->hwndUpDown);
1193 return FALSE;
1195 return res;
1199 /************************************************************
1201 * PRINTDLG_Get16TemplateFrom32 [Internal]
1202 * Generates a 16 bits template from the Wine 32 bits resource
1205 static HGLOBAL16 PRINTDLG_Get16TemplateFrom32(char *PrintResourceName)
1207 HANDLE hResInfo, hDlgTmpl32;
1208 LPCVOID template32;
1209 DWORD size;
1210 HGLOBAL16 hGlobal16;
1211 LPVOID template;
1213 if (!(hResInfo = FindResourceA(COMMDLG_hInstance32,
1214 PrintResourceName, RT_DIALOGA)))
1216 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1217 return 0;
1219 if (!(hDlgTmpl32 = LoadResource(COMMDLG_hInstance32, hResInfo )) ||
1220 !(template32 = LockResource( hDlgTmpl32 )))
1222 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1223 return 0;
1225 size = SizeofResource(COMMDLG_hInstance32, hResInfo);
1226 hGlobal16 = GlobalAlloc16(0, size);
1227 if (!hGlobal16)
1229 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
1230 ERR("alloc failure for %ld bytes\n", size);
1231 return 0;
1233 template = GlobalLock16(hGlobal16);
1234 if (!template)
1236 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
1237 ERR("global lock failure for %x handle\n", hGlobal16);
1238 GlobalFree16(hGlobal16);
1239 return 0;
1241 ConvertDialog32To16((LPVOID)template32, size, (LPVOID)template);
1242 GlobalUnlock16(hGlobal16);
1243 return hGlobal16;
1246 /************************************************************
1248 * PRINTDLG_GetDlgTemplate
1251 static HGLOBAL PRINTDLG_GetDlgTemplate(PRINTDLGA *lppd)
1253 HGLOBAL hDlgTmpl, hResInfo;
1255 if (lppd->Flags & PD_PRINTSETUP) {
1256 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1257 hDlgTmpl = lppd->hSetupTemplate;
1258 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1259 hResInfo = FindResourceA(lppd->hInstance,
1260 lppd->lpSetupTemplateName, RT_DIALOGA);
1261 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1262 } else {
1263 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32_SETUP",
1264 RT_DIALOGA);
1265 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1267 } else {
1268 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1269 hDlgTmpl = lppd->hPrintTemplate;
1270 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1271 hResInfo = FindResourceA(lppd->hInstance,
1272 lppd->lpPrintTemplateName,
1273 RT_DIALOGA);
1274 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1275 } else {
1276 hResInfo = FindResourceA(COMDLG32_hInstance, "PRINT32",
1277 RT_DIALOGA);
1278 hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo);
1281 return hDlgTmpl;
1285 /************************************************************
1287 * PRINTDLG_GetDlgTemplate
1290 static HGLOBAL16 PRINTDLG_GetDlgTemplate16(PRINTDLG16 *lppd)
1292 HGLOBAL16 hDlgTmpl, hResInfo;
1294 if (lppd->Flags & PD_PRINTSETUP) {
1295 if(lppd->Flags & PD_ENABLESETUPTEMPLATEHANDLE) {
1296 hDlgTmpl = lppd->hSetupTemplate;
1297 } else if(lppd->Flags & PD_ENABLESETUPTEMPLATE) {
1298 hResInfo = FindResource16(lppd->hInstance,
1299 MapSL(lppd->lpSetupTemplateName), RT_DIALOGA);
1300 hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1301 } else {
1302 hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32_SETUP");
1304 } else {
1305 if(lppd->Flags & PD_ENABLEPRINTTEMPLATEHANDLE) {
1306 hDlgTmpl = lppd->hPrintTemplate;
1307 } else if(lppd->Flags & PD_ENABLEPRINTTEMPLATE) {
1308 hResInfo = FindResource16(lppd->hInstance,
1309 MapSL(lppd->lpPrintTemplateName),
1310 RT_DIALOGA);
1311 hDlgTmpl = LoadResource16(lppd->hInstance, hResInfo);
1312 } else {
1313 hDlgTmpl = PRINTDLG_Get16TemplateFrom32("PRINT32");
1316 return hDlgTmpl;
1319 /***********************************************************************
1321 * PRINTDLG_CreateDC
1324 static BOOL PRINTDLG_CreateDC(LPPRINTDLGA lppd)
1326 DEVNAMES *pdn = GlobalLock(lppd->hDevNames);
1327 DEVMODEA *pdm = GlobalLock(lppd->hDevMode);
1329 if(lppd->Flags & PD_RETURNDC) {
1330 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1331 (char*)pdn + pdn->wDeviceOffset,
1332 (char*)pdn + pdn->wOutputOffset,
1333 pdm );
1334 } else if(lppd->Flags & PD_RETURNIC) {
1335 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1336 (char*)pdn + pdn->wDeviceOffset,
1337 (char*)pdn + pdn->wOutputOffset,
1338 pdm );
1340 GlobalUnlock(lppd->hDevNames);
1341 GlobalUnlock(lppd->hDevMode);
1342 return lppd->hDC ? TRUE : FALSE;
1345 static BOOL PRINTDLG_CreateDC16(LPPRINTDLG16 lppd)
1347 DEVNAMES *pdn = GlobalLock16(lppd->hDevNames);
1348 DEVMODEA *pdm = GlobalLock16(lppd->hDevMode);
1350 if(lppd->Flags & PD_RETURNDC) {
1351 lppd->hDC = CreateDCA((char*)pdn + pdn->wDriverOffset,
1352 (char*)pdn + pdn->wDeviceOffset,
1353 (char*)pdn + pdn->wOutputOffset,
1354 pdm );
1355 } else if(lppd->Flags & PD_RETURNIC) {
1356 lppd->hDC = CreateICA((char*)pdn + pdn->wDriverOffset,
1357 (char*)pdn + pdn->wDeviceOffset,
1358 (char*)pdn + pdn->wOutputOffset,
1359 pdm );
1361 GlobalUnlock16(lppd->hDevNames);
1362 GlobalUnlock16(lppd->hDevMode);
1363 return lppd->hDC ? TRUE : FALSE;
1366 /***********************************************************************
1367 * PrintDlgA (COMDLG32.@)
1369 * Displays the the PRINT dialog box, which enables the user to specify
1370 * specific properties of the print job.
1372 * RETURNS
1373 * nonzero if the user pressed the OK button
1374 * zero if the user cancelled the window or an error occurred
1376 * BUGS
1377 * PrintDlg:
1378 * * The Collate Icons do not display, even though they are in the code.
1379 * * The Properties Button(s) should call DocumentPropertiesA().
1380 * PrintSetupDlg:
1381 * * The Paper Orientation Icons are not implemented yet.
1382 * * The Properties Button(s) should call DocumentPropertiesA().
1383 * * Settings are not yet taken from a provided DevMode or
1384 * default printer settings.
1386 BOOL WINAPI PrintDlgA(
1387 LPPRINTDLGA lppd /* [in/out] ptr to PRINTDLG32 struct */
1390 BOOL bRet = FALSE;
1391 LPVOID ptr;
1392 HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1394 if(TRACE_ON(commdlg)) {
1395 char flagstr[1000] = "";
1396 struct pd_flags *pflag = pd_flags;
1397 for( ; pflag->name; pflag++) {
1398 if(lppd->Flags & pflag->flag)
1399 strcat(flagstr, pflag->name);
1401 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1402 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1403 "flags %08lx (%s)\n",
1404 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1405 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1406 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1409 if(lppd->lStructSize != sizeof(PRINTDLGA)) {
1410 WARN("structure size failure !!!\n");
1411 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1412 return FALSE;
1415 if(lppd->Flags & PD_RETURNDEFAULT) {
1416 PRINTER_INFO_2A *pbuf;
1417 DRIVER_INFO_3A *dbuf;
1418 HANDLE hprn;
1419 DWORD needed;
1421 if(lppd->hDevMode || lppd->hDevNames) {
1422 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1423 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1424 return FALSE;
1426 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1427 WARN("Can't find default printer\n");
1428 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
1429 return FALSE;
1432 GetPrinterA(hprn, 2, NULL, 0, &needed);
1433 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1434 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1436 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1437 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1438 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1439 ERR("GetPrinterDriverA failed, le %ld, fix your config for printer %s!\n",GetLastError(),pbuf->pPrinterName);
1440 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1441 return FALSE;
1443 ClosePrinter(hprn);
1445 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1446 dbuf->pDriverPath,
1447 pbuf->pPrinterName,
1448 pbuf->pPortName);
1449 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, pbuf->pDevMode->dmSize +
1450 pbuf->pDevMode->dmDriverExtra);
1451 ptr = GlobalLock(lppd->hDevMode);
1452 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1453 pbuf->pDevMode->dmDriverExtra);
1454 GlobalUnlock(lppd->hDevMode);
1455 HeapFree(GetProcessHeap(), 0, pbuf);
1456 HeapFree(GetProcessHeap(), 0, dbuf);
1457 bRet = TRUE;
1458 } else {
1459 HGLOBAL hDlgTmpl;
1460 PRINT_PTRA *PrintStructures;
1462 /* load Dialog resources,
1463 * depending on Flags indicates Print32 or Print32_setup dialog
1465 hDlgTmpl = PRINTDLG_GetDlgTemplate(lppd);
1466 if (!hDlgTmpl) {
1467 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1468 return FALSE;
1470 ptr = LockResource( hDlgTmpl );
1471 if (!ptr) {
1472 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1473 return FALSE;
1476 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1477 sizeof(PRINT_PTRA));
1478 PrintStructures->dlg.lpPrintDlg = lppd;
1480 /* and create & process the dialog .
1481 * -1 is failure, 0 is broken hwnd, everything else is ok.
1483 bRet = (0<DialogBoxIndirectParamA(hInst, ptr, lppd->hwndOwner,
1484 PrintDlgProcA,
1485 (LPARAM)PrintStructures));
1487 if(bRet) {
1488 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1489 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1490 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1492 if (lppd->hDevMode == 0) {
1493 TRACE(" No hDevMode yet... Need to create my own\n");
1494 lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE,
1495 lpdm->dmSize + lpdm->dmDriverExtra);
1496 } else {
1497 WORD locks;
1498 if((locks = (GlobalFlags(lppd->hDevMode) & GMEM_LOCKCOUNT))) {
1499 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1500 while(locks--) {
1501 GlobalUnlock(lppd->hDevMode);
1502 TRACE("Now got %d locks\n", locks);
1505 lppd->hDevMode = GlobalReAlloc(lppd->hDevMode,
1506 lpdm->dmSize + lpdm->dmDriverExtra,
1507 GMEM_MOVEABLE);
1509 lpdmReturn = GlobalLock(lppd->hDevMode);
1510 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1512 if (lppd->hDevNames != 0) {
1513 WORD locks;
1514 if((locks = (GlobalFlags(lppd->hDevNames) & GMEM_LOCKCOUNT))) {
1515 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1516 while(locks--)
1517 GlobalUnlock(lppd->hDevNames);
1520 PRINTDLG_CreateDevNames(&(lppd->hDevNames),
1521 di->pDriverPath,
1522 pi->pPrinterName,
1523 pi->pPortName
1525 GlobalUnlock(lppd->hDevMode);
1527 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1528 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1529 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1530 HeapFree(GetProcessHeap(), 0, PrintStructures);
1532 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1533 bRet = PRINTDLG_CreateDC(lppd);
1535 TRACE("exit! (%d)\n", bRet);
1536 return bRet;
1539 /***********************************************************************
1540 * PrintDlg (COMMDLG.20)
1542 * Displays the the PRINT dialog box, which enables the user to specify
1543 * specific properties of the print job.
1545 * RETURNS
1546 * nonzero if the user pressed the OK button
1547 * zero if the user cancelled the window or an error occurred
1549 * BUGS
1550 * * calls up to the 32-bit versions of the Dialogs, which look different
1551 * * Customizing is *not* implemented.
1554 BOOL16 WINAPI PrintDlg16(
1555 LPPRINTDLG16 lppd /* [in/out] ptr to PRINTDLG struct */
1557 BOOL bRet = FALSE;
1558 LPVOID ptr;
1559 HINSTANCE hInst = GetWindowLongA( lppd->hwndOwner, GWL_HINSTANCE );
1561 if(TRACE_ON(commdlg)) {
1562 char flagstr[1000] = "";
1563 struct pd_flags *pflag = pd_flags;
1564 for( ; pflag->name; pflag++) {
1565 if(lppd->Flags & pflag->flag)
1566 strcat(flagstr, pflag->name);
1568 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
1569 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
1570 "flags %08lx (%s)\n",
1571 lppd, lppd->hwndOwner, lppd->hDevMode, lppd->hDevNames,
1572 lppd->nFromPage, lppd->nToPage, lppd->nMinPage, lppd->nMaxPage,
1573 lppd->nCopies, lppd->hInstance, lppd->Flags, flagstr);
1576 if(lppd->lStructSize != sizeof(PRINTDLG16)) {
1577 ERR("structure size (%ld/%d)\n",lppd->lStructSize,sizeof(PRINTDLG16));
1578 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE);
1579 return FALSE;
1582 if(lppd->Flags & PD_RETURNDEFAULT) {
1583 PRINTER_INFO_2A *pbuf;
1584 DRIVER_INFO_3A *dbuf;
1585 HANDLE hprn;
1586 DWORD needed;
1588 if(lppd->hDevMode || lppd->hDevNames) {
1589 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
1590 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1591 return FALSE;
1593 if(!PRINTDLG_OpenDefaultPrinter(&hprn)) {
1594 WARN("Can't find default printer\n");
1595 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN);
1596 return FALSE;
1599 GetPrinterA(hprn, 2, NULL, 0, &needed);
1600 pbuf = HeapAlloc(GetProcessHeap(), 0, needed);
1601 GetPrinterA(hprn, 2, (LPBYTE)pbuf, needed, &needed);
1602 GetPrinterDriverA(hprn, NULL, 3, NULL, 0, &needed);
1603 dbuf = HeapAlloc(GetProcessHeap(),0,needed);
1604 if (!GetPrinterDriverA(hprn, NULL, 3, (LPBYTE)dbuf, needed, &needed)) {
1605 ERR("GetPrinterDriverA failed for %s, le %ld, fix your config!\n",
1606 pbuf->pPrinterName,GetLastError());
1607 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE);
1608 return FALSE;
1610 ClosePrinter(hprn);
1611 PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1612 dbuf->pDriverPath,
1613 pbuf->pPrinterName,
1614 pbuf->pPortName);
1615 lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,pbuf->pDevMode->dmSize+
1616 pbuf->pDevMode->dmDriverExtra);
1617 ptr = GlobalLock16(lppd->hDevMode);
1618 memcpy(ptr, pbuf->pDevMode, pbuf->pDevMode->dmSize +
1619 pbuf->pDevMode->dmDriverExtra);
1620 GlobalUnlock16(lppd->hDevMode);
1621 HeapFree(GetProcessHeap(), 0, pbuf);
1622 HeapFree(GetProcessHeap(), 0, dbuf);
1623 bRet = TRUE;
1624 } else {
1625 HGLOBAL hDlgTmpl;
1626 PRINT_PTRA *PrintStructures;
1628 /* load Dialog resources,
1629 * depending on Flags indicates Print32 or Print32_setup dialog
1631 hDlgTmpl = PRINTDLG_GetDlgTemplate16(lppd);
1632 if (!hDlgTmpl) {
1633 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1634 return FALSE;
1636 PrintStructures = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1637 sizeof(PRINT_PTRA));
1638 PrintStructures->dlg.lpPrintDlg16 = lppd;
1639 PrintStructures->dlg.lpPrintDlg = (LPPRINTDLGA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(PRINTDLGA));
1640 #define CVAL(x) PrintStructures->dlg.lpPrintDlg->x = lppd->x;
1641 #define MVAL(x) PrintStructures->dlg.lpPrintDlg->x = MapSL(lppd->x);
1642 CVAL(Flags);CVAL(hwndOwner);CVAL(hDC);
1643 CVAL(nFromPage);CVAL(nToPage);CVAL(nMinPage);CVAL(nMaxPage);
1644 CVAL(nCopies);CVAL(hInstance);CVAL(lCustData);
1645 MVAL(lpPrintTemplateName);MVAL(lpSetupTemplateName);
1646 /* Don't copy rest, it is 16 bit specific */
1647 #undef MVAL
1648 #undef CVAL
1650 PrintStructures->lpDevMode = HeapAlloc(GetProcessHeap(),0,sizeof(DEVMODEA));
1652 /* and create & process the dialog .
1653 * -1 is failure, 0 is broken hwnd, everything else is ok.
1655 bRet = (0<DialogBoxIndirectParam16(
1656 hInst, hDlgTmpl, lppd->hwndOwner,
1657 (DLGPROC16)GetProcAddress16(GetModuleHandle16("COMMDLG"),(LPCSTR)21),
1658 (LPARAM)PrintStructures
1661 if (!PrintStructures->lpPrinterInfo) bRet = FALSE;
1662 if(bRet) {
1663 DEVMODEA *lpdm = PrintStructures->lpDevMode, *lpdmReturn;
1664 PRINTER_INFO_2A *pi = PrintStructures->lpPrinterInfo;
1665 DRIVER_INFO_3A *di = PrintStructures->lpDriverInfo;
1667 if (lppd->hDevMode == 0) {
1668 TRACE(" No hDevMode yet... Need to create my own\n");
1669 lppd->hDevMode = GlobalAlloc16(GMEM_MOVEABLE,
1670 lpdm->dmSize + lpdm->dmDriverExtra);
1671 } else {
1672 WORD locks;
1673 if((locks = (GlobalFlags16(lppd->hDevMode)&GMEM_LOCKCOUNT))) {
1674 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks);
1675 while(locks--) {
1676 GlobalUnlock16(lppd->hDevMode);
1677 TRACE("Now got %d locks\n", locks);
1680 lppd->hDevMode = GlobalReAlloc16(lppd->hDevMode,
1681 lpdm->dmSize + lpdm->dmDriverExtra,
1682 GMEM_MOVEABLE);
1684 lpdmReturn = GlobalLock16(lppd->hDevMode);
1685 memcpy(lpdmReturn, lpdm, lpdm->dmSize + lpdm->dmDriverExtra);
1687 if (lppd->hDevNames != 0) {
1688 WORD locks;
1689 if((locks = (GlobalFlags16(lppd->hDevNames)&GMEM_LOCKCOUNT))) {
1690 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks);
1691 while(locks--)
1692 GlobalUnlock16(lppd->hDevNames);
1695 PRINTDLG_CreateDevNames16(&(lppd->hDevNames),
1696 di->pDriverPath,
1697 pi->pPrinterName,
1698 pi->pPortName
1700 GlobalUnlock16(lppd->hDevMode);
1702 if (!(lppd->Flags & (PD_ENABLESETUPTEMPLATEHANDLE | PD_ENABLESETUPTEMPLATE)))
1703 GlobalFree16(hDlgTmpl); /* created from the 32 bits resource */
1704 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDevMode);
1705 HeapFree(GetProcessHeap(), 0, PrintStructures->lpPrinterInfo);
1706 HeapFree(GetProcessHeap(), 0, PrintStructures->lpDriverInfo);
1707 HeapFree(GetProcessHeap(), 0, PrintStructures);
1709 if(bRet && (lppd->Flags & PD_RETURNDC || lppd->Flags & PD_RETURNIC))
1710 bRet = PRINTDLG_CreateDC16(lppd);
1712 TRACE("exit! (%d)\n", bRet);
1713 return bRet;
1718 /***********************************************************************
1719 * PrintDlgW (COMDLG32.@)
1721 BOOL WINAPI PrintDlgW( LPPRINTDLGW printdlg )
1723 FIXME("A really empty stub\n" );
1724 return FALSE;
1727 /***********************************************************************
1729 * PageSetupDlg
1730 * rad1 - portrait
1731 * rad2 - landscape
1732 * cmb2 - paper size
1733 * cmb3 - source (tray?)
1734 * edt4 - border left
1735 * edt5 - border top
1736 * edt6 - border right
1737 * edt7 - border bottom
1738 * psh3 - "Printer..."
1741 typedef struct {
1742 LPPAGESETUPDLGA dlga;
1744 PRINTDLGA pdlg;
1745 } PageSetupData;
1747 static HGLOBAL PRINTDLG_GetPGSTemplate(PAGESETUPDLGA *lppd)
1749 HGLOBAL hDlgTmpl, hResInfo;
1751 if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATEHANDLE) {
1752 hDlgTmpl = lppd->hPageSetupTemplate;
1753 } else if(lppd->Flags & PSD_ENABLEPAGESETUPTEMPLATE) {
1754 hResInfo = FindResourceA(lppd->hInstance,
1755 lppd->lpPageSetupTemplateName, RT_DIALOGA);
1756 hDlgTmpl = LoadResource(lppd->hInstance, hResInfo);
1757 } else {
1758 hResInfo = FindResourceA(COMDLG32_hInstance,(LPCSTR)PAGESETUPDLGORD,RT_DIALOGA);
1759 hDlgTmpl = LoadResource(COMDLG32_hInstance,hResInfo);
1761 return hDlgTmpl;
1764 static DWORD
1765 _c_10mm2size(PAGESETUPDLGA *dlga,DWORD size) {
1766 if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1767 return 10*size*10/25.4;
1768 /* If we don't have a flag, we can choose one. Use millimeters
1769 * to avoid confusing me
1771 dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1772 FIXME("returning %ld/100 mm \n",size);
1773 return 10*size;
1777 static DWORD
1778 _c_inch2size(PAGESETUPDLGA *dlga,DWORD size) {
1779 if (dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1780 return size;
1781 if (dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1782 return (size*254)/10;
1783 /* if we don't have a flag, we can choose one. Use millimeters
1784 * to avoid confusing me
1786 dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1787 return (size*254)/10;
1790 static void
1791 _c_size2str(PageSetupData *pda,DWORD size,LPSTR strout) {
1792 strcpy(strout,"<undef>");
1793 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1794 sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1795 return;
1797 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1798 sprintf(strout,"%.2fin",(size*1.0)/1000.0);
1799 return;
1801 pda->dlga->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
1802 sprintf(strout,"%.2fmm",(size*1.0)/100.0);
1803 return;
1806 static DWORD
1807 _c_str2size(PageSetupData *pda,LPCSTR strin) {
1808 float val;
1809 char rest[200];
1811 rest[0]='\0';
1812 if (!sscanf(strin,"%f%s",&val,rest))
1813 return 0;
1815 if (!strcmp(rest,"in") || !strcmp(rest,"inch")) {
1816 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES)
1817 return 1000*val;
1818 else
1819 return val*25.4*100;
1821 if (!strcmp(rest,"cm")) { rest[0]='m'; val = val*10.0; }
1822 if (!strcmp(rest,"m")) { strcpy(rest,"mm"); val = val*1000.0; }
1824 if (!strcmp(rest,"mm")) {
1825 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
1826 return 100*val;
1827 else
1828 return 1000.0*val/25.4;
1830 if (rest[0]=='\0') {
1831 /* use application supplied default */
1832 if (pda->dlga->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) {
1833 /* 100*mm */
1834 return 100.0*val;
1836 if (pda->dlga->Flags & PSD_INTHOUSANDTHSOFINCHES) {
1837 /* 1000*inch */
1838 return 1000.0*val;
1841 ERR("Did not find a conversion for type '%s'!\n",rest);
1842 return 0;
1847 * This is called on finish and will update the output fields of the
1848 * struct.
1850 static BOOL
1851 PRINTDLG_PS_UpdateDlgStruct(HWND hDlg, PageSetupData *pda) {
1852 DEVNAMES *dn;
1853 DEVMODEA *dm;
1854 LPSTR devname,portname;
1855 char papername[64];
1856 char buf[200];
1858 dn = GlobalLock(pda->pdlg.hDevNames);
1859 dm = GlobalLock(pda->pdlg.hDevMode);
1860 devname = ((char*)dn)+dn->wDeviceOffset;
1861 portname = ((char*)dn)+dn->wOutputOffset;
1862 PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1863 PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1865 if (GetDlgItemTextA(hDlg,cmb2,papername,sizeof(papername))>0) {
1866 PRINTDLG_PaperSize(&(pda->pdlg),papername,&(pda->dlga->ptPaperSize));
1867 pda->dlga->ptPaperSize.x = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.x);
1868 pda->dlga->ptPaperSize.y = _c_10mm2size(pda->dlga,pda->dlga->ptPaperSize.y);
1869 } else
1870 FIXME("could not get dialog text for papersize cmbbox?\n");
1871 #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); }
1872 GETVAL(edt4,pda->dlga->rtMargin.left);
1873 GETVAL(edt5,pda->dlga->rtMargin.top);
1874 GETVAL(edt6,pda->dlga->rtMargin.right);
1875 GETVAL(edt7,pda->dlga->rtMargin.bottom);
1876 #undef GETVAL
1878 /* If we are in landscape, swap x and y of page size */
1879 if (IsDlgButtonChecked(hDlg, rad2)) {
1880 DWORD tmp;
1881 tmp = pda->dlga->ptPaperSize.x;
1882 pda->dlga->ptPaperSize.x = pda->dlga->ptPaperSize.y;
1883 pda->dlga->ptPaperSize.y = tmp;
1885 GlobalUnlock(pda->pdlg.hDevNames);
1886 GlobalUnlock(pda->pdlg.hDevMode);
1887 return TRUE;
1891 * This is called after returning from PrintDlg().
1893 static BOOL
1894 PRINTDLG_PS_ChangePrinter(HWND hDlg, PageSetupData *pda) {
1895 DEVNAMES *dn;
1896 DEVMODEA *dm;
1897 LPSTR devname,portname;
1899 dn = GlobalLock(pda->pdlg.hDevNames);
1900 dm = GlobalLock(pda->pdlg.hDevMode);
1901 devname = ((char*)dn)+dn->wDeviceOffset;
1902 portname = ((char*)dn)+dn->wOutputOffset;
1903 PRINTDLG_SetUpPaperComboBox(hDlg,cmb2,devname,portname,dm);
1904 PRINTDLG_SetUpPaperComboBox(hDlg,cmb3,devname,portname,dm);
1905 GlobalUnlock(pda->pdlg.hDevNames);
1906 GlobalUnlock(pda->pdlg.hDevMode);
1907 return TRUE;
1910 static BOOL
1911 PRINTDLG_PS_WMCommand(
1912 HWND hDlg, WPARAM wParam, LPARAM lParam, PageSetupData *pda
1914 switch (LOWORD(wParam)) {
1915 case IDOK:
1916 if (!PRINTDLG_PS_UpdateDlgStruct(hDlg, pda))
1917 return(FALSE);
1918 EndDialog(hDlg, TRUE);
1919 return TRUE ;
1921 case IDCANCEL:
1922 EndDialog(hDlg, FALSE);
1923 return FALSE ;
1925 case psh3: {
1926 pda->pdlg.Flags = 0;
1927 pda->pdlg.hwndOwner = hDlg;
1928 if (PrintDlgA(&(pda->pdlg)))
1929 PRINTDLG_PS_ChangePrinter(hDlg,pda);
1930 return TRUE;
1933 FIXME("loword (lparam) %d, wparam 0x%x, lparam %08lx, STUB mostly.\n",
1934 LOWORD(lParam),wParam,lParam
1936 return FALSE;
1940 static BOOL WINAPI
1941 PageDlgProcA(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1943 PageSetupData *pda;
1944 BOOL res = FALSE;
1946 if (uMsg==WM_INITDIALOG) {
1947 res = TRUE;
1948 pda = (PageSetupData*)lParam;
1949 SetPropA(hDlg,"__WINE_PAGESETUPDLGDATA",lParam);
1950 if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
1951 res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
1952 if (!res) {
1953 FIXME("Setup page hook failed?\n");
1954 res = TRUE;
1957 if (pda->dlga->Flags & PSD_ENABLEPAGEPAINTHOOK) {
1958 FIXME("PagePaintHook not yet implemented!\n");
1960 if (pda->dlga->Flags & PSD_DISABLEPRINTER)
1961 EnableWindow(GetDlgItem(hDlg, psh3), FALSE);
1962 if (pda->dlga->Flags & PSD_DISABLEMARGINS) {
1963 EnableWindow(GetDlgItem(hDlg, edt4), FALSE);
1964 EnableWindow(GetDlgItem(hDlg, edt5), FALSE);
1965 EnableWindow(GetDlgItem(hDlg, edt6), FALSE);
1966 EnableWindow(GetDlgItem(hDlg, edt7), FALSE);
1968 /* width larger as height -> landscape */
1969 if (pda->dlga->ptPaperSize.x > pda->dlga->ptPaperSize.y)
1970 CheckRadioButton(hDlg, rad1, rad2, rad2);
1971 else /* this is default if papersize is not set */
1972 CheckRadioButton(hDlg, rad1, rad2, rad1);
1973 if (pda->dlga->Flags & PSD_DISABLEORIENTATION) {
1974 EnableWindow(GetDlgItem(hDlg,rad1),FALSE);
1975 EnableWindow(GetDlgItem(hDlg,rad2),FALSE);
1977 /* We fill them out enabled or not */
1978 if (pda->dlga->Flags & PSD_MARGINS) {
1979 char str[100];
1980 _c_size2str(pda,pda->dlga->rtMargin.left,str);
1981 SetDlgItemTextA(hDlg,edt4,str);
1982 _c_size2str(pda,pda->dlga->rtMargin.top,str);
1983 SetDlgItemTextA(hDlg,edt5,str);
1984 _c_size2str(pda,pda->dlga->rtMargin.right,str);
1985 SetDlgItemTextA(hDlg,edt6,str);
1986 _c_size2str(pda,pda->dlga->rtMargin.bottom,str);
1987 SetDlgItemTextA(hDlg,edt7,str);
1988 } else {
1989 /* default is 1 inch */
1990 DWORD size = _c_inch2size(pda->dlga,1000);
1991 char str[20];
1992 _c_size2str(pda,size,str);
1993 SetDlgItemTextA(hDlg,edt4,str);
1994 SetDlgItemTextA(hDlg,edt5,str);
1995 SetDlgItemTextA(hDlg,edt6,str);
1996 SetDlgItemTextA(hDlg,edt7,str);
1998 PRINTDLG_PS_ChangePrinter(hDlg,pda);
1999 if (pda->dlga->Flags & PSD_DISABLEPAPER) {
2000 EnableWindow(GetDlgItem(hDlg,cmb2),FALSE);
2001 EnableWindow(GetDlgItem(hDlg,cmb3),FALSE);
2003 return TRUE;
2004 } else {
2005 pda = (PageSetupData*)GetPropA(hDlg,"__WINE_PAGESETUPDLGDATA");
2006 if (!pda) {
2007 WARN("__WINE_PAGESETUPDLGDATA prop not set?\n");
2008 return FALSE;
2010 if (pda->dlga->Flags & PSD_ENABLEPAGESETUPHOOK) {
2011 res = pda->dlga->lpfnPageSetupHook(hDlg,uMsg,wParam,lParam);
2012 if (res) return res;
2015 switch (uMsg) {
2016 case WM_COMMAND:
2017 return PRINTDLG_PS_WMCommand(hDlg, wParam, lParam, pda);
2019 return FALSE;
2022 /***********************************************************************
2023 * PageSetupDlgA (COMDLG32.@)
2025 BOOL WINAPI PageSetupDlgA(LPPAGESETUPDLGA setupdlg) {
2026 HGLOBAL hDlgTmpl;
2027 LPVOID ptr;
2028 BOOL bRet;
2029 PageSetupData *pda;
2030 PRINTDLGA pdlg;
2032 if(TRACE_ON(commdlg)) {
2033 char flagstr[1000] = "";
2034 struct pd_flags *pflag = psd_flags;
2035 for( ; pflag->name; pflag++) {
2036 if(setupdlg->Flags & pflag->flag) {
2037 strcat(flagstr, pflag->name);
2038 strcat(flagstr, "|");
2041 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
2042 "hinst %08x, flags %08lx (%s)\n",
2043 setupdlg, setupdlg->hwndOwner, setupdlg->hDevMode,
2044 setupdlg->hDevNames,
2045 setupdlg->hInstance, setupdlg->Flags, flagstr);
2048 /* First get default printer data, we need it right after that. */
2049 memset(&pdlg,0,sizeof(pdlg));
2050 pdlg.lStructSize = sizeof(pdlg);
2051 pdlg.Flags = PD_RETURNDEFAULT;
2052 bRet = PrintDlgA(&pdlg);
2053 if (!bRet) return FALSE;
2055 /* short cut exit, just return default values */
2056 if (setupdlg->Flags & PSD_RETURNDEFAULT) {
2057 setupdlg->hDevMode = pdlg.hDevMode;
2058 setupdlg->hDevNames = pdlg.hDevNames;
2059 /* FIXME: Just return "A4" for now. */
2060 PRINTDLG_PaperSize(&pdlg,"A4",&setupdlg->ptPaperSize);
2061 setupdlg->ptPaperSize.x=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.x);
2062 setupdlg->ptPaperSize.y=_c_10mm2size(setupdlg,setupdlg->ptPaperSize.y);
2063 return TRUE;
2065 hDlgTmpl = PRINTDLG_GetPGSTemplate(setupdlg);
2066 if (!hDlgTmpl) {
2067 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2068 return FALSE;
2070 ptr = LockResource( hDlgTmpl );
2071 if (!ptr) {
2072 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
2073 return FALSE;
2075 pda = HeapAlloc(GetProcessHeap(),0,sizeof(*pda));
2076 pda->dlga = setupdlg;
2077 memcpy(&pda->pdlg,&pdlg,sizeof(pdlg));
2079 bRet = (0<DialogBoxIndirectParamA(
2080 setupdlg->hInstance,
2081 ptr,
2082 setupdlg->hwndOwner,
2083 PageDlgProcA,
2084 (LPARAM)pda)
2086 return bRet;
2088 /***********************************************************************
2089 * PageSetupDlgW (COMDLG32.@)
2091 BOOL WINAPI PageSetupDlgW(LPPAGESETUPDLGW setupdlg) {
2092 FIXME("(%p), stub!\n",setupdlg);
2093 return FALSE;
2096 /**********************************************************************
2098 * 16 bit commdlg
2101 /***********************************************************************
2102 * PrintDlgProc (COMMDLG.21)
2104 LRESULT WINAPI PrintDlgProc16(HWND16 hDlg, UINT16 uMsg, WPARAM16 wParam,
2105 LPARAM lParam)
2107 PRINT_PTRA* PrintStructures;
2108 LRESULT res=FALSE;
2110 if (uMsg!=WM_INITDIALOG) {
2111 PrintStructures = (PRINT_PTRA*) GetWindowLongA(hDlg, DWL_USER);
2112 if (!PrintStructures)
2113 return FALSE;
2114 } else {
2115 PrintStructures = (PRINT_PTRA*) lParam;
2116 SetWindowLongA(hDlg, DWL_USER, lParam);
2117 res = PRINTDLG_WMInitDialog16(hDlg, wParam, PrintStructures);
2119 if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2120 res = CallWindowProc16(
2121 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2122 hDlg, uMsg, wParam, (LPARAM)PrintStructures->dlg.lpPrintDlg16
2125 return res;
2128 if(PrintStructures->dlg.lpPrintDlg16->Flags & PD_ENABLEPRINTHOOK) {
2129 res = CallWindowProc16(
2130 (WNDPROC16)PrintStructures->dlg.lpPrintDlg16->lpfnPrintHook,
2131 hDlg,uMsg, wParam, lParam
2133 if(LOWORD(res)) return res;
2136 switch (uMsg) {
2137 case WM_COMMAND: {
2138 /* We need to map those for the 32bit window procedure, compare
2139 * with 32Ato16 mapper in winproc.c
2141 return PRINTDLG_WMCommand(
2142 hDlg,
2143 MAKEWPARAM(wParam,HIWORD(lParam)),
2144 LOWORD(lParam),
2145 PrintStructures
2148 case WM_DESTROY:
2149 DestroyIcon(PrintStructures->hCollateIcon);
2150 DestroyIcon(PrintStructures->hNoCollateIcon);
2151 /* FIXME: don't forget to delete the paper orientation icons here! */
2153 return FALSE;
2155 return res;
2159 /***********************************************************************
2160 * PrintSetupDlgProc (COMMDLG.22)
2162 LRESULT WINAPI PrintSetupDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
2163 LPARAM lParam)
2165 switch (wMsg)
2167 case WM_INITDIALOG:
2168 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
2169 ShowWindow(hWnd, SW_SHOWNORMAL);
2170 return (TRUE);
2171 case WM_COMMAND:
2172 switch (wParam) {
2173 case IDOK:
2174 EndDialog(hWnd, TRUE);
2175 return(TRUE);
2176 case IDCANCEL:
2177 EndDialog(hWnd, FALSE);
2178 return(TRUE);
2180 return(FALSE);
2182 return FALSE;
2186 /***********************************************************************
2187 * PrintDlgExA (COMDLG32.@)
2189 HRESULT WINAPI PrintDlgExA(LPVOID lpPrintDlgExA) /* [???] FIXME: LPPRINTDLGEXA */
2191 FIXME("stub\n");
2192 return E_NOTIMPL;
2194 /***********************************************************************
2195 * PrintDlgExW (COMDLG32.@)
2197 HRESULT WINAPI PrintDlgExW(LPVOID lpPrintDlgExW) /* [???] FIXME: LPPRINTDLGEXW */
2199 FIXME("stub\n");
2200 return E_NOTIMPL;