netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / user32 / dialog.c
blobc48f404eceb8cefcb852788a8c154c69e3e9775a
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 <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "controls.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
42 /* Dialog control information */
43 typedef struct
45 DWORD style;
46 DWORD exStyle;
47 DWORD helpId;
48 INT16 x;
49 INT16 y;
50 INT16 cx;
51 INT16 cy;
52 UINT_PTR id;
53 LPCWSTR className;
54 LPCWSTR windowName;
55 LPCVOID data;
56 } DLG_CONTROL_INFO;
58 /* Dialog template */
59 typedef struct
61 DWORD style;
62 DWORD exStyle;
63 DWORD helpId;
64 UINT16 nbItems;
65 INT16 x;
66 INT16 y;
67 INT16 cx;
68 INT16 cy;
69 LPCWSTR menuName;
70 LPCWSTR className;
71 LPCWSTR caption;
72 INT16 pointSize;
73 WORD weight;
74 BOOL italic;
75 LPCWSTR faceName;
76 BOOL dialogEx;
77 } DLG_TEMPLATE;
79 /* Radio button group */
80 typedef struct
82 UINT firstID;
83 UINT lastID;
84 UINT checkID;
85 } RADIOGROUP;
88 /*********************************************************************
89 * dialog class descriptor
91 const struct builtin_class_descr DIALOG_builtin_class =
93 (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
94 CS_SAVEBITS | CS_DBLCLKS, /* style */
95 WINPROC_DIALOG, /* proc */
96 DLGWINDOWEXTRA, /* extra */
97 IDC_ARROW, /* cursor */
98 0 /* brush */
102 /***********************************************************************
103 * DIALOG_GetControl32
105 * Return the class and text of the control pointed to by ptr,
106 * fill the header structure and return a pointer to the next control.
108 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
109 BOOL dialogEx )
111 if (dialogEx)
113 info->helpId = GET_DWORD(p); p += 2;
114 info->exStyle = GET_DWORD(p); p += 2;
115 info->style = GET_DWORD(p); p += 2;
117 else
119 info->helpId = 0;
120 info->style = GET_DWORD(p); p += 2;
121 info->exStyle = GET_DWORD(p); p += 2;
123 info->x = GET_WORD(p); p++;
124 info->y = GET_WORD(p); p++;
125 info->cx = GET_WORD(p); p++;
126 info->cy = GET_WORD(p); p++;
128 if (dialogEx)
130 /* id is 4 bytes for DIALOGEX */
131 info->id = GET_LONG(p);
132 p += 2;
134 else
136 info->id = GET_WORD(p);
137 p++;
140 if (GET_WORD(p) == 0xffff)
142 static const WCHAR *class_names[6] =
144 L"Button", /* 0x80 */
145 L"Edit", /* 0x81 */
146 L"Static", /* 0x82 */
147 L"ListBox", /* 0x83 */
148 L"ScrollBar", /* 0x84 */
149 L"ComboBox" /* 0x85 */
151 WORD id = GET_WORD(p+1);
152 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
153 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
154 if (id <= 5)
155 info->className = class_names[id];
156 else
158 info->className = NULL;
159 ERR("Unknown built-in class id %04x\n", id );
161 p += 2;
163 else
165 info->className = p;
166 p += lstrlenW( info->className ) + 1;
169 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
171 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
172 p += 2;
174 else
176 info->windowName = p;
177 p += lstrlenW( info->windowName ) + 1;
180 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
181 debugstr_w( info->className ), debugstr_w( info->windowName ),
182 info->id, info->x, info->y, info->cx, info->cy,
183 info->style, info->exStyle, info->helpId );
185 if (GET_WORD(p))
187 if (TRACE_ON(dialog))
189 WORD i, count = GET_WORD(p) / sizeof(WORD);
190 TRACE(" BEGIN\n");
191 TRACE(" ");
192 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
193 TRACE("\n");
194 TRACE(" END\n" );
196 info->data = p;
197 p += GET_WORD(p) / sizeof(WORD);
199 else info->data = NULL;
200 p++;
202 /* Next control is on dword boundary */
203 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
207 /***********************************************************************
208 * DIALOG_CreateControls32
210 * Create the control windows for a dialog.
212 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
213 HINSTANCE hInst, BOOL unicode )
215 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
216 DLG_CONTROL_INFO info;
217 HWND hwndCtrl, hwndDefButton = 0;
218 INT items = dlgTemplate->nbItems;
220 TRACE(" BEGIN\n" );
221 while (items--)
223 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
224 dlgTemplate->dialogEx );
225 info.style &= ~WS_POPUP;
226 info.style |= WS_CHILD;
228 if (info.style & WS_BORDER)
230 info.style &= ~WS_BORDER;
231 info.exStyle |= WS_EX_CLIENTEDGE;
233 if (unicode)
235 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
236 info.className, info.windowName,
237 info.style | WS_CHILD,
238 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
239 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
240 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
241 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
242 hwnd, (HMENU)info.id,
243 hInst, (LPVOID)info.data );
245 else
247 LPCSTR class = (LPCSTR)info.className;
248 LPCSTR caption = (LPCSTR)info.windowName;
249 LPSTR class_tmp = NULL;
250 LPSTR caption_tmp = NULL;
252 if (!IS_INTRESOURCE(class))
254 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
255 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
256 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
257 class = class_tmp;
259 if (!IS_INTRESOURCE(caption))
261 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
262 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
263 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
264 caption = caption_tmp;
266 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
267 class, caption, info.style | WS_CHILD,
268 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
269 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
270 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
271 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
272 hwnd, (HMENU)info.id,
273 hInst, (LPVOID)info.data );
274 HeapFree( GetProcessHeap(), 0, class_tmp );
275 HeapFree( GetProcessHeap(), 0, caption_tmp );
277 if (!hwndCtrl)
279 WARN("control %s %s creation failed\n", debugstr_w(info.className),
280 debugstr_w(info.windowName));
281 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
282 SetLastError(0);
283 return FALSE;
286 /* Send initialisation messages to the control */
287 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
288 (WPARAM)dlgInfo->hUserFont, 0 );
289 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
291 /* If there's already a default push-button, set it back */
292 /* to normal and use this one instead. */
293 if (hwndDefButton)
294 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
295 hwndDefButton = hwndCtrl;
296 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
299 TRACE(" END\n" );
300 return TRUE;
304 /***********************************************************************
305 * DIALOG_ParseTemplate32
307 * Fill a DLG_TEMPLATE structure from the dialog template, and return
308 * a pointer to the first control.
310 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
312 const WORD *p = (const WORD *)template;
313 WORD signature;
314 WORD dlgver;
316 dlgver = GET_WORD(p); p++;
317 signature = GET_WORD(p); p++;
319 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
321 result->dialogEx = TRUE;
322 result->helpId = GET_DWORD(p); p += 2;
323 result->exStyle = GET_DWORD(p); p += 2;
324 result->style = GET_DWORD(p); p += 2;
326 else
328 result->style = GET_DWORD(p - 2);
329 result->dialogEx = FALSE;
330 result->helpId = 0;
331 result->exStyle = GET_DWORD(p); p += 2;
333 result->nbItems = GET_WORD(p); p++;
334 result->x = GET_WORD(p); p++;
335 result->y = GET_WORD(p); p++;
336 result->cx = GET_WORD(p); p++;
337 result->cy = GET_WORD(p); p++;
338 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
339 result->dialogEx ? "EX" : "", result->x, result->y,
340 result->cx, result->cy, result->helpId );
341 TRACE(" STYLE 0x%08x\n", result->style );
342 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
344 /* Get the menu name */
346 switch(GET_WORD(p))
348 case 0x0000:
349 result->menuName = NULL;
350 p++;
351 break;
352 case 0xffff:
353 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
354 p += 2;
355 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
356 break;
357 default:
358 result->menuName = p;
359 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
360 p += lstrlenW( result->menuName ) + 1;
361 break;
364 /* Get the class name */
366 switch(GET_WORD(p))
368 case 0x0000:
369 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
370 p++;
371 break;
372 case 0xffff:
373 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
374 p += 2;
375 TRACE(" CLASS %04x\n", LOWORD(result->className) );
376 break;
377 default:
378 result->className = p;
379 TRACE(" CLASS %s\n", debugstr_w( result->className ));
380 p += lstrlenW( result->className ) + 1;
381 break;
384 /* Get the window caption */
386 result->caption = p;
387 p += lstrlenW( result->caption ) + 1;
388 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
390 /* Get the font name */
392 result->pointSize = 0;
393 result->faceName = NULL;
394 result->weight = FW_DONTCARE;
395 result->italic = FALSE;
397 if (result->style & DS_SETFONT)
399 result->pointSize = GET_WORD(p);
400 p++;
402 /* If pointSize is 0x7fff, it means that we need to use the font
403 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
404 * italic, and facename from the dialog template.
406 if (result->pointSize == 0x7fff)
408 /* We could call SystemParametersInfo here, but then we'd have
409 * to convert from pixel size to point size (which can be
410 * imprecise).
412 TRACE(" FONT: Using message box font\n");
414 else
416 if (result->dialogEx)
418 result->weight = GET_WORD(p); p++;
419 result->italic = LOBYTE(GET_WORD(p)); p++;
421 result->faceName = p;
422 p += lstrlenW( result->faceName ) + 1;
424 TRACE(" FONT %d, %s, %d, %s\n",
425 result->pointSize, debugstr_w( result->faceName ),
426 result->weight, result->italic ? "TRUE" : "FALSE" );
430 /* First control is on dword boundary */
431 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
435 /***********************************************************************
436 * DIALOG_CreateIndirect
437 * Creates a dialog box window
439 * modal = TRUE if we are called from a modal dialog box.
440 * (it's more compatible to do it here, as under Windows the owner
441 * is never disabled if the dialog fails because of an invalid template)
443 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
444 HWND owner, DLGPROC dlgProc, LPARAM param,
445 BOOL unicode, HWND *modal_owner )
447 HWND hwnd;
448 RECT rect;
449 POINT pos;
450 SIZE size;
451 DLG_TEMPLATE template;
452 DIALOGINFO * dlgInfo;
453 DWORD units = GetDialogBaseUnits();
454 HWND disabled_owner = NULL;
455 HMENU hMenu = 0;
456 HFONT hUserFont = 0;
457 UINT flags = 0;
458 UINT xBaseUnit = LOWORD(units);
459 UINT yBaseUnit = HIWORD(units);
461 /* Parse dialog template */
463 if (!dlgTemplate) return 0;
464 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
466 /* Load menu */
468 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
470 /* Create custom font if needed */
472 if (template.style & DS_SETFONT)
474 HDC dc = GetDC(0);
476 if (template.pointSize == 0x7fff)
478 /* We get the message font from the non-client metrics */
479 NONCLIENTMETRICSW ncMetrics;
481 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
482 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
483 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
485 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
488 else
490 /* We convert the size to pixels and then make it -ve. This works
491 * for both +ve and -ve template.pointSize */
492 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
493 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
494 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
495 PROOF_QUALITY, FF_DONTCARE,
496 template.faceName );
499 if (hUserFont)
501 SIZE charSize;
502 HFONT hOldFont = SelectObject( dc, hUserFont );
503 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
504 if (charSize.cx)
506 xBaseUnit = charSize.cx;
507 yBaseUnit = charSize.cy;
509 SelectObject( dc, hOldFont );
511 ReleaseDC(0, dc);
512 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
515 /* Create dialog main window */
517 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
519 if (template.style & DS_CONTROL)
520 template.style &= ~(WS_CAPTION|WS_SYSMENU);
521 template.style |= DS_3DLOOK;
522 if (template.style & DS_MODALFRAME)
523 template.exStyle |= WS_EX_DLGMODALFRAME;
524 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
525 template.exStyle |= WS_EX_CONTROLPARENT;
526 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
527 pos.x = rect.left;
528 pos.y = rect.top;
529 size.cx = rect.right - rect.left;
530 size.cy = rect.bottom - rect.top;
532 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
534 pos.x = pos.y = CW_USEDEFAULT;
536 else
538 HMONITOR monitor = 0;
539 MONITORINFO mon_info;
541 mon_info.cbSize = sizeof(mon_info);
542 if (template.style & DS_CENTER)
544 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
545 GetMonitorInfoW( monitor, &mon_info );
546 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
547 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
549 else if (template.style & DS_CENTERMOUSE)
551 GetCursorPos( &pos );
552 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
553 GetMonitorInfoW( monitor, &mon_info );
555 else
557 pos.x += MulDiv(template.x, xBaseUnit, 4);
558 pos.y += MulDiv(template.y, yBaseUnit, 8);
559 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
561 if ( !(template.style & WS_CHILD) )
563 INT dX, dY;
565 /* try to fit it into the desktop */
567 if (!monitor)
569 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
570 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
571 GetMonitorInfoW( monitor, &mon_info );
573 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
574 pos.x -= dX;
575 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
576 pos.y -= dY;
577 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
578 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
582 if (modal_owner && owner)
584 HWND parent;
586 * Owner needs to be top level window. We need to duplicate the logic from server,
587 * because we need to disable it before creating dialog window. Note that we do that
588 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
589 * Windows does.
591 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
593 parent = GetParent( owner );
594 if (!parent || parent == GetDesktopWindow()) break;
595 owner = parent;
597 *modal_owner = owner;
598 if (IsWindowEnabled( owner ))
600 disabled_owner = owner;
601 EnableWindow( disabled_owner, FALSE );
605 if (unicode)
607 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
608 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
609 owner, hMenu, hInst, NULL );
611 else
613 LPCSTR class = (LPCSTR)template.className;
614 LPCSTR caption = (LPCSTR)template.caption;
615 LPSTR class_tmp = NULL;
616 LPSTR caption_tmp = NULL;
618 if (!IS_INTRESOURCE(class))
620 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
621 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
622 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
623 class = class_tmp;
625 if (!IS_INTRESOURCE(caption))
627 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
628 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
629 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
630 caption = caption_tmp;
632 hwnd = CreateWindowExA(template.exStyle, class, caption,
633 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
634 owner, hMenu, hInst, NULL );
635 HeapFree( GetProcessHeap(), 0, class_tmp );
636 HeapFree( GetProcessHeap(), 0, caption_tmp );
639 if (!hwnd)
641 if (hUserFont) DeleteObject( hUserFont );
642 if (hMenu) DestroyMenu( hMenu );
643 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
644 return 0;
647 /* moved this from the top of the method to here as DIALOGINFO structure
648 will be valid only after WM_CREATE message has been handled in DefDlgProc
649 All the members of the structure get filled here using temp variables */
650 dlgInfo = DIALOG_get_info( hwnd, TRUE );
651 dlgInfo->hwndFocus = 0;
652 dlgInfo->hUserFont = hUserFont;
653 dlgInfo->hMenu = hMenu;
654 dlgInfo->xBaseUnit = xBaseUnit;
655 dlgInfo->yBaseUnit = yBaseUnit;
656 dlgInfo->flags = flags;
658 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
660 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
661 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
663 if (dlgProc && dlgInfo->hUserFont)
664 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
666 /* Create controls */
668 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
670 HWND capture;
672 /* Send initialisation messages and set focus */
674 if (dlgProc)
676 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
677 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
678 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
679 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
681 /* By returning TRUE, app has requested a default focus assignment.
682 * WM_INITDIALOG may have changed the tab order, so find the first
683 * tabstop control again. */
684 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
685 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
686 if (focus)
688 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
689 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
690 SetFocus( focus );
692 else
694 if (!(template.style & WS_CHILD))
695 SetFocus( hwnd );
700 if (modal_owner && (capture = GetCapture()))
701 SendMessageW( capture, WM_CANCELMODE, 0, 0 );
703 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
705 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
707 return hwnd;
709 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
710 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
711 return 0;
715 /***********************************************************************
716 * CreateDialogParamA (USER32.@)
718 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
719 DLGPROC dlgProc, LPARAM param )
721 HRSRC hrsrc;
722 LPCDLGTEMPLATEA ptr;
724 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
725 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
726 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
730 /***********************************************************************
731 * CreateDialogParamW (USER32.@)
733 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
734 DLGPROC dlgProc, LPARAM param )
736 HRSRC hrsrc;
737 LPCDLGTEMPLATEA ptr;
739 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
740 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
741 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
745 /***********************************************************************
746 * CreateDialogIndirectParamAorW (USER32.@)
748 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
749 HWND owner, DLGPROC dlgProc, LPARAM param,
750 DWORD flags )
752 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
755 /***********************************************************************
756 * CreateDialogIndirectParamA (USER32.@)
758 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
759 HWND owner, DLGPROC dlgProc, LPARAM param )
761 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
764 /***********************************************************************
765 * CreateDialogIndirectParamW (USER32.@)
767 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
768 HWND owner, DLGPROC dlgProc, LPARAM param )
770 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
774 /***********************************************************************
775 * DIALOG_DoDialogBox
777 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
779 DIALOGINFO * dlgInfo;
780 MSG msg;
781 INT retval;
782 BOOL bFirstEmpty;
784 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
786 bFirstEmpty = TRUE;
787 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
789 for (;;)
791 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
793 if (bFirstEmpty)
795 /* ShowWindow the first time the queue goes empty */
796 ShowWindow( hwnd, SW_SHOWNORMAL );
797 bFirstEmpty = FALSE;
799 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
801 /* No message present -> send ENTERIDLE and wait */
802 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
804 GetMessageW( &msg, 0, 0, 0 );
807 if (msg.message == WM_QUIT)
809 PostQuitMessage( msg.wParam );
810 if (!IsWindow( hwnd )) return 0;
811 break;
813 if (!IsWindow( hwnd )) return 0;
814 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
816 TranslateMessage( &msg );
817 DispatchMessageW( &msg );
819 if (!IsWindow( hwnd )) return 0;
820 if (dlgInfo->flags & DF_END) break;
822 if (bFirstEmpty && msg.message == WM_TIMER)
824 ShowWindow( hwnd, SW_SHOWNORMAL );
825 bFirstEmpty = FALSE;
829 retval = dlgInfo->idResult;
830 DestroyWindow( hwnd );
831 return retval;
835 /***********************************************************************
836 * DialogBoxParamA (USER32.@)
838 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
839 HWND owner, DLGPROC dlgProc, LPARAM param )
841 HWND hwnd;
842 HRSRC hrsrc;
843 LPCDLGTEMPLATEA ptr;
845 if (owner && !IsWindow(owner)) return 0;
847 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
848 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
849 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner ))) return -1;
850 return DIALOG_DoDialogBox( hwnd, owner );
854 /***********************************************************************
855 * DialogBoxParamW (USER32.@)
857 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
858 HWND owner, DLGPROC dlgProc, LPARAM param )
860 HWND hwnd;
861 HRSRC hrsrc;
862 LPCDLGTEMPLATEW ptr;
864 if (owner && !IsWindow(owner)) return 0;
866 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
867 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
868 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner ))) return -1;
869 return DIALOG_DoDialogBox( hwnd, owner );
873 /***********************************************************************
874 * DialogBoxIndirectParamAorW (USER32.@)
876 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
877 HWND owner, DLGPROC dlgProc,
878 LPARAM param, DWORD flags )
880 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
881 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
882 return -1;
885 /***********************************************************************
886 * DialogBoxIndirectParamA (USER32.@)
888 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
889 HWND owner, DLGPROC dlgProc, LPARAM param )
891 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
895 /***********************************************************************
896 * DialogBoxIndirectParamW (USER32.@)
898 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
899 HWND owner, DLGPROC dlgProc, LPARAM param )
901 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
904 /***********************************************************************
905 * EndDialog (USER32.@)
907 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
909 DIALOGINFO * dlgInfo;
910 HWND owner;
912 TRACE("%p %ld\n", hwnd, retval );
914 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
916 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
917 return FALSE;
919 dlgInfo->idResult = retval;
920 dlgInfo->flags |= DF_END;
922 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
923 if (owner)
924 EnableWindow( owner, TRUE );
926 /* Windows sets the focus to the dialog itself in EndDialog */
928 if (IsChild(hwnd, GetFocus()))
929 SetFocus( hwnd );
931 /* Don't have to send a ShowWindow(SW_HIDE), just do
932 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
934 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
935 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
937 if (hwnd == GetActiveWindow())
939 /* If this dialog was given an owner then set the focus to that owner. */
940 if (owner)
941 SetForegroundWindow( owner );
942 else
943 WINPOS_ActivateOtherWindow( hwnd );
946 /* unblock dialog loop */
947 PostMessageA(hwnd, WM_NULL, 0, 0);
948 return TRUE;
952 /***********************************************************************
953 * DIALOG_IsAccelerator
955 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
957 HWND hwndControl = hwnd;
958 HWND hwndNext;
959 INT dlgCode;
960 WCHAR buffer[128];
964 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
965 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
967 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
968 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
969 GetWindowTextW( hwndControl, buffer, ARRAY_SIZE( buffer )))
971 /* find the accelerator key */
972 LPWSTR p = buffer - 2;
976 p = wcschr( p + 2, '&' );
978 while (p != NULL && p[1] == '&');
980 /* and check if it's the one we're looking for */
981 if (p != NULL && towupper( p[1] ) == towupper( wParam ) )
983 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
985 /* set focus to the control */
986 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
987 /* and bump it on to next */
988 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
990 else if (dlgCode & DLGC_BUTTON)
992 /* send BM_CLICK message to the control */
993 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
995 return TRUE;
998 hwndNext = GetWindow( hwndControl, GW_CHILD );
1000 else hwndNext = 0;
1002 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1004 while (!hwndNext && hwndControl)
1006 hwndControl = GetParent( hwndControl );
1007 if (hwndControl == hwndDlg)
1009 if(hwnd==hwndDlg) /* prevent endless loop */
1011 hwndNext=hwnd;
1012 break;
1014 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1016 else
1017 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1019 hwndControl = hwndNext;
1021 while (hwndControl && (hwndControl != hwnd));
1023 return FALSE;
1026 /***********************************************************************
1027 * DIALOG_FindMsgDestination
1029 * The messages that IsDialogMessage sends may not go to the dialog
1030 * calling IsDialogMessage if that dialog is a child, and it has the
1031 * DS_CONTROL style set.
1032 * We propagate up until we hit one that does not have DS_CONTROL, or
1033 * whose parent is not a dialog.
1035 * This is undocumented behaviour.
1037 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1039 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1041 WND *pParent;
1042 HWND hParent = GetParent(hwndDlg);
1043 if (!hParent) break;
1045 pParent = WIN_GetPtr(hParent);
1046 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1048 if (!pParent->dlgInfo)
1050 WIN_ReleasePtr(pParent);
1051 break;
1053 WIN_ReleasePtr(pParent);
1055 hwndDlg = hParent;
1058 return hwndDlg;
1061 /***********************************************************************
1062 * DIALOG_FixOneChildOnChangeFocus
1064 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1067 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1068 LPARAM lParam)
1070 /* If a default pushbutton then no longer default */
1071 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1072 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1073 return TRUE;
1076 /***********************************************************************
1077 * DIALOG_FixChildrenOnChangeFocus
1079 * Following the change of focus that occurs for example after handling
1080 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1081 * children may be required.
1083 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1085 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1086 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1087 /* Windows does ask for this. I don't know why yet */
1089 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1091 /* If the button that is getting the focus WAS flagged as the default
1092 * pushbutton then ask the dialog what it thinks the default is and
1093 * set that in the default style.
1095 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1097 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1098 if (HIWORD(def_id) == DC_HASDEFID)
1100 HWND hwndDef;
1101 def_id = LOWORD(def_id);
1102 hwndDef = GetDlgItem (hwndDlg, def_id);
1103 if (hwndDef)
1105 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1106 /* I know that if it is a button then it should already be a
1107 * UNDEFPUSHBUTTON, since we have just told the buttons to
1108 * change style. But maybe they ignored our request
1110 if ((dlgcode_def & DLGC_BUTTON) &&
1111 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1113 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1118 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1120 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1121 /* I wonder why it doesn't send a DM_SETDEFID */
1125 /***********************************************************************
1126 * DIALOG_IdToHwnd
1128 * A recursive version of GetDlgItem
1130 * RETURNS
1131 * The HWND for a Child ID.
1133 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1135 int i;
1136 HWND *list = WIN_ListChildren( hwndDlg );
1137 HWND ret = 0;
1139 if (!list) return 0;
1141 for (i = 0; list[i]; i++)
1143 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1145 ret = list[i];
1146 break;
1149 /* Recurse into every child */
1150 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1153 HeapFree( GetProcessHeap(), 0, list );
1154 return ret;
1157 /***********************************************************************
1158 * IsDialogMessageW (USER32.@)
1160 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1162 INT dlgCode;
1164 if (!IsWindow( hwndDlg ))
1165 return FALSE;
1167 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1169 hwndDlg = WIN_GetFullHandle( hwndDlg );
1170 if (is_desktop_window(hwndDlg)) return FALSE;
1171 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1173 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1175 switch(msg->message)
1177 case WM_KEYDOWN:
1178 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1179 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1181 switch(msg->wParam)
1183 case VK_TAB:
1184 if (!(dlgCode & DLGC_WANTTAB))
1186 BOOL fIsDialog = TRUE;
1187 WND *pWnd = WIN_GetPtr( hwndDlg );
1189 if (pWnd && pWnd != WND_OTHER_PROCESS)
1191 fIsDialog = (pWnd->dlgInfo != NULL);
1192 WIN_ReleasePtr(pWnd);
1195 /* I am not sure under which circumstances the TAB is handled
1196 * each way. All I do know is that it does not always simply
1197 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1198 * do so but I presume someone has)
1200 if (fIsDialog)
1201 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1202 else
1204 /* It would appear that GetNextDlgTabItem can handle being
1205 * passed hwndDlg rather than NULL but that is undocumented
1206 * so let's do it properly
1208 HWND hwndFocus = GetFocus();
1209 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1210 hwndFocus == hwndDlg ? NULL : hwndFocus,
1211 GetKeyState (VK_SHIFT) & 0x8000);
1212 if (hwndNext)
1214 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1215 msg->wParam, (LPARAM)msg);
1216 if (dlgCode & DLGC_HASSETSEL)
1218 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1219 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1220 if (buffer)
1222 INT length;
1223 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1224 length = lstrlenW (buffer);
1225 HeapFree (GetProcessHeap(), 0, buffer);
1226 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1229 SetFocus (hwndNext);
1230 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1232 else
1233 return FALSE;
1235 return TRUE;
1237 break;
1239 case VK_RIGHT:
1240 case VK_DOWN:
1241 case VK_LEFT:
1242 case VK_UP:
1243 if (!(dlgCode & DLGC_WANTARROWS))
1245 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1246 HWND hwndNext = GetNextDlgGroupItem( hwndDlg, msg->hwnd, fPrevious );
1247 if (hwndNext && SendMessageW( hwndNext, WM_GETDLGCODE, msg->wParam, (LPARAM)msg ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
1249 SetFocus( hwndNext );
1250 if ((GetWindowLongW( hwndNext, GWL_STYLE ) & BS_TYPEMASK) == BS_AUTORADIOBUTTON &&
1251 SendMessageW( hwndNext, BM_GETCHECK, 0, 0 ) != BST_CHECKED)
1252 SendMessageW( hwndNext, BM_CLICK, 1, 0 );
1254 else
1255 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1256 return TRUE;
1258 break;
1260 case VK_CANCEL:
1261 case VK_ESCAPE:
1262 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1263 return TRUE;
1265 case VK_EXECUTE:
1266 case VK_RETURN:
1268 DWORD dw;
1269 HWND hwndFocus = GetFocus();
1270 if (IsChild( hwndDlg, hwndFocus ) &&
1271 (SendMessageW( hwndFocus, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON))
1273 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( GetDlgCtrlID( hwndFocus ), BN_CLICKED ), (LPARAM)hwndFocus );
1275 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1277 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1278 if (!hwndDef || IsWindowEnabled(hwndDef))
1279 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1281 else
1283 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1286 return TRUE;
1288 break;
1290 case WM_CHAR:
1291 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1292 * It does NOT get sent in the test program I have
1294 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1295 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1296 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1297 /* drop through */
1299 case WM_SYSCHAR:
1300 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1302 /* don't translate or dispatch */
1303 return TRUE;
1305 break;
1308 TranslateMessage( msg );
1309 DispatchMessageW( msg );
1310 return TRUE;
1314 /***********************************************************************
1315 * GetDlgCtrlID (USER32.@)
1317 INT WINAPI GetDlgCtrlID( HWND hwnd )
1319 return GetWindowLongPtrW( hwnd, GWLP_ID );
1323 /***********************************************************************
1324 * GetDlgItem (USER32.@)
1326 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1328 int i;
1329 HWND *list = WIN_ListChildren( hwndDlg );
1330 HWND ret = 0;
1332 if (!list) return 0;
1334 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1335 ret = list[i];
1336 HeapFree( GetProcessHeap(), 0, list );
1337 return ret;
1341 /*******************************************************************
1342 * SendDlgItemMessageA (USER32.@)
1344 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1345 WPARAM wParam, LPARAM lParam )
1347 HWND hwndCtrl = GetDlgItem( hwnd, id );
1348 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1349 else return 0;
1353 /*******************************************************************
1354 * SendDlgItemMessageW (USER32.@)
1356 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1357 WPARAM wParam, LPARAM lParam )
1359 HWND hwndCtrl = GetDlgItem( hwnd, id );
1360 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1361 else return 0;
1365 /*******************************************************************
1366 * SetDlgItemTextA (USER32.@)
1368 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1370 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1374 /*******************************************************************
1375 * SetDlgItemTextW (USER32.@)
1377 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1379 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1383 /***********************************************************************
1384 * GetDlgItemTextA (USER32.@)
1386 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1388 if (str && (len > 0)) str[0] = '\0';
1389 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1390 len, (LPARAM)str );
1394 /***********************************************************************
1395 * GetDlgItemTextW (USER32.@)
1397 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1399 if (str && (len > 0)) str[0] = '\0';
1400 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1401 len, (LPARAM)str );
1405 /*******************************************************************
1406 * SetDlgItemInt (USER32.@)
1408 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1409 BOOL fSigned )
1411 char str[20];
1413 if (fSigned) sprintf( str, "%d", (INT)value );
1414 else sprintf( str, "%u", value );
1415 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1416 return TRUE;
1420 /***********************************************************************
1421 * GetDlgItemInt (USER32.@)
1423 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1424 BOOL fSigned )
1426 char str[30];
1427 char * endptr;
1428 LONG_PTR result = 0;
1430 if (translated) *translated = FALSE;
1431 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1432 return 0;
1433 if (fSigned)
1435 result = strtol( str, &endptr, 10 );
1436 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1437 return 0;
1438 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1439 return 0;
1441 else
1443 result = strtoul( str, &endptr, 10 );
1444 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1445 return 0;
1446 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1448 if (translated) *translated = TRUE;
1449 return (UINT)result;
1453 /***********************************************************************
1454 * CheckDlgButton (USER32.@)
1456 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1458 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1459 return TRUE;
1463 /***********************************************************************
1464 * IsDlgButtonChecked (USER32.@)
1466 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1468 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1472 /***********************************************************************
1473 * CheckRB
1475 * Callback function used to check/uncheck radio buttons that fall
1476 * within a specific range of IDs.
1478 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1480 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1481 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1483 if ((lChildID >= lpRadioGroup->firstID) &&
1484 (lChildID <= lpRadioGroup->lastID))
1486 if (lChildID == lpRadioGroup->checkID)
1488 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1490 else
1492 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1496 return TRUE;
1500 /***********************************************************************
1501 * CheckRadioButton (USER32.@)
1503 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1504 int lastID, int checkID )
1506 RADIOGROUP radioGroup;
1508 radioGroup.firstID = firstID;
1509 radioGroup.lastID = lastID;
1510 radioGroup.checkID = checkID;
1512 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1516 /***********************************************************************
1517 * GetDialogBaseUnits (USER.243)
1518 * GetDialogBaseUnits (USER32.@)
1520 DWORD WINAPI GetDialogBaseUnits(void)
1522 static LONG cx, cy;
1524 if (!cx)
1526 HDC hdc;
1528 if ((hdc = GetDC(0)))
1530 cx = GdiGetCharDimensions( hdc, NULL, &cy );
1531 ReleaseDC( 0, hdc );
1533 TRACE( "base units = %d,%d\n", cx, cy );
1536 return MAKELONG( MulDiv( cx, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI ),
1537 MulDiv( cy, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI ));
1541 /***********************************************************************
1542 * MapDialogRect (USER32.@)
1544 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1546 DIALOGINFO * dlgInfo;
1547 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1548 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1549 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1550 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1551 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1552 return TRUE;
1556 /***********************************************************************
1557 * GetNextDlgGroupItem (USER32.@)
1559 * Corrections to MSDN documentation
1561 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1562 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1563 * 2. Prev of NULL or hwndDlg fails
1565 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1567 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1568 BOOL fLooped=FALSE;
1569 BOOL fSkipping=FALSE;
1571 hwndDlg = WIN_GetFullHandle( hwndDlg );
1572 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1574 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1575 if (!hwndCtrl && fPrevious) return 0;
1577 if (hwndCtrl)
1579 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1581 else
1583 /* No ctrl specified -> start from the beginning */
1584 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1585 /* MSDN is wrong. fPrevious does not result in the last child */
1587 /* Maybe that first one is valid. If so then we don't want to skip it*/
1588 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1590 return hwndCtrl;
1594 /* Always go forward around the group and list of controls; for the
1595 * previous control keep track; for the next break when you find one
1597 retvalue = hwndCtrl;
1598 hwnd = hwndCtrl;
1599 while (1)
1601 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1602 while (!hwndNext)
1604 /* Climb out until there is a next sibling of the ancestor or we
1605 * reach the top (in which case we loop back to the start)
1607 if (hwndDlg == GetParent (hwnd))
1609 /* Wrap around to the beginning of the list, within the same
1610 * group. (Once only)
1612 if (fLooped) goto end;
1613 fLooped = TRUE;
1614 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1616 else
1618 hwnd = GetParent (hwnd);
1619 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1622 hwnd = hwndNext;
1624 /* Wander down the leading edge of controlparents */
1625 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1626 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1627 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1628 hwnd = hwndNext;
1629 /* Question. If the control is a control parent but either has no
1630 * children or is not visible/enabled then if it has a WS_GROUP does
1631 * it count? For that matter does it count anyway?
1632 * I believe it doesn't count.
1635 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1637 hwndLastGroup = hwnd;
1638 if (!fSkipping)
1640 /* Look for the beginning of the group */
1641 fSkipping = TRUE;
1645 if (hwnd == hwndCtrl)
1647 if (!fSkipping) break;
1648 if (hwndLastGroup == hwnd) break;
1649 hwnd = hwndLastGroup;
1650 fSkipping = FALSE;
1651 fLooped = FALSE;
1654 if (!fSkipping &&
1655 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1656 WS_VISIBLE)
1658 retvalue = hwnd;
1659 if (!fPrevious) break;
1662 end:
1663 return retvalue;
1667 /***********************************************************************
1668 * DIALOG_GetNextTabItem
1670 * Recursive helper for GetNextDlgTabItem
1672 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1674 LONG dsStyle;
1675 LONG exStyle;
1676 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1677 HWND retWnd = 0;
1678 HWND hChildFirst = 0;
1680 if(!hwndCtrl)
1682 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1683 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1685 else if (IsChild( hwndMain, hwndCtrl ))
1687 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1688 if(!hChildFirst)
1690 if(GetParent(hwndCtrl) != hwndMain)
1691 /* i.e. if we are not at the top level of the recursion */
1692 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1693 else
1694 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1698 while(hChildFirst)
1700 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1701 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1702 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1704 HWND retWnd;
1705 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1706 if (retWnd) return (retWnd);
1708 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1710 return (hChildFirst);
1712 hChildFirst = GetWindow(hChildFirst,wndSearch);
1714 if(hwndCtrl)
1716 HWND hParent = GetParent(hwndCtrl);
1717 while(hParent)
1719 if(hParent == hwndMain) break;
1720 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1721 if(retWnd) break;
1722 hParent = GetParent(hParent);
1724 if(!retWnd)
1725 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1727 return retWnd ? retWnd : hwndCtrl;
1730 /***********************************************************************
1731 * GetNextDlgTabItem (USER32.@)
1733 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1734 BOOL fPrevious )
1736 hwndDlg = WIN_GetFullHandle( hwndDlg );
1737 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1739 /* Undocumented but tested under Win2000 and WinME */
1740 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1742 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1743 * NB GetLastError returns whatever was set before the function was
1744 * called.
1746 if (!hwndCtrl && fPrevious) return 0;
1748 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1751 /**********************************************************************
1752 * DIALOG_DlgDirSelect
1754 * Helper function for DlgDirSelect*
1756 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1757 INT id, BOOL unicode, BOOL combo )
1759 WCHAR *buffer, *ptr;
1760 INT item, size;
1761 BOOL ret;
1762 HWND listbox = GetDlgItem( hwnd, id );
1764 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1765 if (!listbox) return FALSE;
1767 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1768 if (item == LB_ERR) return FALSE;
1770 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1771 if (size == LB_ERR) return FALSE;
1773 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1775 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1777 if ((ret = (buffer[0] == '['))) /* drive or directory */
1779 if (buffer[1] == '-') /* drive */
1781 buffer[3] = ':';
1782 buffer[4] = 0;
1783 ptr = buffer + 2;
1785 else
1787 buffer[lstrlenW(buffer)-1] = '\\';
1788 ptr = buffer + 1;
1791 else
1793 /* Filenames without a dot extension must have one tacked at the end */
1794 if (wcschr(buffer, '.') == NULL)
1796 buffer[lstrlenW(buffer)+1] = '\0';
1797 buffer[lstrlenW(buffer)] = '.';
1799 ptr = buffer;
1802 if (!unicode)
1804 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1805 ((LPSTR)str)[len-1] = 0;
1807 else lstrcpynW( str, ptr, len );
1808 HeapFree( GetProcessHeap(), 0, buffer );
1809 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1810 return ret;
1814 /**********************************************************************
1815 * DIALOG_DlgDirListW
1817 * Helper function for DlgDirList*W
1819 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1820 INT idStatic, UINT attrib, BOOL combo )
1822 HWND hwnd;
1823 LPWSTR orig_spec = spec;
1824 WCHAR any[] = L"*.*";
1825 WCHAR star[] = L"*";
1827 #define SENDMSG(msg,wparam,lparam) \
1828 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1829 : SendMessageW( hwnd, msg, wparam, lparam ))
1831 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1833 /* If the path exists and is a directory, chdir to it */
1834 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = star;
1835 else
1837 WCHAR *p, *p2;
1839 if (!wcschr(spec, '*') && !wcschr(spec, '?'))
1841 SetLastError(ERROR_NO_WILDCARD_CHARACTERS);
1842 return FALSE;
1844 p = spec;
1845 if ((p2 = wcschr( p, ':' ))) p = p2 + 1;
1846 if ((p2 = wcsrchr( p, '\\' ))) p = p2;
1847 if ((p2 = wcsrchr( p, '/' ))) p = p2;
1848 if (p != spec)
1850 WCHAR sep = *p;
1851 *p = 0;
1852 if (!SetCurrentDirectoryW( spec ))
1854 *p = sep; /* Restore the original spec */
1855 return FALSE;
1857 spec = p + 1;
1861 TRACE( "mask=%s\n", debugstr_w(spec) );
1863 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1865 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1867 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1868 if (attrib & DDL_DIRECTORY)
1870 if (!(attrib & DDL_EXCLUSIVE))
1872 SENDMSG( combo ? CB_DIR : LB_DIR,
1873 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1874 (LPARAM)spec );
1876 SENDMSG( combo ? CB_DIR : LB_DIR,
1877 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1878 (LPARAM)any );
1880 else
1882 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1886 /* Convert path specification to uppercase */
1887 if (spec) CharUpperW(spec);
1889 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1891 WCHAR temp[MAX_PATH];
1892 GetCurrentDirectoryW( ARRAY_SIZE( temp ), temp );
1893 CharLowerW( temp );
1894 /* Can't use PostMessage() here, because the string is on the stack */
1895 SetDlgItemTextW( hDlg, idStatic, temp );
1898 if (orig_spec && (spec != orig_spec))
1900 /* Update the original file spec */
1901 WCHAR *p = spec;
1902 while ((*orig_spec++ = *p++));
1905 return TRUE;
1906 #undef SENDMSG
1910 /**********************************************************************
1911 * DIALOG_DlgDirListA
1913 * Helper function for DlgDirList*A
1915 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1916 INT idStatic, UINT attrib, BOOL combo )
1918 if (spec)
1920 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1921 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1922 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1923 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1924 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1925 HeapFree( GetProcessHeap(), 0, specW );
1926 return ret;
1928 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1932 /**********************************************************************
1933 * DlgDirSelectExA (USER32.@)
1935 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1937 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1941 /**********************************************************************
1942 * DlgDirSelectExW (USER32.@)
1944 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1946 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1950 /**********************************************************************
1951 * DlgDirSelectComboBoxExA (USER32.@)
1953 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1954 INT id )
1956 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1960 /**********************************************************************
1961 * DlgDirSelectComboBoxExW (USER32.@)
1963 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1964 INT id)
1966 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1970 /**********************************************************************
1971 * DlgDirListA (USER32.@)
1973 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1974 INT idStatic, UINT attrib )
1976 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1980 /**********************************************************************
1981 * DlgDirListW (USER32.@)
1983 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1984 INT idStatic, UINT attrib )
1986 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1990 /**********************************************************************
1991 * DlgDirListComboBoxA (USER32.@)
1993 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1994 INT idStatic, UINT attrib )
1996 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2000 /**********************************************************************
2001 * DlgDirListComboBoxW (USER32.@)
2003 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2004 INT idStatic, UINT attrib )
2006 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );