Make FindFirstFileExW and FindFirstChangeNotificationW use NtOpenFile
[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 ))) 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 ))) return;
99     if (!IsWindow( infoPtr->hwndFocus )) return;
100     /* Don't set the focus back to controls if EndDialog is already called.*/
101     if (!(infoPtr->flags & DF_END))
102     {
103         DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
104         return;
105     }
106     /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
107        sometimes losing focus when receiving WM_SETFOCUS messages. */
108 }
109
110
111 /***********************************************************************
112  *           DEFDLG_FindDefButton
113  *
114  * Find the current default push-button.
115  */
116 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
117 {
118     HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
119     while (hwndChild)
120     {
121         if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
122             break;
123         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
124     }
125     return hwndChild;
126 }
127
128
129 /***********************************************************************
130  *           DEFDLG_SetDefButton
131  *
132  * Set the new default button to be hwndNew.
133  */
134 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
135                                    HWND hwndNew )
136 {
137     DWORD dlgcode=0; /* initialize just to avoid a warning */
138     if (hwndNew &&
139         !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
140             & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
141         return FALSE;  /* Destination is not a push button */
142
143     if (dlgInfo->idResult)  /* There's already a default pushbutton */
144     {
145         HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
146         if (SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
147             SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
148     }
149     if (hwndNew)
150     {
151         if(dlgcode==DLGC_UNDEFPUSHBUTTON)
152             SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
153         dlgInfo->idResult = GetDlgCtrlID( hwndNew );
154     }
155     else dlgInfo->idResult = 0;
156     return TRUE;
157 }
158
159
160 /***********************************************************************
161  *           DEFDLG_Proc
162  *
163  * Implementation of DefDlgProc(). Only handle messages that need special
164  * handling for dialogs.
165  */
166 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
167                             LPARAM lParam, DIALOGINFO *dlgInfo )
168 {
169     switch(msg)
170     {
171         case WM_ERASEBKGND:
172         {
173             HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
174             if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
175             if (brush)
176             {
177                 RECT rect;
178                 HDC hdc = (HDC)wParam;
179                 GetClientRect( hwnd, &rect );
180                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
181                 FillRect( hdc, &rect, brush );
182             }
183             return 1;
184         }
185         case WM_NCDESTROY:
186             if ((dlgInfo = (DIALOGINFO *)SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, 0 )))
187             {
188                 /* Free dialog heap (if created) */
189                 if (dlgInfo->hDialogHeap)
190                 {
191                     GlobalUnlock16(dlgInfo->hDialogHeap);
192                     GlobalFree16(dlgInfo->hDialogHeap);
193                 }
194                 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
195                 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
196                 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd ), WIN_PROC_WINDOW );
197                 HeapFree( GetProcessHeap(), 0, dlgInfo );
198             }
199               /* Window clean-up */
200             return DefWindowProcA( hwnd, msg, wParam, lParam );
201
202         case WM_SHOWWINDOW:
203             if (!wParam) DEFDLG_SaveFocus( hwnd );
204             return DefWindowProcA( hwnd, msg, wParam, lParam );
205
206         case WM_ACTIVATE:
207             if (wParam) DEFDLG_RestoreFocus( hwnd );
208             else DEFDLG_SaveFocus( hwnd );
209             return 0;
210
211         case WM_SETFOCUS:
212             DEFDLG_RestoreFocus( hwnd );
213             return 0;
214
215         case DM_SETDEFID:
216             if (dlgInfo && !(dlgInfo->flags & DF_END))
217                 DEFDLG_SetDefButton( hwnd, dlgInfo, wParam ? GetDlgItem( hwnd, wParam ) : 0 );
218             return 1;
219
220         case DM_GETDEFID:
221             if (dlgInfo && !(dlgInfo->flags & DF_END))
222             {
223                 HWND hwndDefId;
224                 if (dlgInfo->idResult)
225                     return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
226                 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
227                     return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
228             }
229             return 0;
230
231         case WM_NEXTDLGCTL:
232             if (dlgInfo)
233             {
234                 HWND hwndDest = (HWND)wParam;
235                 if (!lParam)
236                     hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
237                 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
238                 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
239             }
240             return 0;
241
242         case WM_ENTERMENULOOP:
243         case WM_LBUTTONDOWN:
244         case WM_NCLBUTTONDOWN:
245             {
246                 HWND hwndFocus = GetFocus();
247                 if (hwndFocus)
248                 {
249                     /* always make combo box hide its listbox control */
250                     if (!SendMessageA( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
251                         SendMessageA( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
252                 }
253             }
254             return DefWindowProcA( hwnd, msg, wParam, lParam );
255
256         case WM_GETFONT:
257             return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
258
259         case WM_CLOSE:
260             PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
261                             (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
262             return 0;
263
264         case WM_NOTIFYFORMAT:
265             return DefWindowProcA( hwnd, msg, wParam, lParam );
266     }
267     return 0;
268 }
269
270 /***********************************************************************
271  *           DEFDLG_Epilog
272  */
273 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
274 {
275     /* see SDK 3.1 */
276
277     if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
278          msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
279          msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
280          msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
281         return fResult;
282
283     return GetWindowLongA( hwnd, DWL_MSGRESULT );
284 }
285
286 /***********************************************************************
287  *              DefDlgProc (USER.308)
288  */
289 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
290                              LPARAM lParam )
291 {
292     WNDPROC16 dlgproc;
293     HWND hwnd32 = WIN_Handle32( hwnd );
294     BOOL result = FALSE;
295
296     SetWindowLongW( hwnd32, DWL_MSGRESULT, 0 );
297
298     if ((dlgproc = (WNDPROC16)DEFDLG_GetDlgProc( hwnd32 )))
299     {
300         /* Call dialog procedure */
301         result = CallWindowProc16( dlgproc, hwnd, msg, wParam, lParam );
302         /* 16 bit dlg procs only return BOOL16 */
303         if( WINPROC_GetProcType( (WNDPROC)dlgproc ) == WIN_PROC_16 )
304             result = LOWORD(result);
305     }
306
307     if (!result && IsWindow(hwnd32))
308     {
309         /* callback didn't process this message */
310
311         switch(msg)
312         {
313             case WM_ERASEBKGND:
314             case WM_SHOWWINDOW:
315             case WM_ACTIVATE:
316             case WM_SETFOCUS:
317             case DM_SETDEFID:
318             case DM_GETDEFID:
319             case WM_NEXTDLGCTL:
320             case WM_GETFONT:
321             case WM_CLOSE:
322             case WM_NCDESTROY:
323             case WM_ENTERMENULOOP:
324             case WM_LBUTTONDOWN:
325             case WM_NCLBUTTONDOWN:
326                 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, DIALOG_get_info(hwnd32) );
327             case WM_INITDIALOG:
328             case WM_VKEYTOITEM:
329             case WM_COMPAREITEM:
330             case WM_CHARTOITEM:
331                 break;
332
333             default:
334                 return DefWindowProc16( hwnd, msg, wParam, lParam );
335         }
336     }
337     return DEFDLG_Epilog( hwnd32, msg, result);
338 }
339
340
341 /***********************************************************************
342  *              DefDlgProcA (USER32.@)
343  */
344 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
345 {
346     WNDPROC dlgproc;
347     BOOL result = FALSE;
348
349     SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
350
351     if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
352     {
353         /* Call dialog procedure */
354         result = CallWindowProcA( dlgproc, hwnd, msg, wParam, lParam );
355         /* 16 bit dlg procs only return BOOL16 */
356         if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
357             result = LOWORD(result);
358     }
359
360     if (!result && IsWindow(hwnd))
361     {
362         /* callback didn't process this message */
363
364         switch(msg)
365         {
366             case WM_ERASEBKGND:
367             case WM_SHOWWINDOW:
368             case WM_ACTIVATE:
369             case WM_SETFOCUS:
370             case DM_SETDEFID:
371             case DM_GETDEFID:
372             case WM_NEXTDLGCTL:
373             case WM_GETFONT:
374             case WM_CLOSE:
375             case WM_NCDESTROY:
376             case WM_ENTERMENULOOP:
377             case WM_LBUTTONDOWN:
378             case WM_NCLBUTTONDOWN:
379                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
380             case WM_INITDIALOG:
381             case WM_VKEYTOITEM:
382             case WM_COMPAREITEM:
383             case WM_CHARTOITEM:
384                  break;
385
386             default:
387                  return DefWindowProcA( hwnd, msg, wParam, lParam );
388         }
389     }
390     return DEFDLG_Epilog(hwnd, msg, result);
391 }
392
393
394 /***********************************************************************
395  *              DefDlgProcW (USER32.@)
396  */
397 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
398 {
399     BOOL result = FALSE;
400     WNDPROC dlgproc;
401
402     SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
403
404     if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
405     {
406         /* Call dialog procedure */
407         result = CallWindowProcW( dlgproc, hwnd, msg, wParam, lParam );
408         /* 16 bit dlg procs only return BOOL16 */
409         if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
410             result = LOWORD(result);
411     }
412
413     if (!result && IsWindow(hwnd))
414     {
415         /* callback didn't process this message */
416
417         switch(msg)
418         {
419             case WM_ERASEBKGND:
420             case WM_SHOWWINDOW:
421             case WM_ACTIVATE:
422             case WM_SETFOCUS:
423             case DM_SETDEFID:
424             case DM_GETDEFID:
425             case WM_NEXTDLGCTL:
426             case WM_GETFONT:
427             case WM_CLOSE:
428             case WM_NCDESTROY:
429             case WM_ENTERMENULOOP:
430             case WM_LBUTTONDOWN:
431             case WM_NCLBUTTONDOWN:
432                  return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
433             case WM_INITDIALOG:
434             case WM_VKEYTOITEM:
435             case WM_COMPAREITEM:
436             case WM_CHARTOITEM:
437                  break;
438
439             default:
440                  return DefWindowProcW( hwnd, msg, wParam, lParam );
441         }
442     }
443     return DEFDLG_Epilog(hwnd, msg, result);
444 }