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 (LPCSTR)DIALOG_CLASS_ATOM, /* 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 LPCSTR class = (LPCSTR)info.className;
289 LPCSTR caption = (LPCSTR)info.windowName;
290 LPSTR class_tmp = NULL;
291 LPSTR caption_tmp = NULL;
295 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
296 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
297 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
302 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
303 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
304 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
305 caption = caption_tmp;
307 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
308 class, caption, info.style | WS_CHILD,
309 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
310 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
311 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
312 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
313 hwnd, (HMENU)info.id,
314 hInst, (LPVOID)info.data );
315 HeapFree( GetProcessHeap(), 0, class_tmp );
316 HeapFree( GetProcessHeap(), 0, caption_tmp );
320 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
324 /* Send initialisation messages to the control */
325 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
326 (WPARAM)dlgInfo->hUserFont, 0 );
327 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
329 /* If there's already a default push-button, set it back */
330 /* to normal and use this one instead. */
332 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
333 hwndDefButton = hwndCtrl;
334 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
342 /***********************************************************************
343 * DIALOG_ParseTemplate32
345 * Fill a DLG_TEMPLATE structure from the dialog template, and return
346 * a pointer to the first control.
348 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
350 const WORD *p = (const WORD *)template;
354 signature = GET_WORD(p); p++;
355 dlgver = GET_WORD(p); p++;
357 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
359 result->dialogEx = TRUE;
360 result->helpId = GET_DWORD(p); p += 2;
361 result->exStyle = GET_DWORD(p); p += 2;
362 result->style = GET_DWORD(p); p += 2;
366 result->style = GET_DWORD(p - 2);
367 result->dialogEx = FALSE;
369 result->exStyle = GET_DWORD(p); p += 2;
371 result->nbItems = GET_WORD(p); p++;
372 result->x = GET_WORD(p); p++;
373 result->y = GET_WORD(p); p++;
374 result->cx = GET_WORD(p); p++;
375 result->cy = GET_WORD(p); p++;
376 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
377 result->dialogEx ? "EX" : "", result->x, result->y,
378 result->cx, result->cy, result->helpId );
379 TRACE(" STYLE 0x%08x\n", result->style );
380 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
382 /* Get the menu name */
387 result->menuName = NULL;
391 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
393 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
396 result->menuName = (LPCWSTR)p;
397 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
398 p += strlenW( result->menuName ) + 1;
402 /* Get the class name */
407 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
411 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
413 TRACE(" CLASS %04x\n", LOWORD(result->className) );
416 result->className = (LPCWSTR)p;
417 TRACE(" CLASS %s\n", debugstr_w( result->className ));
418 p += strlenW( result->className ) + 1;
422 /* Get the window caption */
424 result->caption = (LPCWSTR)p;
425 p += strlenW( result->caption ) + 1;
426 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
428 /* Get the font name */
430 result->pointSize = 0;
431 result->faceName = NULL;
432 result->weight = FW_DONTCARE;
433 result->italic = FALSE;
435 if (result->style & DS_SETFONT)
437 result->pointSize = GET_WORD(p);
439 if (result->dialogEx)
441 result->weight = GET_WORD(p); p++;
442 result->italic = LOBYTE(GET_WORD(p)); p++;
444 result->faceName = (LPCWSTR)p;
445 p += strlenW( result->faceName ) + 1;
446 TRACE(" FONT %d, %s, %d, %s\n",
447 result->pointSize, debugstr_w( result->faceName ),
448 result->weight, result->italic ? "TRUE" : "FALSE" );
451 /* First control is on dword boundary */
452 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
456 /***********************************************************************
457 * DIALOG_CreateIndirect
458 * Creates a dialog box window
460 * modal = TRUE if we are called from a modal dialog box.
461 * (it's more compatible to do it here, as under Windows the owner
462 * is never disabled if the dialog fails because of an invalid template)
464 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
465 HWND owner, DLGPROC dlgProc, LPARAM param,
466 BOOL unicode, BOOL modal )
472 DLG_TEMPLATE template;
473 DIALOGINFO * dlgInfo = NULL;
474 DWORD units = GetDialogBaseUnits();
475 BOOL ownerEnabled = TRUE;
479 UINT xBaseUnit = LOWORD(units);
480 UINT yBaseUnit = HIWORD(units);
482 /* Parse dialog template */
484 if (!dlgTemplate) return 0;
485 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
489 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
491 /* Create custom font if needed */
493 if (template.style & DS_SETFONT)
495 /* We convert the size to pixels and then make it -ve. This works
496 * for both +ve and -ve template.pointSize */
500 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
501 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
502 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
503 PROOF_QUALITY, FF_DONTCARE,
508 HFONT hOldFont = SelectObject( dc, hUserFont );
509 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
512 xBaseUnit = charSize.cx;
513 yBaseUnit = charSize.cy;
515 SelectObject( dc, hOldFont );
518 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
521 /* Create dialog main window */
523 rect.left = rect.top = 0;
524 rect.right = MulDiv(template.cx, xBaseUnit, 4);
525 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
526 if (template.style & WS_CHILD)
527 template.style &= ~(WS_CAPTION|WS_SYSMENU);
528 if (template.style & DS_MODALFRAME)
529 template.exStyle |= WS_EX_DLGMODALFRAME;
530 if (template.style & DS_CONTROL)
531 template.exStyle |= WS_EX_CONTROLPARENT;
532 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
535 size.cx = rect.right - rect.left;
536 size.cy = rect.bottom - rect.top;
538 if (template.x == CW_USEDEFAULT16)
540 pos.x = pos.y = CW_USEDEFAULT;
544 HMONITOR monitor = 0;
545 MONITORINFO mon_info;
547 mon_info.cbSize = sizeof(mon_info);
548 if (template.style & DS_CENTER)
550 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
551 GetMonitorInfoW( monitor, &mon_info );
552 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
553 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
555 else if (template.style & DS_CENTERMOUSE)
557 GetCursorPos( &pos );
558 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
559 GetMonitorInfoW( monitor, &mon_info );
563 pos.x += MulDiv(template.x, xBaseUnit, 4);
564 pos.y += MulDiv(template.y, yBaseUnit, 8);
565 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
567 if ( !(template.style & WS_CHILD) )
571 /* try to fit it into the desktop */
575 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
576 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
577 GetMonitorInfoW( monitor, &mon_info );
579 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
581 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
583 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
584 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
590 ownerEnabled = DIALOG_DisableOwner( owner );
591 if (ownerEnabled) flags |= DF_OWNERENABLED;
596 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
597 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
598 owner, hMenu, hInst, NULL );
602 LPCSTR class = (LPCSTR)template.className;
603 LPCSTR caption = (LPCSTR)template.caption;
604 LPSTR class_tmp = NULL;
605 LPSTR caption_tmp = NULL;
609 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
610 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
611 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
616 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
617 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
618 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
619 caption = caption_tmp;
621 hwnd = CreateWindowExA(template.exStyle, class, caption,
622 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
623 owner, hMenu, hInst, NULL );
624 HeapFree( GetProcessHeap(), 0, class_tmp );
625 HeapFree( GetProcessHeap(), 0, caption_tmp );
630 if (hUserFont) DeleteObject( hUserFont );
631 if (hMenu) DestroyMenu( hMenu );
632 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
636 /* moved this from the top of the method to here as DIALOGINFO structure
637 will be valid only after WM_CREATE message has been handled in DefDlgProc
638 All the members of the structure get filled here using temp variables */
639 dlgInfo = DIALOG_get_info( hwnd, TRUE );
640 dlgInfo->hwndFocus = 0;
641 dlgInfo->hUserFont = hUserFont;
642 dlgInfo->hMenu = hMenu;
643 dlgInfo->xBaseUnit = xBaseUnit;
644 dlgInfo->yBaseUnit = yBaseUnit;
645 dlgInfo->idResult = 0;
646 dlgInfo->flags = flags;
647 dlgInfo->hDialogHeap = 0;
649 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
651 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
652 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
654 if (dlgInfo->hUserFont)
655 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
657 /* Create controls */
659 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
661 /* Send initialisation messages and set focus */
663 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
664 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
666 /* By returning TRUE, app has requested a default focus assignment */
667 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
668 if( dlgInfo->hwndFocus )
669 SetFocus( dlgInfo->hwndFocus );
672 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
674 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
678 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
679 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
684 /***********************************************************************
685 * CreateDialogParamA (USER32.@)
687 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
688 DLGPROC dlgProc, LPARAM param )
693 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
694 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
695 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
699 /***********************************************************************
700 * CreateDialogParamW (USER32.@)
702 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
703 DLGPROC dlgProc, LPARAM param )
708 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
709 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
710 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
714 /***********************************************************************
715 * CreateDialogIndirectParamAorW (USER32.@)
717 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
718 HWND owner, DLGPROC dlgProc, LPARAM param,
721 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
724 /***********************************************************************
725 * CreateDialogIndirectParamA (USER32.@)
727 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
728 HWND owner, DLGPROC dlgProc, LPARAM param )
730 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
733 /***********************************************************************
734 * CreateDialogIndirectParamW (USER32.@)
736 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
737 HWND owner, DLGPROC dlgProc, LPARAM param )
739 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
743 /***********************************************************************
746 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
748 DIALOGINFO * dlgInfo;
751 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
754 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
757 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
761 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
765 /* ShowWindow the first time the queue goes empty */
766 ShowWindow( hwnd, SW_SHOWNORMAL );
769 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
771 /* No message present -> send ENTERIDLE and wait */
772 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
774 if (!GetMessageW( &msg, 0, 0, 0 )) break;
777 if (!IsWindow( hwnd )) return -1;
778 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
780 TranslateMessage( &msg );
781 DispatchMessageW( &msg );
783 if (dlgInfo->flags & DF_END) break;
786 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
787 retval = dlgInfo->idResult;
788 DestroyWindow( hwnd );
793 /***********************************************************************
794 * DialogBoxParamA (USER32.@)
796 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
797 HWND owner, DLGPROC dlgProc, LPARAM param )
803 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
804 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
805 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
806 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
811 /***********************************************************************
812 * DialogBoxParamW (USER32.@)
814 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
815 HWND owner, DLGPROC dlgProc, LPARAM param )
821 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
822 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
823 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
824 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
829 /***********************************************************************
830 * DialogBoxIndirectParamAorW (USER32.@)
832 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
833 HWND owner, DLGPROC dlgProc,
834 LPARAM param, DWORD flags )
836 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
837 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
841 /***********************************************************************
842 * DialogBoxIndirectParamA (USER32.@)
844 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
845 HWND owner, DLGPROC dlgProc, LPARAM param )
847 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
851 /***********************************************************************
852 * DialogBoxIndirectParamW (USER32.@)
854 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
855 HWND owner, DLGPROC dlgProc, LPARAM param )
857 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
860 /***********************************************************************
861 * EndDialog (USER32.@)
863 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
865 BOOL wasEnabled = TRUE;
866 DIALOGINFO * dlgInfo;
869 TRACE("%p %d\n", hwnd, retval );
871 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
873 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
876 dlgInfo->idResult = retval;
877 dlgInfo->flags |= DF_END;
878 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
880 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
881 DIALOG_EnableOwner( owner );
883 /* Windows sets the focus to the dialog itself in EndDialog */
885 if (IsChild(hwnd, GetFocus()))
888 /* Don't have to send a ShowWindow(SW_HIDE), just do
889 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
891 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
892 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
894 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
896 /* unblock dialog loop */
897 PostMessageA(hwnd, WM_NULL, 0, 0);
902 /***********************************************************************
903 * DIALOG_IsAccelerator
905 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
907 HWND hwndControl = hwnd;
914 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
915 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
917 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
918 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
919 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
921 /* find the accelerator key */
922 LPWSTR p = buffer - 2;
926 p = strchrW( p + 2, '&' );
928 while (p != NULL && p[1] == '&');
930 /* and check if it's the one we're looking for */
931 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
933 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
935 /* set focus to the control */
936 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
937 /* and bump it on to next */
938 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
940 else if (dlgCode & DLGC_BUTTON)
942 /* send BM_CLICK message to the control */
943 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
948 hwndNext = GetWindow( hwndControl, GW_CHILD );
952 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
954 while (!hwndNext && hwndControl)
956 hwndControl = GetParent( hwndControl );
957 if (hwndControl == hwndDlg)
959 if(hwnd==hwndDlg) /* prevent endless loop */
964 hwndNext = GetWindow( hwndDlg, GW_CHILD );
967 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
969 hwndControl = hwndNext;
971 while (hwndControl && (hwndControl != hwnd));
976 /***********************************************************************
977 * DIALOG_FindMsgDestination
979 * The messages that IsDialogMessage sends may not go to the dialog
980 * calling IsDialogMessage if that dialog is a child, and it has the
981 * DS_CONTROL style set.
982 * We propagate up until we hit one that does not have DS_CONTROL, or
983 * whose parent is not a dialog.
985 * This is undocumented behaviour.
987 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
989 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
992 HWND hParent = GetParent(hwndDlg);
995 pParent = WIN_GetPtr(hParent);
996 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
998 if (!(pParent->flags & WIN_ISDIALOG))
1000 WIN_ReleasePtr(pParent);
1003 WIN_ReleasePtr(pParent);
1011 /***********************************************************************
1012 * DIALOG_FixOneChildOnChangeFocus
1014 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1017 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1020 /* If a default pushbutton then no longer default */
1021 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1022 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1026 /***********************************************************************
1027 * DIALOG_FixChildrenOnChangeFocus
1029 * Following the change of focus that occurs for example after handling
1030 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1031 * children may be required.
1033 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1035 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1036 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1037 /* Windows does ask for this. I don't know why yet */
1039 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1041 /* If the button that is getting the focus WAS flagged as the default
1042 * pushbutton then ask the dialog what it thinks the default is and
1043 * set that in the default style.
1045 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1047 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1048 if (HIWORD(def_id) == DC_HASDEFID)
1051 def_id = LOWORD(def_id);
1052 hwndDef = GetDlgItem (hwndDlg, def_id);
1055 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1056 /* I know that if it is a button then it should already be a
1057 * UNDEFPUSHBUTTON, since we have just told the buttons to
1058 * change style. But maybe they ignored our request
1060 if ((dlgcode_def & DLGC_BUTTON) &&
1061 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1063 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1068 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1070 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1071 /* I wonder why it doesn't send a DM_SETDEFID */
1075 /***********************************************************************
1076 * IsDialogMessageW (USER32.@)
1078 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1082 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1084 hwndDlg = WIN_GetFullHandle( hwndDlg );
1085 if (hwndDlg == GetDesktopWindow()) return FALSE;
1086 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1088 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1090 switch(msg->message)
1093 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1094 if (dlgCode & DLGC_WANTMESSAGE) break;
1099 if (!(dlgCode & DLGC_WANTTAB))
1101 BOOL fIsDialog = TRUE;
1102 WND *pWnd = WIN_GetPtr( hwndDlg );
1104 if (pWnd && pWnd != WND_OTHER_PROCESS)
1106 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1107 WIN_ReleasePtr(pWnd);
1110 /* I am not sure under which circumstances the TAB is handled
1111 * each way. All I do know is that it does not always simply
1112 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1113 * do so but I presume someone has)
1116 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1119 /* It would appear that GetNextDlgTabItem can handle being
1120 * passed hwndDlg rather than NULL but that is undocumented
1121 * so let's do it properly
1123 HWND hwndFocus = GetFocus();
1124 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1125 hwndFocus == hwndDlg ? NULL : hwndFocus,
1126 GetKeyState (VK_SHIFT) & 0x8000);
1129 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1130 msg->wParam, (LPARAM)msg);
1131 if (dlgCode & DLGC_HASSETSEL)
1133 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1134 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1138 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1139 length = strlenW (buffer);
1140 HeapFree (GetProcessHeap(), 0, buffer);
1141 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1144 SetFocus (hwndNext);
1145 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1158 if (!(dlgCode & DLGC_WANTARROWS))
1160 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1161 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1162 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1169 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1176 if ((GetFocus() == msg->hwnd) &&
1177 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1179 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1181 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1183 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1184 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1188 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1197 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1198 * It does NOT get sent in the test program I have
1200 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1201 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1202 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1206 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1208 /* don't translate or dispatch */
1214 TranslateMessage( msg );
1215 DispatchMessageW( msg );
1220 /***********************************************************************
1221 * GetDlgCtrlID (USER32.@)
1223 INT WINAPI GetDlgCtrlID( HWND hwnd )
1225 return GetWindowLongPtrW( hwnd, GWLP_ID );
1229 /***********************************************************************
1230 * GetDlgItem (USER32.@)
1232 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1235 HWND *list = WIN_ListChildren( hwndDlg );
1238 if (!list) return 0;
1240 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1242 HeapFree( GetProcessHeap(), 0, list );
1247 /*******************************************************************
1248 * SendDlgItemMessageA (USER32.@)
1250 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1251 WPARAM wParam, LPARAM lParam )
1253 HWND hwndCtrl = GetDlgItem( hwnd, id );
1254 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1259 /*******************************************************************
1260 * SendDlgItemMessageW (USER32.@)
1262 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1263 WPARAM wParam, LPARAM lParam )
1265 HWND hwndCtrl = GetDlgItem( hwnd, id );
1266 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1271 /*******************************************************************
1272 * SetDlgItemTextA (USER32.@)
1274 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1276 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1280 /*******************************************************************
1281 * SetDlgItemTextW (USER32.@)
1283 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1285 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1289 /***********************************************************************
1290 * GetDlgItemTextA (USER32.@)
1292 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1294 if (str && (len > 0)) str[0] = '\0';
1295 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1300 /***********************************************************************
1301 * GetDlgItemTextW (USER32.@)
1303 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1305 if (str && (len > 0)) str[0] = '\0';
1306 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1311 /*******************************************************************
1312 * SetDlgItemInt (USER32.@)
1314 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1319 if (fSigned) sprintf( str, "%d", (INT)value );
1320 else sprintf( str, "%u", value );
1321 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1326 /***********************************************************************
1327 * GetDlgItemInt (USER32.@)
1329 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1336 if (translated) *translated = FALSE;
1337 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1341 result = strtol( str, &endptr, 10 );
1342 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1344 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1349 result = strtoul( str, &endptr, 10 );
1350 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1352 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1354 if (translated) *translated = TRUE;
1355 return (UINT)result;
1359 /***********************************************************************
1360 * CheckDlgButton (USER32.@)
1362 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1364 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1369 /***********************************************************************
1370 * IsDlgButtonChecked (USER32.@)
1372 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1374 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1378 /***********************************************************************
1381 * Callback function used to check/uncheck radio buttons that fall
1382 * within a specific range of IDs.
1384 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1386 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1387 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1389 if ((lChildID >= lpRadioGroup->firstID) &&
1390 (lChildID <= lpRadioGroup->lastID))
1392 if (lChildID == lpRadioGroup->checkID)
1394 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1398 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1406 /***********************************************************************
1407 * CheckRadioButton (USER32.@)
1409 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1410 int lastID, int checkID )
1412 RADIOGROUP radioGroup;
1414 radioGroup.firstID = firstID;
1415 radioGroup.lastID = lastID;
1416 radioGroup.checkID = checkID;
1418 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1419 (LPARAM)&radioGroup);
1423 /***********************************************************************
1424 * GetDialogBaseUnits (USER.243)
1425 * GetDialogBaseUnits (USER32.@)
1427 DWORD WINAPI GetDialogBaseUnits(void)
1436 if ((hdc = GetDC(0)))
1438 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1439 if (size.cx) units = MAKELONG( size.cx, size.cy );
1440 ReleaseDC( 0, hdc );
1442 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1448 /***********************************************************************
1449 * MapDialogRect (USER32.@)
1451 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1453 DIALOGINFO * dlgInfo;
1454 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1455 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1456 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1457 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1458 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1463 /***********************************************************************
1464 * GetNextDlgGroupItem (USER32.@)
1466 * Corrections to MSDN documentation
1468 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1469 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1470 * 2. Prev of NULL or hwndDlg fails
1472 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1474 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1476 BOOL fSkipping=FALSE;
1478 hwndDlg = WIN_GetFullHandle( hwndDlg );
1479 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1481 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1482 if (!hwndCtrl && fPrevious) return 0;
1486 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1490 /* No ctrl specified -> start from the beginning */
1491 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1492 /* MSDN is wrong. fPrevious does not result in the last child */
1494 /* Maybe that first one is valid. If so then we don't want to skip it*/
1495 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1501 /* Always go forward around the group and list of controls; for the
1502 * previous control keep track; for the next break when you find one
1504 retvalue = hwndCtrl;
1506 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1511 /* Climb out until there is a next sibling of the ancestor or we
1512 * reach the top (in which case we loop back to the start)
1514 if (hwndDlg == GetParent (hwnd))
1516 /* Wrap around to the beginning of the list, within the same
1517 * group. (Once only)
1519 if (fLooped) goto end;
1521 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1525 hwnd = GetParent (hwnd);
1526 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1531 /* Wander down the leading edge of controlparents */
1532 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1533 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1534 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1536 /* Question. If the control is a control parent but either has no
1537 * children or is not visible/enabled then if it has a WS_GROUP does
1538 * it count? For that matter does it count anyway?
1539 * I believe it doesn't count.
1542 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1544 hwndLastGroup = hwnd;
1547 /* Look for the beginning of the group */
1552 if (hwnd == hwndCtrl)
1554 if (!fSkipping) break;
1555 if (hwndLastGroup == hwnd) break;
1556 hwnd = hwndLastGroup;
1562 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1566 if (!fPrevious) break;
1574 /***********************************************************************
1575 * DIALOG_GetNextTabItem
1577 * Recursive helper for GetNextDlgTabItem
1579 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1583 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1585 HWND hChildFirst = 0;
1589 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1590 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1592 else if (IsChild( hwndMain, hwndCtrl ))
1594 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1597 if(GetParent(hwndCtrl) != hwndMain)
1598 /* i.e. if we are not at the top level of the recursion */
1599 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1601 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1607 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1608 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1609 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1612 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1613 if (retWnd) return (retWnd);
1615 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1617 return (hChildFirst);
1619 hChildFirst = GetWindow(hChildFirst,wndSearch);
1623 HWND hParent = GetParent(hwndCtrl);
1626 if(hParent == hwndMain) break;
1627 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1629 hParent = GetParent(hParent);
1632 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1634 return retWnd ? retWnd : hwndCtrl;
1637 /***********************************************************************
1638 * GetNextDlgTabItem (USER32.@)
1640 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1643 hwndDlg = WIN_GetFullHandle( hwndDlg );
1644 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1646 /* Undocumented but tested under Win2000 and WinME */
1647 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1649 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1650 * NB GetLastError returns whatever was set before the function was
1653 if (!hwndCtrl && fPrevious) return 0;
1655 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1658 /**********************************************************************
1659 * DIALOG_DlgDirSelect
1661 * Helper function for DlgDirSelect*
1663 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1664 INT id, BOOL unicode, BOOL combo )
1666 WCHAR *buffer, *ptr;
1669 HWND listbox = GetDlgItem( hwnd, id );
1671 TRACE("%p '%s' %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1672 if (!listbox) return FALSE;
1674 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1675 if (item == LB_ERR) return FALSE;
1677 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1678 if (size == LB_ERR) return FALSE;
1680 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+1) * sizeof(WCHAR) ))) return FALSE;
1682 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1684 if ((ret = (buffer[0] == '['))) /* drive or directory */
1686 if (buffer[1] == '-') /* drive */
1694 buffer[strlenW(buffer)-1] = '\\';
1702 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1703 ((LPSTR)str)[len-1] = 0;
1705 else lstrcpynW( str, ptr, len );
1706 HeapFree( GetProcessHeap(), 0, buffer );
1707 TRACE("Returning %d '%s'\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1712 /**********************************************************************
1713 * DIALOG_DlgDirListW
1715 * Helper function for DlgDirList*W
1717 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1718 INT idStatic, UINT attrib, BOOL combo )
1721 LPWSTR orig_spec = spec;
1722 WCHAR any[] = {'*','.','*',0};
1724 #define SENDMSG(msg,wparam,lparam) \
1725 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1726 : SendMessageW( hwnd, msg, wparam, lparam ))
1728 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1730 /* If the path exists and is a directory, chdir to it */
1731 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1736 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1737 if ((p2 = strrchrW( p, '/' ))) p = p2;
1742 if (!SetCurrentDirectoryW( spec ))
1744 *p = sep; /* Restore the original spec */
1751 TRACE( "mask=%s\n", debugstr_w(spec) );
1753 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1755 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1756 if (attrib & DDL_DIRECTORY)
1758 if (!(attrib & DDL_EXCLUSIVE))
1760 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1761 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1762 (LPARAM)spec ) == LB_ERR)
1765 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1766 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1767 (LPARAM)any ) == LB_ERR)
1772 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1773 (LPARAM)spec ) == LB_ERR)
1778 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1780 WCHAR temp[MAX_PATH];
1781 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1783 /* Can't use PostMessage() here, because the string is on the stack */
1784 SetDlgItemTextW( hDlg, idStatic, temp );
1787 if (orig_spec && (spec != orig_spec))
1789 /* Update the original file spec */
1791 while ((*orig_spec++ = *p++));
1799 /**********************************************************************
1800 * DIALOG_DlgDirListA
1802 * Helper function for DlgDirList*A
1804 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1805 INT idStatic, UINT attrib, BOOL combo )
1809 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1810 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1811 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1812 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1813 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1814 HeapFree( GetProcessHeap(), 0, specW );
1817 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1821 /**********************************************************************
1822 * DlgDirSelectExA (USER32.@)
1824 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1826 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1830 /**********************************************************************
1831 * DlgDirSelectExW (USER32.@)
1833 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1835 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1839 /**********************************************************************
1840 * DlgDirSelectComboBoxExA (USER32.@)
1842 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1845 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1849 /**********************************************************************
1850 * DlgDirSelectComboBoxExW (USER32.@)
1852 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1855 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1859 /**********************************************************************
1860 * DlgDirListA (USER32.@)
1862 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1863 INT idStatic, UINT attrib )
1865 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1869 /**********************************************************************
1870 * DlgDirListW (USER32.@)
1872 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1873 INT idStatic, UINT attrib )
1875 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1879 /**********************************************************************
1880 * DlgDirListComboBoxA (USER32.@)
1882 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1883 INT idStatic, UINT attrib )
1885 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1889 /**********************************************************************
1890 * DlgDirListComboBoxW (USER32.@)
1892 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1893 INT idStatic, UINT attrib )
1895 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );