Release 951105
[wine/multimedia.git] / windows / defdlg.c
blob4f152fb8301a1fceec0da1710353bdcf392e01cb
1 /*
2 * Default dialog procedure
4 * Copyright 1993 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
7 */
9 #include "windows.h"
10 #include "dialog.h"
11 #include "win.h"
12 #include "stddebug.h"
13 /* #define DEBUG_DIALOG */
14 #include "debug.h"
16 /***********************************************************************
17 * DEFDLG_SetFocus
19 * Set the focus to a control of the dialog, selecting the text if
20 * the control is an edit dialog.
22 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
24 HWND hwndPrev = GetFocus();
26 if (IsChild( hwndDlg, hwndPrev ))
28 if (SendMessage( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
29 SendMessage( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
31 if (SendMessage( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
32 SendMessage( hwndCtrl, EM_SETSEL, FALSE, MAKELONG( 0, -1 ) );
33 SetFocus( hwndCtrl );
37 /***********************************************************************
38 * DEFDLG_SaveFocus
40 static BOOL DEFDLG_SaveFocus( HWND hwnd, DIALOGINFO *infoPtr )
42 HWND hwndFocus = GetFocus();
44 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return FALSE;
45 if (!infoPtr->hwndFocus) return FALSE; /* Already saved */
46 infoPtr->hwndFocus = hwndFocus;
47 /* Remove default button */
48 return TRUE;
52 /***********************************************************************
53 * DEFDLG_RestoreFocus
55 static BOOL DEFDLG_RestoreFocus( HWND hwnd, DIALOGINFO *infoPtr )
57 if (!infoPtr->hwndFocus || IsIconic(hwnd)) return FALSE;
58 if (!IsWindow( infoPtr->hwndFocus )) return FALSE;
59 DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
60 infoPtr->hwndFocus = 0;
61 return TRUE;
65 #ifdef SUPERFLUOUS_FUNCTIONS
66 /***********************************************************************
67 * DEFDLG_FindDefButton
69 * Find the current default push-button.
71 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
73 HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
74 while (hwndChild)
76 if (SendMessage( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
77 break;
78 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
80 return hwndChild;
82 #endif
85 /***********************************************************************
86 * DEFDLG_SetDefButton
88 * Set the new default button to be hwndNew.
90 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo,
91 HWND hwndNew )
93 if (hwndNew &&
94 !(SendMessage( hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
95 return FALSE; /* Destination is not a push button */
97 if (dlgInfo->msgResult) /* There's already a default pushbutton */
99 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->msgResult );
100 if (SendMessage( hwndOld, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
101 SendMessage( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
103 if (hwndNew)
105 SendMessage( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
106 dlgInfo->msgResult = GetDlgCtrlID( hwndNew );
108 else dlgInfo->msgResult = 0;
109 return TRUE;
113 /***********************************************************************
114 * DefDlgProc (USER.308)
116 LRESULT DefDlgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
118 DIALOGINFO * dlgInfo;
119 BOOL result = FALSE;
120 WND * wndPtr = WIN_FindWndPtr( hwnd );
122 if (!wndPtr) return 0;
123 dlgInfo = (DIALOGINFO *)&wndPtr->wExtra;
125 dprintf_dialog(stddeb, "DefDlgProc: "NPFMT" %04x %ld %08lx\n",
126 hwnd, msg, (DWORD)wParam, lParam );
128 dlgInfo->msgResult = 0;
129 if (dlgInfo->dlgProc)
131 /* Call dialog procedure */
132 result = (BOOL)CallWindowProc( dlgInfo->dlgProc, hwnd,
133 msg, wParam, lParam );
135 /* Check if window destroyed by dialog procedure */
136 wndPtr = WIN_FindWndPtr( hwnd );
137 if (!wndPtr) return result;
140 if (!result) switch(msg)
142 case WM_INITDIALOG:
143 break;
145 case WM_ERASEBKGND:
146 FillWindow( hwnd, hwnd, (HDC)wParam, (HBRUSH)CTLCOLOR_DLG );
147 return TRUE;
149 case WM_NCDESTROY:
151 /* Free dialog heap (if created) */
152 if (dlgInfo->hDialogHeap)
154 GlobalUnlock(dlgInfo->hDialogHeap);
155 GlobalFree(dlgInfo->hDialogHeap);
156 dlgInfo->hDialogHeap = 0;
159 /* Delete font */
160 if (dlgInfo->hUserFont)
162 DeleteObject( dlgInfo->hUserFont );
163 dlgInfo->hUserFont = 0;
166 /* Delete menu */
167 if (dlgInfo->hMenu)
169 DestroyMenu( dlgInfo->hMenu );
170 dlgInfo->hMenu = 0;
173 /* Window clean-up */
174 DefWindowProc( hwnd, msg, wParam, lParam );
175 break;
177 case WM_SHOWWINDOW:
178 if (!wParam) DEFDLG_SaveFocus( hwnd, dlgInfo );
179 return DefWindowProc( hwnd, msg, wParam, lParam );
181 case WM_ACTIVATE:
182 if (wParam) DEFDLG_RestoreFocus( hwnd, dlgInfo );
183 else DEFDLG_SaveFocus( hwnd, dlgInfo );
184 break;
186 case WM_SETFOCUS:
187 DEFDLG_RestoreFocus( hwnd, dlgInfo );
188 break;
190 case DM_SETDEFID:
191 if (dlgInfo->fEnd) return TRUE;
192 DEFDLG_SetDefButton( hwnd, dlgInfo,
193 wParam ? GetDlgItem( hwnd, wParam ) : 0 );
194 return TRUE;
196 case DM_GETDEFID:
197 if (dlgInfo->fEnd || !dlgInfo->msgResult) return 0;
198 return MAKELONG( dlgInfo->msgResult, DC_HASDEFID );
200 case WM_NEXTDLGCTL:
202 HWND hwndDest = (HWND)wParam;
203 if (!lParam)
205 HWND hwndPrev = GetFocus();
206 if (!hwndPrev) /* Set focus to the first item */
207 hwndDest = DIALOG_GetFirstTabItem( hwnd );
208 else
209 hwndDest = GetNextDlgTabItem( hwnd, hwndPrev, wParam );
211 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
212 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
214 break;
216 case WM_CLOSE:
217 EndDialog( hwnd, TRUE );
218 DestroyWindow( hwnd );
219 return 0;
221 default:
222 return DefWindowProc( hwnd, msg, wParam, lParam );
225 return result;