user32: Run the explorer process from the system directory.
[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 "wine/winuser16.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.
59  */
60 static void DEFDLG_SetFocus( HWND hwndDlg, 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 )
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 no saved focus control exists, set focus to the first visible,
96            non-disabled, WS_TABSTOP control in the dialog */
97         infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
98         if (!IsWindow( infoPtr->hwndFocus )) return;
99     }
100     DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
101
102     /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
103        sometimes losing focus when receiving WM_SETFOCUS messages. */
104 }
105
106
107 /***********************************************************************
108  *           DEFDLG_FindDefButton
109  *
110  * Find the current default push-button.
111  */
112 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
113 {
114     HWND hwndChild, hwndTmp;
115
116     hwndChild = GetWindow( hwndDlg, GW_CHILD );
117     while (hwndChild)
118     {
119         if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
120             break;
121
122         /* Recurse into WS_EX_CONTROLPARENT controls */
123         if (GetWindowLongW( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
124         {
125             LONG dsStyle = GetWindowLongW( hwndChild, GWL_STYLE );
126             if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
127                 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
128            return hwndTmp;
129         }
130         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
131     }
132     return hwndChild;
133 }
134
135
136 /***********************************************************************
137  *           DEFDLG_SetDefId
138  *
139  * Set the default button id.
140  */
141 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
142 {
143     DWORD dlgcode=0; /* initialize just to avoid a warning */
144     HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
145     INT old_id = dlgInfo->idResult;
146
147     dlgInfo->idResult = wParam;
148     if (hwndNew &&
149         !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
150             & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
151         return FALSE;  /* Destination is not a push button */
152
153     /* Make sure the old default control is a valid push button ID */
154     hwndOld = GetDlgItem( hwndDlg, old_id );
155     if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
156         hwndOld = DEFDLG_FindDefButton( hwndDlg );
157     if (hwndOld && hwndOld != hwndNew)
158         SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
159
160     if (hwndNew)
161     {
162         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
163             SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
164     }
165     return TRUE;
166 }
167
168
169 /***********************************************************************
170  *           DEFDLG_SetDefButton
171  *
172  * Set the new default button to be hwndNew.
173  */
174 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
175 {
176     DWORD dlgcode=0; /* initialize just to avoid a warning */
177     HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
178
179     if (hwndNew &&
180         !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
181             & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
182     {
183         /**
184          * Need to draw only default push button rectangle.
185          * Since the next control is not a push button, need to draw the push
186          * button rectangle for the default control.
187          */
188         hwndNew = hwndOld;
189         dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
190     }
191
192     /* Make sure the old default control is a valid push button ID */
193     if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
194         hwndOld = DEFDLG_FindDefButton( hwndDlg );
195     if (hwndOld && hwndOld != hwndNew)
196         SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
197
198     if (hwndNew)
199     {
200         if(dlgcode & DLGC_UNDEFPUSHBUTTON)
201             SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
202     }
203     return TRUE;
204 }
205
206
207 /***********************************************************************
208  *           DEFDLG_Proc
209  *
210  * Implementation of DefDlgProc(). Only handle messages that need special
211  * handling for dialogs.
212  */
213 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
214                             LPARAM lParam, DIALOGINFO *dlgInfo )
215 {
216     switch(msg)
217     {
218         case WM_ERASEBKGND:
219         {
220             HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
221             if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
222             if (brush)
223             {
224                 RECT rect;
225                 HDC hdc = (HDC)wParam;
226                 GetClientRect( hwnd, &rect );
227                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
228                 FillRect( hdc, &rect, brush );
229             }
230             return 1;
231         }
232         case WM_NCDESTROY:
233             if (dlgInfo)
234             {
235                 WND *wndPtr;
236
237                 /* Free dialog heap (if created) */
238                 if (dlgInfo->hDialogHeap)
239                 {
240                     GlobalUnlock16(dlgInfo->hDialogHeap);
241                     GlobalFree16(dlgInfo->hDialogHeap);
242                 }
243                 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
244                 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
245                 HeapFree( GetProcessHeap(), 0, dlgInfo );
246
247                 wndPtr = WIN_GetPtr( hwnd );
248                 wndPtr->dlgInfo = NULL;
249                 WIN_ReleasePtr( wndPtr );
250             }
251               /* Window clean-up */
252             return DefWindowProcA( hwnd, msg, wParam, lParam );
253
254         case WM_SHOWWINDOW:
255             if (!wParam) DEFDLG_SaveFocus( hwnd );
256             return DefWindowProcA( hwnd, msg, wParam, lParam );
257
258         case WM_ACTIVATE:
259             if (wParam) DEFDLG_RestoreFocus( hwnd );
260             else DEFDLG_SaveFocus( hwnd );
261             return 0;
262
263         case WM_SETFOCUS:
264             DEFDLG_RestoreFocus( hwnd );
265             return 0;
266
267         case DM_SETDEFID:
268             if (dlgInfo && !(dlgInfo->flags & DF_END))
269                 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
270             return 1;
271
272         case DM_GETDEFID:
273             if (dlgInfo && !(dlgInfo->flags & DF_END))
274             {
275                 HWND hwndDefId;
276                 if (dlgInfo->idResult)
277                     return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
278                 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
279                     return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
280             }
281             return 0;
282
283         case WM_NEXTDLGCTL:
284             if (dlgInfo)
285             {
286                 HWND hwndDest = (HWND)wParam;
287                 if (!lParam)
288                     hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
289                 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
290                 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
291             }
292             return 0;
293
294         case WM_ENTERMENULOOP:
295         case WM_LBUTTONDOWN:
296         case WM_NCLBUTTONDOWN:
297             {
298                 HWND hwndFocus = GetFocus();
299                 if (hwndFocus)
300                 {
301                     /* always make combo box hide its listbox control */
302                     if (!SendMessageW( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
303                         SendMessageW( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
304                 }
305             }
306             return DefWindowProcA( hwnd, msg, wParam, lParam );
307
308         case WM_GETFONT:
309             return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
310
311         case WM_CLOSE:
312             PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
313                             (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
314             return 0;
315     }
316     return 0;
317 }
318
319 /***********************************************************************
320  *           DEFDLG_Epilog
321  */
322 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
323 {
324     /* see SDK 3.1 */
325
326     if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
327          msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
328          msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
329          msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
330         return fResult;
331
332     return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
333 }
334
335 /***********************************************************************
336 *               DIALOG_get_info
337 *
338 * Get the DIALOGINFO structure of a window, allocating it if needed
339 * and 'create' is TRUE.
340 */
341 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
342 {
343     WND* wndPtr;
344     DIALOGINFO* dlgInfo;
345
346     wndPtr = WIN_GetPtr( hwnd );
347     if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
348     {
349         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
350         return NULL;
351     }
352
353     dlgInfo = wndPtr->dlgInfo;
354
355     if (!dlgInfo && create)
356     {
357         if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) )))
358             goto out;
359         dlgInfo->hwndFocus   = 0;
360         dlgInfo->hUserFont   = 0;
361         dlgInfo->hMenu       = 0;
362         dlgInfo->xBaseUnit   = 0;
363         dlgInfo->yBaseUnit   = 0;
364         dlgInfo->idResult    = 0;
365         dlgInfo->flags       = 0;
366         dlgInfo->hDialogHeap = 0;
367         wndPtr->dlgInfo = dlgInfo;
368         wndPtr->flags |= WIN_ISDIALOG;
369     }
370
371 out:
372     WIN_ReleasePtr( wndPtr );
373     return dlgInfo;
374 }
375
376 /***********************************************************************
377  *              DefDlgProc (USER.308)
378  */
379 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
380                              LPARAM lParam )
381 {
382     DIALOGINFO *dlgInfo;
383     DLGPROC16 dlgproc;
384     HWND hwnd32 = WIN_Handle32( hwnd );
385     BOOL result = FALSE;
386
387     /* Perform DIALOGINFO initialization if not done */
388     if(!(dlgInfo = DIALOG_get_info(hwnd32, TRUE))) return 0;
389
390     SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, 0 );
391
392     if ((dlgproc = (DLGPROC16)DEFDLG_GetDlgProc( hwnd32 ))) /* Call dialog procedure */
393         result = WINPROC_CallDlgProc16( dlgproc, hwnd, msg, wParam, lParam );
394
395     if (!result && IsWindow(hwnd32))
396     {
397         /* callback didn't process this message */
398
399         switch(msg)
400         {
401             case WM_ERASEBKGND:
402             case WM_SHOWWINDOW:
403             case WM_ACTIVATE:
404             case WM_SETFOCUS:
405             case DM_SETDEFID:
406             case DM_GETDEFID:
407             case WM_NEXTDLGCTL:
408             case WM_GETFONT:
409             case WM_CLOSE:
410             case WM_NCDESTROY:
411             case WM_ENTERMENULOOP:
412             case WM_LBUTTONDOWN:
413             case WM_NCLBUTTONDOWN:
414                 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, dlgInfo );
415             case WM_INITDIALOG:
416             case WM_VKEYTOITEM:
417             case WM_COMPAREITEM:
418             case WM_CHARTOITEM:
419                 break;
420
421             default:
422                 return DefWindowProc16( hwnd, msg, wParam, lParam );
423         }
424     }
425     return DEFDLG_Epilog( hwnd32, msg, result);
426 }
427
428
429 /***********************************************************************
430  *              DefDlgProcA (USER32.@)
431  */
432 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
433 {
434     DIALOGINFO *dlgInfo;
435     DLGPROC dlgproc;
436     BOOL result = FALSE;
437
438     /* Perform DIALOGINFO initialization if not done */
439     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
440
441     SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
442
443     if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
444         result = WINPROC_CallDlgProcA( dlgproc, hwnd, msg, wParam, lParam );
445
446     if (!result && IsWindow(hwnd))
447     {
448         /* callback didn't process this message */
449
450         switch(msg)
451         {
452             case WM_ERASEBKGND:
453             case WM_SHOWWINDOW:
454             case WM_ACTIVATE:
455             case WM_SETFOCUS:
456             case DM_SETDEFID:
457             case DM_GETDEFID:
458             case WM_NEXTDLGCTL:
459             case WM_GETFONT:
460             case WM_CLOSE:
461             case WM_NCDESTROY:
462             case WM_ENTERMENULOOP:
463             case WM_LBUTTONDOWN:
464             case WM_NCLBUTTONDOWN:
465                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
466             case WM_INITDIALOG:
467             case WM_VKEYTOITEM:
468             case WM_COMPAREITEM:
469             case WM_CHARTOITEM:
470                  break;
471
472             default:
473                  return DefWindowProcA( hwnd, msg, wParam, lParam );
474         }
475     }
476     return DEFDLG_Epilog(hwnd, msg, result);
477 }
478
479
480 /***********************************************************************
481  *              DefDlgProcW (USER32.@)
482  */
483 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
484 {
485     DIALOGINFO *dlgInfo;
486     BOOL result = FALSE;
487     DLGPROC dlgproc;
488
489     /* Perform DIALOGINFO initialization if not done */
490     if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
491
492     SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
493
494     if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
495         result = WINPROC_CallDlgProcW( dlgproc, hwnd, msg, wParam, lParam );
496
497     if (!result && IsWindow(hwnd))
498     {
499         /* callback didn't process this message */
500
501         switch(msg)
502         {
503             case WM_ERASEBKGND:
504             case WM_SHOWWINDOW:
505             case WM_ACTIVATE:
506             case WM_SETFOCUS:
507             case DM_SETDEFID:
508             case DM_GETDEFID:
509             case WM_NEXTDLGCTL:
510             case WM_GETFONT:
511             case WM_CLOSE:
512             case WM_NCDESTROY:
513             case WM_ENTERMENULOOP:
514             case WM_LBUTTONDOWN:
515             case WM_NCLBUTTONDOWN:
516                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
517             case WM_INITDIALOG:
518             case WM_VKEYTOITEM:
519             case WM_COMPAREITEM:
520             case WM_CHARTOITEM:
521                  break;
522
523             default:
524                  return DefWindowProcW( hwnd, msg, wParam, lParam );
525         }
526     }
527     return DEFDLG_Epilog(hwnd, msg, result);
528 }