shlwapi: Fix a string leak (Valgrind).
[wine.git] / dlls / user32 / dialog.c
blob9cb4b8d90dc15fd3330a47c423ff11316fe4c138
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, HWND *modal_owner )
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 && owner)
589 HWND parent;
591 * Owner needs to be top level window. We need to duplicate the logic from server,
592 * because we need to disable it before creating dialog window. Note that we do that
593 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
594 * Windows does.
596 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
598 parent = GetParent( owner );
599 if (!parent || parent == GetDesktopWindow()) break;
600 owner = parent;
602 *modal_owner = owner;
603 if (IsWindowEnabled( owner ))
605 disabled_owner = owner;
606 EnableWindow( disabled_owner, FALSE );
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, HWND owner )
774 DIALOGINFO * dlgInfo;
775 MSG msg;
776 INT retval;
777 BOOL bFirstEmpty;
779 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
781 bFirstEmpty = TRUE;
782 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
784 for (;;)
786 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
788 if (bFirstEmpty)
790 /* ShowWindow the first time the queue goes empty */
791 ShowWindow( hwnd, SW_SHOWNORMAL );
792 bFirstEmpty = FALSE;
794 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
796 /* No message present -> send ENTERIDLE and wait */
797 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
799 GetMessageW( &msg, 0, 0, 0 );
802 if (msg.message == WM_QUIT)
804 PostQuitMessage( msg.wParam );
805 if (!IsWindow( hwnd )) return 0;
806 break;
808 if (!IsWindow( hwnd )) return 0;
809 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
811 TranslateMessage( &msg );
812 DispatchMessageW( &msg );
814 if (!IsWindow( hwnd )) return 0;
815 if (dlgInfo->flags & DF_END) break;
817 if (bFirstEmpty && msg.message == WM_TIMER)
819 ShowWindow( hwnd, SW_SHOWNORMAL );
820 bFirstEmpty = FALSE;
824 retval = dlgInfo->idResult;
825 DestroyWindow( hwnd );
826 return retval;
830 /***********************************************************************
831 * DialogBoxParamA (USER32.@)
833 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
834 HWND owner, DLGPROC dlgProc, LPARAM param )
836 HWND hwnd;
837 HRSRC hrsrc;
838 LPCDLGTEMPLATEA ptr;
840 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
841 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
842 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner );
843 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
844 return 0;
848 /***********************************************************************
849 * DialogBoxParamW (USER32.@)
851 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
852 HWND owner, DLGPROC dlgProc, LPARAM param )
854 HWND hwnd;
855 HRSRC hrsrc;
856 LPCDLGTEMPLATEW ptr;
858 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
859 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
860 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner );
861 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
862 return 0;
866 /***********************************************************************
867 * DialogBoxIndirectParamAorW (USER32.@)
869 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
870 HWND owner, DLGPROC dlgProc,
871 LPARAM param, DWORD flags )
873 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
874 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
875 return -1;
878 /***********************************************************************
879 * DialogBoxIndirectParamA (USER32.@)
881 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
882 HWND owner, DLGPROC dlgProc, LPARAM param )
884 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
888 /***********************************************************************
889 * DialogBoxIndirectParamW (USER32.@)
891 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
892 HWND owner, DLGPROC dlgProc, LPARAM param )
894 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
897 /***********************************************************************
898 * EndDialog (USER32.@)
900 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
902 DIALOGINFO * dlgInfo;
903 HWND owner;
905 TRACE("%p %ld\n", hwnd, retval );
907 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
909 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
910 return FALSE;
912 dlgInfo->idResult = retval;
913 dlgInfo->flags |= DF_END;
915 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
916 if (owner)
917 EnableWindow( owner, TRUE );
919 /* Windows sets the focus to the dialog itself in EndDialog */
921 if (IsChild(hwnd, GetFocus()))
922 SetFocus( hwnd );
924 /* Don't have to send a ShowWindow(SW_HIDE), just do
925 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
927 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
928 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
930 if (hwnd == GetActiveWindow())
932 /* If this dialog was given an owner then set the focus to that owner. */
933 if (owner)
934 SetForegroundWindow( owner );
935 else
936 WINPOS_ActivateOtherWindow( hwnd );
939 /* unblock dialog loop */
940 PostMessageA(hwnd, WM_NULL, 0, 0);
941 return TRUE;
945 /***********************************************************************
946 * DIALOG_IsAccelerator
948 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
950 HWND hwndControl = hwnd;
951 HWND hwndNext;
952 INT dlgCode;
953 WCHAR buffer[128];
957 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
958 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
960 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
961 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
962 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
964 /* find the accelerator key */
965 LPWSTR p = buffer - 2;
969 p = strchrW( p + 2, '&' );
971 while (p != NULL && p[1] == '&');
973 /* and check if it's the one we're looking for */
974 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
976 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
978 /* set focus to the control */
979 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
980 /* and bump it on to next */
981 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
983 else if (dlgCode & DLGC_BUTTON)
985 /* send BM_CLICK message to the control */
986 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
988 return TRUE;
991 hwndNext = GetWindow( hwndControl, GW_CHILD );
993 else hwndNext = 0;
995 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
997 while (!hwndNext && hwndControl)
999 hwndControl = GetParent( hwndControl );
1000 if (hwndControl == hwndDlg)
1002 if(hwnd==hwndDlg) /* prevent endless loop */
1004 hwndNext=hwnd;
1005 break;
1007 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1009 else
1010 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1012 hwndControl = hwndNext;
1014 while (hwndControl && (hwndControl != hwnd));
1016 return FALSE;
1019 /***********************************************************************
1020 * DIALOG_FindMsgDestination
1022 * The messages that IsDialogMessage sends may not go to the dialog
1023 * calling IsDialogMessage if that dialog is a child, and it has the
1024 * DS_CONTROL style set.
1025 * We propagate up until we hit one that does not have DS_CONTROL, or
1026 * whose parent is not a dialog.
1028 * This is undocumented behaviour.
1030 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1032 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1034 WND *pParent;
1035 HWND hParent = GetParent(hwndDlg);
1036 if (!hParent) break;
1038 pParent = WIN_GetPtr(hParent);
1039 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1041 if (!pParent->dlgInfo)
1043 WIN_ReleasePtr(pParent);
1044 break;
1046 WIN_ReleasePtr(pParent);
1048 hwndDlg = hParent;
1051 return hwndDlg;
1054 /***********************************************************************
1055 * DIALOG_FixOneChildOnChangeFocus
1057 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1060 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1061 LPARAM lParam)
1063 /* If a default pushbutton then no longer default */
1064 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1065 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1066 return TRUE;
1069 /***********************************************************************
1070 * DIALOG_FixChildrenOnChangeFocus
1072 * Following the change of focus that occurs for example after handling
1073 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1074 * children may be required.
1076 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1078 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1079 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1080 /* Windows does ask for this. I don't know why yet */
1082 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1084 /* If the button that is getting the focus WAS flagged as the default
1085 * pushbutton then ask the dialog what it thinks the default is and
1086 * set that in the default style.
1088 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1090 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1091 if (HIWORD(def_id) == DC_HASDEFID)
1093 HWND hwndDef;
1094 def_id = LOWORD(def_id);
1095 hwndDef = GetDlgItem (hwndDlg, def_id);
1096 if (hwndDef)
1098 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1099 /* I know that if it is a button then it should already be a
1100 * UNDEFPUSHBUTTON, since we have just told the buttons to
1101 * change style. But maybe they ignored our request
1103 if ((dlgcode_def & DLGC_BUTTON) &&
1104 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1106 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1111 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1113 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1114 /* I wonder why it doesn't send a DM_SETDEFID */
1118 /***********************************************************************
1119 * DIALOG_IdToHwnd
1121 * A recursive version of GetDlgItem
1123 * RETURNS
1124 * The HWND for a Child ID.
1126 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1128 int i;
1129 HWND *list = WIN_ListChildren( hwndDlg );
1130 HWND ret = 0;
1132 if (!list) return 0;
1134 for (i = 0; list[i]; i++)
1136 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1138 ret = list[i];
1139 break;
1142 /* Recurse into every child */
1143 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1146 HeapFree( GetProcessHeap(), 0, list );
1147 return ret;
1150 /***********************************************************************
1151 * IsDialogMessageW (USER32.@)
1153 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1155 INT dlgCode;
1157 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1159 hwndDlg = WIN_GetFullHandle( hwndDlg );
1160 if (is_desktop_window(hwndDlg)) return FALSE;
1161 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1163 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1165 switch(msg->message)
1167 case WM_KEYDOWN:
1168 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1169 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1171 switch(msg->wParam)
1173 case VK_TAB:
1174 if (!(dlgCode & DLGC_WANTTAB))
1176 BOOL fIsDialog = TRUE;
1177 WND *pWnd = WIN_GetPtr( hwndDlg );
1179 if (pWnd && pWnd != WND_OTHER_PROCESS)
1181 fIsDialog = (pWnd->dlgInfo != NULL);
1182 WIN_ReleasePtr(pWnd);
1185 /* I am not sure under which circumstances the TAB is handled
1186 * each way. All I do know is that it does not always simply
1187 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1188 * do so but I presume someone has)
1190 if (fIsDialog)
1191 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1192 else
1194 /* It would appear that GetNextDlgTabItem can handle being
1195 * passed hwndDlg rather than NULL but that is undocumented
1196 * so let's do it properly
1198 HWND hwndFocus = GetFocus();
1199 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1200 hwndFocus == hwndDlg ? NULL : hwndFocus,
1201 GetKeyState (VK_SHIFT) & 0x8000);
1202 if (hwndNext)
1204 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1205 msg->wParam, (LPARAM)msg);
1206 if (dlgCode & DLGC_HASSETSEL)
1208 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1209 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1210 if (buffer)
1212 INT length;
1213 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1214 length = strlenW (buffer);
1215 HeapFree (GetProcessHeap(), 0, buffer);
1216 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1219 SetFocus (hwndNext);
1220 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1222 else
1223 return FALSE;
1225 return TRUE;
1227 break;
1229 case VK_RIGHT:
1230 case VK_DOWN:
1231 case VK_LEFT:
1232 case VK_UP:
1233 if (!(dlgCode & DLGC_WANTARROWS))
1235 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1236 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1237 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1238 return TRUE;
1240 break;
1242 case VK_CANCEL:
1243 case VK_ESCAPE:
1244 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1245 return TRUE;
1247 case VK_EXECUTE:
1248 case VK_RETURN:
1250 DWORD dw;
1251 if ((GetFocus() == msg->hwnd) &&
1252 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1254 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1256 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1258 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1259 if (!hwndDef || IsWindowEnabled(hwndDef))
1260 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1262 else
1264 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1268 return TRUE;
1270 break;
1272 case WM_CHAR:
1273 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1274 * It does NOT get sent in the test program I have
1276 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1277 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1278 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1279 /* drop through */
1281 case WM_SYSCHAR:
1282 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1284 /* don't translate or dispatch */
1285 return TRUE;
1287 break;
1290 TranslateMessage( msg );
1291 DispatchMessageW( msg );
1292 return TRUE;
1296 /***********************************************************************
1297 * GetDlgCtrlID (USER32.@)
1299 INT WINAPI GetDlgCtrlID( HWND hwnd )
1301 return GetWindowLongPtrW( hwnd, GWLP_ID );
1305 /***********************************************************************
1306 * GetDlgItem (USER32.@)
1308 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1310 int i;
1311 HWND *list = WIN_ListChildren( hwndDlg );
1312 HWND ret = 0;
1314 if (!list) return 0;
1316 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1317 ret = list[i];
1318 HeapFree( GetProcessHeap(), 0, list );
1319 return ret;
1323 /*******************************************************************
1324 * SendDlgItemMessageA (USER32.@)
1326 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1327 WPARAM wParam, LPARAM lParam )
1329 HWND hwndCtrl = GetDlgItem( hwnd, id );
1330 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1331 else return 0;
1335 /*******************************************************************
1336 * SendDlgItemMessageW (USER32.@)
1338 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1339 WPARAM wParam, LPARAM lParam )
1341 HWND hwndCtrl = GetDlgItem( hwnd, id );
1342 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1343 else return 0;
1347 /*******************************************************************
1348 * SetDlgItemTextA (USER32.@)
1350 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1352 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1356 /*******************************************************************
1357 * SetDlgItemTextW (USER32.@)
1359 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1361 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1365 /***********************************************************************
1366 * GetDlgItemTextA (USER32.@)
1368 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1370 if (str && (len > 0)) str[0] = '\0';
1371 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1372 len, (LPARAM)str );
1376 /***********************************************************************
1377 * GetDlgItemTextW (USER32.@)
1379 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1381 if (str && (len > 0)) str[0] = '\0';
1382 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1383 len, (LPARAM)str );
1387 /*******************************************************************
1388 * SetDlgItemInt (USER32.@)
1390 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1391 BOOL fSigned )
1393 char str[20];
1395 if (fSigned) sprintf( str, "%d", (INT)value );
1396 else sprintf( str, "%u", value );
1397 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1398 return TRUE;
1402 /***********************************************************************
1403 * GetDlgItemInt (USER32.@)
1405 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1406 BOOL fSigned )
1408 char str[30];
1409 char * endptr;
1410 LONG_PTR result = 0;
1412 if (translated) *translated = FALSE;
1413 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1414 return 0;
1415 if (fSigned)
1417 result = strtol( str, &endptr, 10 );
1418 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1419 return 0;
1420 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1421 return 0;
1423 else
1425 result = strtoul( str, &endptr, 10 );
1426 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1427 return 0;
1428 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1430 if (translated) *translated = TRUE;
1431 return (UINT)result;
1435 /***********************************************************************
1436 * CheckDlgButton (USER32.@)
1438 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1440 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1441 return TRUE;
1445 /***********************************************************************
1446 * IsDlgButtonChecked (USER32.@)
1448 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1450 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1454 /***********************************************************************
1455 * CheckRB
1457 * Callback function used to check/uncheck radio buttons that fall
1458 * within a specific range of IDs.
1460 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1462 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1463 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1465 if ((lChildID >= lpRadioGroup->firstID) &&
1466 (lChildID <= lpRadioGroup->lastID))
1468 if (lChildID == lpRadioGroup->checkID)
1470 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1472 else
1474 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1478 return TRUE;
1482 /***********************************************************************
1483 * CheckRadioButton (USER32.@)
1485 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1486 int lastID, int checkID )
1488 RADIOGROUP radioGroup;
1490 radioGroup.firstID = firstID;
1491 radioGroup.lastID = lastID;
1492 radioGroup.checkID = checkID;
1494 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1498 /***********************************************************************
1499 * GetDialogBaseUnits (USER.243)
1500 * GetDialogBaseUnits (USER32.@)
1502 DWORD WINAPI GetDialogBaseUnits(void)
1504 static DWORD units;
1506 if (!units)
1508 HDC hdc;
1509 SIZE size;
1511 if ((hdc = GetDC(0)))
1513 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1514 if (size.cx) units = MAKELONG( size.cx, size.cy );
1515 ReleaseDC( 0, hdc );
1517 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1519 return units;
1523 /***********************************************************************
1524 * MapDialogRect (USER32.@)
1526 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1528 DIALOGINFO * dlgInfo;
1529 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1530 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1531 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1532 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1533 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1534 return TRUE;
1538 /***********************************************************************
1539 * GetNextDlgGroupItem (USER32.@)
1541 * Corrections to MSDN documentation
1543 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1544 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1545 * 2. Prev of NULL or hwndDlg fails
1547 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1549 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1550 BOOL fLooped=FALSE;
1551 BOOL fSkipping=FALSE;
1553 hwndDlg = WIN_GetFullHandle( hwndDlg );
1554 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1556 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1557 if (!hwndCtrl && fPrevious) return 0;
1559 if (hwndCtrl)
1561 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1563 else
1565 /* No ctrl specified -> start from the beginning */
1566 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1567 /* MSDN is wrong. fPrevious does not result in the last child */
1569 /* Maybe that first one is valid. If so then we don't want to skip it*/
1570 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1572 return hwndCtrl;
1576 /* Always go forward around the group and list of controls; for the
1577 * previous control keep track; for the next break when you find one
1579 retvalue = hwndCtrl;
1580 hwnd = hwndCtrl;
1581 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1584 while (!hwndNext)
1586 /* Climb out until there is a next sibling of the ancestor or we
1587 * reach the top (in which case we loop back to the start)
1589 if (hwndDlg == GetParent (hwnd))
1591 /* Wrap around to the beginning of the list, within the same
1592 * group. (Once only)
1594 if (fLooped) goto end;
1595 fLooped = TRUE;
1596 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1598 else
1600 hwnd = GetParent (hwnd);
1601 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1604 hwnd = hwndNext;
1606 /* Wander down the leading edge of controlparents */
1607 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1608 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1609 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1610 hwnd = hwndNext;
1611 /* Question. If the control is a control parent but either has no
1612 * children or is not visible/enabled then if it has a WS_GROUP does
1613 * it count? For that matter does it count anyway?
1614 * I believe it doesn't count.
1617 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1619 hwndLastGroup = hwnd;
1620 if (!fSkipping)
1622 /* Look for the beginning of the group */
1623 fSkipping = TRUE;
1627 if (hwnd == hwndCtrl)
1629 if (!fSkipping) break;
1630 if (hwndLastGroup == hwnd) break;
1631 hwnd = hwndLastGroup;
1632 fSkipping = FALSE;
1633 fLooped = FALSE;
1636 if (!fSkipping &&
1637 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1638 WS_VISIBLE)
1640 retvalue = hwnd;
1641 if (!fPrevious) break;
1644 end:
1645 return retvalue;
1649 /***********************************************************************
1650 * DIALOG_GetNextTabItem
1652 * Recursive helper for GetNextDlgTabItem
1654 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1656 LONG dsStyle;
1657 LONG exStyle;
1658 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1659 HWND retWnd = 0;
1660 HWND hChildFirst = 0;
1662 if(!hwndCtrl)
1664 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1665 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1667 else if (IsChild( hwndMain, hwndCtrl ))
1669 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1670 if(!hChildFirst)
1672 if(GetParent(hwndCtrl) != hwndMain)
1673 /* i.e. if we are not at the top level of the recursion */
1674 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1675 else
1676 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1680 while(hChildFirst)
1682 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1683 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1684 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1686 HWND retWnd;
1687 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1688 if (retWnd) return (retWnd);
1690 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1692 return (hChildFirst);
1694 hChildFirst = GetWindow(hChildFirst,wndSearch);
1696 if(hwndCtrl)
1698 HWND hParent = GetParent(hwndCtrl);
1699 while(hParent)
1701 if(hParent == hwndMain) break;
1702 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1703 if(retWnd) break;
1704 hParent = GetParent(hParent);
1706 if(!retWnd)
1707 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1709 return retWnd ? retWnd : hwndCtrl;
1712 /***********************************************************************
1713 * GetNextDlgTabItem (USER32.@)
1715 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1716 BOOL fPrevious )
1718 hwndDlg = WIN_GetFullHandle( hwndDlg );
1719 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1721 /* Undocumented but tested under Win2000 and WinME */
1722 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1724 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1725 * NB GetLastError returns whatever was set before the function was
1726 * called.
1728 if (!hwndCtrl && fPrevious) return 0;
1730 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1733 /**********************************************************************
1734 * DIALOG_DlgDirSelect
1736 * Helper function for DlgDirSelect*
1738 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1739 INT id, BOOL unicode, BOOL combo )
1741 WCHAR *buffer, *ptr;
1742 INT item, size;
1743 BOOL ret;
1744 HWND listbox = GetDlgItem( hwnd, id );
1746 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1747 if (!listbox) return FALSE;
1749 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1750 if (item == LB_ERR) return FALSE;
1752 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1753 if (size == LB_ERR) return FALSE;
1755 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1757 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1759 if ((ret = (buffer[0] == '['))) /* drive or directory */
1761 if (buffer[1] == '-') /* drive */
1763 buffer[3] = ':';
1764 buffer[4] = 0;
1765 ptr = buffer + 2;
1767 else
1769 buffer[strlenW(buffer)-1] = '\\';
1770 ptr = buffer + 1;
1773 else
1775 /* Filenames without a dot extension must have one tacked at the end */
1776 if (strchrW(buffer, '.') == NULL)
1778 buffer[strlenW(buffer)+1] = '\0';
1779 buffer[strlenW(buffer)] = '.';
1781 ptr = buffer;
1784 if (!unicode)
1786 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1787 ((LPSTR)str)[len-1] = 0;
1789 else lstrcpynW( str, ptr, len );
1790 HeapFree( GetProcessHeap(), 0, buffer );
1791 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1792 return ret;
1796 /**********************************************************************
1797 * DIALOG_DlgDirListW
1799 * Helper function for DlgDirList*W
1801 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1802 INT idStatic, UINT attrib, BOOL combo )
1804 HWND hwnd;
1805 LPWSTR orig_spec = spec;
1806 WCHAR any[] = {'*','.','*',0};
1808 #define SENDMSG(msg,wparam,lparam) \
1809 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1810 : SendMessageW( hwnd, msg, wparam, lparam ))
1812 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1814 /* If the path exists and is a directory, chdir to it */
1815 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1816 else
1818 WCHAR *p, *p2;
1819 p = spec;
1820 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1821 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1822 if ((p2 = strrchrW( p, '/' ))) p = p2;
1823 if (p != spec)
1825 WCHAR sep = *p;
1826 *p = 0;
1827 if (!SetCurrentDirectoryW( spec ))
1829 *p = sep; /* Restore the original spec */
1830 return FALSE;
1832 spec = p + 1;
1836 TRACE( "mask=%s\n", debugstr_w(spec) );
1838 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1840 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1842 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1843 if (attrib & DDL_DIRECTORY)
1845 if (!(attrib & DDL_EXCLUSIVE))
1847 SENDMSG( combo ? CB_DIR : LB_DIR,
1848 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1849 (LPARAM)spec );
1851 SENDMSG( combo ? CB_DIR : LB_DIR,
1852 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1853 (LPARAM)any );
1855 else
1857 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1861 /* Convert path specification to uppercase */
1862 if (spec) CharUpperW(spec);
1864 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1866 WCHAR temp[MAX_PATH];
1867 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1868 CharLowerW( temp );
1869 /* Can't use PostMessage() here, because the string is on the stack */
1870 SetDlgItemTextW( hDlg, idStatic, temp );
1873 if (orig_spec && (spec != orig_spec))
1875 /* Update the original file spec */
1876 WCHAR *p = spec;
1877 while ((*orig_spec++ = *p++));
1880 return TRUE;
1881 #undef SENDMSG
1885 /**********************************************************************
1886 * DIALOG_DlgDirListA
1888 * Helper function for DlgDirList*A
1890 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1891 INT idStatic, UINT attrib, BOOL combo )
1893 if (spec)
1895 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1896 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1897 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1898 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1899 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1900 HeapFree( GetProcessHeap(), 0, specW );
1901 return ret;
1903 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1907 /**********************************************************************
1908 * DlgDirSelectExA (USER32.@)
1910 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1912 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1916 /**********************************************************************
1917 * DlgDirSelectExW (USER32.@)
1919 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1921 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1925 /**********************************************************************
1926 * DlgDirSelectComboBoxExA (USER32.@)
1928 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1929 INT id )
1931 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1935 /**********************************************************************
1936 * DlgDirSelectComboBoxExW (USER32.@)
1938 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1939 INT id)
1941 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1945 /**********************************************************************
1946 * DlgDirListA (USER32.@)
1948 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1949 INT idStatic, UINT attrib )
1951 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1955 /**********************************************************************
1956 * DlgDirListW (USER32.@)
1958 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1959 INT idStatic, UINT attrib )
1961 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1965 /**********************************************************************
1966 * DlgDirListComboBoxA (USER32.@)
1968 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1969 INT idStatic, UINT attrib )
1971 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1975 /**********************************************************************
1976 * DlgDirListComboBoxW (USER32.@)
1978 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1979 INT idStatic, UINT attrib )
1981 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );