user32: Use the correct top-level window when activating through a mouse click.
[wine.git] / dlls / user32 / dialog.c
blobc2aad8d2901cf4c275ea718dc7881775e3c9ed9b
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 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
522 if (template.style & DS_CONTROL)
523 template.style &= ~(WS_CAPTION|WS_SYSMENU);
524 template.style |= DS_3DLOOK;
525 if (template.style & DS_MODALFRAME)
526 template.exStyle |= WS_EX_DLGMODALFRAME;
527 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
528 template.exStyle |= WS_EX_CONTROLPARENT;
529 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
530 pos.x = rect.left;
531 pos.y = rect.top;
532 size.cx = rect.right - rect.left;
533 size.cy = rect.bottom - rect.top;
535 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
537 pos.x = pos.y = CW_USEDEFAULT;
539 else
541 HMONITOR monitor = 0;
542 MONITORINFO mon_info;
544 mon_info.cbSize = sizeof(mon_info);
545 if (template.style & DS_CENTER)
547 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
548 GetMonitorInfoW( monitor, &mon_info );
549 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
550 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
552 else if (template.style & DS_CENTERMOUSE)
554 GetCursorPos( &pos );
555 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
556 GetMonitorInfoW( monitor, &mon_info );
558 else
560 pos.x += MulDiv(template.x, xBaseUnit, 4);
561 pos.y += MulDiv(template.y, yBaseUnit, 8);
562 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
564 if ( !(template.style & WS_CHILD) )
566 INT dX, dY;
568 /* try to fit it into the desktop */
570 if (!monitor)
572 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
573 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
574 GetMonitorInfoW( monitor, &mon_info );
576 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
577 pos.x -= dX;
578 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
579 pos.y -= dY;
580 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
581 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
585 if (modal_owner && owner)
587 HWND parent;
589 * Owner needs to be top level window. We need to duplicate the logic from server,
590 * because we need to disable it before creating dialog window. Note that we do that
591 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
592 * Windows does.
594 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
596 parent = GetParent( owner );
597 if (!parent || parent == GetDesktopWindow()) break;
598 owner = parent;
600 *modal_owner = owner;
601 if (IsWindowEnabled( owner ))
603 disabled_owner = owner;
604 EnableWindow( disabled_owner, FALSE );
608 if (unicode)
610 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
611 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
612 owner, hMenu, hInst, NULL );
614 else
616 LPCSTR class = (LPCSTR)template.className;
617 LPCSTR caption = (LPCSTR)template.caption;
618 LPSTR class_tmp = NULL;
619 LPSTR caption_tmp = NULL;
621 if (!IS_INTRESOURCE(class))
623 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
624 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
625 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
626 class = class_tmp;
628 if (!IS_INTRESOURCE(caption))
630 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
631 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
632 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
633 caption = caption_tmp;
635 hwnd = CreateWindowExA(template.exStyle, class, caption,
636 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
637 owner, hMenu, hInst, NULL );
638 HeapFree( GetProcessHeap(), 0, class_tmp );
639 HeapFree( GetProcessHeap(), 0, caption_tmp );
642 if (!hwnd)
644 if (hUserFont) DeleteObject( hUserFont );
645 if (hMenu) DestroyMenu( hMenu );
646 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
647 return 0;
650 /* moved this from the top of the method to here as DIALOGINFO structure
651 will be valid only after WM_CREATE message has been handled in DefDlgProc
652 All the members of the structure get filled here using temp variables */
653 dlgInfo = DIALOG_get_info( hwnd, TRUE );
654 dlgInfo->hwndFocus = 0;
655 dlgInfo->hUserFont = hUserFont;
656 dlgInfo->hMenu = hMenu;
657 dlgInfo->xBaseUnit = xBaseUnit;
658 dlgInfo->yBaseUnit = yBaseUnit;
659 dlgInfo->flags = flags;
661 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
663 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
664 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
666 if (dlgProc && dlgInfo->hUserFont)
667 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
669 /* Create controls */
671 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
673 /* Send initialisation messages and set focus */
675 if (dlgProc)
677 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
678 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
679 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
680 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
682 /* By returning TRUE, app has requested a default focus assignment.
683 * WM_INITDIALOG may have changed the tab order, so find the first
684 * tabstop control again. */
685 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
686 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
687 if (focus)
689 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
690 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
691 SetFocus( focus );
696 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
698 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
700 return hwnd;
702 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
703 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
704 return 0;
708 /***********************************************************************
709 * CreateDialogParamA (USER32.@)
711 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
712 DLGPROC dlgProc, LPARAM param )
714 HRSRC hrsrc;
715 LPCDLGTEMPLATEA ptr;
717 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
718 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
719 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
723 /***********************************************************************
724 * CreateDialogParamW (USER32.@)
726 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
727 DLGPROC dlgProc, LPARAM param )
729 HRSRC hrsrc;
730 LPCDLGTEMPLATEA ptr;
732 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
733 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
734 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
738 /***********************************************************************
739 * CreateDialogIndirectParamAorW (USER32.@)
741 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
742 HWND owner, DLGPROC dlgProc, LPARAM param,
743 DWORD flags )
745 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
748 /***********************************************************************
749 * CreateDialogIndirectParamA (USER32.@)
751 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
752 HWND owner, DLGPROC dlgProc, LPARAM param )
754 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
757 /***********************************************************************
758 * CreateDialogIndirectParamW (USER32.@)
760 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
761 HWND owner, DLGPROC dlgProc, LPARAM param )
763 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
767 /***********************************************************************
768 * DIALOG_DoDialogBox
770 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
772 DIALOGINFO * dlgInfo;
773 MSG msg;
774 INT retval;
775 BOOL bFirstEmpty;
777 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
779 bFirstEmpty = TRUE;
780 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
782 for (;;)
784 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
786 if (bFirstEmpty)
788 /* ShowWindow the first time the queue goes empty */
789 ShowWindow( hwnd, SW_SHOWNORMAL );
790 bFirstEmpty = FALSE;
792 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
794 /* No message present -> send ENTERIDLE and wait */
795 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
797 GetMessageW( &msg, 0, 0, 0 );
800 if (msg.message == WM_QUIT)
802 PostQuitMessage( msg.wParam );
803 if (!IsWindow( hwnd )) return 0;
804 break;
806 if (!IsWindow( hwnd )) return 0;
807 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
809 TranslateMessage( &msg );
810 DispatchMessageW( &msg );
812 if (!IsWindow( hwnd )) return 0;
813 if (dlgInfo->flags & DF_END) break;
815 if (bFirstEmpty && msg.message == WM_TIMER)
817 ShowWindow( hwnd, SW_SHOWNORMAL );
818 bFirstEmpty = FALSE;
822 retval = dlgInfo->idResult;
823 DestroyWindow( hwnd );
824 return retval;
828 /***********************************************************************
829 * DialogBoxParamA (USER32.@)
831 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
832 HWND owner, DLGPROC dlgProc, LPARAM param )
834 HWND hwnd;
835 HRSRC hrsrc;
836 LPCDLGTEMPLATEA ptr;
838 if (owner && !IsWindow(owner)) return 0;
840 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
841 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
842 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner ))) return -1;
843 return DIALOG_DoDialogBox( hwnd, owner );
847 /***********************************************************************
848 * DialogBoxParamW (USER32.@)
850 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
851 HWND owner, DLGPROC dlgProc, LPARAM param )
853 HWND hwnd;
854 HRSRC hrsrc;
855 LPCDLGTEMPLATEW ptr;
857 if (owner && !IsWindow(owner)) return 0;
859 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
860 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
861 if (!(hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner ))) return -1;
862 return DIALOG_DoDialogBox( hwnd, owner );
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 (!IsWindow( hwndDlg ))
1158 return FALSE;
1160 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1162 hwndDlg = WIN_GetFullHandle( hwndDlg );
1163 if (is_desktop_window(hwndDlg)) return FALSE;
1164 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1166 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1168 switch(msg->message)
1170 case WM_KEYDOWN:
1171 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1172 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1174 switch(msg->wParam)
1176 case VK_TAB:
1177 if (!(dlgCode & DLGC_WANTTAB))
1179 BOOL fIsDialog = TRUE;
1180 WND *pWnd = WIN_GetPtr( hwndDlg );
1182 if (pWnd && pWnd != WND_OTHER_PROCESS)
1184 fIsDialog = (pWnd->dlgInfo != NULL);
1185 WIN_ReleasePtr(pWnd);
1188 /* I am not sure under which circumstances the TAB is handled
1189 * each way. All I do know is that it does not always simply
1190 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1191 * do so but I presume someone has)
1193 if (fIsDialog)
1194 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1195 else
1197 /* It would appear that GetNextDlgTabItem can handle being
1198 * passed hwndDlg rather than NULL but that is undocumented
1199 * so let's do it properly
1201 HWND hwndFocus = GetFocus();
1202 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1203 hwndFocus == hwndDlg ? NULL : hwndFocus,
1204 GetKeyState (VK_SHIFT) & 0x8000);
1205 if (hwndNext)
1207 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1208 msg->wParam, (LPARAM)msg);
1209 if (dlgCode & DLGC_HASSETSEL)
1211 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1212 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1213 if (buffer)
1215 INT length;
1216 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1217 length = strlenW (buffer);
1218 HeapFree (GetProcessHeap(), 0, buffer);
1219 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1222 SetFocus (hwndNext);
1223 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1225 else
1226 return FALSE;
1228 return TRUE;
1230 break;
1232 case VK_RIGHT:
1233 case VK_DOWN:
1234 case VK_LEFT:
1235 case VK_UP:
1236 if (!(dlgCode & DLGC_WANTARROWS))
1238 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1239 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1240 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1241 return TRUE;
1243 break;
1245 case VK_CANCEL:
1246 case VK_ESCAPE:
1247 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1248 return TRUE;
1250 case VK_EXECUTE:
1251 case VK_RETURN:
1253 DWORD dw;
1254 if ((GetFocus() == msg->hwnd) &&
1255 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1257 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1259 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1261 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1262 if (!hwndDef || IsWindowEnabled(hwndDef))
1263 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1265 else
1267 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1271 return TRUE;
1273 break;
1275 case WM_CHAR:
1276 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1277 * It does NOT get sent in the test program I have
1279 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1280 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1281 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1282 /* drop through */
1284 case WM_SYSCHAR:
1285 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1287 /* don't translate or dispatch */
1288 return TRUE;
1290 break;
1293 TranslateMessage( msg );
1294 DispatchMessageW( msg );
1295 return TRUE;
1299 /***********************************************************************
1300 * GetDlgCtrlID (USER32.@)
1302 INT WINAPI GetDlgCtrlID( HWND hwnd )
1304 return GetWindowLongPtrW( hwnd, GWLP_ID );
1308 /***********************************************************************
1309 * GetDlgItem (USER32.@)
1311 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1313 int i;
1314 HWND *list = WIN_ListChildren( hwndDlg );
1315 HWND ret = 0;
1317 if (!list) return 0;
1319 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1320 ret = list[i];
1321 HeapFree( GetProcessHeap(), 0, list );
1322 return ret;
1326 /*******************************************************************
1327 * SendDlgItemMessageA (USER32.@)
1329 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1330 WPARAM wParam, LPARAM lParam )
1332 HWND hwndCtrl = GetDlgItem( hwnd, id );
1333 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1334 else return 0;
1338 /*******************************************************************
1339 * SendDlgItemMessageW (USER32.@)
1341 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1342 WPARAM wParam, LPARAM lParam )
1344 HWND hwndCtrl = GetDlgItem( hwnd, id );
1345 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1346 else return 0;
1350 /*******************************************************************
1351 * SetDlgItemTextA (USER32.@)
1353 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1355 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1359 /*******************************************************************
1360 * SetDlgItemTextW (USER32.@)
1362 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1364 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1368 /***********************************************************************
1369 * GetDlgItemTextA (USER32.@)
1371 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1373 if (str && (len > 0)) str[0] = '\0';
1374 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1375 len, (LPARAM)str );
1379 /***********************************************************************
1380 * GetDlgItemTextW (USER32.@)
1382 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1384 if (str && (len > 0)) str[0] = '\0';
1385 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1386 len, (LPARAM)str );
1390 /*******************************************************************
1391 * SetDlgItemInt (USER32.@)
1393 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1394 BOOL fSigned )
1396 char str[20];
1398 if (fSigned) sprintf( str, "%d", (INT)value );
1399 else sprintf( str, "%u", value );
1400 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1401 return TRUE;
1405 /***********************************************************************
1406 * GetDlgItemInt (USER32.@)
1408 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1409 BOOL fSigned )
1411 char str[30];
1412 char * endptr;
1413 LONG_PTR result = 0;
1415 if (translated) *translated = FALSE;
1416 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1417 return 0;
1418 if (fSigned)
1420 result = strtol( str, &endptr, 10 );
1421 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1422 return 0;
1423 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1424 return 0;
1426 else
1428 result = strtoul( str, &endptr, 10 );
1429 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1430 return 0;
1431 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1433 if (translated) *translated = TRUE;
1434 return (UINT)result;
1438 /***********************************************************************
1439 * CheckDlgButton (USER32.@)
1441 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1443 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1444 return TRUE;
1448 /***********************************************************************
1449 * IsDlgButtonChecked (USER32.@)
1451 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1453 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1457 /***********************************************************************
1458 * CheckRB
1460 * Callback function used to check/uncheck radio buttons that fall
1461 * within a specific range of IDs.
1463 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1465 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1466 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1468 if ((lChildID >= lpRadioGroup->firstID) &&
1469 (lChildID <= lpRadioGroup->lastID))
1471 if (lChildID == lpRadioGroup->checkID)
1473 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1475 else
1477 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1481 return TRUE;
1485 /***********************************************************************
1486 * CheckRadioButton (USER32.@)
1488 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1489 int lastID, int checkID )
1491 RADIOGROUP radioGroup;
1493 radioGroup.firstID = firstID;
1494 radioGroup.lastID = lastID;
1495 radioGroup.checkID = checkID;
1497 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1501 /***********************************************************************
1502 * GetDialogBaseUnits (USER.243)
1503 * GetDialogBaseUnits (USER32.@)
1505 DWORD WINAPI GetDialogBaseUnits(void)
1507 static DWORD units;
1509 if (!units)
1511 HDC hdc;
1512 SIZE size;
1514 if ((hdc = GetDC(0)))
1516 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1517 if (size.cx) units = MAKELONG( size.cx, size.cy );
1518 ReleaseDC( 0, hdc );
1520 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1522 return units;
1526 /***********************************************************************
1527 * MapDialogRect (USER32.@)
1529 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1531 DIALOGINFO * dlgInfo;
1532 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1533 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1534 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1535 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1536 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1537 return TRUE;
1541 /***********************************************************************
1542 * GetNextDlgGroupItem (USER32.@)
1544 * Corrections to MSDN documentation
1546 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1547 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1548 * 2. Prev of NULL or hwndDlg fails
1550 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1552 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1553 BOOL fLooped=FALSE;
1554 BOOL fSkipping=FALSE;
1556 hwndDlg = WIN_GetFullHandle( hwndDlg );
1557 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1559 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1560 if (!hwndCtrl && fPrevious) return 0;
1562 if (hwndCtrl)
1564 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1566 else
1568 /* No ctrl specified -> start from the beginning */
1569 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1570 /* MSDN is wrong. fPrevious does not result in the last child */
1572 /* Maybe that first one is valid. If so then we don't want to skip it*/
1573 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1575 return hwndCtrl;
1579 /* Always go forward around the group and list of controls; for the
1580 * previous control keep track; for the next break when you find one
1582 retvalue = hwndCtrl;
1583 hwnd = hwndCtrl;
1584 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1587 while (!hwndNext)
1589 /* Climb out until there is a next sibling of the ancestor or we
1590 * reach the top (in which case we loop back to the start)
1592 if (hwndDlg == GetParent (hwnd))
1594 /* Wrap around to the beginning of the list, within the same
1595 * group. (Once only)
1597 if (fLooped) goto end;
1598 fLooped = TRUE;
1599 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1601 else
1603 hwnd = GetParent (hwnd);
1604 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1607 hwnd = hwndNext;
1609 /* Wander down the leading edge of controlparents */
1610 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1611 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1612 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1613 hwnd = hwndNext;
1614 /* Question. If the control is a control parent but either has no
1615 * children or is not visible/enabled then if it has a WS_GROUP does
1616 * it count? For that matter does it count anyway?
1617 * I believe it doesn't count.
1620 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1622 hwndLastGroup = hwnd;
1623 if (!fSkipping)
1625 /* Look for the beginning of the group */
1626 fSkipping = TRUE;
1630 if (hwnd == hwndCtrl)
1632 if (!fSkipping) break;
1633 if (hwndLastGroup == hwnd) break;
1634 hwnd = hwndLastGroup;
1635 fSkipping = FALSE;
1636 fLooped = FALSE;
1639 if (!fSkipping &&
1640 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1641 WS_VISIBLE)
1643 retvalue = hwnd;
1644 if (!fPrevious) break;
1647 end:
1648 return retvalue;
1652 /***********************************************************************
1653 * DIALOG_GetNextTabItem
1655 * Recursive helper for GetNextDlgTabItem
1657 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1659 LONG dsStyle;
1660 LONG exStyle;
1661 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1662 HWND retWnd = 0;
1663 HWND hChildFirst = 0;
1665 if(!hwndCtrl)
1667 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1668 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1670 else if (IsChild( hwndMain, hwndCtrl ))
1672 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1673 if(!hChildFirst)
1675 if(GetParent(hwndCtrl) != hwndMain)
1676 /* i.e. if we are not at the top level of the recursion */
1677 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1678 else
1679 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1683 while(hChildFirst)
1685 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1686 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1687 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1689 HWND retWnd;
1690 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1691 if (retWnd) return (retWnd);
1693 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1695 return (hChildFirst);
1697 hChildFirst = GetWindow(hChildFirst,wndSearch);
1699 if(hwndCtrl)
1701 HWND hParent = GetParent(hwndCtrl);
1702 while(hParent)
1704 if(hParent == hwndMain) break;
1705 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1706 if(retWnd) break;
1707 hParent = GetParent(hParent);
1709 if(!retWnd)
1710 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1712 return retWnd ? retWnd : hwndCtrl;
1715 /***********************************************************************
1716 * GetNextDlgTabItem (USER32.@)
1718 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1719 BOOL fPrevious )
1721 hwndDlg = WIN_GetFullHandle( hwndDlg );
1722 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1724 /* Undocumented but tested under Win2000 and WinME */
1725 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1727 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1728 * NB GetLastError returns whatever was set before the function was
1729 * called.
1731 if (!hwndCtrl && fPrevious) return 0;
1733 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1736 /**********************************************************************
1737 * DIALOG_DlgDirSelect
1739 * Helper function for DlgDirSelect*
1741 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1742 INT id, BOOL unicode, BOOL combo )
1744 WCHAR *buffer, *ptr;
1745 INT item, size;
1746 BOOL ret;
1747 HWND listbox = GetDlgItem( hwnd, id );
1749 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1750 if (!listbox) return FALSE;
1752 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1753 if (item == LB_ERR) return FALSE;
1755 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1756 if (size == LB_ERR) return FALSE;
1758 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1760 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1762 if ((ret = (buffer[0] == '['))) /* drive or directory */
1764 if (buffer[1] == '-') /* drive */
1766 buffer[3] = ':';
1767 buffer[4] = 0;
1768 ptr = buffer + 2;
1770 else
1772 buffer[strlenW(buffer)-1] = '\\';
1773 ptr = buffer + 1;
1776 else
1778 /* Filenames without a dot extension must have one tacked at the end */
1779 if (strchrW(buffer, '.') == NULL)
1781 buffer[strlenW(buffer)+1] = '\0';
1782 buffer[strlenW(buffer)] = '.';
1784 ptr = buffer;
1787 if (!unicode)
1789 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1790 ((LPSTR)str)[len-1] = 0;
1792 else lstrcpynW( str, ptr, len );
1793 HeapFree( GetProcessHeap(), 0, buffer );
1794 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1795 return ret;
1799 /**********************************************************************
1800 * DIALOG_DlgDirListW
1802 * Helper function for DlgDirList*W
1804 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1805 INT idStatic, UINT attrib, BOOL combo )
1807 HWND hwnd;
1808 LPWSTR orig_spec = spec;
1809 WCHAR any[] = {'*','.','*',0};
1811 #define SENDMSG(msg,wparam,lparam) \
1812 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1813 : SendMessageW( hwnd, msg, wparam, lparam ))
1815 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1817 /* If the path exists and is a directory, chdir to it */
1818 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1819 else
1821 WCHAR *p, *p2;
1822 p = spec;
1823 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1824 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1825 if ((p2 = strrchrW( p, '/' ))) p = p2;
1826 if (p != spec)
1828 WCHAR sep = *p;
1829 *p = 0;
1830 if (!SetCurrentDirectoryW( spec ))
1832 *p = sep; /* Restore the original spec */
1833 return FALSE;
1835 spec = p + 1;
1839 TRACE( "mask=%s\n", debugstr_w(spec) );
1841 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1843 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1845 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1846 if (attrib & DDL_DIRECTORY)
1848 if (!(attrib & DDL_EXCLUSIVE))
1850 SENDMSG( combo ? CB_DIR : LB_DIR,
1851 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1852 (LPARAM)spec );
1854 SENDMSG( combo ? CB_DIR : LB_DIR,
1855 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1856 (LPARAM)any );
1858 else
1860 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1864 /* Convert path specification to uppercase */
1865 if (spec) CharUpperW(spec);
1867 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1869 WCHAR temp[MAX_PATH];
1870 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1871 CharLowerW( temp );
1872 /* Can't use PostMessage() here, because the string is on the stack */
1873 SetDlgItemTextW( hDlg, idStatic, temp );
1876 if (orig_spec && (spec != orig_spec))
1878 /* Update the original file spec */
1879 WCHAR *p = spec;
1880 while ((*orig_spec++ = *p++));
1883 return TRUE;
1884 #undef SENDMSG
1888 /**********************************************************************
1889 * DIALOG_DlgDirListA
1891 * Helper function for DlgDirList*A
1893 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1894 INT idStatic, UINT attrib, BOOL combo )
1896 if (spec)
1898 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1899 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1900 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1901 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1902 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1903 HeapFree( GetProcessHeap(), 0, specW );
1904 return ret;
1906 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1910 /**********************************************************************
1911 * DlgDirSelectExA (USER32.@)
1913 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1915 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1919 /**********************************************************************
1920 * DlgDirSelectExW (USER32.@)
1922 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1924 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1928 /**********************************************************************
1929 * DlgDirSelectComboBoxExA (USER32.@)
1931 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1932 INT id )
1934 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1938 /**********************************************************************
1939 * DlgDirSelectComboBoxExW (USER32.@)
1941 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1942 INT id)
1944 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1948 /**********************************************************************
1949 * DlgDirListA (USER32.@)
1951 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1952 INT idStatic, UINT attrib )
1954 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1958 /**********************************************************************
1959 * DlgDirListW (USER32.@)
1961 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1962 INT idStatic, UINT attrib )
1964 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1968 /**********************************************************************
1969 * DlgDirListComboBoxA (USER32.@)
1971 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1972 INT idStatic, UINT attrib )
1974 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1978 /**********************************************************************
1979 * DlgDirListComboBoxW (USER32.@)
1981 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1982 INT idStatic, UINT attrib )
1984 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );