Update WinXP version to SP2.
[wine/multimedia.git] / windows / dialog.c
blob08b7e89090f34ba62dfb3b859b124953603929a7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "winpos.h"
42 #include "user.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
48 /* Dialog control information */
49 typedef struct
51 DWORD style;
52 DWORD exStyle;
53 DWORD helpId;
54 INT16 x;
55 INT16 y;
56 INT16 cx;
57 INT16 cy;
58 UINT id;
59 LPCWSTR className;
60 LPCWSTR windowName;
61 LPCVOID data;
62 } DLG_CONTROL_INFO;
64 /* Dialog template */
65 typedef struct
67 DWORD style;
68 DWORD exStyle;
69 DWORD helpId;
70 UINT16 nbItems;
71 INT16 x;
72 INT16 y;
73 INT16 cx;
74 INT16 cy;
75 LPCWSTR menuName;
76 LPCWSTR className;
77 LPCWSTR caption;
78 INT16 pointSize;
79 WORD weight;
80 BOOL italic;
81 LPCWSTR faceName;
82 BOOL dialogEx;
83 } DLG_TEMPLATE;
85 /* Radio button group */
86 typedef struct
88 UINT firstID;
89 UINT lastID;
90 UINT checkID;
91 } RADIOGROUP;
94 /*********************************************************************
95 * dialog class descriptor
97 const struct builtin_class_descr DIALOG_builtin_class =
99 DIALOG_CLASS_ATOMA, /* name */
100 CS_SAVEBITS | CS_DBLCLKS, /* style */
101 DefDlgProcA, /* procA */
102 DefDlgProcW, /* procW */
103 DLGWINDOWEXTRA, /* extra */
104 IDC_ARROW, /* cursor */
105 0 /* brush */
109 /***********************************************************************
110 * DIALOG_EnableOwner
112 * Helper function for modal dialogs to enable again the
113 * owner of the dialog box.
115 void DIALOG_EnableOwner( HWND hOwner )
117 /* Owner must be a top-level window */
118 if (hOwner)
119 hOwner = GetAncestor( hOwner, GA_ROOT );
120 if (!hOwner) return;
121 EnableWindow( hOwner, TRUE );
125 /***********************************************************************
126 * DIALOG_DisableOwner
128 * Helper function for modal dialogs to disable the
129 * owner of the dialog box. Returns TRUE if owner was enabled.
131 BOOL DIALOG_DisableOwner( HWND hOwner )
133 /* Owner must be a top-level window */
134 if (hOwner)
135 hOwner = GetAncestor( hOwner, GA_ROOT );
136 if (!hOwner) return FALSE;
137 if (IsWindowEnabled( hOwner ))
139 EnableWindow( hOwner, FALSE );
140 return TRUE;
142 else
143 return FALSE;
146 /***********************************************************************
147 * DIALOG_GetCharSize
149 * Despite most of MSDN insisting that the horizontal base unit is
150 * tmAveCharWidth it isn't. Knowledge base article Q145994
151 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
152 * says that we should take the average of the 52 English upper and lower
153 * case characters.
155 BOOL DIALOG_GetCharSize( HDC hDC, HFONT hFont, SIZE * pSize )
157 HFONT hFontPrev = 0;
158 const char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
159 SIZE sz;
160 TEXTMETRICA tm;
162 if(!hDC) return FALSE;
164 if(hFont) hFontPrev = SelectObject(hDC, hFont);
165 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
166 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
168 pSize->cy = tm.tmHeight;
169 pSize->cx = (sz.cx / 26 + 1) / 2;
171 if (hFontPrev) SelectObject(hDC, hFontPrev);
173 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
174 return TRUE;
178 /***********************************************************************
179 * DIALOG_GetControl32
181 * Return the class and text of the control pointed to by ptr,
182 * fill the header structure and return a pointer to the next control.
184 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
185 BOOL dialogEx )
187 if (dialogEx)
189 info->helpId = GET_DWORD(p); p += 2;
190 info->exStyle = GET_DWORD(p); p += 2;
191 info->style = GET_DWORD(p); p += 2;
193 else
195 info->helpId = 0;
196 info->style = GET_DWORD(p); p += 2;
197 info->exStyle = GET_DWORD(p); p += 2;
199 info->x = GET_WORD(p); p++;
200 info->y = GET_WORD(p); p++;
201 info->cx = GET_WORD(p); p++;
202 info->cy = GET_WORD(p); p++;
204 if (dialogEx)
206 /* id is a DWORD for DIALOGEX */
207 info->id = GET_DWORD(p);
208 p += 2;
210 else
212 info->id = GET_WORD(p);
213 p++;
216 if (GET_WORD(p) == 0xffff)
218 static const WCHAR class_names[6][10] =
220 { 'B','u','t','t','o','n', }, /* 0x80 */
221 { 'E','d','i','t', }, /* 0x81 */
222 { 'S','t','a','t','i','c', }, /* 0x82 */
223 { 'L','i','s','t','B','o','x', }, /* 0x83 */
224 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
225 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
227 WORD id = GET_WORD(p+1);
228 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
229 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
230 if (id <= 5)
231 info->className = class_names[id];
232 else
234 info->className = NULL;
235 ERR("Unknown built-in class id %04x\n", id );
237 p += 2;
239 else
241 info->className = (LPCWSTR)p;
242 p += strlenW( info->className ) + 1;
245 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
247 info->windowName = (LPCWSTR)(UINT)GET_WORD(p + 1);
248 p += 2;
250 else
252 info->windowName = (LPCWSTR)p;
253 p += strlenW( info->windowName ) + 1;
256 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
257 debugstr_w( info->className ), debugstr_w( info->windowName ),
258 info->id, info->x, info->y, info->cx, info->cy,
259 info->style, info->exStyle, info->helpId );
261 if (GET_WORD(p))
263 if (TRACE_ON(dialog))
265 WORD i, count = GET_WORD(p) / sizeof(WORD);
266 TRACE(" BEGIN\n");
267 TRACE(" ");
268 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
269 TRACE("\n");
270 TRACE(" END\n" );
272 info->data = p + 1;
273 p += GET_WORD(p) / sizeof(WORD);
275 else info->data = NULL;
276 p++;
278 /* Next control is on dword boundary */
279 return (const WORD *)((((int)p) + 3) & ~3);
283 /***********************************************************************
284 * DIALOG_CreateControls32
286 * Create the control windows for a dialog.
288 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
289 HINSTANCE hInst, BOOL unicode )
291 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
292 DLG_CONTROL_INFO info;
293 HWND hwndCtrl, hwndDefButton = 0;
294 INT items = dlgTemplate->nbItems;
296 TRACE(" BEGIN\n" );
297 while (items--)
299 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
300 dlgTemplate->dialogEx );
301 /* Is this it? */
302 if (info.style & WS_BORDER)
304 info.style &= ~WS_BORDER;
305 info.exStyle |= WS_EX_CLIENTEDGE;
307 if (unicode)
309 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
310 info.className, info.windowName,
311 info.style | WS_CHILD,
312 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
313 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
314 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
315 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
316 hwnd, (HMENU)info.id,
317 hInst, (LPVOID)info.data );
319 else
321 LPSTR class = (LPSTR)info.className;
322 LPSTR caption = (LPSTR)info.windowName;
324 if (HIWORD(class))
326 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
327 class = HeapAlloc( GetProcessHeap(), 0, len );
328 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
330 if (HIWORD(caption))
332 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
333 caption = HeapAlloc( GetProcessHeap(), 0, len );
334 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
336 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
337 class, caption, info.style | WS_CHILD,
338 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
339 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
340 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
341 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
342 hwnd, (HMENU)info.id,
343 hInst, (LPVOID)info.data );
344 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
345 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
347 if (!hwndCtrl)
349 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
350 return FALSE;
353 /* Send initialisation messages to the control */
354 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
355 (WPARAM)dlgInfo->hUserFont, 0 );
356 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
358 /* If there's already a default push-button, set it back */
359 /* to normal and use this one instead. */
360 if (hwndDefButton)
361 SendMessageA( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
362 hwndDefButton = hwndCtrl;
363 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
366 TRACE(" END\n" );
367 return TRUE;
371 /***********************************************************************
372 * DIALOG_ParseTemplate32
374 * Fill a DLG_TEMPLATE structure from the dialog template, and return
375 * a pointer to the first control.
377 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
379 const WORD *p = (const WORD *)template;
381 result->style = GET_DWORD(p); p += 2;
382 if (result->style == 0xffff0001) /* DIALOGEX resource */
384 result->dialogEx = TRUE;
385 result->helpId = GET_DWORD(p); p += 2;
386 result->exStyle = GET_DWORD(p); p += 2;
387 result->style = GET_DWORD(p); p += 2;
389 else
391 result->dialogEx = FALSE;
392 result->helpId = 0;
393 result->exStyle = GET_DWORD(p); p += 2;
395 result->nbItems = GET_WORD(p); p++;
396 result->x = GET_WORD(p); p++;
397 result->y = GET_WORD(p); p++;
398 result->cx = GET_WORD(p); p++;
399 result->cy = GET_WORD(p); p++;
400 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
401 result->dialogEx ? "EX" : "", result->x, result->y,
402 result->cx, result->cy, result->helpId );
403 TRACE(" STYLE 0x%08lx\n", result->style );
404 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
406 /* Get the menu name */
408 switch(GET_WORD(p))
410 case 0x0000:
411 result->menuName = NULL;
412 p++;
413 break;
414 case 0xffff:
415 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
416 p += 2;
417 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
418 break;
419 default:
420 result->menuName = (LPCWSTR)p;
421 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
422 p += strlenW( result->menuName ) + 1;
423 break;
426 /* Get the class name */
428 switch(GET_WORD(p))
430 case 0x0000:
431 result->className = DIALOG_CLASS_ATOMW;
432 p++;
433 break;
434 case 0xffff:
435 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
436 p += 2;
437 TRACE(" CLASS %04x\n", LOWORD(result->className) );
438 break;
439 default:
440 result->className = (LPCWSTR)p;
441 TRACE(" CLASS %s\n", debugstr_w( result->className ));
442 p += strlenW( result->className ) + 1;
443 break;
446 /* Get the window caption */
448 result->caption = (LPCWSTR)p;
449 p += strlenW( result->caption ) + 1;
450 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
452 /* Get the font name */
454 if (result->style & DS_SETFONT)
456 result->pointSize = GET_WORD(p);
457 p++;
458 if (result->dialogEx)
460 result->weight = GET_WORD(p); p++;
461 result->italic = LOBYTE(GET_WORD(p)); p++;
463 else
465 result->weight = FW_DONTCARE;
466 result->italic = FALSE;
468 result->faceName = (LPCWSTR)p;
469 p += strlenW( result->faceName ) + 1;
470 TRACE(" FONT %d, %s, %d, %s\n",
471 result->pointSize, debugstr_w( result->faceName ),
472 result->weight, result->italic ? "TRUE" : "FALSE" );
475 /* First control is on dword boundary */
476 return (LPCSTR)((((int)p) + 3) & ~3);
480 /***********************************************************************
481 * DIALOG_CreateIndirect
482 * Creates a dialog box window
484 * modal = TRUE if we are called from a modal dialog box.
485 * (it's more compatible to do it here, as under Windows the owner
486 * is never disabled if the dialog fails because of an invalid template)
488 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
489 HWND owner, DLGPROC dlgProc, LPARAM param,
490 BOOL unicode, BOOL modal )
492 HWND hwnd;
493 RECT rect;
494 DLG_TEMPLATE template;
495 DIALOGINFO * dlgInfo = NULL;
496 DWORD units = GetDialogBaseUnits();
497 BOOL ownerEnabled = TRUE;
498 HMENU hMenu = 0;
499 HFONT hUserFont = 0;
500 UINT flags = 0;
501 UINT xBaseUnit = LOWORD(units);
502 UINT yBaseUnit = HIWORD(units);
504 /* Parse dialog template */
506 if (!dlgTemplate) return 0;
507 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
509 /* Load menu */
511 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
513 /* Create custom font if needed */
515 if (template.style & DS_SETFONT)
517 /* We convert the size to pixels and then make it -ve. This works
518 * for both +ve and -ve template.pointSize */
519 HDC dc;
520 int pixels;
521 dc = GetDC(0);
522 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
523 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
524 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
525 PROOF_QUALITY, FF_DONTCARE,
526 template.faceName );
527 if (hUserFont)
529 SIZE charSize;
530 if (DIALOG_GetCharSize( dc, hUserFont, &charSize ))
532 xBaseUnit = charSize.cx;
533 yBaseUnit = charSize.cy;
536 ReleaseDC(0, dc);
537 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
540 /* Create dialog main window */
542 rect.left = rect.top = 0;
543 rect.right = MulDiv(template.cx, xBaseUnit, 4);
544 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
545 if (template.style & WS_CHILD)
546 template.style &= ~(WS_CAPTION|WS_SYSMENU);
547 if (template.style & DS_MODALFRAME)
548 template.exStyle |= WS_EX_DLGMODALFRAME;
549 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
550 rect.right -= rect.left;
551 rect.bottom -= rect.top;
553 if (template.x == CW_USEDEFAULT16)
555 rect.left = rect.top = CW_USEDEFAULT;
557 else
559 if (template.style & DS_CENTER)
561 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
562 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
564 else
566 rect.left += MulDiv(template.x, xBaseUnit, 4);
567 rect.top += MulDiv(template.y, yBaseUnit, 8);
569 if ( !(template.style & WS_CHILD) )
571 INT dX, dY;
573 if( !(template.style & DS_ABSALIGN) )
574 ClientToScreen( owner, (POINT *)&rect );
576 /* try to fit it into the desktop */
578 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
579 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
580 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
581 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
582 if( rect.left < 0 ) rect.left = 0;
583 if( rect.top < 0 ) rect.top = 0;
587 if (modal)
589 ownerEnabled = DIALOG_DisableOwner( owner );
590 if (ownerEnabled) flags |= DF_OWNERENABLED;
593 if (unicode)
595 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
596 template.style & ~WS_VISIBLE,
597 rect.left, rect.top, rect.right, rect.bottom,
598 owner, hMenu, hInst, NULL );
600 else
602 LPSTR class = (LPSTR)template.className;
603 LPSTR caption = (LPSTR)template.caption;
605 if (HIWORD(class))
607 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
608 class = HeapAlloc( GetProcessHeap(), 0, len );
609 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
611 if (HIWORD(caption))
613 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
614 caption = HeapAlloc( GetProcessHeap(), 0, len );
615 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
617 hwnd = CreateWindowExA(template.exStyle, class, caption,
618 template.style & ~WS_VISIBLE,
619 rect.left, rect.top, rect.right, rect.bottom,
620 owner, hMenu, hInst, NULL );
621 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
622 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
625 if (!hwnd)
627 if (hUserFont) DeleteObject( hUserFont );
628 if (hMenu) DestroyMenu( hMenu );
629 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
630 return 0;
633 /* moved this from the top of the method to here as DIALOGINFO structure
634 will be valid only after WM_CREATE message has been handled in DefDlgProc
635 All the members of the structure get filled here using temp variables */
636 dlgInfo = DIALOG_get_info( hwnd, TRUE );
637 dlgInfo->hwndFocus = 0;
638 dlgInfo->hUserFont = hUserFont;
639 dlgInfo->hMenu = hMenu;
640 dlgInfo->xBaseUnit = xBaseUnit;
641 dlgInfo->yBaseUnit = yBaseUnit;
642 dlgInfo->idResult = 0;
643 dlgInfo->flags = flags;
644 dlgInfo->hDialogHeap = 0;
646 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
648 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
649 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
651 if (dlgInfo->hUserFont)
652 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
654 /* Create controls */
656 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
658 HWND hwndPreInitFocus;
660 /* Send initialisation messages and set focus */
662 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
664 hwndPreInitFocus = GetFocus();
665 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
667 /* check where the focus is again,
668 * some controls status might have changed in WM_INITDIALOG */
669 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
670 if( dlgInfo->hwndFocus )
671 SetFocus( dlgInfo->hwndFocus );
673 else
675 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
676 but the focus has not changed, set the focus where we expect it. */
677 if ((GetFocus() == hwndPreInitFocus) &&
678 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
680 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
681 if( dlgInfo->hwndFocus )
682 SetFocus( dlgInfo->hwndFocus );
686 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
688 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
690 return hwnd;
692 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
693 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
694 return 0;
698 /***********************************************************************
699 * CreateDialogParamA (USER32.@)
701 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
702 DLGPROC dlgProc, LPARAM param )
704 HRSRC hrsrc;
705 LPCDLGTEMPLATEA ptr;
707 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
708 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
709 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
713 /***********************************************************************
714 * CreateDialogParamW (USER32.@)
716 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
717 DLGPROC dlgProc, LPARAM param )
719 HRSRC hrsrc;
720 LPCDLGTEMPLATEA ptr;
722 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
723 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
724 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
728 /***********************************************************************
729 * CreateDialogIndirectParamAorW (USER32.@)
731 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
732 HWND owner, DLGPROC dlgProc, LPARAM param,
733 DWORD flags )
735 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
738 /***********************************************************************
739 * CreateDialogIndirectParamA (USER32.@)
741 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
742 HWND owner, DLGPROC dlgProc, LPARAM param )
744 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
747 /***********************************************************************
748 * CreateDialogIndirectParamW (USER32.@)
750 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
751 HWND owner, DLGPROC dlgProc, LPARAM param )
753 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
757 /***********************************************************************
758 * DIALOG_DoDialogBox
760 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
762 DIALOGINFO * dlgInfo;
763 MSG msg;
764 INT retval;
765 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
767 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
769 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
771 ShowWindow( hwnd, SW_SHOW );
772 for (;;)
774 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
776 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
778 /* No message present -> send ENTERIDLE and wait */
779 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
780 if (!GetMessageW( &msg, 0, 0, 0 )) break;
783 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
785 if (!IsWindow( hwnd )) return -1;
786 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
788 TranslateMessage( &msg );
789 DispatchMessageW( &msg );
791 if (dlgInfo->flags & DF_END) break;
794 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
795 retval = dlgInfo->idResult;
796 DestroyWindow( hwnd );
797 return retval;
801 /***********************************************************************
802 * DialogBoxParamA (USER32.@)
804 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
805 HWND owner, DLGPROC dlgProc, LPARAM param )
807 HWND hwnd;
808 HRSRC hrsrc;
809 LPCDLGTEMPLATEA ptr;
811 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
812 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
813 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
814 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
815 return -1;
819 /***********************************************************************
820 * DialogBoxParamW (USER32.@)
822 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
823 HWND owner, DLGPROC dlgProc, LPARAM param )
825 HWND hwnd;
826 HRSRC hrsrc;
827 LPCDLGTEMPLATEW ptr;
829 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
830 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
831 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
832 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
833 return -1;
837 /***********************************************************************
838 * DialogBoxIndirectParamAorW (USER32.@)
840 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
841 HWND owner, DLGPROC dlgProc,
842 LPARAM param, DWORD flags )
844 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
845 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
846 return -1;
849 /***********************************************************************
850 * DialogBoxIndirectParamA (USER32.@)
852 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
853 HWND owner, DLGPROC dlgProc, LPARAM param )
855 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
859 /***********************************************************************
860 * DialogBoxIndirectParamW (USER32.@)
862 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
863 HWND owner, DLGPROC dlgProc, LPARAM param )
865 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
868 /***********************************************************************
869 * EndDialog (USER32.@)
871 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
873 BOOL wasEnabled = TRUE;
874 DIALOGINFO * dlgInfo;
875 HWND owner;
877 TRACE("%p %d\n", hwnd, retval );
879 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
881 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
882 return FALSE;
884 dlgInfo->idResult = retval;
885 dlgInfo->flags |= DF_END;
886 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
888 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
889 DIALOG_EnableOwner( owner );
891 /* Windows sets the focus to the dialog itself in EndDialog */
893 if (IsChild(hwnd, GetFocus()))
894 SetFocus( hwnd );
896 /* Don't have to send a ShowWindow(SW_HIDE), just do
897 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
899 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
900 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
902 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
904 /* unblock dialog loop */
905 PostMessageA(hwnd, WM_NULL, 0, 0);
906 return TRUE;
910 /***********************************************************************
911 * DIALOG_IsAccelerator
913 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
915 HWND hwndControl = hwnd;
916 HWND hwndNext;
917 INT dlgCode;
918 WCHAR buffer[128];
922 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
923 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
925 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
926 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
927 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
929 /* find the accelerator key */
930 LPWSTR p = buffer - 2;
934 p = strchrW( p + 2, '&' );
936 while (p != NULL && p[1] == '&');
938 /* and check if it's the one we're looking for */
939 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
941 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
943 /* set focus to the control */
944 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
945 /* and bump it on to next */
946 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
948 else if (dlgCode & DLGC_BUTTON)
950 /* send BM_CLICK message to the control */
951 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
953 return TRUE;
956 hwndNext = GetWindow( hwndControl, GW_CHILD );
958 else hwndNext = 0;
960 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
962 while (!hwndNext && hwndControl)
964 hwndControl = GetParent( hwndControl );
965 if (hwndControl == hwndDlg)
967 if(hwnd==hwndDlg) /* prevent endless loop */
969 hwndNext=hwnd;
970 break;
972 hwndNext = GetWindow( hwndDlg, GW_CHILD );
974 else
975 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
977 hwndControl = hwndNext;
979 while (hwndControl && (hwndControl != hwnd));
981 return FALSE;
984 /***********************************************************************
985 * DIALOG_FindMsgDestination
987 * The messages that IsDialogMessage sends may not go to the dialog
988 * calling IsDialogMessage if that dialog is a child, and it has the
989 * DS_CONTROL style set.
990 * We propagate up until we hit one that does not have DS_CONTROL, or
991 * whose parent is not a dialog.
993 * This is undocumented behaviour.
995 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
997 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
999 WND *pParent;
1000 HWND hParent = GetParent(hwndDlg);
1001 if (!hParent) break;
1003 pParent = WIN_FindWndPtr(hParent);
1004 if (!pParent) break;
1006 if (!(pParent->flags & WIN_ISDIALOG))
1008 WIN_ReleaseWndPtr(pParent);
1009 break;
1011 WIN_ReleaseWndPtr(pParent);
1013 hwndDlg = hParent;
1016 return hwndDlg;
1019 /***********************************************************************
1020 * DIALOG_FixOneChildOnChangeFocus
1022 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1025 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1026 LPARAM lParam)
1028 /* If a default pushbutton then no longer default */
1029 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1030 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1031 return TRUE;
1034 /***********************************************************************
1035 * DIALOG_FixChildrenOnChangeFocus
1037 * Following the change of focus that occurs for example after handling
1038 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1039 * children may be required.
1041 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1043 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1044 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1045 /* Windows does ask for this. I don't know why yet */
1047 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1049 /* If the button that is getting the focus WAS flagged as the default
1050 * pushbutton then ask the dialog what it thinks the default is and
1051 * set that in the default style.
1053 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1055 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1056 if (HIWORD(def_id) == DC_HASDEFID)
1058 HWND hwndDef;
1059 def_id = LOWORD(def_id);
1060 hwndDef = GetDlgItem (hwndDlg, def_id);
1061 if (hwndDef)
1063 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1064 /* I know that if it is a button then it should already be a
1065 * UNDEFPUSHBUTTON, since we have just told the buttons to
1066 * change style. But maybe they ignored our request
1068 if ((dlgcode_def & DLGC_BUTTON) &&
1069 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1071 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1076 else
1078 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1079 /* I wonder why it doesn't send a DM_SETDEFID */
1083 /***********************************************************************
1084 * IsDialogMessageW (USER32.@)
1086 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1088 INT dlgCode = 0;
1090 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1092 hwndDlg = WIN_GetFullHandle( hwndDlg );
1093 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1095 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1097 switch(msg->message)
1099 case WM_KEYDOWN:
1100 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1101 if (dlgCode & DLGC_WANTMESSAGE) break;
1103 switch(msg->wParam)
1105 case VK_TAB:
1106 if (!(dlgCode & DLGC_WANTTAB))
1108 BOOL fIsDialog = TRUE;
1109 WND *pWnd = WIN_GetPtr( hwndDlg );
1111 if (pWnd && pWnd != WND_OTHER_PROCESS)
1113 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1114 WIN_ReleasePtr(pWnd);
1117 /* I am not sure under which circumstances the TAB is handled
1118 * each way. All I do know is that it does not always simply
1119 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1120 * do so but I presume someone has)
1122 if (fIsDialog)
1123 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1124 else
1126 /* It would appear that GetNextDlgTabItem can handle being
1127 * passed hwndDlg rather than NULL but that is undocumented
1128 * so let's do it properly
1130 HWND hwndFocus = GetFocus();
1131 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1132 hwndFocus == hwndDlg ? NULL : hwndFocus,
1133 GetKeyState (VK_SHIFT) & 0x8000);
1134 if (hwndNext)
1136 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1137 msg->wParam, (LPARAM)msg);
1138 if (dlgCode & DLGC_HASSETSEL)
1140 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1141 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1142 if (buffer)
1144 INT length;
1145 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1146 length = strlenW (buffer);
1147 HeapFree (GetProcessHeap(), 0, buffer);
1148 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1151 SetFocus (hwndNext);
1152 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1154 else
1155 return FALSE;
1157 return TRUE;
1159 break;
1161 case VK_RIGHT:
1162 case VK_DOWN:
1163 case VK_LEFT:
1164 case VK_UP:
1165 if (!(dlgCode & DLGC_WANTARROWS))
1167 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1168 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1169 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1170 return TRUE;
1172 break;
1174 case VK_CANCEL:
1175 case VK_ESCAPE:
1176 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1177 return TRUE;
1179 case VK_EXECUTE:
1180 case VK_RETURN:
1182 DWORD dw;
1183 if ((GetFocus() == msg->hwnd) &&
1184 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1186 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1188 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1190 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1191 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1193 else
1195 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1199 return TRUE;
1201 break;
1203 case WM_CHAR:
1204 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1205 * It does NOT get sent in the test program I have
1207 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1208 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1209 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1210 /* drop through */
1212 case WM_SYSCHAR:
1213 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1215 /* don't translate or dispatch */
1216 return TRUE;
1218 break;
1221 TranslateMessage( msg );
1222 DispatchMessageW( msg );
1223 return TRUE;
1227 /***********************************************************************
1228 * GetDlgCtrlID (USER32.@)
1230 INT WINAPI GetDlgCtrlID( HWND hwnd )
1232 return GetWindowLongW( hwnd, GWL_ID );
1236 /***********************************************************************
1237 * GetDlgItem (USER32.@)
1239 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1241 int i;
1242 HWND *list = WIN_ListChildren( hwndDlg );
1243 HWND ret = 0;
1245 if (!list) return 0;
1247 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1248 ret = list[i];
1249 HeapFree( GetProcessHeap(), 0, list );
1250 return ret;
1254 /*******************************************************************
1255 * SendDlgItemMessageA (USER32.@)
1257 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1258 WPARAM wParam, LPARAM lParam )
1260 HWND hwndCtrl = GetDlgItem( hwnd, id );
1261 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1262 else return 0;
1266 /*******************************************************************
1267 * SendDlgItemMessageW (USER32.@)
1269 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1270 WPARAM wParam, LPARAM lParam )
1272 HWND hwndCtrl = GetDlgItem( hwnd, id );
1273 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1274 else return 0;
1278 /*******************************************************************
1279 * SetDlgItemTextA (USER32.@)
1281 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1283 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1287 /*******************************************************************
1288 * SetDlgItemTextW (USER32.@)
1290 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1292 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1296 /***********************************************************************
1297 * GetDlgItemTextA (USER32.@)
1299 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1301 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1302 len, (LPARAM)str );
1306 /***********************************************************************
1307 * GetDlgItemTextW (USER32.@)
1309 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1311 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1312 len, (LPARAM)str );
1316 /*******************************************************************
1317 * SetDlgItemInt (USER32.@)
1319 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1320 BOOL fSigned )
1322 char str[20];
1324 if (fSigned) sprintf( str, "%d", (INT)value );
1325 else sprintf( str, "%u", value );
1326 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1327 return TRUE;
1331 /***********************************************************************
1332 * GetDlgItemInt (USER32.@)
1334 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1335 BOOL fSigned )
1337 char str[30];
1338 char * endptr;
1339 long result = 0;
1341 if (translated) *translated = FALSE;
1342 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1343 return 0;
1344 if (fSigned)
1346 result = strtol( str, &endptr, 10 );
1347 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1348 return 0;
1349 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1350 return 0;
1352 else
1354 result = strtoul( str, &endptr, 10 );
1355 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1356 return 0;
1357 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1359 if (translated) *translated = TRUE;
1360 return (UINT)result;
1364 /***********************************************************************
1365 * CheckDlgButton (USER32.@)
1367 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1369 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1370 return TRUE;
1374 /***********************************************************************
1375 * IsDlgButtonChecked (USER32.@)
1377 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1379 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1383 /***********************************************************************
1384 * CheckRB
1386 * Callback function used to check/uncheck radio buttons that fall
1387 * within a specific range of IDs.
1389 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1391 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1392 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1394 if ((lChildID >= lpRadioGroup->firstID) &&
1395 (lChildID <= lpRadioGroup->lastID))
1397 if (lChildID == lpRadioGroup->checkID)
1399 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1401 else
1403 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1407 return TRUE;
1411 /***********************************************************************
1412 * CheckRadioButton (USER32.@)
1414 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1415 UINT lastID, UINT checkID )
1417 RADIOGROUP radioGroup;
1419 radioGroup.firstID = firstID;
1420 radioGroup.lastID = lastID;
1421 radioGroup.checkID = checkID;
1423 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1424 (LPARAM)&radioGroup);
1428 /***********************************************************************
1429 * GetDialogBaseUnits (USER.243)
1430 * GetDialogBaseUnits (USER32.@)
1432 DWORD WINAPI GetDialogBaseUnits(void)
1434 static DWORD units;
1436 if (!units)
1438 HDC hdc;
1439 SIZE size;
1441 if ((hdc = GetDC(0)))
1443 if (DIALOG_GetCharSize( hdc, 0, &size )) units = MAKELONG( size.cx, size.cy );
1444 ReleaseDC( 0, hdc );
1446 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1448 return units;
1452 /***********************************************************************
1453 * MapDialogRect (USER32.@)
1455 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1457 DIALOGINFO * dlgInfo;
1458 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1459 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1460 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1461 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1462 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1463 return TRUE;
1467 /***********************************************************************
1468 * GetNextDlgGroupItem (USER32.@)
1470 * Corrections to MSDN documentation
1472 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1473 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1474 * 2. Prev of NULL or hwndDlg fails
1476 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1478 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1479 BOOL fLooped=FALSE;
1480 BOOL fSkipping=FALSE;
1482 hwndDlg = WIN_GetFullHandle( hwndDlg );
1483 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1485 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1486 if (!hwndCtrl && fPrevious) return 0;
1488 if (hwndCtrl)
1490 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1492 else
1494 /* No ctrl specified -> start from the beginning */
1495 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1496 /* MSDN is wrong. fPrevious does not result in the last child */
1498 /* Maybe that first one is valid. If so then we don't want to skip it*/
1499 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1501 return hwndCtrl;
1505 /* Always go forward around the group and list of controls; for the
1506 * previous control keep track; for the next break when you find one
1508 retvalue = hwndCtrl;
1509 hwnd = hwndCtrl;
1510 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1513 while (!hwndNext)
1515 /* Climb out until there is a next sibling of the ancestor or we
1516 * reach the top (in which case we loop back to the start)
1518 if (hwndDlg == GetParent (hwnd))
1520 /* Wrap around to the beginning of the list, within the same
1521 * group. (Once only)
1523 if (fLooped) goto end;
1524 fLooped = TRUE;
1525 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1527 else
1529 hwnd = GetParent (hwnd);
1530 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1533 hwnd = hwndNext;
1535 /* Wander down the leading edge of controlparents */
1536 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1537 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1538 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1539 hwnd = hwndNext;
1540 /* Question. If the control is a control parent but either has no
1541 * children or is not visible/enabled then if it has a WS_GROUP does
1542 * it count? For that matter does it count anyway?
1543 * I believe it doesn't count.
1546 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1548 hwndLastGroup = hwnd;
1549 if (!fSkipping)
1551 /* Look for the beginning of the group */
1552 fSkipping = TRUE;
1556 if (hwnd == hwndCtrl)
1558 if (!fSkipping) break;
1559 if (hwndLastGroup == hwnd) break;
1560 hwnd = hwndLastGroup;
1561 fSkipping = FALSE;
1562 fLooped = FALSE;
1565 if (!fSkipping &&
1566 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1567 WS_VISIBLE)
1569 retvalue = hwnd;
1570 if (!fPrevious) break;
1573 end:
1574 return retvalue;
1578 /***********************************************************************
1579 * DIALOG_GetNextTabItem
1581 * Recursive helper for GetNextDlgTabItem
1583 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1585 LONG dsStyle;
1586 LONG exStyle;
1587 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1588 HWND retWnd = 0;
1589 HWND hChildFirst = 0;
1591 if(!hwndCtrl)
1593 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1594 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1596 else if (IsChild( hwndMain, hwndCtrl ))
1598 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1599 if(!hChildFirst)
1601 if(GetParent(hwndCtrl) != hwndMain)
1602 /* i.e. if we are not at the top level of the recursion */
1603 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1604 else
1605 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1609 while(hChildFirst)
1611 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1612 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1613 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1615 HWND retWnd;
1616 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1617 if (retWnd) return (retWnd);
1619 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1621 return (hChildFirst);
1623 hChildFirst = GetWindow(hChildFirst,wndSearch);
1625 if(hwndCtrl)
1627 HWND hParent = GetParent(hwndCtrl);
1628 while(hParent)
1630 if(hParent == hwndMain) break;
1631 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1632 if(retWnd) break;
1633 hParent = GetParent(hParent);
1635 if(!retWnd)
1636 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1638 return retWnd ? retWnd : hwndCtrl;
1641 /***********************************************************************
1642 * GetNextDlgTabItem (USER32.@)
1644 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1645 BOOL fPrevious )
1647 hwndDlg = WIN_GetFullHandle( hwndDlg );
1648 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1650 /* Undocumented but tested under Win2000 and WinME */
1651 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1653 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1654 * NB GetLastError returns whatever was set before the function was
1655 * called.
1657 if (!hwndCtrl && fPrevious) return 0;
1659 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1662 /**********************************************************************
1663 * DIALOG_DlgDirSelect
1665 * Helper function for DlgDirSelect*
1667 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1668 INT id, BOOL unicode, BOOL combo )
1670 char *buffer, *ptr;
1671 INT item, size;
1672 BOOL ret;
1673 HWND listbox = GetDlgItem( hwnd, id );
1675 TRACE("%p '%s' %d\n", hwnd, str, id );
1676 if (!listbox) return FALSE;
1678 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1679 if (item == LB_ERR) return FALSE;
1680 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1681 if (size == LB_ERR) return FALSE;
1683 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1685 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1687 if ((ret = (buffer[0] == '['))) /* drive or directory */
1689 if (buffer[1] == '-') /* drive */
1691 buffer[3] = ':';
1692 buffer[4] = 0;
1693 ptr = buffer + 2;
1695 else
1697 buffer[strlen(buffer)-1] = '\\';
1698 ptr = buffer + 1;
1701 else ptr = buffer;
1703 if (unicode)
1705 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1706 ((LPWSTR)str)[len-1] = 0;
1708 else lstrcpynA( str, ptr, len );
1709 HeapFree( GetProcessHeap(), 0, buffer );
1710 TRACE("Returning %d '%s'\n", ret, str );
1711 return ret;
1715 /**********************************************************************
1716 * DIALOG_DlgDirListW
1718 * Helper function for DlgDirList*W
1720 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1721 INT idStatic, UINT attrib, BOOL combo )
1723 HWND hwnd;
1724 LPWSTR orig_spec = spec;
1725 WCHAR any[] = {'*','.','*',0};
1727 #define SENDMSG(msg,wparam,lparam) \
1728 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1729 : SendMessageW( hwnd, msg, wparam, lparam ))
1731 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1733 /* If the path exists and is a directory, chdir to it */
1734 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1735 else
1737 WCHAR *p, *p2;
1738 p = spec;
1739 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1740 if ((p2 = strrchrW( p, '/' ))) p = p2;
1741 if (p != spec)
1743 WCHAR sep = *p;
1744 *p = 0;
1745 if (!SetCurrentDirectoryW( spec ))
1747 *p = sep; /* Restore the original spec */
1748 return FALSE;
1750 spec = p + 1;
1754 TRACE( "mask=%s\n", debugstr_w(spec) );
1756 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1758 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1759 if (attrib & DDL_DIRECTORY)
1761 if (!(attrib & DDL_EXCLUSIVE))
1763 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1764 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1765 (LPARAM)spec ) == LB_ERR)
1766 return FALSE;
1768 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1769 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1770 (LPARAM)any ) == LB_ERR)
1771 return FALSE;
1773 else
1775 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1776 (LPARAM)spec ) == LB_ERR)
1777 return FALSE;
1781 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1783 WCHAR temp[MAX_PATH];
1784 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1785 CharLowerW( temp );
1786 /* Can't use PostMessage() here, because the string is on the stack */
1787 SetDlgItemTextW( hDlg, idStatic, temp );
1790 if (orig_spec && (spec != orig_spec))
1792 /* Update the original file spec */
1793 WCHAR *p = spec;
1794 while ((*orig_spec++ = *p++));
1797 return TRUE;
1798 #undef SENDMSG
1802 /**********************************************************************
1803 * DIALOG_DlgDirListA
1805 * Helper function for DlgDirList*A
1807 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1808 INT idStatic, UINT attrib, BOOL combo )
1810 if (spec)
1812 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1813 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1814 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1815 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1816 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1817 HeapFree( GetProcessHeap(), 0, specW );
1818 return ret;
1820 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1824 /**********************************************************************
1825 * DlgDirSelectExA (USER32.@)
1827 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1829 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1833 /**********************************************************************
1834 * DlgDirSelectExW (USER32.@)
1836 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1838 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1842 /**********************************************************************
1843 * DlgDirSelectComboBoxExA (USER32.@)
1845 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1846 INT id )
1848 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1852 /**********************************************************************
1853 * DlgDirSelectComboBoxExW (USER32.@)
1855 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1856 INT id)
1858 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
1862 /**********************************************************************
1863 * DlgDirListA (USER32.@)
1865 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1866 INT idStatic, UINT attrib )
1868 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1872 /**********************************************************************
1873 * DlgDirListW (USER32.@)
1875 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1876 INT idStatic, UINT attrib )
1878 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1882 /**********************************************************************
1883 * DlgDirListComboBoxA (USER32.@)
1885 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1886 INT idStatic, UINT attrib )
1888 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1892 /**********************************************************************
1893 * DlgDirListComboBoxW (USER32.@)
1895 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1896 INT idStatic, UINT attrib )
1898 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );