user32: Removed unused owner argument.
[wine.git] / dlls / user32 / dialog.c
blob3ea426d606b486e6194d6a316f4ab4422881cff2
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 a DWORD for DIALOGEX */
135 info->id = GET_DWORD(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 + 1;
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 return FALSE;
289 /* Send initialisation messages to the control */
290 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
291 (WPARAM)dlgInfo->hUserFont, 0 );
292 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
294 /* If there's already a default push-button, set it back */
295 /* to normal and use this one instead. */
296 if (hwndDefButton)
297 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
298 hwndDefButton = hwndCtrl;
299 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
302 TRACE(" END\n" );
303 return TRUE;
307 /***********************************************************************
308 * DIALOG_ParseTemplate32
310 * Fill a DLG_TEMPLATE structure from the dialog template, and return
311 * a pointer to the first control.
313 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
315 const WORD *p = (const WORD *)template;
316 WORD signature;
317 WORD dlgver;
319 dlgver = GET_WORD(p); p++;
320 signature = GET_WORD(p); p++;
322 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
324 result->dialogEx = TRUE;
325 result->helpId = GET_DWORD(p); p += 2;
326 result->exStyle = GET_DWORD(p); p += 2;
327 result->style = GET_DWORD(p); p += 2;
329 else
331 result->style = GET_DWORD(p - 2);
332 result->dialogEx = FALSE;
333 result->helpId = 0;
334 result->exStyle = GET_DWORD(p); p += 2;
336 result->nbItems = GET_WORD(p); p++;
337 result->x = GET_WORD(p); p++;
338 result->y = GET_WORD(p); p++;
339 result->cx = GET_WORD(p); p++;
340 result->cy = GET_WORD(p); p++;
341 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
342 result->dialogEx ? "EX" : "", result->x, result->y,
343 result->cx, result->cy, result->helpId );
344 TRACE(" STYLE 0x%08x\n", result->style );
345 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
347 /* Get the menu name */
349 switch(GET_WORD(p))
351 case 0x0000:
352 result->menuName = NULL;
353 p++;
354 break;
355 case 0xffff:
356 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
357 p += 2;
358 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
359 break;
360 default:
361 result->menuName = p;
362 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
363 p += strlenW( result->menuName ) + 1;
364 break;
367 /* Get the class name */
369 switch(GET_WORD(p))
371 case 0x0000:
372 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
373 p++;
374 break;
375 case 0xffff:
376 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
377 p += 2;
378 TRACE(" CLASS %04x\n", LOWORD(result->className) );
379 break;
380 default:
381 result->className = p;
382 TRACE(" CLASS %s\n", debugstr_w( result->className ));
383 p += strlenW( result->className ) + 1;
384 break;
387 /* Get the window caption */
389 result->caption = p;
390 p += strlenW( result->caption ) + 1;
391 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
393 /* Get the font name */
395 result->pointSize = 0;
396 result->faceName = NULL;
397 result->weight = FW_DONTCARE;
398 result->italic = FALSE;
400 if (result->style & DS_SETFONT)
402 result->pointSize = GET_WORD(p);
403 p++;
405 /* If pointSize is 0x7fff, it means that we need to use the font
406 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
407 * italic, and facename from the dialog template.
409 if (result->pointSize == 0x7fff)
411 /* We could call SystemParametersInfo here, but then we'd have
412 * to convert from pixel size to point size (which can be
413 * imprecise).
415 TRACE(" FONT: Using message box font\n");
417 else
419 if (result->dialogEx)
421 result->weight = GET_WORD(p); p++;
422 result->italic = LOBYTE(GET_WORD(p)); p++;
424 result->faceName = p;
425 p += strlenW( result->faceName ) + 1;
427 TRACE(" FONT %d, %s, %d, %s\n",
428 result->pointSize, debugstr_w( result->faceName ),
429 result->weight, result->italic ? "TRUE" : "FALSE" );
433 /* First control is on dword boundary */
434 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
438 /***********************************************************************
439 * DIALOG_CreateIndirect
440 * Creates a dialog box window
442 * modal = TRUE if we are called from a modal dialog box.
443 * (it's more compatible to do it here, as under Windows the owner
444 * is never disabled if the dialog fails because of an invalid template)
446 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
447 HWND owner, DLGPROC dlgProc, LPARAM param,
448 BOOL unicode, BOOL modal )
450 HWND hwnd;
451 RECT rect;
452 POINT pos;
453 SIZE size;
454 DLG_TEMPLATE template;
455 DIALOGINFO * dlgInfo;
456 DWORD units = GetDialogBaseUnits();
457 HWND disabled_owner = NULL;
458 HMENU hMenu = 0;
459 HFONT hUserFont = 0;
460 UINT flags = 0;
461 UINT xBaseUnit = LOWORD(units);
462 UINT yBaseUnit = HIWORD(units);
464 /* Parse dialog template */
466 if (!dlgTemplate) return 0;
467 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
469 /* Load menu */
471 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
473 /* Create custom font if needed */
475 if (template.style & DS_SETFONT)
477 HDC dc = GetDC(0);
479 if (template.pointSize == 0x7fff)
481 /* We get the message font from the non-client metrics */
482 NONCLIENTMETRICSW ncMetrics;
484 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
485 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
486 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
488 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
491 else
493 /* We convert the size to pixels and then make it -ve. This works
494 * for both +ve and -ve template.pointSize */
495 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
496 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
497 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
498 PROOF_QUALITY, FF_DONTCARE,
499 template.faceName );
502 if (hUserFont)
504 SIZE charSize;
505 HFONT hOldFont = SelectObject( dc, hUserFont );
506 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
507 if (charSize.cx)
509 xBaseUnit = charSize.cx;
510 yBaseUnit = charSize.cy;
512 SelectObject( dc, hOldFont );
514 ReleaseDC(0, dc);
515 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
518 /* Create dialog main window */
520 rect.left = rect.top = 0;
521 rect.right = MulDiv(template.cx, xBaseUnit, 4);
522 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
524 if (template.style & DS_CONTROL)
525 template.style &= ~(WS_CAPTION|WS_SYSMENU);
526 template.style |= DS_3DLOOK;
527 if (template.style & DS_MODALFRAME)
528 template.exStyle |= WS_EX_DLGMODALFRAME;
529 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
530 template.exStyle |= WS_EX_CONTROLPARENT;
531 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
532 pos.x = rect.left;
533 pos.y = rect.top;
534 size.cx = rect.right - rect.left;
535 size.cy = rect.bottom - rect.top;
537 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
539 pos.x = pos.y = CW_USEDEFAULT;
541 else
543 HMONITOR monitor = 0;
544 MONITORINFO mon_info;
546 mon_info.cbSize = sizeof(mon_info);
547 if (template.style & DS_CENTER)
549 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
550 GetMonitorInfoW( monitor, &mon_info );
551 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
552 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
554 else if (template.style & DS_CENTERMOUSE)
556 GetCursorPos( &pos );
557 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
558 GetMonitorInfoW( monitor, &mon_info );
560 else
562 pos.x += MulDiv(template.x, xBaseUnit, 4);
563 pos.y += MulDiv(template.y, yBaseUnit, 8);
564 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
566 if ( !(template.style & WS_CHILD) )
568 INT dX, dY;
570 /* try to fit it into the desktop */
572 if (!monitor)
574 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
575 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
576 GetMonitorInfoW( monitor, &mon_info );
578 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
579 pos.x -= dX;
580 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
581 pos.y -= dY;
582 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
583 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
587 if (modal && owner)
589 HWND parent;
590 disabled_owner = owner;
592 * Owner needs to be top level window. We need to duplicate the logic from server,
593 * because we need to disable it before creating dialog window.
595 while ((GetWindowLongW( disabled_owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
597 parent = GetParent( disabled_owner );
598 if (!parent || parent == GetDesktopWindow()) break;
599 disabled_owner = parent;
601 if (IsWindowEnabled( disabled_owner ))
603 flags |= DF_OWNERENABLED;
604 EnableWindow( disabled_owner, FALSE );
606 else
607 disabled_owner = NULL;
610 if (unicode)
612 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
613 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
614 owner, hMenu, hInst, NULL );
616 else
618 LPCSTR class = (LPCSTR)template.className;
619 LPCSTR caption = (LPCSTR)template.caption;
620 LPSTR class_tmp = NULL;
621 LPSTR caption_tmp = NULL;
623 if (!IS_INTRESOURCE(class))
625 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
626 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
627 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
628 class = class_tmp;
630 if (!IS_INTRESOURCE(caption))
632 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
633 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
634 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
635 caption = caption_tmp;
637 hwnd = CreateWindowExA(template.exStyle, class, caption,
638 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
639 owner, hMenu, hInst, NULL );
640 HeapFree( GetProcessHeap(), 0, class_tmp );
641 HeapFree( GetProcessHeap(), 0, caption_tmp );
644 if (!hwnd)
646 if (hUserFont) DeleteObject( hUserFont );
647 if (hMenu) DestroyMenu( hMenu );
648 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
649 return 0;
652 /* moved this from the top of the method to here as DIALOGINFO structure
653 will be valid only after WM_CREATE message has been handled in DefDlgProc
654 All the members of the structure get filled here using temp variables */
655 dlgInfo = DIALOG_get_info( hwnd, TRUE );
656 dlgInfo->hwndFocus = 0;
657 dlgInfo->hUserFont = hUserFont;
658 dlgInfo->hMenu = hMenu;
659 dlgInfo->xBaseUnit = xBaseUnit;
660 dlgInfo->yBaseUnit = yBaseUnit;
661 dlgInfo->flags = flags;
663 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
665 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
666 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
668 if (dlgProc && dlgInfo->hUserFont)
669 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
671 /* Create controls */
673 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
675 /* Send initialisation messages and set focus */
677 if (dlgProc)
679 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
680 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
681 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
682 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
684 /* By returning TRUE, app has requested a default focus assignment.
685 * WM_INITDIALOG may have changed the tab order, so find the first
686 * tabstop control again. */
687 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
688 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
689 if (focus)
691 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
692 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
693 SetFocus( focus );
698 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
700 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
702 return hwnd;
704 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
705 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
706 return 0;
710 /***********************************************************************
711 * CreateDialogParamA (USER32.@)
713 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
714 DLGPROC dlgProc, LPARAM param )
716 HRSRC hrsrc;
717 LPCDLGTEMPLATEA ptr;
719 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
720 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
721 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
725 /***********************************************************************
726 * CreateDialogParamW (USER32.@)
728 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
729 DLGPROC dlgProc, LPARAM param )
731 HRSRC hrsrc;
732 LPCDLGTEMPLATEA ptr;
734 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
735 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
736 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
740 /***********************************************************************
741 * CreateDialogIndirectParamAorW (USER32.@)
743 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
744 HWND owner, DLGPROC dlgProc, LPARAM param,
745 DWORD flags )
747 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
750 /***********************************************************************
751 * CreateDialogIndirectParamA (USER32.@)
753 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
754 HWND owner, DLGPROC dlgProc, LPARAM param )
756 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
759 /***********************************************************************
760 * CreateDialogIndirectParamW (USER32.@)
762 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
763 HWND owner, DLGPROC dlgProc, LPARAM param )
765 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
769 /***********************************************************************
770 * DIALOG_DoDialogBox
772 INT DIALOG_DoDialogBox( HWND hwnd )
774 HWND owner = GetWindow( hwnd, GW_OWNER );
775 DIALOGINFO * dlgInfo;
776 MSG msg;
777 INT retval;
778 BOOL bFirstEmpty;
780 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
782 bFirstEmpty = TRUE;
783 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
785 for (;;)
787 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
789 if (bFirstEmpty)
791 /* ShowWindow the first time the queue goes empty */
792 ShowWindow( hwnd, SW_SHOWNORMAL );
793 bFirstEmpty = FALSE;
795 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
797 /* No message present -> send ENTERIDLE and wait */
798 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
800 GetMessageW( &msg, 0, 0, 0 );
803 if (msg.message == WM_QUIT)
805 PostQuitMessage( msg.wParam );
806 if (!IsWindow( hwnd )) return 0;
807 break;
809 if (!IsWindow( hwnd )) return 0;
810 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
812 TranslateMessage( &msg );
813 DispatchMessageW( &msg );
815 if (!IsWindow( hwnd )) return 0;
816 if (dlgInfo->flags & DF_END) break;
818 if (bFirstEmpty && msg.message == WM_TIMER)
820 ShowWindow( hwnd, SW_SHOWNORMAL );
821 bFirstEmpty = FALSE;
825 if (dlgInfo->flags & DF_OWNERENABLED) EnableWindow( owner, TRUE );
826 retval = dlgInfo->idResult;
827 DestroyWindow( hwnd );
828 return retval;
832 /***********************************************************************
833 * DialogBoxParamA (USER32.@)
835 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
836 HWND owner, DLGPROC dlgProc, LPARAM param )
838 HWND hwnd;
839 HRSRC hrsrc;
840 LPCDLGTEMPLATEA ptr;
842 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
843 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
844 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
845 if (hwnd) return DIALOG_DoDialogBox( hwnd );
846 return 0;
850 /***********************************************************************
851 * DialogBoxParamW (USER32.@)
853 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
854 HWND owner, DLGPROC dlgProc, LPARAM param )
856 HWND hwnd;
857 HRSRC hrsrc;
858 LPCDLGTEMPLATEW ptr;
860 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
861 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
862 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
863 if (hwnd) return DIALOG_DoDialogBox( hwnd );
864 return 0;
868 /***********************************************************************
869 * DialogBoxIndirectParamAorW (USER32.@)
871 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
872 HWND owner, DLGPROC dlgProc,
873 LPARAM param, DWORD flags )
875 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
876 if (hwnd) return DIALOG_DoDialogBox( hwnd );
877 return -1;
880 /***********************************************************************
881 * DialogBoxIndirectParamA (USER32.@)
883 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
884 HWND owner, DLGPROC dlgProc, LPARAM param )
886 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
890 /***********************************************************************
891 * DialogBoxIndirectParamW (USER32.@)
893 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
894 HWND owner, DLGPROC dlgProc, LPARAM param )
896 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
899 /***********************************************************************
900 * EndDialog (USER32.@)
902 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
904 DIALOGINFO * dlgInfo;
905 HWND owner;
907 TRACE("%p %ld\n", hwnd, retval );
909 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
911 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
912 return FALSE;
914 dlgInfo->idResult = retval;
915 dlgInfo->flags |= DF_END;
917 owner = GetWindow( hwnd, GW_OWNER );
918 if (owner)
919 EnableWindow( owner, TRUE );
921 /* Windows sets the focus to the dialog itself in EndDialog */
923 if (IsChild(hwnd, GetFocus()))
924 SetFocus( hwnd );
926 /* Don't have to send a ShowWindow(SW_HIDE), just do
927 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
929 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
930 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
932 if (hwnd == GetActiveWindow())
934 /* If this dialog was given an owner then set the focus to that owner. */
935 if (owner)
936 SetForegroundWindow( owner );
937 else
938 WINPOS_ActivateOtherWindow( hwnd );
941 /* unblock dialog loop */
942 PostMessageA(hwnd, WM_NULL, 0, 0);
943 return TRUE;
947 /***********************************************************************
948 * DIALOG_IsAccelerator
950 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
952 HWND hwndControl = hwnd;
953 HWND hwndNext;
954 INT dlgCode;
955 WCHAR buffer[128];
959 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
960 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
962 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
963 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
964 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
966 /* find the accelerator key */
967 LPWSTR p = buffer - 2;
971 p = strchrW( p + 2, '&' );
973 while (p != NULL && p[1] == '&');
975 /* and check if it's the one we're looking for */
976 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
978 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
980 /* set focus to the control */
981 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
982 /* and bump it on to next */
983 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
985 else if (dlgCode & DLGC_BUTTON)
987 /* send BM_CLICK message to the control */
988 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
990 return TRUE;
993 hwndNext = GetWindow( hwndControl, GW_CHILD );
995 else hwndNext = 0;
997 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
999 while (!hwndNext && hwndControl)
1001 hwndControl = GetParent( hwndControl );
1002 if (hwndControl == hwndDlg)
1004 if(hwnd==hwndDlg) /* prevent endless loop */
1006 hwndNext=hwnd;
1007 break;
1009 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1011 else
1012 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1014 hwndControl = hwndNext;
1016 while (hwndControl && (hwndControl != hwnd));
1018 return FALSE;
1021 /***********************************************************************
1022 * DIALOG_FindMsgDestination
1024 * The messages that IsDialogMessage sends may not go to the dialog
1025 * calling IsDialogMessage if that dialog is a child, and it has the
1026 * DS_CONTROL style set.
1027 * We propagate up until we hit one that does not have DS_CONTROL, or
1028 * whose parent is not a dialog.
1030 * This is undocumented behaviour.
1032 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1034 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1036 WND *pParent;
1037 HWND hParent = GetParent(hwndDlg);
1038 if (!hParent) break;
1040 pParent = WIN_GetPtr(hParent);
1041 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1043 if (!pParent->dlgInfo)
1045 WIN_ReleasePtr(pParent);
1046 break;
1048 WIN_ReleasePtr(pParent);
1050 hwndDlg = hParent;
1053 return hwndDlg;
1056 /***********************************************************************
1057 * DIALOG_FixOneChildOnChangeFocus
1059 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1062 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1063 LPARAM lParam)
1065 /* If a default pushbutton then no longer default */
1066 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1067 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1068 return TRUE;
1071 /***********************************************************************
1072 * DIALOG_FixChildrenOnChangeFocus
1074 * Following the change of focus that occurs for example after handling
1075 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1076 * children may be required.
1078 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1080 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1081 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1082 /* Windows does ask for this. I don't know why yet */
1084 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1086 /* If the button that is getting the focus WAS flagged as the default
1087 * pushbutton then ask the dialog what it thinks the default is and
1088 * set that in the default style.
1090 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1092 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1093 if (HIWORD(def_id) == DC_HASDEFID)
1095 HWND hwndDef;
1096 def_id = LOWORD(def_id);
1097 hwndDef = GetDlgItem (hwndDlg, def_id);
1098 if (hwndDef)
1100 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1101 /* I know that if it is a button then it should already be a
1102 * UNDEFPUSHBUTTON, since we have just told the buttons to
1103 * change style. But maybe they ignored our request
1105 if ((dlgcode_def & DLGC_BUTTON) &&
1106 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1108 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1113 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1115 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1116 /* I wonder why it doesn't send a DM_SETDEFID */
1120 /***********************************************************************
1121 * DIALOG_IdToHwnd
1123 * A recursive version of GetDlgItem
1125 * RETURNS
1126 * The HWND for a Child ID.
1128 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1130 int i;
1131 HWND *list = WIN_ListChildren( hwndDlg );
1132 HWND ret = 0;
1134 if (!list) return 0;
1136 for (i = 0; list[i]; i++)
1138 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1140 ret = list[i];
1141 break;
1144 /* Recurse into every child */
1145 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1148 HeapFree( GetProcessHeap(), 0, list );
1149 return ret;
1152 /***********************************************************************
1153 * IsDialogMessageW (USER32.@)
1155 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1157 INT dlgCode;
1159 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1161 hwndDlg = WIN_GetFullHandle( hwndDlg );
1162 if (is_desktop_window(hwndDlg)) return FALSE;
1163 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1165 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1167 switch(msg->message)
1169 case WM_KEYDOWN:
1170 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1171 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1173 switch(msg->wParam)
1175 case VK_TAB:
1176 if (!(dlgCode & DLGC_WANTTAB))
1178 BOOL fIsDialog = TRUE;
1179 WND *pWnd = WIN_GetPtr( hwndDlg );
1181 if (pWnd && pWnd != WND_OTHER_PROCESS)
1183 fIsDialog = (pWnd->dlgInfo != NULL);
1184 WIN_ReleasePtr(pWnd);
1187 /* I am not sure under which circumstances the TAB is handled
1188 * each way. All I do know is that it does not always simply
1189 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1190 * do so but I presume someone has)
1192 if (fIsDialog)
1193 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1194 else
1196 /* It would appear that GetNextDlgTabItem can handle being
1197 * passed hwndDlg rather than NULL but that is undocumented
1198 * so let's do it properly
1200 HWND hwndFocus = GetFocus();
1201 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1202 hwndFocus == hwndDlg ? NULL : hwndFocus,
1203 GetKeyState (VK_SHIFT) & 0x8000);
1204 if (hwndNext)
1206 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1207 msg->wParam, (LPARAM)msg);
1208 if (dlgCode & DLGC_HASSETSEL)
1210 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1211 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1212 if (buffer)
1214 INT length;
1215 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1216 length = strlenW (buffer);
1217 HeapFree (GetProcessHeap(), 0, buffer);
1218 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1221 SetFocus (hwndNext);
1222 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1224 else
1225 return FALSE;
1227 return TRUE;
1229 break;
1231 case VK_RIGHT:
1232 case VK_DOWN:
1233 case VK_LEFT:
1234 case VK_UP:
1235 if (!(dlgCode & DLGC_WANTARROWS))
1237 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1238 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1239 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1240 return TRUE;
1242 break;
1244 case VK_CANCEL:
1245 case VK_ESCAPE:
1246 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1247 return TRUE;
1249 case VK_EXECUTE:
1250 case VK_RETURN:
1252 DWORD dw;
1253 if ((GetFocus() == msg->hwnd) &&
1254 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1256 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1258 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1260 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1261 if (!hwndDef || IsWindowEnabled(hwndDef))
1262 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1264 else
1266 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1270 return TRUE;
1272 break;
1274 case WM_CHAR:
1275 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1276 * It does NOT get sent in the test program I have
1278 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1279 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1280 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1281 /* drop through */
1283 case WM_SYSCHAR:
1284 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1286 /* don't translate or dispatch */
1287 return TRUE;
1289 break;
1292 TranslateMessage( msg );
1293 DispatchMessageW( msg );
1294 return TRUE;
1298 /***********************************************************************
1299 * GetDlgCtrlID (USER32.@)
1301 INT WINAPI GetDlgCtrlID( HWND hwnd )
1303 return GetWindowLongPtrW( hwnd, GWLP_ID );
1307 /***********************************************************************
1308 * GetDlgItem (USER32.@)
1310 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1312 int i;
1313 HWND *list = WIN_ListChildren( hwndDlg );
1314 HWND ret = 0;
1316 if (!list) return 0;
1318 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1319 ret = list[i];
1320 HeapFree( GetProcessHeap(), 0, list );
1321 return ret;
1325 /*******************************************************************
1326 * SendDlgItemMessageA (USER32.@)
1328 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1329 WPARAM wParam, LPARAM lParam )
1331 HWND hwndCtrl = GetDlgItem( hwnd, id );
1332 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1333 else return 0;
1337 /*******************************************************************
1338 * SendDlgItemMessageW (USER32.@)
1340 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1341 WPARAM wParam, LPARAM lParam )
1343 HWND hwndCtrl = GetDlgItem( hwnd, id );
1344 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1345 else return 0;
1349 /*******************************************************************
1350 * SetDlgItemTextA (USER32.@)
1352 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1354 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1358 /*******************************************************************
1359 * SetDlgItemTextW (USER32.@)
1361 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1363 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1367 /***********************************************************************
1368 * GetDlgItemTextA (USER32.@)
1370 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1372 if (str && (len > 0)) str[0] = '\0';
1373 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1374 len, (LPARAM)str );
1378 /***********************************************************************
1379 * GetDlgItemTextW (USER32.@)
1381 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1383 if (str && (len > 0)) str[0] = '\0';
1384 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1385 len, (LPARAM)str );
1389 /*******************************************************************
1390 * SetDlgItemInt (USER32.@)
1392 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1393 BOOL fSigned )
1395 char str[20];
1397 if (fSigned) sprintf( str, "%d", (INT)value );
1398 else sprintf( str, "%u", value );
1399 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1400 return TRUE;
1404 /***********************************************************************
1405 * GetDlgItemInt (USER32.@)
1407 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1408 BOOL fSigned )
1410 char str[30];
1411 char * endptr;
1412 LONG_PTR result = 0;
1414 if (translated) *translated = FALSE;
1415 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1416 return 0;
1417 if (fSigned)
1419 result = strtol( str, &endptr, 10 );
1420 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1421 return 0;
1422 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1423 return 0;
1425 else
1427 result = strtoul( str, &endptr, 10 );
1428 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1429 return 0;
1430 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1432 if (translated) *translated = TRUE;
1433 return (UINT)result;
1437 /***********************************************************************
1438 * CheckDlgButton (USER32.@)
1440 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1442 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1443 return TRUE;
1447 /***********************************************************************
1448 * IsDlgButtonChecked (USER32.@)
1450 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1452 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1456 /***********************************************************************
1457 * CheckRB
1459 * Callback function used to check/uncheck radio buttons that fall
1460 * within a specific range of IDs.
1462 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1464 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1465 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1467 if ((lChildID >= lpRadioGroup->firstID) &&
1468 (lChildID <= lpRadioGroup->lastID))
1470 if (lChildID == lpRadioGroup->checkID)
1472 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1474 else
1476 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1480 return TRUE;
1484 /***********************************************************************
1485 * CheckRadioButton (USER32.@)
1487 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1488 int lastID, int checkID )
1490 RADIOGROUP radioGroup;
1492 radioGroup.firstID = firstID;
1493 radioGroup.lastID = lastID;
1494 radioGroup.checkID = checkID;
1496 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1500 /***********************************************************************
1501 * GetDialogBaseUnits (USER.243)
1502 * GetDialogBaseUnits (USER32.@)
1504 DWORD WINAPI GetDialogBaseUnits(void)
1506 static DWORD units;
1508 if (!units)
1510 HDC hdc;
1511 SIZE size;
1513 if ((hdc = GetDC(0)))
1515 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1516 if (size.cx) units = MAKELONG( size.cx, size.cy );
1517 ReleaseDC( 0, hdc );
1519 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1521 return units;
1525 /***********************************************************************
1526 * MapDialogRect (USER32.@)
1528 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1530 DIALOGINFO * dlgInfo;
1531 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1532 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1533 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1534 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1535 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1536 return TRUE;
1540 /***********************************************************************
1541 * GetNextDlgGroupItem (USER32.@)
1543 * Corrections to MSDN documentation
1545 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1546 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1547 * 2. Prev of NULL or hwndDlg fails
1549 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1551 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1552 BOOL fLooped=FALSE;
1553 BOOL fSkipping=FALSE;
1555 hwndDlg = WIN_GetFullHandle( hwndDlg );
1556 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1558 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1559 if (!hwndCtrl && fPrevious) return 0;
1561 if (hwndCtrl)
1563 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1565 else
1567 /* No ctrl specified -> start from the beginning */
1568 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1569 /* MSDN is wrong. fPrevious does not result in the last child */
1571 /* Maybe that first one is valid. If so then we don't want to skip it*/
1572 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1574 return hwndCtrl;
1578 /* Always go forward around the group and list of controls; for the
1579 * previous control keep track; for the next break when you find one
1581 retvalue = hwndCtrl;
1582 hwnd = hwndCtrl;
1583 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1586 while (!hwndNext)
1588 /* Climb out until there is a next sibling of the ancestor or we
1589 * reach the top (in which case we loop back to the start)
1591 if (hwndDlg == GetParent (hwnd))
1593 /* Wrap around to the beginning of the list, within the same
1594 * group. (Once only)
1596 if (fLooped) goto end;
1597 fLooped = TRUE;
1598 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1600 else
1602 hwnd = GetParent (hwnd);
1603 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1606 hwnd = hwndNext;
1608 /* Wander down the leading edge of controlparents */
1609 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1610 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1611 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1612 hwnd = hwndNext;
1613 /* Question. If the control is a control parent but either has no
1614 * children or is not visible/enabled then if it has a WS_GROUP does
1615 * it count? For that matter does it count anyway?
1616 * I believe it doesn't count.
1619 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1621 hwndLastGroup = hwnd;
1622 if (!fSkipping)
1624 /* Look for the beginning of the group */
1625 fSkipping = TRUE;
1629 if (hwnd == hwndCtrl)
1631 if (!fSkipping) break;
1632 if (hwndLastGroup == hwnd) break;
1633 hwnd = hwndLastGroup;
1634 fSkipping = FALSE;
1635 fLooped = FALSE;
1638 if (!fSkipping &&
1639 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1640 WS_VISIBLE)
1642 retvalue = hwnd;
1643 if (!fPrevious) break;
1646 end:
1647 return retvalue;
1651 /***********************************************************************
1652 * DIALOG_GetNextTabItem
1654 * Recursive helper for GetNextDlgTabItem
1656 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1658 LONG dsStyle;
1659 LONG exStyle;
1660 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1661 HWND retWnd = 0;
1662 HWND hChildFirst = 0;
1664 if(!hwndCtrl)
1666 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1667 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1669 else if (IsChild( hwndMain, hwndCtrl ))
1671 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1672 if(!hChildFirst)
1674 if(GetParent(hwndCtrl) != hwndMain)
1675 /* i.e. if we are not at the top level of the recursion */
1676 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1677 else
1678 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1682 while(hChildFirst)
1684 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1685 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1686 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1688 HWND retWnd;
1689 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1690 if (retWnd) return (retWnd);
1692 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1694 return (hChildFirst);
1696 hChildFirst = GetWindow(hChildFirst,wndSearch);
1698 if(hwndCtrl)
1700 HWND hParent = GetParent(hwndCtrl);
1701 while(hParent)
1703 if(hParent == hwndMain) break;
1704 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1705 if(retWnd) break;
1706 hParent = GetParent(hParent);
1708 if(!retWnd)
1709 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1711 return retWnd ? retWnd : hwndCtrl;
1714 /***********************************************************************
1715 * GetNextDlgTabItem (USER32.@)
1717 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1718 BOOL fPrevious )
1720 hwndDlg = WIN_GetFullHandle( hwndDlg );
1721 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1723 /* Undocumented but tested under Win2000 and WinME */
1724 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1726 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1727 * NB GetLastError returns whatever was set before the function was
1728 * called.
1730 if (!hwndCtrl && fPrevious) return 0;
1732 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1735 /**********************************************************************
1736 * DIALOG_DlgDirSelect
1738 * Helper function for DlgDirSelect*
1740 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1741 INT id, BOOL unicode, BOOL combo )
1743 WCHAR *buffer, *ptr;
1744 INT item, size;
1745 BOOL ret;
1746 HWND listbox = GetDlgItem( hwnd, id );
1748 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1749 if (!listbox) return FALSE;
1751 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1752 if (item == LB_ERR) return FALSE;
1754 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1755 if (size == LB_ERR) return FALSE;
1757 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1759 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1761 if ((ret = (buffer[0] == '['))) /* drive or directory */
1763 if (buffer[1] == '-') /* drive */
1765 buffer[3] = ':';
1766 buffer[4] = 0;
1767 ptr = buffer + 2;
1769 else
1771 buffer[strlenW(buffer)-1] = '\\';
1772 ptr = buffer + 1;
1775 else
1777 /* Filenames without a dot extension must have one tacked at the end */
1778 if (strchrW(buffer, '.') == NULL)
1780 buffer[strlenW(buffer)+1] = '\0';
1781 buffer[strlenW(buffer)] = '.';
1783 ptr = buffer;
1786 if (!unicode)
1788 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1789 ((LPSTR)str)[len-1] = 0;
1791 else lstrcpynW( str, ptr, len );
1792 HeapFree( GetProcessHeap(), 0, buffer );
1793 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1794 return ret;
1798 /**********************************************************************
1799 * DIALOG_DlgDirListW
1801 * Helper function for DlgDirList*W
1803 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1804 INT idStatic, UINT attrib, BOOL combo )
1806 HWND hwnd;
1807 LPWSTR orig_spec = spec;
1808 WCHAR any[] = {'*','.','*',0};
1810 #define SENDMSG(msg,wparam,lparam) \
1811 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1812 : SendMessageW( hwnd, msg, wparam, lparam ))
1814 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1816 /* If the path exists and is a directory, chdir to it */
1817 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1818 else
1820 WCHAR *p, *p2;
1821 p = spec;
1822 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1823 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1824 if ((p2 = strrchrW( p, '/' ))) p = p2;
1825 if (p != spec)
1827 WCHAR sep = *p;
1828 *p = 0;
1829 if (!SetCurrentDirectoryW( spec ))
1831 *p = sep; /* Restore the original spec */
1832 return FALSE;
1834 spec = p + 1;
1838 TRACE( "mask=%s\n", debugstr_w(spec) );
1840 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1842 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1844 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1845 if (attrib & DDL_DIRECTORY)
1847 if (!(attrib & DDL_EXCLUSIVE))
1849 SENDMSG( combo ? CB_DIR : LB_DIR,
1850 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1851 (LPARAM)spec );
1853 SENDMSG( combo ? CB_DIR : LB_DIR,
1854 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1855 (LPARAM)any );
1857 else
1859 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1863 /* Convert path specification to uppercase */
1864 if (spec) CharUpperW(spec);
1866 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1868 WCHAR temp[MAX_PATH];
1869 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1870 CharLowerW( temp );
1871 /* Can't use PostMessage() here, because the string is on the stack */
1872 SetDlgItemTextW( hDlg, idStatic, temp );
1875 if (orig_spec && (spec != orig_spec))
1877 /* Update the original file spec */
1878 WCHAR *p = spec;
1879 while ((*orig_spec++ = *p++));
1882 return TRUE;
1883 #undef SENDMSG
1887 /**********************************************************************
1888 * DIALOG_DlgDirListA
1890 * Helper function for DlgDirList*A
1892 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1893 INT idStatic, UINT attrib, BOOL combo )
1895 if (spec)
1897 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1898 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1899 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1900 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1901 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1902 HeapFree( GetProcessHeap(), 0, specW );
1903 return ret;
1905 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1909 /**********************************************************************
1910 * DlgDirSelectExA (USER32.@)
1912 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1914 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1918 /**********************************************************************
1919 * DlgDirSelectExW (USER32.@)
1921 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1923 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1927 /**********************************************************************
1928 * DlgDirSelectComboBoxExA (USER32.@)
1930 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1931 INT id )
1933 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1937 /**********************************************************************
1938 * DlgDirSelectComboBoxExW (USER32.@)
1940 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1941 INT id)
1943 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1947 /**********************************************************************
1948 * DlgDirListA (USER32.@)
1950 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1951 INT idStatic, UINT attrib )
1953 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1957 /**********************************************************************
1958 * DlgDirListW (USER32.@)
1960 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1961 INT idStatic, UINT attrib )
1963 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1967 /**********************************************************************
1968 * DlgDirListComboBoxA (USER32.@)
1970 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1971 INT idStatic, UINT attrib )
1973 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1977 /**********************************************************************
1978 * DlgDirListComboBoxW (USER32.@)
1980 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1981 INT idStatic, UINT attrib )
1983 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );