Remove the winetest_ok() hack that adds a trailing '\n' if it is
[wine.git] / windows / dialog.c
blobb3d6f5d433a213901326bec64c95a8033f2648a8
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
39 #include "controls.h"
40 #include "win.h"
41 #include "user.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
47 /* Dialog control information */
48 typedef struct
50 DWORD style;
51 DWORD exStyle;
52 DWORD helpId;
53 INT16 x;
54 INT16 y;
55 INT16 cx;
56 INT16 cy;
57 UINT id;
58 LPCWSTR className;
59 LPCWSTR windowName;
60 LPCVOID data;
61 } DLG_CONTROL_INFO;
63 /* Dialog template */
64 typedef struct
66 DWORD style;
67 DWORD exStyle;
68 DWORD helpId;
69 UINT16 nbItems;
70 INT16 x;
71 INT16 y;
72 INT16 cx;
73 INT16 cy;
74 LPCWSTR menuName;
75 LPCWSTR className;
76 LPCWSTR caption;
77 INT16 pointSize;
78 WORD weight;
79 BOOL italic;
80 LPCWSTR faceName;
81 BOOL dialogEx;
82 } DLG_TEMPLATE;
84 /* Radio button group */
85 typedef struct
87 UINT firstID;
88 UINT lastID;
89 UINT checkID;
90 } RADIOGROUP;
93 /*********************************************************************
94 * dialog class descriptor
96 const struct builtin_class_descr DIALOG_builtin_class =
98 DIALOG_CLASS_ATOMA, /* name */
99 CS_SAVEBITS | CS_DBLCLKS, /* style */
100 DefDlgProcA, /* procA */
101 DefDlgProcW, /* procW */
102 DLGWINDOWEXTRA, /* extra */
103 IDC_ARROW, /* cursor */
104 0 /* brush */
108 /***********************************************************************
109 * DIALOG_EnableOwner
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 */
117 if (hOwner)
118 hOwner = GetAncestor( hOwner, GA_ROOT );
119 if (!hOwner) return;
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 */
133 if (hOwner)
134 hOwner = GetAncestor( hOwner, GA_ROOT );
135 if (!hOwner) return FALSE;
136 if (IsWindowEnabled( hOwner ))
138 EnableWindow( hOwner, FALSE );
139 return TRUE;
141 else
142 return FALSE;
145 /***********************************************************************
146 * DIALOG_GetCharSize
148 * Despite most of MSDN insisting that the horizontal base unit is
149 * tmAveCharWidth it isn't. Knowledge base article Q145994
150 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
151 * says that we should take the average of the 52 English upper and lower
152 * case characters.
154 BOOL DIALOG_GetCharSize( HDC hDC, HFONT hFont, SIZE * pSize )
156 HFONT hFontPrev = 0;
157 const char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
158 SIZE sz;
159 TEXTMETRICA tm;
161 if(!hDC) return FALSE;
163 if(hFont) hFontPrev = SelectObject(hDC, hFont);
164 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
165 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
167 pSize->cy = tm.tmHeight;
168 pSize->cx = (sz.cx / 26 + 1) / 2;
170 if (hFontPrev) SelectObject(hDC, hFontPrev);
172 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
173 return TRUE;
177 /***********************************************************************
178 * DIALOG_GetControl32
180 * Return the class and text of the control pointed to by ptr,
181 * fill the header structure and return a pointer to the next control.
183 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
184 BOOL dialogEx )
186 if (dialogEx)
188 info->helpId = GET_DWORD(p); p += 2;
189 info->exStyle = GET_DWORD(p); p += 2;
190 info->style = GET_DWORD(p); p += 2;
192 else
194 info->helpId = 0;
195 info->style = GET_DWORD(p); p += 2;
196 info->exStyle = GET_DWORD(p); p += 2;
198 info->x = GET_WORD(p); p++;
199 info->y = GET_WORD(p); p++;
200 info->cx = GET_WORD(p); p++;
201 info->cy = GET_WORD(p); p++;
203 if (dialogEx)
205 /* id is a DWORD for DIALOGEX */
206 info->id = GET_DWORD(p);
207 p += 2;
209 else
211 info->id = GET_WORD(p);
212 p++;
215 if (GET_WORD(p) == 0xffff)
217 static const WCHAR class_names[6][10] =
219 { 'B','u','t','t','o','n', }, /* 0x80 */
220 { 'E','d','i','t', }, /* 0x81 */
221 { 'S','t','a','t','i','c', }, /* 0x82 */
222 { 'L','i','s','t','B','o','x', }, /* 0x83 */
223 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
224 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
226 WORD id = GET_WORD(p+1);
227 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
228 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
229 if (id <= 5)
230 info->className = class_names[id];
231 else
233 info->className = NULL;
234 ERR("Unknown built-in class id %04x\n", id );
236 p += 2;
238 else
240 info->className = (LPCWSTR)p;
241 p += strlenW( info->className ) + 1;
244 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
246 info->windowName = (LPCWSTR)(UINT)GET_WORD(p + 1);
247 p += 2;
249 else
251 info->windowName = (LPCWSTR)p;
252 p += strlenW( info->windowName ) + 1;
255 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
256 debugstr_w( info->className ), debugstr_w( info->windowName ),
257 info->id, info->x, info->y, info->cx, info->cy,
258 info->style, info->exStyle, info->helpId );
260 if (GET_WORD(p))
262 if (TRACE_ON(dialog))
264 WORD i, count = GET_WORD(p) / sizeof(WORD);
265 TRACE(" BEGIN\n");
266 TRACE(" ");
267 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
268 TRACE("\n");
269 TRACE(" END\n" );
271 info->data = p + 1;
272 p += GET_WORD(p) / sizeof(WORD);
274 else info->data = NULL;
275 p++;
277 /* Next control is on dword boundary */
278 return (const WORD *)((((int)p) + 3) & ~3);
282 /***********************************************************************
283 * DIALOG_CreateControls32
285 * Create the control windows for a dialog.
287 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
288 HINSTANCE hInst, BOOL unicode )
290 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
291 DLG_CONTROL_INFO info;
292 HWND hwndCtrl, hwndDefButton = 0;
293 INT items = dlgTemplate->nbItems;
295 TRACE(" BEGIN\n" );
296 while (items--)
298 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
299 dlgTemplate->dialogEx );
300 /* Is this it? */
301 if (info.style & WS_BORDER)
303 info.style &= ~WS_BORDER;
304 info.exStyle |= WS_EX_CLIENTEDGE;
306 if (unicode)
308 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
309 info.className, info.windowName,
310 info.style | WS_CHILD,
311 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
312 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
313 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
314 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
315 hwnd, (HMENU)info.id,
316 hInst, (LPVOID)info.data );
318 else
320 LPSTR class = (LPSTR)info.className;
321 LPSTR caption = (LPSTR)info.windowName;
323 if (HIWORD(class))
325 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
326 class = HeapAlloc( GetProcessHeap(), 0, len );
327 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
329 if (HIWORD(caption))
331 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
332 caption = HeapAlloc( GetProcessHeap(), 0, len );
333 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
335 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
336 class, caption, info.style | WS_CHILD,
337 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
338 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
339 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
340 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
341 hwnd, (HMENU)info.id,
342 hInst, (LPVOID)info.data );
343 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
344 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
346 if (!hwndCtrl)
348 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
349 return FALSE;
352 /* Send initialisation messages to the control */
353 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
354 (WPARAM)dlgInfo->hUserFont, 0 );
355 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
357 /* If there's already a default push-button, set it back */
358 /* to normal and use this one instead. */
359 if (hwndDefButton)
360 SendMessageA( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
361 hwndDefButton = hwndCtrl;
362 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
365 TRACE(" END\n" );
366 return TRUE;
370 /***********************************************************************
371 * DIALOG_ParseTemplate32
373 * Fill a DLG_TEMPLATE structure from the dialog template, and return
374 * a pointer to the first control.
376 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
378 const WORD *p = (const WORD *)template;
380 result->style = GET_DWORD(p); p += 2;
381 if (result->style == 0xffff0001) /* DIALOGEX resource */
383 result->dialogEx = TRUE;
384 result->helpId = GET_DWORD(p); p += 2;
385 result->exStyle = GET_DWORD(p); p += 2;
386 result->style = GET_DWORD(p); p += 2;
388 else
390 result->dialogEx = FALSE;
391 result->helpId = 0;
392 result->exStyle = GET_DWORD(p); p += 2;
394 result->nbItems = GET_WORD(p); p++;
395 result->x = GET_WORD(p); p++;
396 result->y = GET_WORD(p); p++;
397 result->cx = GET_WORD(p); p++;
398 result->cy = GET_WORD(p); p++;
399 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
400 result->dialogEx ? "EX" : "", result->x, result->y,
401 result->cx, result->cy, result->helpId );
402 TRACE(" STYLE 0x%08lx\n", result->style );
403 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
405 /* Get the menu name */
407 switch(GET_WORD(p))
409 case 0x0000:
410 result->menuName = NULL;
411 p++;
412 break;
413 case 0xffff:
414 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
415 p += 2;
416 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
417 break;
418 default:
419 result->menuName = (LPCWSTR)p;
420 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
421 p += strlenW( result->menuName ) + 1;
422 break;
425 /* Get the class name */
427 switch(GET_WORD(p))
429 case 0x0000:
430 result->className = DIALOG_CLASS_ATOMW;
431 p++;
432 break;
433 case 0xffff:
434 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
435 p += 2;
436 TRACE(" CLASS %04x\n", LOWORD(result->className) );
437 break;
438 default:
439 result->className = (LPCWSTR)p;
440 TRACE(" CLASS %s\n", debugstr_w( result->className ));
441 p += strlenW( result->className ) + 1;
442 break;
445 /* Get the window caption */
447 result->caption = (LPCWSTR)p;
448 p += strlenW( result->caption ) + 1;
449 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
451 /* Get the font name */
453 if (result->style & DS_SETFONT)
455 result->pointSize = GET_WORD(p);
456 p++;
457 if (result->dialogEx)
459 result->weight = GET_WORD(p); p++;
460 result->italic = LOBYTE(GET_WORD(p)); p++;
462 else
464 result->weight = FW_DONTCARE;
465 result->italic = FALSE;
467 result->faceName = (LPCWSTR)p;
468 p += strlenW( result->faceName ) + 1;
469 TRACE(" FONT %d, %s, %d, %s\n",
470 result->pointSize, debugstr_w( result->faceName ),
471 result->weight, result->italic ? "TRUE" : "FALSE" );
474 /* First control is on dword boundary */
475 return (LPCSTR)((((int)p) + 3) & ~3);
479 /***********************************************************************
480 * DIALOG_CreateIndirect
481 * Creates a dialog box window
483 * modal = TRUE if we are called from a modal dialog box.
484 * (it's more compatible to do it here, as under Windows the owner
485 * is never disabled if the dialog fails because of an invalid template)
487 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
488 HWND owner, DLGPROC dlgProc, LPARAM param,
489 BOOL unicode, BOOL modal )
491 HWND hwnd;
492 RECT rect;
493 WND * wndPtr;
494 DLG_TEMPLATE template;
495 DIALOGINFO * dlgInfo;
496 DWORD units = GetDialogBaseUnits();
497 BOOL ownerEnabled = TRUE;
499 /* Parse dialog template */
501 if (!dlgTemplate) return 0;
502 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
504 /* Initialise dialog extra data */
506 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
507 dlgInfo->hwndFocus = 0;
508 dlgInfo->hUserFont = 0;
509 dlgInfo->hMenu = 0;
510 dlgInfo->xBaseUnit = LOWORD(units);
511 dlgInfo->yBaseUnit = HIWORD(units);
512 dlgInfo->idResult = 0;
513 dlgInfo->flags = 0;
514 dlgInfo->hDialogHeap = 0;
516 /* Load menu */
518 if (template.menuName) dlgInfo->hMenu = LoadMenuW( hInst, template.menuName );
520 /* Create custom font if needed */
522 if (template.style & DS_SETFONT)
524 /* We convert the size to pixels and then make it -ve. This works
525 * for both +ve and -ve template.pointSize */
526 HDC dc;
527 int pixels;
528 dc = GetDC(0);
529 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
530 dlgInfo->hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
531 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
532 PROOF_QUALITY, FF_DONTCARE,
533 template.faceName );
534 if (dlgInfo->hUserFont)
536 SIZE charSize;
537 if (DIALOG_GetCharSize( dc, dlgInfo->hUserFont, &charSize ))
539 dlgInfo->xBaseUnit = charSize.cx;
540 dlgInfo->yBaseUnit = charSize.cy;
543 ReleaseDC(0, dc);
544 TRACE("units = %d,%d\n", dlgInfo->xBaseUnit, dlgInfo->yBaseUnit );
547 /* Create dialog main window */
549 rect.left = rect.top = 0;
550 rect.right = MulDiv(template.cx, dlgInfo->xBaseUnit, 4);
551 rect.bottom = MulDiv(template.cy, dlgInfo->yBaseUnit, 8);
552 if (template.style & WS_CHILD)
553 template.style &= ~(WS_CAPTION|WS_SYSMENU);
554 if (template.style & DS_MODALFRAME)
555 template.exStyle |= WS_EX_DLGMODALFRAME;
556 AdjustWindowRectEx( &rect, template.style, (dlgInfo->hMenu != 0), template.exStyle );
557 rect.right -= rect.left;
558 rect.bottom -= rect.top;
560 if (template.x == CW_USEDEFAULT16)
562 rect.left = rect.top = CW_USEDEFAULT;
564 else
566 if (template.style & DS_CENTER)
568 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
569 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
571 else
573 rect.left += MulDiv(template.x, dlgInfo->xBaseUnit, 4);
574 rect.top += MulDiv(template.y, dlgInfo->yBaseUnit, 8);
576 if ( !(template.style & WS_CHILD) )
578 INT dX, dY;
580 if( !(template.style & DS_ABSALIGN) )
581 ClientToScreen( owner, (POINT *)&rect );
583 /* try to fit it into the desktop */
585 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
586 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
587 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
588 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
589 if( rect.left < 0 ) rect.left = 0;
590 if( rect.top < 0 ) rect.top = 0;
594 if (modal)
596 ownerEnabled = DIALOG_DisableOwner( owner );
597 if (ownerEnabled) dlgInfo->flags |= DF_OWNERENABLED;
600 if (unicode)
602 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
603 template.style & ~WS_VISIBLE,
604 rect.left, rect.top, rect.right, rect.bottom,
605 owner, dlgInfo->hMenu, hInst, NULL );
607 else
609 LPSTR class = (LPSTR)template.className;
610 LPSTR caption = (LPSTR)template.caption;
612 if (HIWORD(class))
614 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
615 class = HeapAlloc( GetProcessHeap(), 0, len );
616 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
618 if (HIWORD(caption))
620 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
621 caption = HeapAlloc( GetProcessHeap(), 0, len );
622 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
624 hwnd = CreateWindowExA(template.exStyle, class, caption,
625 template.style & ~WS_VISIBLE,
626 rect.left, rect.top, rect.right, rect.bottom,
627 owner, dlgInfo->hMenu, hInst, NULL );
628 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
629 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
632 if (!hwnd)
634 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
635 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
636 if (modal && (dlgInfo->flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
637 HeapFree( GetProcessHeap(), 0, dlgInfo );
638 return 0;
640 wndPtr = WIN_GetPtr( hwnd );
641 wndPtr->flags |= WIN_ISDIALOG;
642 WIN_ReleasePtr( wndPtr );
644 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
645 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
647 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
648 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
650 if (dlgInfo->hUserFont)
651 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
653 /* Create controls */
655 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
657 HWND hwndPreInitFocus;
659 /* Send initialisation messages and set focus */
661 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
663 hwndPreInitFocus = GetFocus();
664 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
666 /* check where the focus is again,
667 * some controls status might have changed in WM_INITDIALOG */
668 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
669 if( dlgInfo->hwndFocus )
670 SetFocus( dlgInfo->hwndFocus );
672 else
674 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
675 but the focus has not changed, set the focus where we expect it. */
676 if ((GetFocus() == hwndPreInitFocus) &&
677 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
679 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
680 if( dlgInfo->hwndFocus )
681 SetFocus( dlgInfo->hwndFocus );
685 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
687 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
689 return hwnd;
691 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
692 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
693 return 0;
697 /***********************************************************************
698 * CreateDialogParamA (USER32.@)
700 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
701 DLGPROC dlgProc, LPARAM param )
703 HRSRC hrsrc;
704 LPCDLGTEMPLATEA ptr;
706 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
707 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
708 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
712 /***********************************************************************
713 * CreateDialogParamW (USER32.@)
715 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
716 DLGPROC dlgProc, LPARAM param )
718 HRSRC hrsrc;
719 LPCDLGTEMPLATEA ptr;
721 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
722 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
723 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
727 /***********************************************************************
728 * CreateDialogIndirectParamAorW (USER32.@)
730 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
731 HWND owner, DLGPROC dlgProc, LPARAM param,
732 DWORD flags )
734 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
737 /***********************************************************************
738 * CreateDialogIndirectParamA (USER32.@)
740 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
741 HWND owner, DLGPROC dlgProc, LPARAM param )
743 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
746 /***********************************************************************
747 * CreateDialogIndirectParamW (USER32.@)
749 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
750 HWND owner, DLGPROC dlgProc, LPARAM param )
752 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
756 /***********************************************************************
757 * DIALOG_DoDialogBox
759 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
761 DIALOGINFO * dlgInfo;
762 MSG msg;
763 INT retval;
764 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
766 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
768 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
770 ShowWindow( hwnd, SW_SHOW );
771 for (;;)
773 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
775 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
777 /* No message present -> send ENTERIDLE and wait */
778 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
779 if (!GetMessageW( &msg, 0, 0, 0 )) break;
782 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
784 if (!IsWindow( hwnd )) return -1;
785 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
787 TranslateMessage( &msg );
788 DispatchMessageW( &msg );
790 if (dlgInfo->flags & DF_END) break;
793 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
794 retval = dlgInfo->idResult;
795 DestroyWindow( hwnd );
796 return retval;
800 /***********************************************************************
801 * DialogBoxParamA (USER32.@)
803 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
804 HWND owner, DLGPROC dlgProc, LPARAM param )
806 HWND hwnd;
807 HRSRC hrsrc;
808 LPCDLGTEMPLATEA ptr;
810 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
811 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
812 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
813 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
814 return -1;
818 /***********************************************************************
819 * DialogBoxParamW (USER32.@)
821 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
822 HWND owner, DLGPROC dlgProc, LPARAM param )
824 HWND hwnd;
825 HRSRC hrsrc;
826 LPCDLGTEMPLATEW ptr;
828 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
829 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
830 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
831 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
832 return -1;
836 /***********************************************************************
837 * DialogBoxIndirectParamAorW (USER32.@)
839 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
840 HWND owner, DLGPROC dlgProc,
841 LPARAM param, DWORD flags )
843 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
844 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
845 return -1;
848 /***********************************************************************
849 * DialogBoxIndirectParamA (USER32.@)
851 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
852 HWND owner, DLGPROC dlgProc, LPARAM param )
854 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
858 /***********************************************************************
859 * DialogBoxIndirectParamW (USER32.@)
861 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
862 HWND owner, DLGPROC dlgProc, LPARAM param )
864 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
867 /***********************************************************************
868 * EndDialog (USER32.@)
870 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
872 BOOL wasEnabled = TRUE;
873 DIALOGINFO * dlgInfo;
874 HWND owner;
876 TRACE("%p %d\n", hwnd, retval );
878 if (!(dlgInfo = DIALOG_get_info( hwnd )))
880 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
881 return FALSE;
883 dlgInfo->idResult = retval;
884 dlgInfo->flags |= DF_END;
885 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
887 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
888 DIALOG_EnableOwner( owner );
890 /* Windows sets the focus to the dialog itself in EndDialog */
892 if (IsChild(hwnd, GetFocus()))
893 SetFocus( hwnd );
895 /* Don't have to send a ShowWindow(SW_HIDE), just do
896 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
898 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
899 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
901 /* unblock dialog loop */
902 PostMessageA(hwnd, WM_NULL, 0, 0);
903 return TRUE;
907 /***********************************************************************
908 * DIALOG_IsAccelerator
910 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
912 HWND hwndControl = hwnd;
913 HWND hwndNext;
914 INT dlgCode;
915 WCHAR buffer[128];
919 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
920 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
922 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
923 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
924 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
926 /* find the accelerator key */
927 LPWSTR p = buffer - 2;
931 p = strchrW( p + 2, '&' );
933 while (p != NULL && p[1] == '&');
935 /* and check if it's the one we're looking for */
936 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
938 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
940 /* set focus to the control */
941 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
942 /* and bump it on to next */
943 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
945 else if (dlgCode & DLGC_BUTTON)
947 /* send BM_CLICK message to the control */
948 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
950 return TRUE;
953 hwndNext = GetWindow( hwndControl, GW_CHILD );
955 else hwndNext = 0;
957 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
959 while (!hwndNext && hwndControl)
961 hwndControl = GetParent( hwndControl );
962 if (hwndControl == hwndDlg)
964 if(hwnd==hwndDlg) /* prevent endless loop */
966 hwndNext=hwnd;
967 break;
969 hwndNext = GetWindow( hwndDlg, GW_CHILD );
971 else
972 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
974 hwndControl = hwndNext;
976 while (hwndControl && (hwndControl != hwnd));
978 return FALSE;
981 /***********************************************************************
982 * DIALOG_FindMsgDestination
984 * The messages that IsDialogMessage sends may not go to the dialog
985 * calling IsDialogMessage if that dialog is a child, and it has the
986 * DS_CONTROL style set.
987 * We propagate up until we hit one that does not have DS_CONTROL, or
988 * whose parent is not a dialog.
990 * This is undocumented behaviour.
992 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
994 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
996 WND *pParent;
997 HWND hParent = GetParent(hwndDlg);
998 if (!hParent) break;
1000 pParent = WIN_FindWndPtr(hParent);
1001 if (!pParent) break;
1003 if (!(pParent->flags & WIN_ISDIALOG))
1005 WIN_ReleaseWndPtr(pParent);
1006 break;
1008 WIN_ReleaseWndPtr(pParent);
1010 hwndDlg = hParent;
1013 return hwndDlg;
1016 /***********************************************************************
1017 * IsDialogMessageW (USER32.@)
1019 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1021 INT dlgCode = 0;
1023 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1025 hwndDlg = WIN_GetFullHandle( hwndDlg );
1026 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1028 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1030 switch(msg->message)
1032 case WM_KEYDOWN:
1033 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1034 if (dlgCode & DLGC_WANTMESSAGE) break;
1036 switch(msg->wParam)
1038 case VK_TAB:
1039 if (!(dlgCode & DLGC_WANTTAB))
1041 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1042 return TRUE;
1044 break;
1046 case VK_RIGHT:
1047 case VK_DOWN:
1048 case VK_LEFT:
1049 case VK_UP:
1050 if (!(dlgCode & DLGC_WANTARROWS))
1052 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1053 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1054 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1055 return TRUE;
1057 break;
1059 case VK_CANCEL:
1060 case VK_ESCAPE:
1061 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1062 return TRUE;
1064 case VK_EXECUTE:
1065 case VK_RETURN:
1067 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1068 if (HIWORD(dw) == DC_HASDEFID)
1070 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1071 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1073 else
1075 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1079 return TRUE;
1081 break;
1083 case WM_CHAR:
1084 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1085 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1086 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1087 /* drop through */
1089 case WM_SYSCHAR:
1090 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1092 /* don't translate or dispatch */
1093 return TRUE;
1095 break;
1098 TranslateMessage( msg );
1099 DispatchMessageW( msg );
1100 return TRUE;
1104 /***********************************************************************
1105 * GetDlgCtrlID (USER32.@)
1107 INT WINAPI GetDlgCtrlID( HWND hwnd )
1109 return GetWindowLongW( hwnd, GWL_ID );
1113 /***********************************************************************
1114 * GetDlgItem (USER32.@)
1116 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1118 int i;
1119 HWND *list = WIN_ListChildren( hwndDlg );
1120 HWND ret = 0;
1122 if (!list) return 0;
1124 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1125 ret = list[i];
1126 HeapFree( GetProcessHeap(), 0, list );
1127 return ret;
1131 /*******************************************************************
1132 * SendDlgItemMessageA (USER32.@)
1134 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1135 WPARAM wParam, LPARAM lParam )
1137 HWND hwndCtrl = GetDlgItem( hwnd, id );
1138 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1139 else return 0;
1143 /*******************************************************************
1144 * SendDlgItemMessageW (USER32.@)
1146 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1147 WPARAM wParam, LPARAM lParam )
1149 HWND hwndCtrl = GetDlgItem( hwnd, id );
1150 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1151 else return 0;
1155 /*******************************************************************
1156 * SetDlgItemTextA (USER32.@)
1158 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1160 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1164 /*******************************************************************
1165 * SetDlgItemTextW (USER32.@)
1167 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1169 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1173 /***********************************************************************
1174 * GetDlgItemTextA (USER32.@)
1176 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1178 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1179 len, (LPARAM)str );
1183 /***********************************************************************
1184 * GetDlgItemTextW (USER32.@)
1186 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1188 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1189 len, (LPARAM)str );
1193 /*******************************************************************
1194 * SetDlgItemInt (USER32.@)
1196 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1197 BOOL fSigned )
1199 char str[20];
1201 if (fSigned) sprintf( str, "%d", (INT)value );
1202 else sprintf( str, "%u", value );
1203 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1204 return TRUE;
1208 /***********************************************************************
1209 * GetDlgItemInt (USER32.@)
1211 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1212 BOOL fSigned )
1214 char str[30];
1215 char * endptr;
1216 long result = 0;
1218 if (translated) *translated = FALSE;
1219 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1220 return 0;
1221 if (fSigned)
1223 result = strtol( str, &endptr, 10 );
1224 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1225 return 0;
1226 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1227 return 0;
1229 else
1231 result = strtoul( str, &endptr, 10 );
1232 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1233 return 0;
1234 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1236 if (translated) *translated = TRUE;
1237 return (UINT)result;
1241 /***********************************************************************
1242 * CheckDlgButton (USER32.@)
1244 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1246 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1247 return TRUE;
1251 /***********************************************************************
1252 * IsDlgButtonChecked (USER32.@)
1254 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1256 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1260 /***********************************************************************
1261 * CheckRB
1263 * Callback function used to check/uncheck radio buttons that fall
1264 * within a specific range of IDs.
1266 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1268 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1269 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1271 if ((lChildID >= lpRadioGroup->firstID) &&
1272 (lChildID <= lpRadioGroup->lastID))
1274 if (lChildID == lpRadioGroup->checkID)
1276 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1278 else
1280 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1284 return TRUE;
1288 /***********************************************************************
1289 * CheckRadioButton (USER32.@)
1291 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1292 UINT lastID, UINT checkID )
1294 RADIOGROUP radioGroup;
1296 radioGroup.firstID = firstID;
1297 radioGroup.lastID = lastID;
1298 radioGroup.checkID = checkID;
1300 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1301 (LPARAM)&radioGroup);
1305 /***********************************************************************
1306 * GetDialogBaseUnits (USER.243)
1307 * GetDialogBaseUnits (USER32.@)
1309 DWORD WINAPI GetDialogBaseUnits(void)
1311 static DWORD units;
1313 if (!units)
1315 HDC hdc;
1316 SIZE size;
1318 if ((hdc = GetDC(0)))
1320 if (DIALOG_GetCharSize( hdc, 0, &size )) units = MAKELONG( size.cx, size.cy );
1321 ReleaseDC( 0, hdc );
1323 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1325 return units;
1329 /***********************************************************************
1330 * MapDialogRect (USER32.@)
1332 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1334 DIALOGINFO * dlgInfo;
1335 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1336 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1337 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1338 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1339 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1340 return TRUE;
1344 /***********************************************************************
1345 * GetNextDlgGroupItem (USER32.@)
1347 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1349 HWND hwnd, retvalue;
1351 hwndDlg = WIN_GetFullHandle( hwndDlg );
1352 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1354 if(hwndCtrl)
1356 /* if the hwndCtrl is the child of the control in the hwndDlg,
1357 * then the hwndDlg has to be the parent of the hwndCtrl */
1358 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1359 hwndDlg = GetParent(hwndCtrl);
1362 if (hwndCtrl)
1364 /* Make sure hwndCtrl is a top-level child */
1365 HWND parent = GetParent( hwndCtrl );
1366 while (parent && parent != hwndDlg) parent = GetParent(parent);
1367 if (parent != hwndDlg) return 0;
1369 else
1371 /* No ctrl specified -> start from the beginning */
1372 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1373 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1376 retvalue = hwndCtrl;
1377 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1378 while (1)
1380 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1382 /* Wrap-around to the beginning of the group */
1383 HWND tmp;
1385 hwnd = GetWindow( hwndDlg, GW_CHILD );
1386 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1388 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1389 if (tmp == hwndCtrl) break;
1392 if (hwnd == hwndCtrl) break;
1393 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1395 retvalue = hwnd;
1396 if (!fPrevious) break;
1398 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1400 return retvalue;
1404 /***********************************************************************
1405 * DIALOG_GetNextTabItem
1407 * Helper for GetNextDlgTabItem
1409 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1411 LONG dsStyle;
1412 LONG exStyle;
1413 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1414 HWND retWnd = 0;
1415 HWND hChildFirst = 0;
1417 if(!hwndCtrl)
1419 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1420 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1422 else if (IsChild( hwndMain, hwndCtrl ))
1424 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1425 if(!hChildFirst)
1427 if(GetParent(hwndCtrl) != hwndMain)
1428 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1429 else
1431 if(fPrevious)
1432 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1433 else
1434 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1439 while(hChildFirst)
1441 BOOL bCtrl = FALSE;
1442 while(hChildFirst)
1444 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1445 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1446 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1448 bCtrl=TRUE;
1449 break;
1451 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1452 break;
1453 hChildFirst = GetWindow(hChildFirst,wndSearch);
1455 if(hChildFirst)
1457 if(bCtrl)
1458 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1459 else
1460 retWnd = hChildFirst;
1462 if(retWnd) break;
1463 hChildFirst = GetWindow(hChildFirst,wndSearch);
1465 if(!retWnd && hwndCtrl)
1467 HWND hParent = GetParent(hwndCtrl);
1468 while(hParent)
1470 if(hParent == hwndMain) break;
1471 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1472 if(retWnd) break;
1473 hParent = GetParent(hParent);
1475 if(!retWnd)
1476 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1478 return retWnd ? retWnd : hwndCtrl;
1481 /***********************************************************************
1482 * GetNextDlgTabItem (USER32.@)
1484 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1485 BOOL fPrevious )
1487 hwndDlg = WIN_GetFullHandle( hwndDlg );
1488 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1489 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1492 /**********************************************************************
1493 * DIALOG_DlgDirSelect
1495 * Helper function for DlgDirSelect*
1497 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1498 INT id, BOOL unicode, BOOL combo )
1500 char *buffer, *ptr;
1501 INT item, size;
1502 BOOL ret;
1503 HWND listbox = GetDlgItem( hwnd, id );
1505 TRACE("%p '%s' %d\n", hwnd, str, id );
1506 if (!listbox) return FALSE;
1508 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1509 if (item == LB_ERR) return FALSE;
1510 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1511 if (size == LB_ERR) return FALSE;
1513 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1515 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1517 if ((ret = (buffer[0] == '['))) /* drive or directory */
1519 if (buffer[1] == '-') /* drive */
1521 buffer[3] = ':';
1522 buffer[4] = 0;
1523 ptr = buffer + 2;
1525 else
1527 buffer[strlen(buffer)-1] = '\\';
1528 ptr = buffer + 1;
1531 else ptr = buffer;
1533 if (unicode)
1535 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1536 ((LPWSTR)str)[len-1] = 0;
1538 else lstrcpynA( str, ptr, len );
1539 HeapFree( GetProcessHeap(), 0, buffer );
1540 TRACE("Returning %d '%s'\n", ret, str );
1541 return ret;
1545 /**********************************************************************
1546 * DIALOG_DlgDirListW
1548 * Helper function for DlgDirList*W
1550 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1551 INT idStatic, UINT attrib, BOOL combo )
1553 HWND hwnd;
1554 LPWSTR orig_spec = spec;
1555 WCHAR any[] = {'*','.','*',0};
1557 #define SENDMSG(msg,wparam,lparam) \
1558 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1559 : SendMessageW( hwnd, msg, wparam, lparam ))
1561 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1563 /* If the path exists and is a directory, chdir to it */
1564 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1565 else
1567 WCHAR *p, *p2;
1568 p = spec;
1569 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1570 if ((p2 = strrchrW( p, '/' ))) p = p2;
1571 if (p != spec)
1573 WCHAR sep = *p;
1574 *p = 0;
1575 if (!SetCurrentDirectoryW( spec ))
1577 *p = sep; /* Restore the original spec */
1578 return FALSE;
1580 spec = p + 1;
1584 TRACE( "mask=%s\n", debugstr_w(spec) );
1586 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1588 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1589 if (attrib & DDL_DIRECTORY)
1591 if (!(attrib & DDL_EXCLUSIVE))
1593 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1594 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1595 (LPARAM)spec ) == LB_ERR)
1596 return FALSE;
1598 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1599 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1600 (LPARAM)any ) == LB_ERR)
1601 return FALSE;
1603 else
1605 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1606 (LPARAM)spec ) == LB_ERR)
1607 return FALSE;
1611 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1613 WCHAR temp[MAX_PATH];
1614 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1615 CharLowerW( temp );
1616 /* Can't use PostMessage() here, because the string is on the stack */
1617 SetDlgItemTextW( hDlg, idStatic, temp );
1620 if (orig_spec && (spec != orig_spec))
1622 /* Update the original file spec */
1623 WCHAR *p = spec;
1624 while ((*orig_spec++ = *p++));
1627 return TRUE;
1628 #undef SENDMSG
1632 /**********************************************************************
1633 * DIALOG_DlgDirListA
1635 * Helper function for DlgDirList*A
1637 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1638 INT idStatic, UINT attrib, BOOL combo )
1640 if (spec)
1642 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1643 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1644 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1645 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1646 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1647 HeapFree( GetProcessHeap(), 0, specW );
1648 return ret;
1650 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1654 /**********************************************************************
1655 * DlgDirSelectExA (USER32.@)
1657 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1659 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1663 /**********************************************************************
1664 * DlgDirSelectExW (USER32.@)
1666 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1668 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1672 /**********************************************************************
1673 * DlgDirSelectComboBoxExA (USER32.@)
1675 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1676 INT id )
1678 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1682 /**********************************************************************
1683 * DlgDirSelectComboBoxExW (USER32.@)
1685 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1686 INT id)
1688 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
1692 /**********************************************************************
1693 * DlgDirListA (USER32.@)
1695 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1696 INT idStatic, UINT attrib )
1698 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1702 /**********************************************************************
1703 * DlgDirListW (USER32.@)
1705 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1706 INT idStatic, UINT attrib )
1708 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1712 /**********************************************************************
1713 * DlgDirListComboBoxA (USER32.@)
1715 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1716 INT idStatic, UINT attrib )
1718 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1722 /**********************************************************************
1723 * DlgDirListComboBoxW (USER32.@)
1725 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1726 INT idStatic, UINT attrib )
1728 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );