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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
35 #include "wine/wingdi16.h"
37 #include "wine/winuser16.h"
40 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
51 static BOOL
PRINTDLG_CreateDevNames16(HGLOBAL16
*hmem
, char* DeviceDriverName
,
52 char* DeviceName
, char* OutputPort
)
57 LPDEVNAMES lpDevNames
;
60 size
= strlen(DeviceDriverName
) + 1
61 + strlen(DeviceName
) + 1
62 + strlen(OutputPort
) + 1
66 *hmem
= GlobalReAlloc16(*hmem
, size
, GMEM_MOVEABLE
);
68 *hmem
= GlobalAlloc16(GMEM_MOVEABLE
, size
);
72 pDevNamesSpace
= GlobalLock16(*hmem
);
73 lpDevNames
= (LPDEVNAMES
) pDevNamesSpace
;
75 pTempPtr
= pDevNamesSpace
+ sizeof(DEVNAMES
);
76 strcpy(pTempPtr
, DeviceDriverName
);
77 lpDevNames
->wDriverOffset
= pTempPtr
- pDevNamesSpace
;
79 pTempPtr
+= strlen(DeviceDriverName
) + 1;
80 strcpy(pTempPtr
, DeviceName
);
81 lpDevNames
->wDeviceOffset
= pTempPtr
- pDevNamesSpace
;
83 pTempPtr
+= strlen(DeviceName
) + 1;
84 strcpy(pTempPtr
, OutputPort
);
85 lpDevNames
->wOutputOffset
= pTempPtr
- pDevNamesSpace
;
87 PRINTDLG_GetDefaultPrinterNameA(buf
, sizeof(buf
));
88 lpDevNames
->wDefault
= (strcmp(buf
, DeviceName
) == 0) ? 1 : 0;
89 GlobalUnlock16(*hmem
);
94 /***********************************************************************
95 * PRINTDLG_WMInitDialog [internal]
97 static LRESULT
PRINTDLG_WMInitDialog16(HWND hDlg
, WPARAM wParam
,
98 PRINT_PTRA
* PrintStructures
)
100 LPPRINTDLG16 lppd
= PrintStructures
->dlg
.lpPrintDlg16
;
104 UINT comboID
= (lppd
->Flags
& PD_PRINTSETUP
) ? cmb1
: cmb4
;
106 /* load Collate ICONs */
107 PrintStructures
->hCollateIcon
=
108 LoadIconA(COMDLG32_hInstance
, "PD32_COLLATE");
109 PrintStructures
->hNoCollateIcon
=
110 LoadIconA(COMDLG32_hInstance
, "PD32_NOCOLLATE");
111 if(PrintStructures
->hCollateIcon
== 0 ||
112 PrintStructures
->hNoCollateIcon
== 0) {
113 ERR("no icon in resourcefile\n");
114 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
115 EndDialog(hDlg
, FALSE
);
118 /* load Paper Orientation ICON */
119 /* FIXME: not implemented yet */
122 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
123 * must be registered and the Help button must be shown.
125 if (lppd
->Flags
& PD_SHOWHELP
) {
126 if((PrintStructures
->HelpMessageID
=
127 RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0) {
128 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
132 PrintStructures
->HelpMessageID
= 0;
134 if (!(lppd
->Flags
& PD_PRINTSETUP
)) {
135 /* We have a print quality combo box. What shall we do? */
136 if (GetDlgItem(hDlg
,cmb1
)) {
139 FIXME("Print quality only displaying currently.\n");
141 pdm
= GlobalLock16(lppd
->hDevMode
);
143 switch (pdm
->dmPrintQuality
) {
144 case DMRES_HIGH
: strcpy(buf
,"High");break;
145 case DMRES_MEDIUM
: strcpy(buf
,"Medium");break;
146 case DMRES_LOW
: strcpy(buf
,"Low");break;
147 case DMRES_DRAFT
: strcpy(buf
,"Draft");break;
148 case 0 : strcpy(buf
,"Default");break;
149 default : sprintf(buf
,"%ddpi",pdm
->dmPrintQuality
);break;
151 GlobalUnlock16(lppd
->hDevMode
);
153 strcpy(buf
,"Default");
154 SendDlgItemMessageA(hDlg
,cmb1
,CB_ADDSTRING
,0,(LPARAM
)buf
);
155 SendDlgItemMessageA(hDlg
,cmb1
,CB_SETCURSEL
,0,0);
156 EnableWindow(GetDlgItem(hDlg
,cmb1
),FALSE
);
160 /* FIXME: I allow more freedom than either Win95 or WinNT,
161 * which do not agree to what errors should be thrown or not
162 * in case nToPage or nFromPage is out-of-range.
164 if (lppd
->nMaxPage
< lppd
->nMinPage
)
165 lppd
->nMaxPage
= lppd
->nMinPage
;
166 if (lppd
->nMinPage
== lppd
->nMaxPage
)
167 lppd
->Flags
|= PD_NOPAGENUMS
;
168 if (lppd
->nToPage
< lppd
->nMinPage
)
169 lppd
->nToPage
= lppd
->nMinPage
;
170 if (lppd
->nToPage
> lppd
->nMaxPage
)
171 lppd
->nToPage
= lppd
->nMaxPage
;
172 if (lppd
->nFromPage
< lppd
->nMinPage
)
173 lppd
->nFromPage
= lppd
->nMinPage
;
174 if (lppd
->nFromPage
> lppd
->nMaxPage
)
175 lppd
->nFromPage
= lppd
->nMaxPage
;
177 /* If the printer combo box is in the dialog, fill it */
178 if (GetDlgItem(hDlg
,comboID
)) {
181 pdn
= GlobalLock16(lppd
->hDevNames
);
182 pdm
= GlobalLock16(lppd
->hDevMode
);
184 name
= (char*)pdn
+ pdn
->wDeviceOffset
;
186 name
= pdm
->dmDeviceName
;
187 PRINTDLG_SetUpPrinterListComboA(hDlg
, comboID
, name
);
188 if(pdm
) GlobalUnlock16(lppd
->hDevMode
);
189 if(pdn
) GlobalUnlock16(lppd
->hDevNames
);
191 /* Now find selected printer and update rest of dlg */
192 name
= HeapAlloc(GetProcessHeap(),0,256);
193 if (GetDlgItemTextA(hDlg
, comboID
, name
, 255))
194 PRINTDLG_ChangePrinterA(hDlg
, name
, PrintStructures
);
196 /* else just use default printer */
198 BOOL ret
= PRINTDLG_GetDefaultPrinterNameA(name
, sizeof(name
));
201 PRINTDLG_ChangePrinterA(hDlg
, name
, PrintStructures
);
203 FIXME("No default printer found, expect problems!\n");
205 HeapFree(GetProcessHeap(),0,name
);
210 /************************************************************
212 * PRINTDLG_Get16TemplateFrom32 [Internal]
213 * Generates a 16 bits template from the Wine 32 bits resource
216 static HGLOBAL16
PRINTDLG_Get16TemplateFrom32(LPCSTR PrintResourceName
)
225 if (!(hResInfo
= FindResourceA(COMDLG32_hInstance
,
226 PrintResourceName
, (LPSTR
)RT_DIALOG
)))
228 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
231 if (!(hDlgTmpl32
= LoadResource(COMDLG32_hInstance
, hResInfo
)) ||
232 !(template32
= LockResource( hDlgTmpl32
)))
234 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
237 size
= SizeofResource(COMDLG32_hInstance
, hResInfo
);
238 hGlobal16
= GlobalAlloc16(0, size
);
241 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE
);
242 ERR("alloc failure for %ld bytes\n", size
);
245 template = GlobalLock16(hGlobal16
);
248 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE
);
249 ERR("global lock failure for %x handle\n", hGlobal16
);
250 GlobalFree16(hGlobal16
);
253 ConvertDialog32To16((LPVOID
)template32
, size
, (LPVOID
)template);
254 GlobalUnlock16(hGlobal16
);
258 static BOOL
PRINTDLG_CreateDC16(LPPRINTDLG16 lppd
)
260 DEVNAMES
*pdn
= GlobalLock16(lppd
->hDevNames
);
261 DEVMODEA
*pdm
= GlobalLock16(lppd
->hDevMode
);
263 if(lppd
->Flags
& PD_RETURNDC
) {
264 lppd
->hDC
= HDC_16(CreateDCA((char*)pdn
+ pdn
->wDriverOffset
,
265 (char*)pdn
+ pdn
->wDeviceOffset
,
266 (char*)pdn
+ pdn
->wOutputOffset
,
268 } else if(lppd
->Flags
& PD_RETURNIC
) {
269 lppd
->hDC
= HDC_16(CreateICA((char*)pdn
+ pdn
->wDriverOffset
,
270 (char*)pdn
+ pdn
->wDeviceOffset
,
271 (char*)pdn
+ pdn
->wOutputOffset
,
274 GlobalUnlock16(lppd
->hDevNames
);
275 GlobalUnlock16(lppd
->hDevMode
);
276 return lppd
->hDC
? TRUE
: FALSE
;
279 /************************************************************
281 * PRINTDLG_GetDlgTemplate
284 static HGLOBAL16
PRINTDLG_GetDlgTemplate16(PRINTDLG16
*lppd
)
286 HGLOBAL16 hDlgTmpl
, hResInfo
;
288 if (lppd
->Flags
& PD_PRINTSETUP
) {
289 if(lppd
->Flags
& PD_ENABLESETUPTEMPLATEHANDLE
) {
290 hDlgTmpl
= lppd
->hSetupTemplate
;
291 } else if(lppd
->Flags
& PD_ENABLESETUPTEMPLATE
) {
292 hResInfo
= FindResource16(lppd
->hInstance
,
293 MapSL(lppd
->lpSetupTemplateName
), (LPSTR
)RT_DIALOG
);
294 hDlgTmpl
= LoadResource16(lppd
->hInstance
, hResInfo
);
296 hDlgTmpl
= PRINTDLG_Get16TemplateFrom32("PRINT32_SETUP");
299 if(lppd
->Flags
& PD_ENABLEPRINTTEMPLATEHANDLE
) {
300 hDlgTmpl
= lppd
->hPrintTemplate
;
301 } else if(lppd
->Flags
& PD_ENABLEPRINTTEMPLATE
) {
302 hResInfo
= FindResource16(lppd
->hInstance
,
303 MapSL(lppd
->lpPrintTemplateName
),
305 hDlgTmpl
= LoadResource16(lppd
->hInstance
, hResInfo
);
307 hDlgTmpl
= PRINTDLG_Get16TemplateFrom32("PRINT32");
313 /***********************************************************************
314 * PrintDlg (COMMDLG.20)
316 * Displays the the PRINT dialog box, which enables the user to specify
317 * specific properties of the print job.
320 * nonzero if the user pressed the OK button
321 * zero if the user cancelled the window or an error occurred
324 * * calls up to the 32-bit versions of the Dialogs, which look different
325 * * Customizing is *not* implemented.
328 BOOL16 WINAPI
PrintDlg16(
329 LPPRINTDLG16 lppd
/* [in/out] ptr to PRINTDLG struct */
333 HINSTANCE16 hInst
= GetWindowWord( HWND_32(lppd
->hwndOwner
), GWL_HINSTANCE
);
335 if(TRACE_ON(commdlg
)) {
336 char flagstr
[1000] = "";
337 struct pd_flags
*pflag
= pd_flags
;
338 for( ; pflag
->name
; pflag
++) {
339 if(lppd
->Flags
& pflag
->flag
)
340 strcat(flagstr
, pflag
->name
);
342 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
343 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
344 "flags %08lx (%s)\n",
345 lppd
, lppd
->hwndOwner
, lppd
->hDevMode
, lppd
->hDevNames
,
346 lppd
->nFromPage
, lppd
->nToPage
, lppd
->nMinPage
, lppd
->nMaxPage
,
347 lppd
->nCopies
, lppd
->hInstance
, lppd
->Flags
, flagstr
);
350 if(lppd
->lStructSize
!= sizeof(PRINTDLG16
)) {
351 ERR("structure size (%ld/%d)\n",lppd
->lStructSize
,sizeof(PRINTDLG16
));
352 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
356 if(lppd
->Flags
& PD_RETURNDEFAULT
) {
357 PRINTER_INFO_2A
*pbuf
;
358 DRIVER_INFO_3A
*dbuf
;
362 if(lppd
->hDevMode
|| lppd
->hDevNames
) {
363 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
364 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE
);
367 if(!PRINTDLG_OpenDefaultPrinter(&hprn
)) {
368 WARN("Can't find default printer\n");
369 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN
);
373 GetPrinterA(hprn
, 2, NULL
, 0, &needed
);
374 pbuf
= HeapAlloc(GetProcessHeap(), 0, needed
);
375 GetPrinterA(hprn
, 2, (LPBYTE
)pbuf
, needed
, &needed
);
376 GetPrinterDriverA(hprn
, NULL
, 3, NULL
, 0, &needed
);
377 dbuf
= HeapAlloc(GetProcessHeap(),0,needed
);
378 if (!GetPrinterDriverA(hprn
, NULL
, 3, (LPBYTE
)dbuf
, needed
, &needed
)) {
379 ERR("GetPrinterDriverA failed for %s, le %ld, fix your config!\n",
380 pbuf
->pPrinterName
,GetLastError());
381 HeapFree(GetProcessHeap(), 0, dbuf
);
382 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE
);
386 PRINTDLG_CreateDevNames16(&(lppd
->hDevNames
),
390 lppd
->hDevMode
= GlobalAlloc16(GMEM_MOVEABLE
,pbuf
->pDevMode
->dmSize
+
391 pbuf
->pDevMode
->dmDriverExtra
);
392 ptr
= GlobalLock16(lppd
->hDevMode
);
393 memcpy(ptr
, pbuf
->pDevMode
, pbuf
->pDevMode
->dmSize
+
394 pbuf
->pDevMode
->dmDriverExtra
);
395 GlobalUnlock16(lppd
->hDevMode
);
396 HeapFree(GetProcessHeap(), 0, pbuf
);
397 HeapFree(GetProcessHeap(), 0, dbuf
);
401 PRINT_PTRA
*PrintStructures
;
403 /* load Dialog resources,
404 * depending on Flags indicates Print32 or Print32_setup dialog
406 hDlgTmpl
= PRINTDLG_GetDlgTemplate16(lppd
);
408 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
411 PrintStructures
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
413 PrintStructures
->dlg
.lpPrintDlg16
= lppd
;
414 PrintStructures
->dlg
.lpPrintDlg
= (LPPRINTDLGA
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(PRINTDLGA
));
415 #define CVAL(x) PrintStructures->dlg.lpPrintDlg->x = lppd->x;
416 #define MVAL(x) PrintStructures->dlg.lpPrintDlg->x = MapSL(lppd->x);
418 PrintStructures
->dlg
.lpPrintDlg
->hwndOwner
= HWND_32(lppd
->hwndOwner
);
419 PrintStructures
->dlg
.lpPrintDlg
->hDC
= HDC_32(lppd
->hDC
);
420 CVAL(nFromPage
);CVAL(nToPage
);CVAL(nMinPage
);CVAL(nMaxPage
);
422 PrintStructures
->dlg
.lpPrintDlg
->hInstance
= HINSTANCE_32(lppd
->hInstance
);
424 MVAL(lpPrintTemplateName
);MVAL(lpSetupTemplateName
);
425 /* Don't copy rest, it is 16 bit specific */
429 PrintStructures
->lpDevMode
= HeapAlloc(GetProcessHeap(),0,sizeof(DEVMODEA
));
431 /* and create & process the dialog .
432 * -1 is failure, 0 is broken hwnd, everything else is ok.
434 bRet
= (0<DialogBoxIndirectParam16(
435 hInst
, hDlgTmpl
, lppd
->hwndOwner
,
436 (DLGPROC16
)GetProcAddress16(GetModuleHandle16("COMMDLG"),(LPCSTR
)21),
437 (LPARAM
)PrintStructures
440 if (!PrintStructures
->lpPrinterInfo
) bRet
= FALSE
;
442 DEVMODEA
*lpdm
= PrintStructures
->lpDevMode
, *lpdmReturn
;
443 PRINTER_INFO_2A
*pi
= PrintStructures
->lpPrinterInfo
;
444 DRIVER_INFO_3A
*di
= PrintStructures
->lpDriverInfo
;
446 if (lppd
->hDevMode
== 0) {
447 TRACE(" No hDevMode yet... Need to create my own\n");
448 lppd
->hDevMode
= GlobalAlloc16(GMEM_MOVEABLE
,
449 lpdm
->dmSize
+ lpdm
->dmDriverExtra
);
452 if((locks
= (GlobalFlags16(lppd
->hDevMode
)&GMEM_LOCKCOUNT
))) {
453 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks
);
455 GlobalUnlock16(lppd
->hDevMode
);
456 TRACE("Now got %d locks\n", locks
);
459 lppd
->hDevMode
= GlobalReAlloc16(lppd
->hDevMode
,
460 lpdm
->dmSize
+ lpdm
->dmDriverExtra
,
463 lpdmReturn
= GlobalLock16(lppd
->hDevMode
);
464 memcpy(lpdmReturn
, lpdm
, lpdm
->dmSize
+ lpdm
->dmDriverExtra
);
466 if (lppd
->hDevNames
!= 0) {
468 if((locks
= (GlobalFlags16(lppd
->hDevNames
)&GMEM_LOCKCOUNT
))) {
469 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks
);
471 GlobalUnlock16(lppd
->hDevNames
);
474 PRINTDLG_CreateDevNames16(&(lppd
->hDevNames
),
479 GlobalUnlock16(lppd
->hDevMode
);
481 if (!(lppd
->Flags
& (PD_ENABLESETUPTEMPLATEHANDLE
| PD_ENABLESETUPTEMPLATE
)))
482 GlobalFree16(hDlgTmpl
); /* created from the 32 bits resource */
483 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpDevMode
);
484 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpPrinterInfo
);
485 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpDriverInfo
);
486 HeapFree(GetProcessHeap(), 0, PrintStructures
);
488 if(bRet
&& (lppd
->Flags
& PD_RETURNDC
|| lppd
->Flags
& PD_RETURNIC
))
489 bRet
= PRINTDLG_CreateDC16(lppd
);
491 TRACE("exit! (%d)\n", bRet
);
495 /**********************************************************************
500 /***********************************************************************
501 * PrintDlgProc (COMMDLG.21)
503 BOOL16 CALLBACK
PrintDlgProc16(HWND16 hDlg16
, UINT16 uMsg
, WPARAM16 wParam
,
506 HWND hDlg
= HWND_32(hDlg16
);
507 PRINT_PTRA
* PrintStructures
;
510 if (uMsg
!=WM_INITDIALOG
) {
511 PrintStructures
= (PRINT_PTRA
*)GetPropA(hDlg
,"__WINE_PRINTDLGDATA");
512 if (!PrintStructures
)
515 PrintStructures
= (PRINT_PTRA
*) lParam
;
516 SetPropA(hDlg
,"__WINE_PRINTDLGDATA",PrintStructures
);
517 res
= PRINTDLG_WMInitDialog16(hDlg
, wParam
, PrintStructures
);
519 if(PrintStructures
->dlg
.lpPrintDlg16
->Flags
& PD_ENABLEPRINTHOOK
) {
520 res
= CallWindowProc16(
521 (WNDPROC16
)PrintStructures
->dlg
.lpPrintDlg16
->lpfnPrintHook
,
522 hDlg16
, uMsg
, wParam
, (LPARAM
)PrintStructures
->dlg
.lpPrintDlg16
528 if(PrintStructures
->dlg
.lpPrintDlg16
->Flags
& PD_ENABLEPRINTHOOK
) {
529 res
= CallWindowProc16(
530 (WNDPROC16
)PrintStructures
->dlg
.lpPrintDlg16
->lpfnPrintHook
,
531 hDlg16
,uMsg
, wParam
, lParam
533 if(LOWORD(res
)) return res
;
538 /* We need to map those for the 32bit window procedure, compare
539 * with 32Ato16 mapper in winproc.c
541 return PRINTDLG_WMCommandA(
543 MAKEWPARAM(wParam
,HIWORD(lParam
)),
549 DestroyIcon(PrintStructures
->hCollateIcon
);
550 DestroyIcon(PrintStructures
->hNoCollateIcon
);
551 /* FIXME: don't forget to delete the paper orientation icons here! */
558 /***********************************************************************
559 * PrintSetupDlgProc (COMMDLG.22)
561 BOOL16 CALLBACK
PrintSetupDlgProc16(HWND16 hWnd16
, UINT16 wMsg
, WPARAM16 wParam
,
564 HWND hWnd
= HWND_32(hWnd16
);
568 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam
);
569 ShowWindow(hWnd
, SW_SHOWNORMAL
);
574 EndDialog(hWnd
, TRUE
);
577 EndDialog(hWnd
, FALSE
);