usp10: Update tests in test_ScriptItemIzeShapePlace to match Windows results.
[wine/multimedia.git] / dlls / user / defdlg.c
blob9c2aba4b1d2f2c08abc5add222acd0a328ae30ee
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 "wine/winuser16.h"
27 #include "controls.h"
28 #include "win.h"
29 #include "winproc.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.
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 );
71 SetFocus( hwndCtrl );
75 /***********************************************************************
76 * DEFDLG_SaveFocus
78 static void DEFDLG_SaveFocus( HWND hwnd )
80 DIALOGINFO *infoPtr;
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 /***********************************************************************
91 * DEFDLG_RestoreFocus
93 static void DEFDLG_RestoreFocus( HWND hwnd )
95 DIALOGINFO *infoPtr;
97 if (IsIconic( hwnd )) return;
98 if (!(infoPtr = DIALOG_get_info( hwnd, FALSE ))) return;
99 /* Don't set the focus back to controls if EndDialog is already called.*/
100 if (infoPtr->flags & DF_END) return;
101 if (!IsWindow(infoPtr->hwndFocus) || infoPtr->hwndFocus == hwnd) {
102 /* If no saved focus control exists, set focus to the first visible,
103 non-disabled, WS_TABSTOP control in the dialog */
104 infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
105 if (!IsWindow( infoPtr->hwndFocus )) return;
107 SetFocus( infoPtr->hwndFocus );
109 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
110 sometimes losing focus when receiving WM_SETFOCUS messages. */
114 /***********************************************************************
115 * DEFDLG_FindDefButton
117 * Find the current default push-button.
119 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
121 HWND hwndChild, hwndTmp;
123 hwndChild = GetWindow( hwndDlg, GW_CHILD );
124 while (hwndChild)
126 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
127 break;
129 /* Recurse into WS_EX_CONTROLPARENT controls */
130 if (GetWindowLongW( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
132 LONG dsStyle = GetWindowLongW( hwndChild, GWL_STYLE );
133 if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
134 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
135 return hwndTmp;
137 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
139 return hwndChild;
143 /***********************************************************************
144 * DEFDLG_SetDefId
146 * Set the default button id.
148 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
150 DWORD dlgcode=0; /* initialize just to avoid a warning */
151 HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
152 INT old_id = dlgInfo->idResult;
154 dlgInfo->idResult = wParam;
155 if (hwndNew &&
156 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
157 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
158 return FALSE; /* Destination is not a push button */
160 /* Make sure the old default control is a valid push button ID */
161 hwndOld = GetDlgItem( hwndDlg, old_id );
162 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
163 hwndOld = DEFDLG_FindDefButton( hwndDlg );
164 if (hwndOld && hwndOld != hwndNew)
165 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
167 if (hwndNew)
169 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
170 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
172 return TRUE;
176 /***********************************************************************
177 * DEFDLG_SetDefButton
179 * Set the new default button to be hwndNew.
181 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
183 DWORD dlgcode=0; /* initialize just to avoid a warning */
184 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
186 if (hwndNew &&
187 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
188 & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
191 * Need to draw only default push button rectangle.
192 * Since the next control is not a push button, need to draw the push
193 * button rectangle for the default control.
195 hwndNew = hwndOld;
196 dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
199 /* Make sure the old default control is a valid push button ID */
200 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
201 hwndOld = DEFDLG_FindDefButton( hwndDlg );
202 if (hwndOld && hwndOld != hwndNew)
203 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
205 if (hwndNew)
207 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
208 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
210 return TRUE;
214 /***********************************************************************
215 * DEFDLG_Proc
217 * Implementation of DefDlgProc(). Only handle messages that need special
218 * handling for dialogs.
220 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
221 LPARAM lParam, DIALOGINFO *dlgInfo )
223 switch(msg)
225 case WM_ERASEBKGND:
227 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
228 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
229 if (brush)
231 RECT rect;
232 HDC hdc = (HDC)wParam;
233 GetClientRect( hwnd, &rect );
234 DPtoLP( hdc, (LPPOINT)&rect, 2 );
235 FillRect( hdc, &rect, brush );
237 return 1;
239 case WM_NCDESTROY:
240 if ((dlgInfo = (DIALOGINFO *)SetWindowLongPtrW( hwnd, DWLP_WINE_DIALOGINFO, 0 )))
242 /* Free dialog heap (if created) */
243 if (dlgInfo->hDialogHeap)
245 GlobalUnlock16(dlgInfo->hDialogHeap);
246 GlobalFree16(dlgInfo->hDialogHeap);
248 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
249 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
250 HeapFree( GetProcessHeap(), 0, dlgInfo );
252 /* Window clean-up */
253 return DefWindowProcA( hwnd, msg, wParam, lParam );
255 case WM_SHOWWINDOW:
256 if (!wParam) DEFDLG_SaveFocus( hwnd );
257 return DefWindowProcA( hwnd, msg, wParam, lParam );
259 case WM_ACTIVATE:
260 if (wParam) DEFDLG_RestoreFocus( hwnd );
261 else DEFDLG_SaveFocus( hwnd );
262 return 0;
264 case WM_SETFOCUS:
265 DEFDLG_RestoreFocus( hwnd );
266 return 0;
268 case DM_SETDEFID:
269 if (dlgInfo && !(dlgInfo->flags & DF_END))
270 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
271 return 1;
273 case DM_GETDEFID:
274 if (dlgInfo && !(dlgInfo->flags & DF_END))
276 HWND hwndDefId;
277 if (dlgInfo->idResult)
278 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
279 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
280 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
282 return 0;
284 case WM_NEXTDLGCTL:
285 if (dlgInfo)
287 HWND hwndDest = (HWND)wParam;
288 if (!lParam)
289 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
290 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
291 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
293 return 0;
295 case WM_ENTERMENULOOP:
296 case WM_LBUTTONDOWN:
297 case WM_NCLBUTTONDOWN:
299 HWND hwndFocus = GetFocus();
300 if (hwndFocus)
302 /* always make combo box hide its listbox control */
303 if (!SendMessageW( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
304 SendMessageW( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
307 return DefWindowProcA( hwnd, msg, wParam, lParam );
309 case WM_GETFONT:
310 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
312 case WM_CLOSE:
313 PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
314 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
315 return 0;
317 return 0;
320 /***********************************************************************
321 * DEFDLG_Epilog
323 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
325 /* see SDK 3.1 */
327 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
328 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
329 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
330 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
331 return fResult;
333 return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
336 /***********************************************************************
337 * DIALOG_get_info
339 * Get the DIALOGINFO structure of a window, allocating it if needed
340 * and 'create' is TRUE.
342 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
344 WND* wndPtr;
345 DIALOGINFO* dlgInfo = (DIALOGINFO *)GetWindowLongPtrW( hwnd, DWLP_WINE_DIALOGINFO );
347 if(!dlgInfo && create)
349 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return NULL;
350 dlgInfo->hwndFocus = 0;
351 dlgInfo->hUserFont = 0;
352 dlgInfo->hMenu = 0;
353 dlgInfo->xBaseUnit = 0;
354 dlgInfo->yBaseUnit = 0;
355 dlgInfo->idResult = 0;
356 dlgInfo->flags = 0;
357 dlgInfo->hDialogHeap = 0;
358 wndPtr = WIN_GetPtr( hwnd );
359 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
361 wndPtr->flags |= WIN_ISDIALOG;
362 WIN_ReleasePtr( wndPtr );
363 SetWindowLongPtrW( hwnd, DWLP_WINE_DIALOGINFO, (ULONG_PTR)dlgInfo );
365 else
367 HeapFree( GetProcessHeap(), 0, dlgInfo );
368 return NULL;
371 return dlgInfo;
374 /***********************************************************************
375 * DefDlgProc (USER.308)
377 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
378 LPARAM lParam )
380 DIALOGINFO *dlgInfo;
381 DLGPROC16 dlgproc;
382 HWND hwnd32 = WIN_Handle32( hwnd );
383 BOOL result = FALSE;
385 /* Perform DIALOGINFO intialization if not done */
386 if(!(dlgInfo = DIALOG_get_info(hwnd32, TRUE))) return -1;
388 SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, 0 );
390 if ((dlgproc = (DLGPROC16)DEFDLG_GetDlgProc( hwnd32 ))) /* Call dialog procedure */
391 result = WINPROC_CallDlgProc16( dlgproc, hwnd, msg, wParam, lParam );
393 if (!result && IsWindow(hwnd32))
395 /* callback didn't process this message */
397 switch(msg)
399 case WM_ERASEBKGND:
400 case WM_SHOWWINDOW:
401 case WM_ACTIVATE:
402 case WM_SETFOCUS:
403 case DM_SETDEFID:
404 case DM_GETDEFID:
405 case WM_NEXTDLGCTL:
406 case WM_GETFONT:
407 case WM_CLOSE:
408 case WM_NCDESTROY:
409 case WM_ENTERMENULOOP:
410 case WM_LBUTTONDOWN:
411 case WM_NCLBUTTONDOWN:
412 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, dlgInfo );
413 case WM_INITDIALOG:
414 case WM_VKEYTOITEM:
415 case WM_COMPAREITEM:
416 case WM_CHARTOITEM:
417 break;
419 default:
420 return DefWindowProc16( hwnd, msg, wParam, lParam );
423 return DEFDLG_Epilog( hwnd32, msg, result);
427 /***********************************************************************
428 * DefDlgProcA (USER32.@)
430 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
432 DIALOGINFO *dlgInfo;
433 DLGPROC dlgproc;
434 BOOL result = FALSE;
436 /* Perform DIALOGINFO initialization if not done */
437 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
439 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
441 if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
442 result = WINPROC_CallDlgProcA( dlgproc, hwnd, msg, wParam, lParam );
444 if (!result && IsWindow(hwnd))
446 /* callback didn't process this message */
448 switch(msg)
450 case WM_ERASEBKGND:
451 case WM_SHOWWINDOW:
452 case WM_ACTIVATE:
453 case WM_SETFOCUS:
454 case DM_SETDEFID:
455 case DM_GETDEFID:
456 case WM_NEXTDLGCTL:
457 case WM_GETFONT:
458 case WM_CLOSE:
459 case WM_NCDESTROY:
460 case WM_ENTERMENULOOP:
461 case WM_LBUTTONDOWN:
462 case WM_NCLBUTTONDOWN:
463 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
464 case WM_INITDIALOG:
465 case WM_VKEYTOITEM:
466 case WM_COMPAREITEM:
467 case WM_CHARTOITEM:
468 break;
470 default:
471 return DefWindowProcA( hwnd, msg, wParam, lParam );
474 return DEFDLG_Epilog(hwnd, msg, result);
478 /***********************************************************************
479 * DefDlgProcW (USER32.@)
481 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
483 DIALOGINFO *dlgInfo;
484 BOOL result = FALSE;
485 DLGPROC dlgproc;
487 /* Perform DIALOGINFO intialization if not done */
488 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
490 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, 0 );
492 if ((dlgproc = DEFDLG_GetDlgProc( hwnd ))) /* Call dialog procedure */
493 result = WINPROC_CallDlgProcW( dlgproc, hwnd, msg, wParam, lParam );
495 if (!result && IsWindow(hwnd))
497 /* callback didn't process this message */
499 switch(msg)
501 case WM_ERASEBKGND:
502 case WM_SHOWWINDOW:
503 case WM_ACTIVATE:
504 case WM_SETFOCUS:
505 case DM_SETDEFID:
506 case DM_GETDEFID:
507 case WM_NEXTDLGCTL:
508 case WM_GETFONT:
509 case WM_CLOSE:
510 case WM_NCDESTROY:
511 case WM_ENTERMENULOOP:
512 case WM_LBUTTONDOWN:
513 case WM_NCLBUTTONDOWN:
514 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
515 case WM_INITDIALOG:
516 case WM_VKEYTOITEM:
517 case WM_COMPAREITEM:
518 case WM_CHARTOITEM:
519 break;
521 default:
522 return DefWindowProcW( hwnd, msg, wParam, lParam );
525 return DEFDLG_Epilog(hwnd, msg, result);