msi: Add more join tests.
[wine/wine64.git] / dlls / user / dialog.c
blob6eaa942208760ea7a24257a0490c830d218fce07
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/winuser16.h"
38 #include "wine/unicode.h"
39 #include "controls.h"
40 #include "win.h"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
47 /* Dialog control information */
48 typedef struct
50 DWORD style;
51 DWORD exStyle;
52 DWORD helpId;
53 INT16 x;
54 INT16 y;
55 INT16 cx;
56 INT16 cy;
57 UINT_PTR id;
58 LPCWSTR className;
59 LPCWSTR windowName;
60 LPCVOID data;
61 } DLG_CONTROL_INFO;
63 /* Dialog template */
64 typedef struct
66 DWORD style;
67 DWORD exStyle;
68 DWORD helpId;
69 UINT16 nbItems;
70 INT16 x;
71 INT16 y;
72 INT16 cx;
73 INT16 cy;
74 LPCWSTR menuName;
75 LPCWSTR className;
76 LPCWSTR caption;
77 INT16 pointSize;
78 WORD weight;
79 BOOL italic;
80 LPCWSTR faceName;
81 BOOL dialogEx;
82 } DLG_TEMPLATE;
84 /* Radio button group */
85 typedef struct
87 UINT firstID;
88 UINT lastID;
89 UINT checkID;
90 } RADIOGROUP;
93 /*********************************************************************
94 * dialog class descriptor
96 const struct builtin_class_descr DIALOG_builtin_class =
98 DIALOG_CLASS_ATOMA, /* name */
99 CS_SAVEBITS | CS_DBLCLKS, /* style */
100 DefDlgProcA, /* procA */
101 DefDlgProcW, /* procW */
102 DLGWINDOWEXTRA, /* extra */
103 IDC_ARROW, /* cursor */
104 0 /* brush */
108 /***********************************************************************
109 * DIALOG_EnableOwner
111 * Helper function for modal dialogs to enable again the
112 * owner of the dialog box.
114 void DIALOG_EnableOwner( HWND hOwner )
116 /* Owner must be a top-level window */
117 if (hOwner)
118 hOwner = GetAncestor( hOwner, GA_ROOT );
119 if (!hOwner) return;
120 EnableWindow( hOwner, TRUE );
124 /***********************************************************************
125 * DIALOG_DisableOwner
127 * Helper function for modal dialogs to disable the
128 * owner of the dialog box. Returns TRUE if owner was enabled.
130 BOOL DIALOG_DisableOwner( HWND hOwner )
132 /* Owner must be a top-level window */
133 if (hOwner)
134 hOwner = GetAncestor( hOwner, GA_ROOT );
135 if (!hOwner) return FALSE;
136 if (IsWindowEnabled( hOwner ))
138 EnableWindow( hOwner, FALSE );
139 return TRUE;
141 else
142 return FALSE;
145 /***********************************************************************
146 * DIALOG_GetControl32
148 * Return the class and text of the control pointed to by ptr,
149 * fill the header structure and return a pointer to the next control.
151 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
152 BOOL dialogEx )
154 if (dialogEx)
156 info->helpId = GET_DWORD(p); p += 2;
157 info->exStyle = GET_DWORD(p); p += 2;
158 info->style = GET_DWORD(p); p += 2;
160 else
162 info->helpId = 0;
163 info->style = GET_DWORD(p); p += 2;
164 info->exStyle = GET_DWORD(p); p += 2;
166 info->x = GET_WORD(p); p++;
167 info->y = GET_WORD(p); p++;
168 info->cx = GET_WORD(p); p++;
169 info->cy = GET_WORD(p); p++;
171 if (dialogEx)
173 /* id is a DWORD for DIALOGEX */
174 info->id = GET_DWORD(p);
175 p += 2;
177 else
179 info->id = GET_WORD(p);
180 p++;
183 if (GET_WORD(p) == 0xffff)
185 static const WCHAR class_names[6][10] =
187 { 'B','u','t','t','o','n', }, /* 0x80 */
188 { 'E','d','i','t', }, /* 0x81 */
189 { 'S','t','a','t','i','c', }, /* 0x82 */
190 { 'L','i','s','t','B','o','x', }, /* 0x83 */
191 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
192 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
194 WORD id = GET_WORD(p+1);
195 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
196 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
197 if (id <= 5)
198 info->className = class_names[id];
199 else
201 info->className = NULL;
202 ERR("Unknown built-in class id %04x\n", id );
204 p += 2;
206 else
208 info->className = (LPCWSTR)p;
209 p += strlenW( info->className ) + 1;
212 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
214 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
215 p += 2;
217 else
219 info->windowName = (LPCWSTR)p;
220 p += strlenW( info->windowName ) + 1;
223 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
224 debugstr_w( info->className ), debugstr_w( info->windowName ),
225 info->id, info->x, info->y, info->cx, info->cy,
226 info->style, info->exStyle, info->helpId );
228 if (GET_WORD(p))
230 if (TRACE_ON(dialog))
232 WORD i, count = GET_WORD(p) / sizeof(WORD);
233 TRACE(" BEGIN\n");
234 TRACE(" ");
235 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
236 TRACE("\n");
237 TRACE(" END\n" );
239 info->data = p + 1;
240 p += GET_WORD(p) / sizeof(WORD);
242 else info->data = NULL;
243 p++;
245 /* Next control is on dword boundary */
246 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
250 /***********************************************************************
251 * DIALOG_CreateControls32
253 * Create the control windows for a dialog.
255 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
256 HINSTANCE hInst, BOOL unicode )
258 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
259 DLG_CONTROL_INFO info;
260 HWND hwndCtrl, hwndDefButton = 0;
261 INT items = dlgTemplate->nbItems;
263 TRACE(" BEGIN\n" );
264 while (items--)
266 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
267 dlgTemplate->dialogEx );
268 /* Is this it? */
269 if (info.style & WS_BORDER)
271 info.style &= ~WS_BORDER;
272 info.exStyle |= WS_EX_CLIENTEDGE;
274 if (unicode)
276 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
277 info.className, info.windowName,
278 info.style | WS_CHILD,
279 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
281 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
282 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
283 hwnd, (HMENU)info.id,
284 hInst, (LPVOID)info.data );
286 else
288 LPSTR class = (LPSTR)info.className;
289 LPSTR caption = (LPSTR)info.windowName;
291 if (HIWORD(class))
293 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
294 class = HeapAlloc( GetProcessHeap(), 0, len );
295 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
297 if (HIWORD(caption))
299 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
300 caption = HeapAlloc( GetProcessHeap(), 0, len );
301 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
303 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
304 class, caption, info.style | WS_CHILD,
305 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
306 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
307 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
308 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
309 hwnd, (HMENU)info.id,
310 hInst, (LPVOID)info.data );
311 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
312 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
314 if (!hwndCtrl)
316 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
317 return FALSE;
320 /* Send initialisation messages to the control */
321 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
322 (WPARAM)dlgInfo->hUserFont, 0 );
323 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
325 /* If there's already a default push-button, set it back */
326 /* to normal and use this one instead. */
327 if (hwndDefButton)
328 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
329 hwndDefButton = hwndCtrl;
330 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
333 TRACE(" END\n" );
334 return TRUE;
338 /***********************************************************************
339 * DIALOG_ParseTemplate32
341 * Fill a DLG_TEMPLATE structure from the dialog template, and return
342 * a pointer to the first control.
344 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
346 const WORD *p = (const WORD *)template;
347 WORD signature;
348 WORD dlgver;
350 signature = GET_WORD(p); p++;
351 dlgver = GET_WORD(p); p++;
353 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
355 result->dialogEx = TRUE;
356 result->helpId = GET_DWORD(p); p += 2;
357 result->exStyle = GET_DWORD(p); p += 2;
358 result->style = GET_DWORD(p); p += 2;
360 else
362 result->style = GET_DWORD(p - 2);
363 result->dialogEx = FALSE;
364 result->helpId = 0;
365 result->exStyle = GET_DWORD(p); p += 2;
367 result->nbItems = GET_WORD(p); p++;
368 result->x = GET_WORD(p); p++;
369 result->y = GET_WORD(p); p++;
370 result->cx = GET_WORD(p); p++;
371 result->cy = GET_WORD(p); p++;
372 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
373 result->dialogEx ? "EX" : "", result->x, result->y,
374 result->cx, result->cy, result->helpId );
375 TRACE(" STYLE 0x%08lx\n", result->style );
376 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
378 /* Get the menu name */
380 switch(GET_WORD(p))
382 case 0x0000:
383 result->menuName = NULL;
384 p++;
385 break;
386 case 0xffff:
387 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
388 p += 2;
389 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
390 break;
391 default:
392 result->menuName = (LPCWSTR)p;
393 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
394 p += strlenW( result->menuName ) + 1;
395 break;
398 /* Get the class name */
400 switch(GET_WORD(p))
402 case 0x0000:
403 result->className = DIALOG_CLASS_ATOMW;
404 p++;
405 break;
406 case 0xffff:
407 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
408 p += 2;
409 TRACE(" CLASS %04x\n", LOWORD(result->className) );
410 break;
411 default:
412 result->className = (LPCWSTR)p;
413 TRACE(" CLASS %s\n", debugstr_w( result->className ));
414 p += strlenW( result->className ) + 1;
415 break;
418 /* Get the window caption */
420 result->caption = (LPCWSTR)p;
421 p += strlenW( result->caption ) + 1;
422 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
424 /* Get the font name */
426 result->pointSize = 0;
427 result->faceName = NULL;
428 result->weight = FW_DONTCARE;
429 result->italic = FALSE;
431 if (result->style & DS_SETFONT)
433 result->pointSize = GET_WORD(p);
434 p++;
435 if (result->dialogEx)
437 result->weight = GET_WORD(p); p++;
438 result->italic = LOBYTE(GET_WORD(p)); p++;
440 result->faceName = (LPCWSTR)p;
441 p += strlenW( result->faceName ) + 1;
442 TRACE(" FONT %d, %s, %d, %s\n",
443 result->pointSize, debugstr_w( result->faceName ),
444 result->weight, result->italic ? "TRUE" : "FALSE" );
447 /* First control is on dword boundary */
448 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
452 /***********************************************************************
453 * DIALOG_CreateIndirect
454 * Creates a dialog box window
456 * modal = TRUE if we are called from a modal dialog box.
457 * (it's more compatible to do it here, as under Windows the owner
458 * is never disabled if the dialog fails because of an invalid template)
460 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
461 HWND owner, DLGPROC dlgProc, LPARAM param,
462 BOOL unicode, BOOL modal )
464 HWND hwnd;
465 RECT rect;
466 DLG_TEMPLATE template;
467 DIALOGINFO * dlgInfo = NULL;
468 DWORD units = GetDialogBaseUnits();
469 BOOL ownerEnabled = TRUE;
470 HMENU hMenu = 0;
471 HFONT hUserFont = 0;
472 UINT flags = 0;
473 UINT xBaseUnit = LOWORD(units);
474 UINT yBaseUnit = HIWORD(units);
476 /* Parse dialog template */
478 if (!dlgTemplate) return 0;
479 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
481 /* Load menu */
483 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
485 /* Create custom font if needed */
487 if (template.style & DS_SETFONT)
489 /* We convert the size to pixels and then make it -ve. This works
490 * for both +ve and -ve template.pointSize */
491 HDC dc;
492 int pixels;
493 dc = GetDC(0);
494 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
495 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
496 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
497 PROOF_QUALITY, FF_DONTCARE,
498 template.faceName );
499 if (hUserFont)
501 SIZE charSize;
502 HFONT hOldFont = SelectObject( dc, hUserFont );
503 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
504 if (charSize.cx)
506 xBaseUnit = charSize.cx;
507 yBaseUnit = charSize.cy;
509 SelectObject( dc, hOldFont );
511 ReleaseDC(0, dc);
512 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
515 /* Create dialog main window */
517 rect.left = rect.top = 0;
518 rect.right = MulDiv(template.cx, xBaseUnit, 4);
519 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
520 if (template.style & WS_CHILD)
521 template.style &= ~(WS_CAPTION|WS_SYSMENU);
522 if (template.style & DS_MODALFRAME)
523 template.exStyle |= WS_EX_DLGMODALFRAME;
524 if (template.style & DS_CONTROL)
525 template.exStyle |= WS_EX_CONTROLPARENT;
526 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
527 rect.right -= rect.left;
528 rect.bottom -= rect.top;
530 if (template.x == CW_USEDEFAULT16)
532 rect.left = rect.top = CW_USEDEFAULT;
534 else
536 if (template.style & DS_CENTER)
538 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
539 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
541 else
543 rect.left += MulDiv(template.x, xBaseUnit, 4);
544 rect.top += MulDiv(template.y, yBaseUnit, 8);
546 if ( !(template.style & WS_CHILD) )
548 INT dX, dY;
550 if( !(template.style & DS_ABSALIGN) )
551 ClientToScreen( owner, (POINT *)&rect );
553 /* try to fit it into the desktop */
555 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
556 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
557 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
558 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
559 if( rect.left < 0 ) rect.left = 0;
560 if( rect.top < 0 ) rect.top = 0;
564 if (modal)
566 ownerEnabled = DIALOG_DisableOwner( owner );
567 if (ownerEnabled) flags |= DF_OWNERENABLED;
570 if (unicode)
572 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
573 template.style & ~WS_VISIBLE,
574 rect.left, rect.top, rect.right, rect.bottom,
575 owner, hMenu, hInst, NULL );
577 else
579 LPSTR class = (LPSTR)template.className;
580 LPSTR caption = (LPSTR)template.caption;
582 if (HIWORD(class))
584 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
585 class = HeapAlloc( GetProcessHeap(), 0, len );
586 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
588 if (HIWORD(caption))
590 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
591 caption = HeapAlloc( GetProcessHeap(), 0, len );
592 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
594 hwnd = CreateWindowExA(template.exStyle, class, caption,
595 template.style & ~WS_VISIBLE,
596 rect.left, rect.top, rect.right, rect.bottom,
597 owner, hMenu, hInst, NULL );
598 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
599 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
602 if (!hwnd)
604 if (hUserFont) DeleteObject( hUserFont );
605 if (hMenu) DestroyMenu( hMenu );
606 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
607 return 0;
610 /* moved this from the top of the method to here as DIALOGINFO structure
611 will be valid only after WM_CREATE message has been handled in DefDlgProc
612 All the members of the structure get filled here using temp variables */
613 dlgInfo = DIALOG_get_info( hwnd, TRUE );
614 dlgInfo->hwndFocus = 0;
615 dlgInfo->hUserFont = hUserFont;
616 dlgInfo->hMenu = hMenu;
617 dlgInfo->xBaseUnit = xBaseUnit;
618 dlgInfo->yBaseUnit = yBaseUnit;
619 dlgInfo->idResult = 0;
620 dlgInfo->flags = flags;
621 dlgInfo->hDialogHeap = 0;
623 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
625 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
626 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
628 if (dlgInfo->hUserFont)
629 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
631 /* Create controls */
633 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
635 /* Send initialisation messages and set focus */
637 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
638 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
640 /* By returning TRUE, app has requested a default focus assignment */
641 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
642 if( dlgInfo->hwndFocus )
643 SetFocus( dlgInfo->hwndFocus );
646 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
648 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
650 return hwnd;
652 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
653 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
654 return 0;
658 /***********************************************************************
659 * CreateDialogParamA (USER32.@)
661 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
662 DLGPROC dlgProc, LPARAM param )
664 HRSRC hrsrc;
665 LPCDLGTEMPLATEA ptr;
667 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
668 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
669 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
673 /***********************************************************************
674 * CreateDialogParamW (USER32.@)
676 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
677 DLGPROC dlgProc, LPARAM param )
679 HRSRC hrsrc;
680 LPCDLGTEMPLATEA ptr;
682 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
683 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
684 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
688 /***********************************************************************
689 * CreateDialogIndirectParamAorW (USER32.@)
691 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
692 HWND owner, DLGPROC dlgProc, LPARAM param,
693 DWORD flags )
695 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
698 /***********************************************************************
699 * CreateDialogIndirectParamA (USER32.@)
701 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
702 HWND owner, DLGPROC dlgProc, LPARAM param )
704 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
707 /***********************************************************************
708 * CreateDialogIndirectParamW (USER32.@)
710 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
711 HWND owner, DLGPROC dlgProc, LPARAM param )
713 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
717 /***********************************************************************
718 * DIALOG_DoDialogBox
720 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
722 DIALOGINFO * dlgInfo;
723 MSG msg;
724 INT retval;
725 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
726 BOOL bFirstEmpty;
728 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
730 bFirstEmpty = TRUE;
731 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
733 for (;;)
735 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
737 if (bFirstEmpty)
739 /* ShowWindow the first time the queue goes empty */
740 ShowWindow( hwnd, SW_SHOWNORMAL );
741 bFirstEmpty = FALSE;
743 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
745 /* No message present -> send ENTERIDLE and wait */
746 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
748 if (!GetMessageW( &msg, 0, 0, 0 )) break;
751 if (!IsWindow( hwnd )) return -1;
752 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
754 TranslateMessage( &msg );
755 DispatchMessageW( &msg );
757 if (dlgInfo->flags & DF_END) break;
760 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
761 retval = dlgInfo->idResult;
762 DestroyWindow( hwnd );
763 return retval;
767 /***********************************************************************
768 * DialogBoxParamA (USER32.@)
770 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
771 HWND owner, DLGPROC dlgProc, LPARAM param )
773 HWND hwnd;
774 HRSRC hrsrc;
775 LPCDLGTEMPLATEA ptr;
777 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
778 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
779 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
780 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
781 return -1;
785 /***********************************************************************
786 * DialogBoxParamW (USER32.@)
788 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
789 HWND owner, DLGPROC dlgProc, LPARAM param )
791 HWND hwnd;
792 HRSRC hrsrc;
793 LPCDLGTEMPLATEW ptr;
795 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
796 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
797 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
798 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
799 return -1;
803 /***********************************************************************
804 * DialogBoxIndirectParamAorW (USER32.@)
806 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
807 HWND owner, DLGPROC dlgProc,
808 LPARAM param, DWORD flags )
810 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
811 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
812 return -1;
815 /***********************************************************************
816 * DialogBoxIndirectParamA (USER32.@)
818 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
819 HWND owner, DLGPROC dlgProc, LPARAM param )
821 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
825 /***********************************************************************
826 * DialogBoxIndirectParamW (USER32.@)
828 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
829 HWND owner, DLGPROC dlgProc, LPARAM param )
831 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
834 /***********************************************************************
835 * EndDialog (USER32.@)
837 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
839 BOOL wasEnabled = TRUE;
840 DIALOGINFO * dlgInfo;
841 HWND owner;
843 TRACE("%p %d\n", hwnd, retval );
845 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
847 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
848 return FALSE;
850 dlgInfo->idResult = retval;
851 dlgInfo->flags |= DF_END;
852 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
854 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
855 DIALOG_EnableOwner( owner );
857 /* Windows sets the focus to the dialog itself in EndDialog */
859 if (IsChild(hwnd, GetFocus()))
860 SetFocus( hwnd );
862 /* Don't have to send a ShowWindow(SW_HIDE), just do
863 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
865 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
866 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
868 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
870 /* unblock dialog loop */
871 PostMessageA(hwnd, WM_NULL, 0, 0);
872 return TRUE;
876 /***********************************************************************
877 * DIALOG_IsAccelerator
879 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
881 HWND hwndControl = hwnd;
882 HWND hwndNext;
883 INT dlgCode;
884 WCHAR buffer[128];
888 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
889 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
891 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
892 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
893 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
895 /* find the accelerator key */
896 LPWSTR p = buffer - 2;
900 p = strchrW( p + 2, '&' );
902 while (p != NULL && p[1] == '&');
904 /* and check if it's the one we're looking for */
905 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
907 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
909 /* set focus to the control */
910 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
911 /* and bump it on to next */
912 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
914 else if (dlgCode & DLGC_BUTTON)
916 /* send BM_CLICK message to the control */
917 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
919 return TRUE;
922 hwndNext = GetWindow( hwndControl, GW_CHILD );
924 else hwndNext = 0;
926 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
928 while (!hwndNext && hwndControl)
930 hwndControl = GetParent( hwndControl );
931 if (hwndControl == hwndDlg)
933 if(hwnd==hwndDlg) /* prevent endless loop */
935 hwndNext=hwnd;
936 break;
938 hwndNext = GetWindow( hwndDlg, GW_CHILD );
940 else
941 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
943 hwndControl = hwndNext;
945 while (hwndControl && (hwndControl != hwnd));
947 return FALSE;
950 /***********************************************************************
951 * DIALOG_FindMsgDestination
953 * The messages that IsDialogMessage sends may not go to the dialog
954 * calling IsDialogMessage if that dialog is a child, and it has the
955 * DS_CONTROL style set.
956 * We propagate up until we hit one that does not have DS_CONTROL, or
957 * whose parent is not a dialog.
959 * This is undocumented behaviour.
961 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
963 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
965 WND *pParent;
966 HWND hParent = GetParent(hwndDlg);
967 if (!hParent) break;
969 pParent = WIN_GetPtr(hParent);
970 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
972 if (!(pParent->flags & WIN_ISDIALOG))
974 WIN_ReleasePtr(pParent);
975 break;
977 WIN_ReleasePtr(pParent);
979 hwndDlg = hParent;
982 return hwndDlg;
985 /***********************************************************************
986 * DIALOG_FixOneChildOnChangeFocus
988 * Callback helper for DIALOG_FixChildrenOnChangeFocus
991 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
992 LPARAM lParam)
994 /* If a default pushbutton then no longer default */
995 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
996 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
997 return TRUE;
1000 /***********************************************************************
1001 * DIALOG_FixChildrenOnChangeFocus
1003 * Following the change of focus that occurs for example after handling
1004 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1005 * children may be required.
1007 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1009 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1010 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1011 /* Windows does ask for this. I don't know why yet */
1013 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1015 /* If the button that is getting the focus WAS flagged as the default
1016 * pushbutton then ask the dialog what it thinks the default is and
1017 * set that in the default style.
1019 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1021 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1022 if (HIWORD(def_id) == DC_HASDEFID)
1024 HWND hwndDef;
1025 def_id = LOWORD(def_id);
1026 hwndDef = GetDlgItem (hwndDlg, def_id);
1027 if (hwndDef)
1029 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1030 /* I know that if it is a button then it should already be a
1031 * UNDEFPUSHBUTTON, since we have just told the buttons to
1032 * change style. But maybe they ignored our request
1034 if ((dlgcode_def & DLGC_BUTTON) &&
1035 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1037 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1042 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1044 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1045 /* I wonder why it doesn't send a DM_SETDEFID */
1049 /***********************************************************************
1050 * IsDialogMessageW (USER32.@)
1052 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1054 INT dlgCode = 0;
1056 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1058 hwndDlg = WIN_GetFullHandle( hwndDlg );
1059 if (hwndDlg == GetDesktopWindow()) return FALSE;
1060 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1062 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1064 switch(msg->message)
1066 case WM_KEYDOWN:
1067 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1068 if (dlgCode & DLGC_WANTMESSAGE) break;
1070 switch(msg->wParam)
1072 case VK_TAB:
1073 if (!(dlgCode & DLGC_WANTTAB))
1075 BOOL fIsDialog = TRUE;
1076 WND *pWnd = WIN_GetPtr( hwndDlg );
1078 if (pWnd && pWnd != WND_OTHER_PROCESS)
1080 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1081 WIN_ReleasePtr(pWnd);
1084 /* I am not sure under which circumstances the TAB is handled
1085 * each way. All I do know is that it does not always simply
1086 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1087 * do so but I presume someone has)
1089 if (fIsDialog)
1090 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1091 else
1093 /* It would appear that GetNextDlgTabItem can handle being
1094 * passed hwndDlg rather than NULL but that is undocumented
1095 * so let's do it properly
1097 HWND hwndFocus = GetFocus();
1098 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1099 hwndFocus == hwndDlg ? NULL : hwndFocus,
1100 GetKeyState (VK_SHIFT) & 0x8000);
1101 if (hwndNext)
1103 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1104 msg->wParam, (LPARAM)msg);
1105 if (dlgCode & DLGC_HASSETSEL)
1107 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1108 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1109 if (buffer)
1111 INT length;
1112 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1113 length = strlenW (buffer);
1114 HeapFree (GetProcessHeap(), 0, buffer);
1115 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1118 SetFocus (hwndNext);
1119 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1121 else
1122 return FALSE;
1124 return TRUE;
1126 break;
1128 case VK_RIGHT:
1129 case VK_DOWN:
1130 case VK_LEFT:
1131 case VK_UP:
1132 if (!(dlgCode & DLGC_WANTARROWS))
1134 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1135 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1136 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1137 return TRUE;
1139 break;
1141 case VK_CANCEL:
1142 case VK_ESCAPE:
1143 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1144 return TRUE;
1146 case VK_EXECUTE:
1147 case VK_RETURN:
1149 DWORD dw;
1150 if ((GetFocus() == msg->hwnd) &&
1151 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1153 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1155 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1157 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1158 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1160 else
1162 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1166 return TRUE;
1168 break;
1170 case WM_CHAR:
1171 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1172 * It does NOT get sent in the test program I have
1174 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1175 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1176 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1177 /* drop through */
1179 case WM_SYSCHAR:
1180 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1182 /* don't translate or dispatch */
1183 return TRUE;
1185 break;
1188 TranslateMessage( msg );
1189 DispatchMessageW( msg );
1190 return TRUE;
1194 /***********************************************************************
1195 * GetDlgCtrlID (USER32.@)
1197 INT WINAPI GetDlgCtrlID( HWND hwnd )
1199 return GetWindowLongPtrW( hwnd, GWLP_ID );
1203 /***********************************************************************
1204 * GetDlgItem (USER32.@)
1206 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1208 int i;
1209 HWND *list = WIN_ListChildren( hwndDlg );
1210 HWND ret = 0;
1212 if (!list) return 0;
1214 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1215 ret = list[i];
1216 HeapFree( GetProcessHeap(), 0, list );
1217 return ret;
1221 /*******************************************************************
1222 * SendDlgItemMessageA (USER32.@)
1224 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1225 WPARAM wParam, LPARAM lParam )
1227 HWND hwndCtrl = GetDlgItem( hwnd, id );
1228 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1229 else return 0;
1233 /*******************************************************************
1234 * SendDlgItemMessageW (USER32.@)
1236 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1237 WPARAM wParam, LPARAM lParam )
1239 HWND hwndCtrl = GetDlgItem( hwnd, id );
1240 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1241 else return 0;
1245 /*******************************************************************
1246 * SetDlgItemTextA (USER32.@)
1248 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1250 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1254 /*******************************************************************
1255 * SetDlgItemTextW (USER32.@)
1257 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1259 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1263 /***********************************************************************
1264 * GetDlgItemTextA (USER32.@)
1266 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1268 if (str && (len > 0)) str[0] = '\0';
1269 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1270 len, (LPARAM)str );
1274 /***********************************************************************
1275 * GetDlgItemTextW (USER32.@)
1277 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1279 if (str && (len > 0)) str[0] = '\0';
1280 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1281 len, (LPARAM)str );
1285 /*******************************************************************
1286 * SetDlgItemInt (USER32.@)
1288 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1289 BOOL fSigned )
1291 char str[20];
1293 if (fSigned) sprintf( str, "%d", (INT)value );
1294 else sprintf( str, "%u", value );
1295 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1296 return TRUE;
1300 /***********************************************************************
1301 * GetDlgItemInt (USER32.@)
1303 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1304 BOOL fSigned )
1306 char str[30];
1307 char * endptr;
1308 long result = 0;
1310 if (translated) *translated = FALSE;
1311 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1312 return 0;
1313 if (fSigned)
1315 result = strtol( str, &endptr, 10 );
1316 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1317 return 0;
1318 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1319 return 0;
1321 else
1323 result = strtoul( str, &endptr, 10 );
1324 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1325 return 0;
1326 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1328 if (translated) *translated = TRUE;
1329 return (UINT)result;
1333 /***********************************************************************
1334 * CheckDlgButton (USER32.@)
1336 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1338 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1339 return TRUE;
1343 /***********************************************************************
1344 * IsDlgButtonChecked (USER32.@)
1346 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1348 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1352 /***********************************************************************
1353 * CheckRB
1355 * Callback function used to check/uncheck radio buttons that fall
1356 * within a specific range of IDs.
1358 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1360 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1361 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1363 if ((lChildID >= lpRadioGroup->firstID) &&
1364 (lChildID <= lpRadioGroup->lastID))
1366 if (lChildID == lpRadioGroup->checkID)
1368 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1370 else
1372 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1376 return TRUE;
1380 /***********************************************************************
1381 * CheckRadioButton (USER32.@)
1383 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1384 int lastID, int checkID )
1386 RADIOGROUP radioGroup;
1388 radioGroup.firstID = firstID;
1389 radioGroup.lastID = lastID;
1390 radioGroup.checkID = checkID;
1392 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1393 (LPARAM)&radioGroup);
1397 /***********************************************************************
1398 * GetDialogBaseUnits (USER.243)
1399 * GetDialogBaseUnits (USER32.@)
1401 DWORD WINAPI GetDialogBaseUnits(void)
1403 static DWORD units;
1405 if (!units)
1407 HDC hdc;
1408 SIZE size;
1410 if ((hdc = GetDC(0)))
1412 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1413 if (size.cx) units = MAKELONG( size.cx, size.cy );
1414 ReleaseDC( 0, hdc );
1416 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1418 return units;
1422 /***********************************************************************
1423 * MapDialogRect (USER32.@)
1425 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1427 DIALOGINFO * dlgInfo;
1428 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1429 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1430 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1431 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1432 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1433 return TRUE;
1437 /***********************************************************************
1438 * GetNextDlgGroupItem (USER32.@)
1440 * Corrections to MSDN documentation
1442 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1443 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1444 * 2. Prev of NULL or hwndDlg fails
1446 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1448 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1449 BOOL fLooped=FALSE;
1450 BOOL fSkipping=FALSE;
1452 hwndDlg = WIN_GetFullHandle( hwndDlg );
1453 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1455 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1456 if (!hwndCtrl && fPrevious) return 0;
1458 if (hwndCtrl)
1460 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1462 else
1464 /* No ctrl specified -> start from the beginning */
1465 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1466 /* MSDN is wrong. fPrevious does not result in the last child */
1468 /* Maybe that first one is valid. If so then we don't want to skip it*/
1469 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1471 return hwndCtrl;
1475 /* Always go forward around the group and list of controls; for the
1476 * previous control keep track; for the next break when you find one
1478 retvalue = hwndCtrl;
1479 hwnd = hwndCtrl;
1480 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1483 while (!hwndNext)
1485 /* Climb out until there is a next sibling of the ancestor or we
1486 * reach the top (in which case we loop back to the start)
1488 if (hwndDlg == GetParent (hwnd))
1490 /* Wrap around to the beginning of the list, within the same
1491 * group. (Once only)
1493 if (fLooped) goto end;
1494 fLooped = TRUE;
1495 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1497 else
1499 hwnd = GetParent (hwnd);
1500 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1503 hwnd = hwndNext;
1505 /* Wander down the leading edge of controlparents */
1506 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1507 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1508 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1509 hwnd = hwndNext;
1510 /* Question. If the control is a control parent but either has no
1511 * children or is not visible/enabled then if it has a WS_GROUP does
1512 * it count? For that matter does it count anyway?
1513 * I believe it doesn't count.
1516 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1518 hwndLastGroup = hwnd;
1519 if (!fSkipping)
1521 /* Look for the beginning of the group */
1522 fSkipping = TRUE;
1526 if (hwnd == hwndCtrl)
1528 if (!fSkipping) break;
1529 if (hwndLastGroup == hwnd) break;
1530 hwnd = hwndLastGroup;
1531 fSkipping = FALSE;
1532 fLooped = FALSE;
1535 if (!fSkipping &&
1536 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1537 WS_VISIBLE)
1539 retvalue = hwnd;
1540 if (!fPrevious) break;
1543 end:
1544 return retvalue;
1548 /***********************************************************************
1549 * DIALOG_GetNextTabItem
1551 * Recursive helper for GetNextDlgTabItem
1553 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1555 LONG dsStyle;
1556 LONG exStyle;
1557 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1558 HWND retWnd = 0;
1559 HWND hChildFirst = 0;
1561 if(!hwndCtrl)
1563 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1564 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1566 else if (IsChild( hwndMain, hwndCtrl ))
1568 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1569 if(!hChildFirst)
1571 if(GetParent(hwndCtrl) != hwndMain)
1572 /* i.e. if we are not at the top level of the recursion */
1573 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1574 else
1575 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1579 while(hChildFirst)
1581 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1582 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1583 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1585 HWND retWnd;
1586 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1587 if (retWnd) return (retWnd);
1589 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1591 return (hChildFirst);
1593 hChildFirst = GetWindow(hChildFirst,wndSearch);
1595 if(hwndCtrl)
1597 HWND hParent = GetParent(hwndCtrl);
1598 while(hParent)
1600 if(hParent == hwndMain) break;
1601 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1602 if(retWnd) break;
1603 hParent = GetParent(hParent);
1605 if(!retWnd)
1606 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1608 return retWnd ? retWnd : hwndCtrl;
1611 /***********************************************************************
1612 * GetNextDlgTabItem (USER32.@)
1614 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1615 BOOL fPrevious )
1617 hwndDlg = WIN_GetFullHandle( hwndDlg );
1618 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1620 /* Undocumented but tested under Win2000 and WinME */
1621 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1623 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1624 * NB GetLastError returns whatever was set before the function was
1625 * called.
1627 if (!hwndCtrl && fPrevious) return 0;
1629 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1632 /**********************************************************************
1633 * DIALOG_DlgDirSelect
1635 * Helper function for DlgDirSelect*
1637 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1638 INT id, BOOL unicode, BOOL combo )
1640 WCHAR *buffer, *ptr;
1641 INT item, size;
1642 BOOL ret;
1643 HWND listbox = GetDlgItem( hwnd, id );
1645 TRACE("%p '%s' %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1646 if (!listbox) return FALSE;
1648 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1649 if (item == LB_ERR) return FALSE;
1651 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1652 if (size == LB_ERR) return FALSE;
1654 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1656 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1658 if ((ret = (buffer[0] == '['))) /* drive or directory */
1660 if (buffer[1] == '-') /* drive */
1662 buffer[3] = ':';
1663 buffer[4] = 0;
1664 ptr = buffer + 2;
1666 else
1668 buffer[strlenW(buffer)-1] = '\\';
1669 ptr = buffer + 1;
1672 else ptr = buffer;
1674 if (!unicode)
1676 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1677 ((LPSTR)str)[len-1] = 0;
1679 else lstrcpynW( str, ptr, len );
1680 HeapFree( GetProcessHeap(), 0, buffer );
1681 TRACE("Returning %d '%s'\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1682 return ret;
1686 /**********************************************************************
1687 * DIALOG_DlgDirListW
1689 * Helper function for DlgDirList*W
1691 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1692 INT idStatic, UINT attrib, BOOL combo )
1694 HWND hwnd;
1695 LPWSTR orig_spec = spec;
1696 WCHAR any[] = {'*','.','*',0};
1698 #define SENDMSG(msg,wparam,lparam) \
1699 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1700 : SendMessageW( hwnd, msg, wparam, lparam ))
1702 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1704 /* If the path exists and is a directory, chdir to it */
1705 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1706 else
1708 WCHAR *p, *p2;
1709 p = spec;
1710 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1711 if ((p2 = strrchrW( p, '/' ))) p = p2;
1712 if (p != spec)
1714 WCHAR sep = *p;
1715 *p = 0;
1716 if (!SetCurrentDirectoryW( spec ))
1718 *p = sep; /* Restore the original spec */
1719 return FALSE;
1721 spec = p + 1;
1725 TRACE( "mask=%s\n", debugstr_w(spec) );
1727 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1729 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1730 if (attrib & DDL_DIRECTORY)
1732 if (!(attrib & DDL_EXCLUSIVE))
1734 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1735 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1736 (LPARAM)spec ) == LB_ERR)
1737 return FALSE;
1739 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1740 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1741 (LPARAM)any ) == LB_ERR)
1742 return FALSE;
1744 else
1746 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1747 (LPARAM)spec ) == LB_ERR)
1748 return FALSE;
1752 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1754 WCHAR temp[MAX_PATH];
1755 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1756 CharLowerW( temp );
1757 /* Can't use PostMessage() here, because the string is on the stack */
1758 SetDlgItemTextW( hDlg, idStatic, temp );
1761 if (orig_spec && (spec != orig_spec))
1763 /* Update the original file spec */
1764 WCHAR *p = spec;
1765 while ((*orig_spec++ = *p++));
1768 return TRUE;
1769 #undef SENDMSG
1773 /**********************************************************************
1774 * DIALOG_DlgDirListA
1776 * Helper function for DlgDirList*A
1778 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1779 INT idStatic, UINT attrib, BOOL combo )
1781 if (spec)
1783 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1784 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1785 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1786 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1787 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1788 HeapFree( GetProcessHeap(), 0, specW );
1789 return ret;
1791 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1795 /**********************************************************************
1796 * DlgDirSelectExA (USER32.@)
1798 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1800 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1804 /**********************************************************************
1805 * DlgDirSelectExW (USER32.@)
1807 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1809 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1813 /**********************************************************************
1814 * DlgDirSelectComboBoxExA (USER32.@)
1816 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1817 INT id )
1819 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1823 /**********************************************************************
1824 * DlgDirSelectComboBoxExW (USER32.@)
1826 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1827 INT id)
1829 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1833 /**********************************************************************
1834 * DlgDirListA (USER32.@)
1836 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1837 INT idStatic, UINT attrib )
1839 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1843 /**********************************************************************
1844 * DlgDirListW (USER32.@)
1846 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1847 INT idStatic, UINT attrib )
1849 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1853 /**********************************************************************
1854 * DlgDirListComboBoxA (USER32.@)
1856 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1857 INT idStatic, UINT attrib )
1859 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1863 /**********************************************************************
1864 * DlgDirListComboBoxW (USER32.@)
1866 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1867 INT idStatic, UINT attrib )
1869 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );