Rewrite cookies to use shared list.h list code.
[wine/multimedia.git] / windows / defdlg.c
blob0d9f1f0440b87881a97e869c638a1888d5303deb
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 WNDPROC DEFDLG_GetDlgProc( HWND hwnd )
40 WNDPROC 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 = *(WNDPROC *)((char *)wndPtr->wExtra + DWL_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 if (!IsWindow( infoPtr->hwndFocus )) return;
100 /* Don't set the focus back to controls if EndDialog is already called.*/
101 if (!(infoPtr->flags & DF_END))
103 DEFDLG_SetFocus( hwnd, infoPtr->hwndFocus );
104 return;
106 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
107 sometimes losing focus when receiving WM_SETFOCUS messages. */
111 /***********************************************************************
112 * DEFDLG_FindDefButton
114 * Find the current default push-button.
116 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
118 HWND hwndChild = GetWindow( hwndDlg, GW_CHILD );
119 while (hwndChild)
121 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
122 break;
123 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
125 return hwndChild;
129 /***********************************************************************
130 * DEFDLG_SetDefButton
132 * Set the new default button to be hwndNew.
134 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam )
136 DWORD dlgcode=0; /* initialize just to avoid a warning */
137 HWND hwndNew = GetDlgItem(hwndDlg, wParam);
139 dlgInfo->idResult = wParam;
140 if (hwndNew &&
141 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
142 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
143 return FALSE; /* Destination is not a push button */
145 if (dlgInfo->idResult) /* There's already a default pushbutton */
147 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
148 if (hwndOld && (SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
149 SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
151 if (hwndNew)
153 if(dlgcode==DLGC_UNDEFPUSHBUTTON)
154 SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
156 return TRUE;
160 /***********************************************************************
161 * DEFDLG_Proc
163 * Implementation of DefDlgProc(). Only handle messages that need special
164 * handling for dialogs.
166 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
167 LPARAM lParam, DIALOGINFO *dlgInfo )
169 switch(msg)
171 case WM_ERASEBKGND:
173 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
174 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
175 if (brush)
177 RECT rect;
178 HDC hdc = (HDC)wParam;
179 GetClientRect( hwnd, &rect );
180 DPtoLP( hdc, (LPPOINT)&rect, 2 );
181 FillRect( hdc, &rect, brush );
183 return 1;
185 case WM_NCDESTROY:
186 if ((dlgInfo = (DIALOGINFO *)SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, 0 )))
188 /* Free dialog heap (if created) */
189 if (dlgInfo->hDialogHeap)
191 GlobalUnlock16(dlgInfo->hDialogHeap);
192 GlobalFree16(dlgInfo->hDialogHeap);
194 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
195 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
196 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd ), WIN_PROC_WINDOW );
197 HeapFree( GetProcessHeap(), 0, dlgInfo );
199 /* Window clean-up */
200 return DefWindowProcA( hwnd, msg, wParam, lParam );
202 case WM_SHOWWINDOW:
203 if (!wParam) DEFDLG_SaveFocus( hwnd );
204 return DefWindowProcA( hwnd, msg, wParam, lParam );
206 case WM_ACTIVATE:
207 if (wParam) DEFDLG_RestoreFocus( hwnd );
208 else DEFDLG_SaveFocus( hwnd );
209 return 0;
211 case WM_SETFOCUS:
212 DEFDLG_RestoreFocus( hwnd );
213 return 0;
215 case DM_SETDEFID:
216 if (dlgInfo && !(dlgInfo->flags & DF_END))
217 DEFDLG_SetDefButton( hwnd, dlgInfo, wParam );
218 return 1;
220 case DM_GETDEFID:
221 if (dlgInfo && !(dlgInfo->flags & DF_END))
223 HWND hwndDefId;
224 if (dlgInfo->idResult)
225 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
226 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
227 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
229 return 0;
231 case WM_NEXTDLGCTL:
232 if (dlgInfo)
234 HWND hwndDest = (HWND)wParam;
235 if (!lParam)
236 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
237 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
238 DEFDLG_SetDefButton( hwnd, dlgInfo, GetDlgCtrlID(hwndDest) );
240 return 0;
242 case WM_ENTERMENULOOP:
243 case WM_LBUTTONDOWN:
244 case WM_NCLBUTTONDOWN:
246 HWND hwndFocus = GetFocus();
247 if (hwndFocus)
249 /* always make combo box hide its listbox control */
250 if (!SendMessageA( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
251 SendMessageA( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
254 return DefWindowProcA( hwnd, msg, wParam, lParam );
256 case WM_GETFONT:
257 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
259 case WM_CLOSE:
260 PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
261 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
262 return 0;
264 case WM_NOTIFYFORMAT:
265 return DefWindowProcA( hwnd, msg, wParam, lParam );
267 return 0;
270 /***********************************************************************
271 * DEFDLG_Epilog
273 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
275 /* see SDK 3.1 */
277 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
278 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
279 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
280 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
281 return fResult;
283 return GetWindowLongA( hwnd, DWL_MSGRESULT );
286 /***********************************************************************
287 * DIALOG_get_info
289 * Get the DIALOGINFO structure of a window, allocating it if needed
290 * and 'create' is TRUE.
292 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
294 WND* wndPtr;
295 DIALOGINFO* dlgInfo = (DIALOGINFO *)GetWindowLongW( hwnd, DWL_WINE_DIALOGINFO );
297 if(!dlgInfo && create)
299 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return NULL;
300 dlgInfo->hwndFocus = 0;
301 dlgInfo->hUserFont = 0;
302 dlgInfo->hMenu = 0;
303 dlgInfo->xBaseUnit = 0;
304 dlgInfo->yBaseUnit = 0;
305 dlgInfo->idResult = 0;
306 dlgInfo->flags = 0;
307 dlgInfo->hDialogHeap = 0;
308 wndPtr = WIN_GetPtr( hwnd );
309 if (wndPtr && wndPtr != WND_OTHER_PROCESS)
311 wndPtr->flags |= WIN_ISDIALOG;
312 WIN_ReleasePtr( wndPtr );
313 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
315 else
317 HeapFree( GetProcessHeap(), 0, dlgInfo );
318 return NULL;
321 return dlgInfo;
324 /***********************************************************************
325 * DefDlgProc (USER.308)
327 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
328 LPARAM lParam )
330 DIALOGINFO *dlgInfo;
331 WNDPROC16 dlgproc;
332 HWND hwnd32 = WIN_Handle32( hwnd );
333 BOOL result = FALSE;
335 /* Perform DIALOGINFO intialization if not done */
336 if(!(dlgInfo = DIALOG_get_info(hwnd32, TRUE))) return -1;
338 SetWindowLongW( hwnd32, DWL_MSGRESULT, 0 );
340 if ((dlgproc = (WNDPROC16)DEFDLG_GetDlgProc( hwnd32 )))
342 /* Call dialog procedure */
343 result = CallWindowProc16( dlgproc, hwnd, msg, wParam, lParam );
344 /* 16 bit dlg procs only return BOOL16 */
345 if( WINPROC_GetProcType( (WNDPROC)dlgproc ) == WIN_PROC_16 )
346 result = LOWORD(result);
349 if (!result && IsWindow(hwnd32))
351 /* callback didn't process this message */
353 switch(msg)
355 case WM_ERASEBKGND:
356 case WM_SHOWWINDOW:
357 case WM_ACTIVATE:
358 case WM_SETFOCUS:
359 case DM_SETDEFID:
360 case DM_GETDEFID:
361 case WM_NEXTDLGCTL:
362 case WM_GETFONT:
363 case WM_CLOSE:
364 case WM_NCDESTROY:
365 case WM_ENTERMENULOOP:
366 case WM_LBUTTONDOWN:
367 case WM_NCLBUTTONDOWN:
368 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, dlgInfo );
369 case WM_INITDIALOG:
370 case WM_VKEYTOITEM:
371 case WM_COMPAREITEM:
372 case WM_CHARTOITEM:
373 break;
375 default:
376 return DefWindowProc16( hwnd, msg, wParam, lParam );
379 return DEFDLG_Epilog( hwnd32, msg, result);
383 /***********************************************************************
384 * DefDlgProcA (USER32.@)
386 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
388 DIALOGINFO *dlgInfo;
389 WNDPROC dlgproc;
390 BOOL result = FALSE;
392 /* Perform DIALOGINFO initialization if not done */
393 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
395 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
397 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
399 /* Call dialog procedure */
400 result = CallWindowProcA( dlgproc, hwnd, msg, wParam, lParam );
401 /* 16 bit dlg procs only return BOOL16 */
402 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
403 result = LOWORD(result);
406 if (!result && IsWindow(hwnd))
408 /* callback didn't process this message */
410 switch(msg)
412 case WM_ERASEBKGND:
413 case WM_SHOWWINDOW:
414 case WM_ACTIVATE:
415 case WM_SETFOCUS:
416 case DM_SETDEFID:
417 case DM_GETDEFID:
418 case WM_NEXTDLGCTL:
419 case WM_GETFONT:
420 case WM_CLOSE:
421 case WM_NCDESTROY:
422 case WM_ENTERMENULOOP:
423 case WM_LBUTTONDOWN:
424 case WM_NCLBUTTONDOWN:
425 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
426 case WM_INITDIALOG:
427 case WM_VKEYTOITEM:
428 case WM_COMPAREITEM:
429 case WM_CHARTOITEM:
430 break;
432 default:
433 return DefWindowProcA( hwnd, msg, wParam, lParam );
436 return DEFDLG_Epilog(hwnd, msg, result);
440 /***********************************************************************
441 * DefDlgProcW (USER32.@)
443 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
445 DIALOGINFO *dlgInfo;
446 BOOL result = FALSE;
447 WNDPROC dlgproc;
449 /* Perform DIALOGINFO intialization if not done */
450 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
452 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
454 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
456 /* Call dialog procedure */
457 result = CallWindowProcW( dlgproc, hwnd, msg, wParam, lParam );
458 /* 16 bit dlg procs only return BOOL16 */
459 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
460 result = LOWORD(result);
463 if (!result && IsWindow(hwnd))
465 /* callback didn't process this message */
467 switch(msg)
469 case WM_ERASEBKGND:
470 case WM_SHOWWINDOW:
471 case WM_ACTIVATE:
472 case WM_SETFOCUS:
473 case DM_SETDEFID:
474 case DM_GETDEFID:
475 case WM_NEXTDLGCTL:
476 case WM_GETFONT:
477 case WM_CLOSE:
478 case WM_NCDESTROY:
479 case WM_ENTERMENULOOP:
480 case WM_LBUTTONDOWN:
481 case WM_NCLBUTTONDOWN:
482 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
483 case WM_INITDIALOG:
484 case WM_VKEYTOITEM:
485 case WM_COMPAREITEM:
486 case WM_CHARTOITEM:
487 break;
489 default:
490 return DefWindowProcW( hwnd, msg, wParam, lParam );
493 return DEFDLG_Epilog(hwnd, msg, result);