ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / dlls / user32 / dialog.c
blob88c2930c065409f0ee0bad9d2ed36d37fd1f676f
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_GetControl32
109 * Return the class and text of the control pointed to by ptr,
110 * fill the header structure and return a pointer to the next control.
112 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
113 BOOL dialogEx )
115 if (dialogEx)
117 info->helpId = GET_DWORD(p); p += 2;
118 info->exStyle = GET_DWORD(p); p += 2;
119 info->style = GET_DWORD(p); p += 2;
121 else
123 info->helpId = 0;
124 info->style = GET_DWORD(p); p += 2;
125 info->exStyle = GET_DWORD(p); p += 2;
127 info->x = GET_WORD(p); p++;
128 info->y = GET_WORD(p); p++;
129 info->cx = GET_WORD(p); p++;
130 info->cy = GET_WORD(p); p++;
132 if (dialogEx)
134 /* id is 4 bytes for DIALOGEX */
135 info->id = GET_LONG(p);
136 p += 2;
138 else
140 info->id = GET_WORD(p);
141 p++;
144 if (GET_WORD(p) == 0xffff)
146 static const WCHAR class_names[6][10] =
148 { 'B','u','t','t','o','n', }, /* 0x80 */
149 { 'E','d','i','t', }, /* 0x81 */
150 { 'S','t','a','t','i','c', }, /* 0x82 */
151 { 'L','i','s','t','B','o','x', }, /* 0x83 */
152 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
153 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
155 WORD id = GET_WORD(p+1);
156 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
157 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
158 if (id <= 5)
159 info->className = class_names[id];
160 else
162 info->className = NULL;
163 ERR("Unknown built-in class id %04x\n", id );
165 p += 2;
167 else
169 info->className = p;
170 p += strlenW( info->className ) + 1;
173 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
175 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
176 p += 2;
178 else
180 info->windowName = p;
181 p += strlenW( info->windowName ) + 1;
184 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
185 debugstr_w( info->className ), debugstr_w( info->windowName ),
186 info->id, info->x, info->y, info->cx, info->cy,
187 info->style, info->exStyle, info->helpId );
189 if (GET_WORD(p))
191 if (TRACE_ON(dialog))
193 WORD i, count = GET_WORD(p) / sizeof(WORD);
194 TRACE(" BEGIN\n");
195 TRACE(" ");
196 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
197 TRACE("\n");
198 TRACE(" END\n" );
200 info->data = p;
201 p += GET_WORD(p) / sizeof(WORD);
203 else info->data = NULL;
204 p++;
206 /* Next control is on dword boundary */
207 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
211 /***********************************************************************
212 * DIALOG_CreateControls32
214 * Create the control windows for a dialog.
216 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
217 HINSTANCE hInst, BOOL unicode )
219 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
220 DLG_CONTROL_INFO info;
221 HWND hwndCtrl, hwndDefButton = 0;
222 INT items = dlgTemplate->nbItems;
224 TRACE(" BEGIN\n" );
225 while (items--)
227 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
228 dlgTemplate->dialogEx );
229 info.style &= ~WS_POPUP;
230 info.style |= WS_CHILD;
232 if (info.style & WS_BORDER)
234 info.style &= ~WS_BORDER;
235 info.exStyle |= WS_EX_CLIENTEDGE;
237 if (unicode)
239 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
240 info.className, info.windowName,
241 info.style | WS_CHILD,
242 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
243 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
244 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
245 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
246 hwnd, (HMENU)info.id,
247 hInst, (LPVOID)info.data );
249 else
251 LPCSTR class = (LPCSTR)info.className;
252 LPCSTR caption = (LPCSTR)info.windowName;
253 LPSTR class_tmp = NULL;
254 LPSTR caption_tmp = NULL;
256 if (!IS_INTRESOURCE(class))
258 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
259 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
260 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
261 class = class_tmp;
263 if (!IS_INTRESOURCE(caption))
265 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
266 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
267 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
268 caption = caption_tmp;
270 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
271 class, caption, info.style | WS_CHILD,
272 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
273 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
274 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
275 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
276 hwnd, (HMENU)info.id,
277 hInst, (LPVOID)info.data );
278 HeapFree( GetProcessHeap(), 0, class_tmp );
279 HeapFree( GetProcessHeap(), 0, caption_tmp );
281 if (!hwndCtrl)
283 WARN("control %s %s creation failed\n", debugstr_w(info.className),
284 debugstr_w(info.windowName));
285 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
286 SetLastError(0);
287 return FALSE;
290 /* Send initialisation messages to the control */
291 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
292 (WPARAM)dlgInfo->hUserFont, 0 );
293 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
295 /* If there's already a default push-button, set it back */
296 /* to normal and use this one instead. */
297 if (hwndDefButton)
298 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
299 hwndDefButton = hwndCtrl;
300 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
303 TRACE(" END\n" );
304 return TRUE;
308 /***********************************************************************
309 * DIALOG_ParseTemplate32
311 * Fill a DLG_TEMPLATE structure from the dialog template, and return
312 * a pointer to the first control.
314 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
316 const WORD *p = (const WORD *)template;
317 WORD signature;
318 WORD dlgver;
320 dlgver = GET_WORD(p); p++;
321 signature = GET_WORD(p); p++;
323 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
325 result->dialogEx = TRUE;
326 result->helpId = GET_DWORD(p); p += 2;
327 result->exStyle = GET_DWORD(p); p += 2;
328 result->style = GET_DWORD(p); p += 2;
330 else
332 result->style = GET_DWORD(p - 2);
333 result->dialogEx = FALSE;
334 result->helpId = 0;
335 result->exStyle = GET_DWORD(p); p += 2;
337 result->nbItems = GET_WORD(p); p++;
338 result->x = GET_WORD(p); p++;
339 result->y = GET_WORD(p); p++;
340 result->cx = GET_WORD(p); p++;
341 result->cy = GET_WORD(p); p++;
342 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
343 result->dialogEx ? "EX" : "", result->x, result->y,
344 result->cx, result->cy, result->helpId );
345 TRACE(" STYLE 0x%08x\n", result->style );
346 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
348 /* Get the menu name */
350 switch(GET_WORD(p))
352 case 0x0000:
353 result->menuName = NULL;
354 p++;
355 break;
356 case 0xffff:
357 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
358 p += 2;
359 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
360 break;
361 default:
362 result->menuName = p;
363 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
364 p += strlenW( result->menuName ) + 1;
365 break;
368 /* Get the class name */
370 switch(GET_WORD(p))
372 case 0x0000:
373 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
374 p++;
375 break;
376 case 0xffff:
377 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
378 p += 2;
379 TRACE(" CLASS %04x\n", LOWORD(result->className) );
380 break;
381 default:
382 result->className = p;
383 TRACE(" CLASS %s\n", debugstr_w( result->className ));
384 p += strlenW( result->className ) + 1;
385 break;
388 /* Get the window caption */
390 result->caption = p;
391 p += strlenW( result->caption ) + 1;
392 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
394 /* Get the font name */
396 result->pointSize = 0;
397 result->faceName = NULL;
398 result->weight = FW_DONTCARE;
399 result->italic = FALSE;
401 if (result->style & DS_SETFONT)
403 result->pointSize = GET_WORD(p);
404 p++;
406 /* If pointSize is 0x7fff, it means that we need to use the font
407 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
408 * italic, and facename from the dialog template.
410 if (result->pointSize == 0x7fff)
412 /* We could call SystemParametersInfo here, but then we'd have
413 * to convert from pixel size to point size (which can be
414 * imprecise).
416 TRACE(" FONT: Using message box font\n");
418 else
420 if (result->dialogEx)
422 result->weight = GET_WORD(p); p++;
423 result->italic = LOBYTE(GET_WORD(p)); p++;
425 result->faceName = p;
426 p += strlenW( result->faceName ) + 1;
428 TRACE(" FONT %d, %s, %d, %s\n",
429 result->pointSize, debugstr_w( result->faceName ),
430 result->weight, result->italic ? "TRUE" : "FALSE" );
434 /* First control is on dword boundary */
435 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
439 /***********************************************************************
440 * DIALOG_CreateIndirect
441 * Creates a dialog box window
443 * modal = TRUE if we are called from a modal dialog box.
444 * (it's more compatible to do it here, as under Windows the owner
445 * is never disabled if the dialog fails because of an invalid template)
447 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
448 HWND owner, DLGPROC dlgProc, LPARAM param,
449 BOOL unicode, HWND *modal_owner )
451 HWND hwnd;
452 RECT rect;
453 POINT pos;
454 SIZE size;
455 DLG_TEMPLATE template;
456 DIALOGINFO * dlgInfo;
457 DWORD units = GetDialogBaseUnits();
458 HWND disabled_owner = NULL;
459 HMENU hMenu = 0;
460 HFONT hUserFont = 0;
461 UINT flags = 0;
462 UINT xBaseUnit = LOWORD(units);
463 UINT yBaseUnit = HIWORD(units);
465 /* Parse dialog template */
467 if (!dlgTemplate) return 0;
468 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
470 /* Load menu */
472 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
474 /* Create custom font if needed */
476 if (template.style & DS_SETFONT)
478 HDC dc = GetDC(0);
480 if (template.pointSize == 0x7fff)
482 /* We get the message font from the non-client metrics */
483 NONCLIENTMETRICSW ncMetrics;
485 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
486 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
487 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
489 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
492 else
494 /* We convert the size to pixels and then make it -ve. This works
495 * for both +ve and -ve template.pointSize */
496 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
497 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
498 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
499 PROOF_QUALITY, FF_DONTCARE,
500 template.faceName );
503 if (hUserFont)
505 SIZE charSize;
506 HFONT hOldFont = SelectObject( dc, hUserFont );
507 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
508 if (charSize.cx)
510 xBaseUnit = charSize.cx;
511 yBaseUnit = charSize.cy;
513 SelectObject( dc, hOldFont );
515 ReleaseDC(0, dc);
516 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
519 /* Create dialog main window */
521 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
523 if (template.style & DS_CONTROL)
524 template.style &= ~(WS_CAPTION|WS_SYSMENU);
525 template.style |= DS_3DLOOK;
526 if (template.style & DS_MODALFRAME)
527 template.exStyle |= WS_EX_DLGMODALFRAME;
528 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
529 template.exStyle |= WS_EX_CONTROLPARENT;
530 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
531 pos.x = rect.left;
532 pos.y = rect.top;
533 size.cx = rect.right - rect.left;
534 size.cy = rect.bottom - rect.top;
536 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
538 pos.x = pos.y = CW_USEDEFAULT;
540 else
542 HMONITOR monitor = 0;
543 MONITORINFO mon_info;
545 mon_info.cbSize = sizeof(mon_info);
546 if (template.style & DS_CENTER)
548 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
549 GetMonitorInfoW( monitor, &mon_info );
550 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
551 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
553 else if (template.style & DS_CENTERMOUSE)
555 GetCursorPos( &pos );
556 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
557 GetMonitorInfoW( monitor, &mon_info );
559 else
561 pos.x += MulDiv(template.x, xBaseUnit, 4);
562 pos.y += MulDiv(template.y, yBaseUnit, 8);
563 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
565 if ( !(template.style & WS_CHILD) )
567 INT dX, dY;
569 /* try to fit it into the desktop */
571 if (!monitor)
573 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
574 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
575 GetMonitorInfoW( monitor, &mon_info );
577 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
578 pos.x -= dX;
579 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
580 pos.y -= dY;
581 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
582 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
586 if (modal_owner && owner)
588 HWND parent;
590 * Owner needs to be top level window. We need to duplicate the logic from server,
591 * because we need to disable it before creating dialog window. Note that we do that
592 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
593 * Windows does.
595 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
597 parent = GetParent( owner );
598 if (!parent || parent == GetDesktopWindow()) break;
599 owner = parent;
601 *modal_owner = owner;
602 if (IsWindowEnabled( owner ))
604 disabled_owner = owner;
605 EnableWindow( disabled_owner, FALSE );
609 if (unicode)
611 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
612 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
613 owner, hMenu, hInst, NULL );
615 else
617 LPCSTR class = (LPCSTR)template.className;
618 LPCSTR caption = (LPCSTR)template.caption;
619 LPSTR class_tmp = NULL;
620 LPSTR caption_tmp = NULL;
622 if (!IS_INTRESOURCE(class))
624 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
625 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
626 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
627 class = class_tmp;
629 if (!IS_INTRESOURCE(caption))
631 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
632 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
633 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
634 caption = caption_tmp;
636 hwnd = CreateWindowExA(template.exStyle, class, caption,
637 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
638 owner, hMenu, hInst, NULL );
639 HeapFree( GetProcessHeap(), 0, class_tmp );
640 HeapFree( GetProcessHeap(), 0, caption_tmp );
643 if (!hwnd)
645 if (hUserFont) DeleteObject( hUserFont );
646 if (hMenu) DestroyMenu( hMenu );
647 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
648 return 0;
651 /* moved this from the top of the method to here as DIALOGINFO structure
652 will be valid only after WM_CREATE message has been handled in DefDlgProc
653 All the members of the structure get filled here using temp variables */
654 dlgInfo = DIALOG_get_info( hwnd, TRUE );
655 dlgInfo->hwndFocus = 0;
656 dlgInfo->hUserFont = hUserFont;
657 dlgInfo->hMenu = hMenu;
658 dlgInfo->xBaseUnit = xBaseUnit;
659 dlgInfo->yBaseUnit = yBaseUnit;
660 dlgInfo->flags = flags;
662 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
664 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
665 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
667 if (dlgProc && dlgInfo->hUserFont)
668 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
670 /* Create controls */
672 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
674 /* Send initialisation messages and set focus */
676 if (dlgProc)
678 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
679 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
680 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
681 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
683 /* By returning TRUE, app has requested a default focus assignment.
684 * WM_INITDIALOG may have changed the tab order, so find the first
685 * tabstop control again. */
686 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
687 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
688 if (focus)
690 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
691 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
692 SetFocus( focus );
694 else
696 if (!(template.style & WS_CHILD))
697 SetFocus( hwnd );
702 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
704 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
706 return hwnd;
708 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
709 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
710 return 0;
714 /***********************************************************************
715 * CreateDialogParamA (USER32.@)
717 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
718 DLGPROC dlgProc, LPARAM param )
720 HRSRC hrsrc;
721 LPCDLGTEMPLATEA ptr;
723 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
724 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
725 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
729 /***********************************************************************
730 * CreateDialogParamW (USER32.@)
732 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
733 DLGPROC dlgProc, LPARAM param )
735 HRSRC hrsrc;
736 LPCDLGTEMPLATEA ptr;
738 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
739 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
740 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
744 /***********************************************************************
745 * CreateDialogIndirectParamAorW (USER32.@)
747 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
748 HWND owner, DLGPROC dlgProc, LPARAM param,
749 DWORD flags )
751 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
754 /***********************************************************************
755 * CreateDialogIndirectParamA (USER32.@)
757 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
758 HWND owner, DLGPROC dlgProc, LPARAM param )
760 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
763 /***********************************************************************
764 * CreateDialogIndirectParamW (USER32.@)
766 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
767 HWND owner, DLGPROC dlgProc, LPARAM param )
769 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
773 /***********************************************************************
774 * DIALOG_DoDialogBox
776 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
778 DIALOGINFO * dlgInfo;
779 MSG msg;
780 INT retval;
781 BOOL bFirstEmpty;
783 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
785 bFirstEmpty = TRUE;
786 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
788 for (;;)
790 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
792 if (bFirstEmpty)
794 /* ShowWindow the first time the queue goes empty */
795 ShowWindow( hwnd, SW_SHOWNORMAL );
796 bFirstEmpty = FALSE;
798 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
800 /* No message present -> send ENTERIDLE and wait */
801 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
803 GetMessageW( &msg, 0, 0, 0 );
806 if (msg.message == WM_QUIT)
808 PostQuitMessage( msg.wParam );
809 if (!IsWindow( hwnd )) return 0;
810 break;
812 if (!IsWindow( hwnd )) return 0;
813 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
815 TranslateMessage( &msg );
816 DispatchMessageW( &msg );
818 if (!IsWindow( hwnd )) return 0;
819 if (dlgInfo->flags & DF_END) break;
821 if (bFirstEmpty && msg.message == WM_TIMER)
823 ShowWindow( hwnd, SW_SHOWNORMAL );
824 bFirstEmpty = FALSE;
828 retval = dlgInfo->idResult;
829 DestroyWindow( hwnd );
830 return retval;
834 /***********************************************************************
835 * DialogBoxParamA (USER32.@)
837 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
838 HWND owner, DLGPROC dlgProc, LPARAM param )
840 HWND hwnd;
841 HRSRC hrsrc;
842 LPCDLGTEMPLATEA ptr;
844 if (owner && !IsWindow(owner)) return 0;
846 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
847 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
848 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner ))) return -1;
849 return DIALOG_DoDialogBox( hwnd, owner );
853 /***********************************************************************
854 * DialogBoxParamW (USER32.@)
856 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
857 HWND owner, DLGPROC dlgProc, LPARAM param )
859 HWND hwnd;
860 HRSRC hrsrc;
861 LPCDLGTEMPLATEW ptr;
863 if (owner && !IsWindow(owner)) return 0;
865 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
866 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
867 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner ))) return -1;
868 return DIALOG_DoDialogBox( hwnd, owner );
872 /***********************************************************************
873 * DialogBoxIndirectParamAorW (USER32.@)
875 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
876 HWND owner, DLGPROC dlgProc,
877 LPARAM param, DWORD flags )
879 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
880 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
881 return -1;
884 /***********************************************************************
885 * DialogBoxIndirectParamA (USER32.@)
887 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
888 HWND owner, DLGPROC dlgProc, LPARAM param )
890 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
894 /***********************************************************************
895 * DialogBoxIndirectParamW (USER32.@)
897 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
898 HWND owner, DLGPROC dlgProc, LPARAM param )
900 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
903 /***********************************************************************
904 * EndDialog (USER32.@)
906 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
908 DIALOGINFO * dlgInfo;
909 HWND owner;
911 TRACE("%p %ld\n", hwnd, retval );
913 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
915 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
916 return FALSE;
918 dlgInfo->idResult = retval;
919 dlgInfo->flags |= DF_END;
921 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
922 if (owner)
923 EnableWindow( owner, TRUE );
925 /* Windows sets the focus to the dialog itself in EndDialog */
927 if (IsChild(hwnd, GetFocus()))
928 SetFocus( hwnd );
930 /* Don't have to send a ShowWindow(SW_HIDE), just do
931 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
933 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
934 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
936 if (hwnd == GetActiveWindow())
938 /* If this dialog was given an owner then set the focus to that owner. */
939 if (owner)
940 SetForegroundWindow( owner );
941 else
942 WINPOS_ActivateOtherWindow( hwnd );
945 /* unblock dialog loop */
946 PostMessageA(hwnd, WM_NULL, 0, 0);
947 return TRUE;
951 /***********************************************************************
952 * DIALOG_IsAccelerator
954 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
956 HWND hwndControl = hwnd;
957 HWND hwndNext;
958 INT dlgCode;
959 WCHAR buffer[128];
963 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
964 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
966 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
967 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
968 GetWindowTextW( hwndControl, buffer, ARRAY_SIZE( buffer )))
970 /* find the accelerator key */
971 LPWSTR p = buffer - 2;
975 p = strchrW( p + 2, '&' );
977 while (p != NULL && p[1] == '&');
979 /* and check if it's the one we're looking for */
980 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
982 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
984 /* set focus to the control */
985 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
986 /* and bump it on to next */
987 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
989 else if (dlgCode & DLGC_BUTTON)
991 /* send BM_CLICK message to the control */
992 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
994 return TRUE;
997 hwndNext = GetWindow( hwndControl, GW_CHILD );
999 else hwndNext = 0;
1001 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1003 while (!hwndNext && hwndControl)
1005 hwndControl = GetParent( hwndControl );
1006 if (hwndControl == hwndDlg)
1008 if(hwnd==hwndDlg) /* prevent endless loop */
1010 hwndNext=hwnd;
1011 break;
1013 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1015 else
1016 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1018 hwndControl = hwndNext;
1020 while (hwndControl && (hwndControl != hwnd));
1022 return FALSE;
1025 /***********************************************************************
1026 * DIALOG_FindMsgDestination
1028 * The messages that IsDialogMessage sends may not go to the dialog
1029 * calling IsDialogMessage if that dialog is a child, and it has the
1030 * DS_CONTROL style set.
1031 * We propagate up until we hit one that does not have DS_CONTROL, or
1032 * whose parent is not a dialog.
1034 * This is undocumented behaviour.
1036 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1038 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1040 WND *pParent;
1041 HWND hParent = GetParent(hwndDlg);
1042 if (!hParent) break;
1044 pParent = WIN_GetPtr(hParent);
1045 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1047 if (!pParent->dlgInfo)
1049 WIN_ReleasePtr(pParent);
1050 break;
1052 WIN_ReleasePtr(pParent);
1054 hwndDlg = hParent;
1057 return hwndDlg;
1060 /***********************************************************************
1061 * DIALOG_FixOneChildOnChangeFocus
1063 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1066 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1067 LPARAM lParam)
1069 /* If a default pushbutton then no longer default */
1070 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1071 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1072 return TRUE;
1075 /***********************************************************************
1076 * DIALOG_FixChildrenOnChangeFocus
1078 * Following the change of focus that occurs for example after handling
1079 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1080 * children may be required.
1082 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1084 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1085 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1086 /* Windows does ask for this. I don't know why yet */
1088 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1090 /* If the button that is getting the focus WAS flagged as the default
1091 * pushbutton then ask the dialog what it thinks the default is and
1092 * set that in the default style.
1094 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1096 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1097 if (HIWORD(def_id) == DC_HASDEFID)
1099 HWND hwndDef;
1100 def_id = LOWORD(def_id);
1101 hwndDef = GetDlgItem (hwndDlg, def_id);
1102 if (hwndDef)
1104 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1105 /* I know that if it is a button then it should already be a
1106 * UNDEFPUSHBUTTON, since we have just told the buttons to
1107 * change style. But maybe they ignored our request
1109 if ((dlgcode_def & DLGC_BUTTON) &&
1110 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1112 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1117 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1119 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1120 /* I wonder why it doesn't send a DM_SETDEFID */
1124 /***********************************************************************
1125 * DIALOG_IdToHwnd
1127 * A recursive version of GetDlgItem
1129 * RETURNS
1130 * The HWND for a Child ID.
1132 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1134 int i;
1135 HWND *list = WIN_ListChildren( hwndDlg );
1136 HWND ret = 0;
1138 if (!list) return 0;
1140 for (i = 0; list[i]; i++)
1142 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1144 ret = list[i];
1145 break;
1148 /* Recurse into every child */
1149 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1152 HeapFree( GetProcessHeap(), 0, list );
1153 return ret;
1156 /***********************************************************************
1157 * IsDialogMessageW (USER32.@)
1159 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1161 INT dlgCode;
1163 if (!IsWindow( hwndDlg ))
1164 return FALSE;
1166 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1168 hwndDlg = WIN_GetFullHandle( hwndDlg );
1169 if (is_desktop_window(hwndDlg)) return FALSE;
1170 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1172 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1174 switch(msg->message)
1176 case WM_KEYDOWN:
1177 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1178 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1180 switch(msg->wParam)
1182 case VK_TAB:
1183 if (!(dlgCode & DLGC_WANTTAB))
1185 BOOL fIsDialog = TRUE;
1186 WND *pWnd = WIN_GetPtr( hwndDlg );
1188 if (pWnd && pWnd != WND_OTHER_PROCESS)
1190 fIsDialog = (pWnd->dlgInfo != NULL);
1191 WIN_ReleasePtr(pWnd);
1194 /* I am not sure under which circumstances the TAB is handled
1195 * each way. All I do know is that it does not always simply
1196 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1197 * do so but I presume someone has)
1199 if (fIsDialog)
1200 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1201 else
1203 /* It would appear that GetNextDlgTabItem can handle being
1204 * passed hwndDlg rather than NULL but that is undocumented
1205 * so let's do it properly
1207 HWND hwndFocus = GetFocus();
1208 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1209 hwndFocus == hwndDlg ? NULL : hwndFocus,
1210 GetKeyState (VK_SHIFT) & 0x8000);
1211 if (hwndNext)
1213 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1214 msg->wParam, (LPARAM)msg);
1215 if (dlgCode & DLGC_HASSETSEL)
1217 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1218 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1219 if (buffer)
1221 INT length;
1222 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1223 length = strlenW (buffer);
1224 HeapFree (GetProcessHeap(), 0, buffer);
1225 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1228 SetFocus (hwndNext);
1229 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1231 else
1232 return FALSE;
1234 return TRUE;
1236 break;
1238 case VK_RIGHT:
1239 case VK_DOWN:
1240 case VK_LEFT:
1241 case VK_UP:
1242 if (!(dlgCode & DLGC_WANTARROWS))
1244 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1245 HWND hwndNext = GetNextDlgGroupItem( hwndDlg, msg->hwnd, fPrevious );
1246 if (hwndNext && SendMessageW( hwndNext, WM_GETDLGCODE, msg->wParam, (LPARAM)msg ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
1248 SetFocus( hwndNext );
1249 if ((GetWindowLongW( hwndNext, GWL_STYLE ) & BS_TYPEMASK) == BS_AUTORADIOBUTTON &&
1250 SendMessageW( hwndNext, BM_GETCHECK, 0, 0 ) != BST_CHECKED)
1251 SendMessageW( hwndNext, BM_CLICK, 1, 0 );
1253 else
1254 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1255 return TRUE;
1257 break;
1259 case VK_CANCEL:
1260 case VK_ESCAPE:
1261 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1262 return TRUE;
1264 case VK_EXECUTE:
1265 case VK_RETURN:
1267 DWORD dw;
1268 HWND hwndFocus = GetFocus();
1269 if (IsChild( hwndDlg, hwndFocus ) &&
1270 (SendMessageW( hwndFocus, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON))
1272 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( GetDlgCtrlID( hwndFocus ), BN_CLICKED ), (LPARAM)hwndFocus );
1274 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1276 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1277 if (!hwndDef || IsWindowEnabled(hwndDef))
1278 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1280 else
1282 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1285 return TRUE;
1287 break;
1289 case WM_CHAR:
1290 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1291 * It does NOT get sent in the test program I have
1293 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1294 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1295 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1296 /* drop through */
1298 case WM_SYSCHAR:
1299 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1301 /* don't translate or dispatch */
1302 return TRUE;
1304 break;
1307 TranslateMessage( msg );
1308 DispatchMessageW( msg );
1309 return TRUE;
1313 /***********************************************************************
1314 * GetDlgCtrlID (USER32.@)
1316 INT WINAPI GetDlgCtrlID( HWND hwnd )
1318 return GetWindowLongPtrW( hwnd, GWLP_ID );
1322 /***********************************************************************
1323 * GetDlgItem (USER32.@)
1325 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1327 int i;
1328 HWND *list = WIN_ListChildren( hwndDlg );
1329 HWND ret = 0;
1331 if (!list) return 0;
1333 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1334 ret = list[i];
1335 HeapFree( GetProcessHeap(), 0, list );
1336 return ret;
1340 /*******************************************************************
1341 * SendDlgItemMessageA (USER32.@)
1343 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1344 WPARAM wParam, LPARAM lParam )
1346 HWND hwndCtrl = GetDlgItem( hwnd, id );
1347 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1348 else return 0;
1352 /*******************************************************************
1353 * SendDlgItemMessageW (USER32.@)
1355 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1356 WPARAM wParam, LPARAM lParam )
1358 HWND hwndCtrl = GetDlgItem( hwnd, id );
1359 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1360 else return 0;
1364 /*******************************************************************
1365 * SetDlgItemTextA (USER32.@)
1367 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1369 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1373 /*******************************************************************
1374 * SetDlgItemTextW (USER32.@)
1376 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1378 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1382 /***********************************************************************
1383 * GetDlgItemTextA (USER32.@)
1385 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1387 if (str && (len > 0)) str[0] = '\0';
1388 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1389 len, (LPARAM)str );
1393 /***********************************************************************
1394 * GetDlgItemTextW (USER32.@)
1396 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1398 if (str && (len > 0)) str[0] = '\0';
1399 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1400 len, (LPARAM)str );
1404 /*******************************************************************
1405 * SetDlgItemInt (USER32.@)
1407 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1408 BOOL fSigned )
1410 char str[20];
1412 if (fSigned) sprintf( str, "%d", (INT)value );
1413 else sprintf( str, "%u", value );
1414 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1415 return TRUE;
1419 /***********************************************************************
1420 * GetDlgItemInt (USER32.@)
1422 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1423 BOOL fSigned )
1425 char str[30];
1426 char * endptr;
1427 LONG_PTR result = 0;
1429 if (translated) *translated = FALSE;
1430 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1431 return 0;
1432 if (fSigned)
1434 result = strtol( str, &endptr, 10 );
1435 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1436 return 0;
1437 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1438 return 0;
1440 else
1442 result = strtoul( str, &endptr, 10 );
1443 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1444 return 0;
1445 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1447 if (translated) *translated = TRUE;
1448 return (UINT)result;
1452 /***********************************************************************
1453 * CheckDlgButton (USER32.@)
1455 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1457 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1458 return TRUE;
1462 /***********************************************************************
1463 * IsDlgButtonChecked (USER32.@)
1465 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1467 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1471 /***********************************************************************
1472 * CheckRB
1474 * Callback function used to check/uncheck radio buttons that fall
1475 * within a specific range of IDs.
1477 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1479 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1480 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1482 if ((lChildID >= lpRadioGroup->firstID) &&
1483 (lChildID <= lpRadioGroup->lastID))
1485 if (lChildID == lpRadioGroup->checkID)
1487 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1489 else
1491 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1495 return TRUE;
1499 /***********************************************************************
1500 * CheckRadioButton (USER32.@)
1502 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1503 int lastID, int checkID )
1505 RADIOGROUP radioGroup;
1507 radioGroup.firstID = firstID;
1508 radioGroup.lastID = lastID;
1509 radioGroup.checkID = checkID;
1511 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1515 /***********************************************************************
1516 * GetDialogBaseUnits (USER.243)
1517 * GetDialogBaseUnits (USER32.@)
1519 DWORD WINAPI GetDialogBaseUnits(void)
1521 static LONG cx, cy;
1523 if (!cx)
1525 HDC hdc;
1527 if ((hdc = GetDC(0)))
1529 cx = GdiGetCharDimensions( hdc, NULL, &cy );
1530 ReleaseDC( 0, hdc );
1532 TRACE( "base units = %d,%d\n", cx, cy );
1535 return MAKELONG( MulDiv( cx, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI ),
1536 MulDiv( cy, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI ));
1540 /***********************************************************************
1541 * MapDialogRect (USER32.@)
1543 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1545 DIALOGINFO * dlgInfo;
1546 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1547 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1548 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1549 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1550 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1551 return TRUE;
1555 /***********************************************************************
1556 * GetNextDlgGroupItem (USER32.@)
1558 * Corrections to MSDN documentation
1560 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1561 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1562 * 2. Prev of NULL or hwndDlg fails
1564 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1566 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1567 BOOL fLooped=FALSE;
1568 BOOL fSkipping=FALSE;
1570 hwndDlg = WIN_GetFullHandle( hwndDlg );
1571 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1573 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1574 if (!hwndCtrl && fPrevious) return 0;
1576 if (hwndCtrl)
1578 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1580 else
1582 /* No ctrl specified -> start from the beginning */
1583 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1584 /* MSDN is wrong. fPrevious does not result in the last child */
1586 /* Maybe that first one is valid. If so then we don't want to skip it*/
1587 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1589 return hwndCtrl;
1593 /* Always go forward around the group and list of controls; for the
1594 * previous control keep track; for the next break when you find one
1596 retvalue = hwndCtrl;
1597 hwnd = hwndCtrl;
1598 while (1)
1600 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1601 while (!hwndNext)
1603 /* Climb out until there is a next sibling of the ancestor or we
1604 * reach the top (in which case we loop back to the start)
1606 if (hwndDlg == GetParent (hwnd))
1608 /* Wrap around to the beginning of the list, within the same
1609 * group. (Once only)
1611 if (fLooped) goto end;
1612 fLooped = TRUE;
1613 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1615 else
1617 hwnd = GetParent (hwnd);
1618 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1621 hwnd = hwndNext;
1623 /* Wander down the leading edge of controlparents */
1624 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1625 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1626 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1627 hwnd = hwndNext;
1628 /* Question. If the control is a control parent but either has no
1629 * children or is not visible/enabled then if it has a WS_GROUP does
1630 * it count? For that matter does it count anyway?
1631 * I believe it doesn't count.
1634 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1636 hwndLastGroup = hwnd;
1637 if (!fSkipping)
1639 /* Look for the beginning of the group */
1640 fSkipping = TRUE;
1644 if (hwnd == hwndCtrl)
1646 if (!fSkipping) break;
1647 if (hwndLastGroup == hwnd) break;
1648 hwnd = hwndLastGroup;
1649 fSkipping = FALSE;
1650 fLooped = FALSE;
1653 if (!fSkipping &&
1654 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1655 WS_VISIBLE)
1657 retvalue = hwnd;
1658 if (!fPrevious) break;
1661 end:
1662 return retvalue;
1666 /***********************************************************************
1667 * DIALOG_GetNextTabItem
1669 * Recursive helper for GetNextDlgTabItem
1671 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1673 LONG dsStyle;
1674 LONG exStyle;
1675 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1676 HWND retWnd = 0;
1677 HWND hChildFirst = 0;
1679 if(!hwndCtrl)
1681 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1682 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1684 else if (IsChild( hwndMain, hwndCtrl ))
1686 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1687 if(!hChildFirst)
1689 if(GetParent(hwndCtrl) != hwndMain)
1690 /* i.e. if we are not at the top level of the recursion */
1691 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1692 else
1693 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1697 while(hChildFirst)
1699 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1700 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1701 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1703 HWND retWnd;
1704 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1705 if (retWnd) return (retWnd);
1707 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1709 return (hChildFirst);
1711 hChildFirst = GetWindow(hChildFirst,wndSearch);
1713 if(hwndCtrl)
1715 HWND hParent = GetParent(hwndCtrl);
1716 while(hParent)
1718 if(hParent == hwndMain) break;
1719 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1720 if(retWnd) break;
1721 hParent = GetParent(hParent);
1723 if(!retWnd)
1724 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1726 return retWnd ? retWnd : hwndCtrl;
1729 /***********************************************************************
1730 * GetNextDlgTabItem (USER32.@)
1732 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1733 BOOL fPrevious )
1735 hwndDlg = WIN_GetFullHandle( hwndDlg );
1736 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1738 /* Undocumented but tested under Win2000 and WinME */
1739 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1741 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1742 * NB GetLastError returns whatever was set before the function was
1743 * called.
1745 if (!hwndCtrl && fPrevious) return 0;
1747 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1750 /**********************************************************************
1751 * DIALOG_DlgDirSelect
1753 * Helper function for DlgDirSelect*
1755 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1756 INT id, BOOL unicode, BOOL combo )
1758 WCHAR *buffer, *ptr;
1759 INT item, size;
1760 BOOL ret;
1761 HWND listbox = GetDlgItem( hwnd, id );
1763 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1764 if (!listbox) return FALSE;
1766 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1767 if (item == LB_ERR) return FALSE;
1769 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1770 if (size == LB_ERR) return FALSE;
1772 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1774 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1776 if ((ret = (buffer[0] == '['))) /* drive or directory */
1778 if (buffer[1] == '-') /* drive */
1780 buffer[3] = ':';
1781 buffer[4] = 0;
1782 ptr = buffer + 2;
1784 else
1786 buffer[strlenW(buffer)-1] = '\\';
1787 ptr = buffer + 1;
1790 else
1792 /* Filenames without a dot extension must have one tacked at the end */
1793 if (strchrW(buffer, '.') == NULL)
1795 buffer[strlenW(buffer)+1] = '\0';
1796 buffer[strlenW(buffer)] = '.';
1798 ptr = buffer;
1801 if (!unicode)
1803 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1804 ((LPSTR)str)[len-1] = 0;
1806 else lstrcpynW( str, ptr, len );
1807 HeapFree( GetProcessHeap(), 0, buffer );
1808 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1809 return ret;
1813 /**********************************************************************
1814 * DIALOG_DlgDirListW
1816 * Helper function for DlgDirList*W
1818 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1819 INT idStatic, UINT attrib, BOOL combo )
1821 HWND hwnd;
1822 LPWSTR orig_spec = spec;
1823 WCHAR any[] = {'*','.','*',0};
1824 WCHAR star[] = {'*',0};
1826 #define SENDMSG(msg,wparam,lparam) \
1827 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1828 : SendMessageW( hwnd, msg, wparam, lparam ))
1830 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1832 /* If the path exists and is a directory, chdir to it */
1833 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = star;
1834 else
1836 WCHAR *p, *p2;
1838 if (!strchrW(spec, '*') && !strchrW(spec, '?'))
1840 SetLastError(ERROR_NO_WILDCARD_CHARACTERS);
1841 return FALSE;
1843 p = spec;
1844 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1845 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1846 if ((p2 = strrchrW( p, '/' ))) p = p2;
1847 if (p != spec)
1849 WCHAR sep = *p;
1850 *p = 0;
1851 if (!SetCurrentDirectoryW( spec ))
1853 *p = sep; /* Restore the original spec */
1854 return FALSE;
1856 spec = p + 1;
1860 TRACE( "mask=%s\n", debugstr_w(spec) );
1862 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1864 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1866 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1867 if (attrib & DDL_DIRECTORY)
1869 if (!(attrib & DDL_EXCLUSIVE))
1871 SENDMSG( combo ? CB_DIR : LB_DIR,
1872 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1873 (LPARAM)spec );
1875 SENDMSG( combo ? CB_DIR : LB_DIR,
1876 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1877 (LPARAM)any );
1879 else
1881 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1885 /* Convert path specification to uppercase */
1886 if (spec) CharUpperW(spec);
1888 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1890 WCHAR temp[MAX_PATH];
1891 GetCurrentDirectoryW( ARRAY_SIZE( temp ), temp );
1892 CharLowerW( temp );
1893 /* Can't use PostMessage() here, because the string is on the stack */
1894 SetDlgItemTextW( hDlg, idStatic, temp );
1897 if (orig_spec && (spec != orig_spec))
1899 /* Update the original file spec */
1900 WCHAR *p = spec;
1901 while ((*orig_spec++ = *p++));
1904 return TRUE;
1905 #undef SENDMSG
1909 /**********************************************************************
1910 * DIALOG_DlgDirListA
1912 * Helper function for DlgDirList*A
1914 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1915 INT idStatic, UINT attrib, BOOL combo )
1917 if (spec)
1919 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1920 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1921 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1922 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1923 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1924 HeapFree( GetProcessHeap(), 0, specW );
1925 return ret;
1927 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1931 /**********************************************************************
1932 * DlgDirSelectExA (USER32.@)
1934 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1936 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1940 /**********************************************************************
1941 * DlgDirSelectExW (USER32.@)
1943 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1945 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1949 /**********************************************************************
1950 * DlgDirSelectComboBoxExA (USER32.@)
1952 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1953 INT id )
1955 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1959 /**********************************************************************
1960 * DlgDirSelectComboBoxExW (USER32.@)
1962 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1963 INT id)
1965 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1969 /**********************************************************************
1970 * DlgDirListA (USER32.@)
1972 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1973 INT idStatic, UINT attrib )
1975 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1979 /**********************************************************************
1980 * DlgDirListW (USER32.@)
1982 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1983 INT idStatic, UINT attrib )
1985 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1989 /**********************************************************************
1990 * DlgDirListComboBoxA (USER32.@)
1992 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1993 INT idStatic, UINT attrib )
1995 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1999 /**********************************************************************
2000 * DlgDirListComboBoxW (USER32.@)
2002 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2003 INT idStatic, UINT attrib )
2005 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );