jscript: Get rid of no longer used jsdisp_propput_const.
[wine.git] / dlls / user32 / dialog.c
blob6f23c743095b8d38cf2b2b8a05f99b8edbdd2ff5
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 SetLastError(0);
287 return FALSE;
290 /* Send initialisation messages to the control */
291 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
292 (WPARAM)dlgInfo->hUserFont, 0 );
293 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
295 /* If there's already a default push-button, set it back */
296 /* to normal and use this one instead. */
297 if (hwndDefButton)
298 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
299 hwndDefButton = hwndCtrl;
300 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
303 TRACE(" END\n" );
304 return TRUE;
308 /***********************************************************************
309 * DIALOG_ParseTemplate32
311 * Fill a DLG_TEMPLATE structure from the dialog template, and return
312 * a pointer to the first control.
314 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
316 const WORD *p = (const WORD *)template;
317 WORD signature;
318 WORD dlgver;
320 dlgver = GET_WORD(p); p++;
321 signature = GET_WORD(p); p++;
323 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
325 result->dialogEx = TRUE;
326 result->helpId = GET_DWORD(p); p += 2;
327 result->exStyle = GET_DWORD(p); p += 2;
328 result->style = GET_DWORD(p); p += 2;
330 else
332 result->style = GET_DWORD(p - 2);
333 result->dialogEx = FALSE;
334 result->helpId = 0;
335 result->exStyle = GET_DWORD(p); p += 2;
337 result->nbItems = GET_WORD(p); p++;
338 result->x = GET_WORD(p); p++;
339 result->y = GET_WORD(p); p++;
340 result->cx = GET_WORD(p); p++;
341 result->cy = GET_WORD(p); p++;
342 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
343 result->dialogEx ? "EX" : "", result->x, result->y,
344 result->cx, result->cy, result->helpId );
345 TRACE(" STYLE 0x%08x\n", result->style );
346 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
348 /* Get the menu name */
350 switch(GET_WORD(p))
352 case 0x0000:
353 result->menuName = NULL;
354 p++;
355 break;
356 case 0xffff:
357 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
358 p += 2;
359 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
360 break;
361 default:
362 result->menuName = p;
363 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
364 p += strlenW( result->menuName ) + 1;
365 break;
368 /* Get the class name */
370 switch(GET_WORD(p))
372 case 0x0000:
373 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
374 p++;
375 break;
376 case 0xffff:
377 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
378 p += 2;
379 TRACE(" CLASS %04x\n", LOWORD(result->className) );
380 break;
381 default:
382 result->className = p;
383 TRACE(" CLASS %s\n", debugstr_w( result->className ));
384 p += strlenW( result->className ) + 1;
385 break;
388 /* Get the window caption */
390 result->caption = p;
391 p += strlenW( result->caption ) + 1;
392 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
394 /* Get the font name */
396 result->pointSize = 0;
397 result->faceName = NULL;
398 result->weight = FW_DONTCARE;
399 result->italic = FALSE;
401 if (result->style & DS_SETFONT)
403 result->pointSize = GET_WORD(p);
404 p++;
406 /* If pointSize is 0x7fff, it means that we need to use the font
407 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
408 * italic, and facename from the dialog template.
410 if (result->pointSize == 0x7fff)
412 /* We could call SystemParametersInfo here, but then we'd have
413 * to convert from pixel size to point size (which can be
414 * imprecise).
416 TRACE(" FONT: Using message box font\n");
418 else
420 if (result->dialogEx)
422 result->weight = GET_WORD(p); p++;
423 result->italic = LOBYTE(GET_WORD(p)); p++;
425 result->faceName = p;
426 p += strlenW( result->faceName ) + 1;
428 TRACE(" FONT %d, %s, %d, %s\n",
429 result->pointSize, debugstr_w( result->faceName ),
430 result->weight, result->italic ? "TRUE" : "FALSE" );
434 /* First control is on dword boundary */
435 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
439 /***********************************************************************
440 * DIALOG_CreateIndirect
441 * Creates a dialog box window
443 * modal = TRUE if we are called from a modal dialog box.
444 * (it's more compatible to do it here, as under Windows the owner
445 * is never disabled if the dialog fails because of an invalid template)
447 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
448 HWND owner, DLGPROC dlgProc, LPARAM param,
449 BOOL unicode, HWND *modal_owner )
451 HWND hwnd;
452 RECT rect;
453 POINT pos;
454 SIZE size;
455 DLG_TEMPLATE template;
456 DIALOGINFO * dlgInfo;
457 DWORD units = GetDialogBaseUnits();
458 HWND disabled_owner = NULL;
459 HMENU hMenu = 0;
460 HFONT hUserFont = 0;
461 UINT flags = 0;
462 UINT xBaseUnit = LOWORD(units);
463 UINT yBaseUnit = HIWORD(units);
465 /* Parse dialog template */
467 if (!dlgTemplate) return 0;
468 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
470 /* Load menu */
472 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
474 /* Create custom font if needed */
476 if (template.style & DS_SETFONT)
478 HDC dc = GetDC(0);
480 if (template.pointSize == 0x7fff)
482 /* We get the message font from the non-client metrics */
483 NONCLIENTMETRICSW ncMetrics;
485 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
486 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
487 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
489 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
492 else
494 /* We convert the size to pixels and then make it -ve. This works
495 * for both +ve and -ve template.pointSize */
496 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
497 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
498 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
499 PROOF_QUALITY, FF_DONTCARE,
500 template.faceName );
503 if (hUserFont)
505 SIZE charSize;
506 HFONT hOldFont = SelectObject( dc, hUserFont );
507 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
508 if (charSize.cx)
510 xBaseUnit = charSize.cx;
511 yBaseUnit = charSize.cy;
513 SelectObject( dc, hOldFont );
515 ReleaseDC(0, dc);
516 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
519 /* Create dialog main window */
521 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
523 if (template.style & DS_CONTROL)
524 template.style &= ~(WS_CAPTION|WS_SYSMENU);
525 template.style |= DS_3DLOOK;
526 if (template.style & DS_MODALFRAME)
527 template.exStyle |= WS_EX_DLGMODALFRAME;
528 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
529 template.exStyle |= WS_EX_CONTROLPARENT;
530 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
531 pos.x = rect.left;
532 pos.y = rect.top;
533 size.cx = rect.right - rect.left;
534 size.cy = rect.bottom - rect.top;
536 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
538 pos.x = pos.y = CW_USEDEFAULT;
540 else
542 HMONITOR monitor = 0;
543 MONITORINFO mon_info;
545 mon_info.cbSize = sizeof(mon_info);
546 if (template.style & DS_CENTER)
548 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
549 GetMonitorInfoW( monitor, &mon_info );
550 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
551 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
553 else if (template.style & DS_CENTERMOUSE)
555 GetCursorPos( &pos );
556 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
557 GetMonitorInfoW( monitor, &mon_info );
559 else
561 pos.x += MulDiv(template.x, xBaseUnit, 4);
562 pos.y += MulDiv(template.y, yBaseUnit, 8);
563 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
565 if ( !(template.style & WS_CHILD) )
567 INT dX, dY;
569 /* try to fit it into the desktop */
571 if (!monitor)
573 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
574 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
575 GetMonitorInfoW( monitor, &mon_info );
577 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
578 pos.x -= dX;
579 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
580 pos.y -= dY;
581 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
582 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
586 if (modal_owner && owner)
588 HWND parent;
590 * Owner needs to be top level window. We need to duplicate the logic from server,
591 * because we need to disable it before creating dialog window. Note that we do that
592 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
593 * Windows does.
595 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
597 parent = GetParent( owner );
598 if (!parent || parent == GetDesktopWindow()) break;
599 owner = parent;
601 *modal_owner = owner;
602 if (IsWindowEnabled( owner ))
604 disabled_owner = owner;
605 EnableWindow( disabled_owner, FALSE );
609 if (unicode)
611 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
612 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
613 owner, hMenu, hInst, NULL );
615 else
617 LPCSTR class = (LPCSTR)template.className;
618 LPCSTR caption = (LPCSTR)template.caption;
619 LPSTR class_tmp = NULL;
620 LPSTR caption_tmp = NULL;
622 if (!IS_INTRESOURCE(class))
624 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
625 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
626 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
627 class = class_tmp;
629 if (!IS_INTRESOURCE(caption))
631 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
632 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
633 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
634 caption = caption_tmp;
636 hwnd = CreateWindowExA(template.exStyle, class, caption,
637 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
638 owner, hMenu, hInst, NULL );
639 HeapFree( GetProcessHeap(), 0, class_tmp );
640 HeapFree( GetProcessHeap(), 0, caption_tmp );
643 if (!hwnd)
645 if (hUserFont) DeleteObject( hUserFont );
646 if (hMenu) DestroyMenu( hMenu );
647 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
648 return 0;
651 /* moved this from the top of the method to here as DIALOGINFO structure
652 will be valid only after WM_CREATE message has been handled in DefDlgProc
653 All the members of the structure get filled here using temp variables */
654 dlgInfo = DIALOG_get_info( hwnd, TRUE );
655 dlgInfo->hwndFocus = 0;
656 dlgInfo->hUserFont = hUserFont;
657 dlgInfo->hMenu = hMenu;
658 dlgInfo->xBaseUnit = xBaseUnit;
659 dlgInfo->yBaseUnit = yBaseUnit;
660 dlgInfo->flags = flags;
662 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
664 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
665 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
667 if (dlgProc && dlgInfo->hUserFont)
668 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
670 /* Create controls */
672 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
674 /* Send initialisation messages and set focus */
676 if (dlgProc)
678 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
679 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
680 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
681 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
683 /* By returning TRUE, app has requested a default focus assignment.
684 * WM_INITDIALOG may have changed the tab order, so find the first
685 * tabstop control again. */
686 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
687 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
688 if (focus)
690 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
691 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
692 SetFocus( focus );
697 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
699 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
701 return hwnd;
703 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
704 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
705 return 0;
709 /***********************************************************************
710 * CreateDialogParamA (USER32.@)
712 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
713 DLGPROC dlgProc, LPARAM param )
715 HRSRC hrsrc;
716 LPCDLGTEMPLATEA ptr;
718 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
719 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
720 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
724 /***********************************************************************
725 * CreateDialogParamW (USER32.@)
727 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
728 DLGPROC dlgProc, LPARAM param )
730 HRSRC hrsrc;
731 LPCDLGTEMPLATEA ptr;
733 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
734 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
735 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
739 /***********************************************************************
740 * CreateDialogIndirectParamAorW (USER32.@)
742 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
743 HWND owner, DLGPROC dlgProc, LPARAM param,
744 DWORD flags )
746 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
749 /***********************************************************************
750 * CreateDialogIndirectParamA (USER32.@)
752 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
753 HWND owner, DLGPROC dlgProc, LPARAM param )
755 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
758 /***********************************************************************
759 * CreateDialogIndirectParamW (USER32.@)
761 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
762 HWND owner, DLGPROC dlgProc, LPARAM param )
764 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
768 /***********************************************************************
769 * DIALOG_DoDialogBox
771 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
773 DIALOGINFO * dlgInfo;
774 MSG msg;
775 INT retval;
776 BOOL bFirstEmpty;
778 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
780 bFirstEmpty = TRUE;
781 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
783 for (;;)
785 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
787 if (bFirstEmpty)
789 /* ShowWindow the first time the queue goes empty */
790 ShowWindow( hwnd, SW_SHOWNORMAL );
791 bFirstEmpty = FALSE;
793 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
795 /* No message present -> send ENTERIDLE and wait */
796 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
798 GetMessageW( &msg, 0, 0, 0 );
801 if (msg.message == WM_QUIT)
803 PostQuitMessage( msg.wParam );
804 if (!IsWindow( hwnd )) return 0;
805 break;
807 if (!IsWindow( hwnd )) return 0;
808 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
810 TranslateMessage( &msg );
811 DispatchMessageW( &msg );
813 if (!IsWindow( hwnd )) return 0;
814 if (dlgInfo->flags & DF_END) break;
816 if (bFirstEmpty && msg.message == WM_TIMER)
818 ShowWindow( hwnd, SW_SHOWNORMAL );
819 bFirstEmpty = FALSE;
823 retval = dlgInfo->idResult;
824 DestroyWindow( hwnd );
825 return retval;
829 /***********************************************************************
830 * DialogBoxParamA (USER32.@)
832 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
833 HWND owner, DLGPROC dlgProc, LPARAM param )
835 HWND hwnd;
836 HRSRC hrsrc;
837 LPCDLGTEMPLATEA ptr;
839 if (owner && !IsWindow(owner)) return 0;
841 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
842 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
843 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner ))) return -1;
844 return DIALOG_DoDialogBox( hwnd, owner );
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 (owner && !IsWindow(owner)) return 0;
860 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
861 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
862 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner ))) return -1;
863 return DIALOG_DoDialogBox( hwnd, owner );
867 /***********************************************************************
868 * DialogBoxIndirectParamAorW (USER32.@)
870 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
871 HWND owner, DLGPROC dlgProc,
872 LPARAM param, DWORD flags )
874 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
875 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
876 return -1;
879 /***********************************************************************
880 * DialogBoxIndirectParamA (USER32.@)
882 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
883 HWND owner, DLGPROC dlgProc, LPARAM param )
885 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
889 /***********************************************************************
890 * DialogBoxIndirectParamW (USER32.@)
892 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
893 HWND owner, DLGPROC dlgProc, LPARAM param )
895 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
898 /***********************************************************************
899 * EndDialog (USER32.@)
901 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
903 DIALOGINFO * dlgInfo;
904 HWND owner;
906 TRACE("%p %ld\n", hwnd, retval );
908 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
910 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
911 return FALSE;
913 dlgInfo->idResult = retval;
914 dlgInfo->flags |= DF_END;
916 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
917 if (owner)
918 EnableWindow( owner, TRUE );
920 /* Windows sets the focus to the dialog itself in EndDialog */
922 if (IsChild(hwnd, GetFocus()))
923 SetFocus( hwnd );
925 /* Don't have to send a ShowWindow(SW_HIDE), just do
926 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
928 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
929 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
931 if (hwnd == GetActiveWindow())
933 /* If this dialog was given an owner then set the focus to that owner. */
934 if (owner)
935 SetForegroundWindow( owner );
936 else
937 WINPOS_ActivateOtherWindow( hwnd );
940 /* unblock dialog loop */
941 PostMessageA(hwnd, WM_NULL, 0, 0);
942 return TRUE;
946 /***********************************************************************
947 * DIALOG_IsAccelerator
949 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
951 HWND hwndControl = hwnd;
952 HWND hwndNext;
953 INT dlgCode;
954 WCHAR buffer[128];
958 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
959 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
961 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
962 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
963 GetWindowTextW( hwndControl, buffer, ARRAY_SIZE( buffer )))
965 /* find the accelerator key */
966 LPWSTR p = buffer - 2;
970 p = strchrW( p + 2, '&' );
972 while (p != NULL && p[1] == '&');
974 /* and check if it's the one we're looking for */
975 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
977 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
979 /* set focus to the control */
980 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
981 /* and bump it on to next */
982 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
984 else if (dlgCode & DLGC_BUTTON)
986 /* send BM_CLICK message to the control */
987 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
989 return TRUE;
992 hwndNext = GetWindow( hwndControl, GW_CHILD );
994 else hwndNext = 0;
996 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
998 while (!hwndNext && hwndControl)
1000 hwndControl = GetParent( hwndControl );
1001 if (hwndControl == hwndDlg)
1003 if(hwnd==hwndDlg) /* prevent endless loop */
1005 hwndNext=hwnd;
1006 break;
1008 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1010 else
1011 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1013 hwndControl = hwndNext;
1015 while (hwndControl && (hwndControl != hwnd));
1017 return FALSE;
1020 /***********************************************************************
1021 * DIALOG_FindMsgDestination
1023 * The messages that IsDialogMessage sends may not go to the dialog
1024 * calling IsDialogMessage if that dialog is a child, and it has the
1025 * DS_CONTROL style set.
1026 * We propagate up until we hit one that does not have DS_CONTROL, or
1027 * whose parent is not a dialog.
1029 * This is undocumented behaviour.
1031 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1033 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1035 WND *pParent;
1036 HWND hParent = GetParent(hwndDlg);
1037 if (!hParent) break;
1039 pParent = WIN_GetPtr(hParent);
1040 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1042 if (!pParent->dlgInfo)
1044 WIN_ReleasePtr(pParent);
1045 break;
1047 WIN_ReleasePtr(pParent);
1049 hwndDlg = hParent;
1052 return hwndDlg;
1055 /***********************************************************************
1056 * DIALOG_FixOneChildOnChangeFocus
1058 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1061 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1062 LPARAM lParam)
1064 /* If a default pushbutton then no longer default */
1065 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1066 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1067 return TRUE;
1070 /***********************************************************************
1071 * DIALOG_FixChildrenOnChangeFocus
1073 * Following the change of focus that occurs for example after handling
1074 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1075 * children may be required.
1077 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1079 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1080 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1081 /* Windows does ask for this. I don't know why yet */
1083 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1085 /* If the button that is getting the focus WAS flagged as the default
1086 * pushbutton then ask the dialog what it thinks the default is and
1087 * set that in the default style.
1089 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1091 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1092 if (HIWORD(def_id) == DC_HASDEFID)
1094 HWND hwndDef;
1095 def_id = LOWORD(def_id);
1096 hwndDef = GetDlgItem (hwndDlg, def_id);
1097 if (hwndDef)
1099 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1100 /* I know that if it is a button then it should already be a
1101 * UNDEFPUSHBUTTON, since we have just told the buttons to
1102 * change style. But maybe they ignored our request
1104 if ((dlgcode_def & DLGC_BUTTON) &&
1105 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1107 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1112 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1114 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1115 /* I wonder why it doesn't send a DM_SETDEFID */
1119 /***********************************************************************
1120 * DIALOG_IdToHwnd
1122 * A recursive version of GetDlgItem
1124 * RETURNS
1125 * The HWND for a Child ID.
1127 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1129 int i;
1130 HWND *list = WIN_ListChildren( hwndDlg );
1131 HWND ret = 0;
1133 if (!list) return 0;
1135 for (i = 0; list[i]; i++)
1137 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1139 ret = list[i];
1140 break;
1143 /* Recurse into every child */
1144 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1147 HeapFree( GetProcessHeap(), 0, list );
1148 return ret;
1151 /***********************************************************************
1152 * IsDialogMessageW (USER32.@)
1154 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1156 INT dlgCode;
1158 if (!IsWindow( hwndDlg ))
1159 return FALSE;
1161 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1163 hwndDlg = WIN_GetFullHandle( hwndDlg );
1164 if (is_desktop_window(hwndDlg)) return FALSE;
1165 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1167 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1169 switch(msg->message)
1171 case WM_KEYDOWN:
1172 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1173 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1175 switch(msg->wParam)
1177 case VK_TAB:
1178 if (!(dlgCode & DLGC_WANTTAB))
1180 BOOL fIsDialog = TRUE;
1181 WND *pWnd = WIN_GetPtr( hwndDlg );
1183 if (pWnd && pWnd != WND_OTHER_PROCESS)
1185 fIsDialog = (pWnd->dlgInfo != NULL);
1186 WIN_ReleasePtr(pWnd);
1189 /* I am not sure under which circumstances the TAB is handled
1190 * each way. All I do know is that it does not always simply
1191 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1192 * do so but I presume someone has)
1194 if (fIsDialog)
1195 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1196 else
1198 /* It would appear that GetNextDlgTabItem can handle being
1199 * passed hwndDlg rather than NULL but that is undocumented
1200 * so let's do it properly
1202 HWND hwndFocus = GetFocus();
1203 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1204 hwndFocus == hwndDlg ? NULL : hwndFocus,
1205 GetKeyState (VK_SHIFT) & 0x8000);
1206 if (hwndNext)
1208 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1209 msg->wParam, (LPARAM)msg);
1210 if (dlgCode & DLGC_HASSETSEL)
1212 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1213 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1214 if (buffer)
1216 INT length;
1217 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1218 length = strlenW (buffer);
1219 HeapFree (GetProcessHeap(), 0, buffer);
1220 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1223 SetFocus (hwndNext);
1224 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1226 else
1227 return FALSE;
1229 return TRUE;
1231 break;
1233 case VK_RIGHT:
1234 case VK_DOWN:
1235 case VK_LEFT:
1236 case VK_UP:
1237 if (!(dlgCode & DLGC_WANTARROWS))
1239 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1240 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1241 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1242 return TRUE;
1244 break;
1246 case VK_CANCEL:
1247 case VK_ESCAPE:
1248 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1249 return TRUE;
1251 case VK_EXECUTE:
1252 case VK_RETURN:
1254 DWORD dw;
1255 if ((GetFocus() == msg->hwnd) &&
1256 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1258 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1260 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1262 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1263 if (!hwndDef || IsWindowEnabled(hwndDef))
1264 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1266 else
1268 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1272 return TRUE;
1274 break;
1276 case WM_CHAR:
1277 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1278 * It does NOT get sent in the test program I have
1280 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1281 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1282 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1283 /* drop through */
1285 case WM_SYSCHAR:
1286 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1288 /* don't translate or dispatch */
1289 return TRUE;
1291 break;
1294 TranslateMessage( msg );
1295 DispatchMessageW( msg );
1296 return TRUE;
1300 /***********************************************************************
1301 * GetDlgCtrlID (USER32.@)
1303 INT WINAPI GetDlgCtrlID( HWND hwnd )
1305 return GetWindowLongPtrW( hwnd, GWLP_ID );
1309 /***********************************************************************
1310 * GetDlgItem (USER32.@)
1312 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1314 int i;
1315 HWND *list = WIN_ListChildren( hwndDlg );
1316 HWND ret = 0;
1318 if (!list) return 0;
1320 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1321 ret = list[i];
1322 HeapFree( GetProcessHeap(), 0, list );
1323 return ret;
1327 /*******************************************************************
1328 * SendDlgItemMessageA (USER32.@)
1330 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1331 WPARAM wParam, LPARAM lParam )
1333 HWND hwndCtrl = GetDlgItem( hwnd, id );
1334 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1335 else return 0;
1339 /*******************************************************************
1340 * SendDlgItemMessageW (USER32.@)
1342 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1343 WPARAM wParam, LPARAM lParam )
1345 HWND hwndCtrl = GetDlgItem( hwnd, id );
1346 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1347 else return 0;
1351 /*******************************************************************
1352 * SetDlgItemTextA (USER32.@)
1354 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1356 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1360 /*******************************************************************
1361 * SetDlgItemTextW (USER32.@)
1363 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1365 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1369 /***********************************************************************
1370 * GetDlgItemTextA (USER32.@)
1372 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1374 if (str && (len > 0)) str[0] = '\0';
1375 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1376 len, (LPARAM)str );
1380 /***********************************************************************
1381 * GetDlgItemTextW (USER32.@)
1383 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1385 if (str && (len > 0)) str[0] = '\0';
1386 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1387 len, (LPARAM)str );
1391 /*******************************************************************
1392 * SetDlgItemInt (USER32.@)
1394 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1395 BOOL fSigned )
1397 char str[20];
1399 if (fSigned) sprintf( str, "%d", (INT)value );
1400 else sprintf( str, "%u", value );
1401 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1402 return TRUE;
1406 /***********************************************************************
1407 * GetDlgItemInt (USER32.@)
1409 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1410 BOOL fSigned )
1412 char str[30];
1413 char * endptr;
1414 LONG_PTR result = 0;
1416 if (translated) *translated = FALSE;
1417 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1418 return 0;
1419 if (fSigned)
1421 result = strtol( str, &endptr, 10 );
1422 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1423 return 0;
1424 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1425 return 0;
1427 else
1429 result = strtoul( str, &endptr, 10 );
1430 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1431 return 0;
1432 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1434 if (translated) *translated = TRUE;
1435 return (UINT)result;
1439 /***********************************************************************
1440 * CheckDlgButton (USER32.@)
1442 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1444 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1445 return TRUE;
1449 /***********************************************************************
1450 * IsDlgButtonChecked (USER32.@)
1452 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1454 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1458 /***********************************************************************
1459 * CheckRB
1461 * Callback function used to check/uncheck radio buttons that fall
1462 * within a specific range of IDs.
1464 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1466 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1467 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1469 if ((lChildID >= lpRadioGroup->firstID) &&
1470 (lChildID <= lpRadioGroup->lastID))
1472 if (lChildID == lpRadioGroup->checkID)
1474 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1476 else
1478 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1482 return TRUE;
1486 /***********************************************************************
1487 * CheckRadioButton (USER32.@)
1489 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1490 int lastID, int checkID )
1492 RADIOGROUP radioGroup;
1494 radioGroup.firstID = firstID;
1495 radioGroup.lastID = lastID;
1496 radioGroup.checkID = checkID;
1498 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1502 /***********************************************************************
1503 * GetDialogBaseUnits (USER.243)
1504 * GetDialogBaseUnits (USER32.@)
1506 DWORD WINAPI GetDialogBaseUnits(void)
1508 static DWORD units;
1510 if (!units)
1512 HDC hdc;
1513 SIZE size;
1515 if ((hdc = GetDC(0)))
1517 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1518 if (size.cx) units = MAKELONG( size.cx, size.cy );
1519 ReleaseDC( 0, hdc );
1521 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1523 return units;
1527 /***********************************************************************
1528 * MapDialogRect (USER32.@)
1530 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1532 DIALOGINFO * dlgInfo;
1533 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1534 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1535 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1536 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1537 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1538 return TRUE;
1542 /***********************************************************************
1543 * GetNextDlgGroupItem (USER32.@)
1545 * Corrections to MSDN documentation
1547 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1548 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1549 * 2. Prev of NULL or hwndDlg fails
1551 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1553 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1554 BOOL fLooped=FALSE;
1555 BOOL fSkipping=FALSE;
1557 hwndDlg = WIN_GetFullHandle( hwndDlg );
1558 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1560 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1561 if (!hwndCtrl && fPrevious) return 0;
1563 if (hwndCtrl)
1565 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1567 else
1569 /* No ctrl specified -> start from the beginning */
1570 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1571 /* MSDN is wrong. fPrevious does not result in the last child */
1573 /* Maybe that first one is valid. If so then we don't want to skip it*/
1574 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1576 return hwndCtrl;
1580 /* Always go forward around the group and list of controls; for the
1581 * previous control keep track; for the next break when you find one
1583 retvalue = hwndCtrl;
1584 hwnd = hwndCtrl;
1585 while (1)
1587 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1588 while (!hwndNext)
1590 /* Climb out until there is a next sibling of the ancestor or we
1591 * reach the top (in which case we loop back to the start)
1593 if (hwndDlg == GetParent (hwnd))
1595 /* Wrap around to the beginning of the list, within the same
1596 * group. (Once only)
1598 if (fLooped) goto end;
1599 fLooped = TRUE;
1600 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1602 else
1604 hwnd = GetParent (hwnd);
1605 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1608 hwnd = hwndNext;
1610 /* Wander down the leading edge of controlparents */
1611 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1612 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1613 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1614 hwnd = hwndNext;
1615 /* Question. If the control is a control parent but either has no
1616 * children or is not visible/enabled then if it has a WS_GROUP does
1617 * it count? For that matter does it count anyway?
1618 * I believe it doesn't count.
1621 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1623 hwndLastGroup = hwnd;
1624 if (!fSkipping)
1626 /* Look for the beginning of the group */
1627 fSkipping = TRUE;
1631 if (hwnd == hwndCtrl)
1633 if (!fSkipping) break;
1634 if (hwndLastGroup == hwnd) break;
1635 hwnd = hwndLastGroup;
1636 fSkipping = FALSE;
1637 fLooped = FALSE;
1640 if (!fSkipping &&
1641 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1642 WS_VISIBLE)
1644 retvalue = hwnd;
1645 if (!fPrevious) break;
1648 end:
1649 return retvalue;
1653 /***********************************************************************
1654 * DIALOG_GetNextTabItem
1656 * Recursive helper for GetNextDlgTabItem
1658 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1660 LONG dsStyle;
1661 LONG exStyle;
1662 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1663 HWND retWnd = 0;
1664 HWND hChildFirst = 0;
1666 if(!hwndCtrl)
1668 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1669 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1671 else if (IsChild( hwndMain, hwndCtrl ))
1673 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1674 if(!hChildFirst)
1676 if(GetParent(hwndCtrl) != hwndMain)
1677 /* i.e. if we are not at the top level of the recursion */
1678 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1679 else
1680 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1684 while(hChildFirst)
1686 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1687 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1688 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1690 HWND retWnd;
1691 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1692 if (retWnd) return (retWnd);
1694 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1696 return (hChildFirst);
1698 hChildFirst = GetWindow(hChildFirst,wndSearch);
1700 if(hwndCtrl)
1702 HWND hParent = GetParent(hwndCtrl);
1703 while(hParent)
1705 if(hParent == hwndMain) break;
1706 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1707 if(retWnd) break;
1708 hParent = GetParent(hParent);
1710 if(!retWnd)
1711 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1713 return retWnd ? retWnd : hwndCtrl;
1716 /***********************************************************************
1717 * GetNextDlgTabItem (USER32.@)
1719 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1720 BOOL fPrevious )
1722 hwndDlg = WIN_GetFullHandle( hwndDlg );
1723 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1725 /* Undocumented but tested under Win2000 and WinME */
1726 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1728 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1729 * NB GetLastError returns whatever was set before the function was
1730 * called.
1732 if (!hwndCtrl && fPrevious) return 0;
1734 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1737 /**********************************************************************
1738 * DIALOG_DlgDirSelect
1740 * Helper function for DlgDirSelect*
1742 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1743 INT id, BOOL unicode, BOOL combo )
1745 WCHAR *buffer, *ptr;
1746 INT item, size;
1747 BOOL ret;
1748 HWND listbox = GetDlgItem( hwnd, id );
1750 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1751 if (!listbox) return FALSE;
1753 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1754 if (item == LB_ERR) return FALSE;
1756 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1757 if (size == LB_ERR) return FALSE;
1759 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1761 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1763 if ((ret = (buffer[0] == '['))) /* drive or directory */
1765 if (buffer[1] == '-') /* drive */
1767 buffer[3] = ':';
1768 buffer[4] = 0;
1769 ptr = buffer + 2;
1771 else
1773 buffer[strlenW(buffer)-1] = '\\';
1774 ptr = buffer + 1;
1777 else
1779 /* Filenames without a dot extension must have one tacked at the end */
1780 if (strchrW(buffer, '.') == NULL)
1782 buffer[strlenW(buffer)+1] = '\0';
1783 buffer[strlenW(buffer)] = '.';
1785 ptr = buffer;
1788 if (!unicode)
1790 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1791 ((LPSTR)str)[len-1] = 0;
1793 else lstrcpynW( str, ptr, len );
1794 HeapFree( GetProcessHeap(), 0, buffer );
1795 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1796 return ret;
1800 /**********************************************************************
1801 * DIALOG_DlgDirListW
1803 * Helper function for DlgDirList*W
1805 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1806 INT idStatic, UINT attrib, BOOL combo )
1808 HWND hwnd;
1809 LPWSTR orig_spec = spec;
1810 WCHAR any[] = {'*','.','*',0};
1812 #define SENDMSG(msg,wparam,lparam) \
1813 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1814 : SendMessageW( hwnd, msg, wparam, lparam ))
1816 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1818 /* If the path exists and is a directory, chdir to it */
1819 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1820 else
1822 WCHAR *p, *p2;
1823 p = spec;
1824 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1825 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1826 if ((p2 = strrchrW( p, '/' ))) p = p2;
1827 if (p != spec)
1829 WCHAR sep = *p;
1830 *p = 0;
1831 if (!SetCurrentDirectoryW( spec ))
1833 *p = sep; /* Restore the original spec */
1834 return FALSE;
1836 spec = p + 1;
1840 TRACE( "mask=%s\n", debugstr_w(spec) );
1842 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1844 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1846 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1847 if (attrib & DDL_DIRECTORY)
1849 if (!(attrib & DDL_EXCLUSIVE))
1851 SENDMSG( combo ? CB_DIR : LB_DIR,
1852 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1853 (LPARAM)spec );
1855 SENDMSG( combo ? CB_DIR : LB_DIR,
1856 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1857 (LPARAM)any );
1859 else
1861 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1865 /* Convert path specification to uppercase */
1866 if (spec) CharUpperW(spec);
1868 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1870 WCHAR temp[MAX_PATH];
1871 GetCurrentDirectoryW( ARRAY_SIZE( temp ), temp );
1872 CharLowerW( temp );
1873 /* Can't use PostMessage() here, because the string is on the stack */
1874 SetDlgItemTextW( hDlg, idStatic, temp );
1877 if (orig_spec && (spec != orig_spec))
1879 /* Update the original file spec */
1880 WCHAR *p = spec;
1881 while ((*orig_spec++ = *p++));
1884 return TRUE;
1885 #undef SENDMSG
1889 /**********************************************************************
1890 * DIALOG_DlgDirListA
1892 * Helper function for DlgDirList*A
1894 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1895 INT idStatic, UINT attrib, BOOL combo )
1897 if (spec)
1899 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1900 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1901 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1902 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1903 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1904 HeapFree( GetProcessHeap(), 0, specW );
1905 return ret;
1907 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1911 /**********************************************************************
1912 * DlgDirSelectExA (USER32.@)
1914 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1916 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1920 /**********************************************************************
1921 * DlgDirSelectExW (USER32.@)
1923 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1925 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1929 /**********************************************************************
1930 * DlgDirSelectComboBoxExA (USER32.@)
1932 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1933 INT id )
1935 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1939 /**********************************************************************
1940 * DlgDirSelectComboBoxExW (USER32.@)
1942 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1943 INT id)
1945 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1949 /**********************************************************************
1950 * DlgDirListA (USER32.@)
1952 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1953 INT idStatic, UINT attrib )
1955 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1959 /**********************************************************************
1960 * DlgDirListW (USER32.@)
1962 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1963 INT idStatic, UINT attrib )
1965 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1969 /**********************************************************************
1970 * DlgDirListComboBoxA (USER32.@)
1972 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1973 INT idStatic, UINT attrib )
1975 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1979 /**********************************************************************
1980 * DlgDirListComboBoxW (USER32.@)
1982 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1983 INT idStatic, UINT attrib )
1985 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );