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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
34 #include "wine/wingdi16.h"
36 #include "wine/winuser16.h"
39 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
49 static BOOL
PRINTDLG_CreateDevNames16(HGLOBAL16
*hmem
, char* DeviceDriverName
,
50 char* DeviceName
, char* OutputPort
)
55 LPDEVNAMES lpDevNames
;
58 size
= strlen(DeviceDriverName
) + 1
59 + strlen(DeviceName
) + 1
60 + strlen(OutputPort
) + 1
64 *hmem
= GlobalReAlloc16(*hmem
, size
, GMEM_MOVEABLE
);
66 *hmem
= GlobalAlloc16(GMEM_MOVEABLE
, size
);
70 pDevNamesSpace
= GlobalLock16(*hmem
);
71 lpDevNames
= (LPDEVNAMES
) pDevNamesSpace
;
73 pTempPtr
= pDevNamesSpace
+ sizeof(DEVNAMES
);
74 strcpy(pTempPtr
, DeviceDriverName
);
75 lpDevNames
->wDriverOffset
= pTempPtr
- pDevNamesSpace
;
77 pTempPtr
+= strlen(DeviceDriverName
) + 1;
78 strcpy(pTempPtr
, DeviceName
);
79 lpDevNames
->wDeviceOffset
= pTempPtr
- pDevNamesSpace
;
81 pTempPtr
+= strlen(DeviceName
) + 1;
82 strcpy(pTempPtr
, OutputPort
);
83 lpDevNames
->wOutputOffset
= pTempPtr
- pDevNamesSpace
;
85 PRINTDLG_GetDefaultPrinterNameA(buf
, sizeof(buf
));
86 lpDevNames
->wDefault
= (strcmp(buf
, DeviceName
) == 0) ? 1 : 0;
87 GlobalUnlock16(*hmem
);
92 /***********************************************************************
93 * PRINTDLG_WMInitDialog [internal]
95 static LRESULT
PRINTDLG_WMInitDialog16(HWND hDlg
, WPARAM wParam
,
96 PRINT_PTRA
* PrintStructures
)
98 LPPRINTDLG16 lppd
= PrintStructures
->dlg
.lpPrintDlg16
;
102 UINT comboID
= (lppd
->Flags
& PD_PRINTSETUP
) ? cmb1
: cmb4
;
104 /* load Collate ICONs */
105 PrintStructures
->hCollateIcon
=
106 LoadIconA(COMDLG32_hInstance
, "PD32_COLLATE");
107 PrintStructures
->hNoCollateIcon
=
108 LoadIconA(COMDLG32_hInstance
, "PD32_NOCOLLATE");
109 if(PrintStructures
->hCollateIcon
== 0 ||
110 PrintStructures
->hNoCollateIcon
== 0) {
111 ERR("no icon in resourcefile\n");
112 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
113 EndDialog(hDlg
, FALSE
);
116 /* load Paper Orientation ICON */
117 /* FIXME: not implemented yet */
120 * if lppd->Flags PD_SHOWHELP is specified, a HELPMESGSTRING message
121 * must be registered and the Help button must be shown.
123 if (lppd
->Flags
& PD_SHOWHELP
) {
124 if((PrintStructures
->HelpMessageID
=
125 RegisterWindowMessageA(HELPMSGSTRINGA
)) == 0) {
126 COMDLG32_SetCommDlgExtendedError(CDERR_REGISTERMSGFAIL
);
130 PrintStructures
->HelpMessageID
= 0;
132 if (!(lppd
->Flags
& PD_PRINTSETUP
)) {
133 /* We have a print quality combo box. What shall we do? */
134 if (GetDlgItem(hDlg
,cmb1
)) {
137 FIXME("Print quality only displaying currently.\n");
139 pdm
= GlobalLock16(lppd
->hDevMode
);
141 switch (pdm
->dmPrintQuality
) {
142 case DMRES_HIGH
: strcpy(buf
,"High");break;
143 case DMRES_MEDIUM
: strcpy(buf
,"Medium");break;
144 case DMRES_LOW
: strcpy(buf
,"Low");break;
145 case DMRES_DRAFT
: strcpy(buf
,"Draft");break;
146 case 0 : strcpy(buf
,"Default");break;
147 default : sprintf(buf
,"%ddpi",pdm
->dmPrintQuality
);break;
149 GlobalUnlock16(lppd
->hDevMode
);
151 strcpy(buf
,"Default");
152 SendDlgItemMessageA(hDlg
,cmb1
,CB_ADDSTRING
,0,(LPARAM
)buf
);
153 SendDlgItemMessageA(hDlg
,cmb1
,CB_SETCURSEL
,0,0);
154 EnableWindow(GetDlgItem(hDlg
,cmb1
),FALSE
);
158 /* FIXME: I allow more freedom than either Win95 or WinNT,
159 * which do not agree to what errors should be thrown or not
160 * in case nToPage or nFromPage is out-of-range.
162 if (lppd
->nMaxPage
< lppd
->nMinPage
)
163 lppd
->nMaxPage
= lppd
->nMinPage
;
164 if (lppd
->nMinPage
== lppd
->nMaxPage
)
165 lppd
->Flags
|= PD_NOPAGENUMS
;
166 if (lppd
->nToPage
< lppd
->nMinPage
)
167 lppd
->nToPage
= lppd
->nMinPage
;
168 if (lppd
->nToPage
> lppd
->nMaxPage
)
169 lppd
->nToPage
= lppd
->nMaxPage
;
170 if (lppd
->nFromPage
< lppd
->nMinPage
)
171 lppd
->nFromPage
= lppd
->nMinPage
;
172 if (lppd
->nFromPage
> lppd
->nMaxPage
)
173 lppd
->nFromPage
= lppd
->nMaxPage
;
175 /* If the printer combo box is in the dialog, fill it */
176 if (GetDlgItem(hDlg
,comboID
)) {
179 pdn
= GlobalLock16(lppd
->hDevNames
);
180 pdm
= GlobalLock16(lppd
->hDevMode
);
182 name
= (char*)pdn
+ pdn
->wDeviceOffset
;
184 name
= pdm
->dmDeviceName
;
185 PRINTDLG_SetUpPrinterListComboA(hDlg
, comboID
, name
);
186 if(pdm
) GlobalUnlock16(lppd
->hDevMode
);
187 if(pdn
) GlobalUnlock16(lppd
->hDevNames
);
189 /* Now find selected printer and update rest of dlg */
190 name
= HeapAlloc(GetProcessHeap(),0,256);
191 if (GetDlgItemTextA(hDlg
, comboID
, name
, 255))
192 PRINTDLG_ChangePrinterA(hDlg
, name
, PrintStructures
);
194 /* else just use default printer */
196 BOOL ret
= PRINTDLG_GetDefaultPrinterNameA(name
, sizeof(name
));
199 PRINTDLG_ChangePrinterA(hDlg
, name
, PrintStructures
);
201 FIXME("No default printer found, expect problems!\n");
203 HeapFree(GetProcessHeap(),0,name
);
208 /************************************************************
210 * PRINTDLG_Get16TemplateFrom32 [Internal]
211 * Generates a 16 bits template from the Wine 32 bits resource
214 static HGLOBAL16
PRINTDLG_Get16TemplateFrom32(char *PrintResourceName
)
223 if (!(hResInfo
= FindResourceA(COMDLG32_hInstance
,
224 PrintResourceName
, RT_DIALOGA
)))
226 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
229 if (!(hDlgTmpl32
= LoadResource(COMDLG32_hInstance
, hResInfo
)) ||
230 !(template32
= LockResource( hDlgTmpl32
)))
232 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
235 size
= SizeofResource(COMDLG32_hInstance
, hResInfo
);
236 hGlobal16
= GlobalAlloc16(0, size
);
239 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE
);
240 ERR("alloc failure for %ld bytes\n", size
);
243 template = GlobalLock16(hGlobal16
);
246 COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE
);
247 ERR("global lock failure for %x handle\n", hGlobal16
);
248 GlobalFree16(hGlobal16
);
251 ConvertDialog32To16((LPVOID
)template32
, size
, (LPVOID
)template);
252 GlobalUnlock16(hGlobal16
);
256 static BOOL
PRINTDLG_CreateDC16(LPPRINTDLG16 lppd
)
258 DEVNAMES
*pdn
= GlobalLock16(lppd
->hDevNames
);
259 DEVMODEA
*pdm
= GlobalLock16(lppd
->hDevMode
);
261 if(lppd
->Flags
& PD_RETURNDC
) {
262 lppd
->hDC
= HDC_16(CreateDCA((char*)pdn
+ pdn
->wDriverOffset
,
263 (char*)pdn
+ pdn
->wDeviceOffset
,
264 (char*)pdn
+ pdn
->wOutputOffset
,
266 } else if(lppd
->Flags
& PD_RETURNIC
) {
267 lppd
->hDC
= HDC_16(CreateICA((char*)pdn
+ pdn
->wDriverOffset
,
268 (char*)pdn
+ pdn
->wDeviceOffset
,
269 (char*)pdn
+ pdn
->wOutputOffset
,
272 GlobalUnlock16(lppd
->hDevNames
);
273 GlobalUnlock16(lppd
->hDevMode
);
274 return lppd
->hDC
? TRUE
: FALSE
;
277 /************************************************************
279 * PRINTDLG_GetDlgTemplate
282 static HGLOBAL16
PRINTDLG_GetDlgTemplate16(PRINTDLG16
*lppd
)
284 HGLOBAL16 hDlgTmpl
, hResInfo
;
286 if (lppd
->Flags
& PD_PRINTSETUP
) {
287 if(lppd
->Flags
& PD_ENABLESETUPTEMPLATEHANDLE
) {
288 hDlgTmpl
= lppd
->hSetupTemplate
;
289 } else if(lppd
->Flags
& PD_ENABLESETUPTEMPLATE
) {
290 hResInfo
= FindResource16(lppd
->hInstance
,
291 MapSL(lppd
->lpSetupTemplateName
), RT_DIALOGA
);
292 hDlgTmpl
= LoadResource16(lppd
->hInstance
, hResInfo
);
294 hDlgTmpl
= PRINTDLG_Get16TemplateFrom32("PRINT32_SETUP");
297 if(lppd
->Flags
& PD_ENABLEPRINTTEMPLATEHANDLE
) {
298 hDlgTmpl
= lppd
->hPrintTemplate
;
299 } else if(lppd
->Flags
& PD_ENABLEPRINTTEMPLATE
) {
300 hResInfo
= FindResource16(lppd
->hInstance
,
301 MapSL(lppd
->lpPrintTemplateName
),
303 hDlgTmpl
= LoadResource16(lppd
->hInstance
, hResInfo
);
305 hDlgTmpl
= PRINTDLG_Get16TemplateFrom32("PRINT32");
311 /***********************************************************************
312 * PrintDlg (COMMDLG.20)
314 * Displays the the PRINT dialog box, which enables the user to specify
315 * specific properties of the print job.
318 * nonzero if the user pressed the OK button
319 * zero if the user cancelled the window or an error occurred
322 * * calls up to the 32-bit versions of the Dialogs, which look different
323 * * Customizing is *not* implemented.
326 BOOL16 WINAPI
PrintDlg16(
327 LPPRINTDLG16 lppd
/* [in/out] ptr to PRINTDLG struct */
331 HINSTANCE16 hInst
= GetWindowWord( HWND_32(lppd
->hwndOwner
), GWL_HINSTANCE
);
333 if(TRACE_ON(commdlg
)) {
334 char flagstr
[1000] = "";
335 struct pd_flags
*pflag
= pd_flags
;
336 for( ; pflag
->name
; pflag
++) {
337 if(lppd
->Flags
& pflag
->flag
)
338 strcat(flagstr
, pflag
->name
);
340 TRACE("(%p): hwndOwner = %08x, hDevMode = %08x, hDevNames = %08x\n"
341 "pp. %d-%d, min p %d, max p %d, copies %d, hinst %08x\n"
342 "flags %08lx (%s)\n",
343 lppd
, lppd
->hwndOwner
, lppd
->hDevMode
, lppd
->hDevNames
,
344 lppd
->nFromPage
, lppd
->nToPage
, lppd
->nMinPage
, lppd
->nMaxPage
,
345 lppd
->nCopies
, lppd
->hInstance
, lppd
->Flags
, flagstr
);
348 if(lppd
->lStructSize
!= sizeof(PRINTDLG16
)) {
349 ERR("structure size (%ld/%d)\n",lppd
->lStructSize
,sizeof(PRINTDLG16
));
350 COMDLG32_SetCommDlgExtendedError(CDERR_STRUCTSIZE
);
354 if(lppd
->Flags
& PD_RETURNDEFAULT
) {
355 PRINTER_INFO_2A
*pbuf
;
356 DRIVER_INFO_3A
*dbuf
;
360 if(lppd
->hDevMode
|| lppd
->hDevNames
) {
361 WARN("hDevMode or hDevNames non-zero for PD_RETURNDEFAULT\n");
362 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE
);
365 if(!PRINTDLG_OpenDefaultPrinter(&hprn
)) {
366 WARN("Can't find default printer\n");
367 COMDLG32_SetCommDlgExtendedError(PDERR_NODEFAULTPRN
);
371 GetPrinterA(hprn
, 2, NULL
, 0, &needed
);
372 pbuf
= HeapAlloc(GetProcessHeap(), 0, needed
);
373 GetPrinterA(hprn
, 2, (LPBYTE
)pbuf
, needed
, &needed
);
374 GetPrinterDriverA(hprn
, NULL
, 3, NULL
, 0, &needed
);
375 dbuf
= HeapAlloc(GetProcessHeap(),0,needed
);
376 if (!GetPrinterDriverA(hprn
, NULL
, 3, (LPBYTE
)dbuf
, needed
, &needed
)) {
377 ERR("GetPrinterDriverA failed for %s, le %ld, fix your config!\n",
378 pbuf
->pPrinterName
,GetLastError());
379 COMDLG32_SetCommDlgExtendedError(PDERR_RETDEFFAILURE
);
383 PRINTDLG_CreateDevNames16(&(lppd
->hDevNames
),
387 lppd
->hDevMode
= GlobalAlloc16(GMEM_MOVEABLE
,pbuf
->pDevMode
->dmSize
+
388 pbuf
->pDevMode
->dmDriverExtra
);
389 ptr
= GlobalLock16(lppd
->hDevMode
);
390 memcpy(ptr
, pbuf
->pDevMode
, pbuf
->pDevMode
->dmSize
+
391 pbuf
->pDevMode
->dmDriverExtra
);
392 GlobalUnlock16(lppd
->hDevMode
);
393 HeapFree(GetProcessHeap(), 0, pbuf
);
394 HeapFree(GetProcessHeap(), 0, dbuf
);
398 PRINT_PTRA
*PrintStructures
;
400 /* load Dialog resources,
401 * depending on Flags indicates Print32 or Print32_setup dialog
403 hDlgTmpl
= PRINTDLG_GetDlgTemplate16(lppd
);
405 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
408 PrintStructures
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
410 PrintStructures
->dlg
.lpPrintDlg16
= lppd
;
411 PrintStructures
->dlg
.lpPrintDlg
= (LPPRINTDLGA
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(PRINTDLGA
));
412 #define CVAL(x) PrintStructures->dlg.lpPrintDlg->x = lppd->x;
413 #define MVAL(x) PrintStructures->dlg.lpPrintDlg->x = MapSL(lppd->x);
415 PrintStructures
->dlg
.lpPrintDlg
->hwndOwner
= HWND_32(lppd
->hwndOwner
);
416 PrintStructures
->dlg
.lpPrintDlg
->hDC
= HDC_32(lppd
->hDC
);
417 CVAL(nFromPage
);CVAL(nToPage
);CVAL(nMinPage
);CVAL(nMaxPage
);
419 PrintStructures
->dlg
.lpPrintDlg
->hInstance
= HINSTANCE_32(lppd
->hInstance
);
421 MVAL(lpPrintTemplateName
);MVAL(lpSetupTemplateName
);
422 /* Don't copy rest, it is 16 bit specific */
426 PrintStructures
->lpDevMode
= HeapAlloc(GetProcessHeap(),0,sizeof(DEVMODEA
));
428 /* and create & process the dialog .
429 * -1 is failure, 0 is broken hwnd, everything else is ok.
431 bRet
= (0<DialogBoxIndirectParam16(
432 hInst
, hDlgTmpl
, lppd
->hwndOwner
,
433 (DLGPROC16
)GetProcAddress16(GetModuleHandle16("COMMDLG"),(LPCSTR
)21),
434 (LPARAM
)PrintStructures
437 if (!PrintStructures
->lpPrinterInfo
) bRet
= FALSE
;
439 DEVMODEA
*lpdm
= PrintStructures
->lpDevMode
, *lpdmReturn
;
440 PRINTER_INFO_2A
*pi
= PrintStructures
->lpPrinterInfo
;
441 DRIVER_INFO_3A
*di
= PrintStructures
->lpDriverInfo
;
443 if (lppd
->hDevMode
== 0) {
444 TRACE(" No hDevMode yet... Need to create my own\n");
445 lppd
->hDevMode
= GlobalAlloc16(GMEM_MOVEABLE
,
446 lpdm
->dmSize
+ lpdm
->dmDriverExtra
);
449 if((locks
= (GlobalFlags16(lppd
->hDevMode
)&GMEM_LOCKCOUNT
))) {
450 WARN("hDevMode has %d locks on it. Unlocking it now\n", locks
);
452 GlobalUnlock16(lppd
->hDevMode
);
453 TRACE("Now got %d locks\n", locks
);
456 lppd
->hDevMode
= GlobalReAlloc16(lppd
->hDevMode
,
457 lpdm
->dmSize
+ lpdm
->dmDriverExtra
,
460 lpdmReturn
= GlobalLock16(lppd
->hDevMode
);
461 memcpy(lpdmReturn
, lpdm
, lpdm
->dmSize
+ lpdm
->dmDriverExtra
);
463 if (lppd
->hDevNames
!= 0) {
465 if((locks
= (GlobalFlags16(lppd
->hDevNames
)&GMEM_LOCKCOUNT
))) {
466 WARN("hDevNames has %d locks on it. Unlocking it now\n", locks
);
468 GlobalUnlock16(lppd
->hDevNames
);
471 PRINTDLG_CreateDevNames16(&(lppd
->hDevNames
),
476 GlobalUnlock16(lppd
->hDevMode
);
478 if (!(lppd
->Flags
& (PD_ENABLESETUPTEMPLATEHANDLE
| PD_ENABLESETUPTEMPLATE
)))
479 GlobalFree16(hDlgTmpl
); /* created from the 32 bits resource */
480 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpDevMode
);
481 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpPrinterInfo
);
482 HeapFree(GetProcessHeap(), 0, PrintStructures
->lpDriverInfo
);
483 HeapFree(GetProcessHeap(), 0, PrintStructures
);
485 if(bRet
&& (lppd
->Flags
& PD_RETURNDC
|| lppd
->Flags
& PD_RETURNIC
))
486 bRet
= PRINTDLG_CreateDC16(lppd
);
488 TRACE("exit! (%d)\n", bRet
);
492 /**********************************************************************
497 /***********************************************************************
498 * PrintDlgProc (COMMDLG.21)
500 BOOL16 CALLBACK
PrintDlgProc16(HWND16 hDlg16
, UINT16 uMsg
, WPARAM16 wParam
,
503 HWND hDlg
= HWND_32(hDlg16
);
504 PRINT_PTRA
* PrintStructures
;
507 if (uMsg
!=WM_INITDIALOG
) {
508 PrintStructures
= (PRINT_PTRA
*)GetPropA(hDlg
,"__WINE_PRINTDLGDATA");
509 if (!PrintStructures
)
512 PrintStructures
= (PRINT_PTRA
*) lParam
;
513 SetPropA(hDlg
,"__WINE_PRINTDLGDATA",PrintStructures
);
514 res
= PRINTDLG_WMInitDialog16(hDlg
, wParam
, PrintStructures
);
516 if(PrintStructures
->dlg
.lpPrintDlg16
->Flags
& PD_ENABLEPRINTHOOK
) {
517 res
= CallWindowProc16(
518 (WNDPROC16
)PrintStructures
->dlg
.lpPrintDlg16
->lpfnPrintHook
,
519 hDlg16
, uMsg
, wParam
, (LPARAM
)PrintStructures
->dlg
.lpPrintDlg16
525 if(PrintStructures
->dlg
.lpPrintDlg16
->Flags
& PD_ENABLEPRINTHOOK
) {
526 res
= CallWindowProc16(
527 (WNDPROC16
)PrintStructures
->dlg
.lpPrintDlg16
->lpfnPrintHook
,
528 hDlg16
,uMsg
, wParam
, lParam
530 if(LOWORD(res
)) return res
;
535 /* We need to map those for the 32bit window procedure, compare
536 * with 32Ato16 mapper in winproc.c
538 return PRINTDLG_WMCommandA(
540 MAKEWPARAM(wParam
,HIWORD(lParam
)),
546 DestroyIcon(PrintStructures
->hCollateIcon
);
547 DestroyIcon(PrintStructures
->hNoCollateIcon
);
548 /* FIXME: don't forget to delete the paper orientation icons here! */
555 /***********************************************************************
556 * PrintSetupDlgProc (COMMDLG.22)
558 BOOL16 CALLBACK
PrintSetupDlgProc16(HWND16 hWnd16
, UINT16 wMsg
, WPARAM16 wParam
,
561 HWND hWnd
= HWND_32(hWnd16
);
565 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam
);
566 ShowWindow(hWnd
, SW_SHOWNORMAL
);
571 EndDialog(hWnd
, TRUE
);
574 EndDialog(hWnd
, FALSE
);