gdi32/tests: Make use of todo_wine_if().
[wine.git] / dlls / user32 / dialog.c
blob0663693654e1150936c81e9e9fe697ecc5ab952e
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 (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
839 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
840 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner );
841 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
842 return 0;
846 /***********************************************************************
847 * DialogBoxParamW (USER32.@)
849 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
850 HWND owner, DLGPROC dlgProc, LPARAM param )
852 HWND hwnd;
853 HRSRC hrsrc;
854 LPCDLGTEMPLATEW ptr;
856 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
857 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
858 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner );
859 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
860 return 0;
864 /***********************************************************************
865 * DialogBoxIndirectParamAorW (USER32.@)
867 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
868 HWND owner, DLGPROC dlgProc,
869 LPARAM param, DWORD flags )
871 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
872 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
873 return -1;
876 /***********************************************************************
877 * DialogBoxIndirectParamA (USER32.@)
879 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
880 HWND owner, DLGPROC dlgProc, LPARAM param )
882 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
886 /***********************************************************************
887 * DialogBoxIndirectParamW (USER32.@)
889 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
890 HWND owner, DLGPROC dlgProc, LPARAM param )
892 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
895 /***********************************************************************
896 * EndDialog (USER32.@)
898 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
900 DIALOGINFO * dlgInfo;
901 HWND owner;
903 TRACE("%p %ld\n", hwnd, retval );
905 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
907 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
908 return FALSE;
910 dlgInfo->idResult = retval;
911 dlgInfo->flags |= DF_END;
913 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
914 if (owner)
915 EnableWindow( owner, TRUE );
917 /* Windows sets the focus to the dialog itself in EndDialog */
919 if (IsChild(hwnd, GetFocus()))
920 SetFocus( hwnd );
922 /* Don't have to send a ShowWindow(SW_HIDE), just do
923 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
925 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
926 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
928 if (hwnd == GetActiveWindow())
930 /* If this dialog was given an owner then set the focus to that owner. */
931 if (owner)
932 SetForegroundWindow( owner );
933 else
934 WINPOS_ActivateOtherWindow( hwnd );
937 /* unblock dialog loop */
938 PostMessageA(hwnd, WM_NULL, 0, 0);
939 return TRUE;
943 /***********************************************************************
944 * DIALOG_IsAccelerator
946 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
948 HWND hwndControl = hwnd;
949 HWND hwndNext;
950 INT dlgCode;
951 WCHAR buffer[128];
955 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
956 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
958 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
959 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
960 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
962 /* find the accelerator key */
963 LPWSTR p = buffer - 2;
967 p = strchrW( p + 2, '&' );
969 while (p != NULL && p[1] == '&');
971 /* and check if it's the one we're looking for */
972 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
974 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
976 /* set focus to the control */
977 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
978 /* and bump it on to next */
979 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
981 else if (dlgCode & DLGC_BUTTON)
983 /* send BM_CLICK message to the control */
984 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
986 return TRUE;
989 hwndNext = GetWindow( hwndControl, GW_CHILD );
991 else hwndNext = 0;
993 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
995 while (!hwndNext && hwndControl)
997 hwndControl = GetParent( hwndControl );
998 if (hwndControl == hwndDlg)
1000 if(hwnd==hwndDlg) /* prevent endless loop */
1002 hwndNext=hwnd;
1003 break;
1005 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1007 else
1008 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1010 hwndControl = hwndNext;
1012 while (hwndControl && (hwndControl != hwnd));
1014 return FALSE;
1017 /***********************************************************************
1018 * DIALOG_FindMsgDestination
1020 * The messages that IsDialogMessage sends may not go to the dialog
1021 * calling IsDialogMessage if that dialog is a child, and it has the
1022 * DS_CONTROL style set.
1023 * We propagate up until we hit one that does not have DS_CONTROL, or
1024 * whose parent is not a dialog.
1026 * This is undocumented behaviour.
1028 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1030 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1032 WND *pParent;
1033 HWND hParent = GetParent(hwndDlg);
1034 if (!hParent) break;
1036 pParent = WIN_GetPtr(hParent);
1037 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1039 if (!pParent->dlgInfo)
1041 WIN_ReleasePtr(pParent);
1042 break;
1044 WIN_ReleasePtr(pParent);
1046 hwndDlg = hParent;
1049 return hwndDlg;
1052 /***********************************************************************
1053 * DIALOG_FixOneChildOnChangeFocus
1055 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1058 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1059 LPARAM lParam)
1061 /* If a default pushbutton then no longer default */
1062 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1063 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1064 return TRUE;
1067 /***********************************************************************
1068 * DIALOG_FixChildrenOnChangeFocus
1070 * Following the change of focus that occurs for example after handling
1071 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1072 * children may be required.
1074 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1076 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1077 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1078 /* Windows does ask for this. I don't know why yet */
1080 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1082 /* If the button that is getting the focus WAS flagged as the default
1083 * pushbutton then ask the dialog what it thinks the default is and
1084 * set that in the default style.
1086 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1088 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1089 if (HIWORD(def_id) == DC_HASDEFID)
1091 HWND hwndDef;
1092 def_id = LOWORD(def_id);
1093 hwndDef = GetDlgItem (hwndDlg, def_id);
1094 if (hwndDef)
1096 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1097 /* I know that if it is a button then it should already be a
1098 * UNDEFPUSHBUTTON, since we have just told the buttons to
1099 * change style. But maybe they ignored our request
1101 if ((dlgcode_def & DLGC_BUTTON) &&
1102 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1104 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1109 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1111 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1112 /* I wonder why it doesn't send a DM_SETDEFID */
1116 /***********************************************************************
1117 * DIALOG_IdToHwnd
1119 * A recursive version of GetDlgItem
1121 * RETURNS
1122 * The HWND for a Child ID.
1124 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1126 int i;
1127 HWND *list = WIN_ListChildren( hwndDlg );
1128 HWND ret = 0;
1130 if (!list) return 0;
1132 for (i = 0; list[i]; i++)
1134 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1136 ret = list[i];
1137 break;
1140 /* Recurse into every child */
1141 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1144 HeapFree( GetProcessHeap(), 0, list );
1145 return ret;
1148 /***********************************************************************
1149 * IsDialogMessageW (USER32.@)
1151 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1153 INT dlgCode;
1155 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1157 hwndDlg = WIN_GetFullHandle( hwndDlg );
1158 if (is_desktop_window(hwndDlg)) return FALSE;
1159 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1161 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1163 switch(msg->message)
1165 case WM_KEYDOWN:
1166 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1167 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1169 switch(msg->wParam)
1171 case VK_TAB:
1172 if (!(dlgCode & DLGC_WANTTAB))
1174 BOOL fIsDialog = TRUE;
1175 WND *pWnd = WIN_GetPtr( hwndDlg );
1177 if (pWnd && pWnd != WND_OTHER_PROCESS)
1179 fIsDialog = (pWnd->dlgInfo != NULL);
1180 WIN_ReleasePtr(pWnd);
1183 /* I am not sure under which circumstances the TAB is handled
1184 * each way. All I do know is that it does not always simply
1185 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1186 * do so but I presume someone has)
1188 if (fIsDialog)
1189 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1190 else
1192 /* It would appear that GetNextDlgTabItem can handle being
1193 * passed hwndDlg rather than NULL but that is undocumented
1194 * so let's do it properly
1196 HWND hwndFocus = GetFocus();
1197 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1198 hwndFocus == hwndDlg ? NULL : hwndFocus,
1199 GetKeyState (VK_SHIFT) & 0x8000);
1200 if (hwndNext)
1202 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1203 msg->wParam, (LPARAM)msg);
1204 if (dlgCode & DLGC_HASSETSEL)
1206 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1207 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1208 if (buffer)
1210 INT length;
1211 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1212 length = strlenW (buffer);
1213 HeapFree (GetProcessHeap(), 0, buffer);
1214 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1217 SetFocus (hwndNext);
1218 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1220 else
1221 return FALSE;
1223 return TRUE;
1225 break;
1227 case VK_RIGHT:
1228 case VK_DOWN:
1229 case VK_LEFT:
1230 case VK_UP:
1231 if (!(dlgCode & DLGC_WANTARROWS))
1233 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1234 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1235 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1236 return TRUE;
1238 break;
1240 case VK_CANCEL:
1241 case VK_ESCAPE:
1242 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1243 return TRUE;
1245 case VK_EXECUTE:
1246 case VK_RETURN:
1248 DWORD dw;
1249 if ((GetFocus() == msg->hwnd) &&
1250 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1252 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1254 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1256 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1257 if (!hwndDef || IsWindowEnabled(hwndDef))
1258 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1260 else
1262 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1266 return TRUE;
1268 break;
1270 case WM_CHAR:
1271 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1272 * It does NOT get sent in the test program I have
1274 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1275 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1276 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1277 /* drop through */
1279 case WM_SYSCHAR:
1280 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1282 /* don't translate or dispatch */
1283 return TRUE;
1285 break;
1288 TranslateMessage( msg );
1289 DispatchMessageW( msg );
1290 return TRUE;
1294 /***********************************************************************
1295 * GetDlgCtrlID (USER32.@)
1297 INT WINAPI GetDlgCtrlID( HWND hwnd )
1299 return GetWindowLongPtrW( hwnd, GWLP_ID );
1303 /***********************************************************************
1304 * GetDlgItem (USER32.@)
1306 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1308 int i;
1309 HWND *list = WIN_ListChildren( hwndDlg );
1310 HWND ret = 0;
1312 if (!list) return 0;
1314 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1315 ret = list[i];
1316 HeapFree( GetProcessHeap(), 0, list );
1317 return ret;
1321 /*******************************************************************
1322 * SendDlgItemMessageA (USER32.@)
1324 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1325 WPARAM wParam, LPARAM lParam )
1327 HWND hwndCtrl = GetDlgItem( hwnd, id );
1328 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1329 else return 0;
1333 /*******************************************************************
1334 * SendDlgItemMessageW (USER32.@)
1336 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1337 WPARAM wParam, LPARAM lParam )
1339 HWND hwndCtrl = GetDlgItem( hwnd, id );
1340 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1341 else return 0;
1345 /*******************************************************************
1346 * SetDlgItemTextA (USER32.@)
1348 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1350 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1354 /*******************************************************************
1355 * SetDlgItemTextW (USER32.@)
1357 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1359 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1363 /***********************************************************************
1364 * GetDlgItemTextA (USER32.@)
1366 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1368 if (str && (len > 0)) str[0] = '\0';
1369 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1370 len, (LPARAM)str );
1374 /***********************************************************************
1375 * GetDlgItemTextW (USER32.@)
1377 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1379 if (str && (len > 0)) str[0] = '\0';
1380 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1381 len, (LPARAM)str );
1385 /*******************************************************************
1386 * SetDlgItemInt (USER32.@)
1388 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1389 BOOL fSigned )
1391 char str[20];
1393 if (fSigned) sprintf( str, "%d", (INT)value );
1394 else sprintf( str, "%u", value );
1395 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1396 return TRUE;
1400 /***********************************************************************
1401 * GetDlgItemInt (USER32.@)
1403 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1404 BOOL fSigned )
1406 char str[30];
1407 char * endptr;
1408 LONG_PTR result = 0;
1410 if (translated) *translated = FALSE;
1411 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1412 return 0;
1413 if (fSigned)
1415 result = strtol( str, &endptr, 10 );
1416 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1417 return 0;
1418 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1419 return 0;
1421 else
1423 result = strtoul( str, &endptr, 10 );
1424 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1425 return 0;
1426 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1428 if (translated) *translated = TRUE;
1429 return (UINT)result;
1433 /***********************************************************************
1434 * CheckDlgButton (USER32.@)
1436 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1438 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1439 return TRUE;
1443 /***********************************************************************
1444 * IsDlgButtonChecked (USER32.@)
1446 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1448 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1452 /***********************************************************************
1453 * CheckRB
1455 * Callback function used to check/uncheck radio buttons that fall
1456 * within a specific range of IDs.
1458 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1460 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1461 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1463 if ((lChildID >= lpRadioGroup->firstID) &&
1464 (lChildID <= lpRadioGroup->lastID))
1466 if (lChildID == lpRadioGroup->checkID)
1468 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1470 else
1472 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1476 return TRUE;
1480 /***********************************************************************
1481 * CheckRadioButton (USER32.@)
1483 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1484 int lastID, int checkID )
1486 RADIOGROUP radioGroup;
1488 radioGroup.firstID = firstID;
1489 radioGroup.lastID = lastID;
1490 radioGroup.checkID = checkID;
1492 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1496 /***********************************************************************
1497 * GetDialogBaseUnits (USER.243)
1498 * GetDialogBaseUnits (USER32.@)
1500 DWORD WINAPI GetDialogBaseUnits(void)
1502 static DWORD units;
1504 if (!units)
1506 HDC hdc;
1507 SIZE size;
1509 if ((hdc = GetDC(0)))
1511 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1512 if (size.cx) units = MAKELONG( size.cx, size.cy );
1513 ReleaseDC( 0, hdc );
1515 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1517 return units;
1521 /***********************************************************************
1522 * MapDialogRect (USER32.@)
1524 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1526 DIALOGINFO * dlgInfo;
1527 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1528 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1529 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1530 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1531 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1532 return TRUE;
1536 /***********************************************************************
1537 * GetNextDlgGroupItem (USER32.@)
1539 * Corrections to MSDN documentation
1541 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1542 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1543 * 2. Prev of NULL or hwndDlg fails
1545 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1547 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1548 BOOL fLooped=FALSE;
1549 BOOL fSkipping=FALSE;
1551 hwndDlg = WIN_GetFullHandle( hwndDlg );
1552 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1554 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1555 if (!hwndCtrl && fPrevious) return 0;
1557 if (hwndCtrl)
1559 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1561 else
1563 /* No ctrl specified -> start from the beginning */
1564 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1565 /* MSDN is wrong. fPrevious does not result in the last child */
1567 /* Maybe that first one is valid. If so then we don't want to skip it*/
1568 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1570 return hwndCtrl;
1574 /* Always go forward around the group and list of controls; for the
1575 * previous control keep track; for the next break when you find one
1577 retvalue = hwndCtrl;
1578 hwnd = hwndCtrl;
1579 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1582 while (!hwndNext)
1584 /* Climb out until there is a next sibling of the ancestor or we
1585 * reach the top (in which case we loop back to the start)
1587 if (hwndDlg == GetParent (hwnd))
1589 /* Wrap around to the beginning of the list, within the same
1590 * group. (Once only)
1592 if (fLooped) goto end;
1593 fLooped = TRUE;
1594 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1596 else
1598 hwnd = GetParent (hwnd);
1599 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1602 hwnd = hwndNext;
1604 /* Wander down the leading edge of controlparents */
1605 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1606 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1607 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1608 hwnd = hwndNext;
1609 /* Question. If the control is a control parent but either has no
1610 * children or is not visible/enabled then if it has a WS_GROUP does
1611 * it count? For that matter does it count anyway?
1612 * I believe it doesn't count.
1615 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1617 hwndLastGroup = hwnd;
1618 if (!fSkipping)
1620 /* Look for the beginning of the group */
1621 fSkipping = TRUE;
1625 if (hwnd == hwndCtrl)
1627 if (!fSkipping) break;
1628 if (hwndLastGroup == hwnd) break;
1629 hwnd = hwndLastGroup;
1630 fSkipping = FALSE;
1631 fLooped = FALSE;
1634 if (!fSkipping &&
1635 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1636 WS_VISIBLE)
1638 retvalue = hwnd;
1639 if (!fPrevious) break;
1642 end:
1643 return retvalue;
1647 /***********************************************************************
1648 * DIALOG_GetNextTabItem
1650 * Recursive helper for GetNextDlgTabItem
1652 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1654 LONG dsStyle;
1655 LONG exStyle;
1656 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1657 HWND retWnd = 0;
1658 HWND hChildFirst = 0;
1660 if(!hwndCtrl)
1662 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1663 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1665 else if (IsChild( hwndMain, hwndCtrl ))
1667 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1668 if(!hChildFirst)
1670 if(GetParent(hwndCtrl) != hwndMain)
1671 /* i.e. if we are not at the top level of the recursion */
1672 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1673 else
1674 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1678 while(hChildFirst)
1680 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1681 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1682 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1684 HWND retWnd;
1685 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1686 if (retWnd) return (retWnd);
1688 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1690 return (hChildFirst);
1692 hChildFirst = GetWindow(hChildFirst,wndSearch);
1694 if(hwndCtrl)
1696 HWND hParent = GetParent(hwndCtrl);
1697 while(hParent)
1699 if(hParent == hwndMain) break;
1700 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1701 if(retWnd) break;
1702 hParent = GetParent(hParent);
1704 if(!retWnd)
1705 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1707 return retWnd ? retWnd : hwndCtrl;
1710 /***********************************************************************
1711 * GetNextDlgTabItem (USER32.@)
1713 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1714 BOOL fPrevious )
1716 hwndDlg = WIN_GetFullHandle( hwndDlg );
1717 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1719 /* Undocumented but tested under Win2000 and WinME */
1720 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1722 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1723 * NB GetLastError returns whatever was set before the function was
1724 * called.
1726 if (!hwndCtrl && fPrevious) return 0;
1728 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1731 /**********************************************************************
1732 * DIALOG_DlgDirSelect
1734 * Helper function for DlgDirSelect*
1736 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1737 INT id, BOOL unicode, BOOL combo )
1739 WCHAR *buffer, *ptr;
1740 INT item, size;
1741 BOOL ret;
1742 HWND listbox = GetDlgItem( hwnd, id );
1744 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1745 if (!listbox) return FALSE;
1747 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1748 if (item == LB_ERR) return FALSE;
1750 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1751 if (size == LB_ERR) return FALSE;
1753 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1755 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1757 if ((ret = (buffer[0] == '['))) /* drive or directory */
1759 if (buffer[1] == '-') /* drive */
1761 buffer[3] = ':';
1762 buffer[4] = 0;
1763 ptr = buffer + 2;
1765 else
1767 buffer[strlenW(buffer)-1] = '\\';
1768 ptr = buffer + 1;
1771 else
1773 /* Filenames without a dot extension must have one tacked at the end */
1774 if (strchrW(buffer, '.') == NULL)
1776 buffer[strlenW(buffer)+1] = '\0';
1777 buffer[strlenW(buffer)] = '.';
1779 ptr = buffer;
1782 if (!unicode)
1784 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1785 ((LPSTR)str)[len-1] = 0;
1787 else lstrcpynW( str, ptr, len );
1788 HeapFree( GetProcessHeap(), 0, buffer );
1789 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1790 return ret;
1794 /**********************************************************************
1795 * DIALOG_DlgDirListW
1797 * Helper function for DlgDirList*W
1799 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1800 INT idStatic, UINT attrib, BOOL combo )
1802 HWND hwnd;
1803 LPWSTR orig_spec = spec;
1804 WCHAR any[] = {'*','.','*',0};
1806 #define SENDMSG(msg,wparam,lparam) \
1807 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1808 : SendMessageW( hwnd, msg, wparam, lparam ))
1810 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1812 /* If the path exists and is a directory, chdir to it */
1813 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1814 else
1816 WCHAR *p, *p2;
1817 p = spec;
1818 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1819 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1820 if ((p2 = strrchrW( p, '/' ))) p = p2;
1821 if (p != spec)
1823 WCHAR sep = *p;
1824 *p = 0;
1825 if (!SetCurrentDirectoryW( spec ))
1827 *p = sep; /* Restore the original spec */
1828 return FALSE;
1830 spec = p + 1;
1834 TRACE( "mask=%s\n", debugstr_w(spec) );
1836 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1838 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1840 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1841 if (attrib & DDL_DIRECTORY)
1843 if (!(attrib & DDL_EXCLUSIVE))
1845 SENDMSG( combo ? CB_DIR : LB_DIR,
1846 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1847 (LPARAM)spec );
1849 SENDMSG( combo ? CB_DIR : LB_DIR,
1850 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1851 (LPARAM)any );
1853 else
1855 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1859 /* Convert path specification to uppercase */
1860 if (spec) CharUpperW(spec);
1862 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1864 WCHAR temp[MAX_PATH];
1865 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1866 CharLowerW( temp );
1867 /* Can't use PostMessage() here, because the string is on the stack */
1868 SetDlgItemTextW( hDlg, idStatic, temp );
1871 if (orig_spec && (spec != orig_spec))
1873 /* Update the original file spec */
1874 WCHAR *p = spec;
1875 while ((*orig_spec++ = *p++));
1878 return TRUE;
1879 #undef SENDMSG
1883 /**********************************************************************
1884 * DIALOG_DlgDirListA
1886 * Helper function for DlgDirList*A
1888 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1889 INT idStatic, UINT attrib, BOOL combo )
1891 if (spec)
1893 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1894 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1895 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1896 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1897 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1898 HeapFree( GetProcessHeap(), 0, specW );
1899 return ret;
1901 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1905 /**********************************************************************
1906 * DlgDirSelectExA (USER32.@)
1908 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1910 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1914 /**********************************************************************
1915 * DlgDirSelectExW (USER32.@)
1917 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1919 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1923 /**********************************************************************
1924 * DlgDirSelectComboBoxExA (USER32.@)
1926 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1927 INT id )
1929 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1933 /**********************************************************************
1934 * DlgDirSelectComboBoxExW (USER32.@)
1936 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1937 INT id)
1939 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1943 /**********************************************************************
1944 * DlgDirListA (USER32.@)
1946 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1947 INT idStatic, UINT attrib )
1949 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1953 /**********************************************************************
1954 * DlgDirListW (USER32.@)
1956 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1957 INT idStatic, UINT attrib )
1959 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1963 /**********************************************************************
1964 * DlgDirListComboBoxA (USER32.@)
1966 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1967 INT idStatic, UINT attrib )
1969 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1973 /**********************************************************************
1974 * DlgDirListComboBoxW (USER32.@)
1976 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1977 INT idStatic, UINT attrib )
1979 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );