Release 960717
[wine.git] / windows / defdlg.c
blob0ddbea88596d1e8b52a066b531196606a4be5a27
1 /*
2 * Default dialog procedure
4 * Copyright 1993, 1996 Alexandre Julliard
6 */
8 #include "windows.h"
9 #include "dialog.h"
10 #include "win.h"
11 #include "winproc.h"
14 /***********************************************************************
15 * DEFDLG_SetFocus
17 * Set the focus to a control of the dialog, selecting the text if
18 * the control is an edit dialog.
20 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
22 HWND hwndPrev = GetFocus();
24 if (IsChild( hwndDlg, hwndPrev ))
26 if (SendMessage16( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
27 SendMessage16( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
29 if (SendMessage16( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
30 SendMessage16( hwndCtrl, EM_SETSEL, FALSE, MAKELONG( 0, -1 ) );
31 SetFocus( hwndCtrl );
35 /***********************************************************************
36 * DEFDLG_SaveFocus
38 static BOOL DEFDLG_SaveFocus( HWND hwnd, DIALOGINFO *infoPtr )
40 HWND hwndFocus = GetFocus();
42 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return FALSE;
43 if (!infoPtr->hwndFocus) return FALSE; /* Already saved */
44 infoPtr->hwndFocus = hwndFocus;
45 /* Remove default button */
46 return TRUE;
50 /***********************************************************************
51 * DEFDLG_RestoreFocus
53 static BOOL DEFDLG_RestoreFocus( HWND hwnd, DIALOGINFO *infoPtr )
55 if (!infoPtr->hwndFocus || IsIconic(hwnd)) return FALSE;
56 if (!IsWindow( infoPtr->hwndFocus )) return FALSE;
57 DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
58 infoPtr->hwndFocus = 0;
59 return TRUE;
63 #ifdef SUPERFLUOUS_FUNCTIONS
64 /***********************************************************************
65 * DEFDLG_FindDefButton
67 * Find the current default push-button.
69 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
71 HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
72 while (hwndChild)
74 if (SendMessage16( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
75 break;
76 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
78 return hwndChild;
80 #endif
83 /***********************************************************************
84 * DEFDLG_SetDefButton
86 * Set the new default button to be hwndNew.
88 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
89 HWND hwndNew )
91 if (hwndNew &&
92 !(SendMessage16(hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
93 return FALSE; /* Destination is not a push button */
95 if (dlgInfo->msgResult) /* There's already a default pushbutton */
97 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->msgResult );
98 if (SendMessage32A( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
99 SendMessage32A( hwndOld, BM_SETSTYLE32, BS_PUSHBUTTON, TRUE );
101 if (hwndNew)
103 SendMessage32A( hwndNew, BM_SETSTYLE32, BS_DEFPUSHBUTTON, TRUE );
104 dlgInfo->msgResult = GetDlgCtrlID( hwndNew );
106 else dlgInfo->msgResult = 0;
107 return TRUE;
111 /***********************************************************************
112 * DEFDLG_Proc
114 * Implementation of DefDlgProc(). Only handle messages that need special
115 * handling for dialogs.
117 static LRESULT DEFDLG_Proc( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
118 LPARAM lParam, DIALOGINFO *dlgInfo )
120 switch(msg)
122 case WM_INITDIALOG:
123 return 0;
125 case WM_ERASEBKGND:
126 FillWindow( hwnd, hwnd, (HDC)wParam, (HBRUSH)CTLCOLOR_DLG );
127 return 1;
129 case WM_NCDESTROY:
131 /* Free dialog heap (if created) */
132 if (dlgInfo->hDialogHeap)
134 GlobalUnlock16(dlgInfo->hDialogHeap);
135 GlobalFree16(dlgInfo->hDialogHeap);
136 dlgInfo->hDialogHeap = 0;
139 /* Delete font */
140 if (dlgInfo->hUserFont)
142 DeleteObject( dlgInfo->hUserFont );
143 dlgInfo->hUserFont = 0;
146 /* Delete menu */
147 if (dlgInfo->hMenu)
149 DestroyMenu( dlgInfo->hMenu );
150 dlgInfo->hMenu = 0;
153 /* Delete window procedure */
154 WINPROC_FreeProc( dlgInfo->dlgProc );
155 dlgInfo->dlgProc = (HWINDOWPROC)0;
157 /* Window clean-up */
158 return DefWindowProc32A( hwnd, msg, wParam, lParam );
160 case WM_SHOWWINDOW:
161 if (!wParam) DEFDLG_SaveFocus( hwnd, dlgInfo );
162 return DefWindowProc32A( hwnd, msg, wParam, lParam );
164 case WM_ACTIVATE:
165 if (wParam) DEFDLG_RestoreFocus( hwnd, dlgInfo );
166 else DEFDLG_SaveFocus( hwnd, dlgInfo );
167 return 0;
169 case WM_SETFOCUS:
170 DEFDLG_RestoreFocus( hwnd, dlgInfo );
171 return 0;
173 case DM_SETDEFID:
174 if (dlgInfo->fEnd) return 1;
175 DEFDLG_SetDefButton( hwnd, dlgInfo,
176 wParam ? GetDlgItem( hwnd, wParam ) : 0 );
177 return 1;
179 case DM_GETDEFID:
180 if (dlgInfo->fEnd || !dlgInfo->msgResult) return 0;
181 return MAKELONG( dlgInfo->msgResult, DC_HASDEFID );
183 case WM_NEXTDLGCTL:
185 HWND hwndDest = (HWND)wParam;
186 if (!lParam)
188 HWND hwndPrev = GetFocus();
189 if (!hwndPrev) /* Set focus to the first item */
190 hwndDest = DIALOG_GetFirstTabItem( hwnd );
191 else
192 hwndDest = GetNextDlgTabItem( hwnd, hwndPrev, wParam );
194 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
195 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
197 return 0;
199 case WM_CLOSE:
200 EndDialog( hwnd, TRUE );
201 DestroyWindow( hwnd );
202 return 0;
204 return 0;
208 /***********************************************************************
209 * DefDlgProc16 (USER.308)
211 LRESULT DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
213 DIALOGINFO * dlgInfo;
214 BOOL16 result = FALSE;
215 WND * wndPtr = WIN_FindWndPtr( hwnd );
217 if (!wndPtr) return 0;
218 dlgInfo = (DIALOGINFO *)&wndPtr->wExtra;
220 dlgInfo->msgResult = 0;
221 if (dlgInfo->dlgProc)
223 /* Call dialog procedure */
224 result = (BOOL16)CallWindowProc16( (WNDPROC16)dlgInfo->dlgProc,
225 hwnd, msg, wParam, lParam );
227 /* Check if window was destroyed by dialog procedure */
228 if (result || !IsWindow( hwnd )) return result;
231 switch(msg)
233 case WM_INITDIALOG:
234 case WM_ERASEBKGND:
235 case WM_NCDESTROY:
236 case WM_SHOWWINDOW:
237 case WM_ACTIVATE:
238 case WM_SETFOCUS:
239 case DM_SETDEFID:
240 case DM_GETDEFID:
241 case WM_NEXTDLGCTL:
242 case WM_CLOSE:
243 return DEFDLG_Proc( (HWND32)hwnd, msg, (WPARAM32)wParam,
244 lParam, dlgInfo );
246 default:
247 return DefWindowProc16( hwnd, msg, wParam, lParam );
252 /***********************************************************************
253 * DefDlgProc32A (USER32.119)
255 LRESULT DefDlgProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
257 DIALOGINFO * dlgInfo;
258 BOOL16 result = FALSE;
259 WND * wndPtr = WIN_FindWndPtr( hwnd );
261 if (!wndPtr) return 0;
262 dlgInfo = (DIALOGINFO *)&wndPtr->wExtra;
264 dlgInfo->msgResult = 0;
265 if (dlgInfo->dlgProc)
267 /* Call dialog procedure */
268 result = (BOOL16)CallWindowProc32A( (WNDPROC32)dlgInfo->dlgProc,
269 hwnd, msg, wParam, lParam );
271 /* Check if window was destroyed by dialog procedure */
272 if (result || !IsWindow( hwnd )) return result;
275 switch(msg)
277 case WM_INITDIALOG:
278 case WM_ERASEBKGND:
279 case WM_NCDESTROY:
280 case WM_SHOWWINDOW:
281 case WM_ACTIVATE:
282 case WM_SETFOCUS:
283 case DM_SETDEFID:
284 case DM_GETDEFID:
285 case WM_NEXTDLGCTL:
286 case WM_CLOSE:
287 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
289 default:
290 return DefWindowProc32A( hwnd, msg, wParam, lParam );
295 /***********************************************************************
296 * DefDlgProc32W (USER32.120)
298 LRESULT DefDlgProc32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
300 DIALOGINFO * dlgInfo;
301 BOOL16 result = FALSE;
302 WND * wndPtr = WIN_FindWndPtr( hwnd );
304 if (!wndPtr) return 0;
305 dlgInfo = (DIALOGINFO *)&wndPtr->wExtra;
307 dlgInfo->msgResult = 0;
308 if (dlgInfo->dlgProc)
310 /* Call dialog procedure */
311 result = (BOOL16)CallWindowProc32W( (WNDPROC32)dlgInfo->dlgProc,
312 hwnd, msg, wParam, lParam );
314 /* Check if window was destroyed by dialog procedure */
315 if (result || !IsWindow( hwnd )) return result;
318 switch(msg)
320 case WM_INITDIALOG:
321 case WM_ERASEBKGND:
322 case WM_NCDESTROY:
323 case WM_SHOWWINDOW:
324 case WM_ACTIVATE:
325 case WM_SETFOCUS:
326 case DM_SETDEFID:
327 case DM_GETDEFID:
328 case WM_NEXTDLGCTL:
329 case WM_CLOSE:
330 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
332 default:
333 return DefWindowProc32W( hwnd, msg, wParam, lParam );