2 * Default dialog procedure
4 * Copyright 1993, 1996 Alexandre Julliard
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.
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.
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
21 #include "user_private.h"
25 /***********************************************************************
28 * Set the focus to a control of the dialog, selecting the text if
29 * the control is an edit dialog that has DLGC_HASSETSEL.
31 static void DEFDLG_SetFocus( HWND hwndCtrl
)
33 if (SendMessageW( hwndCtrl
, WM_GETDLGCODE
, 0, 0 ) & DLGC_HASSETSEL
)
34 SendMessageW( hwndCtrl
, EM_SETSEL
, 0, -1 );
35 NtUserSetFocus( hwndCtrl
);
39 /***********************************************************************
42 static void DEFDLG_SaveFocus( HWND hwnd
)
45 HWND hwndFocus
= GetFocus();
47 if (!hwndFocus
|| !IsChild( hwnd
, hwndFocus
)) return;
48 if (!(infoPtr
= DIALOG_get_info( hwnd
, FALSE
))) return;
49 infoPtr
->hwndFocus
= hwndFocus
;
50 /* Remove default button */
54 /***********************************************************************
57 static void DEFDLG_RestoreFocus( HWND hwnd
, BOOL justActivate
)
61 if (IsIconic( hwnd
)) return;
62 if (!(infoPtr
= DIALOG_get_info( hwnd
, FALSE
))) return;
63 /* Don't set the focus back to controls if EndDialog is already called.*/
64 if (infoPtr
->flags
& DF_END
) return;
65 if (!IsWindow(infoPtr
->hwndFocus
) || infoPtr
->hwndFocus
== hwnd
) {
66 if (justActivate
) return;
67 /* If no saved focus control exists, set focus to the first visible,
68 non-disabled, WS_TABSTOP control in the dialog */
69 infoPtr
->hwndFocus
= GetNextDlgTabItem( hwnd
, 0, FALSE
);
70 /* If there are no WS_TABSTOP controls, set focus to the first visible,
71 non-disabled control in the dialog */
72 if (!infoPtr
->hwndFocus
) infoPtr
->hwndFocus
= GetNextDlgGroupItem( hwnd
, 0, FALSE
);
73 if (!IsWindow( infoPtr
->hwndFocus
)) return;
76 NtUserSetFocus( infoPtr
->hwndFocus
);
78 DEFDLG_SetFocus( infoPtr
->hwndFocus
);
79 infoPtr
->hwndFocus
= NULL
;
83 /***********************************************************************
84 * DEFDLG_FindDefButton
86 * Find the current default push-button.
88 static HWND
DEFDLG_FindDefButton( HWND hwndDlg
)
90 HWND hwndChild
, hwndTmp
;
92 hwndChild
= GetWindow( hwndDlg
, GW_CHILD
);
95 if (SendMessageW( hwndChild
, WM_GETDLGCODE
, 0, 0 ) & DLGC_DEFPUSHBUTTON
)
98 /* Recurse into WS_EX_CONTROLPARENT controls */
99 if (GetWindowLongW( hwndChild
, GWL_EXSTYLE
) & WS_EX_CONTROLPARENT
)
101 LONG dsStyle
= GetWindowLongW( hwndChild
, GWL_STYLE
);
102 if ((dsStyle
& WS_VISIBLE
) && !(dsStyle
& WS_DISABLED
) &&
103 (hwndTmp
= DEFDLG_FindDefButton(hwndChild
)) != NULL
)
106 hwndChild
= GetWindow( hwndChild
, GW_HWNDNEXT
);
112 /***********************************************************************
115 * Set the default button id.
117 static BOOL
DEFDLG_SetDefId( HWND hwndDlg
, DIALOGINFO
*dlgInfo
, WPARAM wParam
)
119 DWORD dlgcode
=0; /* initialize just to avoid a warning */
120 HWND hwndOld
, hwndNew
= GetDlgItem(hwndDlg
, wParam
);
121 INT old_id
= dlgInfo
->idResult
;
123 dlgInfo
->idResult
= wParam
;
125 !((dlgcode
=SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 ))
126 & (DLGC_UNDEFPUSHBUTTON
| DLGC_BUTTON
)))
127 return FALSE
; /* Destination is not a push button */
129 /* Make sure the old default control is a valid push button ID */
130 hwndOld
= GetDlgItem( hwndDlg
, old_id
);
131 if (!hwndOld
|| !(SendMessageW( hwndOld
, WM_GETDLGCODE
, 0, 0) & DLGC_DEFPUSHBUTTON
))
132 hwndOld
= DEFDLG_FindDefButton( hwndDlg
);
133 if (hwndOld
&& hwndOld
!= hwndNew
)
134 SendMessageW( hwndOld
, BM_SETSTYLE
, BS_PUSHBUTTON
, TRUE
);
138 if(dlgcode
& DLGC_UNDEFPUSHBUTTON
)
139 SendMessageW( hwndNew
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
145 /***********************************************************************
146 * DEFDLG_SetDefButton
148 * Set the new default button to be hwndNew.
150 static BOOL
DEFDLG_SetDefButton( HWND hwndDlg
, DIALOGINFO
*dlgInfo
, HWND hwndNew
)
152 DWORD dlgcode
=0; /* initialize just to avoid a warning */
153 HWND hwndOld
= GetDlgItem( hwndDlg
, dlgInfo
->idResult
);
156 !((dlgcode
=SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 ))
157 & (DLGC_UNDEFPUSHBUTTON
| DLGC_DEFPUSHBUTTON
)))
160 * Need to draw only default push button rectangle.
161 * Since the next control is not a push button, need to draw the push
162 * button rectangle for the default control.
165 dlgcode
= SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 );
168 /* Make sure the old default control is a valid push button ID */
169 if (!hwndOld
|| !(SendMessageW( hwndOld
, WM_GETDLGCODE
, 0, 0) & DLGC_DEFPUSHBUTTON
))
170 hwndOld
= DEFDLG_FindDefButton( hwndDlg
);
171 if (hwndOld
&& hwndOld
!= hwndNew
)
172 SendMessageW( hwndOld
, BM_SETSTYLE
, BS_PUSHBUTTON
, TRUE
);
176 if(dlgcode
& DLGC_UNDEFPUSHBUTTON
)
177 SendMessageW( hwndNew
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
183 /***********************************************************************
186 * Implementation of DefDlgProc(). Only handle messages that need special
187 * handling for dialogs.
189 static LRESULT
DEFDLG_Proc( HWND hwnd
, UINT msg
, WPARAM wParam
,
190 LPARAM lParam
, DIALOGINFO
*dlgInfo
)
196 HBRUSH brush
= (HBRUSH
)SendMessageW( hwnd
, WM_CTLCOLORDLG
, wParam
, (LPARAM
)hwnd
);
197 if (!brush
) brush
= (HBRUSH
)DefWindowProcW( hwnd
, WM_CTLCOLORDLG
, wParam
, (LPARAM
)hwnd
);
201 HDC hdc
= (HDC
)wParam
;
202 GetClientRect( hwnd
, &rect
);
203 DPtoLP( hdc
, (LPPOINT
)&rect
, 2 );
204 FillRect( hdc
, &rect
, brush
);
211 if (dlgInfo
->hUserFont
) DeleteObject( dlgInfo
->hUserFont
);
212 if (dlgInfo
->hMenu
) NtUserDestroyMenu( dlgInfo
->hMenu
);
213 HeapFree( GetProcessHeap(), 0, dlgInfo
);
215 NtUserSetDialogInfo( hwnd
, NULL
);
217 /* Window clean-up */
218 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
221 if (!wParam
) DEFDLG_SaveFocus( hwnd
);
222 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
225 if (wParam
) DEFDLG_RestoreFocus( hwnd
, TRUE
);
226 else DEFDLG_SaveFocus( hwnd
);
230 DEFDLG_RestoreFocus( hwnd
, FALSE
);
234 if (dlgInfo
&& !(dlgInfo
->flags
& DF_END
))
235 DEFDLG_SetDefId( hwnd
, dlgInfo
, wParam
);
239 if (dlgInfo
&& !(dlgInfo
->flags
& DF_END
))
242 if (dlgInfo
->idResult
)
243 return MAKELONG( dlgInfo
->idResult
, DC_HASDEFID
);
244 if ((hwndDefId
= DEFDLG_FindDefButton( hwnd
)))
245 return MAKELONG( GetDlgCtrlID( hwndDefId
), DC_HASDEFID
);
252 HWND hwndDest
= (HWND
)wParam
;
254 hwndDest
= GetNextDlgTabItem(hwnd
, GetFocus(), wParam
);
255 if (hwndDest
) DEFDLG_SetFocus( hwndDest
);
256 DEFDLG_SetDefButton( hwnd
, dlgInfo
, hwndDest
);
260 case WM_ENTERMENULOOP
:
262 case WM_NCLBUTTONDOWN
:
264 HWND hwndFocus
= GetFocus();
267 /* always make combo box hide its listbox control */
268 if (!SendMessageW( hwndFocus
, CB_SHOWDROPDOWN
, FALSE
, 0 ))
269 SendMessageW( GetParent(hwndFocus
), CB_SHOWDROPDOWN
, FALSE
, 0 );
272 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
275 return dlgInfo
? (LRESULT
)dlgInfo
->hUserFont
: 0;
278 PostMessageA( hwnd
, WM_COMMAND
, MAKEWPARAM(IDCANCEL
, BN_CLICKED
),
279 (LPARAM
)GetDlgItem( hwnd
, IDCANCEL
) );
285 /***********************************************************************
288 * Get the DIALOGINFO structure of a window, allocating it if needed
289 * and 'create' is TRUE.
291 DIALOGINFO
*DIALOG_get_info( HWND hwnd
, BOOL create
)
295 dlgInfo
= NtUserGetDialogInfo( hwnd
);
297 if (!dlgInfo
&& create
)
299 if (!(dlgInfo
= HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo
) )))
301 dlgInfo
->hwndFocus
= 0;
302 dlgInfo
->hUserFont
= 0;
304 dlgInfo
->xBaseUnit
= 0;
305 dlgInfo
->yBaseUnit
= 0;
306 dlgInfo
->idResult
= IDOK
;
308 NtUserSetDialogInfo( hwnd
, dlgInfo
);
314 static LRESULT
USER_DefDlgProcA( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
319 /* Perform DIALOGINFO initialization if not done */
320 if(!(dlgInfo
= DIALOG_get_info( hwnd
, TRUE
))) return 0;
322 SetWindowLongPtrW( hwnd
, DWLP_MSGRESULT
, 0 );
324 result
= WINPROC_CallDlgProcA( hwnd
, msg
, wParam
, lParam
);
326 if (!result
&& IsWindow(hwnd
))
328 /* callback didn't process this message */
342 case WM_ENTERMENULOOP
:
344 case WM_NCLBUTTONDOWN
:
345 return DEFDLG_Proc( hwnd
, msg
, wParam
, lParam
, dlgInfo
);
353 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
357 if ((msg
>= WM_CTLCOLORMSGBOX
&& msg
<= WM_CTLCOLORSTATIC
) ||
358 msg
== WM_CTLCOLOR
|| msg
== WM_COMPAREITEM
||
359 msg
== WM_VKEYTOITEM
|| msg
== WM_CHARTOITEM
||
360 msg
== WM_QUERYDRAGICON
|| msg
== WM_INITDIALOG
)
363 return GetWindowLongPtrW( hwnd
, DWLP_MSGRESULT
);
366 static LRESULT
USER_DefDlgProcW( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
371 /* Perform DIALOGINFO initialization if not done */
372 if(!(dlgInfo
= DIALOG_get_info( hwnd
, TRUE
))) return 0;
374 SetWindowLongPtrW( hwnd
, DWLP_MSGRESULT
, 0 );
375 result
= WINPROC_CallDlgProcW( hwnd
, msg
, wParam
, lParam
);
377 if (!result
&& IsWindow(hwnd
))
379 /* callback didn't process this message */
393 case WM_ENTERMENULOOP
:
395 case WM_NCLBUTTONDOWN
:
396 return DEFDLG_Proc( hwnd
, msg
, wParam
, lParam
, dlgInfo
);
404 return DefWindowProcW( hwnd
, msg
, wParam
, lParam
);
408 if ((msg
>= WM_CTLCOLORMSGBOX
&& msg
<= WM_CTLCOLORSTATIC
) ||
409 msg
== WM_CTLCOLOR
|| msg
== WM_COMPAREITEM
||
410 msg
== WM_VKEYTOITEM
|| msg
== WM_CHARTOITEM
||
411 msg
== WM_QUERYDRAGICON
|| msg
== WM_INITDIALOG
)
414 return GetWindowLongPtrW( hwnd
, DWLP_MSGRESULT
);
417 LRESULT WINAPI
USER_DefDlgProc( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
420 return USER_DefDlgProcW( hwnd
, msg
, wParam
, lParam
);
422 return USER_DefDlgProcA( hwnd
, msg
, wParam
, lParam
);
425 /***********************************************************************
426 * DefDlgProcA (USER32.@)
428 LRESULT WINAPI
DefDlgProcA( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
430 return user_api
->pDefDlgProc( hwnd
, msg
, wParam
, lParam
, FALSE
);
433 /***********************************************************************
434 * DefDlgProcW (USER32.@)
436 LRESULT WINAPI
DefDlgProcW( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
438 return user_api
->pDefDlgProc( hwnd
, msg
, wParam
, lParam
, TRUE
);