gdi32/tests: Mark tests failing randomly on Windows as flaky.
[wine.git] / dlls / user32 / dialog.c
blob68c0d804ca75cadd46b68de3f2ab647c77cff54c
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 <stdlib.h>
22 #include <limits.h>
23 #include <errno.h>
24 #include "user_private.h"
25 #include "controls.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
31 #define DIALOG_CLASS_ATOM MAKEINTATOM(32770) /* Dialog */
33 /* Dialog control information */
34 typedef struct
36 DWORD style;
37 DWORD exStyle;
38 DWORD helpId;
39 INT16 x;
40 INT16 y;
41 INT16 cx;
42 INT16 cy;
43 UINT_PTR id;
44 LPCWSTR className;
45 LPCWSTR windowName;
46 LPCVOID data;
47 } DLG_CONTROL_INFO;
49 /* Dialog template */
50 typedef struct
52 DWORD style;
53 DWORD exStyle;
54 DWORD helpId;
55 UINT16 nbItems;
56 INT16 x;
57 INT16 y;
58 INT16 cx;
59 INT16 cy;
60 LPCWSTR menuName;
61 LPCWSTR className;
62 LPCWSTR caption;
63 INT16 pointSize;
64 WORD weight;
65 BOOL italic;
66 LPCWSTR faceName;
67 BOOL dialogEx;
68 } DLG_TEMPLATE;
70 /* Radio button group */
71 typedef struct
73 UINT firstID;
74 UINT lastID;
75 UINT checkID;
76 } RADIOGROUP;
79 /***********************************************************************
80 * DIALOG_GetControl32
82 * Return the class and text of the control pointed to by ptr,
83 * fill the header structure and return a pointer to the next control.
85 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
86 BOOL dialogEx )
88 if (dialogEx)
90 info->helpId = GET_DWORD(p); p += 2;
91 info->exStyle = GET_DWORD(p); p += 2;
92 info->style = GET_DWORD(p); p += 2;
94 else
96 info->helpId = 0;
97 info->style = GET_DWORD(p); p += 2;
98 info->exStyle = GET_DWORD(p); p += 2;
100 info->x = GET_WORD(p); p++;
101 info->y = GET_WORD(p); p++;
102 info->cx = GET_WORD(p); p++;
103 info->cy = GET_WORD(p); p++;
105 if (dialogEx)
107 /* id is 4 bytes for DIALOGEX */
108 info->id = GET_LONG(p);
109 p += 2;
111 else
113 info->id = GET_WORD(p);
114 p++;
117 if (GET_WORD(p) == 0xffff)
119 static const WCHAR *class_names[6] =
121 L"Button", /* 0x80 */
122 L"Edit", /* 0x81 */
123 L"Static", /* 0x82 */
124 L"ListBox", /* 0x83 */
125 L"ScrollBar", /* 0x84 */
126 L"ComboBox" /* 0x85 */
128 WORD id = GET_WORD(p+1);
129 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
130 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
131 if (id <= 5)
132 info->className = class_names[id];
133 else
135 info->className = NULL;
136 ERR("Unknown built-in class id %04x\n", id );
138 p += 2;
140 else
142 info->className = p;
143 p += lstrlenW( info->className ) + 1;
146 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
148 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
149 p += 2;
151 else
153 info->windowName = p;
154 p += lstrlenW( info->windowName ) + 1;
157 TRACE(" %s %s %Id, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
158 debugstr_w( info->className ), debugstr_w( info->windowName ),
159 info->id, info->x, info->y, info->cx, info->cy,
160 info->style, info->exStyle, info->helpId );
162 if (GET_WORD(p))
164 if (TRACE_ON(dialog))
166 WORD i, count = GET_WORD(p) / sizeof(WORD);
167 TRACE(" BEGIN\n");
168 TRACE(" ");
169 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
170 TRACE("\n");
171 TRACE(" END\n" );
173 info->data = p;
174 p += GET_WORD(p) / sizeof(WORD);
176 else info->data = NULL;
177 p++;
179 /* Next control is on dword boundary */
180 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
184 /***********************************************************************
185 * DIALOG_CreateControls32
187 * Create the control windows for a dialog.
189 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
190 HINSTANCE hInst, BOOL unicode )
192 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
193 DLG_CONTROL_INFO info;
194 HWND hwndCtrl, hwndDefButton = 0;
195 INT items = dlgTemplate->nbItems;
197 TRACE(" BEGIN\n" );
198 while (items--)
200 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
201 dlgTemplate->dialogEx );
202 info.style &= ~WS_POPUP;
203 info.style |= WS_CHILD;
205 if (info.style & WS_BORDER)
207 info.style &= ~WS_BORDER;
208 info.exStyle |= WS_EX_CLIENTEDGE;
210 if (unicode)
212 const WCHAR *caption = info.windowName;
213 WCHAR caption_buf[3];
215 if (IS_INTRESOURCE(caption) && caption)
217 caption_buf[0] = 0xffff;
218 caption_buf[1] = PtrToUlong( caption );
219 caption_buf[2] = 0;
220 caption = caption_buf;
223 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
224 info.className, caption,
225 info.style | WS_CHILD,
226 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
227 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
228 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
229 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
230 hwnd, (HMENU)info.id,
231 hInst, (LPVOID)info.data );
233 else
235 LPCSTR class = (LPCSTR)info.className;
236 LPCSTR caption = (LPCSTR)info.windowName;
237 LPSTR class_tmp = NULL;
238 LPSTR caption_tmp = NULL;
239 char caption_buf[4];
241 if (!IS_INTRESOURCE(class))
243 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
244 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
245 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
246 class = class_tmp;
248 if (!IS_INTRESOURCE(caption))
250 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
251 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
252 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
253 caption = caption_tmp;
255 else if (caption)
257 caption_buf[0] = 0xff;
258 caption_buf[1] = PtrToUlong( caption );
259 caption_buf[2] = PtrToUlong( caption ) >> 8;
260 caption_buf[3] = 0;
261 caption = caption_buf;
264 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
265 class, caption, info.style | WS_CHILD,
266 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
267 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
268 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
269 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
270 hwnd, (HMENU)info.id,
271 hInst, (LPVOID)info.data );
272 HeapFree( GetProcessHeap(), 0, class_tmp );
273 HeapFree( GetProcessHeap(), 0, caption_tmp );
275 if (!hwndCtrl)
277 WARN("control %s %s creation failed\n", debugstr_w(info.className),
278 debugstr_w(info.windowName));
279 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
280 SetLastError(0);
281 return FALSE;
284 /* Send initialisation messages to the control */
285 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
286 (WPARAM)dlgInfo->hUserFont, 0 );
287 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
289 /* If there's already a default push-button, set it back */
290 /* to normal and use this one instead. */
291 if (hwndDefButton)
292 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
293 hwndDefButton = hwndCtrl;
294 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
297 TRACE(" END\n" );
298 return TRUE;
302 /***********************************************************************
303 * DIALOG_ParseTemplate32
305 * Fill a DLG_TEMPLATE structure from the dialog template, and return
306 * a pointer to the first control.
308 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
310 const WORD *p = (const WORD *)template;
311 WORD signature;
312 WORD dlgver;
314 dlgver = GET_WORD(p); p++;
315 signature = GET_WORD(p); p++;
317 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
319 result->dialogEx = TRUE;
320 result->helpId = GET_DWORD(p); p += 2;
321 result->exStyle = GET_DWORD(p); p += 2;
322 result->style = GET_DWORD(p); p += 2;
324 else
326 result->style = GET_DWORD(p - 2);
327 result->dialogEx = FALSE;
328 result->helpId = 0;
329 result->exStyle = GET_DWORD(p); p += 2;
331 result->nbItems = GET_WORD(p); p++;
332 result->x = GET_WORD(p); p++;
333 result->y = GET_WORD(p); p++;
334 result->cx = GET_WORD(p); p++;
335 result->cy = GET_WORD(p); p++;
336 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
337 result->dialogEx ? "EX" : "", result->x, result->y,
338 result->cx, result->cy, result->helpId );
339 TRACE(" STYLE 0x%08lx\n", result->style );
340 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
342 /* Get the menu name */
344 switch(GET_WORD(p))
346 case 0x0000:
347 result->menuName = NULL;
348 p++;
349 break;
350 case 0xffff:
351 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
352 p += 2;
353 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
354 break;
355 default:
356 result->menuName = p;
357 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
358 p += lstrlenW( result->menuName ) + 1;
359 break;
362 /* Get the class name */
364 switch(GET_WORD(p))
366 case 0x0000:
367 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
368 p++;
369 break;
370 case 0xffff:
371 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
372 p += 2;
373 TRACE(" CLASS %04x\n", LOWORD(result->className) );
374 break;
375 default:
376 result->className = p;
377 TRACE(" CLASS %s\n", debugstr_w( result->className ));
378 p += lstrlenW( result->className ) + 1;
379 break;
382 /* Get the window caption */
384 result->caption = p;
385 p += lstrlenW( result->caption ) + 1;
386 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
388 /* Get the font name */
390 result->pointSize = 0;
391 result->faceName = NULL;
392 result->weight = FW_DONTCARE;
393 result->italic = FALSE;
395 if (result->style & DS_SETFONT)
397 result->pointSize = GET_WORD(p);
398 p++;
400 /* If pointSize is 0x7fff, it means that we need to use the font
401 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
402 * italic, and facename from the dialog template.
404 if (result->pointSize == 0x7fff)
406 /* We could call SystemParametersInfo here, but then we'd have
407 * to convert from pixel size to point size (which can be
408 * imprecise).
410 TRACE(" FONT: Using message box font\n");
412 else
414 if (result->dialogEx)
416 result->weight = GET_WORD(p); p++;
417 result->italic = LOBYTE(GET_WORD(p)); p++;
419 result->faceName = p;
420 p += lstrlenW( result->faceName ) + 1;
422 TRACE(" FONT %d, %s, %d, %s\n",
423 result->pointSize, debugstr_w( result->faceName ),
424 result->weight, result->italic ? "TRUE" : "FALSE" );
428 /* First control is on dword boundary */
429 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
433 /***********************************************************************
434 * DIALOG_CreateIndirect
435 * Creates a dialog box window
437 * modal = TRUE if we are called from a modal dialog box.
438 * (it's more compatible to do it here, as under Windows the owner
439 * is never disabled if the dialog fails because of an invalid template)
441 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
442 HWND owner, DLGPROC dlgProc, LPARAM param,
443 BOOL unicode, HWND *modal_owner )
445 HWND hwnd;
446 RECT rect;
447 POINT pos;
448 SIZE size;
449 DLG_TEMPLATE template;
450 DIALOGINFO * dlgInfo;
451 DWORD units = GetDialogBaseUnits();
452 HWND disabled_owner = NULL;
453 HMENU hMenu = 0;
454 HFONT hUserFont = 0;
455 UINT flags = 0;
456 UINT xBaseUnit = LOWORD(units);
457 UINT yBaseUnit = HIWORD(units);
459 /* Parse dialog template */
461 if (!dlgTemplate) return 0;
462 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
464 /* Load menu */
466 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
468 /* Create custom font if needed */
470 if (template.style & DS_SETFONT)
472 HDC dc = GetDC(0);
474 if (template.pointSize == 0x7fff)
476 /* We get the message font from the non-client metrics */
477 NONCLIENTMETRICSW ncMetrics;
479 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
480 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
481 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
483 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
486 else
488 /* We convert the size to pixels and then make it -ve. This works
489 * for both +ve and -ve template.pointSize */
490 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
491 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
492 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
493 PROOF_QUALITY, FF_DONTCARE,
494 template.faceName );
497 if (hUserFont)
499 SIZE charSize;
500 HFONT hOldFont = SelectObject( dc, hUserFont );
501 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
502 if (charSize.cx)
504 xBaseUnit = charSize.cx;
505 yBaseUnit = charSize.cy;
507 SelectObject( dc, hOldFont );
509 NtUserReleaseDC( 0, dc );
510 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
513 /* Create dialog main window */
515 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
517 if (template.style & DS_CONTROL)
518 template.style &= ~(WS_CAPTION|WS_SYSMENU);
519 template.style |= DS_3DLOOK;
520 if (template.style & DS_MODALFRAME)
521 template.exStyle |= WS_EX_DLGMODALFRAME;
522 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
523 template.exStyle |= WS_EX_CONTROLPARENT;
524 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
525 pos.x = rect.left;
526 pos.y = rect.top;
527 size.cx = rect.right - rect.left;
528 size.cy = rect.bottom - rect.top;
530 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
532 pos.x = pos.y = CW_USEDEFAULT;
534 else
536 HMONITOR monitor = 0;
537 MONITORINFO mon_info;
539 mon_info.cbSize = sizeof(mon_info);
540 if (template.style & DS_CENTER)
542 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
543 GetMonitorInfoW( monitor, &mon_info );
544 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
545 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
547 else if (template.style & DS_CENTERMOUSE)
549 GetCursorPos( &pos );
550 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
551 GetMonitorInfoW( monitor, &mon_info );
553 else
555 pos.x += MulDiv(template.x, xBaseUnit, 4);
556 pos.y += MulDiv(template.y, yBaseUnit, 8);
557 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
559 if ( !(template.style & WS_CHILD) )
561 INT dX, dY;
563 /* try to fit it into the desktop */
565 if (!monitor)
567 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
568 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
569 GetMonitorInfoW( monitor, &mon_info );
571 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
572 pos.x -= dX;
573 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
574 pos.y -= dY;
575 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
576 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
580 if (modal_owner && owner)
582 HWND parent;
584 * Owner needs to be top level window. We need to duplicate the logic from server,
585 * because we need to disable it before creating dialog window. Note that we do that
586 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
587 * Windows does.
589 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
591 parent = GetParent( owner );
592 if (!parent || parent == GetDesktopWindow()) break;
593 owner = parent;
595 *modal_owner = owner;
596 if (IsWindowEnabled( owner ))
598 disabled_owner = owner;
599 EnableWindow( disabled_owner, FALSE );
603 if (unicode)
605 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
606 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
607 owner, hMenu, hInst, NULL );
609 else
611 LPCSTR class = (LPCSTR)template.className;
612 LPCSTR caption = (LPCSTR)template.caption;
613 LPSTR class_tmp = NULL;
614 LPSTR caption_tmp = NULL;
616 if (!IS_INTRESOURCE(class))
618 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
619 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
620 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
621 class = class_tmp;
623 if (!IS_INTRESOURCE(caption))
625 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
626 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
627 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
628 caption = caption_tmp;
630 hwnd = CreateWindowExA(template.exStyle, class, caption,
631 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
632 owner, hMenu, hInst, NULL );
633 HeapFree( GetProcessHeap(), 0, class_tmp );
634 HeapFree( GetProcessHeap(), 0, caption_tmp );
637 if (!hwnd)
639 if (hUserFont) DeleteObject( hUserFont );
640 if (hMenu) NtUserDestroyMenu( hMenu );
641 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
642 return 0;
645 /* moved this from the top of the method to here as DIALOGINFO structure
646 will be valid only after WM_CREATE message has been handled in DefDlgProc
647 All the members of the structure get filled here using temp variables */
648 dlgInfo = DIALOG_get_info( hwnd, TRUE );
649 dlgInfo->hwndFocus = 0;
650 dlgInfo->hUserFont = hUserFont;
651 dlgInfo->hMenu = hMenu;
652 dlgInfo->xBaseUnit = xBaseUnit;
653 dlgInfo->yBaseUnit = yBaseUnit;
654 dlgInfo->flags = flags;
656 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
658 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
659 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
661 if (dlgProc && dlgInfo->hUserFont)
662 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
664 /* Create controls */
666 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
668 HWND capture;
670 /* Send initialisation messages and set focus */
672 if (dlgProc)
674 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
675 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
676 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
677 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
679 /* By returning TRUE, app has requested a default focus assignment.
680 * WM_INITDIALOG may have changed the tab order, so find the first
681 * tabstop control again. */
682 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
683 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
684 if (focus)
686 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
687 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
688 NtUserSetFocus( focus );
690 else
692 if (!(template.style & WS_CHILD))
693 NtUserSetFocus( hwnd );
698 if (modal_owner && (capture = GetCapture()))
699 SendMessageW( capture, WM_CANCELMODE, 0, 0 );
701 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
703 NtUserShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
705 return hwnd;
707 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
708 if (IsWindow(hwnd)) NtUserDestroyWindow( hwnd );
709 return 0;
713 /***********************************************************************
714 * CreateDialogParamA (USER32.@)
716 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
717 DLGPROC dlgProc, LPARAM param )
719 HRSRC hrsrc;
720 LPCDLGTEMPLATEA ptr;
722 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
723 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
724 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
728 /***********************************************************************
729 * CreateDialogParamW (USER32.@)
731 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
732 DLGPROC dlgProc, LPARAM param )
734 HRSRC hrsrc;
735 LPCDLGTEMPLATEA ptr;
737 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
738 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
739 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
743 /***********************************************************************
744 * CreateDialogIndirectParamAorW (USER32.@)
746 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
747 HWND owner, DLGPROC dlgProc, LPARAM param,
748 DWORD flags )
750 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
753 /***********************************************************************
754 * CreateDialogIndirectParamA (USER32.@)
756 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
757 HWND owner, DLGPROC dlgProc, LPARAM param )
759 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
762 /***********************************************************************
763 * CreateDialogIndirectParamW (USER32.@)
765 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
766 HWND owner, DLGPROC dlgProc, LPARAM param )
768 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
772 /***********************************************************************
773 * DIALOG_DoDialogBox
775 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
777 DIALOGINFO * dlgInfo;
778 MSG msg;
779 INT retval;
780 BOOL bFirstEmpty;
782 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
784 bFirstEmpty = TRUE;
785 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
787 for (;;)
789 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
791 if (bFirstEmpty)
793 /* ShowWindow the first time the queue goes empty */
794 NtUserShowWindow( hwnd, SW_SHOWNORMAL );
795 bFirstEmpty = FALSE;
797 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
799 /* No message present -> send ENTERIDLE and wait */
800 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
802 GetMessageW( &msg, 0, 0, 0 );
805 if (msg.message == WM_QUIT)
807 PostQuitMessage( msg.wParam );
808 if (!IsWindow( hwnd )) return 0;
809 break;
811 if (!IsWindow( hwnd )) return 0;
812 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
814 TranslateMessage( &msg );
815 DispatchMessageW( &msg );
817 if (!IsWindow( hwnd )) return 0;
818 if (dlgInfo->flags & DF_END) break;
820 if (bFirstEmpty && msg.message == WM_TIMER)
822 NtUserShowWindow( hwnd, SW_SHOWNORMAL );
823 bFirstEmpty = FALSE;
827 retval = dlgInfo->idResult;
828 NtUserDestroyWindow( hwnd );
829 return retval;
833 /***********************************************************************
834 * DialogBoxParamA (USER32.@)
836 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
837 HWND owner, DLGPROC dlgProc, LPARAM param )
839 HWND hwnd;
840 HRSRC hrsrc;
841 LPCDLGTEMPLATEA ptr;
843 if (owner && !IsWindow(owner)) return 0;
845 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
846 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
847 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner ))) return -1;
848 return DIALOG_DoDialogBox( hwnd, owner );
852 /***********************************************************************
853 * DialogBoxParamW (USER32.@)
855 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
856 HWND owner, DLGPROC dlgProc, LPARAM param )
858 HWND hwnd;
859 HRSRC hrsrc;
860 LPCDLGTEMPLATEW ptr;
862 if (owner && !IsWindow(owner)) return 0;
864 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
865 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
866 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner ))) return -1;
867 return DIALOG_DoDialogBox( hwnd, owner );
871 /***********************************************************************
872 * DialogBoxIndirectParamAorW (USER32.@)
874 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
875 HWND owner, DLGPROC dlgProc,
876 LPARAM param, DWORD flags )
878 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
879 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
880 return -1;
883 /***********************************************************************
884 * DialogBoxIndirectParamA (USER32.@)
886 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
887 HWND owner, DLGPROC dlgProc, LPARAM param )
889 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
893 /***********************************************************************
894 * DialogBoxIndirectParamW (USER32.@)
896 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
897 HWND owner, DLGPROC dlgProc, LPARAM param )
899 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
902 /***********************************************************************
903 * EndDialog (USER32.@)
905 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
907 DIALOGINFO * dlgInfo;
908 HWND owner;
910 TRACE("%p %Id\n", hwnd, retval );
912 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
914 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
915 return FALSE;
917 dlgInfo->idResult = retval;
918 dlgInfo->flags |= DF_END;
920 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
921 if (owner)
922 EnableWindow( owner, TRUE );
924 /* Windows sets the focus to the dialog itself in EndDialog */
926 if (IsChild(hwnd, GetFocus()))
927 NtUserSetFocus( hwnd );
929 /* Don't have to send a ShowWindow(SW_HIDE), just do
930 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
932 NtUserSetWindowPos( hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
933 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
935 if (hwnd == GetActiveWindow())
937 /* If this dialog was given an owner then set the focus to that owner. */
938 if (owner)
939 SetForegroundWindow( owner );
940 else
941 NtUserActivateOtherWindow( hwnd );
944 /* unblock dialog loop */
945 PostMessageA(hwnd, WM_NULL, 0, 0);
946 return TRUE;
950 /***********************************************************************
951 * DIALOG_IsAccelerator
953 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
955 HWND hwndControl = hwnd;
956 HWND hwndNext;
957 INT dlgCode;
958 WCHAR buffer[128];
962 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
963 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
965 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
966 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
967 GetWindowTextW( hwndControl, buffer, ARRAY_SIZE( buffer )))
969 /* find the accelerator key */
970 LPWSTR p = buffer - 2;
974 p = wcschr( p + 2, '&' );
976 while (p != NULL && p[1] == '&');
978 /* and check if it's the one we're looking for */
979 if (p != NULL && towupper( p[1] ) == towupper( wParam ) )
981 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
983 /* set focus to the control */
984 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
985 /* and bump it on to next */
986 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
988 else if (dlgCode & DLGC_BUTTON)
990 /* send BM_CLICK message to the control */
991 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
993 return TRUE;
996 hwndNext = GetWindow( hwndControl, GW_CHILD );
998 else hwndNext = 0;
1000 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1002 while (!hwndNext && hwndControl)
1004 hwndControl = GetParent( hwndControl );
1005 if (hwndControl == hwndDlg)
1007 if(hwnd==hwndDlg) /* prevent endless loop */
1009 hwndNext=hwnd;
1010 break;
1012 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1014 else
1015 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1017 hwndControl = hwndNext;
1019 while (hwndControl && (hwndControl != hwnd));
1021 return FALSE;
1024 /***********************************************************************
1025 * DIALOG_FindMsgDestination
1027 * The messages that IsDialogMessage sends may not go to the dialog
1028 * calling IsDialogMessage if that dialog is a child, and it has the
1029 * DS_CONTROL style set.
1030 * We propagate up until we hit one that does not have DS_CONTROL, or
1031 * whose parent is not a dialog.
1033 * This is undocumented behaviour.
1035 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1037 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1039 HWND hParent = GetParent(hwndDlg);
1041 if (!hParent || !WIN_IsCurrentProcess( hParent )) break;
1042 if (!NtUserGetDialogInfo( hParent )) break;
1044 hwndDlg = hParent;
1047 return hwndDlg;
1050 /***********************************************************************
1051 * DIALOG_FixOneChildOnChangeFocus
1053 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1056 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1057 LPARAM lParam)
1059 /* If a default pushbutton then no longer default */
1060 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1061 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1062 return TRUE;
1065 /***********************************************************************
1066 * DIALOG_FixChildrenOnChangeFocus
1068 * Following the change of focus that occurs for example after handling
1069 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1070 * children may be required.
1072 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1074 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1075 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1076 /* Windows does ask for this. I don't know why yet */
1078 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1080 /* If the button that is getting the focus WAS flagged as the default
1081 * pushbutton then ask the dialog what it thinks the default is and
1082 * set that in the default style.
1084 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1086 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1087 if (HIWORD(def_id) == DC_HASDEFID)
1089 HWND hwndDef;
1090 def_id = LOWORD(def_id);
1091 hwndDef = GetDlgItem (hwndDlg, def_id);
1092 if (hwndDef)
1094 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1095 /* I know that if it is a button then it should already be a
1096 * UNDEFPUSHBUTTON, since we have just told the buttons to
1097 * change style. But maybe they ignored our request
1099 if ((dlgcode_def & DLGC_BUTTON) &&
1100 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1102 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1107 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1109 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1110 /* I wonder why it doesn't send a DM_SETDEFID */
1114 /***********************************************************************
1115 * DIALOG_IdToHwnd
1117 * A recursive version of GetDlgItem
1119 * RETURNS
1120 * The HWND for a Child ID.
1122 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1124 int i;
1125 HWND *list = WIN_ListChildren( hwndDlg );
1126 HWND ret = 0;
1128 if (!list) return 0;
1130 for (i = 0; list[i]; i++)
1132 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1134 ret = list[i];
1135 break;
1138 /* Recurse into every child */
1139 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1142 HeapFree( GetProcessHeap(), 0, list );
1143 return ret;
1146 /***********************************************************************
1147 * IsDialogMessageW (USER32.@)
1149 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1151 INT dlgCode;
1153 if (!IsWindow( hwndDlg ))
1154 return FALSE;
1156 if (NtUserCallMsgFilter( msg, MSGF_DIALOGBOX )) return TRUE;
1158 hwndDlg = WIN_GetFullHandle( hwndDlg );
1159 if (is_desktop_window(hwndDlg)) return FALSE;
1160 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1162 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1164 switch(msg->message)
1166 case WM_KEYDOWN:
1167 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1168 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1170 switch(msg->wParam)
1172 case VK_TAB:
1173 if (!(dlgCode & DLGC_WANTTAB))
1175 BOOL fIsDialog = !WIN_IsCurrentProcess( hwndDlg ) ||
1176 NtUserGetDialogInfo( hwndDlg ) != NULL;
1178 /* I am not sure under which circumstances the TAB is handled
1179 * each way. All I do know is that it does not always simply
1180 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1181 * do so but I presume someone has)
1183 if (fIsDialog)
1184 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (NtUserGetKeyState(VK_SHIFT) & 0x8000), 0 );
1185 else
1187 /* It would appear that GetNextDlgTabItem can handle being
1188 * passed hwndDlg rather than NULL but that is undocumented
1189 * so let's do it properly
1191 HWND hwndFocus = GetFocus();
1192 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1193 hwndFocus == hwndDlg ? NULL : hwndFocus,
1194 NtUserGetKeyState (VK_SHIFT) & 0x8000);
1195 if (hwndNext)
1197 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1198 msg->wParam, (LPARAM)msg);
1199 if (dlgCode & DLGC_HASSETSEL)
1201 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1202 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1203 if (buffer)
1205 INT length;
1206 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1207 length = lstrlenW (buffer);
1208 HeapFree (GetProcessHeap(), 0, buffer);
1209 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1212 NtUserSetFocus( hwndNext );
1213 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1215 else
1216 return FALSE;
1218 return TRUE;
1220 break;
1222 case VK_RIGHT:
1223 case VK_DOWN:
1224 case VK_LEFT:
1225 case VK_UP:
1226 if (!(dlgCode & DLGC_WANTARROWS))
1228 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1229 HWND hwndNext = GetNextDlgGroupItem( hwndDlg, msg->hwnd, fPrevious );
1230 if (hwndNext && SendMessageW( hwndNext, WM_GETDLGCODE, msg->wParam, (LPARAM)msg ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
1232 NtUserSetFocus( hwndNext );
1233 if ((GetWindowLongW( hwndNext, GWL_STYLE ) & BS_TYPEMASK) == BS_AUTORADIOBUTTON &&
1234 SendMessageW( hwndNext, BM_GETCHECK, 0, 0 ) != BST_CHECKED)
1235 SendMessageW( hwndNext, BM_CLICK, 1, 0 );
1237 else
1238 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1239 return TRUE;
1241 break;
1243 case VK_CANCEL:
1244 case VK_ESCAPE:
1245 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1246 return TRUE;
1248 case VK_EXECUTE:
1249 case VK_RETURN:
1251 DWORD dw;
1252 HWND hwndFocus = GetFocus();
1253 if (IsChild( hwndDlg, hwndFocus ) &&
1254 (SendMessageW( hwndFocus, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON))
1256 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( GetDlgCtrlID( hwndFocus ), BN_CLICKED ), (LPARAM)hwndFocus );
1258 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1260 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1261 if (!hwndDef || IsWindowEnabled(hwndDef))
1262 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1264 else
1266 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1269 return TRUE;
1271 break;
1273 case WM_CHAR:
1274 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1275 * It does NOT get sent in the test program I have
1277 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1278 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1279 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1280 /* drop through */
1282 case WM_SYSCHAR:
1283 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1285 /* don't translate or dispatch */
1286 return TRUE;
1288 break;
1291 TranslateMessage( msg );
1292 DispatchMessageW( msg );
1293 return TRUE;
1297 /***********************************************************************
1298 * GetDlgCtrlID (USER32.@)
1300 INT WINAPI GetDlgCtrlID( HWND hwnd )
1302 return GetWindowLongPtrW( hwnd, GWLP_ID );
1306 /***********************************************************************
1307 * GetDlgItem (USER32.@)
1309 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1311 int i;
1312 HWND *list = WIN_ListChildren( hwndDlg );
1313 HWND ret = 0;
1315 if (!list) return 0;
1317 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1318 ret = list[i];
1319 HeapFree( GetProcessHeap(), 0, list );
1320 return ret;
1324 /*******************************************************************
1325 * SendDlgItemMessageA (USER32.@)
1327 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1328 WPARAM wParam, LPARAM lParam )
1330 HWND hwndCtrl = GetDlgItem( hwnd, id );
1331 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1332 else return 0;
1336 /*******************************************************************
1337 * SendDlgItemMessageW (USER32.@)
1339 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1340 WPARAM wParam, LPARAM lParam )
1342 HWND hwndCtrl = GetDlgItem( hwnd, id );
1343 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1344 else return 0;
1348 /*******************************************************************
1349 * SetDlgItemTextA (USER32.@)
1351 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1353 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1357 /*******************************************************************
1358 * SetDlgItemTextW (USER32.@)
1360 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1362 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1366 /***********************************************************************
1367 * GetDlgItemTextA (USER32.@)
1369 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1371 if (str && (len > 0)) str[0] = '\0';
1372 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1373 len, (LPARAM)str );
1377 /***********************************************************************
1378 * GetDlgItemTextW (USER32.@)
1380 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1382 if (str && (len > 0)) str[0] = '\0';
1383 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1384 len, (LPARAM)str );
1388 /*******************************************************************
1389 * SetDlgItemInt (USER32.@)
1391 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1392 BOOL fSigned )
1394 char str[20];
1396 if (fSigned) sprintf( str, "%d", (INT)value );
1397 else sprintf( str, "%u", value );
1398 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1399 return TRUE;
1403 /***********************************************************************
1404 * GetDlgItemInt (USER32.@)
1406 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1407 BOOL fSigned )
1409 char str[30];
1410 char * endptr;
1411 LONG_PTR result = 0;
1413 if (translated) *translated = FALSE;
1414 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1415 return 0;
1416 if (fSigned)
1418 result = strtol( str, &endptr, 10 );
1419 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1420 return 0;
1421 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1422 return 0;
1424 else
1426 result = strtoul( str, &endptr, 10 );
1427 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1428 return 0;
1429 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1431 if (translated) *translated = TRUE;
1432 return (UINT)result;
1436 /***********************************************************************
1437 * CheckDlgButton (USER32.@)
1439 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1441 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1442 return TRUE;
1446 /***********************************************************************
1447 * IsDlgButtonChecked (USER32.@)
1449 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1451 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1455 /***********************************************************************
1456 * CheckRB
1458 * Callback function used to check/uncheck radio buttons that fall
1459 * within a specific range of IDs.
1461 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1463 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1464 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1466 if ((lChildID >= lpRadioGroup->firstID) &&
1467 (lChildID <= lpRadioGroup->lastID))
1469 if (lChildID == lpRadioGroup->checkID)
1471 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1473 else
1475 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1479 return TRUE;
1483 /***********************************************************************
1484 * CheckRadioButton (USER32.@)
1486 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1487 int lastID, int checkID )
1489 RADIOGROUP radioGroup;
1491 radioGroup.firstID = firstID;
1492 radioGroup.lastID = lastID;
1493 radioGroup.checkID = checkID;
1495 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1499 /***********************************************************************
1500 * GetDialogBaseUnits (USER.243)
1501 * GetDialogBaseUnits (USER32.@)
1503 DWORD WINAPI GetDialogBaseUnits(void)
1505 return NtUserGetDialogBaseUnits();
1509 /***********************************************************************
1510 * MapDialogRect (USER32.@)
1512 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1514 DIALOGINFO * dlgInfo;
1515 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1516 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1517 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1518 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1519 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1520 return TRUE;
1524 /***********************************************************************
1525 * GetNextDlgGroupItem (USER32.@)
1527 * Corrections to MSDN documentation
1529 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1530 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1531 * 2. Prev of NULL or hwndDlg fails
1533 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1535 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1536 BOOL fLooped=FALSE;
1537 BOOL fSkipping=FALSE;
1539 hwndDlg = WIN_GetFullHandle( hwndDlg );
1540 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1542 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1543 if (!hwndCtrl && fPrevious) return 0;
1545 if (hwndCtrl)
1547 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1549 else
1551 /* No ctrl specified -> start from the beginning */
1552 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1553 /* MSDN is wrong. fPrevious does not result in the last child */
1555 /* Maybe that first one is valid. If so then we don't want to skip it*/
1556 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1558 return hwndCtrl;
1562 /* Always go forward around the group and list of controls; for the
1563 * previous control keep track; for the next break when you find one
1565 retvalue = hwndCtrl;
1566 hwnd = hwndCtrl;
1567 while (1)
1569 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1570 while (!hwndNext)
1572 /* Climb out until there is a next sibling of the ancestor or we
1573 * reach the top (in which case we loop back to the start)
1575 if (hwndDlg == GetParent (hwnd))
1577 /* Wrap around to the beginning of the list, within the same
1578 * group. (Once only)
1580 if (fLooped) goto end;
1581 fLooped = TRUE;
1582 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1584 else
1586 hwnd = GetParent (hwnd);
1587 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1590 hwnd = hwndNext;
1592 /* Wander down the leading edge of controlparents */
1593 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1594 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1595 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1596 hwnd = hwndNext;
1597 /* Question. If the control is a control parent but either has no
1598 * children or is not visible/enabled then if it has a WS_GROUP does
1599 * it count? For that matter does it count anyway?
1600 * I believe it doesn't count.
1603 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1605 hwndLastGroup = hwnd;
1606 if (!fSkipping)
1608 /* Look for the beginning of the group */
1609 fSkipping = TRUE;
1613 if (hwnd == hwndCtrl)
1615 if (!fSkipping) break;
1616 if (hwndLastGroup == hwnd) break;
1617 hwnd = hwndLastGroup;
1618 fSkipping = FALSE;
1619 fLooped = FALSE;
1622 if (!fSkipping &&
1623 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1624 WS_VISIBLE)
1626 retvalue = hwnd;
1627 if (!fPrevious) break;
1630 end:
1631 return retvalue;
1635 /***********************************************************************
1636 * DIALOG_GetNextTabItem
1638 * Recursive helper for GetNextDlgTabItem
1640 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1642 LONG dsStyle;
1643 LONG exStyle;
1644 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1645 HWND retWnd = 0;
1646 HWND hChildFirst = 0;
1648 if(!hwndCtrl)
1650 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1651 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1653 else if (IsChild( hwndMain, hwndCtrl ))
1655 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1656 if(!hChildFirst)
1658 if(GetParent(hwndCtrl) != hwndMain)
1659 /* i.e. if we are not at the top level of the recursion */
1660 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1661 else
1662 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1666 while(hChildFirst)
1668 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1669 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1670 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1672 HWND retWnd;
1673 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1674 if (retWnd) return (retWnd);
1676 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1678 return (hChildFirst);
1680 hChildFirst = GetWindow(hChildFirst,wndSearch);
1682 if(hwndCtrl)
1684 HWND hParent = GetParent(hwndCtrl);
1685 while(hParent)
1687 if(hParent == hwndMain) break;
1688 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1689 if(retWnd) break;
1690 hParent = GetParent(hParent);
1692 if(!retWnd)
1693 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1695 return retWnd ? retWnd : hwndCtrl;
1698 /***********************************************************************
1699 * GetNextDlgTabItem (USER32.@)
1701 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1702 BOOL fPrevious )
1704 hwndDlg = WIN_GetFullHandle( hwndDlg );
1705 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1707 /* Undocumented but tested under Win2000 and WinME */
1708 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1710 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1711 * NB GetLastError returns whatever was set before the function was
1712 * called.
1714 if (!hwndCtrl && fPrevious) return 0;
1716 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1719 /**********************************************************************
1720 * DIALOG_DlgDirSelect
1722 * Helper function for DlgDirSelect*
1724 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1725 INT id, BOOL unicode, BOOL combo )
1727 WCHAR *buffer, *ptr;
1728 INT item, size;
1729 BOOL ret;
1730 HWND listbox = GetDlgItem( hwnd, id );
1732 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1733 if (!listbox) return FALSE;
1735 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1736 if (item == LB_ERR) return FALSE;
1738 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1739 if (size == LB_ERR) return FALSE;
1741 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1743 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1745 if ((ret = (buffer[0] == '['))) /* drive or directory */
1747 if (buffer[1] == '-') /* drive */
1749 buffer[3] = ':';
1750 buffer[4] = 0;
1751 ptr = buffer + 2;
1753 else
1755 buffer[lstrlenW(buffer)-1] = '\\';
1756 ptr = buffer + 1;
1759 else
1761 /* Filenames without a dot extension must have one tacked at the end */
1762 if (wcschr(buffer, '.') == NULL)
1764 buffer[lstrlenW(buffer)+1] = '\0';
1765 buffer[lstrlenW(buffer)] = '.';
1767 ptr = buffer;
1770 if (!unicode)
1772 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1773 ((LPSTR)str)[len-1] = 0;
1775 else lstrcpynW( str, ptr, len );
1776 HeapFree( GetProcessHeap(), 0, buffer );
1777 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1778 return ret;
1782 /**********************************************************************
1783 * DIALOG_DlgDirListW
1785 * Helper function for DlgDirList*W
1787 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1788 INT idStatic, UINT attrib, BOOL combo )
1790 HWND hwnd;
1791 LPWSTR orig_spec = spec;
1792 WCHAR any[] = L"*.*";
1793 WCHAR star[] = L"*";
1795 #define SENDMSG(msg,wparam,lparam) \
1796 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1797 : SendMessageW( hwnd, msg, wparam, lparam ))
1799 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1801 /* If the path exists and is a directory, chdir to it */
1802 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = star;
1803 else
1805 WCHAR *p, *p2;
1807 if (!wcschr(spec, '*') && !wcschr(spec, '?'))
1809 SetLastError(ERROR_NO_WILDCARD_CHARACTERS);
1810 return FALSE;
1812 p = spec;
1813 if ((p2 = wcschr( p, ':' ))) p = p2 + 1;
1814 if ((p2 = wcsrchr( p, '\\' ))) p = p2;
1815 if ((p2 = wcsrchr( p, '/' ))) p = p2;
1816 if (p != spec)
1818 WCHAR sep = *p;
1819 *p = 0;
1820 if (!SetCurrentDirectoryW( spec ))
1822 *p = sep; /* Restore the original spec */
1823 return FALSE;
1825 spec = p + 1;
1829 TRACE( "mask=%s\n", debugstr_w(spec) );
1831 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1833 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1835 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1836 if (attrib & DDL_DIRECTORY)
1838 if (!(attrib & DDL_EXCLUSIVE))
1840 SENDMSG( combo ? CB_DIR : LB_DIR,
1841 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1842 (LPARAM)spec );
1844 SENDMSG( combo ? CB_DIR : LB_DIR,
1845 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1846 (LPARAM)any );
1848 else
1850 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1854 /* Convert path specification to uppercase */
1855 if (spec) CharUpperW(spec);
1857 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1859 WCHAR temp[MAX_PATH];
1860 GetCurrentDirectoryW( ARRAY_SIZE( temp ), temp );
1861 CharLowerW( temp );
1862 /* Can't use PostMessage() here, because the string is on the stack */
1863 SetDlgItemTextW( hDlg, idStatic, temp );
1866 if (orig_spec && (spec != orig_spec))
1868 /* Update the original file spec */
1869 WCHAR *p = spec;
1870 while ((*orig_spec++ = *p++));
1873 return TRUE;
1874 #undef SENDMSG
1878 /**********************************************************************
1879 * DIALOG_DlgDirListA
1881 * Helper function for DlgDirList*A
1883 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1884 INT idStatic, UINT attrib, BOOL combo )
1886 if (spec)
1888 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1889 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1890 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1891 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1892 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1893 HeapFree( GetProcessHeap(), 0, specW );
1894 return ret;
1896 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1900 /**********************************************************************
1901 * DlgDirSelectExA (USER32.@)
1903 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1905 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1909 /**********************************************************************
1910 * DlgDirSelectExW (USER32.@)
1912 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1914 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1918 /**********************************************************************
1919 * DlgDirSelectComboBoxExA (USER32.@)
1921 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1922 INT id )
1924 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1928 /**********************************************************************
1929 * DlgDirSelectComboBoxExW (USER32.@)
1931 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1932 INT id)
1934 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1938 /**********************************************************************
1939 * DlgDirListA (USER32.@)
1941 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1942 INT idStatic, UINT attrib )
1944 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1948 /**********************************************************************
1949 * DlgDirListW (USER32.@)
1951 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1952 INT idStatic, UINT attrib )
1954 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1958 /**********************************************************************
1959 * DlgDirListComboBoxA (USER32.@)
1961 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1962 INT idStatic, UINT attrib )
1964 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1968 /**********************************************************************
1969 * DlgDirListComboBoxW (USER32.@)
1971 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1972 INT idStatic, UINT attrib )
1974 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );