Get rid of the WIN_SetRectangles export from user32.
[wine.git] / windows / defdlg.c
blobe6b1da8ab0000b37cd865f4051e716eeace06add
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, hwndTmp;
120 hwndChild = GetWindow( hwndDlg, GW_CHILD );
121 while (hwndChild)
123 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
124 break;
126 /* Recurse into WS_EX_CONTROLPARENT controls */
127 if (GetWindowLongA( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
129 LONG dsStyle = GetWindowLongA( hwndChild, GWL_STYLE );
130 if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
131 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
132 return hwndTmp;
134 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
136 return hwndChild;
140 /***********************************************************************
141 * DEFDLG_SetDefId
143 * Set the default button id.
145 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
147 DWORD dlgcode=0; /* initialize just to avoid a warning */
148 HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
149 INT old_id = dlgInfo->idResult;
151 dlgInfo->idResult = wParam;
152 if (hwndNew &&
153 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
154 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
155 return FALSE; /* Destination is not a push button */
157 /* Make sure the old default control is a valid push button ID */
158 hwndOld = GetDlgItem( hwndDlg, old_id );
159 if (!hwndOld || !(SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
160 hwndOld = DEFDLG_FindDefButton( hwndDlg );
161 if (hwndOld && hwndOld != hwndNew)
162 SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
164 if (hwndNew)
166 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
167 SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
169 return TRUE;
173 /***********************************************************************
174 * DEFDLG_SetDefButton
176 * Set the new default button to be hwndNew.
178 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
180 DWORD dlgcode=0; /* initialize just to avoid a warning */
181 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
183 if (hwndNew &&
184 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
185 & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
188 * Need to draw only default push button rectangle.
189 * Since the next control is not a push button, need to draw the push
190 * button rectangle for the default control.
192 hwndNew = hwndOld;
193 dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
196 /* Make sure the old default control is a valid push button ID */
197 if (!hwndOld || !(SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
198 hwndOld = DEFDLG_FindDefButton( hwndDlg );
199 if (hwndOld && hwndOld != hwndNew)
200 SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
202 if (hwndNew)
204 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
205 SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
207 return TRUE;
211 /***********************************************************************
212 * DEFDLG_Proc
214 * Implementation of DefDlgProc(). Only handle messages that need special
215 * handling for dialogs.
217 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
218 LPARAM lParam, DIALOGINFO *dlgInfo )
220 switch(msg)
222 case WM_ERASEBKGND:
224 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
225 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
226 if (brush)
228 RECT rect;
229 HDC hdc = (HDC)wParam;
230 GetClientRect( hwnd, &rect );
231 DPtoLP( hdc, (LPPOINT)&rect, 2 );
232 FillRect( hdc, &rect, brush );
234 return 1;
236 case WM_NCDESTROY:
237 if ((dlgInfo = (DIALOGINFO *)SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, 0 )))
239 /* Free dialog heap (if created) */
240 if (dlgInfo->hDialogHeap)
242 GlobalUnlock16(dlgInfo->hDialogHeap);
243 GlobalFree16(dlgInfo->hDialogHeap);
245 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
246 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
247 WINPROC_FreeProc( DEFDLG_GetDlgProc( hwnd ), WIN_PROC_WINDOW );
248 HeapFree( GetProcessHeap(), 0, dlgInfo );
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 );
259 else DEFDLG_SaveFocus( hwnd );
260 return 0;
262 case WM_SETFOCUS:
263 DEFDLG_RestoreFocus( hwnd );
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( hwnd, 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 (!SendMessageA( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
302 SendMessageA( 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 case WM_NOTIFYFORMAT:
316 return DefWindowProcA( hwnd, msg, wParam, lParam );
318 return 0;
321 /***********************************************************************
322 * DEFDLG_Epilog
324 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
326 /* see SDK 3.1 */
328 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
329 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
330 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
331 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
332 return fResult;
334 return GetWindowLongA( hwnd, DWL_MSGRESULT );
337 /***********************************************************************
338 * DIALOG_get_info
340 * Get the DIALOGINFO structure of a window, allocating it if needed
341 * and 'create' is TRUE.
343 DIALOGINFO *DIALOG_get_info( HWND hwnd, BOOL create )
345 WND* wndPtr;
346 DIALOGINFO* dlgInfo = (DIALOGINFO *)GetWindowLongW( hwnd, DWL_WINE_DIALOGINFO );
348 if(!dlgInfo && create)
350 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return NULL;
351 dlgInfo->hwndFocus = 0;
352 dlgInfo->hUserFont = 0;
353 dlgInfo->hMenu = 0;
354 dlgInfo->xBaseUnit = 0;
355 dlgInfo->yBaseUnit = 0;
356 dlgInfo->idResult = 0;
357 dlgInfo->flags = 0;
358 dlgInfo->hDialogHeap = 0;
359 wndPtr = WIN_GetPtr( hwnd );
360 if (wndPtr && wndPtr != WND_OTHER_PROCESS)
362 wndPtr->flags |= WIN_ISDIALOG;
363 WIN_ReleasePtr( wndPtr );
364 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
366 else
368 HeapFree( GetProcessHeap(), 0, dlgInfo );
369 return NULL;
372 return dlgInfo;
375 /***********************************************************************
376 * DefDlgProc (USER.308)
378 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
379 LPARAM lParam )
381 DIALOGINFO *dlgInfo;
382 WNDPROC16 dlgproc;
383 HWND hwnd32 = WIN_Handle32( hwnd );
384 BOOL result = FALSE;
386 /* Perform DIALOGINFO intialization if not done */
387 if(!(dlgInfo = DIALOG_get_info(hwnd32, TRUE))) return -1;
389 SetWindowLongW( hwnd32, DWL_MSGRESULT, 0 );
391 if ((dlgproc = (WNDPROC16)DEFDLG_GetDlgProc( hwnd32 )))
393 /* Call dialog procedure */
394 result = CallWindowProc16( dlgproc, hwnd, msg, wParam, lParam );
395 /* 16 bit dlg procs only return BOOL16 */
396 if( WINPROC_GetProcType( (WNDPROC)dlgproc ) == WIN_PROC_16 )
397 result = LOWORD(result);
400 if (!result && IsWindow(hwnd32))
402 /* callback didn't process this message */
404 switch(msg)
406 case WM_ERASEBKGND:
407 case WM_SHOWWINDOW:
408 case WM_ACTIVATE:
409 case WM_SETFOCUS:
410 case DM_SETDEFID:
411 case DM_GETDEFID:
412 case WM_NEXTDLGCTL:
413 case WM_GETFONT:
414 case WM_CLOSE:
415 case WM_NCDESTROY:
416 case WM_ENTERMENULOOP:
417 case WM_LBUTTONDOWN:
418 case WM_NCLBUTTONDOWN:
419 return DEFDLG_Proc( hwnd32, msg, (WPARAM)wParam, lParam, dlgInfo );
420 case WM_INITDIALOG:
421 case WM_VKEYTOITEM:
422 case WM_COMPAREITEM:
423 case WM_CHARTOITEM:
424 break;
426 default:
427 return DefWindowProc16( hwnd, msg, wParam, lParam );
430 return DEFDLG_Epilog( hwnd32, msg, result);
434 /***********************************************************************
435 * DefDlgProcA (USER32.@)
437 LRESULT WINAPI DefDlgProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
439 DIALOGINFO *dlgInfo;
440 WNDPROC dlgproc;
441 BOOL result = FALSE;
443 /* Perform DIALOGINFO initialization if not done */
444 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
446 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
448 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
450 /* Call dialog procedure */
451 result = CallWindowProcA( dlgproc, hwnd, msg, wParam, lParam );
452 /* 16 bit dlg procs only return BOOL16 */
453 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
454 result = LOWORD(result);
457 if (!result && IsWindow(hwnd))
459 /* callback didn't process this message */
461 switch(msg)
463 case WM_ERASEBKGND:
464 case WM_SHOWWINDOW:
465 case WM_ACTIVATE:
466 case WM_SETFOCUS:
467 case DM_SETDEFID:
468 case DM_GETDEFID:
469 case WM_NEXTDLGCTL:
470 case WM_GETFONT:
471 case WM_CLOSE:
472 case WM_NCDESTROY:
473 case WM_ENTERMENULOOP:
474 case WM_LBUTTONDOWN:
475 case WM_NCLBUTTONDOWN:
476 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
477 case WM_INITDIALOG:
478 case WM_VKEYTOITEM:
479 case WM_COMPAREITEM:
480 case WM_CHARTOITEM:
481 break;
483 default:
484 return DefWindowProcA( hwnd, msg, wParam, lParam );
487 return DEFDLG_Epilog(hwnd, msg, result);
491 /***********************************************************************
492 * DefDlgProcW (USER32.@)
494 LRESULT WINAPI DefDlgProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
496 DIALOGINFO *dlgInfo;
497 BOOL result = FALSE;
498 WNDPROC dlgproc;
500 /* Perform DIALOGINFO intialization if not done */
501 if(!(dlgInfo = DIALOG_get_info( hwnd, TRUE ))) return -1;
503 SetWindowLongW( hwnd, DWL_MSGRESULT, 0 );
505 if ((dlgproc = DEFDLG_GetDlgProc( hwnd )))
507 /* Call dialog procedure */
508 result = CallWindowProcW( dlgproc, hwnd, msg, wParam, lParam );
509 /* 16 bit dlg procs only return BOOL16 */
510 if( WINPROC_GetProcType( dlgproc ) == WIN_PROC_16 )
511 result = LOWORD(result);
514 if (!result && IsWindow(hwnd))
516 /* callback didn't process this message */
518 switch(msg)
520 case WM_ERASEBKGND:
521 case WM_SHOWWINDOW:
522 case WM_ACTIVATE:
523 case WM_SETFOCUS:
524 case DM_SETDEFID:
525 case DM_GETDEFID:
526 case WM_NEXTDLGCTL:
527 case WM_GETFONT:
528 case WM_CLOSE:
529 case WM_NCDESTROY:
530 case WM_ENTERMENULOOP:
531 case WM_LBUTTONDOWN:
532 case WM_NCLBUTTONDOWN:
533 return DEFDLG_Proc( hwnd, msg, wParam, lParam, dlgInfo );
534 case WM_INITDIALOG:
535 case WM_VKEYTOITEM:
536 case WM_COMPAREITEM:
537 case WM_CHARTOITEM:
538 break;
540 default:
541 return DefWindowProcW( hwnd, msg, wParam, lParam );
544 return DEFDLG_Epilog(hwnd, msg, result);