Modal dialogs should not be shown via ShowWindow until the message
[wine.git] / windows / dialog.c
blobde6cf4226cf1e871bad4f4b1e8b17e5cc16261ff
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 if (template.style & DS_CONTROL)
550 template.exStyle |= WS_EX_CONTROLPARENT;
551 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
552 rect.right -= rect.left;
553 rect.bottom -= rect.top;
555 if (template.x == CW_USEDEFAULT16)
557 rect.left = rect.top = CW_USEDEFAULT;
559 else
561 if (template.style & DS_CENTER)
563 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
564 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
566 else
568 rect.left += MulDiv(template.x, xBaseUnit, 4);
569 rect.top += MulDiv(template.y, yBaseUnit, 8);
571 if ( !(template.style & WS_CHILD) )
573 INT dX, dY;
575 if( !(template.style & DS_ABSALIGN) )
576 ClientToScreen( owner, (POINT *)&rect );
578 /* try to fit it into the desktop */
580 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
581 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
582 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
583 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
584 if( rect.left < 0 ) rect.left = 0;
585 if( rect.top < 0 ) rect.top = 0;
589 if (modal)
591 ownerEnabled = DIALOG_DisableOwner( owner );
592 if (ownerEnabled) flags |= DF_OWNERENABLED;
595 if (unicode)
597 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
598 template.style & ~WS_VISIBLE,
599 rect.left, rect.top, rect.right, rect.bottom,
600 owner, hMenu, hInst, NULL );
602 else
604 LPSTR class = (LPSTR)template.className;
605 LPSTR caption = (LPSTR)template.caption;
607 if (HIWORD(class))
609 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
610 class = HeapAlloc( GetProcessHeap(), 0, len );
611 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
613 if (HIWORD(caption))
615 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
616 caption = HeapAlloc( GetProcessHeap(), 0, len );
617 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
619 hwnd = CreateWindowExA(template.exStyle, class, caption,
620 template.style & ~WS_VISIBLE,
621 rect.left, rect.top, rect.right, rect.bottom,
622 owner, hMenu, hInst, NULL );
623 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
624 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
627 if (!hwnd)
629 if (hUserFont) DeleteObject( hUserFont );
630 if (hMenu) DestroyMenu( hMenu );
631 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
632 return 0;
635 /* moved this from the top of the method to here as DIALOGINFO structure
636 will be valid only after WM_CREATE message has been handled in DefDlgProc
637 All the members of the structure get filled here using temp variables */
638 dlgInfo = DIALOG_get_info( hwnd, TRUE );
639 dlgInfo->hwndFocus = 0;
640 dlgInfo->hUserFont = hUserFont;
641 dlgInfo->hMenu = hMenu;
642 dlgInfo->xBaseUnit = xBaseUnit;
643 dlgInfo->yBaseUnit = yBaseUnit;
644 dlgInfo->idResult = 0;
645 dlgInfo->flags = flags;
646 dlgInfo->hDialogHeap = 0;
648 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
650 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
651 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
653 if (dlgInfo->hUserFont)
654 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
656 /* Create controls */
658 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
660 HWND hwndPreInitFocus;
662 /* Send initialisation messages and set focus */
664 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
666 hwndPreInitFocus = GetFocus();
667 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
669 /* check where the focus is again,
670 * some controls status might have changed in WM_INITDIALOG */
671 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
672 if( dlgInfo->hwndFocus )
673 SetFocus( dlgInfo->hwndFocus );
675 else
677 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
678 but the focus has not changed, set the focus where we expect it. */
679 if ((GetFocus() == hwndPreInitFocus) &&
680 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
682 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
683 if( dlgInfo->hwndFocus )
684 SetFocus( dlgInfo->hwndFocus );
688 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
690 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
692 return hwnd;
694 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
695 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
696 return 0;
700 /***********************************************************************
701 * CreateDialogParamA (USER32.@)
703 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
704 DLGPROC dlgProc, LPARAM param )
706 HRSRC hrsrc;
707 LPCDLGTEMPLATEA ptr;
709 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
710 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
711 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
715 /***********************************************************************
716 * CreateDialogParamW (USER32.@)
718 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
719 DLGPROC dlgProc, LPARAM param )
721 HRSRC hrsrc;
722 LPCDLGTEMPLATEA ptr;
724 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
725 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
726 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
730 /***********************************************************************
731 * CreateDialogIndirectParamAorW (USER32.@)
733 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
734 HWND owner, DLGPROC dlgProc, LPARAM param,
735 DWORD flags )
737 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
740 /***********************************************************************
741 * CreateDialogIndirectParamA (USER32.@)
743 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
744 HWND owner, DLGPROC dlgProc, LPARAM param )
746 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
749 /***********************************************************************
750 * CreateDialogIndirectParamW (USER32.@)
752 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
753 HWND owner, DLGPROC dlgProc, LPARAM param )
755 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
759 /***********************************************************************
760 * DIALOG_DoDialogBox
762 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
764 DIALOGINFO * dlgInfo;
765 MSG msg;
766 INT retval;
767 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
768 BOOL bFirstEmpty;
770 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
772 bFirstEmpty = TRUE;
773 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
775 for (;;)
777 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
779 if (bFirstEmpty)
781 /* ShowWindow the first time the queue goes empty */
782 ShowWindow( hwnd, SW_SHOWNORMAL );
783 bFirstEmpty = FALSE;
785 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
787 /* No message present -> send ENTERIDLE and wait */
788 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
790 if (!GetMessageW( &msg, 0, 0, 0 )) break;
793 if (!IsWindow( hwnd )) return -1;
794 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
796 TranslateMessage( &msg );
797 DispatchMessageW( &msg );
799 if (dlgInfo->flags & DF_END) break;
802 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
803 retval = dlgInfo->idResult;
804 DestroyWindow( hwnd );
805 return retval;
809 /***********************************************************************
810 * DialogBoxParamA (USER32.@)
812 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
813 HWND owner, DLGPROC dlgProc, LPARAM param )
815 HWND hwnd;
816 HRSRC hrsrc;
817 LPCDLGTEMPLATEA ptr;
819 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
820 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
821 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
822 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
823 return -1;
827 /***********************************************************************
828 * DialogBoxParamW (USER32.@)
830 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
831 HWND owner, DLGPROC dlgProc, LPARAM param )
833 HWND hwnd;
834 HRSRC hrsrc;
835 LPCDLGTEMPLATEW ptr;
837 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
838 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
839 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
840 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
841 return -1;
845 /***********************************************************************
846 * DialogBoxIndirectParamAorW (USER32.@)
848 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
849 HWND owner, DLGPROC dlgProc,
850 LPARAM param, DWORD flags )
852 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
853 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
854 return -1;
857 /***********************************************************************
858 * DialogBoxIndirectParamA (USER32.@)
860 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
861 HWND owner, DLGPROC dlgProc, LPARAM param )
863 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
867 /***********************************************************************
868 * DialogBoxIndirectParamW (USER32.@)
870 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
871 HWND owner, DLGPROC dlgProc, LPARAM param )
873 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
876 /***********************************************************************
877 * EndDialog (USER32.@)
879 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
881 BOOL wasEnabled = TRUE;
882 DIALOGINFO * dlgInfo;
883 HWND owner;
885 TRACE("%p %d\n", hwnd, retval );
887 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
889 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
890 return FALSE;
892 dlgInfo->idResult = retval;
893 dlgInfo->flags |= DF_END;
894 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
896 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
897 DIALOG_EnableOwner( owner );
899 /* Windows sets the focus to the dialog itself in EndDialog */
901 if (IsChild(hwnd, GetFocus()))
902 SetFocus( hwnd );
904 /* Don't have to send a ShowWindow(SW_HIDE), just do
905 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
907 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
908 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
910 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
912 /* unblock dialog loop */
913 PostMessageA(hwnd, WM_NULL, 0, 0);
914 return TRUE;
918 /***********************************************************************
919 * DIALOG_IsAccelerator
921 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
923 HWND hwndControl = hwnd;
924 HWND hwndNext;
925 INT dlgCode;
926 WCHAR buffer[128];
930 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
931 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
933 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
934 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
935 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
937 /* find the accelerator key */
938 LPWSTR p = buffer - 2;
942 p = strchrW( p + 2, '&' );
944 while (p != NULL && p[1] == '&');
946 /* and check if it's the one we're looking for */
947 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
949 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
951 /* set focus to the control */
952 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
953 /* and bump it on to next */
954 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
956 else if (dlgCode & DLGC_BUTTON)
958 /* send BM_CLICK message to the control */
959 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
961 return TRUE;
964 hwndNext = GetWindow( hwndControl, GW_CHILD );
966 else hwndNext = 0;
968 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
970 while (!hwndNext && hwndControl)
972 hwndControl = GetParent( hwndControl );
973 if (hwndControl == hwndDlg)
975 if(hwnd==hwndDlg) /* prevent endless loop */
977 hwndNext=hwnd;
978 break;
980 hwndNext = GetWindow( hwndDlg, GW_CHILD );
982 else
983 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
985 hwndControl = hwndNext;
987 while (hwndControl && (hwndControl != hwnd));
989 return FALSE;
992 /***********************************************************************
993 * DIALOG_FindMsgDestination
995 * The messages that IsDialogMessage sends may not go to the dialog
996 * calling IsDialogMessage if that dialog is a child, and it has the
997 * DS_CONTROL style set.
998 * We propagate up until we hit one that does not have DS_CONTROL, or
999 * whose parent is not a dialog.
1001 * This is undocumented behaviour.
1003 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1005 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1007 WND *pParent;
1008 HWND hParent = GetParent(hwndDlg);
1009 if (!hParent) break;
1011 pParent = WIN_FindWndPtr(hParent);
1012 if (!pParent) break;
1014 if (!(pParent->flags & WIN_ISDIALOG))
1016 WIN_ReleaseWndPtr(pParent);
1017 break;
1019 WIN_ReleaseWndPtr(pParent);
1021 hwndDlg = hParent;
1024 return hwndDlg;
1027 /***********************************************************************
1028 * DIALOG_FixOneChildOnChangeFocus
1030 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1033 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1034 LPARAM lParam)
1036 /* If a default pushbutton then no longer default */
1037 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1038 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1039 return TRUE;
1042 /***********************************************************************
1043 * DIALOG_FixChildrenOnChangeFocus
1045 * Following the change of focus that occurs for example after handling
1046 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1047 * children may be required.
1049 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1051 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1052 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1053 /* Windows does ask for this. I don't know why yet */
1055 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1057 /* If the button that is getting the focus WAS flagged as the default
1058 * pushbutton then ask the dialog what it thinks the default is and
1059 * set that in the default style.
1061 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1063 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1064 if (HIWORD(def_id) == DC_HASDEFID)
1066 HWND hwndDef;
1067 def_id = LOWORD(def_id);
1068 hwndDef = GetDlgItem (hwndDlg, def_id);
1069 if (hwndDef)
1071 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1072 /* I know that if it is a button then it should already be a
1073 * UNDEFPUSHBUTTON, since we have just told the buttons to
1074 * change style. But maybe they ignored our request
1076 if ((dlgcode_def & DLGC_BUTTON) &&
1077 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1079 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1084 else
1086 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1087 /* I wonder why it doesn't send a DM_SETDEFID */
1091 /***********************************************************************
1092 * IsDialogMessageW (USER32.@)
1094 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1096 INT dlgCode = 0;
1098 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1100 hwndDlg = WIN_GetFullHandle( hwndDlg );
1101 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1103 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1105 switch(msg->message)
1107 case WM_KEYDOWN:
1108 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1109 if (dlgCode & DLGC_WANTMESSAGE) break;
1111 switch(msg->wParam)
1113 case VK_TAB:
1114 if (!(dlgCode & DLGC_WANTTAB))
1116 BOOL fIsDialog = TRUE;
1117 WND *pWnd = WIN_GetPtr( hwndDlg );
1119 if (pWnd && pWnd != WND_OTHER_PROCESS)
1121 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1122 WIN_ReleasePtr(pWnd);
1125 /* I am not sure under which circumstances the TAB is handled
1126 * each way. All I do know is that it does not always simply
1127 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1128 * do so but I presume someone has)
1130 if (fIsDialog)
1131 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1132 else
1134 /* It would appear that GetNextDlgTabItem can handle being
1135 * passed hwndDlg rather than NULL but that is undocumented
1136 * so let's do it properly
1138 HWND hwndFocus = GetFocus();
1139 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1140 hwndFocus == hwndDlg ? NULL : hwndFocus,
1141 GetKeyState (VK_SHIFT) & 0x8000);
1142 if (hwndNext)
1144 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1145 msg->wParam, (LPARAM)msg);
1146 if (dlgCode & DLGC_HASSETSEL)
1148 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1149 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1150 if (buffer)
1152 INT length;
1153 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1154 length = strlenW (buffer);
1155 HeapFree (GetProcessHeap(), 0, buffer);
1156 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1159 SetFocus (hwndNext);
1160 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1162 else
1163 return FALSE;
1165 return TRUE;
1167 break;
1169 case VK_RIGHT:
1170 case VK_DOWN:
1171 case VK_LEFT:
1172 case VK_UP:
1173 if (!(dlgCode & DLGC_WANTARROWS))
1175 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1176 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1177 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1178 return TRUE;
1180 break;
1182 case VK_CANCEL:
1183 case VK_ESCAPE:
1184 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1185 return TRUE;
1187 case VK_EXECUTE:
1188 case VK_RETURN:
1190 DWORD dw;
1191 if ((GetFocus() == msg->hwnd) &&
1192 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1194 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1196 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1198 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1199 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1201 else
1203 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1207 return TRUE;
1209 break;
1211 case WM_CHAR:
1212 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1213 * It does NOT get sent in the test program I have
1215 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1216 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1217 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1218 /* drop through */
1220 case WM_SYSCHAR:
1221 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1223 /* don't translate or dispatch */
1224 return TRUE;
1226 break;
1229 TranslateMessage( msg );
1230 DispatchMessageW( msg );
1231 return TRUE;
1235 /***********************************************************************
1236 * GetDlgCtrlID (USER32.@)
1238 INT WINAPI GetDlgCtrlID( HWND hwnd )
1240 return GetWindowLongW( hwnd, GWL_ID );
1244 /***********************************************************************
1245 * GetDlgItem (USER32.@)
1247 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1249 int i;
1250 HWND *list = WIN_ListChildren( hwndDlg );
1251 HWND ret = 0;
1253 if (!list) return 0;
1255 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1256 ret = list[i];
1257 HeapFree( GetProcessHeap(), 0, list );
1258 return ret;
1262 /*******************************************************************
1263 * SendDlgItemMessageA (USER32.@)
1265 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1266 WPARAM wParam, LPARAM lParam )
1268 HWND hwndCtrl = GetDlgItem( hwnd, id );
1269 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1270 else return 0;
1274 /*******************************************************************
1275 * SendDlgItemMessageW (USER32.@)
1277 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1278 WPARAM wParam, LPARAM lParam )
1280 HWND hwndCtrl = GetDlgItem( hwnd, id );
1281 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1282 else return 0;
1286 /*******************************************************************
1287 * SetDlgItemTextA (USER32.@)
1289 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1291 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1295 /*******************************************************************
1296 * SetDlgItemTextW (USER32.@)
1298 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1300 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1304 /***********************************************************************
1305 * GetDlgItemTextA (USER32.@)
1307 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1309 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1310 len, (LPARAM)str );
1314 /***********************************************************************
1315 * GetDlgItemTextW (USER32.@)
1317 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1319 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1320 len, (LPARAM)str );
1324 /*******************************************************************
1325 * SetDlgItemInt (USER32.@)
1327 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1328 BOOL fSigned )
1330 char str[20];
1332 if (fSigned) sprintf( str, "%d", (INT)value );
1333 else sprintf( str, "%u", value );
1334 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1335 return TRUE;
1339 /***********************************************************************
1340 * GetDlgItemInt (USER32.@)
1342 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1343 BOOL fSigned )
1345 char str[30];
1346 char * endptr;
1347 long result = 0;
1349 if (translated) *translated = FALSE;
1350 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1351 return 0;
1352 if (fSigned)
1354 result = strtol( str, &endptr, 10 );
1355 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1356 return 0;
1357 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1358 return 0;
1360 else
1362 result = strtoul( str, &endptr, 10 );
1363 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1364 return 0;
1365 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1367 if (translated) *translated = TRUE;
1368 return (UINT)result;
1372 /***********************************************************************
1373 * CheckDlgButton (USER32.@)
1375 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1377 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1378 return TRUE;
1382 /***********************************************************************
1383 * IsDlgButtonChecked (USER32.@)
1385 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1387 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1391 /***********************************************************************
1392 * CheckRB
1394 * Callback function used to check/uncheck radio buttons that fall
1395 * within a specific range of IDs.
1397 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1399 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1400 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1402 if ((lChildID >= lpRadioGroup->firstID) &&
1403 (lChildID <= lpRadioGroup->lastID))
1405 if (lChildID == lpRadioGroup->checkID)
1407 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1409 else
1411 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1415 return TRUE;
1419 /***********************************************************************
1420 * CheckRadioButton (USER32.@)
1422 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1423 UINT lastID, UINT checkID )
1425 RADIOGROUP radioGroup;
1427 radioGroup.firstID = firstID;
1428 radioGroup.lastID = lastID;
1429 radioGroup.checkID = checkID;
1431 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1432 (LPARAM)&radioGroup);
1436 /***********************************************************************
1437 * GetDialogBaseUnits (USER.243)
1438 * GetDialogBaseUnits (USER32.@)
1440 DWORD WINAPI GetDialogBaseUnits(void)
1442 static DWORD units;
1444 if (!units)
1446 HDC hdc;
1447 SIZE size;
1449 if ((hdc = GetDC(0)))
1451 if (DIALOG_GetCharSize( hdc, 0, &size )) units = MAKELONG( size.cx, size.cy );
1452 ReleaseDC( 0, hdc );
1454 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1456 return units;
1460 /***********************************************************************
1461 * MapDialogRect (USER32.@)
1463 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1465 DIALOGINFO * dlgInfo;
1466 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1467 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1468 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1469 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1470 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1471 return TRUE;
1475 /***********************************************************************
1476 * GetNextDlgGroupItem (USER32.@)
1478 * Corrections to MSDN documentation
1480 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1481 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1482 * 2. Prev of NULL or hwndDlg fails
1484 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1486 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1487 BOOL fLooped=FALSE;
1488 BOOL fSkipping=FALSE;
1490 hwndDlg = WIN_GetFullHandle( hwndDlg );
1491 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1493 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1494 if (!hwndCtrl && fPrevious) return 0;
1496 if (hwndCtrl)
1498 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1500 else
1502 /* No ctrl specified -> start from the beginning */
1503 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1504 /* MSDN is wrong. fPrevious does not result in the last child */
1506 /* Maybe that first one is valid. If so then we don't want to skip it*/
1507 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1509 return hwndCtrl;
1513 /* Always go forward around the group and list of controls; for the
1514 * previous control keep track; for the next break when you find one
1516 retvalue = hwndCtrl;
1517 hwnd = hwndCtrl;
1518 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1521 while (!hwndNext)
1523 /* Climb out until there is a next sibling of the ancestor or we
1524 * reach the top (in which case we loop back to the start)
1526 if (hwndDlg == GetParent (hwnd))
1528 /* Wrap around to the beginning of the list, within the same
1529 * group. (Once only)
1531 if (fLooped) goto end;
1532 fLooped = TRUE;
1533 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1535 else
1537 hwnd = GetParent (hwnd);
1538 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1541 hwnd = hwndNext;
1543 /* Wander down the leading edge of controlparents */
1544 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1545 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1546 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1547 hwnd = hwndNext;
1548 /* Question. If the control is a control parent but either has no
1549 * children or is not visible/enabled then if it has a WS_GROUP does
1550 * it count? For that matter does it count anyway?
1551 * I believe it doesn't count.
1554 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1556 hwndLastGroup = hwnd;
1557 if (!fSkipping)
1559 /* Look for the beginning of the group */
1560 fSkipping = TRUE;
1564 if (hwnd == hwndCtrl)
1566 if (!fSkipping) break;
1567 if (hwndLastGroup == hwnd) break;
1568 hwnd = hwndLastGroup;
1569 fSkipping = FALSE;
1570 fLooped = FALSE;
1573 if (!fSkipping &&
1574 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1575 WS_VISIBLE)
1577 retvalue = hwnd;
1578 if (!fPrevious) break;
1581 end:
1582 return retvalue;
1586 /***********************************************************************
1587 * DIALOG_GetNextTabItem
1589 * Recursive helper for GetNextDlgTabItem
1591 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1593 LONG dsStyle;
1594 LONG exStyle;
1595 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1596 HWND retWnd = 0;
1597 HWND hChildFirst = 0;
1599 if(!hwndCtrl)
1601 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1602 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1604 else if (IsChild( hwndMain, hwndCtrl ))
1606 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1607 if(!hChildFirst)
1609 if(GetParent(hwndCtrl) != hwndMain)
1610 /* i.e. if we are not at the top level of the recursion */
1611 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1612 else
1613 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1617 while(hChildFirst)
1619 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1620 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1621 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1623 HWND retWnd;
1624 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1625 if (retWnd) return (retWnd);
1627 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1629 return (hChildFirst);
1631 hChildFirst = GetWindow(hChildFirst,wndSearch);
1633 if(hwndCtrl)
1635 HWND hParent = GetParent(hwndCtrl);
1636 while(hParent)
1638 if(hParent == hwndMain) break;
1639 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1640 if(retWnd) break;
1641 hParent = GetParent(hParent);
1643 if(!retWnd)
1644 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1646 return retWnd ? retWnd : hwndCtrl;
1649 /***********************************************************************
1650 * GetNextDlgTabItem (USER32.@)
1652 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1653 BOOL fPrevious )
1655 hwndDlg = WIN_GetFullHandle( hwndDlg );
1656 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1658 /* Undocumented but tested under Win2000 and WinME */
1659 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1661 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1662 * NB GetLastError returns whatever was set before the function was
1663 * called.
1665 if (!hwndCtrl && fPrevious) return 0;
1667 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1670 /**********************************************************************
1671 * DIALOG_DlgDirSelect
1673 * Helper function for DlgDirSelect*
1675 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1676 INT id, BOOL unicode, BOOL combo )
1678 char *buffer, *ptr;
1679 INT item, size;
1680 BOOL ret;
1681 HWND listbox = GetDlgItem( hwnd, id );
1683 TRACE("%p '%s' %d\n", hwnd, str, id );
1684 if (!listbox) return FALSE;
1686 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1687 if (item == LB_ERR) return FALSE;
1688 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1689 if (size == LB_ERR) return FALSE;
1691 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1693 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1695 if ((ret = (buffer[0] == '['))) /* drive or directory */
1697 if (buffer[1] == '-') /* drive */
1699 buffer[3] = ':';
1700 buffer[4] = 0;
1701 ptr = buffer + 2;
1703 else
1705 buffer[strlen(buffer)-1] = '\\';
1706 ptr = buffer + 1;
1709 else ptr = buffer;
1711 if (unicode)
1713 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1714 ((LPWSTR)str)[len-1] = 0;
1716 else lstrcpynA( str, ptr, len );
1717 HeapFree( GetProcessHeap(), 0, buffer );
1718 TRACE("Returning %d '%s'\n", ret, str );
1719 return ret;
1723 /**********************************************************************
1724 * DIALOG_DlgDirListW
1726 * Helper function for DlgDirList*W
1728 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1729 INT idStatic, UINT attrib, BOOL combo )
1731 HWND hwnd;
1732 LPWSTR orig_spec = spec;
1733 WCHAR any[] = {'*','.','*',0};
1735 #define SENDMSG(msg,wparam,lparam) \
1736 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1737 : SendMessageW( hwnd, msg, wparam, lparam ))
1739 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1741 /* If the path exists and is a directory, chdir to it */
1742 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1743 else
1745 WCHAR *p, *p2;
1746 p = spec;
1747 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1748 if ((p2 = strrchrW( p, '/' ))) p = p2;
1749 if (p != spec)
1751 WCHAR sep = *p;
1752 *p = 0;
1753 if (!SetCurrentDirectoryW( spec ))
1755 *p = sep; /* Restore the original spec */
1756 return FALSE;
1758 spec = p + 1;
1762 TRACE( "mask=%s\n", debugstr_w(spec) );
1764 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1766 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1767 if (attrib & DDL_DIRECTORY)
1769 if (!(attrib & DDL_EXCLUSIVE))
1771 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1772 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1773 (LPARAM)spec ) == LB_ERR)
1774 return FALSE;
1776 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1777 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1778 (LPARAM)any ) == LB_ERR)
1779 return FALSE;
1781 else
1783 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1784 (LPARAM)spec ) == LB_ERR)
1785 return FALSE;
1789 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1791 WCHAR temp[MAX_PATH];
1792 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1793 CharLowerW( temp );
1794 /* Can't use PostMessage() here, because the string is on the stack */
1795 SetDlgItemTextW( hDlg, idStatic, temp );
1798 if (orig_spec && (spec != orig_spec))
1800 /* Update the original file spec */
1801 WCHAR *p = spec;
1802 while ((*orig_spec++ = *p++));
1805 return TRUE;
1806 #undef SENDMSG
1810 /**********************************************************************
1811 * DIALOG_DlgDirListA
1813 * Helper function for DlgDirList*A
1815 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1816 INT idStatic, UINT attrib, BOOL combo )
1818 if (spec)
1820 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1821 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1822 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1823 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1824 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1825 HeapFree( GetProcessHeap(), 0, specW );
1826 return ret;
1828 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1832 /**********************************************************************
1833 * DlgDirSelectExA (USER32.@)
1835 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1837 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1841 /**********************************************************************
1842 * DlgDirSelectExW (USER32.@)
1844 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1846 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1850 /**********************************************************************
1851 * DlgDirSelectComboBoxExA (USER32.@)
1853 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1854 INT id )
1856 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1860 /**********************************************************************
1861 * DlgDirSelectComboBoxExW (USER32.@)
1863 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1864 INT id)
1866 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
1870 /**********************************************************************
1871 * DlgDirListA (USER32.@)
1873 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1874 INT idStatic, UINT attrib )
1876 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1880 /**********************************************************************
1881 * DlgDirListW (USER32.@)
1883 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1884 INT idStatic, UINT attrib )
1886 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1890 /**********************************************************************
1891 * DlgDirListComboBoxA (USER32.@)
1893 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1894 INT idStatic, UINT attrib )
1896 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1900 /**********************************************************************
1901 * DlgDirListComboBoxW (USER32.@)
1903 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1904 INT idStatic, UINT attrib )
1906 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );