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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/winuser16.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
33 /***********************************************************************
36 static WNDPROC DEFDLG_GetDlgProc( HWND hwnd )
39 WND *wndPtr = WIN_GetPtr( hwnd );
41 if (!wndPtr) return 0;
42 if (wndPtr == WND_OTHER_PROCESS)
44 ERR( "cannot get dlg proc %p from other process\n", hwnd );
47 ret = *(WNDPROC *)((char *)wndPtr->wExtra + DWL_DLGPROC);
48 WIN_ReleasePtr( wndPtr );
52 /***********************************************************************
55 * Set the focus to a control of the dialog, selecting the text if
56 * the control is an edit dialog.
58 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
60 HWND hwndPrev = GetFocus();
62 if (IsChild( hwndDlg, hwndPrev ))
64 if (SendMessageW( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
65 SendMessageW( hwndPrev, EM_SETSEL, -1, 0 );
67 if (SendMessageW( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
68 SendMessageW( hwndCtrl, EM_SETSEL, 0, -1 );
73 /***********************************************************************
76 static void DEFDLG_SaveFocus( HWND hwnd )
79 HWND hwndFocus = GetFocus();
81 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return;
82 if (!(infoPtr = DIALOG_get_info( hwnd ))) return;
83 infoPtr->hwndFocus = hwndFocus;
84 /* Remove default button */
88 /***********************************************************************
91 static void DEFDLG_RestoreFocus( HWND hwnd )
95 if (IsIconic( hwnd )) return;
96 if (!(infoPtr = DIALOG_get_info( hwnd ))) return;
97 if (!IsWindow( infoPtr->hwndFocus )) return;
98 /* Don't set the focus back to controls if EndDialog is already called.*/
99 if (!(infoPtr->flags & DF_END))
101 DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
104 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
105 sometimes losing focus when receiving WM_SETFOCUS messages. */
109 /***********************************************************************
110 * DEFDLG_FindDefButton
112 * Find the current default push-button.
114 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
116 HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
119 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
121 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
127 /***********************************************************************
128 * DEFDLG_SetDefButton
130 * Set the new default button to be hwndNew.
132 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
135 DWORD dlgcode=0; /* initialize just to avoid a warning */
137 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
138 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
139 return FALSE; /* Destination is not a push button */
141 if (dlgInfo->idResult) /* There's already a default pushbutton */
143 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
144 if (SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
145 SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
149 if(dlgcode==DLGC_UNDEFPUSHBUTTON)
150 SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
151 dlgInfo->idResult = GetDlgCtrlID( hwndNew );
153 else dlgInfo->idResult = 0;
158 /***********************************************************************
161 * Implementation of DefDlgProc(). Only handle messages that need special
162 * handling for dialogs.
164 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
165 LPARAM lParam, DIALOGINFO *dlgInfo )
171 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
172 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
176 HDC hdc = (HDC)wParam;
177 GetClientRect( hwnd, &rect );
178 DPtoLP( hdc, (LPPOINT)&rect, 2 );
179 FillRect( hdc, &rect, brush );
184 if ((dlgInfo = (DIALOGINFO *)SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, 0 )))
186 /* Free dialog heap (if created) */
187 if (dlgInfo->hDialogHeap)
189 GlobalUnlock16(dlgInfo->hDialogHeap);
190 GlobalFree16(dlgInfo->hDialogHeap);
192 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
193 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
194 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd ), WIN_PROC_WINDOW );
195 HeapFree( GetProcessHeap(), 0, dlgInfo );
197 /* Window clean-up */
198 return DefWindowProcA( hwnd, msg, wParam, lParam );
201 if (!wParam) DEFDLG_SaveFocus( hwnd );
202 return DefWindowProcA( hwnd, msg, wParam, lParam );
205 if (wParam) DEFDLG_RestoreFocus( hwnd );
206 else DEFDLG_SaveFocus( hwnd );
210 DEFDLG_RestoreFocus( hwnd );
214 if (dlgInfo && !(dlgInfo->flags & DF_END))
215 DEFDLG_SetDefButton( hwnd, dlgInfo, wParam ? GetDlgItem( hwnd, wParam ) : 0 );
219 if (dlgInfo && !(dlgInfo->flags & DF_END))
222 if (dlgInfo->idResult)
223 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
224 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
225 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
232 HWND hwndDest = (HWND)wParam;
234 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
235 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
236 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
240 case WM_ENTERMENULOOP:
242 case WM_NCLBUTTONDOWN:
244 HWND hwndFocus = GetFocus();
247 /* always make combo box hide its listbox control */
248 if (!SendMessageA( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
249 SendMessageA( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
252 return DefWindowProcA( hwnd, msg, wParam, lParam );
255 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
258 PostMessageA( hwnd, WM_COMMAND, IDCANCEL,
259 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
262 case WM_NOTIFYFORMAT:
263 return DefWindowProcA( hwnd, msg, wParam, lParam );
268 /***********************************************************************
271 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
275 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
276 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
277 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
278 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
281 return GetWindowLongA( hwnd, DWL_MSGRESULT );
284 /***********************************************************************
285 * DefDlgProc (USER.308)
287 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
291 HWND hwnd32 = WIN_Handle32( hwnd );
294 SetWindowLongW( hwnd32, DWL_MSGRESULT, 0 );
296 if ((dlgproc = (WNDPROC16)DEFDLG_GetDlgProc( hwnd32 )))
298 /* Call dialog procedure */
299 result = CallWindowProc16( dlgproc, hwnd, msg, wParam, lParam );
300 /* 16 bit dlg procs only return BOOL16 */
301 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
302 result = LOWORD(result);
305 if (!result && IsWindow(hwnd32))
307 /* callback didn't process this message */
321 case WM_ENTERMENULOOP:
323 case WM_NCLBUTTONDOWN:
324 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, DIALOG_get_info(hwnd32) );
332 return DefWindowProc16( hwnd, msg, wParam, lParam );
335 return DEFDLG_Epilog( hwnd32, msg, result);
339 /***********************************************************************
340 * DefDlgProcA (USER32.@)
342 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
347 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
349 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
351 /* Call dialog procedure */
352 result = CallWindowProcA( dlgproc, hwnd, msg, wParam, lParam );
353 /* 16 bit dlg procs only return BOOL16 */
354 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
355 result = LOWORD(result);
358 if (!result && IsWindow(hwnd))
360 /* callback didn't process this message */
374 case WM_ENTERMENULOOP:
376 case WM_NCLBUTTONDOWN:
377 return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
385 return DefWindowProcA( hwnd, msg, wParam, lParam );
388 return DEFDLG_Epilog(hwnd, msg, result);
392 /***********************************************************************
393 * DefDlgProcW (USER32.@)
395 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
400 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
402 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
404 /* Call dialog procedure */
405 result = CallWindowProcW( dlgproc, hwnd, msg, wParam, lParam );
406 /* 16 bit dlg procs only return BOOL16 */
407 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
408 result = LOWORD(result);
411 if (!result && IsWindow(hwnd))
413 /* callback didn't process this message */
427 case WM_ENTERMENULOOP:
429 case WM_NCLBUTTONDOWN:
430 return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
438 return DefWindowProcW( hwnd, msg, wParam, lParam );
441 return DEFDLG_Epilog(hwnd, msg, result);