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
26 #include "wine/winuser16.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog
);
35 /***********************************************************************
38 static WNDPROC
DEFDLG_GetDlgProc( HWND hwnd
)
41 WND
*wndPtr
= WIN_GetPtr( hwnd
);
43 if (!wndPtr
) return 0;
44 if (wndPtr
== WND_OTHER_PROCESS
)
46 ERR( "cannot get dlg proc %p from other process\n", hwnd
);
49 ret
= *(WNDPROC
*)((char *)wndPtr
->wExtra
+ DWL_DLGPROC
);
50 WIN_ReleasePtr( wndPtr
);
54 /***********************************************************************
57 * Set the focus to a control of the dialog, selecting the text if
58 * the control is an edit dialog.
60 static void DEFDLG_SetFocus( HWND hwndDlg
, HWND hwndCtrl
)
62 HWND hwndPrev
= GetFocus();
64 if (IsChild( hwndDlg
, hwndPrev
))
66 if (SendMessageW( hwndPrev
, WM_GETDLGCODE
, 0, 0 ) & DLGC_HASSETSEL
)
67 SendMessageW( hwndPrev
, EM_SETSEL
, -1, 0 );
69 if (SendMessageW( hwndCtrl
, WM_GETDLGCODE
, 0, 0 ) & DLGC_HASSETSEL
)
70 SendMessageW( hwndCtrl
, EM_SETSEL
, 0, -1 );
75 /***********************************************************************
78 static void DEFDLG_SaveFocus( HWND hwnd
)
81 HWND hwndFocus
= GetFocus();
83 if (!hwndFocus
|| !IsChild( hwnd
, hwndFocus
)) return;
84 if (!(infoPtr
= DIALOG_get_info( hwnd
, FALSE
))) return;
85 infoPtr
->hwndFocus
= hwndFocus
;
86 /* Remove default button */
90 /***********************************************************************
93 static void DEFDLG_RestoreFocus( HWND hwnd
)
97 if (IsIconic( hwnd
)) return;
98 if (!(infoPtr
= DIALOG_get_info( hwnd
, FALSE
))) 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
))
103 DEFDLG_SetFocus( hwnd
, infoPtr
->hwndFocus
);
106 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
107 sometimes losing focus when receiving WM_SETFOCUS messages. */
111 /***********************************************************************
112 * DEFDLG_FindDefButton
114 * Find the current default push-button.
116 static HWND
DEFDLG_FindDefButton( HWND hwndDlg
)
118 HWND hwndChild
, hwndTmp
;
120 hwndChild
= GetWindow( hwndDlg
, GW_CHILD
);
123 if (SendMessageW( hwndChild
, WM_GETDLGCODE
, 0, 0 ) & DLGC_DEFPUSHBUTTON
)
126 /* Recurse into WS_EX_CONTROLPARENT controls */
127 if (GetWindowLongA( hwndChild
, GWL_EXSTYLE
) & WS_EX_CONTROLPARENT
)
129 LONG dsStyle
= GetWindowLongA( hwndChild
, GWL_STYLE
);
130 if ((dsStyle
& WS_VISIBLE
) && !(dsStyle
& WS_DISABLED
) &&
131 (hwndTmp
= DEFDLG_FindDefButton(hwndChild
)) != NULL
)
134 hwndChild
= GetWindow( hwndChild
, GW_HWNDNEXT
);
140 /***********************************************************************
143 * Set the default button id.
145 static BOOL
DEFDLG_SetDefId( HWND hwndDlg
, DIALOGINFO
*dlgInfo
, WPARAM wParam
)
147 DWORD dlgcode
=0; /* initialize just to avoid a warning */
148 HWND hwndOld
, hwndNew
= GetDlgItem(hwndDlg
, wParam
);
149 INT old_id
= dlgInfo
->idResult
;
151 dlgInfo
->idResult
= wParam
;
153 !((dlgcode
=SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 ))
154 & (DLGC_UNDEFPUSHBUTTON
| DLGC_BUTTON
)))
155 return FALSE
; /* Destination is not a push button */
157 /* Make sure the old default control is a valid push button ID */
158 hwndOld
= GetDlgItem( hwndDlg
, old_id
);
159 if (!hwndOld
|| !(SendMessageA( hwndOld
, WM_GETDLGCODE
, 0, 0) & DLGC_DEFPUSHBUTTON
))
160 hwndOld
= DEFDLG_FindDefButton( hwndDlg
);
161 if (hwndOld
&& hwndOld
!= hwndNew
)
162 SendMessageA( hwndOld
, BM_SETSTYLE
, BS_PUSHBUTTON
, TRUE
);
166 if(dlgcode
& DLGC_UNDEFPUSHBUTTON
)
167 SendMessageA( hwndNew
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
173 /***********************************************************************
174 * DEFDLG_SetDefButton
176 * Set the new default button to be hwndNew.
178 static BOOL
DEFDLG_SetDefButton( HWND hwndDlg
, DIALOGINFO
*dlgInfo
, HWND hwndNew
)
180 DWORD dlgcode
=0; /* initialize just to avoid a warning */
181 HWND hwndOld
= GetDlgItem( hwndDlg
, dlgInfo
->idResult
);
184 !((dlgcode
=SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 ))
185 & (DLGC_UNDEFPUSHBUTTON
| DLGC_DEFPUSHBUTTON
)))
188 * Need to draw only default push button rectangle.
189 * Since the next control is not a push button, need to draw the push
190 * button rectangle for the default control.
193 dlgcode
= SendMessageW(hwndNew
, WM_GETDLGCODE
, 0, 0 );
196 /* Make sure the old default control is a valid push button ID */
197 if (!hwndOld
|| !(SendMessageA( hwndOld
, WM_GETDLGCODE
, 0, 0) & DLGC_DEFPUSHBUTTON
))
198 hwndOld
= DEFDLG_FindDefButton( hwndDlg
);
199 if (hwndOld
&& hwndOld
!= hwndNew
)
200 SendMessageA( hwndOld
, BM_SETSTYLE
, BS_PUSHBUTTON
, TRUE
);
204 if(dlgcode
& DLGC_UNDEFPUSHBUTTON
)
205 SendMessageA( hwndNew
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
211 /***********************************************************************
214 * Implementation of DefDlgProc(). Only handle messages that need special
215 * handling for dialogs.
217 static LRESULT
DEFDLG_Proc( HWND hwnd
, UINT msg
, WPARAM wParam
,
218 LPARAM lParam
, DIALOGINFO
*dlgInfo
)
224 HBRUSH brush
= (HBRUSH
)SendMessageW( hwnd
, WM_CTLCOLORDLG
, wParam
, (LPARAM
)hwnd
);
225 if (!brush
) brush
= (HBRUSH
)DefWindowProcW( hwnd
, WM_CTLCOLORDLG
, wParam
, (LPARAM
)hwnd
);
229 HDC hdc
= (HDC
)wParam
;
230 GetClientRect( hwnd
, &rect
);
231 DPtoLP( hdc
, (LPPOINT
)&rect
, 2 );
232 FillRect( hdc
, &rect
, brush
);
237 if ((dlgInfo
= (DIALOGINFO
*)SetWindowLongW( hwnd
, DWL_WINE_DIALOGINFO
, 0 )))
239 /* Free dialog heap (if created) */
240 if (dlgInfo
->hDialogHeap
)
242 GlobalUnlock16(dlgInfo
->hDialogHeap
);
243 GlobalFree16(dlgInfo
->hDialogHeap
);
245 if (dlgInfo
->hUserFont
) DeleteObject( dlgInfo
->hUserFont
);
246 if (dlgInfo
->hMenu
) DestroyMenu( dlgInfo
->hMenu
);
247 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd
), WIN_PROC_WINDOW
);
248 HeapFree( GetProcessHeap(), 0, dlgInfo
);
250 /* Window clean-up */
251 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
254 if (!wParam
) DEFDLG_SaveFocus( hwnd
);
255 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
258 if (wParam
) DEFDLG_RestoreFocus( hwnd
);
259 else DEFDLG_SaveFocus( hwnd
);
263 DEFDLG_RestoreFocus( hwnd
);
267 if (dlgInfo
&& !(dlgInfo
->flags
& DF_END
))
268 DEFDLG_SetDefId( hwnd
, dlgInfo
, wParam
);
272 if (dlgInfo
&& !(dlgInfo
->flags
& DF_END
))
275 if (dlgInfo
->idResult
)
276 return MAKELONG( dlgInfo
->idResult
, DC_HASDEFID
);
277 if ((hwndDefId
= DEFDLG_FindDefButton( hwnd
)))
278 return MAKELONG( GetDlgCtrlID( hwndDefId
), DC_HASDEFID
);
285 HWND hwndDest
= (HWND
)wParam
;
287 hwndDest
= GetNextDlgTabItem(hwnd
, GetFocus(), wParam
);
288 if (hwndDest
) DEFDLG_SetFocus( hwnd
, hwndDest
);
289 DEFDLG_SetDefButton( hwnd
, dlgInfo
, hwndDest
);
293 case WM_ENTERMENULOOP
:
295 case WM_NCLBUTTONDOWN
:
297 HWND hwndFocus
= GetFocus();
300 /* always make combo box hide its listbox control */
301 if (!SendMessageA( hwndFocus
, CB_SHOWDROPDOWN
, FALSE
, 0 ))
302 SendMessageA( GetParent(hwndFocus
), CB_SHOWDROPDOWN
, FALSE
, 0 );
305 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
308 return dlgInfo
? (LRESULT
)dlgInfo
->hUserFont
: 0;
311 PostMessageA( hwnd
, WM_COMMAND
, MAKEWPARAM(IDCANCEL
, BN_CLICKED
),
312 (LPARAM
)GetDlgItem( hwnd
, IDCANCEL
) );
315 case WM_NOTIFYFORMAT
:
316 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
321 /***********************************************************************
324 static LRESULT
DEFDLG_Epilog(HWND hwnd
, UINT msg
, BOOL fResult
)
328 if ((msg
>= WM_CTLCOLORMSGBOX
&& msg
<= WM_CTLCOLORSTATIC
) ||
329 msg
== WM_CTLCOLOR
|| msg
== WM_COMPAREITEM
||
330 msg
== WM_VKEYTOITEM
|| msg
== WM_CHARTOITEM
||
331 msg
== WM_QUERYDRAGICON
|| msg
== WM_INITDIALOG
)
334 return GetWindowLongA( hwnd
, DWL_MSGRESULT
);
337 /***********************************************************************
340 * Get the DIALOGINFO structure of a window, allocating it if needed
341 * and 'create' is TRUE.
343 DIALOGINFO
*DIALOG_get_info( HWND hwnd
, BOOL create
)
346 DIALOGINFO
* dlgInfo
= (DIALOGINFO
*)GetWindowLongW( hwnd
, DWL_WINE_DIALOGINFO
);
348 if(!dlgInfo
&& create
)
350 if (!(dlgInfo
= HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo
) ))) return NULL
;
351 dlgInfo
->hwndFocus
= 0;
352 dlgInfo
->hUserFont
= 0;
354 dlgInfo
->xBaseUnit
= 0;
355 dlgInfo
->yBaseUnit
= 0;
356 dlgInfo
->idResult
= 0;
358 dlgInfo
->hDialogHeap
= 0;
359 wndPtr
= WIN_GetPtr( hwnd
);
360 if (wndPtr
&& wndPtr
!= WND_OTHER_PROCESS
)
362 wndPtr
->flags
|= WIN_ISDIALOG
;
363 WIN_ReleasePtr( wndPtr
);
364 SetWindowLongW( hwnd
, DWL_WINE_DIALOGINFO
, (LONG
)dlgInfo
);
368 HeapFree( GetProcessHeap(), 0, dlgInfo
);
375 /***********************************************************************
376 * DefDlgProc (USER.308)
378 LRESULT WINAPI
DefDlgProc16( HWND16 hwnd
, UINT16 msg
, WPARAM16 wParam
,
383 HWND hwnd32
= WIN_Handle32( hwnd
);
386 /* Perform DIALOGINFO intialization if not done */
387 if(!(dlgInfo
= DIALOG_get_info(hwnd32
, TRUE
))) return -1;
389 SetWindowLongW( hwnd32
, DWL_MSGRESULT
, 0 );
391 if ((dlgproc
= (WNDPROC16
)DEFDLG_GetDlgProc( hwnd32
)))
393 /* Call dialog procedure */
394 result
= CallWindowProc16( dlgproc
, hwnd
, msg
, wParam
, lParam
);
395 /* 16 bit dlg procs only return BOOL16 */
396 if( WINPROC_GetProcType( (WNDPROC
)dlgproc
) == WIN_PROC_16
)
397 result
= LOWORD(result
);
400 if (!result
&& IsWindow(hwnd32
))
402 /* callback didn't process this message */
416 case WM_ENTERMENULOOP
:
418 case WM_NCLBUTTONDOWN
:
419 return DEFDLG_Proc( hwnd32
, msg
, (WPARAM
)wParam
, lParam
, dlgInfo
);
427 return DefWindowProc16( hwnd
, msg
, wParam
, lParam
);
430 return DEFDLG_Epilog( hwnd32
, msg
, result
);
434 /***********************************************************************
435 * DefDlgProcA (USER32.@)
437 LRESULT WINAPI
DefDlgProcA( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
443 /* Perform DIALOGINFO initialization if not done */
444 if(!(dlgInfo
= DIALOG_get_info( hwnd
, TRUE
))) return -1;
446 SetWindowLongW( hwnd
, DWL_MSGRESULT
, 0 );
448 if ((dlgproc
= DEFDLG_GetDlgProc( hwnd
)))
450 /* Call dialog procedure */
451 result
= CallWindowProcA( dlgproc
, hwnd
, msg
, wParam
, lParam
);
452 /* 16 bit dlg procs only return BOOL16 */
453 if( WINPROC_GetProcType( dlgproc
) == WIN_PROC_16
)
454 result
= LOWORD(result
);
457 if (!result
&& IsWindow(hwnd
))
459 /* callback didn't process this message */
473 case WM_ENTERMENULOOP
:
475 case WM_NCLBUTTONDOWN
:
476 return DEFDLG_Proc( hwnd
, msg
, wParam
, lParam
, dlgInfo
);
484 return DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
487 return DEFDLG_Epilog(hwnd
, msg
, result
);
491 /***********************************************************************
492 * DefDlgProcW (USER32.@)
494 LRESULT WINAPI
DefDlgProcW( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
500 /* Perform DIALOGINFO intialization if not done */
501 if(!(dlgInfo
= DIALOG_get_info( hwnd
, TRUE
))) return -1;
503 SetWindowLongW( hwnd
, DWL_MSGRESULT
, 0 );
505 if ((dlgproc
= DEFDLG_GetDlgProc( hwnd
)))
507 /* Call dialog procedure */
508 result
= CallWindowProcW( dlgproc
, hwnd
, msg
, wParam
, lParam
);
509 /* 16 bit dlg procs only return BOOL16 */
510 if( WINPROC_GetProcType( dlgproc
) == WIN_PROC_16
)
511 result
= LOWORD(result
);
514 if (!result
&& IsWindow(hwnd
))
516 /* callback didn't process this message */
530 case WM_ENTERMENULOOP
:
532 case WM_NCLBUTTONDOWN
:
533 return DEFDLG_Proc( hwnd
, msg
, wParam
, lParam
, dlgInfo
);
541 return DefWindowProcW( hwnd
, msg
, wParam
, lParam
);
544 return DEFDLG_Epilog(hwnd
, msg
, result
);