Fix a regression caused by an earlier patch for CreateDIBitmap.
[wine] / windows / defdlg.c
1 /*
2  * Default dialog procedure
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/winuser16.h"
27 #include "controls.h"
28 #include "win.h"
29 #include "winproc.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
33
34
35 /***********************************************************************
36  *           DEFDLG_GetDlgProc
37  */
38 static WNDPROC DEFDLG_GetDlgProc( HWND hwnd )
39 {
40     WNDPROC ret;
41     WND *wndPtr = WIN_GetPtr( hwnd );
42
43     if (!wndPtr) return 0;
44     if (wndPtr == WND_OTHER_PROCESS)
45     {
46         ERR( "cannot get dlg proc %p from other process\n", hwnd );
47         return 0;
48     }
49     ret = *(WNDPROC *)((char *)wndPtr->wExtra + DWL_DLGPROC);
50     WIN_ReleasePtr( wndPtr );
51     return ret;
52 }
53
54 /***********************************************************************
55  *           DEFDLG_SetFocus
56  *
57  * Set the focus to a control of the dialog, selecting the text if
58  * the control is an edit dialog.
59  */
60 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
61 {
62     HWND hwndPrev = GetFocus();
63
64     if (IsChild( hwndDlg, hwndPrev ))
65     {
66         if (SendMessageW( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
67             SendMessageW( hwndPrev, EM_SETSEL, -1, 0 );
68     }
69     if (SendMessageW( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
70         SendMessageW( hwndCtrl, EM_SETSEL, 0, -1 );
71     SetFocus( hwndCtrl );
72 }
73
74
75 /***********************************************************************
76  *           DEFDLG_SaveFocus
77  */
78 static void DEFDLG_SaveFocus( HWND hwnd )
79 {
80     DIALOGINFO *infoPtr;
81     HWND hwndFocus = GetFocus();
82
83     if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return;
84     if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
85     infoPtr->hwndFocus = hwndFocus;
86     /* Remove default button */
87 }
88
89
90 /***********************************************************************
91  *           DEFDLG_RestoreFocus
92  */
93 static void DEFDLG_RestoreFocus( HWND hwnd )
94 {
95     DIALOGINFO *infoPtr;
96
97     if (IsIconic( hwnd )) return;
98     if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
99     /* Don't set the focus back to controls if EndDialog is already called.*/
100     if (infoPtr->flags & DF_END) return;
101     if (!IsWindow(infoPtr->hwndFocus) || infoPtr->hwndFocus == hwnd) {
102         /* If no saved focus control exists, set focus to the first visible,
103            non-disabled, WS_TABSTOP control in the dialog */
104         infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
105        if (!IsWindow( infoPtr->hwndFocus )) return;
106     }
107     DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
108
109     /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
110        sometimes losing focus when receiving WM_SETFOCUS messages. */
111 }
112
113
114 /***********************************************************************
115  *           DEFDLG_FindDefButton
116  *
117  * Find the current default push-button.
118  */
119 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
120 {
121     HWND hwndChild, hwndTmp;
122
123     hwndChild = GetWindow( hwndDlg, GW_CHILD );
124     while (hwndChild)
125     {
126         if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
127             break;
128
129         /* Recurse into WS_EX_CONTROLPARENT controls */
130         if (GetWindowLongA( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
131         {
132             LONG dsStyle = GetWindowLongA( hwndChild, GWL_STYLE );
133             if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
134                 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
135            return hwndTmp;
136         }
137         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
138     }
139     return hwndChild;
140 }
141
142
143 /***********************************************************************
144  *           DEFDLG_SetDefId
145  *
146  * Set the default button id.
147  */
148 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
149 {
150     DWORD dlgcode=0; /* initialize just to avoid a warning */
151     HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
152     INT old_id = dlgInfo->idResult;
153
154     dlgInfo->idResult = wParam;
155     if (hwndNew &&
156         !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
157             & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
158         return FALSE;  /* Destination is not a push button */
159
160     /* Make sure the old default control is a valid push button ID */
161     hwndOld = GetDlgItem( hwndDlg, old_id );
162     if (!hwndOld || !(SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
163         hwndOld = DEFDLG_FindDefButton( hwndDlg );
164     if (hwndOld && hwndOld != hwndNew)
165         SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
166
167     if (hwndNew)
168     {
169         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
170             SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
171     }
172     return TRUE;
173 }
174
175
176 /***********************************************************************
177  *           DEFDLG_SetDefButton
178  *
179  * Set the new default button to be hwndNew.
180  */
181 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
182 {
183     DWORD dlgcode=0; /* initialize just to avoid a warning */
184     HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
185
186     if (hwndNew &&
187         !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
188             & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
189     {
190         /**
191          * Need to draw only default push button rectangle.
192          * Since the next control is not a push button, need to draw the push
193          * button rectangle for the default control.
194          */
195         hwndNew = hwndOld;
196         dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
197     }
198
199     /* Make sure the old default control is a valid push button ID */
200     if (!hwndOld || !(SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
201         hwndOld = DEFDLG_FindDefButton( hwndDlg );
202     if (hwndOld && hwndOld != hwndNew)
203         SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
204
205     if (hwndNew)
206     {
207         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
208             SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
209     }
210     return TRUE;
211 }
212
213
214 /***********************************************************************
215  *           DEFDLG_Proc
216  *
217  * Implementation of DefDlgProc(). Only handle messages that need special
218  * handling for dialogs.
219  */
220 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
221                             LPARAM lParam, DIALOGINFO *dlgInfo )
222 {
223     switch(msg)
224     {
225         case WM_ERASEBKGND:
226         {
227             HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
228             if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
229             if (brush)
230             {
231                 RECT rect;
232                 HDC hdc = (HDC)wParam;
233                 GetClientRect( hwnd, &rect );
234                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
235                 FillRect( hdc, &rect, brush );
236             }
237             return 1;
238         }
239         case WM_NCDESTROY:
240             if ((dlgInfo = (DIALOGINFO *)SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, 0 )))
241             {
242                 /* Free dialog heap (if created) */
243                 if (dlgInfo->hDialogHeap)
244                 {
245                     GlobalUnlock16(dlgInfo->hDialogHeap);
246                     GlobalFree16(dlgInfo->hDialogHeap);
247                 }
248                 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
249                 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
250                 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd ), WIN_PROC_WINDOW );
251                 HeapFree( GetProcessHeap(), 0, dlgInfo );
252             }
253               /* Window clean-up */
254             return DefWindowProcA( hwnd, msg, wParam, lParam );
255
256         case WM_SHOWWINDOW:
257             if (!wParam) DEFDLG_SaveFocus( hwnd );
258             return DefWindowProcA( hwnd, msg, wParam, lParam );
259
260         case WM_ACTIVATE:
261             if (wParam) DEFDLG_RestoreFocus( hwnd );
262             else DEFDLG_SaveFocus( hwnd );
263             return 0;
264
265         case WM_SETFOCUS:
266             DEFDLG_RestoreFocus( hwnd );
267             return 0;
268
269         case DM_SETDEFID:
270             if (dlgInfo && !(dlgInfo->flags & DF_END))
271                 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
272             return 1;
273
274         case DM_GETDEFID:
275             if (dlgInfo && !(dlgInfo->flags & DF_END))
276             {
277                 HWND hwndDefId;
278                 if (dlgInfo->idResult)
279                     return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
280                 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
281                     return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
282             }
283             return 0;
284
285         case WM_NEXTDLGCTL:
286             if (dlgInfo)
287             {
288                 HWND hwndDest = (HWND)wParam;
289                 if (!lParam)
290                     hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
291                 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
292                 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
293             }
294             return 0;
295
296         case WM_ENTERMENULOOP:
297         case WM_LBUTTONDOWN:
298         case WM_NCLBUTTONDOWN:
299             {
300                 HWND hwndFocus = GetFocus();
301                 if (hwndFocus)
302                 {
303                     /* always make combo box hide its listbox control */
304                     if (!SendMessageA( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
305                         SendMessageA( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
306                 }
307             }
308             return DefWindowProcA( hwnd, msg, wParam, lParam );
309
310         case WM_GETFONT:
311             return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
312
313         case WM_CLOSE:
314             PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
315                             (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
316             return 0;
317
318         case WM_NOTIFYFORMAT:
319             return DefWindowProcA( hwnd, msg, wParam, lParam );
320     }
321     return 0;
322 }
323
324 /***********************************************************************
325  *           DEFDLG_Epilog
326  */
327 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
328 {
329     /* see SDK 3.1 */
330
331     if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
332          msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
333          msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
334          msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
335         return fResult;
336
337     return GetWindowLongA( hwnd, DWL_MSGRESULT );
338 }
339
340 /***********************************************************************
341 *               DIALOG_get_info
342 *
343 * Get the DIALOGINFO structure of a window, allocating it if needed
344 * and 'create' is TRUE.
345 */
346 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
347 {
348     WND* wndPtr;
349     DIALOGINFO* dlgInfo = (DIALOGINFO *)GetWindowLongW( hwnd, DWL_WINE_DIALOGINFO );
350
351     if(!dlgInfo && create)
352     {
353         if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return NULL;
354         dlgInfo->hwndFocus   = 0;
355         dlgInfo->hUserFont   = 0;
356         dlgInfo->hMenu       = 0;
357         dlgInfo->xBaseUnit   = 0;
358         dlgInfo->yBaseUnit   = 0;
359         dlgInfo->idResult    = 0;
360         dlgInfo->flags       = 0;
361         dlgInfo->hDialogHeap = 0;
362         wndPtr = WIN_GetPtr( hwnd );
363         if (wndPtr && wndPtr != WND_OTHER_PROCESS)
364         {
365             wndPtr->flags |= WIN_ISDIALOG;
366             WIN_ReleasePtr( wndPtr );
367             SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
368         }
369         else
370         {
371             HeapFree( GetProcessHeap(), 0, dlgInfo );
372             return NULL;
373         }
374     }
375     return dlgInfo;
376 }
377
378 /***********************************************************************
379  *              DefDlgProc (USER.308)
380  */
381 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
382                              LPARAM lParam )
383 {
384     DIALOGINFO *dlgInfo;
385     WNDPROC16 dlgproc;
386     HWND hwnd32 = WIN_Handle32( hwnd );
387     BOOL result = FALSE;
388
389     /* Perform DIALOGINFO intialization if not done */
390     if(!(dlgInfo = DIALOG_get_info(hwnd32, TRUE))) return -1;
391
392     SetWindowLongW( hwnd32, DWL_MSGRESULT, 0 );
393
394     if ((dlgproc = (WNDPROC16)DEFDLG_GetDlgProc( hwnd32 )))
395     {
396         /* Call dialog procedure */
397         result = CallWindowProc16( dlgproc, hwnd, msg, wParam, lParam );
398         /* 16 bit dlg procs only return BOOL16 */
399         if( WINPROC_GetProcType( (WNDPROC)dlgproc ) == WIN_PROC_16 )
400             result = LOWORD(result);
401     }
402
403     if (!result && IsWindow(hwnd32))
404     {
405         /* callback didn't process this message */
406
407         switch(msg)
408         {
409             case WM_ERASEBKGND:
410             case WM_SHOWWINDOW:
411             case WM_ACTIVATE:
412             case WM_SETFOCUS:
413             case DM_SETDEFID:
414             case DM_GETDEFID:
415             case WM_NEXTDLGCTL:
416             case WM_GETFONT:
417             case WM_CLOSE:
418             case WM_NCDESTROY:
419             case WM_ENTERMENULOOP:
420             case WM_LBUTTONDOWN:
421             case WM_NCLBUTTONDOWN:
422                 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, dlgInfo );
423             case WM_INITDIALOG:
424             case WM_VKEYTOITEM:
425             case WM_COMPAREITEM:
426             case WM_CHARTOITEM:
427                 break;
428
429             default:
430                 return DefWindowProc16( hwnd, msg, wParam, lParam );
431         }
432     }
433     return DEFDLG_Epilog( hwnd32, msg, result);
434 }
435
436
437 /***********************************************************************
438  *              DefDlgProcA (USER32.@)
439  */
440 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
441 {
442     DIALOGINFO *dlgInfo;
443     WNDPROC dlgproc;
444     BOOL result = FALSE;
445
446     /* Perform DIALOGINFO initialization if not done */
447     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
448
449     SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
450
451     if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
452     {
453         /* Call dialog procedure */
454         result = CallWindowProcA( dlgproc, hwnd, msg, wParam, lParam );
455         /* 16 bit dlg procs only return BOOL16 */
456         if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
457             result = LOWORD(result);
458     }
459
460     if (!result && IsWindow(hwnd))
461     {
462         /* callback didn't process this message */
463
464         switch(msg)
465         {
466             case WM_ERASEBKGND:
467             case WM_SHOWWINDOW:
468             case WM_ACTIVATE:
469             case WM_SETFOCUS:
470             case DM_SETDEFID:
471             case DM_GETDEFID:
472             case WM_NEXTDLGCTL:
473             case WM_GETFONT:
474             case WM_CLOSE:
475             case WM_NCDESTROY:
476             case WM_ENTERMENULOOP:
477             case WM_LBUTTONDOWN:
478             case WM_NCLBUTTONDOWN:
479                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
480             case WM_INITDIALOG:
481             case WM_VKEYTOITEM:
482             case WM_COMPAREITEM:
483             case WM_CHARTOITEM:
484                  break;
485
486             default:
487                  return DefWindowProcA( hwnd, msg, wParam, lParam );
488         }
489     }
490     return DEFDLG_Epilog(hwnd, msg, result);
491 }
492
493
494 /***********************************************************************
495  *              DefDlgProcW (USER32.@)
496  */
497 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
498 {
499     DIALOGINFO *dlgInfo;
500     BOOL result = FALSE;
501     WNDPROC dlgproc;
502
503     /* Perform DIALOGINFO intialization if not done */
504     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
505
506     SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
507
508     if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
509     {
510         /* Call dialog procedure */
511         result = CallWindowProcW( dlgproc, hwnd, msg, wParam, lParam );
512         /* 16 bit dlg procs only return BOOL16 */
513         if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
514             result = LOWORD(result);
515     }
516
517     if (!result && IsWindow(hwnd))
518     {
519         /* callback didn't process this message */
520
521         switch(msg)
522         {
523             case WM_ERASEBKGND:
524             case WM_SHOWWINDOW:
525             case WM_ACTIVATE:
526             case WM_SETFOCUS:
527             case DM_SETDEFID:
528             case DM_GETDEFID:
529             case WM_NEXTDLGCTL:
530             case WM_GETFONT:
531             case WM_CLOSE:
532             case WM_NCDESTROY:
533             case WM_ENTERMENULOOP:
534             case WM_LBUTTONDOWN:
535             case WM_NCLBUTTONDOWN:
536                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
537             case WM_INITDIALOG:
538             case WM_VKEYTOITEM:
539             case WM_COMPAREITEM:
540             case WM_CHARTOITEM:
541                  break;
542
543             default:
544                  return DefWindowProcW( hwnd, msg, wParam, lParam );
545         }
546     }
547     return DEFDLG_Epilog(hwnd, msg, result);
548 }