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
22 #include "wine/port.h"
37 #include "wine/winuser16.h"
38 #include "wine/winbase16.h"
40 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
50 /* Dialog control information */
87 /* Radio button group */
95 /* Dialog base units */
96 static WORD xBaseUnit = 0, yBaseUnit = 0;
99 /*********************************************************************
100 * dialog class descriptor
102 const struct builtin_class_descr DIALOG_builtin_class =
104 DIALOG_CLASS_ATOM, /* name */
105 CS_GLOBALCLASS | CS_SAVEBITS | CS_DBLCLKS, /* style */
106 DefDlgProcA, /* procA */
107 DefDlgProcW, /* procW */
108 DLGWINDOWEXTRA, /* extra */
109 IDC_ARROWA, /* cursor */
114 /***********************************************************************
117 * Helper function for modal dialogs to enable again the
118 * owner of the dialog box.
120 void DIALOG_EnableOwner( HWND hOwner )
122 /* Owner must be a top-level window */
124 hOwner = GetAncestor( hOwner, GA_ROOT );
126 EnableWindow( hOwner, TRUE );
130 /***********************************************************************
131 * DIALOG_DisableOwner
133 * Helper function for modal dialogs to disable the
134 * owner of the dialog box. Returns TRUE if owner was enabled.
136 BOOL DIALOG_DisableOwner( HWND hOwner )
138 /* Owner must be a top-level window */
140 hOwner = GetAncestor( hOwner, GA_ROOT );
141 if (!hOwner) return FALSE;
142 if (IsWindowEnabled( hOwner ))
144 EnableWindow( hOwner, FALSE );
151 /***********************************************************************
152 * DIALOG_GetCharSizeFromDC
154 * Despite most of MSDN insisting that the horizontal base unit is
155 * tmAveCharWidth it isn't. Knowledge base article Q145994
156 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
157 * says that we should take the average of the 52 English upper and lower
160 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
163 char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
167 pSize->cx = xBaseUnit;
168 pSize->cy = yBaseUnit;
170 if(!hDC) return FALSE;
172 if(hFont) hFontPrev = SelectFont(hDC, hFont);
173 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
174 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
176 pSize->cy = tm.tmHeight;
177 pSize->cx = (sz.cx / 26 + 1) / 2;
179 if (hFontPrev) SelectFont(hDC, hFontPrev);
181 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
185 /***********************************************************************
188 * A convenient variant of DIALOG_GetCharSizeFromDC.
190 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
193 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
198 /***********************************************************************
201 * Initialisation of the dialog manager.
203 BOOL DIALOG_Init(void)
208 /* Calculate the dialog base units */
210 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
212 ERR("Could not create Display DC\n");
216 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
219 ERR("Could not initialize base dialog units\n");
227 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
232 /***********************************************************************
233 * DIALOG_GetControl16
235 * Return the class and text of the control pointed to by ptr,
236 * fill the header structure and return a pointer to the next control.
238 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
240 static char buffer[10];
243 info->x = GET_WORD(p); p += sizeof(WORD);
244 info->y = GET_WORD(p); p += sizeof(WORD);
245 info->cx = GET_WORD(p); p += sizeof(WORD);
246 info->cy = GET_WORD(p); p += sizeof(WORD);
247 info->id = GET_WORD(p); p += sizeof(WORD);
248 info->style = GET_DWORD(p); p += sizeof(DWORD);
255 case 0x80: strcpy( buffer, "BUTTON" ); break;
256 case 0x81: strcpy( buffer, "EDIT" ); break;
257 case 0x82: strcpy( buffer, "STATIC" ); break;
258 case 0x83: strcpy( buffer, "LISTBOX" ); break;
259 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
260 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
261 default: buffer[0] = '\0'; break;
263 info->className = buffer;
272 int_id = ((BYTE)*p == 0xff);
275 /* Integer id, not documented (?). Only works for SS_ICON controls */
276 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
281 info->windowName = p;
285 if (*p) info->data = p + 1;
286 else info->data = NULL;
291 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %p\n",
292 info->className, LOWORD(info->windowName),
293 info->id, info->x, info->y, info->cx, info->cy,
294 info->style, info->data );
296 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %p\n",
297 info->className, info->windowName,
298 info->id, info->x, info->y, info->cx, info->cy,
299 info->style, info->data );
305 /***********************************************************************
306 * DIALOG_GetControl32
308 * Return the class and text of the control pointed to by ptr,
309 * fill the header structure and return a pointer to the next control.
311 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
316 info->helpId = GET_DWORD(p); p += 2;
317 info->exStyle = GET_DWORD(p); p += 2;
318 info->style = GET_DWORD(p); p += 2;
323 info->style = GET_DWORD(p); p += 2;
324 info->exStyle = GET_DWORD(p); p += 2;
326 info->x = GET_WORD(p); p++;
327 info->y = GET_WORD(p); p++;
328 info->cx = GET_WORD(p); p++;
329 info->cy = GET_WORD(p); p++;
333 /* id is a DWORD for DIALOGEX */
334 info->id = GET_DWORD(p);
339 info->id = GET_WORD(p);
343 if (GET_WORD(p) == 0xffff)
345 static const WCHAR class_names[6][10] =
347 { 'B','u','t','t','o','n', }, /* 0x80 */
348 { 'E','d','i','t', }, /* 0x81 */
349 { 'S','t','a','t','i','c', }, /* 0x82 */
350 { 'L','i','s','t','B','o','x', }, /* 0x83 */
351 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
352 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
354 WORD id = GET_WORD(p+1);
355 if ((id >= 0x80) && (id <= 0x85))
356 info->className = (LPCSTR)class_names[id - 0x80];
359 info->className = NULL;
360 ERR("Unknown built-in class id %04x\n", id );
366 info->className = (LPCSTR)p;
367 p += strlenW( (LPCWSTR)p ) + 1;
370 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
372 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
377 info->windowName = (LPCSTR)p;
378 p += strlenW( (LPCWSTR)p ) + 1;
381 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
382 debugstr_w( (LPCWSTR)info->className ),
383 debugstr_w( (LPCWSTR)info->windowName ),
384 info->id, info->x, info->y, info->cx, info->cy,
385 info->style, info->exStyle, info->helpId );
389 if (TRACE_ON(dialog))
391 WORD i, count = GET_WORD(p) / sizeof(WORD);
394 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
399 p += GET_WORD(p) / sizeof(WORD);
401 else info->data = NULL;
404 /* Next control is on dword boundary */
405 return (const WORD *)((((int)p) + 3) & ~3);
409 /***********************************************************************
410 * DIALOG_CreateControls16
412 * Create the control windows for a dialog.
414 static BOOL DIALOG_CreateControls16( HWND hwnd, LPCSTR template,
415 const DLG_TEMPLATE *dlgTemplate, HINSTANCE16 hInst )
417 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
418 DLG_CONTROL_INFO info;
419 HWND hwndCtrl, hwndDefButton = 0;
420 INT items = dlgTemplate->nbItems;
425 HINSTANCE16 instance = hInst;
428 template = DIALOG_GetControl16( template, &info );
429 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
430 !(GetWindowLongW( hwnd, GWL_STYLE ) & DS_LOCALEDIT))
432 if (!dlgInfo->hDialogHeap)
434 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
435 if (!dlgInfo->hDialogHeap)
437 ERR("Insufficient memory to create heap for edit control\n" );
440 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
442 instance = dlgInfo->hDialogHeap;
445 segptr = MapLS( info.data );
446 hwndCtrl = WIN_Handle32( CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
447 info.className, info.windowName,
448 info.style | WS_CHILD,
449 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
450 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
451 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
452 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
453 HWND_16(hwnd), (HMENU16)info.id,
454 instance, (LPVOID)segptr ));
457 if (!hwndCtrl) return FALSE;
459 /* Send initialisation messages to the control */
460 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
461 (WPARAM)dlgInfo->hUserFont, 0 );
462 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
464 /* If there's already a default push-button, set it back */
465 /* to normal and use this one instead. */
467 SendMessageA( hwndDefButton, BM_SETSTYLE,
468 BS_PUSHBUTTON,FALSE );
469 hwndDefButton = hwndCtrl;
470 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
478 /***********************************************************************
479 * DIALOG_CreateControls32
481 * Create the control windows for a dialog.
483 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template,
484 const DLG_TEMPLATE *dlgTemplate, HINSTANCE hInst )
486 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
487 DLG_CONTROL_INFO info;
488 HWND hwndCtrl, hwndDefButton = 0;
489 INT items = dlgTemplate->nbItems;
494 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
495 dlgTemplate->dialogEx );
497 if (info.style & WS_BORDER)
499 info.style &= ~WS_BORDER;
500 info.exStyle |= WS_EX_CLIENTEDGE;
502 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
503 (LPCWSTR)info.className,
504 (LPCWSTR)info.windowName,
505 info.style | WS_CHILD,
506 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
507 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
508 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
509 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
510 hwnd, (HMENU)info.id,
511 hInst, (LPVOID)info.data );
512 if (!hwndCtrl) return FALSE;
514 /* Send initialisation messages to the control */
515 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
516 (WPARAM)dlgInfo->hUserFont, 0 );
517 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
519 /* If there's already a default push-button, set it back */
520 /* to normal and use this one instead. */
522 SendMessageA( hwndDefButton, BM_SETSTYLE,
523 BS_PUSHBUTTON,FALSE );
524 hwndDefButton = hwndCtrl;
525 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
533 /***********************************************************************
534 * DIALOG_ParseTemplate16
536 * Fill a DLG_TEMPLATE structure from the dialog template, and return
537 * a pointer to the first control.
539 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
541 result->style = GET_DWORD(p); p += sizeof(DWORD);
543 result->nbItems = (unsigned char) *p++;
544 result->x = GET_WORD(p); p += sizeof(WORD);
545 result->y = GET_WORD(p); p += sizeof(WORD);
546 result->cx = GET_WORD(p); p += sizeof(WORD);
547 result->cy = GET_WORD(p); p += sizeof(WORD);
548 TRACE("DIALOG %d, %d, %d, %d\n",
549 result->x, result->y, result->cx, result->cy );
550 TRACE(" STYLE %08lx\n", result->style );
552 /* Get the menu name */
557 result->menuName = 0;
561 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
563 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
566 result->menuName = p;
567 TRACE(" MENU '%s'\n", p );
572 /* Get the class name */
576 result->className = p;
577 TRACE(" CLASS '%s'\n", result->className );
579 else result->className = DIALOG_CLASS_ATOM;
582 /* Get the window caption */
586 TRACE(" CAPTION '%s'\n", result->caption );
588 /* Get the font name */
590 if (result->style & DS_SETFONT)
592 result->pointSize = GET_WORD(p);
594 result->faceName = p;
596 TRACE(" FONT %d,'%s'\n",
597 result->pointSize, result->faceName );
603 /***********************************************************************
604 * DIALOG_ParseTemplate32
606 * Fill a DLG_TEMPLATE structure from the dialog template, and return
607 * a pointer to the first control.
609 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
611 const WORD *p = (const WORD *)template;
613 result->style = GET_DWORD(p); p += 2;
614 if (result->style == 0xffff0001) /* DIALOGEX resource */
616 result->dialogEx = TRUE;
617 result->helpId = GET_DWORD(p); p += 2;
618 result->exStyle = GET_DWORD(p); p += 2;
619 result->style = GET_DWORD(p); p += 2;
623 result->dialogEx = FALSE;
625 result->exStyle = GET_DWORD(p); p += 2;
627 result->nbItems = GET_WORD(p); p++;
628 result->x = GET_WORD(p); p++;
629 result->y = GET_WORD(p); p++;
630 result->cx = GET_WORD(p); p++;
631 result->cy = GET_WORD(p); p++;
632 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
633 result->dialogEx ? "EX" : "", result->x, result->y,
634 result->cx, result->cy, result->helpId );
635 TRACE(" STYLE 0x%08lx\n", result->style );
636 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
638 /* Get the menu name */
643 result->menuName = NULL;
647 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
649 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
652 result->menuName = (LPCSTR)p;
653 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
654 p += strlenW( (LPCWSTR)p ) + 1;
658 /* Get the class name */
663 result->className = DIALOG_CLASS_ATOM;
667 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
669 TRACE(" CLASS %04x\n", LOWORD(result->className) );
672 result->className = (LPCSTR)p;
673 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
674 p += strlenW( (LPCWSTR)p ) + 1;
678 /* Get the window caption */
680 result->caption = (LPCSTR)p;
681 p += strlenW( (LPCWSTR)p ) + 1;
682 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
684 /* Get the font name */
686 if (result->style & DS_SETFONT)
688 result->pointSize = GET_WORD(p);
690 if (result->dialogEx)
692 result->weight = GET_WORD(p); p++;
693 result->italic = LOBYTE(GET_WORD(p)); p++;
697 result->weight = FW_DONTCARE;
698 result->italic = FALSE;
700 result->faceName = (LPCSTR)p;
701 p += strlenW( (LPCWSTR)p ) + 1;
702 TRACE(" FONT %d, %s, %d, %s\n",
703 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
704 result->weight, result->italic ? "TRUE" : "FALSE" );
707 /* First control is on dword boundary */
708 return (LPCSTR)((((int)p) + 3) & ~3);
712 /***********************************************************************
713 * DIALOG_CreateIndirect
714 * Creates a dialog box window
716 * modal = TRUE if we are called from a modal dialog box.
717 * (it's more compatible to do it here, as under Windows the owner
718 * is never disabled if the dialog fails because of an invalid template)
720 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
721 HWND owner, DLGPROC dlgProc, LPARAM param,
722 WINDOWPROCTYPE procType, BOOL modal )
727 DLG_TEMPLATE template;
728 DIALOGINFO * dlgInfo;
729 BOOL ownerEnabled = TRUE;
730 BOOL win32Template = (procType != WIN_PROC_16);
733 /* Parse dialog template */
735 if (!dlgTemplate) return 0;
737 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
739 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
741 /* Initialise dialog extra data */
743 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
744 dlgInfo->hwndFocus = 0;
745 dlgInfo->hUserFont = 0;
747 dlgInfo->xBaseUnit = xBaseUnit;
748 dlgInfo->yBaseUnit = yBaseUnit;
749 dlgInfo->idResult = 0;
751 dlgInfo->hDialogHeap = 0;
755 if (template.menuName)
757 if (!win32Template) dlgInfo->hMenu = HMENU_32(LoadMenu16( HINSTANCE_16(hInst), template.menuName ));
758 else dlgInfo->hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
761 /* Create custom font if needed */
763 if (template.style & DS_SETFONT)
765 /* We convert the size to pixels and then make it -ve. This works
766 * for both +ve and -ve template.pointSize */
770 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
773 dlgInfo->hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
774 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
775 PROOF_QUALITY, FF_DONTCARE,
776 (LPCWSTR)template.faceName );
778 dlgInfo->hUserFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
779 FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
780 PROOF_QUALITY, FF_DONTCARE, template.faceName );
781 if (dlgInfo->hUserFont)
784 if (DIALOG_GetCharSize( dlgInfo->hUserFont, &charSize ))
786 dlgInfo->xBaseUnit = charSize.cx;
787 dlgInfo->yBaseUnit = charSize.cy;
790 TRACE("units = %d,%d\n", dlgInfo->xBaseUnit, dlgInfo->yBaseUnit );
793 /* Create dialog main window */
795 rect.left = rect.top = 0;
796 rect.right = MulDiv(template.cx, dlgInfo->xBaseUnit, 4);
797 rect.bottom = MulDiv(template.cy, dlgInfo->yBaseUnit, 8);
798 if (template.style & DS_MODALFRAME)
799 template.exStyle |= WS_EX_DLGMODALFRAME;
800 AdjustWindowRectEx( &rect, template.style, (dlgInfo->hMenu != 0), template.exStyle );
801 rect.right -= rect.left;
802 rect.bottom -= rect.top;
804 if ((INT16)template.x == CW_USEDEFAULT16)
806 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
810 if (template.style & DS_CENTER)
812 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
813 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
817 rect.left += MulDiv(template.x, dlgInfo->xBaseUnit, 4);
818 rect.top += MulDiv(template.y, dlgInfo->yBaseUnit, 8);
820 if ( !(template.style & WS_CHILD) )
824 if( !(template.style & DS_ABSALIGN) )
825 ClientToScreen( owner, (POINT *)&rect );
827 /* try to fit it into the desktop */
829 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
830 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
831 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
832 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
833 if( rect.left < 0 ) rect.left = 0;
834 if( rect.top < 0 ) rect.top = 0;
840 ownerEnabled = DIALOG_DisableOwner( owner );
841 if (ownerEnabled) dlgInfo->flags |= DF_OWNERENABLED;
845 hwnd = WIN_Handle32( CreateWindowEx16(template.exStyle, template.className,
846 template.caption, template.style & ~WS_VISIBLE,
847 rect.left, rect.top, rect.right, rect.bottom,
848 HWND_16(owner), HMENU_16(dlgInfo->hMenu),
849 HINSTANCE_16(hInst), NULL ));
851 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
852 (LPCWSTR)template.caption,
853 template.style & ~WS_VISIBLE,
854 rect.left, rect.top, rect.right, rect.bottom,
855 owner, dlgInfo->hMenu, hInst, NULL );
859 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
860 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
861 if (modal && (dlgInfo->flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
862 HeapFree( GetProcessHeap(), 0, dlgInfo );
865 wndPtr = WIN_GetPtr( hwnd );
866 wndPtr->flags |= WIN_ISDIALOG;
867 WIN_ReleasePtr( wndPtr );
869 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
870 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
873 case WIN_PROC_16: SetWindowLong16( HWND_16(hwnd), DWL_DLGPROC, (LONG)dlgProc ); break;
874 case WIN_PROC_32A: SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc ); break;
875 case WIN_PROC_32W: SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc ); break;
879 if (dlgInfo->hUserFont)
880 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
882 /* Create controls */
885 res = DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst );
887 res = DIALOG_CreateControls16( hwnd, dlgTemplate, &template, HINSTANCE_16(hInst) );
891 HWND hwndPreInitFocus;
893 /* Send initialisation messages and set focus */
895 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
897 hwndPreInitFocus = GetFocus();
898 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
900 /* check where the focus is again,
901 * some controls status might have changed in WM_INITDIALOG */
902 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
903 if( dlgInfo->hwndFocus )
904 SetFocus( dlgInfo->hwndFocus );
908 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
909 but the focus has not changed, set the focus where we expect it. */
910 if ((GetFocus() == hwndPreInitFocus) &&
911 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
913 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
914 if( dlgInfo->hwndFocus )
915 SetFocus( dlgInfo->hwndFocus );
919 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
921 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
925 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
926 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
931 /***********************************************************************
932 * CreateDialog (USER.89)
934 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
935 HWND16 owner, DLGPROC16 dlgProc )
937 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
941 /***********************************************************************
942 * CreateDialogParam (USER.241)
944 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
945 HWND16 owner, DLGPROC16 dlgProc,
953 TRACE("%04x,%s,%04x,%08lx,%ld\n",
954 hInst, debugstr_a(dlgTemplate), owner, (DWORD)dlgProc, param );
956 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
957 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
958 if (!(data = LockResource16( hmem ))) hwnd = 0;
959 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
961 FreeResource16( hmem );
965 /***********************************************************************
966 * CreateDialogParamA (USER32.@)
968 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
969 HWND owner, DLGPROC dlgProc,
972 HRSRC hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
973 if (!hrsrc) return 0;
974 return CreateDialogIndirectParamA( hInst,
975 (LPVOID)LoadResource(hInst, hrsrc),
976 owner, dlgProc, param );
980 /***********************************************************************
981 * CreateDialogParamW (USER32.@)
983 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
984 HWND owner, DLGPROC dlgProc,
987 HRSRC hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
988 if (!hrsrc) return 0;
989 return CreateDialogIndirectParamW( hInst,
990 (LPVOID)LoadResource(hInst, hrsrc),
991 owner, dlgProc, param );
995 /***********************************************************************
996 * CreateDialogIndirect (USER.219)
998 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
999 HWND16 owner, DLGPROC16 dlgProc )
1001 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
1005 /***********************************************************************
1006 * CreateDialogIndirectParam (USER.242)
1008 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
1009 LPCVOID dlgTemplate,
1010 HWND16 owner, DLGPROC16 dlgProc,
1013 return HWND_16( DIALOG_CreateIndirect( HINSTANCE_32(hInst), dlgTemplate, WIN_Handle32(owner),
1014 (DLGPROC)dlgProc, param, WIN_PROC_16, FALSE ));
1018 /***********************************************************************
1019 * CreateDialogIndirectParamA (USER32.@)
1021 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
1022 LPCDLGTEMPLATEA dlgTemplate,
1023 HWND owner, DLGPROC dlgProc,
1026 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32A, FALSE );
1029 /***********************************************************************
1030 * CreateDialogIndirectParamAorW (USER32.@)
1032 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
1033 LPCVOID dlgTemplate,
1034 HWND owner, DLGPROC dlgProc,
1036 { FIXME("assume WIN_PROC_32W\n");
1037 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1040 /***********************************************************************
1041 * CreateDialogIndirectParamW (USER32.@)
1043 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1044 LPCDLGTEMPLATEW dlgTemplate,
1045 HWND owner, DLGPROC dlgProc,
1048 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1052 /***********************************************************************
1053 * DIALOG_DoDialogBox
1055 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1057 DIALOGINFO * dlgInfo;
1060 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
1062 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
1064 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1066 ShowWindow( hwnd, SW_SHOW );
1069 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
1071 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1073 /* No message present -> send ENTERIDLE and wait */
1074 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
1075 if (!GetMessageW( &msg, 0, 0, 0 )) break;
1078 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
1080 if (!IsWindow( hwnd )) return -1;
1081 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
1083 TranslateMessage( &msg );
1084 DispatchMessageW( &msg );
1086 if (dlgInfo->flags & DF_END) break;
1089 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
1090 retval = dlgInfo->idResult;
1091 DestroyWindow( hwnd );
1096 /***********************************************************************
1097 * DialogBox (USER.87)
1099 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1100 HWND16 owner, DLGPROC16 dlgProc )
1102 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1106 /***********************************************************************
1107 * DialogBoxParam (USER.239)
1109 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1110 HWND16 owner16, DLGPROC16 dlgProc, LPARAM param )
1118 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1119 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1120 if ((data = LockResource16( hmem )))
1122 HWND owner = WIN_Handle32(owner16);
1123 hwnd = DIALOG_CreateIndirect( HINSTANCE_32(hInst), data, owner,
1124 (DLGPROC)dlgProc, param, WIN_PROC_16, TRUE );
1125 if (hwnd) ret = DIALOG_DoDialogBox( hwnd, owner );
1126 GlobalUnlock16( hmem );
1128 FreeResource16( hmem );
1133 /***********************************************************************
1134 * DialogBoxParamA (USER32.@)
1136 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1137 HWND owner, DLGPROC dlgProc, LPARAM param )
1140 HRSRC hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1141 if (!hrsrc) return 0;
1142 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1143 owner, dlgProc, param, WIN_PROC_32A, TRUE );
1144 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1149 /***********************************************************************
1150 * DialogBoxParamW (USER32.@)
1152 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1153 HWND owner, DLGPROC dlgProc, LPARAM param )
1156 HRSRC hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1157 if (!hrsrc) return 0;
1158 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1159 owner, dlgProc, param, WIN_PROC_32W, TRUE );
1160 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1165 /***********************************************************************
1166 * DialogBoxIndirect (USER.218)
1168 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1169 HWND16 owner, DLGPROC16 dlgProc )
1171 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1175 /***********************************************************************
1176 * DialogBoxIndirectParam (USER.240)
1178 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1179 HWND16 owner16, DLGPROC16 dlgProc,
1182 HWND hwnd, owner = WIN_Handle32( owner16 );
1185 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1186 hwnd = DIALOG_CreateIndirect( HINSTANCE_32(hInst), ptr, owner, (DLGPROC)dlgProc,
1187 param, WIN_PROC_16, TRUE );
1188 GlobalUnlock16( dlgTemplate );
1189 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1194 /***********************************************************************
1195 * DialogBoxIndirectParamA (USER32.@)
1197 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
1198 HWND owner, DLGPROC dlgProc,
1201 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1202 dlgProc, param, WIN_PROC_32A, TRUE );
1203 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1208 /***********************************************************************
1209 * DialogBoxIndirectParamW (USER32.@)
1211 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
1212 HWND owner, DLGPROC dlgProc,
1215 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1216 dlgProc, param, WIN_PROC_32W, TRUE );
1217 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1221 /***********************************************************************
1222 * DialogBoxIndirectParamAorW (USER32.@)
1224 INT_PTR WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1225 HWND owner, DLGPROC dlgProc,
1226 LPARAM param, DWORD x )
1229 FIXME("%p %p %p %p 0x%08lx 0x%08lx\n",
1230 hInstance, template, owner, dlgProc, param, x);
1231 hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, WIN_PROC_32W, TRUE );
1232 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1236 /***********************************************************************
1237 * EndDialog (USER32.@)
1239 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
1241 BOOL wasEnabled = TRUE;
1242 DIALOGINFO * dlgInfo;
1245 TRACE("%p %d\n", hwnd, retval );
1247 if (!(dlgInfo = DIALOG_get_info( hwnd )))
1249 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
1252 dlgInfo->idResult = retval;
1253 dlgInfo->flags |= DF_END;
1254 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1256 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1257 DIALOG_EnableOwner( owner );
1259 /* Windows sets the focus to the dialog itself in EndDialog */
1261 if (IsChild(hwnd, GetFocus()))
1264 /* Don't have to send a ShowWindow(SW_HIDE), just do
1265 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1267 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1268 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1270 /* unblock dialog loop */
1271 PostMessageA(hwnd, WM_NULL, 0, 0);
1276 /***********************************************************************
1277 * DIALOG_IsAccelerator
1279 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
1281 HWND hwndControl = hwnd;
1288 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
1289 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
1291 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1292 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1293 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
1295 /* find the accelerator key */
1296 LPWSTR p = buffer - 2;
1300 p = strchrW( p + 2, '&' );
1302 while (p != NULL && p[1] == '&');
1304 /* and check if it's the one we're looking for */
1305 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
1307 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
1309 /* set focus to the control */
1310 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
1311 /* and bump it on to next */
1312 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1314 else if (dlgCode & DLGC_BUTTON)
1316 /* send BM_CLICK message to the control */
1317 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1322 hwndNext = GetWindow( hwndControl, GW_CHILD );
1326 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1328 while (!hwndNext && hwndControl)
1330 hwndControl = GetParent( hwndControl );
1331 if (hwndControl == hwndDlg)
1333 if(hwnd==hwndDlg) /* prevent endless loop */
1338 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1341 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1343 hwndControl = hwndNext;
1345 while (hwndControl && (hwndControl != hwnd));
1350 /***********************************************************************
1351 * DIALOG_FindMsgDestination
1353 * The messages that IsDialogMessage sends may not go to the dialog
1354 * calling IsDialogMessage if that dialog is a child, and it has the
1355 * DS_CONTROL style set.
1356 * We propagate up until we hit one that does not have DS_CONTROL, or
1357 * whose parent is not a dialog.
1359 * This is undocumented behaviour.
1361 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1363 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1366 HWND hParent = GetParent(hwndDlg);
1367 if (!hParent) break;
1369 pParent = WIN_FindWndPtr(hParent);
1370 if (!pParent) break;
1372 if (!(pParent->flags & WIN_ISDIALOG))
1374 WIN_ReleaseWndPtr(pParent);
1377 WIN_ReleaseWndPtr(pParent);
1385 /***********************************************************************
1386 * IsDialogMessageW (USER32.@)
1388 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1392 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1394 hwndDlg = WIN_GetFullHandle( hwndDlg );
1395 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1397 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1399 switch(msg->message)
1402 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1403 if (dlgCode & DLGC_WANTMESSAGE) break;
1408 if (!(dlgCode & DLGC_WANTTAB))
1410 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1419 if (!(dlgCode & DLGC_WANTARROWS))
1421 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1422 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1423 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1430 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1436 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1437 if (HIWORD(dw) == DC_HASDEFID)
1439 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1440 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1444 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1453 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1454 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1455 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1459 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1461 /* don't translate or dispatch */
1467 TranslateMessage( msg );
1468 DispatchMessageW( msg );
1473 /***********************************************************************
1474 * GetDlgCtrlID (USER32.@)
1476 INT WINAPI GetDlgCtrlID( HWND hwnd )
1478 return GetWindowLongW( hwnd, GWL_ID );
1482 /***********************************************************************
1483 * GetDlgItem (USER32.@)
1485 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1488 HWND *list = WIN_ListChildren( hwndDlg );
1491 if (!list) return 0;
1493 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1495 HeapFree( GetProcessHeap(), 0, list );
1500 /*******************************************************************
1501 * SendDlgItemMessageA (USER32.@)
1503 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1504 WPARAM wParam, LPARAM lParam )
1506 HWND hwndCtrl = GetDlgItem( hwnd, id );
1507 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1512 /*******************************************************************
1513 * SendDlgItemMessageW (USER32.@)
1515 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1516 WPARAM wParam, LPARAM lParam )
1518 HWND hwndCtrl = GetDlgItem( hwnd, id );
1519 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1524 /*******************************************************************
1525 * SetDlgItemTextA (USER32.@)
1527 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1529 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1533 /*******************************************************************
1534 * SetDlgItemTextW (USER32.@)
1536 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1538 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1542 /***********************************************************************
1543 * GetDlgItemTextA (USER32.@)
1545 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1547 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1552 /***********************************************************************
1553 * GetDlgItemTextW (USER32.@)
1555 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1557 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1562 /*******************************************************************
1563 * SetDlgItemInt (USER32.@)
1565 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1570 if (fSigned) sprintf( str, "%d", (INT)value );
1571 else sprintf( str, "%u", value );
1572 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1577 /***********************************************************************
1578 * GetDlgItemInt (USER32.@)
1580 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1587 if (translated) *translated = FALSE;
1588 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1592 result = strtol( str, &endptr, 10 );
1593 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1595 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1600 result = strtoul( str, &endptr, 10 );
1601 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1603 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1605 if (translated) *translated = TRUE;
1606 return (UINT)result;
1610 /***********************************************************************
1611 * CheckDlgButton (USER32.@)
1613 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1615 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1620 /***********************************************************************
1621 * IsDlgButtonChecked (USER32.@)
1623 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1625 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1629 /***********************************************************************
1632 * Callback function used to check/uncheck radio buttons that fall
1633 * within a specific range of IDs.
1635 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1637 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1638 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1640 if ((lChildID >= lpRadioGroup->firstID) &&
1641 (lChildID <= lpRadioGroup->lastID))
1643 if (lChildID == lpRadioGroup->checkID)
1645 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1649 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1657 /***********************************************************************
1658 * CheckRadioButton (USER32.@)
1660 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1661 UINT lastID, UINT checkID )
1663 RADIOGROUP radioGroup;
1665 /* perform bounds checking for a radio button group */
1666 radioGroup.firstID = min(min(firstID, lastID), checkID);
1667 radioGroup.lastID = max(max(firstID, lastID), checkID);
1668 radioGroup.checkID = checkID;
1670 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1671 (LPARAM)&radioGroup);
1675 /***********************************************************************
1676 * GetDialogBaseUnits (USER.243)
1677 * GetDialogBaseUnits (USER32.@)
1679 DWORD WINAPI GetDialogBaseUnits(void)
1681 return MAKELONG( xBaseUnit, yBaseUnit );
1685 /***********************************************************************
1686 * MapDialogRect (USER32.@)
1688 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1690 DIALOGINFO * dlgInfo;
1691 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1692 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1693 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1694 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1695 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1700 /***********************************************************************
1701 * GetNextDlgGroupItem (USER32.@)
1703 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1705 HWND hwnd, retvalue;
1707 hwndDlg = WIN_GetFullHandle( hwndDlg );
1708 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1712 /* if the hwndCtrl is the child of the control in the hwndDlg,
1713 * then the hwndDlg has to be the parent of the hwndCtrl */
1714 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1715 hwndDlg = GetParent(hwndCtrl);
1720 /* Make sure hwndCtrl is a top-level child */
1721 HWND parent = GetParent( hwndCtrl );
1722 while (parent && parent != hwndDlg) parent = GetParent(parent);
1723 if (parent != hwndDlg) return 0;
1727 /* No ctrl specified -> start from the beginning */
1728 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1729 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1732 retvalue = hwndCtrl;
1733 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1736 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1738 /* Wrap-around to the beginning of the group */
1741 hwnd = GetWindow( hwndDlg, GW_CHILD );
1742 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1744 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1745 if (tmp == hwndCtrl) break;
1748 if (hwnd == hwndCtrl) break;
1749 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1752 if (!fPrevious) break;
1754 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1760 /***********************************************************************
1761 * DIALOG_GetNextTabItem
1763 * Helper for GetNextDlgTabItem
1765 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1769 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1771 HWND hChildFirst = 0;
1775 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1776 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1778 else if (IsChild( hwndMain, hwndCtrl ))
1780 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1783 if(GetParent(hwndCtrl) != hwndMain)
1784 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1788 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1790 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1800 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1801 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1802 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1807 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1809 hChildFirst = GetWindow(hChildFirst,wndSearch);
1814 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1816 retWnd = hChildFirst;
1819 hChildFirst = GetWindow(hChildFirst,wndSearch);
1821 if(!retWnd && hwndCtrl)
1823 HWND hParent = GetParent(hwndCtrl);
1826 if(hParent == hwndMain) break;
1827 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1829 hParent = GetParent(hParent);
1832 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1837 /***********************************************************************
1838 * GetNextDlgTabItem (USER32.@)
1840 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1843 hwndDlg = WIN_GetFullHandle( hwndDlg );
1844 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1845 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1848 /**********************************************************************
1849 * DIALOG_DlgDirSelect
1851 * Helper function for DlgDirSelect*
1853 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1854 INT id, BOOL unicode, BOOL combo )
1859 HWND listbox = GetDlgItem( hwnd, id );
1861 TRACE("%p '%s' %d\n", hwnd, str, id );
1862 if (!listbox) return FALSE;
1864 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1865 if (item == LB_ERR) return FALSE;
1866 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1867 if (size == LB_ERR) return FALSE;
1869 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1871 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1873 if ((ret = (buffer[0] == '['))) /* drive or directory */
1875 if (buffer[1] == '-') /* drive */
1883 buffer[strlen(buffer)-1] = '\\';
1891 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1892 ((LPWSTR)str)[len-1] = 0;
1894 else lstrcpynA( str, ptr, len );
1895 HeapFree( GetProcessHeap(), 0, buffer );
1896 TRACE("Returning %d '%s'\n", ret, str );
1901 /**********************************************************************
1904 * Helper function for DlgDirList*
1906 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
1907 INT idStatic, UINT attrib, BOOL combo )
1910 LPSTR orig_spec = spec;
1912 #define SENDMSG(msg,wparam,lparam) \
1913 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
1914 : SendMessageA( hwnd, msg, wparam, lparam ))
1916 TRACE("%p '%s' %d %d %04x\n",
1917 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1919 /* If the path exists and is a directory, chdir to it */
1920 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
1925 if ((p2 = strrchr( p, '\\' ))) p = p2;
1926 if ((p2 = strrchr( p, '/' ))) p = p2;
1931 if (!SetCurrentDirectoryA( spec ))
1933 *p = sep; /* Restore the original spec */
1940 TRACE( "mask=%s\n", spec );
1942 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1944 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1945 if (attrib & DDL_DIRECTORY)
1947 if (!(attrib & DDL_EXCLUSIVE))
1949 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1950 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1951 (LPARAM)spec ) == LB_ERR)
1954 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1955 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1956 (LPARAM)"*.*" ) == LB_ERR)
1961 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1962 (LPARAM)spec ) == LB_ERR)
1967 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1969 char temp[MAX_PATH];
1970 GetCurrentDirectoryA( sizeof(temp), temp );
1972 /* Can't use PostMessage() here, because the string is on the stack */
1973 SetDlgItemTextA( hDlg, idStatic, temp );
1976 if (orig_spec && (spec != orig_spec))
1978 /* Update the original file spec */
1980 while ((*orig_spec++ = *p++));
1988 /**********************************************************************
1989 * DIALOG_DlgDirListW
1991 * Helper function for DlgDirList*W
1993 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1994 INT idStatic, UINT attrib, BOOL combo )
1998 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
1999 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2001 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
2002 HeapFree( GetProcessHeap(), 0, specA );
2005 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2009 /**********************************************************************
2010 * DlgDirSelectExA (USER32.@)
2012 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2014 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
2018 /**********************************************************************
2019 * DlgDirSelectExW (USER32.@)
2021 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2023 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
2027 /**********************************************************************
2028 * DlgDirSelectComboBoxExA (USER32.@)
2030 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2033 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
2037 /**********************************************************************
2038 * DlgDirSelectComboBoxExW (USER32.@)
2040 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2043 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
2047 /**********************************************************************
2048 * DlgDirListA (USER32.@)
2050 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2051 INT idStatic, UINT attrib )
2053 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2057 /**********************************************************************
2058 * DlgDirListW (USER32.@)
2060 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2061 INT idStatic, UINT attrib )
2063 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2067 /**********************************************************************
2068 * DlgDirListComboBoxA (USER32.@)
2070 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2071 INT idStatic, UINT attrib )
2073 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2077 /**********************************************************************
2078 * DlgDirListComboBoxW (USER32.@)
2080 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2081 INT idStatic, UINT attrib )
2083 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );