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/unicode.h"
40 #include "user_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
46 /* Dialog control information */
83 /* Radio button group */
92 /*********************************************************************
93 * dialog class descriptor
95 const struct builtin_class_descr DIALOG_builtin_class =
97 (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
98 CS_SAVEBITS | CS_DBLCLKS, /* style */
99 WINPROC_DIALOG, /* proc */
100 DLGWINDOWEXTRA, /* extra */
101 IDC_ARROW, /* cursor */
106 /***********************************************************************
109 * Helper function for modal dialogs to enable again the
110 * owner of the dialog box.
112 static void DIALOG_EnableOwner( HWND hOwner )
114 /* Owner must be a top-level window */
116 hOwner = GetAncestor( hOwner, GA_ROOT );
118 EnableWindow( hOwner, TRUE );
122 /***********************************************************************
123 * DIALOG_DisableOwner
125 * Helper function for modal dialogs to disable the
126 * owner of the dialog box. Returns TRUE if owner was enabled.
128 static BOOL DIALOG_DisableOwner( HWND hOwner )
130 /* Owner must be a top-level window */
132 hOwner = GetAncestor( hOwner, GA_ROOT );
133 if (!hOwner) return FALSE;
134 if (IsWindowEnabled( hOwner ))
136 EnableWindow( hOwner, FALSE );
143 /***********************************************************************
144 * DIALOG_GetControl32
146 * Return the class and text of the control pointed to by ptr,
147 * fill the header structure and return a pointer to the next control.
149 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
154 info->helpId = GET_DWORD(p); p += 2;
155 info->exStyle = GET_DWORD(p); p += 2;
156 info->style = GET_DWORD(p); p += 2;
161 info->style = GET_DWORD(p); p += 2;
162 info->exStyle = GET_DWORD(p); p += 2;
164 info->x = GET_WORD(p); p++;
165 info->y = GET_WORD(p); p++;
166 info->cx = GET_WORD(p); p++;
167 info->cy = GET_WORD(p); p++;
171 /* id is a DWORD for DIALOGEX */
172 info->id = GET_DWORD(p);
177 info->id = GET_WORD(p);
181 if (GET_WORD(p) == 0xffff)
183 static const WCHAR class_names[6][10] =
185 { 'B','u','t','t','o','n', }, /* 0x80 */
186 { 'E','d','i','t', }, /* 0x81 */
187 { 'S','t','a','t','i','c', }, /* 0x82 */
188 { 'L','i','s','t','B','o','x', }, /* 0x83 */
189 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
190 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
192 WORD id = GET_WORD(p+1);
193 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
194 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
196 info->className = class_names[id];
199 info->className = NULL;
200 ERR("Unknown built-in class id %04x\n", id );
207 p += strlenW( info->className ) + 1;
210 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
212 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
217 info->windowName = p;
218 p += strlenW( info->windowName ) + 1;
221 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
222 debugstr_w( info->className ), debugstr_w( info->windowName ),
223 info->id, info->x, info->y, info->cx, info->cy,
224 info->style, info->exStyle, info->helpId );
228 if (TRACE_ON(dialog))
230 WORD i, count = GET_WORD(p) / sizeof(WORD);
233 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
238 p += GET_WORD(p) / sizeof(WORD);
240 else info->data = NULL;
243 /* Next control is on dword boundary */
244 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
248 /***********************************************************************
249 * DIALOG_CreateControls32
251 * Create the control windows for a dialog.
253 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
254 HINSTANCE hInst, BOOL unicode )
256 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
257 DLG_CONTROL_INFO info;
258 HWND hwndCtrl, hwndDefButton = 0;
259 INT items = dlgTemplate->nbItems;
264 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
265 dlgTemplate->dialogEx );
267 if (info.style & WS_BORDER)
269 info.style &= ~WS_BORDER;
270 info.exStyle |= WS_EX_CLIENTEDGE;
274 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
275 info.className, info.windowName,
276 info.style | WS_CHILD,
277 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
278 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
279 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
281 hwnd, (HMENU)info.id,
282 hInst, (LPVOID)info.data );
286 LPCSTR class = (LPCSTR)info.className;
287 LPCSTR caption = (LPCSTR)info.windowName;
288 LPSTR class_tmp = NULL;
289 LPSTR caption_tmp = NULL;
291 if (!IS_INTRESOURCE(class))
293 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
294 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
295 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
298 if (!IS_INTRESOURCE(caption))
300 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
301 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
302 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
303 caption = caption_tmp;
305 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
306 class, caption, info.style | WS_CHILD,
307 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
308 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
309 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
310 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
311 hwnd, (HMENU)info.id,
312 hInst, (LPVOID)info.data );
313 HeapFree( GetProcessHeap(), 0, class_tmp );
314 HeapFree( GetProcessHeap(), 0, caption_tmp );
318 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
322 /* Send initialisation messages to the control */
323 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
324 (WPARAM)dlgInfo->hUserFont, 0 );
325 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
327 /* If there's already a default push-button, set it back */
328 /* to normal and use this one instead. */
330 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
331 hwndDefButton = hwndCtrl;
332 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
340 /***********************************************************************
341 * DIALOG_ParseTemplate32
343 * Fill a DLG_TEMPLATE structure from the dialog template, and return
344 * a pointer to the first control.
346 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
348 const WORD *p = (const WORD *)template;
352 dlgver = GET_WORD(p); p++;
353 signature = GET_WORD(p); p++;
355 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
357 result->dialogEx = TRUE;
358 result->helpId = GET_DWORD(p); p += 2;
359 result->exStyle = GET_DWORD(p); p += 2;
360 result->style = GET_DWORD(p); p += 2;
364 result->style = GET_DWORD(p - 2);
365 result->dialogEx = FALSE;
367 result->exStyle = GET_DWORD(p); p += 2;
369 result->nbItems = GET_WORD(p); p++;
370 result->x = GET_WORD(p); p++;
371 result->y = GET_WORD(p); p++;
372 result->cx = GET_WORD(p); p++;
373 result->cy = GET_WORD(p); p++;
374 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
375 result->dialogEx ? "EX" : "", result->x, result->y,
376 result->cx, result->cy, result->helpId );
377 TRACE(" STYLE 0x%08x\n", result->style );
378 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
380 /* Get the menu name */
385 result->menuName = NULL;
389 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
391 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
394 result->menuName = p;
395 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
396 p += strlenW( result->menuName ) + 1;
400 /* Get the class name */
405 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
409 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
411 TRACE(" CLASS %04x\n", LOWORD(result->className) );
414 result->className = p;
415 TRACE(" CLASS %s\n", debugstr_w( result->className ));
416 p += strlenW( result->className ) + 1;
420 /* Get the window caption */
423 p += strlenW( result->caption ) + 1;
424 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
426 /* Get the font name */
428 result->pointSize = 0;
429 result->faceName = NULL;
430 result->weight = FW_DONTCARE;
431 result->italic = FALSE;
433 if (result->style & DS_SETFONT)
435 result->pointSize = GET_WORD(p);
438 /* If pointSize is 0x7fff, it means that we need to use the font
439 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
440 * italic, and facename from the dialog template.
442 if (result->pointSize == 0x7fff)
444 /* We could call SystemParametersInfo here, but then we'd have
445 * to convert from pixel size to point size (which can be
448 TRACE(" FONT: Using message box font\n");
452 if (result->dialogEx)
454 result->weight = GET_WORD(p); p++;
455 result->italic = LOBYTE(GET_WORD(p)); p++;
457 result->faceName = p;
458 p += strlenW( result->faceName ) + 1;
460 TRACE(" FONT %d, %s, %d, %s\n",
461 result->pointSize, debugstr_w( result->faceName ),
462 result->weight, result->italic ? "TRUE" : "FALSE" );
466 /* First control is on dword boundary */
467 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
471 /***********************************************************************
472 * DIALOG_CreateIndirect
473 * Creates a dialog box window
475 * modal = TRUE if we are called from a modal dialog box.
476 * (it's more compatible to do it here, as under Windows the owner
477 * is never disabled if the dialog fails because of an invalid template)
479 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
480 HWND owner, DLGPROC dlgProc, LPARAM param,
481 BOOL unicode, BOOL modal )
487 DLG_TEMPLATE template;
488 DIALOGINFO * dlgInfo = NULL;
489 DWORD units = GetDialogBaseUnits();
490 BOOL ownerEnabled = TRUE;
494 UINT xBaseUnit = LOWORD(units);
495 UINT yBaseUnit = HIWORD(units);
497 /* Parse dialog template */
499 if (!dlgTemplate) return 0;
500 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
504 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
506 /* Create custom font if needed */
508 if (template.style & DS_SETFONT)
512 if (template.pointSize == 0x7fff)
514 /* We get the message font from the non-client metrics */
515 NONCLIENTMETRICSW ncMetrics;
517 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
518 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
519 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
521 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
526 /* We convert the size to pixels and then make it -ve. This works
527 * for both +ve and -ve template.pointSize */
528 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
529 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
530 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
531 PROOF_QUALITY, FF_DONTCARE,
538 HFONT hOldFont = SelectObject( dc, hUserFont );
539 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
542 xBaseUnit = charSize.cx;
543 yBaseUnit = charSize.cy;
545 SelectObject( dc, hOldFont );
548 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
551 /* Create dialog main window */
553 rect.left = rect.top = 0;
554 rect.right = MulDiv(template.cx, xBaseUnit, 4);
555 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
556 if (template.style & WS_CHILD)
557 template.style &= ~(WS_CAPTION|WS_SYSMENU);
558 if (template.style & DS_MODALFRAME)
559 template.exStyle |= WS_EX_DLGMODALFRAME;
560 if (template.style & DS_CONTROL)
561 template.exStyle |= WS_EX_CONTROLPARENT;
562 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
565 size.cx = rect.right - rect.left;
566 size.cy = rect.bottom - rect.top;
568 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
570 pos.x = pos.y = CW_USEDEFAULT;
574 HMONITOR monitor = 0;
575 MONITORINFO mon_info;
577 mon_info.cbSize = sizeof(mon_info);
578 if (template.style & DS_CENTER)
580 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
581 GetMonitorInfoW( monitor, &mon_info );
582 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
583 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
585 else if (template.style & DS_CENTERMOUSE)
587 GetCursorPos( &pos );
588 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
589 GetMonitorInfoW( monitor, &mon_info );
593 pos.x += MulDiv(template.x, xBaseUnit, 4);
594 pos.y += MulDiv(template.y, yBaseUnit, 8);
595 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
597 if ( !(template.style & WS_CHILD) )
601 /* try to fit it into the desktop */
605 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
606 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
607 GetMonitorInfoW( monitor, &mon_info );
609 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
611 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
613 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
614 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
620 ownerEnabled = DIALOG_DisableOwner( owner );
621 if (ownerEnabled) flags |= DF_OWNERENABLED;
626 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
627 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
628 owner, hMenu, hInst, NULL );
632 LPCSTR class = (LPCSTR)template.className;
633 LPCSTR caption = (LPCSTR)template.caption;
634 LPSTR class_tmp = NULL;
635 LPSTR caption_tmp = NULL;
637 if (!IS_INTRESOURCE(class))
639 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
640 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
641 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
644 if (!IS_INTRESOURCE(caption))
646 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
647 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
648 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
649 caption = caption_tmp;
651 hwnd = CreateWindowExA(template.exStyle, class, caption,
652 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
653 owner, hMenu, hInst, NULL );
654 HeapFree( GetProcessHeap(), 0, class_tmp );
655 HeapFree( GetProcessHeap(), 0, caption_tmp );
660 if (hUserFont) DeleteObject( hUserFont );
661 if (hMenu) DestroyMenu( hMenu );
662 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
666 /* moved this from the top of the method to here as DIALOGINFO structure
667 will be valid only after WM_CREATE message has been handled in DefDlgProc
668 All the members of the structure get filled here using temp variables */
669 dlgInfo = DIALOG_get_info( hwnd, TRUE );
670 dlgInfo->hwndFocus = 0;
671 dlgInfo->hUserFont = hUserFont;
672 dlgInfo->hMenu = hMenu;
673 dlgInfo->xBaseUnit = xBaseUnit;
674 dlgInfo->yBaseUnit = yBaseUnit;
675 dlgInfo->flags = flags;
677 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
679 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
680 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
682 if (dlgProc && dlgInfo->hUserFont)
683 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
685 /* Create controls */
687 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
689 /* Send initialisation messages and set focus */
693 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
694 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) &&
695 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
697 /* By returning TRUE, app has requested a default focus assignment.
698 * WM_INITDIALOG may have changed the tab order, so find the first
699 * tabstop control again. */
700 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
701 if( dlgInfo->hwndFocus )
702 SetFocus( dlgInfo->hwndFocus );
706 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
708 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
712 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
713 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
718 /***********************************************************************
719 * CreateDialogParamA (USER32.@)
721 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
722 DLGPROC dlgProc, LPARAM param )
727 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
728 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
729 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
733 /***********************************************************************
734 * CreateDialogParamW (USER32.@)
736 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
737 DLGPROC dlgProc, LPARAM param )
742 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
743 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
744 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
748 /***********************************************************************
749 * CreateDialogIndirectParamAorW (USER32.@)
751 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
752 HWND owner, DLGPROC dlgProc, LPARAM param,
755 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
758 /***********************************************************************
759 * CreateDialogIndirectParamA (USER32.@)
761 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
762 HWND owner, DLGPROC dlgProc, LPARAM param )
764 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
767 /***********************************************************************
768 * CreateDialogIndirectParamW (USER32.@)
770 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
771 HWND owner, DLGPROC dlgProc, LPARAM param )
773 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
777 /***********************************************************************
780 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
782 DIALOGINFO * dlgInfo;
785 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
788 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
791 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
795 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
799 /* ShowWindow the first time the queue goes empty */
800 ShowWindow( hwnd, SW_SHOWNORMAL );
803 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
805 /* No message present -> send ENTERIDLE and wait */
806 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
808 GetMessageW( &msg, 0, 0, 0 );
811 if (msg.message == WM_QUIT)
813 PostQuitMessage( msg.wParam );
814 dlgInfo->flags |= DF_END;
817 if (!IsWindow( hwnd )) return 0;
818 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
820 TranslateMessage( &msg );
821 DispatchMessageW( &msg );
823 if (dlgInfo->flags & DF_END) break;
825 if (bFirstEmpty && msg.message == WM_TIMER)
827 ShowWindow( hwnd, SW_SHOWNORMAL );
832 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
833 retval = dlgInfo->idResult;
834 DestroyWindow( hwnd );
839 /***********************************************************************
840 * DialogBoxParamA (USER32.@)
842 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
843 HWND owner, DLGPROC dlgProc, LPARAM param )
849 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
850 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
851 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
852 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
857 /***********************************************************************
858 * DialogBoxParamW (USER32.@)
860 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
861 HWND owner, DLGPROC dlgProc, LPARAM param )
867 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
868 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
869 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
870 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
875 /***********************************************************************
876 * DialogBoxIndirectParamAorW (USER32.@)
878 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
879 HWND owner, DLGPROC dlgProc,
880 LPARAM param, DWORD flags )
882 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
883 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
887 /***********************************************************************
888 * DialogBoxIndirectParamA (USER32.@)
890 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
891 HWND owner, DLGPROC dlgProc, LPARAM param )
893 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
897 /***********************************************************************
898 * DialogBoxIndirectParamW (USER32.@)
900 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
901 HWND owner, DLGPROC dlgProc, LPARAM param )
903 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
906 /***********************************************************************
907 * EndDialog (USER32.@)
909 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
911 BOOL wasEnabled = TRUE;
912 DIALOGINFO * dlgInfo;
915 TRACE("%p %ld\n", hwnd, retval );
917 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
919 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
922 dlgInfo->idResult = retval;
923 dlgInfo->flags |= DF_END;
924 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
926 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
927 DIALOG_EnableOwner( owner );
929 /* Windows sets the focus to the dialog itself in EndDialog */
931 if (IsChild(hwnd, GetFocus()))
934 /* Don't have to send a ShowWindow(SW_HIDE), just do
935 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
937 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
938 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
940 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
942 /* unblock dialog loop */
943 PostMessageA(hwnd, WM_NULL, 0, 0);
948 /***********************************************************************
949 * DIALOG_IsAccelerator
951 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
953 HWND hwndControl = hwnd;
960 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
961 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
963 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
964 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
965 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
967 /* find the accelerator key */
968 LPWSTR p = buffer - 2;
972 p = strchrW( p + 2, '&' );
974 while (p != NULL && p[1] == '&');
976 /* and check if it's the one we're looking for */
977 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
979 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
981 /* set focus to the control */
982 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
983 /* and bump it on to next */
984 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
986 else if (dlgCode & DLGC_BUTTON)
988 /* send BM_CLICK message to the control */
989 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
994 hwndNext = GetWindow( hwndControl, GW_CHILD );
998 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1000 while (!hwndNext && hwndControl)
1002 hwndControl = GetParent( hwndControl );
1003 if (hwndControl == hwndDlg)
1005 if(hwnd==hwndDlg) /* prevent endless loop */
1010 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1013 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1015 hwndControl = hwndNext;
1017 while (hwndControl && (hwndControl != hwnd));
1022 /***********************************************************************
1023 * DIALOG_FindMsgDestination
1025 * The messages that IsDialogMessage sends may not go to the dialog
1026 * calling IsDialogMessage if that dialog is a child, and it has the
1027 * DS_CONTROL style set.
1028 * We propagate up until we hit one that does not have DS_CONTROL, or
1029 * whose parent is not a dialog.
1031 * This is undocumented behaviour.
1033 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1035 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1038 HWND hParent = GetParent(hwndDlg);
1039 if (!hParent) break;
1041 pParent = WIN_GetPtr(hParent);
1042 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1044 if (!pParent->dlgInfo)
1046 WIN_ReleasePtr(pParent);
1049 WIN_ReleasePtr(pParent);
1057 /***********************************************************************
1058 * DIALOG_FixOneChildOnChangeFocus
1060 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1063 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1066 /* If a default pushbutton then no longer default */
1067 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1068 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1072 /***********************************************************************
1073 * DIALOG_FixChildrenOnChangeFocus
1075 * Following the change of focus that occurs for example after handling
1076 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1077 * children may be required.
1079 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1081 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1082 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1083 /* Windows does ask for this. I don't know why yet */
1085 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1087 /* If the button that is getting the focus WAS flagged as the default
1088 * pushbutton then ask the dialog what it thinks the default is and
1089 * set that in the default style.
1091 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1093 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1094 if (HIWORD(def_id) == DC_HASDEFID)
1097 def_id = LOWORD(def_id);
1098 hwndDef = GetDlgItem (hwndDlg, def_id);
1101 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1102 /* I know that if it is a button then it should already be a
1103 * UNDEFPUSHBUTTON, since we have just told the buttons to
1104 * change style. But maybe they ignored our request
1106 if ((dlgcode_def & DLGC_BUTTON) &&
1107 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1109 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1114 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1116 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1117 /* I wonder why it doesn't send a DM_SETDEFID */
1121 /***********************************************************************
1122 * IsDialogMessageW (USER32.@)
1124 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1128 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1130 hwndDlg = WIN_GetFullHandle( hwndDlg );
1131 if (is_desktop_window(hwndDlg)) return FALSE;
1132 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1134 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1136 switch(msg->message)
1139 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1140 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1145 if (!(dlgCode & DLGC_WANTTAB))
1147 BOOL fIsDialog = TRUE;
1148 WND *pWnd = WIN_GetPtr( hwndDlg );
1150 if (pWnd && pWnd != WND_OTHER_PROCESS)
1152 fIsDialog = (pWnd->dlgInfo != NULL);
1153 WIN_ReleasePtr(pWnd);
1156 /* I am not sure under which circumstances the TAB is handled
1157 * each way. All I do know is that it does not always simply
1158 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1159 * do so but I presume someone has)
1162 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1165 /* It would appear that GetNextDlgTabItem can handle being
1166 * passed hwndDlg rather than NULL but that is undocumented
1167 * so let's do it properly
1169 HWND hwndFocus = GetFocus();
1170 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1171 hwndFocus == hwndDlg ? NULL : hwndFocus,
1172 GetKeyState (VK_SHIFT) & 0x8000);
1175 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1176 msg->wParam, (LPARAM)msg);
1177 if (dlgCode & DLGC_HASSETSEL)
1179 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1180 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1184 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1185 length = strlenW (buffer);
1186 HeapFree (GetProcessHeap(), 0, buffer);
1187 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1190 SetFocus (hwndNext);
1191 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1204 if (!(dlgCode & DLGC_WANTARROWS))
1206 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1207 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1208 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1215 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1222 if ((GetFocus() == msg->hwnd) &&
1223 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1225 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1227 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1229 HWND hwndDef = GetDlgItem(hwndDlg, LOWORD(dw));
1230 if (hwndDef ? IsWindowEnabled(hwndDef) : LOWORD(dw)==IDOK)
1231 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1235 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1244 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1245 * It does NOT get sent in the test program I have
1247 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1248 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1249 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1253 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1255 /* don't translate or dispatch */
1261 TranslateMessage( msg );
1262 DispatchMessageW( msg );
1267 /***********************************************************************
1268 * GetDlgCtrlID (USER32.@)
1270 INT WINAPI GetDlgCtrlID( HWND hwnd )
1272 return GetWindowLongPtrW( hwnd, GWLP_ID );
1276 /***********************************************************************
1277 * GetDlgItem (USER32.@)
1279 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1282 HWND *list = WIN_ListChildren( hwndDlg );
1285 if (!list) return 0;
1287 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1289 HeapFree( GetProcessHeap(), 0, list );
1294 /*******************************************************************
1295 * SendDlgItemMessageA (USER32.@)
1297 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1298 WPARAM wParam, LPARAM lParam )
1300 HWND hwndCtrl = GetDlgItem( hwnd, id );
1301 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1306 /*******************************************************************
1307 * SendDlgItemMessageW (USER32.@)
1309 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1310 WPARAM wParam, LPARAM lParam )
1312 HWND hwndCtrl = GetDlgItem( hwnd, id );
1313 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1318 /*******************************************************************
1319 * SetDlgItemTextA (USER32.@)
1321 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1323 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1327 /*******************************************************************
1328 * SetDlgItemTextW (USER32.@)
1330 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1332 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1336 /***********************************************************************
1337 * GetDlgItemTextA (USER32.@)
1339 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1341 if (str && (len > 0)) str[0] = '\0';
1342 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1347 /***********************************************************************
1348 * GetDlgItemTextW (USER32.@)
1350 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1352 if (str && (len > 0)) str[0] = '\0';
1353 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1358 /*******************************************************************
1359 * SetDlgItemInt (USER32.@)
1361 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1366 if (fSigned) sprintf( str, "%d", (INT)value );
1367 else sprintf( str, "%u", value );
1368 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1373 /***********************************************************************
1374 * GetDlgItemInt (USER32.@)
1376 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1381 LONG_PTR result = 0;
1383 if (translated) *translated = FALSE;
1384 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1388 result = strtol( str, &endptr, 10 );
1389 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1391 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1396 result = strtoul( str, &endptr, 10 );
1397 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1399 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1401 if (translated) *translated = TRUE;
1402 return (UINT)result;
1406 /***********************************************************************
1407 * CheckDlgButton (USER32.@)
1409 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1411 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1416 /***********************************************************************
1417 * IsDlgButtonChecked (USER32.@)
1419 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1421 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1425 /***********************************************************************
1428 * Callback function used to check/uncheck radio buttons that fall
1429 * within a specific range of IDs.
1431 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1433 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1434 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1436 if ((lChildID >= lpRadioGroup->firstID) &&
1437 (lChildID <= lpRadioGroup->lastID))
1439 if (lChildID == lpRadioGroup->checkID)
1441 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1445 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1453 /***********************************************************************
1454 * CheckRadioButton (USER32.@)
1456 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1457 int lastID, int checkID )
1459 RADIOGROUP radioGroup;
1461 radioGroup.firstID = firstID;
1462 radioGroup.lastID = lastID;
1463 radioGroup.checkID = checkID;
1465 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1466 (LPARAM)&radioGroup);
1470 /***********************************************************************
1471 * GetDialogBaseUnits (USER.243)
1472 * GetDialogBaseUnits (USER32.@)
1474 DWORD WINAPI GetDialogBaseUnits(void)
1483 if ((hdc = GetDC(0)))
1485 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1486 if (size.cx) units = MAKELONG( size.cx, size.cy );
1487 ReleaseDC( 0, hdc );
1489 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1495 /***********************************************************************
1496 * MapDialogRect (USER32.@)
1498 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1500 DIALOGINFO * dlgInfo;
1501 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1502 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1503 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1504 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1505 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1510 /***********************************************************************
1511 * GetNextDlgGroupItem (USER32.@)
1513 * Corrections to MSDN documentation
1515 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1516 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1517 * 2. Prev of NULL or hwndDlg fails
1519 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1521 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1523 BOOL fSkipping=FALSE;
1525 hwndDlg = WIN_GetFullHandle( hwndDlg );
1526 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1528 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1529 if (!hwndCtrl && fPrevious) return 0;
1533 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1537 /* No ctrl specified -> start from the beginning */
1538 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1539 /* MSDN is wrong. fPrevious does not result in the last child */
1541 /* Maybe that first one is valid. If so then we don't want to skip it*/
1542 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1548 /* Always go forward around the group and list of controls; for the
1549 * previous control keep track; for the next break when you find one
1551 retvalue = hwndCtrl;
1553 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1558 /* Climb out until there is a next sibling of the ancestor or we
1559 * reach the top (in which case we loop back to the start)
1561 if (hwndDlg == GetParent (hwnd))
1563 /* Wrap around to the beginning of the list, within the same
1564 * group. (Once only)
1566 if (fLooped) goto end;
1568 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1572 hwnd = GetParent (hwnd);
1573 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1578 /* Wander down the leading edge of controlparents */
1579 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1580 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1581 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1583 /* Question. If the control is a control parent but either has no
1584 * children or is not visible/enabled then if it has a WS_GROUP does
1585 * it count? For that matter does it count anyway?
1586 * I believe it doesn't count.
1589 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1591 hwndLastGroup = hwnd;
1594 /* Look for the beginning of the group */
1599 if (hwnd == hwndCtrl)
1601 if (!fSkipping) break;
1602 if (hwndLastGroup == hwnd) break;
1603 hwnd = hwndLastGroup;
1609 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1613 if (!fPrevious) break;
1621 /***********************************************************************
1622 * DIALOG_GetNextTabItem
1624 * Recursive helper for GetNextDlgTabItem
1626 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1630 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1632 HWND hChildFirst = 0;
1636 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1637 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1639 else if (IsChild( hwndMain, hwndCtrl ))
1641 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1644 if(GetParent(hwndCtrl) != hwndMain)
1645 /* i.e. if we are not at the top level of the recursion */
1646 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1648 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1654 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1655 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1656 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1659 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1660 if (retWnd) return (retWnd);
1662 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1664 return (hChildFirst);
1666 hChildFirst = GetWindow(hChildFirst,wndSearch);
1670 HWND hParent = GetParent(hwndCtrl);
1673 if(hParent == hwndMain) break;
1674 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1676 hParent = GetParent(hParent);
1679 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1681 return retWnd ? retWnd : hwndCtrl;
1684 /***********************************************************************
1685 * GetNextDlgTabItem (USER32.@)
1687 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1690 hwndDlg = WIN_GetFullHandle( hwndDlg );
1691 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1693 /* Undocumented but tested under Win2000 and WinME */
1694 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1696 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1697 * NB GetLastError returns whatever was set before the function was
1700 if (!hwndCtrl && fPrevious) return 0;
1702 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1705 /**********************************************************************
1706 * DIALOG_DlgDirSelect
1708 * Helper function for DlgDirSelect*
1710 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1711 INT id, BOOL unicode, BOOL combo )
1713 WCHAR *buffer, *ptr;
1716 HWND listbox = GetDlgItem( hwnd, id );
1718 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1719 if (!listbox) return FALSE;
1721 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1722 if (item == LB_ERR) return FALSE;
1724 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1725 if (size == LB_ERR) return FALSE;
1727 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1729 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1731 if ((ret = (buffer[0] == '['))) /* drive or directory */
1733 if (buffer[1] == '-') /* drive */
1741 buffer[strlenW(buffer)-1] = '\\';
1747 /* Filenames without a dot extension must have one tacked at the end */
1748 if (strchrW(buffer, '.') == NULL)
1750 buffer[strlenW(buffer)+1] = '\0';
1751 buffer[strlenW(buffer)] = '.';
1758 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1759 ((LPSTR)str)[len-1] = 0;
1761 else lstrcpynW( str, ptr, len );
1762 HeapFree( GetProcessHeap(), 0, buffer );
1763 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1768 /**********************************************************************
1769 * DIALOG_DlgDirListW
1771 * Helper function for DlgDirList*W
1773 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1774 INT idStatic, UINT attrib, BOOL combo )
1777 LPWSTR orig_spec = spec;
1778 WCHAR any[] = {'*','.','*',0};
1780 #define SENDMSG(msg,wparam,lparam) \
1781 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1782 : SendMessageW( hwnd, msg, wparam, lparam ))
1784 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1786 /* If the path exists and is a directory, chdir to it */
1787 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1792 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1793 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1794 if ((p2 = strrchrW( p, '/' ))) p = p2;
1799 if (!SetCurrentDirectoryW( spec ))
1801 *p = sep; /* Restore the original spec */
1808 TRACE( "mask=%s\n", debugstr_w(spec) );
1810 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1812 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1814 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1815 if (attrib & DDL_DIRECTORY)
1817 if (!(attrib & DDL_EXCLUSIVE))
1819 SENDMSG( combo ? CB_DIR : LB_DIR,
1820 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1823 SENDMSG( combo ? CB_DIR : LB_DIR,
1824 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1829 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1833 /* Convert path specification to uppercase */
1834 if (spec) CharUpperW(spec);
1836 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1838 WCHAR temp[MAX_PATH];
1839 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1841 /* Can't use PostMessage() here, because the string is on the stack */
1842 SetDlgItemTextW( hDlg, idStatic, temp );
1845 if (orig_spec && (spec != orig_spec))
1847 /* Update the original file spec */
1849 while ((*orig_spec++ = *p++));
1857 /**********************************************************************
1858 * DIALOG_DlgDirListA
1860 * Helper function for DlgDirList*A
1862 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1863 INT idStatic, UINT attrib, BOOL combo )
1867 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1868 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1869 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1870 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1871 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1872 HeapFree( GetProcessHeap(), 0, specW );
1875 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1879 /**********************************************************************
1880 * DlgDirSelectExA (USER32.@)
1882 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1884 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1888 /**********************************************************************
1889 * DlgDirSelectExW (USER32.@)
1891 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1893 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1897 /**********************************************************************
1898 * DlgDirSelectComboBoxExA (USER32.@)
1900 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1903 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1907 /**********************************************************************
1908 * DlgDirSelectComboBoxExW (USER32.@)
1910 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1913 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1917 /**********************************************************************
1918 * DlgDirListA (USER32.@)
1920 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1921 INT idStatic, UINT attrib )
1923 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1927 /**********************************************************************
1928 * DlgDirListW (USER32.@)
1930 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1931 INT idStatic, UINT attrib )
1933 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1937 /**********************************************************************
1938 * DlgDirListComboBoxA (USER32.@)
1940 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1941 INT idStatic, UINT attrib )
1943 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1947 /**********************************************************************
1948 * DlgDirListComboBoxW (USER32.@)
1950 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1951 INT idStatic, UINT attrib )
1953 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );