2 * Default dialog procedure
4 * Copyright 1993 Alexandre Julliard
12 /* #define DEBUG_DIALOG */
15 /***********************************************************************
18 * Set the focus to a control of the dialog, selecting the text if
19 * the control is an edit dialog.
21 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
23 HWND hwndPrev = GetFocus();
25 if (IsChild( hwndDlg, hwndPrev ))
27 if (SendMessage( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
28 SendMessage( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
30 if (SendMessage( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
31 SendMessage( hwndCtrl, EM_SETSEL, FALSE, MAKELONG( 0, -1 ) );
36 /***********************************************************************
39 static BOOL DEFDLG_SaveFocus( HWND hwnd, DIALOGINFO *infoPtr )
41 HWND hwndFocus = GetFocus();
43 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return FALSE;
44 if (!infoPtr->hwndFocus) return FALSE; /* Already saved */
45 infoPtr->hwndFocus = hwndFocus;
46 /* Remove default button */
51 /***********************************************************************
54 static BOOL DEFDLG_RestoreFocus( HWND hwnd, DIALOGINFO *infoPtr )
56 if (!infoPtr->hwndFocus || IsIconic(hwnd)) return FALSE;
57 if (!IsWindow( infoPtr->hwndFocus )) return FALSE;
58 DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
59 infoPtr->hwndFocus = 0;
64 #ifdef SUPERFLUOUS_FUNCTIONS
65 /***********************************************************************
66 * DEFDLG_FindDefButton
68 * Find the current default push-button.
70 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
72 HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
75 if (SendMessage( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
77 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
84 /***********************************************************************
87 * Set the new default button to be hwndNew.
89 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
93 !(SendMessage( hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
94 return FALSE; /* Destination is not a push button */
96 if (dlgInfo->msgResult) /* There's already a default pushbutton */
98 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->msgResult );
99 if (SendMessage( hwndOld, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
100 SendMessage( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
104 SendMessage( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
105 dlgInfo->msgResult = GetDlgCtrlID( hwndNew );
107 else dlgInfo->msgResult = 0;
112 /***********************************************************************
113 * DefDlgProc (USER.308)
115 LRESULT DefDlgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
117 DIALOGINFO * dlgInfo;
119 WND * wndPtr = WIN_FindWndPtr( hwnd );
121 if (!wndPtr) return 0;
122 dlgInfo = (DIALOGINFO *)&wndPtr->wExtra;
124 dlgInfo->msgResult = 0;
125 if (dlgInfo->dlgProc)
127 /* Call dialog procedure */
128 result = (BOOL)CallWindowProc( dlgInfo->dlgProc, hwnd,
129 msg, wParam, lParam );
131 /* Check if window destroyed by dialog procedure */
132 wndPtr = WIN_FindWndPtr( hwnd );
133 if (!wndPtr) return result;
136 if (!result) switch(msg)
142 FillWindow( hwnd, hwnd, (HDC)wParam, (HBRUSH)CTLCOLOR_DLG );
147 /* Free dialog heap (if created) */
148 if (dlgInfo->hDialogHeap)
150 GlobalUnlock16(dlgInfo->hDialogHeap);
151 GlobalFree16(dlgInfo->hDialogHeap);
152 dlgInfo->hDialogHeap = 0;
156 if (dlgInfo->hUserFont)
158 DeleteObject( dlgInfo->hUserFont );
159 dlgInfo->hUserFont = 0;
165 DestroyMenu( dlgInfo->hMenu );
169 /* Window clean-up */
170 DefWindowProc( hwnd, msg, wParam, lParam );
174 if (!wParam) DEFDLG_SaveFocus( hwnd, dlgInfo );
175 return DefWindowProc( hwnd, msg, wParam, lParam );
178 if (wParam) DEFDLG_RestoreFocus( hwnd, dlgInfo );
179 else DEFDLG_SaveFocus( hwnd, dlgInfo );
183 DEFDLG_RestoreFocus( hwnd, dlgInfo );
187 if (dlgInfo->fEnd) return TRUE;
188 DEFDLG_SetDefButton( hwnd, dlgInfo,
189 wParam ? GetDlgItem( hwnd, wParam ) : 0 );
193 if (dlgInfo->fEnd || !dlgInfo->msgResult) return 0;
194 return MAKELONG( dlgInfo->msgResult, DC_HASDEFID );
198 HWND hwndDest = (HWND)wParam;
201 HWND hwndPrev = GetFocus();
202 if (!hwndPrev) /* Set focus to the first item */
203 hwndDest = DIALOG_GetFirstTabItem( hwnd );
205 hwndDest = GetNextDlgTabItem( hwnd, hwndPrev, wParam );
207 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
208 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
213 EndDialog( hwnd, TRUE );
214 DestroyWindow( hwnd );
218 return DefWindowProc( hwnd, msg, wParam, lParam );