4 * Copyright 1993, 1994, 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
47 /* Dialog control information */
84 /* Radio button group */
93 /*********************************************************************
94 * dialog class descriptor
96 const struct builtin_class_descr DIALOG_builtin_class =
98 DIALOG_CLASS_ATOMA, /* name */
99 CS_SAVEBITS | CS_DBLCLKS, /* style */
100 DefDlgProcA, /* procA */
101 DefDlgProcW, /* procW */
102 DLGWINDOWEXTRA, /* extra */
103 IDC_ARROW, /* cursor */
108 /***********************************************************************
111 * Helper function for modal dialogs to enable again the
112 * owner of the dialog box.
114 void DIALOG_EnableOwner( HWND hOwner )
116 /* Owner must be a top-level window */
118 hOwner = GetAncestor( hOwner, GA_ROOT );
120 EnableWindow( hOwner, TRUE );
124 /***********************************************************************
125 * DIALOG_DisableOwner
127 * Helper function for modal dialogs to disable the
128 * owner of the dialog box. Returns TRUE if owner was enabled.
130 BOOL DIALOG_DisableOwner( HWND hOwner )
132 /* Owner must be a top-level window */
134 hOwner = GetAncestor( hOwner, GA_ROOT );
135 if (!hOwner) return FALSE;
136 if (IsWindowEnabled( hOwner ))
138 EnableWindow( hOwner, FALSE );
145 /***********************************************************************
146 * DIALOG_GetControl32
148 * Return the class and text of the control pointed to by ptr,
149 * fill the header structure and return a pointer to the next control.
151 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
156 info->helpId = GET_DWORD(p); p += 2;
157 info->exStyle = GET_DWORD(p); p += 2;
158 info->style = GET_DWORD(p); p += 2;
163 info->style = GET_DWORD(p); p += 2;
164 info->exStyle = GET_DWORD(p); p += 2;
166 info->x = GET_WORD(p); p++;
167 info->y = GET_WORD(p); p++;
168 info->cx = GET_WORD(p); p++;
169 info->cy = GET_WORD(p); p++;
173 /* id is a DWORD for DIALOGEX */
174 info->id = GET_DWORD(p);
179 info->id = GET_WORD(p);
183 if (GET_WORD(p) == 0xffff)
185 static const WCHAR class_names[6][10] =
187 { 'B','u','t','t','o','n', }, /* 0x80 */
188 { 'E','d','i','t', }, /* 0x81 */
189 { 'S','t','a','t','i','c', }, /* 0x82 */
190 { 'L','i','s','t','B','o','x', }, /* 0x83 */
191 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
192 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
194 WORD id = GET_WORD(p+1);
195 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
196 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
198 info->className = class_names[id];
201 info->className = NULL;
202 ERR("Unknown built-in class id %04x\n", id );
208 info->className = (LPCWSTR)p;
209 p += strlenW( info->className ) + 1;
212 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
214 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
219 info->windowName = (LPCWSTR)p;
220 p += strlenW( info->windowName ) + 1;
223 TRACE(" %s %s %d, %d, %d, %d, %d, %08x, %08x, %08x\n",
224 debugstr_w( info->className ), debugstr_w( info->windowName ),
225 info->id, info->x, info->y, info->cx, info->cy,
226 info->style, info->exStyle, info->helpId );
230 if (TRACE_ON(dialog))
232 WORD i, count = GET_WORD(p) / sizeof(WORD);
235 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
240 p += GET_WORD(p) / sizeof(WORD);
242 else info->data = NULL;
245 /* Next control is on dword boundary */
246 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
250 /***********************************************************************
251 * DIALOG_CreateControls32
253 * Create the control windows for a dialog.
255 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
256 HINSTANCE hInst, BOOL unicode )
258 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
259 DLG_CONTROL_INFO info;
260 HWND hwndCtrl, hwndDefButton = 0;
261 INT items = dlgTemplate->nbItems;
266 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
267 dlgTemplate->dialogEx );
269 if (info.style & WS_BORDER)
271 info.style &= ~WS_BORDER;
272 info.exStyle |= WS_EX_CLIENTEDGE;
276 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
277 info.className, info.windowName,
278 info.style | WS_CHILD,
279 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
281 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
282 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
283 hwnd, (HMENU)info.id,
284 hInst, (LPVOID)info.data );
288 LPSTR class = (LPSTR)info.className;
289 LPSTR caption = (LPSTR)info.windowName;
293 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
294 class = HeapAlloc( GetProcessHeap(), 0, len );
295 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
299 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
300 caption = HeapAlloc( GetProcessHeap(), 0, len );
301 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
303 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
304 class, caption, info.style | WS_CHILD,
305 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
306 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
307 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
308 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
309 hwnd, (HMENU)info.id,
310 hInst, (LPVOID)info.data );
311 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
312 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
316 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
320 /* Send initialisation messages to the control */
321 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
322 (WPARAM)dlgInfo->hUserFont, 0 );
323 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
325 /* If there's already a default push-button, set it back */
326 /* to normal and use this one instead. */
328 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
329 hwndDefButton = hwndCtrl;
330 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
338 /***********************************************************************
339 * DIALOG_ParseTemplate32
341 * Fill a DLG_TEMPLATE structure from the dialog template, and return
342 * a pointer to the first control.
344 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
346 const WORD *p = (const WORD *)template;
350 signature = GET_WORD(p); p++;
351 dlgver = GET_WORD(p); p++;
353 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
355 result->dialogEx = TRUE;
356 result->helpId = GET_DWORD(p); p += 2;
357 result->exStyle = GET_DWORD(p); p += 2;
358 result->style = GET_DWORD(p); p += 2;
362 result->style = GET_DWORD(p - 2);
363 result->dialogEx = FALSE;
365 result->exStyle = GET_DWORD(p); p += 2;
367 result->nbItems = GET_WORD(p); p++;
368 result->x = GET_WORD(p); p++;
369 result->y = GET_WORD(p); p++;
370 result->cx = GET_WORD(p); p++;
371 result->cy = GET_WORD(p); p++;
372 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
373 result->dialogEx ? "EX" : "", result->x, result->y,
374 result->cx, result->cy, result->helpId );
375 TRACE(" STYLE 0x%08x\n", result->style );
376 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
378 /* Get the menu name */
383 result->menuName = NULL;
387 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
389 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
392 result->menuName = (LPCWSTR)p;
393 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
394 p += strlenW( result->menuName ) + 1;
398 /* Get the class name */
403 result->className = DIALOG_CLASS_ATOMW;
407 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
409 TRACE(" CLASS %04x\n", LOWORD(result->className) );
412 result->className = (LPCWSTR)p;
413 TRACE(" CLASS %s\n", debugstr_w( result->className ));
414 p += strlenW( result->className ) + 1;
418 /* Get the window caption */
420 result->caption = (LPCWSTR)p;
421 p += strlenW( result->caption ) + 1;
422 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
424 /* Get the font name */
426 result->pointSize = 0;
427 result->faceName = NULL;
428 result->weight = FW_DONTCARE;
429 result->italic = FALSE;
431 if (result->style & DS_SETFONT)
433 result->pointSize = GET_WORD(p);
435 if (result->dialogEx)
437 result->weight = GET_WORD(p); p++;
438 result->italic = LOBYTE(GET_WORD(p)); p++;
440 result->faceName = (LPCWSTR)p;
441 p += strlenW( result->faceName ) + 1;
442 TRACE(" FONT %d, %s, %d, %s\n",
443 result->pointSize, debugstr_w( result->faceName ),
444 result->weight, result->italic ? "TRUE" : "FALSE" );
447 /* First control is on dword boundary */
448 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
452 /***********************************************************************
453 * DIALOG_CreateIndirect
454 * Creates a dialog box window
456 * modal = TRUE if we are called from a modal dialog box.
457 * (it's more compatible to do it here, as under Windows the owner
458 * is never disabled if the dialog fails because of an invalid template)
460 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
461 HWND owner, DLGPROC dlgProc, LPARAM param,
462 BOOL unicode, BOOL modal )
468 DLG_TEMPLATE template;
469 DIALOGINFO * dlgInfo = NULL;
470 DWORD units = GetDialogBaseUnits();
471 BOOL ownerEnabled = TRUE;
475 UINT xBaseUnit = LOWORD(units);
476 UINT yBaseUnit = HIWORD(units);
478 /* Parse dialog template */
480 if (!dlgTemplate) return 0;
481 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
485 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
487 /* Create custom font if needed */
489 if (template.style & DS_SETFONT)
491 /* We convert the size to pixels and then make it -ve. This works
492 * for both +ve and -ve template.pointSize */
496 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
497 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
498 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
499 PROOF_QUALITY, FF_DONTCARE,
504 HFONT hOldFont = SelectObject( dc, hUserFont );
505 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
508 xBaseUnit = charSize.cx;
509 yBaseUnit = charSize.cy;
511 SelectObject( dc, hOldFont );
514 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
517 /* Create dialog main window */
519 rect.left = rect.top = 0;
520 rect.right = MulDiv(template.cx, xBaseUnit, 4);
521 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
522 if (template.style & WS_CHILD)
523 template.style &= ~(WS_CAPTION|WS_SYSMENU);
524 if (template.style & DS_MODALFRAME)
525 template.exStyle |= WS_EX_DLGMODALFRAME;
526 if (template.style & DS_CONTROL)
527 template.exStyle |= WS_EX_CONTROLPARENT;
528 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
531 size.cx = rect.right - rect.left;
532 size.cy = rect.bottom - rect.top;
534 if (template.x == CW_USEDEFAULT16)
536 pos.x = pos.y = CW_USEDEFAULT;
540 HMONITOR monitor = 0;
541 MONITORINFO mon_info;
543 mon_info.cbSize = sizeof(mon_info);
544 if (template.style & DS_CENTER)
546 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
547 GetMonitorInfoW( monitor, &mon_info );
548 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
549 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
551 else if (template.style & DS_CENTERMOUSE)
553 GetCursorPos( &pos );
554 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
555 GetMonitorInfoW( monitor, &mon_info );
559 pos.x += MulDiv(template.x, xBaseUnit, 4);
560 pos.y += MulDiv(template.y, yBaseUnit, 8);
561 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
563 if ( !(template.style & WS_CHILD) )
567 /* try to fit it into the desktop */
571 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
572 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
573 GetMonitorInfoW( monitor, &mon_info );
575 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
577 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
579 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
580 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
586 ownerEnabled = DIALOG_DisableOwner( owner );
587 if (ownerEnabled) flags |= DF_OWNERENABLED;
592 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
593 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
594 owner, hMenu, hInst, NULL );
598 LPSTR class = (LPSTR)template.className;
599 LPSTR caption = (LPSTR)template.caption;
603 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
604 class = HeapAlloc( GetProcessHeap(), 0, len );
605 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
609 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
610 caption = HeapAlloc( GetProcessHeap(), 0, len );
611 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
613 hwnd = CreateWindowExA(template.exStyle, class, caption,
614 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
615 owner, hMenu, hInst, NULL );
616 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
617 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
622 if (hUserFont) DeleteObject( hUserFont );
623 if (hMenu) DestroyMenu( hMenu );
624 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
628 /* moved this from the top of the method to here as DIALOGINFO structure
629 will be valid only after WM_CREATE message has been handled in DefDlgProc
630 All the members of the structure get filled here using temp variables */
631 dlgInfo = DIALOG_get_info( hwnd, TRUE );
632 dlgInfo->hwndFocus = 0;
633 dlgInfo->hUserFont = hUserFont;
634 dlgInfo->hMenu = hMenu;
635 dlgInfo->xBaseUnit = xBaseUnit;
636 dlgInfo->yBaseUnit = yBaseUnit;
637 dlgInfo->idResult = 0;
638 dlgInfo->flags = flags;
639 dlgInfo->hDialogHeap = 0;
641 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
643 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
644 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
646 if (dlgInfo->hUserFont)
647 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
649 /* Create controls */
651 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
653 /* Send initialisation messages and set focus */
655 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
656 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
658 /* By returning TRUE, app has requested a default focus assignment */
659 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
660 if( dlgInfo->hwndFocus )
661 SetFocus( dlgInfo->hwndFocus );
664 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
666 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
670 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
671 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
676 /***********************************************************************
677 * CreateDialogParamA (USER32.@)
679 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
680 DLGPROC dlgProc, LPARAM param )
685 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
686 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
687 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
691 /***********************************************************************
692 * CreateDialogParamW (USER32.@)
694 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
695 DLGPROC dlgProc, LPARAM param )
700 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
701 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
702 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
706 /***********************************************************************
707 * CreateDialogIndirectParamAorW (USER32.@)
709 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
710 HWND owner, DLGPROC dlgProc, LPARAM param,
713 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
716 /***********************************************************************
717 * CreateDialogIndirectParamA (USER32.@)
719 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
720 HWND owner, DLGPROC dlgProc, LPARAM param )
722 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
725 /***********************************************************************
726 * CreateDialogIndirectParamW (USER32.@)
728 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
729 HWND owner, DLGPROC dlgProc, LPARAM param )
731 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
735 /***********************************************************************
738 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
740 DIALOGINFO * dlgInfo;
743 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
746 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
749 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
753 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
757 /* ShowWindow the first time the queue goes empty */
758 ShowWindow( hwnd, SW_SHOWNORMAL );
761 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
763 /* No message present -> send ENTERIDLE and wait */
764 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
766 if (!GetMessageW( &msg, 0, 0, 0 )) break;
769 if (!IsWindow( hwnd )) return -1;
770 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
772 TranslateMessage( &msg );
773 DispatchMessageW( &msg );
775 if (dlgInfo->flags & DF_END) break;
778 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
779 retval = dlgInfo->idResult;
780 DestroyWindow( hwnd );
785 /***********************************************************************
786 * DialogBoxParamA (USER32.@)
788 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
789 HWND owner, DLGPROC dlgProc, LPARAM param )
795 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
796 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
797 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
798 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
803 /***********************************************************************
804 * DialogBoxParamW (USER32.@)
806 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
807 HWND owner, DLGPROC dlgProc, LPARAM param )
813 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
814 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
815 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
816 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
821 /***********************************************************************
822 * DialogBoxIndirectParamAorW (USER32.@)
824 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
825 HWND owner, DLGPROC dlgProc,
826 LPARAM param, DWORD flags )
828 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
829 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
833 /***********************************************************************
834 * DialogBoxIndirectParamA (USER32.@)
836 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
837 HWND owner, DLGPROC dlgProc, LPARAM param )
839 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
843 /***********************************************************************
844 * DialogBoxIndirectParamW (USER32.@)
846 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
847 HWND owner, DLGPROC dlgProc, LPARAM param )
849 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
852 /***********************************************************************
853 * EndDialog (USER32.@)
855 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
857 BOOL wasEnabled = TRUE;
858 DIALOGINFO * dlgInfo;
861 TRACE("%p %d\n", hwnd, retval );
863 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
865 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
868 dlgInfo->idResult = retval;
869 dlgInfo->flags |= DF_END;
870 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
872 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
873 DIALOG_EnableOwner( owner );
875 /* Windows sets the focus to the dialog itself in EndDialog */
877 if (IsChild(hwnd, GetFocus()))
880 /* Don't have to send a ShowWindow(SW_HIDE), just do
881 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
883 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
884 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
886 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
888 /* unblock dialog loop */
889 PostMessageA(hwnd, WM_NULL, 0, 0);
894 /***********************************************************************
895 * DIALOG_IsAccelerator
897 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
899 HWND hwndControl = hwnd;
906 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
907 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
909 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
910 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
911 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
913 /* find the accelerator key */
914 LPWSTR p = buffer - 2;
918 p = strchrW( p + 2, '&' );
920 while (p != NULL && p[1] == '&');
922 /* and check if it's the one we're looking for */
923 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
925 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
927 /* set focus to the control */
928 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
929 /* and bump it on to next */
930 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
932 else if (dlgCode & DLGC_BUTTON)
934 /* send BM_CLICK message to the control */
935 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
940 hwndNext = GetWindow( hwndControl, GW_CHILD );
944 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
946 while (!hwndNext && hwndControl)
948 hwndControl = GetParent( hwndControl );
949 if (hwndControl == hwndDlg)
951 if(hwnd==hwndDlg) /* prevent endless loop */
956 hwndNext = GetWindow( hwndDlg, GW_CHILD );
959 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
961 hwndControl = hwndNext;
963 while (hwndControl && (hwndControl != hwnd));
968 /***********************************************************************
969 * DIALOG_FindMsgDestination
971 * The messages that IsDialogMessage sends may not go to the dialog
972 * calling IsDialogMessage if that dialog is a child, and it has the
973 * DS_CONTROL style set.
974 * We propagate up until we hit one that does not have DS_CONTROL, or
975 * whose parent is not a dialog.
977 * This is undocumented behaviour.
979 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
981 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
984 HWND hParent = GetParent(hwndDlg);
987 pParent = WIN_GetPtr(hParent);
988 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
990 if (!(pParent->flags & WIN_ISDIALOG))
992 WIN_ReleasePtr(pParent);
995 WIN_ReleasePtr(pParent);
1003 /***********************************************************************
1004 * DIALOG_FixOneChildOnChangeFocus
1006 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1009 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1012 /* If a default pushbutton then no longer default */
1013 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1014 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1018 /***********************************************************************
1019 * DIALOG_FixChildrenOnChangeFocus
1021 * Following the change of focus that occurs for example after handling
1022 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1023 * children may be required.
1025 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1027 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1028 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1029 /* Windows does ask for this. I don't know why yet */
1031 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1033 /* If the button that is getting the focus WAS flagged as the default
1034 * pushbutton then ask the dialog what it thinks the default is and
1035 * set that in the default style.
1037 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1039 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1040 if (HIWORD(def_id) == DC_HASDEFID)
1043 def_id = LOWORD(def_id);
1044 hwndDef = GetDlgItem (hwndDlg, def_id);
1047 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1048 /* I know that if it is a button then it should already be a
1049 * UNDEFPUSHBUTTON, since we have just told the buttons to
1050 * change style. But maybe they ignored our request
1052 if ((dlgcode_def & DLGC_BUTTON) &&
1053 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1055 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1060 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1062 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1063 /* I wonder why it doesn't send a DM_SETDEFID */
1067 /***********************************************************************
1068 * IsDialogMessageW (USER32.@)
1070 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1074 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1076 hwndDlg = WIN_GetFullHandle( hwndDlg );
1077 if (hwndDlg == GetDesktopWindow()) return FALSE;
1078 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1080 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1082 switch(msg->message)
1085 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1086 if (dlgCode & DLGC_WANTMESSAGE) break;
1091 if (!(dlgCode & DLGC_WANTTAB))
1093 BOOL fIsDialog = TRUE;
1094 WND *pWnd = WIN_GetPtr( hwndDlg );
1096 if (pWnd && pWnd != WND_OTHER_PROCESS)
1098 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1099 WIN_ReleasePtr(pWnd);
1102 /* I am not sure under which circumstances the TAB is handled
1103 * each way. All I do know is that it does not always simply
1104 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1105 * do so but I presume someone has)
1108 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1111 /* It would appear that GetNextDlgTabItem can handle being
1112 * passed hwndDlg rather than NULL but that is undocumented
1113 * so let's do it properly
1115 HWND hwndFocus = GetFocus();
1116 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1117 hwndFocus == hwndDlg ? NULL : hwndFocus,
1118 GetKeyState (VK_SHIFT) & 0x8000);
1121 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1122 msg->wParam, (LPARAM)msg);
1123 if (dlgCode & DLGC_HASSETSEL)
1125 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1126 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1130 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1131 length = strlenW (buffer);
1132 HeapFree (GetProcessHeap(), 0, buffer);
1133 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1136 SetFocus (hwndNext);
1137 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1150 if (!(dlgCode & DLGC_WANTARROWS))
1152 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1153 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1154 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1161 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1168 if ((GetFocus() == msg->hwnd) &&
1169 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1171 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1173 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1175 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1176 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1180 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1189 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1190 * It does NOT get sent in the test program I have
1192 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1193 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1194 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1198 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1200 /* don't translate or dispatch */
1206 TranslateMessage( msg );
1207 DispatchMessageW( msg );
1212 /***********************************************************************
1213 * GetDlgCtrlID (USER32.@)
1215 INT WINAPI GetDlgCtrlID( HWND hwnd )
1217 return GetWindowLongPtrW( hwnd, GWLP_ID );
1221 /***********************************************************************
1222 * GetDlgItem (USER32.@)
1224 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1227 HWND *list = WIN_ListChildren( hwndDlg );
1230 if (!list) return 0;
1232 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1234 HeapFree( GetProcessHeap(), 0, list );
1239 /*******************************************************************
1240 * SendDlgItemMessageA (USER32.@)
1242 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1243 WPARAM wParam, LPARAM lParam )
1245 HWND hwndCtrl = GetDlgItem( hwnd, id );
1246 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1251 /*******************************************************************
1252 * SendDlgItemMessageW (USER32.@)
1254 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1255 WPARAM wParam, LPARAM lParam )
1257 HWND hwndCtrl = GetDlgItem( hwnd, id );
1258 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1263 /*******************************************************************
1264 * SetDlgItemTextA (USER32.@)
1266 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1268 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1272 /*******************************************************************
1273 * SetDlgItemTextW (USER32.@)
1275 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1277 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1281 /***********************************************************************
1282 * GetDlgItemTextA (USER32.@)
1284 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1286 if (str && (len > 0)) str[0] = '\0';
1287 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1292 /***********************************************************************
1293 * GetDlgItemTextW (USER32.@)
1295 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1297 if (str && (len > 0)) str[0] = '\0';
1298 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1303 /*******************************************************************
1304 * SetDlgItemInt (USER32.@)
1306 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1311 if (fSigned) sprintf( str, "%d", (INT)value );
1312 else sprintf( str, "%u", value );
1313 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1318 /***********************************************************************
1319 * GetDlgItemInt (USER32.@)
1321 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1328 if (translated) *translated = FALSE;
1329 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1333 result = strtol( str, &endptr, 10 );
1334 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1336 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1341 result = strtoul( str, &endptr, 10 );
1342 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1344 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1346 if (translated) *translated = TRUE;
1347 return (UINT)result;
1351 /***********************************************************************
1352 * CheckDlgButton (USER32.@)
1354 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1356 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1361 /***********************************************************************
1362 * IsDlgButtonChecked (USER32.@)
1364 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1366 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1370 /***********************************************************************
1373 * Callback function used to check/uncheck radio buttons that fall
1374 * within a specific range of IDs.
1376 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1378 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1379 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1381 if ((lChildID >= lpRadioGroup->firstID) &&
1382 (lChildID <= lpRadioGroup->lastID))
1384 if (lChildID == lpRadioGroup->checkID)
1386 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1390 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1398 /***********************************************************************
1399 * CheckRadioButton (USER32.@)
1401 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1402 int lastID, int checkID )
1404 RADIOGROUP radioGroup;
1406 radioGroup.firstID = firstID;
1407 radioGroup.lastID = lastID;
1408 radioGroup.checkID = checkID;
1410 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1411 (LPARAM)&radioGroup);
1415 /***********************************************************************
1416 * GetDialogBaseUnits (USER.243)
1417 * GetDialogBaseUnits (USER32.@)
1419 DWORD WINAPI GetDialogBaseUnits(void)
1428 if ((hdc = GetDC(0)))
1430 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1431 if (size.cx) units = MAKELONG( size.cx, size.cy );
1432 ReleaseDC( 0, hdc );
1434 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1440 /***********************************************************************
1441 * MapDialogRect (USER32.@)
1443 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1445 DIALOGINFO * dlgInfo;
1446 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1447 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1448 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1449 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1450 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1455 /***********************************************************************
1456 * GetNextDlgGroupItem (USER32.@)
1458 * Corrections to MSDN documentation
1460 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1461 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1462 * 2. Prev of NULL or hwndDlg fails
1464 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1466 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1468 BOOL fSkipping=FALSE;
1470 hwndDlg = WIN_GetFullHandle( hwndDlg );
1471 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1473 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1474 if (!hwndCtrl && fPrevious) return 0;
1478 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1482 /* No ctrl specified -> start from the beginning */
1483 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1484 /* MSDN is wrong. fPrevious does not result in the last child */
1486 /* Maybe that first one is valid. If so then we don't want to skip it*/
1487 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1493 /* Always go forward around the group and list of controls; for the
1494 * previous control keep track; for the next break when you find one
1496 retvalue = hwndCtrl;
1498 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1503 /* Climb out until there is a next sibling of the ancestor or we
1504 * reach the top (in which case we loop back to the start)
1506 if (hwndDlg == GetParent (hwnd))
1508 /* Wrap around to the beginning of the list, within the same
1509 * group. (Once only)
1511 if (fLooped) goto end;
1513 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1517 hwnd = GetParent (hwnd);
1518 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1523 /* Wander down the leading edge of controlparents */
1524 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1525 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1526 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1528 /* Question. If the control is a control parent but either has no
1529 * children or is not visible/enabled then if it has a WS_GROUP does
1530 * it count? For that matter does it count anyway?
1531 * I believe it doesn't count.
1534 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1536 hwndLastGroup = hwnd;
1539 /* Look for the beginning of the group */
1544 if (hwnd == hwndCtrl)
1546 if (!fSkipping) break;
1547 if (hwndLastGroup == hwnd) break;
1548 hwnd = hwndLastGroup;
1554 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1558 if (!fPrevious) break;
1566 /***********************************************************************
1567 * DIALOG_GetNextTabItem
1569 * Recursive helper for GetNextDlgTabItem
1571 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1575 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1577 HWND hChildFirst = 0;
1581 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1582 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1584 else if (IsChild( hwndMain, hwndCtrl ))
1586 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1589 if(GetParent(hwndCtrl) != hwndMain)
1590 /* i.e. if we are not at the top level of the recursion */
1591 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1593 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1599 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1600 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1601 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1604 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1605 if (retWnd) return (retWnd);
1607 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1609 return (hChildFirst);
1611 hChildFirst = GetWindow(hChildFirst,wndSearch);
1615 HWND hParent = GetParent(hwndCtrl);
1618 if(hParent == hwndMain) break;
1619 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1621 hParent = GetParent(hParent);
1624 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1626 return retWnd ? retWnd : hwndCtrl;
1629 /***********************************************************************
1630 * GetNextDlgTabItem (USER32.@)
1632 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1635 hwndDlg = WIN_GetFullHandle( hwndDlg );
1636 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1638 /* Undocumented but tested under Win2000 and WinME */
1639 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1641 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1642 * NB GetLastError returns whatever was set before the function was
1645 if (!hwndCtrl && fPrevious) return 0;
1647 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1650 /**********************************************************************
1651 * DIALOG_DlgDirSelect
1653 * Helper function for DlgDirSelect*
1655 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1656 INT id, BOOL unicode, BOOL combo )
1658 WCHAR *buffer, *ptr;
1661 HWND listbox = GetDlgItem( hwnd, id );
1663 TRACE("%p '%s' %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1664 if (!listbox) return FALSE;
1666 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1667 if (item == LB_ERR) return FALSE;
1669 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1670 if (size == LB_ERR) return FALSE;
1672 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+1) * sizeof(WCHAR) ))) return FALSE;
1674 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1676 if ((ret = (buffer[0] == '['))) /* drive or directory */
1678 if (buffer[1] == '-') /* drive */
1686 buffer[strlenW(buffer)-1] = '\\';
1694 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1695 ((LPSTR)str)[len-1] = 0;
1697 else lstrcpynW( str, ptr, len );
1698 HeapFree( GetProcessHeap(), 0, buffer );
1699 TRACE("Returning %d '%s'\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1704 /**********************************************************************
1705 * DIALOG_DlgDirListW
1707 * Helper function for DlgDirList*W
1709 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1710 INT idStatic, UINT attrib, BOOL combo )
1713 LPWSTR orig_spec = spec;
1714 WCHAR any[] = {'*','.','*',0};
1716 #define SENDMSG(msg,wparam,lparam) \
1717 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1718 : SendMessageW( hwnd, msg, wparam, lparam ))
1720 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1722 /* If the path exists and is a directory, chdir to it */
1723 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1728 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1729 if ((p2 = strrchrW( p, '/' ))) p = p2;
1734 if (!SetCurrentDirectoryW( spec ))
1736 *p = sep; /* Restore the original spec */
1743 TRACE( "mask=%s\n", debugstr_w(spec) );
1745 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1747 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1748 if (attrib & DDL_DIRECTORY)
1750 if (!(attrib & DDL_EXCLUSIVE))
1752 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1753 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1754 (LPARAM)spec ) == LB_ERR)
1757 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1758 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1759 (LPARAM)any ) == LB_ERR)
1764 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1765 (LPARAM)spec ) == LB_ERR)
1770 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1772 WCHAR temp[MAX_PATH];
1773 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1775 /* Can't use PostMessage() here, because the string is on the stack */
1776 SetDlgItemTextW( hDlg, idStatic, temp );
1779 if (orig_spec && (spec != orig_spec))
1781 /* Update the original file spec */
1783 while ((*orig_spec++ = *p++));
1791 /**********************************************************************
1792 * DIALOG_DlgDirListA
1794 * Helper function for DlgDirList*A
1796 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1797 INT idStatic, UINT attrib, BOOL combo )
1801 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1802 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1803 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1804 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1805 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1806 HeapFree( GetProcessHeap(), 0, specW );
1809 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1813 /**********************************************************************
1814 * DlgDirSelectExA (USER32.@)
1816 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1818 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1822 /**********************************************************************
1823 * DlgDirSelectExW (USER32.@)
1825 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1827 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1831 /**********************************************************************
1832 * DlgDirSelectComboBoxExA (USER32.@)
1834 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1837 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1841 /**********************************************************************
1842 * DlgDirSelectComboBoxExW (USER32.@)
1844 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1847 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1851 /**********************************************************************
1852 * DlgDirListA (USER32.@)
1854 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1855 INT idStatic, UINT attrib )
1857 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1861 /**********************************************************************
1862 * DlgDirListW (USER32.@)
1864 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1865 INT idStatic, UINT attrib )
1867 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1871 /**********************************************************************
1872 * DlgDirListComboBoxA (USER32.@)
1874 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1875 INT idStatic, UINT attrib )
1877 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1881 /**********************************************************************
1882 * DlgDirListComboBoxW (USER32.@)
1884 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1885 INT idStatic, UINT attrib )
1887 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );