user32: Don't flush window surfaces while waiting for a sent message reply.
[wine] / dlls / user32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "controls.h"
28 #include "win.h"
29 #include "user_private.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
33
34
35 /***********************************************************************
36  *           DEFDLG_GetDlgProc
37  */
38 static DLGPROC DEFDLG_GetDlgProc( HWND hwnd )
39 {
40     DLGPROC 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 = *(DLGPROC *)((char *)wndPtr->wExtra + DWLP_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 that has DLGC_HASSETSEL.
59  */
60 static void DEFDLG_SetFocus( HWND hwndCtrl )
61 {
62     if (SendMessageW( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
63         SendMessageW( hwndCtrl, EM_SETSEL, 0, -1 );
64     SetFocus( hwndCtrl );
65 }
66
67
68 /***********************************************************************
69  *           DEFDLG_SaveFocus
70  */
71 static void DEFDLG_SaveFocus( HWND hwnd )
72 {
73     DIALOGINFO *infoPtr;
74     HWND hwndFocus = GetFocus();
75
76     if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return;
77     if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
78     infoPtr->hwndFocus = hwndFocus;
79     /* Remove default button */
80 }
81
82
83 /***********************************************************************
84  *           DEFDLG_RestoreFocus
85  */
86 static void DEFDLG_RestoreFocus( HWND hwnd, BOOL justActivate )
87 {
88     DIALOGINFO *infoPtr;
89
90     if (IsIconic( hwnd )) return;
91     if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
92     /* Don't set the focus back to controls if EndDialog is already called.*/
93     if (infoPtr->flags & DF_END) return;
94     if (!IsWindow(infoPtr->hwndFocus) || infoPtr->hwndFocus == hwnd) {
95         if (justActivate) return;
96         /* If no saved focus control exists, set focus to the first visible,
97            non-disabled, WS_TABSTOP control in the dialog */
98         infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
99         /* If there are no WS_TABSTOP controls, set focus to the first visible,
100            non-disabled control in the dialog */
101         if (!infoPtr->hwndFocus) infoPtr->hwndFocus = GetNextDlgGroupItem( hwnd, 0, FALSE );
102         if (!IsWindow( infoPtr->hwndFocus )) return;
103     }
104     if (justActivate)
105         SetFocus( infoPtr->hwndFocus );
106     else
107         DEFDLG_SetFocus( 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 (GetWindowLongW( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
131         {
132             LONG dsStyle = GetWindowLongW( 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 || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
163         hwndOld = DEFDLG_FindDefButton( hwndDlg );
164     if (hwndOld && hwndOld != hwndNew)
165         SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
166
167     if (hwndNew)
168     {
169         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
170             SendMessageW( 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 || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
201         hwndOld = DEFDLG_FindDefButton( hwndDlg );
202     if (hwndOld && hwndOld != hwndNew)
203         SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
204
205     if (hwndNew)
206     {
207         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
208             SendMessageW( 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)
241             {
242                 WND *wndPtr;
243
244                 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
245                 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
246                 HeapFree( GetProcessHeap(), 0, dlgInfo );
247
248                 wndPtr = WIN_GetPtr( hwnd );
249                 wndPtr->dlgInfo = NULL;
250                 WIN_ReleasePtr( wndPtr );
251             }
252               /* Window clean-up */
253             return DefWindowProcA( hwnd, msg, wParam, lParam );
254
255         case WM_SHOWWINDOW:
256             if (!wParam) DEFDLG_SaveFocus( hwnd );
257             return DefWindowProcA( hwnd, msg, wParam, lParam );
258
259         case WM_ACTIVATE:
260             if (wParam) DEFDLG_RestoreFocus( hwnd, TRUE );
261             else DEFDLG_SaveFocus( hwnd );
262             return 0;
263
264         case WM_SETFOCUS:
265             DEFDLG_RestoreFocus( hwnd, FALSE );
266             return 0;
267
268         case DM_SETDEFID:
269             if (dlgInfo && !(dlgInfo->flags & DF_END))
270                 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
271             return 1;
272
273         case DM_GETDEFID:
274             if (dlgInfo && !(dlgInfo->flags & DF_END))
275             {
276                 HWND hwndDefId;
277                 if (dlgInfo->idResult)
278                     return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
279                 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
280                     return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
281             }
282             return 0;
283
284         case WM_NEXTDLGCTL:
285             if (dlgInfo)
286             {
287                 HWND hwndDest = (HWND)wParam;
288                 if (!lParam)
289                     hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
290                 if (hwndDest) DEFDLG_SetFocus( hwndDest );
291                 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
292             }
293             return 0;
294
295         case WM_ENTERMENULOOP:
296         case WM_LBUTTONDOWN:
297         case WM_NCLBUTTONDOWN:
298             {
299                 HWND hwndFocus = GetFocus();
300                 if (hwndFocus)
301                 {
302                     /* always make combo box hide its listbox control */
303                     if (!SendMessageW( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
304                         SendMessageW( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
305                 }
306             }
307             return DefWindowProcA( hwnd, msg, wParam, lParam );
308
309         case WM_GETFONT:
310             return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
311
312         case WM_CLOSE:
313             PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
314                             (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
315             return 0;
316     }
317     return 0;
318 }
319
320 /***********************************************************************
321 *               DIALOG_get_info
322 *
323 * Get the DIALOGINFO structure of a window, allocating it if needed
324 * and 'create' is TRUE.
325 */
326 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
327 {
328     WND* wndPtr;
329     DIALOGINFO* dlgInfo;
330
331     wndPtr = WIN_GetPtr( hwnd );
332     if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
333     {
334         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
335         return NULL;
336     }
337
338     dlgInfo = wndPtr->dlgInfo;
339
340     if (!dlgInfo && create)
341     {
342         if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) )))
343             goto out;
344         dlgInfo->hwndFocus   = 0;
345         dlgInfo->hUserFont   = 0;
346         dlgInfo->hMenu       = 0;
347         dlgInfo->xBaseUnit   = 0;
348         dlgInfo->yBaseUnit   = 0;
349         dlgInfo->idResult    = IDOK;
350         dlgInfo->flags       = 0;
351         wndPtr->dlgInfo = dlgInfo;
352     }
353
354 out:
355     WIN_ReleasePtr( wndPtr );
356     return dlgInfo;
357 }
358
359 /***********************************************************************
360  *              DefDlgProcA (USER32.@)
361  */
362 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
363 {
364     DIALOGINFO *dlgInfo;
365     DLGPROC dlgproc;
366     LRESULT result = 0;
367
368     /* Perform DIALOGINFO initialization if not done */
369     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
370
371     SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
372
373     if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
374         result = WINPROC_CallDlgProcA( dlgproc, hwnd, msg, wParam, lParam );
375
376     if (!result && IsWindow(hwnd))
377     {
378         /* callback didn't process this message */
379
380         switch(msg)
381         {
382             case WM_ERASEBKGND:
383             case WM_SHOWWINDOW:
384             case WM_ACTIVATE:
385             case WM_SETFOCUS:
386             case DM_SETDEFID:
387             case DM_GETDEFID:
388             case WM_NEXTDLGCTL:
389             case WM_GETFONT:
390             case WM_CLOSE:
391             case WM_NCDESTROY:
392             case WM_ENTERMENULOOP:
393             case WM_LBUTTONDOWN:
394             case WM_NCLBUTTONDOWN:
395                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
396             case WM_INITDIALOG:
397             case WM_VKEYTOITEM:
398             case WM_COMPAREITEM:
399             case WM_CHARTOITEM:
400                  break;
401
402             default:
403                  return DefWindowProcA( hwnd, msg, wParam, lParam );
404         }
405     }
406
407     if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
408          msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
409          msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
410          msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
411         return result;
412
413     return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
414 }
415
416
417 /***********************************************************************
418  *              DefDlgProcW (USER32.@)
419  */
420 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
421 {
422     DIALOGINFO *dlgInfo;
423     DLGPROC dlgproc;
424     LRESULT result = 0;
425
426     /* Perform DIALOGINFO initialization if not done */
427     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
428
429     SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
430
431     if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
432         result = WINPROC_CallDlgProcW( dlgproc, hwnd, msg, wParam, lParam );
433
434     if (!result && IsWindow(hwnd))
435     {
436         /* callback didn't process this message */
437
438         switch(msg)
439         {
440             case WM_ERASEBKGND:
441             case WM_SHOWWINDOW:
442             case WM_ACTIVATE:
443             case WM_SETFOCUS:
444             case DM_SETDEFID:
445             case DM_GETDEFID:
446             case WM_NEXTDLGCTL:
447             case WM_GETFONT:
448             case WM_CLOSE:
449             case WM_NCDESTROY:
450             case WM_ENTERMENULOOP:
451             case WM_LBUTTONDOWN:
452             case WM_NCLBUTTONDOWN:
453                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
454             case WM_INITDIALOG:
455             case WM_VKEYTOITEM:
456             case WM_COMPAREITEM:
457             case WM_CHARTOITEM:
458                  break;
459
460             default:
461                  return DefWindowProcW( hwnd, msg, wParam, lParam );
462         }
463     }
464
465     if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
466          msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
467          msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
468          msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
469         return result;
470
471     return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
472 }