qedit: Remove an unused and wrong assignment (PVS-Studio).
[wine/multimedia.git] / dlls / user32 / defdlg.c
blob00a73c6b927edef45a30638667818a53b2d8d79f
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "controls.h"
28 #include "win.h"
29 #include "user_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
35 /***********************************************************************
36 * DEFDLG_GetDlgProc
38 static DLGPROC DEFDLG_GetDlgProc( HWND hwnd )
40 DLGPROC ret;
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 );
47 return 0;
49 ret = *(DLGPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
50 WIN_ReleasePtr( wndPtr );
51 return ret;
54 /***********************************************************************
55 * DEFDLG_SetFocus
57 * Set the focus to a control of the dialog, selecting the text if
58 * the control is an edit dialog that has DLGC_HASSETSEL.
60 static void DEFDLG_SetFocus( HWND hwndCtrl )
62 if (SendMessageW( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
63 SendMessageW( hwndCtrl, EM_SETSEL, 0, -1 );
64 SetFocus( hwndCtrl );
68 /***********************************************************************
69 * DEFDLG_SaveFocus
71 static void DEFDLG_SaveFocus( HWND hwnd )
73 DIALOGINFO *infoPtr;
74 HWND hwndFocus = GetFocus();
76 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return;
77 if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
78 infoPtr->hwndFocus = hwndFocus;
79 /* Remove default button */
83 /***********************************************************************
84 * DEFDLG_RestoreFocus
86 static void DEFDLG_RestoreFocus( HWND hwnd, BOOL justActivate )
88 DIALOGINFO *infoPtr;
90 if (IsIconic( hwnd )) return;
91 if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
92 /* Don't set the focus back to controls if EndDialog is already called.*/
93 if (infoPtr->flags & DF_END) return;
94 if (!IsWindow(infoPtr->hwndFocus) || infoPtr->hwndFocus == hwnd) {
95 if (justActivate) return;
96 /* If no saved focus control exists, set focus to the first visible,
97 non-disabled, WS_TABSTOP control in the dialog */
98 infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
99 /* If there are no WS_TABSTOP controls, set focus to the first visible,
100 non-disabled control in the dialog */
101 if (!infoPtr->hwndFocus) infoPtr->hwndFocus = GetNextDlgGroupItem( hwnd, 0, FALSE );
102 if (!IsWindow( infoPtr->hwndFocus )) return;
104 if (justActivate)
105 SetFocus( infoPtr->hwndFocus );
106 else
107 DEFDLG_SetFocus( infoPtr->hwndFocus );
108 infoPtr->hwndFocus = NULL;
112 /***********************************************************************
113 * DEFDLG_FindDefButton
115 * Find the current default push-button.
117 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
119 HWND hwndChild, hwndTmp;
121 hwndChild = GetWindow( hwndDlg, GW_CHILD );
122 while (hwndChild)
124 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
125 break;
127 /* Recurse into WS_EX_CONTROLPARENT controls */
128 if (GetWindowLongW( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
130 LONG dsStyle = GetWindowLongW( hwndChild, GWL_STYLE );
131 if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
132 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
133 return hwndTmp;
135 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
137 return hwndChild;
141 /***********************************************************************
142 * DEFDLG_SetDefId
144 * Set the default button id.
146 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
148 DWORD dlgcode=0; /* initialize just to avoid a warning */
149 HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
150 INT old_id = dlgInfo->idResult;
152 dlgInfo->idResult = wParam;
153 if (hwndNew &&
154 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
155 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
156 return FALSE; /* Destination is not a push button */
158 /* Make sure the old default control is a valid push button ID */
159 hwndOld = GetDlgItem( hwndDlg, old_id );
160 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
161 hwndOld = DEFDLG_FindDefButton( hwndDlg );
162 if (hwndOld && hwndOld != hwndNew)
163 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
165 if (hwndNew)
167 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
168 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
170 return TRUE;
174 /***********************************************************************
175 * DEFDLG_SetDefButton
177 * Set the new default button to be hwndNew.
179 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
181 DWORD dlgcode=0; /* initialize just to avoid a warning */
182 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
184 if (hwndNew &&
185 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
186 & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
189 * Need to draw only default push button rectangle.
190 * Since the next control is not a push button, need to draw the push
191 * button rectangle for the default control.
193 hwndNew = hwndOld;
194 dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
197 /* Make sure the old default control is a valid push button ID */
198 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
199 hwndOld = DEFDLG_FindDefButton( hwndDlg );
200 if (hwndOld && hwndOld != hwndNew)
201 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
203 if (hwndNew)
205 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
206 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
208 return TRUE;
212 /***********************************************************************
213 * DEFDLG_Proc
215 * Implementation of DefDlgProc(). Only handle messages that need special
216 * handling for dialogs.
218 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
219 LPARAM lParam, DIALOGINFO *dlgInfo )
221 switch(msg)
223 case WM_ERASEBKGND:
225 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
226 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
227 if (brush)
229 RECT rect;
230 HDC hdc = (HDC)wParam;
231 GetClientRect( hwnd, &rect );
232 DPtoLP( hdc, (LPPOINT)&rect, 2 );
233 FillRect( hdc, &rect, brush );
235 return 1;
237 case WM_NCDESTROY:
238 if (dlgInfo)
240 WND *wndPtr;
242 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
243 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
244 HeapFree( GetProcessHeap(), 0, dlgInfo );
246 wndPtr = WIN_GetPtr( hwnd );
247 wndPtr->dlgInfo = NULL;
248 WIN_ReleasePtr( wndPtr );
250 /* Window clean-up */
251 return DefWindowProcA( hwnd, msg, wParam, lParam );
253 case WM_SHOWWINDOW:
254 if (!wParam) DEFDLG_SaveFocus( hwnd );
255 return DefWindowProcA( hwnd, msg, wParam, lParam );
257 case WM_ACTIVATE:
258 if (wParam) DEFDLG_RestoreFocus( hwnd, TRUE );
259 else DEFDLG_SaveFocus( hwnd );
260 return 0;
262 case WM_SETFOCUS:
263 DEFDLG_RestoreFocus( hwnd, FALSE );
264 return 0;
266 case DM_SETDEFID:
267 if (dlgInfo && !(dlgInfo->flags & DF_END))
268 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
269 return 1;
271 case DM_GETDEFID:
272 if (dlgInfo && !(dlgInfo->flags & DF_END))
274 HWND hwndDefId;
275 if (dlgInfo->idResult)
276 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
277 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
278 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
280 return 0;
282 case WM_NEXTDLGCTL:
283 if (dlgInfo)
285 HWND hwndDest = (HWND)wParam;
286 if (!lParam)
287 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
288 if (hwndDest) DEFDLG_SetFocus( hwndDest );
289 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
291 return 0;
293 case WM_ENTERMENULOOP:
294 case WM_LBUTTONDOWN:
295 case WM_NCLBUTTONDOWN:
297 HWND hwndFocus = GetFocus();
298 if (hwndFocus)
300 /* always make combo box hide its listbox control */
301 if (!SendMessageW( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
302 SendMessageW( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
305 return DefWindowProcA( hwnd, msg, wParam, lParam );
307 case WM_GETFONT:
308 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
310 case WM_CLOSE:
311 PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
312 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
313 return 0;
315 return 0;
318 /***********************************************************************
319 * DIALOG_get_info
321 * Get the DIALOGINFO structure of a window, allocating it if needed
322 * and 'create' is TRUE.
324 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
326 WND* wndPtr;
327 DIALOGINFO* dlgInfo;
329 wndPtr = WIN_GetPtr( hwnd );
330 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
332 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
333 return NULL;
336 dlgInfo = wndPtr->dlgInfo;
338 if (!dlgInfo && create)
340 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) )))
341 goto out;
342 dlgInfo->hwndFocus = 0;
343 dlgInfo->hUserFont = 0;
344 dlgInfo->hMenu = 0;
345 dlgInfo->xBaseUnit = 0;
346 dlgInfo->yBaseUnit = 0;
347 dlgInfo->idResult = IDOK;
348 dlgInfo->flags = 0;
349 wndPtr->dlgInfo = dlgInfo;
352 out:
353 WIN_ReleasePtr( wndPtr );
354 return dlgInfo;
357 /***********************************************************************
358 * DefDlgProcA (USER32.@)
360 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
362 DIALOGINFO *dlgInfo;
363 DLGPROC dlgproc;
364 LRESULT result = 0;
366 /* Perform DIALOGINFO initialization if not done */
367 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
369 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
371 if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
372 result = WINPROC_CallDlgProcA( dlgproc, hwnd, msg, wParam, lParam );
374 if (!result && IsWindow(hwnd))
376 /* callback didn't process this message */
378 switch(msg)
380 case WM_ERASEBKGND:
381 case WM_SHOWWINDOW:
382 case WM_ACTIVATE:
383 case WM_SETFOCUS:
384 case DM_SETDEFID:
385 case DM_GETDEFID:
386 case WM_NEXTDLGCTL:
387 case WM_GETFONT:
388 case WM_CLOSE:
389 case WM_NCDESTROY:
390 case WM_ENTERMENULOOP:
391 case WM_LBUTTONDOWN:
392 case WM_NCLBUTTONDOWN:
393 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
394 case WM_INITDIALOG:
395 case WM_VKEYTOITEM:
396 case WM_COMPAREITEM:
397 case WM_CHARTOITEM:
398 break;
400 default:
401 return DefWindowProcA( hwnd, msg, wParam, lParam );
405 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
406 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
407 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
408 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
409 return result;
411 return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
415 /***********************************************************************
416 * DefDlgProcW (USER32.@)
418 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
420 DIALOGINFO *dlgInfo;
421 DLGPROC dlgproc;
422 LRESULT result = 0;
424 /* Perform DIALOGINFO initialization if not done */
425 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return 0;
427 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
429 if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
430 result = WINPROC_CallDlgProcW( dlgproc, hwnd, msg, wParam, lParam );
432 if (!result && IsWindow(hwnd))
434 /* callback didn't process this message */
436 switch(msg)
438 case WM_ERASEBKGND:
439 case WM_SHOWWINDOW:
440 case WM_ACTIVATE:
441 case WM_SETFOCUS:
442 case DM_SETDEFID:
443 case DM_GETDEFID:
444 case WM_NEXTDLGCTL:
445 case WM_GETFONT:
446 case WM_CLOSE:
447 case WM_NCDESTROY:
448 case WM_ENTERMENULOOP:
449 case WM_LBUTTONDOWN:
450 case WM_NCLBUTTONDOWN:
451 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
452 case WM_INITDIALOG:
453 case WM_VKEYTOITEM:
454 case WM_COMPAREITEM:
455 case WM_CHARTOITEM:
456 break;
458 default:
459 return DefWindowProcW( hwnd, msg, wParam, lParam );
463 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
464 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
465 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
466 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
467 return result;
469 return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );