Release 20030408.
[wine/gsoc-2012-control.git] / windows / defdlg.c
blobf6df0540ba2342d0cf43ab23bbefa84d0f179577
1 /*
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
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "wine/winuser16.h"
25 #include "controls.h"
26 #include "win.h"
27 #include "winproc.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
33 /***********************************************************************
34 * DEFDLG_GetDlgProc
36 static WNDPROC DEFDLG_GetDlgProc( HWND hwnd )
38 WNDPROC ret;
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 );
45 return 0;
47 ret = *(WNDPROC *)((char *)wndPtr->wExtra + DWL_DLGPROC);
48 WIN_ReleasePtr( wndPtr );
49 return ret;
52 /***********************************************************************
53 * DEFDLG_SetFocus
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 );
69 SetFocus( hwndCtrl );
73 /***********************************************************************
74 * DEFDLG_SaveFocus
76 static void DEFDLG_SaveFocus( HWND hwnd )
78 DIALOGINFO *infoPtr;
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 /***********************************************************************
89 * DEFDLG_RestoreFocus
91 static void DEFDLG_RestoreFocus( HWND hwnd )
93 DIALOGINFO *infoPtr;
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 );
102 return;
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 );
117 while (hwndChild)
119 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
120 break;
121 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
123 return hwndChild;
127 /***********************************************************************
128 * DEFDLG_SetDefButton
130 * Set the new default button to be hwndNew.
132 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
133 HWND hwndNew )
135 DWORD dlgcode=0; /* initialize just to avoid a warning */
136 if (hwndNew &&
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 );
147 if (hwndNew)
149 if(dlgcode==DLGC_UNDEFPUSHBUTTON)
150 SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
151 dlgInfo->idResult = GetDlgCtrlID( hwndNew );
153 else dlgInfo->idResult = 0;
154 return TRUE;
158 /***********************************************************************
159 * DEFDLG_Proc
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 )
167 switch(msg)
169 case WM_ERASEBKGND:
171 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
172 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
173 if (brush)
175 RECT rect;
176 HDC hdc = (HDC)wParam;
177 GetClientRect( hwnd, &rect );
178 DPtoLP( hdc, (LPPOINT)&rect, 2 );
179 FillRect( hdc, &rect, brush );
181 return 1;
183 case WM_NCDESTROY:
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 );
200 case WM_SHOWWINDOW:
201 if (!wParam) DEFDLG_SaveFocus( hwnd );
202 return DefWindowProcA( hwnd, msg, wParam, lParam );
204 case WM_ACTIVATE:
205 if (wParam) DEFDLG_RestoreFocus( hwnd );
206 else DEFDLG_SaveFocus( hwnd );
207 return 0;
209 case WM_SETFOCUS:
210 DEFDLG_RestoreFocus( hwnd );
211 return 0;
213 case DM_SETDEFID:
214 if (dlgInfo && !(dlgInfo->flags & DF_END))
215 DEFDLG_SetDefButton( hwnd, dlgInfo, wParam ? GetDlgItem( hwnd, wParam ) : 0 );
216 return 1;
218 case DM_GETDEFID:
219 if (dlgInfo && !(dlgInfo->flags & DF_END))
221 HWND hwndDefId;
222 if (dlgInfo->idResult)
223 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
224 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
225 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
227 return 0;
229 case WM_NEXTDLGCTL:
230 if (dlgInfo)
232 HWND hwndDest = (HWND)wParam;
233 if (!lParam)
234 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
235 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
236 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
238 return 0;
240 case WM_ENTERMENULOOP:
241 case WM_LBUTTONDOWN:
242 case WM_NCLBUTTONDOWN:
244 HWND hwndFocus = GetFocus();
245 if (hwndFocus)
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 );
254 case WM_GETFONT:
255 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
257 case WM_CLOSE:
258 PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
259 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
260 return 0;
262 case WM_NOTIFYFORMAT:
263 return DefWindowProcA( hwnd, msg, wParam, lParam );
265 return 0;
268 /***********************************************************************
269 * DEFDLG_Epilog
271 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
273 /* see SDK 3.1 */
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)
279 return fResult;
281 return GetWindowLongA( hwnd, DWL_MSGRESULT );
284 /***********************************************************************
285 * DefDlgProc (USER.308)
287 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
288 LPARAM lParam )
290 WNDPROC16 dlgproc;
291 HWND hwnd32 = WIN_Handle32( hwnd );
292 BOOL result = FALSE;
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( (WNDPROC)dlgproc ) == WIN_PROC_16 )
302 result = LOWORD(result);
305 if (!result && IsWindow(hwnd32))
307 /* callback didn't process this message */
309 switch(msg)
311 case WM_ERASEBKGND:
312 case WM_SHOWWINDOW:
313 case WM_ACTIVATE:
314 case WM_SETFOCUS:
315 case DM_SETDEFID:
316 case DM_GETDEFID:
317 case WM_NEXTDLGCTL:
318 case WM_GETFONT:
319 case WM_CLOSE:
320 case WM_NCDESTROY:
321 case WM_ENTERMENULOOP:
322 case WM_LBUTTONDOWN:
323 case WM_NCLBUTTONDOWN:
324 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, DIALOG_get_info(hwnd32) );
325 case WM_INITDIALOG:
326 case WM_VKEYTOITEM:
327 case WM_COMPAREITEM:
328 case WM_CHARTOITEM:
329 break;
331 default:
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 )
344 WNDPROC dlgproc;
345 BOOL result = FALSE;
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 */
362 switch(msg)
364 case WM_ERASEBKGND:
365 case WM_SHOWWINDOW:
366 case WM_ACTIVATE:
367 case WM_SETFOCUS:
368 case DM_SETDEFID:
369 case DM_GETDEFID:
370 case WM_NEXTDLGCTL:
371 case WM_GETFONT:
372 case WM_CLOSE:
373 case WM_NCDESTROY:
374 case WM_ENTERMENULOOP:
375 case WM_LBUTTONDOWN:
376 case WM_NCLBUTTONDOWN:
377 return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
378 case WM_INITDIALOG:
379 case WM_VKEYTOITEM:
380 case WM_COMPAREITEM:
381 case WM_CHARTOITEM:
382 break;
384 default:
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 )
397 BOOL result = FALSE;
398 WNDPROC dlgproc;
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 */
415 switch(msg)
417 case WM_ERASEBKGND:
418 case WM_SHOWWINDOW:
419 case WM_ACTIVATE:
420 case WM_SETFOCUS:
421 case DM_SETDEFID:
422 case DM_GETDEFID:
423 case WM_NEXTDLGCTL:
424 case WM_GETFONT:
425 case WM_CLOSE:
426 case WM_NCDESTROY:
427 case WM_ENTERMENULOOP:
428 case WM_LBUTTONDOWN:
429 case WM_NCLBUTTONDOWN:
430 return DEFDLG_Proc( hwnd, msg, wParam, lParam, DIALOG_get_info(hwnd) );
431 case WM_INITDIALOG:
432 case WM_VKEYTOITEM:
433 case WM_COMPAREITEM:
434 case WM_CHARTOITEM:
435 break;
437 default:
438 return DefWindowProcW( hwnd, msg, wParam, lParam );
441 return DEFDLG_Epilog(hwnd, msg, result);