user32: If a dialog has no tab-accessible controls, set focus to first control (with...
[wine/multimedia.git] / dlls / user32 / dialog.c
blobc6b2455a9540d620b40a6f91fb4a1dbb290747be
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 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 "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "wine/unicode.h"
38 #include "controls.h"
39 #include "win.h"
40 #include "user_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
46 /* Dialog control information */
47 typedef struct
49 DWORD style;
50 DWORD exStyle;
51 DWORD helpId;
52 INT16 x;
53 INT16 y;
54 INT16 cx;
55 INT16 cy;
56 UINT_PTR id;
57 LPCWSTR className;
58 LPCWSTR windowName;
59 LPCVOID data;
60 } DLG_CONTROL_INFO;
62 /* Dialog template */
63 typedef struct
65 DWORD style;
66 DWORD exStyle;
67 DWORD helpId;
68 UINT16 nbItems;
69 INT16 x;
70 INT16 y;
71 INT16 cx;
72 INT16 cy;
73 LPCWSTR menuName;
74 LPCWSTR className;
75 LPCWSTR caption;
76 INT16 pointSize;
77 WORD weight;
78 BOOL italic;
79 LPCWSTR faceName;
80 BOOL dialogEx;
81 } DLG_TEMPLATE;
83 /* Radio button group */
84 typedef struct
86 UINT firstID;
87 UINT lastID;
88 UINT checkID;
89 } RADIOGROUP;
92 /*********************************************************************
93 * dialog class descriptor
95 const struct builtin_class_descr DIALOG_builtin_class =
97 (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
98 CS_SAVEBITS | CS_DBLCLKS, /* style */
99 WINPROC_DIALOG, /* proc */
100 DLGWINDOWEXTRA, /* extra */
101 IDC_ARROW, /* cursor */
102 0 /* brush */
106 /***********************************************************************
107 * DIALOG_EnableOwner
109 * Helper function for modal dialogs to enable again the
110 * owner of the dialog box.
112 static void DIALOG_EnableOwner( HWND hOwner )
114 /* Owner must be a top-level window */
115 if (hOwner)
116 hOwner = GetAncestor( hOwner, GA_ROOT );
117 if (!hOwner) return;
118 EnableWindow( hOwner, TRUE );
122 /***********************************************************************
123 * DIALOG_DisableOwner
125 * Helper function for modal dialogs to disable the
126 * owner of the dialog box. Returns TRUE if owner was enabled.
128 static BOOL DIALOG_DisableOwner( HWND hOwner )
130 /* Owner must be a top-level window */
131 if (hOwner)
132 hOwner = GetAncestor( hOwner, GA_ROOT );
133 if (!hOwner) return FALSE;
134 if (IsWindowEnabled( hOwner ))
136 EnableWindow( hOwner, FALSE );
137 return TRUE;
139 else
140 return FALSE;
143 /***********************************************************************
144 * DIALOG_GetControl32
146 * Return the class and text of the control pointed to by ptr,
147 * fill the header structure and return a pointer to the next control.
149 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
150 BOOL dialogEx )
152 if (dialogEx)
154 info->helpId = GET_DWORD(p); p += 2;
155 info->exStyle = GET_DWORD(p); p += 2;
156 info->style = GET_DWORD(p); p += 2;
158 else
160 info->helpId = 0;
161 info->style = GET_DWORD(p); p += 2;
162 info->exStyle = GET_DWORD(p); p += 2;
164 info->x = GET_WORD(p); p++;
165 info->y = GET_WORD(p); p++;
166 info->cx = GET_WORD(p); p++;
167 info->cy = GET_WORD(p); p++;
169 if (dialogEx)
171 /* id is a DWORD for DIALOGEX */
172 info->id = GET_DWORD(p);
173 p += 2;
175 else
177 info->id = GET_WORD(p);
178 p++;
181 if (GET_WORD(p) == 0xffff)
183 static const WCHAR class_names[6][10] =
185 { 'B','u','t','t','o','n', }, /* 0x80 */
186 { 'E','d','i','t', }, /* 0x81 */
187 { 'S','t','a','t','i','c', }, /* 0x82 */
188 { 'L','i','s','t','B','o','x', }, /* 0x83 */
189 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
190 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
192 WORD id = GET_WORD(p+1);
193 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
194 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
195 if (id <= 5)
196 info->className = class_names[id];
197 else
199 info->className = NULL;
200 ERR("Unknown built-in class id %04x\n", id );
202 p += 2;
204 else
206 info->className = p;
207 p += strlenW( info->className ) + 1;
210 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
212 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
213 p += 2;
215 else
217 info->windowName = p;
218 p += strlenW( info->windowName ) + 1;
221 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
222 debugstr_w( info->className ), debugstr_w( info->windowName ),
223 info->id, info->x, info->y, info->cx, info->cy,
224 info->style, info->exStyle, info->helpId );
226 if (GET_WORD(p))
228 if (TRACE_ON(dialog))
230 WORD i, count = GET_WORD(p) / sizeof(WORD);
231 TRACE(" BEGIN\n");
232 TRACE(" ");
233 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
234 TRACE("\n");
235 TRACE(" END\n" );
237 info->data = p + 1;
238 p += GET_WORD(p) / sizeof(WORD);
240 else info->data = NULL;
241 p++;
243 /* Next control is on dword boundary */
244 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
248 /***********************************************************************
249 * DIALOG_CreateControls32
251 * Create the control windows for a dialog.
253 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
254 HINSTANCE hInst, BOOL unicode )
256 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
257 DLG_CONTROL_INFO info;
258 HWND hwndCtrl, hwndDefButton = 0;
259 INT items = dlgTemplate->nbItems;
261 TRACE(" BEGIN\n" );
262 while (items--)
264 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
265 dlgTemplate->dialogEx );
266 info.style &= ~WS_POPUP;
267 info.style |= WS_CHILD;
269 if (info.style & WS_BORDER)
271 info.style &= ~WS_BORDER;
272 info.exStyle |= WS_EX_CLIENTEDGE;
274 if (unicode)
276 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
277 info.className, info.windowName,
278 info.style | WS_CHILD,
279 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
281 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
282 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
283 hwnd, (HMENU)info.id,
284 hInst, (LPVOID)info.data );
286 else
288 LPCSTR class = (LPCSTR)info.className;
289 LPCSTR caption = (LPCSTR)info.windowName;
290 LPSTR class_tmp = NULL;
291 LPSTR caption_tmp = NULL;
293 if (!IS_INTRESOURCE(class))
295 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
296 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
297 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
298 class = class_tmp;
300 if (!IS_INTRESOURCE(caption))
302 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
303 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
304 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
305 caption = caption_tmp;
307 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
308 class, caption, info.style | WS_CHILD,
309 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
310 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
311 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
312 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
313 hwnd, (HMENU)info.id,
314 hInst, (LPVOID)info.data );
315 HeapFree( GetProcessHeap(), 0, class_tmp );
316 HeapFree( GetProcessHeap(), 0, caption_tmp );
318 if (!hwndCtrl)
320 WARN("control %s %s creation failed\n", debugstr_w(info.className),
321 debugstr_w(info.windowName));
322 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
323 return FALSE;
326 /* Send initialisation messages to the control */
327 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
328 (WPARAM)dlgInfo->hUserFont, 0 );
329 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
331 /* If there's already a default push-button, set it back */
332 /* to normal and use this one instead. */
333 if (hwndDefButton)
334 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
335 hwndDefButton = hwndCtrl;
336 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
339 TRACE(" END\n" );
340 return TRUE;
344 /***********************************************************************
345 * DIALOG_ParseTemplate32
347 * Fill a DLG_TEMPLATE structure from the dialog template, and return
348 * a pointer to the first control.
350 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
352 const WORD *p = (const WORD *)template;
353 WORD signature;
354 WORD dlgver;
356 dlgver = GET_WORD(p); p++;
357 signature = GET_WORD(p); p++;
359 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
361 result->dialogEx = TRUE;
362 result->helpId = GET_DWORD(p); p += 2;
363 result->exStyle = GET_DWORD(p); p += 2;
364 result->style = GET_DWORD(p); p += 2;
366 else
368 result->style = GET_DWORD(p - 2);
369 result->dialogEx = FALSE;
370 result->helpId = 0;
371 result->exStyle = GET_DWORD(p); p += 2;
373 result->nbItems = GET_WORD(p); p++;
374 result->x = GET_WORD(p); p++;
375 result->y = GET_WORD(p); p++;
376 result->cx = GET_WORD(p); p++;
377 result->cy = GET_WORD(p); p++;
378 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
379 result->dialogEx ? "EX" : "", result->x, result->y,
380 result->cx, result->cy, result->helpId );
381 TRACE(" STYLE 0x%08x\n", result->style );
382 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
384 /* Get the menu name */
386 switch(GET_WORD(p))
388 case 0x0000:
389 result->menuName = NULL;
390 p++;
391 break;
392 case 0xffff:
393 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
394 p += 2;
395 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
396 break;
397 default:
398 result->menuName = p;
399 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
400 p += strlenW( result->menuName ) + 1;
401 break;
404 /* Get the class name */
406 switch(GET_WORD(p))
408 case 0x0000:
409 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
410 p++;
411 break;
412 case 0xffff:
413 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
414 p += 2;
415 TRACE(" CLASS %04x\n", LOWORD(result->className) );
416 break;
417 default:
418 result->className = p;
419 TRACE(" CLASS %s\n", debugstr_w( result->className ));
420 p += strlenW( result->className ) + 1;
421 break;
424 /* Get the window caption */
426 result->caption = p;
427 p += strlenW( result->caption ) + 1;
428 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
430 /* Get the font name */
432 result->pointSize = 0;
433 result->faceName = NULL;
434 result->weight = FW_DONTCARE;
435 result->italic = FALSE;
437 if (result->style & DS_SETFONT)
439 result->pointSize = GET_WORD(p);
440 p++;
442 /* If pointSize is 0x7fff, it means that we need to use the font
443 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
444 * italic, and facename from the dialog template.
446 if (result->pointSize == 0x7fff)
448 /* We could call SystemParametersInfo here, but then we'd have
449 * to convert from pixel size to point size (which can be
450 * imprecise).
452 TRACE(" FONT: Using message box font\n");
454 else
456 if (result->dialogEx)
458 result->weight = GET_WORD(p); p++;
459 result->italic = LOBYTE(GET_WORD(p)); p++;
461 result->faceName = p;
462 p += strlenW( result->faceName ) + 1;
464 TRACE(" FONT %d, %s, %d, %s\n",
465 result->pointSize, debugstr_w( result->faceName ),
466 result->weight, result->italic ? "TRUE" : "FALSE" );
470 /* First control is on dword boundary */
471 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
475 /***********************************************************************
476 * DIALOG_CreateIndirect
477 * Creates a dialog box window
479 * modal = TRUE if we are called from a modal dialog box.
480 * (it's more compatible to do it here, as under Windows the owner
481 * is never disabled if the dialog fails because of an invalid template)
483 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
484 HWND owner, DLGPROC dlgProc, LPARAM param,
485 BOOL unicode, BOOL modal )
487 HWND hwnd;
488 RECT rect;
489 POINT pos;
490 SIZE size;
491 DLG_TEMPLATE template;
492 DIALOGINFO * dlgInfo = NULL;
493 DWORD units = GetDialogBaseUnits();
494 BOOL ownerEnabled = TRUE;
495 HMENU hMenu = 0;
496 HFONT hUserFont = 0;
497 UINT flags = 0;
498 UINT xBaseUnit = LOWORD(units);
499 UINT yBaseUnit = HIWORD(units);
501 /* Parse dialog template */
503 if (!dlgTemplate) return 0;
504 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
506 /* Load menu */
508 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
510 /* Create custom font if needed */
512 if (template.style & DS_SETFONT)
514 HDC dc = GetDC(0);
516 if (template.pointSize == 0x7fff)
518 /* We get the message font from the non-client metrics */
519 NONCLIENTMETRICSW ncMetrics;
521 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
522 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
523 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
525 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
528 else
530 /* We convert the size to pixels and then make it -ve. This works
531 * for both +ve and -ve template.pointSize */
532 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
533 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
534 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
535 PROOF_QUALITY, FF_DONTCARE,
536 template.faceName );
539 if (hUserFont)
541 SIZE charSize;
542 HFONT hOldFont = SelectObject( dc, hUserFont );
543 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
544 if (charSize.cx)
546 xBaseUnit = charSize.cx;
547 yBaseUnit = charSize.cy;
549 SelectObject( dc, hOldFont );
551 ReleaseDC(0, dc);
552 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
555 /* Create dialog main window */
557 rect.left = rect.top = 0;
558 rect.right = MulDiv(template.cx, xBaseUnit, 4);
559 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
560 if (template.style & WS_CHILD)
561 template.style &= ~(WS_CAPTION|WS_SYSMENU);
562 if (template.style & DS_MODALFRAME)
563 template.exStyle |= WS_EX_DLGMODALFRAME;
564 if (template.style & DS_CONTROL)
565 template.exStyle |= WS_EX_CONTROLPARENT;
566 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
567 pos.x = rect.left;
568 pos.y = rect.top;
569 size.cx = rect.right - rect.left;
570 size.cy = rect.bottom - rect.top;
572 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
574 pos.x = pos.y = CW_USEDEFAULT;
576 else
578 HMONITOR monitor = 0;
579 MONITORINFO mon_info;
581 mon_info.cbSize = sizeof(mon_info);
582 if (template.style & DS_CENTER)
584 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
585 GetMonitorInfoW( monitor, &mon_info );
586 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
587 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
589 else if (template.style & DS_CENTERMOUSE)
591 GetCursorPos( &pos );
592 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
593 GetMonitorInfoW( monitor, &mon_info );
595 else
597 pos.x += MulDiv(template.x, xBaseUnit, 4);
598 pos.y += MulDiv(template.y, yBaseUnit, 8);
599 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
601 if ( !(template.style & WS_CHILD) )
603 INT dX, dY;
605 /* try to fit it into the desktop */
607 if (!monitor)
609 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
610 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
611 GetMonitorInfoW( monitor, &mon_info );
613 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
614 pos.x -= dX;
615 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
616 pos.y -= dY;
617 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
618 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
622 if (modal)
624 ownerEnabled = DIALOG_DisableOwner( owner );
625 if (ownerEnabled) flags |= DF_OWNERENABLED;
628 if (unicode)
630 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
631 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
632 owner, hMenu, hInst, NULL );
634 else
636 LPCSTR class = (LPCSTR)template.className;
637 LPCSTR caption = (LPCSTR)template.caption;
638 LPSTR class_tmp = NULL;
639 LPSTR caption_tmp = NULL;
641 if (!IS_INTRESOURCE(class))
643 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
644 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
645 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
646 class = class_tmp;
648 if (!IS_INTRESOURCE(caption))
650 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
651 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
652 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
653 caption = caption_tmp;
655 hwnd = CreateWindowExA(template.exStyle, class, caption,
656 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
657 owner, hMenu, hInst, NULL );
658 HeapFree( GetProcessHeap(), 0, class_tmp );
659 HeapFree( GetProcessHeap(), 0, caption_tmp );
662 if (!hwnd)
664 if (hUserFont) DeleteObject( hUserFont );
665 if (hMenu) DestroyMenu( hMenu );
666 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
667 return 0;
670 /* moved this from the top of the method to here as DIALOGINFO structure
671 will be valid only after WM_CREATE message has been handled in DefDlgProc
672 All the members of the structure get filled here using temp variables */
673 dlgInfo = DIALOG_get_info( hwnd, TRUE );
674 dlgInfo->hwndFocus = 0;
675 dlgInfo->hUserFont = hUserFont;
676 dlgInfo->hMenu = hMenu;
677 dlgInfo->xBaseUnit = xBaseUnit;
678 dlgInfo->yBaseUnit = yBaseUnit;
679 dlgInfo->flags = flags;
681 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
683 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
684 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
686 if (dlgProc && dlgInfo->hUserFont)
687 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
689 /* Create controls */
691 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
693 /* Send initialisation messages and set focus */
695 if (dlgProc)
697 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
698 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
699 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
700 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
702 /* By returning TRUE, app has requested a default focus assignment.
703 * WM_INITDIALOG may have changed the tab order, so find the first
704 * tabstop control again. */
705 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
706 if (!dlgInfo->hwndFocus) dlgInfo->hwndFocus = GetNextDlgGroupItem( hwnd, 0, FALSE );
707 if( dlgInfo->hwndFocus )
708 SetFocus( dlgInfo->hwndFocus );
712 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
714 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
716 return hwnd;
718 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
719 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
720 return 0;
724 /***********************************************************************
725 * CreateDialogParamA (USER32.@)
727 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
728 DLGPROC dlgProc, LPARAM param )
730 HRSRC hrsrc;
731 LPCDLGTEMPLATEA ptr;
733 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
734 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
735 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
739 /***********************************************************************
740 * CreateDialogParamW (USER32.@)
742 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
743 DLGPROC dlgProc, LPARAM param )
745 HRSRC hrsrc;
746 LPCDLGTEMPLATEA ptr;
748 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
749 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
750 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
754 /***********************************************************************
755 * CreateDialogIndirectParamAorW (USER32.@)
757 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
758 HWND owner, DLGPROC dlgProc, LPARAM param,
759 DWORD flags )
761 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
764 /***********************************************************************
765 * CreateDialogIndirectParamA (USER32.@)
767 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
768 HWND owner, DLGPROC dlgProc, LPARAM param )
770 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
773 /***********************************************************************
774 * CreateDialogIndirectParamW (USER32.@)
776 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
777 HWND owner, DLGPROC dlgProc, LPARAM param )
779 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
783 /***********************************************************************
784 * DIALOG_DoDialogBox
786 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
788 DIALOGINFO * dlgInfo;
789 MSG msg;
790 INT retval;
791 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
792 BOOL bFirstEmpty;
794 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
796 bFirstEmpty = TRUE;
797 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
799 for (;;)
801 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
803 if (bFirstEmpty)
805 /* ShowWindow the first time the queue goes empty */
806 ShowWindow( hwnd, SW_SHOWNORMAL );
807 bFirstEmpty = FALSE;
809 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
811 /* No message present -> send ENTERIDLE and wait */
812 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
814 GetMessageW( &msg, 0, 0, 0 );
817 if (msg.message == WM_QUIT)
819 PostQuitMessage( msg.wParam );
820 if (!IsWindow( hwnd )) return 0;
821 break;
823 if (!IsWindow( hwnd )) return 0;
824 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
826 TranslateMessage( &msg );
827 DispatchMessageW( &msg );
829 if (!IsWindow( hwnd )) return 0;
830 if (dlgInfo->flags & DF_END) break;
832 if (bFirstEmpty && msg.message == WM_TIMER)
834 ShowWindow( hwnd, SW_SHOWNORMAL );
835 bFirstEmpty = FALSE;
839 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
840 retval = dlgInfo->idResult;
841 DestroyWindow( hwnd );
842 return retval;
846 /***********************************************************************
847 * DialogBoxParamA (USER32.@)
849 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
850 HWND owner, DLGPROC dlgProc, LPARAM param )
852 HWND hwnd;
853 HRSRC hrsrc;
854 LPCDLGTEMPLATEA ptr;
856 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
857 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
858 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
859 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
860 return 0;
864 /***********************************************************************
865 * DialogBoxParamW (USER32.@)
867 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
868 HWND owner, DLGPROC dlgProc, LPARAM param )
870 HWND hwnd;
871 HRSRC hrsrc;
872 LPCDLGTEMPLATEW ptr;
874 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
875 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
876 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
877 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
878 return 0;
882 /***********************************************************************
883 * DialogBoxIndirectParamAorW (USER32.@)
885 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
886 HWND owner, DLGPROC dlgProc,
887 LPARAM param, DWORD flags )
889 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
890 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
891 return -1;
894 /***********************************************************************
895 * DialogBoxIndirectParamA (USER32.@)
897 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
898 HWND owner, DLGPROC dlgProc, LPARAM param )
900 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
904 /***********************************************************************
905 * DialogBoxIndirectParamW (USER32.@)
907 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
908 HWND owner, DLGPROC dlgProc, LPARAM param )
910 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
913 /***********************************************************************
914 * EndDialog (USER32.@)
916 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
918 BOOL wasEnabled = TRUE;
919 DIALOGINFO * dlgInfo;
920 HWND owner;
922 TRACE("%p %ld\n", hwnd, retval );
924 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
926 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
927 return FALSE;
929 dlgInfo->idResult = retval;
930 dlgInfo->flags |= DF_END;
931 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
933 owner = GetWindow( hwnd, GW_OWNER );
934 if (wasEnabled && owner)
935 DIALOG_EnableOwner( owner );
937 /* Windows sets the focus to the dialog itself in EndDialog */
939 if (IsChild(hwnd, GetFocus()))
940 SetFocus( hwnd );
942 /* Don't have to send a ShowWindow(SW_HIDE), just do
943 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
945 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
946 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
948 if (hwnd == GetActiveWindow())
950 /* If this dialog was given an owner then set the focus to that owner
951 even when the owner is disabled (normally when a window closes any
952 disabled windows cannot receive the focus). */
953 if (owner)
954 SetForegroundWindow( owner );
955 else
956 WINPOS_ActivateOtherWindow( hwnd );
959 /* unblock dialog loop */
960 PostMessageA(hwnd, WM_NULL, 0, 0);
961 return TRUE;
965 /***********************************************************************
966 * DIALOG_IsAccelerator
968 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
970 HWND hwndControl = hwnd;
971 HWND hwndNext;
972 INT dlgCode;
973 WCHAR buffer[128];
977 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
978 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
980 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
981 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
982 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
984 /* find the accelerator key */
985 LPWSTR p = buffer - 2;
989 p = strchrW( p + 2, '&' );
991 while (p != NULL && p[1] == '&');
993 /* and check if it's the one we're looking for */
994 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
996 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
998 /* set focus to the control */
999 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
1000 /* and bump it on to next */
1001 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1003 else if (dlgCode & DLGC_BUTTON)
1005 /* send BM_CLICK message to the control */
1006 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
1008 return TRUE;
1011 hwndNext = GetWindow( hwndControl, GW_CHILD );
1013 else hwndNext = 0;
1015 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1017 while (!hwndNext && hwndControl)
1019 hwndControl = GetParent( hwndControl );
1020 if (hwndControl == hwndDlg)
1022 if(hwnd==hwndDlg) /* prevent endless loop */
1024 hwndNext=hwnd;
1025 break;
1027 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1029 else
1030 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1032 hwndControl = hwndNext;
1034 while (hwndControl && (hwndControl != hwnd));
1036 return FALSE;
1039 /***********************************************************************
1040 * DIALOG_FindMsgDestination
1042 * The messages that IsDialogMessage sends may not go to the dialog
1043 * calling IsDialogMessage if that dialog is a child, and it has the
1044 * DS_CONTROL style set.
1045 * We propagate up until we hit one that does not have DS_CONTROL, or
1046 * whose parent is not a dialog.
1048 * This is undocumented behaviour.
1050 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1052 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1054 WND *pParent;
1055 HWND hParent = GetParent(hwndDlg);
1056 if (!hParent) break;
1058 pParent = WIN_GetPtr(hParent);
1059 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1061 if (!pParent->dlgInfo)
1063 WIN_ReleasePtr(pParent);
1064 break;
1066 WIN_ReleasePtr(pParent);
1068 hwndDlg = hParent;
1071 return hwndDlg;
1074 /***********************************************************************
1075 * DIALOG_FixOneChildOnChangeFocus
1077 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1080 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1081 LPARAM lParam)
1083 /* If a default pushbutton then no longer default */
1084 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1085 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1086 return TRUE;
1089 /***********************************************************************
1090 * DIALOG_FixChildrenOnChangeFocus
1092 * Following the change of focus that occurs for example after handling
1093 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1094 * children may be required.
1096 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1098 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1099 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1100 /* Windows does ask for this. I don't know why yet */
1102 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1104 /* If the button that is getting the focus WAS flagged as the default
1105 * pushbutton then ask the dialog what it thinks the default is and
1106 * set that in the default style.
1108 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1110 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1111 if (HIWORD(def_id) == DC_HASDEFID)
1113 HWND hwndDef;
1114 def_id = LOWORD(def_id);
1115 hwndDef = GetDlgItem (hwndDlg, def_id);
1116 if (hwndDef)
1118 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1119 /* I know that if it is a button then it should already be a
1120 * UNDEFPUSHBUTTON, since we have just told the buttons to
1121 * change style. But maybe they ignored our request
1123 if ((dlgcode_def & DLGC_BUTTON) &&
1124 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1126 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1131 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1133 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1134 /* I wonder why it doesn't send a DM_SETDEFID */
1138 /***********************************************************************
1139 * DIALOG_IdToHwnd
1141 * A recursive version of GetDlgItem
1143 * RETURNS
1144 * The HWND for a Child ID.
1146 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1148 int i;
1149 HWND *list = WIN_ListChildren( hwndDlg );
1150 HWND ret = 0;
1152 if (!list) return 0;
1154 for (i = 0; list[i]; i++)
1156 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1158 ret = list[i];
1159 break;
1162 /* Recurse into every child */
1163 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1166 HeapFree( GetProcessHeap(), 0, list );
1167 return ret;
1170 /***********************************************************************
1171 * IsDialogMessageW (USER32.@)
1173 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1175 INT dlgCode = 0;
1177 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1179 hwndDlg = WIN_GetFullHandle( hwndDlg );
1180 if (is_desktop_window(hwndDlg)) return FALSE;
1181 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1183 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1185 switch(msg->message)
1187 case WM_KEYDOWN:
1188 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1189 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1191 switch(msg->wParam)
1193 case VK_TAB:
1194 if (!(dlgCode & DLGC_WANTTAB))
1196 BOOL fIsDialog = TRUE;
1197 WND *pWnd = WIN_GetPtr( hwndDlg );
1199 if (pWnd && pWnd != WND_OTHER_PROCESS)
1201 fIsDialog = (pWnd->dlgInfo != NULL);
1202 WIN_ReleasePtr(pWnd);
1205 /* I am not sure under which circumstances the TAB is handled
1206 * each way. All I do know is that it does not always simply
1207 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1208 * do so but I presume someone has)
1210 if (fIsDialog)
1211 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1212 else
1214 /* It would appear that GetNextDlgTabItem can handle being
1215 * passed hwndDlg rather than NULL but that is undocumented
1216 * so let's do it properly
1218 HWND hwndFocus = GetFocus();
1219 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1220 hwndFocus == hwndDlg ? NULL : hwndFocus,
1221 GetKeyState (VK_SHIFT) & 0x8000);
1222 if (hwndNext)
1224 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1225 msg->wParam, (LPARAM)msg);
1226 if (dlgCode & DLGC_HASSETSEL)
1228 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1229 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1230 if (buffer)
1232 INT length;
1233 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1234 length = strlenW (buffer);
1235 HeapFree (GetProcessHeap(), 0, buffer);
1236 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1239 SetFocus (hwndNext);
1240 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1242 else
1243 return FALSE;
1245 return TRUE;
1247 break;
1249 case VK_RIGHT:
1250 case VK_DOWN:
1251 case VK_LEFT:
1252 case VK_UP:
1253 if (!(dlgCode & DLGC_WANTARROWS))
1255 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1256 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1257 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1258 return TRUE;
1260 break;
1262 case VK_CANCEL:
1263 case VK_ESCAPE:
1264 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1265 return TRUE;
1267 case VK_EXECUTE:
1268 case VK_RETURN:
1270 DWORD dw;
1271 if ((GetFocus() == msg->hwnd) &&
1272 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1274 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1276 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1278 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1279 if (hwndDef ? IsWindowEnabled(hwndDef) : LOWORD(dw)==IDOK)
1280 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1282 else
1284 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1288 return TRUE;
1290 break;
1292 case WM_CHAR:
1293 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1294 * It does NOT get sent in the test program I have
1296 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1297 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1298 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1299 /* drop through */
1301 case WM_SYSCHAR:
1302 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1304 /* don't translate or dispatch */
1305 return TRUE;
1307 break;
1310 TranslateMessage( msg );
1311 DispatchMessageW( msg );
1312 return TRUE;
1316 /***********************************************************************
1317 * GetDlgCtrlID (USER32.@)
1319 INT WINAPI GetDlgCtrlID( HWND hwnd )
1321 return GetWindowLongPtrW( hwnd, GWLP_ID );
1325 /***********************************************************************
1326 * GetDlgItem (USER32.@)
1328 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1330 int i;
1331 HWND *list = WIN_ListChildren( hwndDlg );
1332 HWND ret = 0;
1334 if (!list) return 0;
1336 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1337 ret = list[i];
1338 HeapFree( GetProcessHeap(), 0, list );
1339 return ret;
1343 /*******************************************************************
1344 * SendDlgItemMessageA (USER32.@)
1346 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1347 WPARAM wParam, LPARAM lParam )
1349 HWND hwndCtrl = GetDlgItem( hwnd, id );
1350 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1351 else return 0;
1355 /*******************************************************************
1356 * SendDlgItemMessageW (USER32.@)
1358 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1359 WPARAM wParam, LPARAM lParam )
1361 HWND hwndCtrl = GetDlgItem( hwnd, id );
1362 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1363 else return 0;
1367 /*******************************************************************
1368 * SetDlgItemTextA (USER32.@)
1370 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1372 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1376 /*******************************************************************
1377 * SetDlgItemTextW (USER32.@)
1379 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1381 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1385 /***********************************************************************
1386 * GetDlgItemTextA (USER32.@)
1388 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1390 if (str && (len > 0)) str[0] = '\0';
1391 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1392 len, (LPARAM)str );
1396 /***********************************************************************
1397 * GetDlgItemTextW (USER32.@)
1399 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1401 if (str && (len > 0)) str[0] = '\0';
1402 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1403 len, (LPARAM)str );
1407 /*******************************************************************
1408 * SetDlgItemInt (USER32.@)
1410 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1411 BOOL fSigned )
1413 char str[20];
1415 if (fSigned) sprintf( str, "%d", (INT)value );
1416 else sprintf( str, "%u", value );
1417 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1418 return TRUE;
1422 /***********************************************************************
1423 * GetDlgItemInt (USER32.@)
1425 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1426 BOOL fSigned )
1428 char str[30];
1429 char * endptr;
1430 LONG_PTR result = 0;
1432 if (translated) *translated = FALSE;
1433 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1434 return 0;
1435 if (fSigned)
1437 result = strtol( str, &endptr, 10 );
1438 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1439 return 0;
1440 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1441 return 0;
1443 else
1445 result = strtoul( str, &endptr, 10 );
1446 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1447 return 0;
1448 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1450 if (translated) *translated = TRUE;
1451 return (UINT)result;
1455 /***********************************************************************
1456 * CheckDlgButton (USER32.@)
1458 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1460 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1461 return TRUE;
1465 /***********************************************************************
1466 * IsDlgButtonChecked (USER32.@)
1468 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1470 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1474 /***********************************************************************
1475 * CheckRB
1477 * Callback function used to check/uncheck radio buttons that fall
1478 * within a specific range of IDs.
1480 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1482 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1483 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1485 if ((lChildID >= lpRadioGroup->firstID) &&
1486 (lChildID <= lpRadioGroup->lastID))
1488 if (lChildID == lpRadioGroup->checkID)
1490 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1492 else
1494 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1498 return TRUE;
1502 /***********************************************************************
1503 * CheckRadioButton (USER32.@)
1505 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1506 int lastID, int checkID )
1508 RADIOGROUP radioGroup;
1510 radioGroup.firstID = firstID;
1511 radioGroup.lastID = lastID;
1512 radioGroup.checkID = checkID;
1514 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1515 (LPARAM)&radioGroup);
1519 /***********************************************************************
1520 * GetDialogBaseUnits (USER.243)
1521 * GetDialogBaseUnits (USER32.@)
1523 DWORD WINAPI GetDialogBaseUnits(void)
1525 static DWORD units;
1527 if (!units)
1529 HDC hdc;
1530 SIZE size;
1532 if ((hdc = GetDC(0)))
1534 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1535 if (size.cx) units = MAKELONG( size.cx, size.cy );
1536 ReleaseDC( 0, hdc );
1538 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1540 return units;
1544 /***********************************************************************
1545 * MapDialogRect (USER32.@)
1547 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1549 DIALOGINFO * dlgInfo;
1550 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1551 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1552 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1553 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1554 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1555 return TRUE;
1559 /***********************************************************************
1560 * GetNextDlgGroupItem (USER32.@)
1562 * Corrections to MSDN documentation
1564 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1565 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1566 * 2. Prev of NULL or hwndDlg fails
1568 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1570 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1571 BOOL fLooped=FALSE;
1572 BOOL fSkipping=FALSE;
1574 hwndDlg = WIN_GetFullHandle( hwndDlg );
1575 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1577 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1578 if (!hwndCtrl && fPrevious) return 0;
1580 if (hwndCtrl)
1582 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1584 else
1586 /* No ctrl specified -> start from the beginning */
1587 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1588 /* MSDN is wrong. fPrevious does not result in the last child */
1590 /* Maybe that first one is valid. If so then we don't want to skip it*/
1591 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1593 return hwndCtrl;
1597 /* Always go forward around the group and list of controls; for the
1598 * previous control keep track; for the next break when you find one
1600 retvalue = hwndCtrl;
1601 hwnd = hwndCtrl;
1602 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1605 while (!hwndNext)
1607 /* Climb out until there is a next sibling of the ancestor or we
1608 * reach the top (in which case we loop back to the start)
1610 if (hwndDlg == GetParent (hwnd))
1612 /* Wrap around to the beginning of the list, within the same
1613 * group. (Once only)
1615 if (fLooped) goto end;
1616 fLooped = TRUE;
1617 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1619 else
1621 hwnd = GetParent (hwnd);
1622 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1625 hwnd = hwndNext;
1627 /* Wander down the leading edge of controlparents */
1628 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1629 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1630 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1631 hwnd = hwndNext;
1632 /* Question. If the control is a control parent but either has no
1633 * children or is not visible/enabled then if it has a WS_GROUP does
1634 * it count? For that matter does it count anyway?
1635 * I believe it doesn't count.
1638 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1640 hwndLastGroup = hwnd;
1641 if (!fSkipping)
1643 /* Look for the beginning of the group */
1644 fSkipping = TRUE;
1648 if (hwnd == hwndCtrl)
1650 if (!fSkipping) break;
1651 if (hwndLastGroup == hwnd) break;
1652 hwnd = hwndLastGroup;
1653 fSkipping = FALSE;
1654 fLooped = FALSE;
1657 if (!fSkipping &&
1658 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1659 WS_VISIBLE)
1661 retvalue = hwnd;
1662 if (!fPrevious) break;
1665 end:
1666 return retvalue;
1670 /***********************************************************************
1671 * DIALOG_GetNextTabItem
1673 * Recursive helper for GetNextDlgTabItem
1675 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1677 LONG dsStyle;
1678 LONG exStyle;
1679 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1680 HWND retWnd = 0;
1681 HWND hChildFirst = 0;
1683 if(!hwndCtrl)
1685 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1686 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1688 else if (IsChild( hwndMain, hwndCtrl ))
1690 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1691 if(!hChildFirst)
1693 if(GetParent(hwndCtrl) != hwndMain)
1694 /* i.e. if we are not at the top level of the recursion */
1695 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1696 else
1697 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1701 while(hChildFirst)
1703 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1704 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1705 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1707 HWND retWnd;
1708 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1709 if (retWnd) return (retWnd);
1711 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1713 return (hChildFirst);
1715 hChildFirst = GetWindow(hChildFirst,wndSearch);
1717 if(hwndCtrl)
1719 HWND hParent = GetParent(hwndCtrl);
1720 while(hParent)
1722 if(hParent == hwndMain) break;
1723 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1724 if(retWnd) break;
1725 hParent = GetParent(hParent);
1727 if(!retWnd)
1728 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1730 return retWnd ? retWnd : hwndCtrl;
1733 /***********************************************************************
1734 * GetNextDlgTabItem (USER32.@)
1736 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1737 BOOL fPrevious )
1739 hwndDlg = WIN_GetFullHandle( hwndDlg );
1740 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1742 /* Undocumented but tested under Win2000 and WinME */
1743 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1745 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1746 * NB GetLastError returns whatever was set before the function was
1747 * called.
1749 if (!hwndCtrl && fPrevious) return 0;
1751 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1754 /**********************************************************************
1755 * DIALOG_DlgDirSelect
1757 * Helper function for DlgDirSelect*
1759 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1760 INT id, BOOL unicode, BOOL combo )
1762 WCHAR *buffer, *ptr;
1763 INT item, size;
1764 BOOL ret;
1765 HWND listbox = GetDlgItem( hwnd, id );
1767 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1768 if (!listbox) return FALSE;
1770 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1771 if (item == LB_ERR) return FALSE;
1773 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1774 if (size == LB_ERR) return FALSE;
1776 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1778 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1780 if ((ret = (buffer[0] == '['))) /* drive or directory */
1782 if (buffer[1] == '-') /* drive */
1784 buffer[3] = ':';
1785 buffer[4] = 0;
1786 ptr = buffer + 2;
1788 else
1790 buffer[strlenW(buffer)-1] = '\\';
1791 ptr = buffer + 1;
1794 else
1796 /* Filenames without a dot extension must have one tacked at the end */
1797 if (strchrW(buffer, '.') == NULL)
1799 buffer[strlenW(buffer)+1] = '\0';
1800 buffer[strlenW(buffer)] = '.';
1802 ptr = buffer;
1805 if (!unicode)
1807 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1808 ((LPSTR)str)[len-1] = 0;
1810 else lstrcpynW( str, ptr, len );
1811 HeapFree( GetProcessHeap(), 0, buffer );
1812 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1813 return ret;
1817 /**********************************************************************
1818 * DIALOG_DlgDirListW
1820 * Helper function for DlgDirList*W
1822 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1823 INT idStatic, UINT attrib, BOOL combo )
1825 HWND hwnd;
1826 LPWSTR orig_spec = spec;
1827 WCHAR any[] = {'*','.','*',0};
1829 #define SENDMSG(msg,wparam,lparam) \
1830 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1831 : SendMessageW( hwnd, msg, wparam, lparam ))
1833 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1835 /* If the path exists and is a directory, chdir to it */
1836 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1837 else
1839 WCHAR *p, *p2;
1840 p = spec;
1841 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1842 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1843 if ((p2 = strrchrW( p, '/' ))) p = p2;
1844 if (p != spec)
1846 WCHAR sep = *p;
1847 *p = 0;
1848 if (!SetCurrentDirectoryW( spec ))
1850 *p = sep; /* Restore the original spec */
1851 return FALSE;
1853 spec = p + 1;
1857 TRACE( "mask=%s\n", debugstr_w(spec) );
1859 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1861 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1863 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1864 if (attrib & DDL_DIRECTORY)
1866 if (!(attrib & DDL_EXCLUSIVE))
1868 SENDMSG( combo ? CB_DIR : LB_DIR,
1869 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1870 (LPARAM)spec );
1872 SENDMSG( combo ? CB_DIR : LB_DIR,
1873 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1874 (LPARAM)any );
1876 else
1878 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1882 /* Convert path specification to uppercase */
1883 if (spec) CharUpperW(spec);
1885 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1887 WCHAR temp[MAX_PATH];
1888 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1889 CharLowerW( temp );
1890 /* Can't use PostMessage() here, because the string is on the stack */
1891 SetDlgItemTextW( hDlg, idStatic, temp );
1894 if (orig_spec && (spec != orig_spec))
1896 /* Update the original file spec */
1897 WCHAR *p = spec;
1898 while ((*orig_spec++ = *p++));
1901 return TRUE;
1902 #undef SENDMSG
1906 /**********************************************************************
1907 * DIALOG_DlgDirListA
1909 * Helper function for DlgDirList*A
1911 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1912 INT idStatic, UINT attrib, BOOL combo )
1914 if (spec)
1916 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1917 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1918 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1919 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1920 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1921 HeapFree( GetProcessHeap(), 0, specW );
1922 return ret;
1924 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1928 /**********************************************************************
1929 * DlgDirSelectExA (USER32.@)
1931 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1933 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1937 /**********************************************************************
1938 * DlgDirSelectExW (USER32.@)
1940 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1942 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1946 /**********************************************************************
1947 * DlgDirSelectComboBoxExA (USER32.@)
1949 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1950 INT id )
1952 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1956 /**********************************************************************
1957 * DlgDirSelectComboBoxExW (USER32.@)
1959 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1960 INT id)
1962 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1966 /**********************************************************************
1967 * DlgDirListA (USER32.@)
1969 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1970 INT idStatic, UINT attrib )
1972 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1976 /**********************************************************************
1977 * DlgDirListW (USER32.@)
1979 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1980 INT idStatic, UINT attrib )
1982 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1986 /**********************************************************************
1987 * DlgDirListComboBoxA (USER32.@)
1989 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1990 INT idStatic, UINT attrib )
1992 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1996 /**********************************************************************
1997 * DlgDirListComboBoxW (USER32.@)
1999 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2000 INT idStatic, UINT attrib )
2002 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );