user32: Move a few 16-bit stubs to user16.c.
[wine] / dlls / user32 / dialog.c
1 /*
2  * Dialog functions
3  *
4  * Copyright 1993, 1994, 1996 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "wine/unicode.h"
38 #include "controls.h"
39 #include "win.h"
40 #include "user_private.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
44
45
46   /* Dialog control information */
47 typedef struct
48 {
49     DWORD      style;
50     DWORD      exStyle;
51     DWORD      helpId;
52     INT16      x;
53     INT16      y;
54     INT16      cx;
55     INT16      cy;
56     UINT_PTR   id;
57     LPCWSTR    className;
58     LPCWSTR    windowName;
59     LPCVOID    data;
60 } DLG_CONTROL_INFO;
61
62   /* Dialog template */
63 typedef struct
64 {
65     DWORD      style;
66     DWORD      exStyle;
67     DWORD      helpId;
68     UINT16     nbItems;
69     INT16      x;
70     INT16      y;
71     INT16      cx;
72     INT16      cy;
73     LPCWSTR    menuName;
74     LPCWSTR    className;
75     LPCWSTR    caption;
76     INT16      pointSize;
77     WORD       weight;
78     BOOL       italic;
79     LPCWSTR    faceName;
80     BOOL       dialogEx;
81 } DLG_TEMPLATE;
82
83   /* Radio button group */
84 typedef struct
85 {
86     UINT firstID;
87     UINT lastID;
88     UINT checkID;
89 } RADIOGROUP;
90
91
92 /*********************************************************************
93  * dialog class descriptor
94  */
95 const struct builtin_class_descr DIALOG_builtin_class =
96 {
97     (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
98     CS_SAVEBITS | CS_DBLCLKS, /* style  */
99     WINPROC_DIALOG,     /* proc */
100     DLGWINDOWEXTRA,     /* extra */
101     IDC_ARROW,          /* cursor */
102     0                   /* brush */
103 };
104
105
106 /***********************************************************************
107  *           DIALOG_EnableOwner
108  *
109  * Helper function for modal dialogs to enable again the
110  * owner of the dialog box.
111  */
112 static void DIALOG_EnableOwner( HWND hOwner )
113 {
114     /* Owner must be a top-level window */
115     if (hOwner)
116         hOwner = GetAncestor( hOwner, GA_ROOT );
117     if (!hOwner) return;
118     EnableWindow( hOwner, TRUE );
119 }
120
121
122 /***********************************************************************
123  *           DIALOG_DisableOwner
124  *
125  * Helper function for modal dialogs to disable the
126  * owner of the dialog box. Returns TRUE if owner was enabled.
127  */
128 static BOOL DIALOG_DisableOwner( HWND hOwner )
129 {
130     /* Owner must be a top-level window */
131     if (hOwner)
132         hOwner = GetAncestor( hOwner, GA_ROOT );
133     if (!hOwner) return FALSE;
134     if (IsWindowEnabled( hOwner ))
135     {
136         EnableWindow( hOwner, FALSE );
137         return TRUE;
138     }
139     else
140         return FALSE;
141 }
142
143 /***********************************************************************
144  *           DIALOG_GetControl32
145  *
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.
148  */
149 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
150                                         BOOL dialogEx )
151 {
152     if (dialogEx)
153     {
154         info->helpId  = GET_DWORD(p); p += 2;
155         info->exStyle = GET_DWORD(p); p += 2;
156         info->style   = GET_DWORD(p); p += 2;
157     }
158     else
159     {
160         info->helpId  = 0;
161         info->style   = GET_DWORD(p); p += 2;
162         info->exStyle = GET_DWORD(p); p += 2;
163     }
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++;
168
169     if (dialogEx)
170     {
171         /* id is a DWORD for DIALOGEX */
172         info->id = GET_DWORD(p);
173         p += 2;
174     }
175     else
176     {
177         info->id = GET_WORD(p);
178         p++;
179     }
180
181     if (GET_WORD(p) == 0xffff)
182     {
183         static const WCHAR class_names[6][10] =
184         {
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 */
191         };
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;
195         if (id <= 5)
196             info->className = class_names[id];
197         else
198         {
199             info->className = NULL;
200             ERR("Unknown built-in class id %04x\n", id );
201         }
202         p += 2;
203     }
204     else
205     {
206         info->className = p;
207         p += strlenW( info->className ) + 1;
208     }
209
210     if (GET_WORD(p) == 0xffff)  /* Is it an integer id? */
211     {
212         info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
213         p += 2;
214     }
215     else
216     {
217         info->windowName = p;
218         p += strlenW( info->windowName ) + 1;
219     }
220
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 );
225
226     if (GET_WORD(p))
227     {
228         if (TRACE_ON(dialog))
229         {
230             WORD i, count = GET_WORD(p) / sizeof(WORD);
231             TRACE("  BEGIN\n");
232             TRACE("    ");
233             for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
234             TRACE("\n");
235             TRACE("  END\n" );
236         }
237         info->data = p + 1;
238         p += GET_WORD(p) / sizeof(WORD);
239     }
240     else info->data = NULL;
241     p++;
242
243     /* Next control is on dword boundary */
244     return (const WORD *)(((UINT_PTR)p + 3) & ~3);
245 }
246
247
248 /***********************************************************************
249  *           DIALOG_CreateControls32
250  *
251  * Create the control windows for a dialog.
252  */
253 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
254                                      HINSTANCE hInst, BOOL unicode )
255 {
256     DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
257     DLG_CONTROL_INFO info;
258     HWND hwndCtrl, hwndDefButton = 0;
259     INT items = dlgTemplate->nbItems;
260
261     TRACE(" BEGIN\n" );
262     while (items--)
263     {
264         template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
265                                                 dlgTemplate->dialogEx );
266         /* Is this it? */
267         if (info.style & WS_BORDER)
268         {
269             info.style &= ~WS_BORDER;
270             info.exStyle |= WS_EX_CLIENTEDGE;
271         }
272         if (unicode)
273         {
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 );
283         }
284         else
285         {
286             LPCSTR class = (LPCSTR)info.className;
287             LPCSTR caption = (LPCSTR)info.windowName;
288             LPSTR class_tmp = NULL;
289             LPSTR caption_tmp = NULL;
290
291             if (HIWORD(class))
292             {
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 );
296                 class = class_tmp;
297             }
298             if (HIWORD(caption))
299             {
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;
304             }
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 );
315         }
316         if (!hwndCtrl)
317         {
318             if (dlgTemplate->style & DS_NOFAILCREATE) continue;
319             return FALSE;
320         }
321
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)
326         {
327               /* If there's already a default push-button, set it back */
328               /* to normal and use this one instead. */
329             if (hwndDefButton)
330                 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
331             hwndDefButton = hwndCtrl;
332             dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
333         }
334     }
335     TRACE(" END\n" );
336     return TRUE;
337 }
338
339
340 /***********************************************************************
341  *           DIALOG_ParseTemplate32
342  *
343  * Fill a DLG_TEMPLATE structure from the dialog template, and return
344  * a pointer to the first control.
345  */
346 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
347 {
348     const WORD *p = (const WORD *)template;
349     WORD signature;
350     WORD dlgver;
351
352     signature = GET_WORD(p); p++;
353     dlgver = GET_WORD(p); p++;
354
355     if (signature == 1 && dlgver == 0xffff)  /* DIALOGEX resource */
356     {
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;
361     }
362     else
363     {
364         result->style = GET_DWORD(p - 2);
365         result->dialogEx = FALSE;
366         result->helpId   = 0;
367         result->exStyle  = GET_DWORD(p); p += 2;
368     }
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 );
379
380     /* Get the menu name */
381
382     switch(GET_WORD(p))
383     {
384     case 0x0000:
385         result->menuName = NULL;
386         p++;
387         break;
388     case 0xffff:
389         result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
390         p += 2;
391         TRACE(" MENU %04x\n", LOWORD(result->menuName) );
392         break;
393     default:
394         result->menuName = p;
395         TRACE(" MENU %s\n", debugstr_w(result->menuName) );
396         p += strlenW( result->menuName ) + 1;
397         break;
398     }
399
400     /* Get the class name */
401
402     switch(GET_WORD(p))
403     {
404     case 0x0000:
405         result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
406         p++;
407         break;
408     case 0xffff:
409         result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
410         p += 2;
411         TRACE(" CLASS %04x\n", LOWORD(result->className) );
412         break;
413     default:
414         result->className = p;
415         TRACE(" CLASS %s\n", debugstr_w( result->className ));
416         p += strlenW( result->className ) + 1;
417         break;
418     }
419
420     /* Get the window caption */
421
422     result->caption = p;
423     p += strlenW( result->caption ) + 1;
424     TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
425
426     /* Get the font name */
427
428     result->pointSize = 0;
429     result->faceName = NULL;
430     result->weight = FW_DONTCARE;
431     result->italic = FALSE;
432
433     if (result->style & DS_SETFONT)
434     {
435         result->pointSize = GET_WORD(p);
436         p++;
437
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.
441          */
442         if (result->pointSize == 0x7fff)
443         {
444             /* We could call SystemParametersInfo here, but then we'd have
445              * to convert from pixel size to point size (which can be
446              * imprecise).
447              */
448             TRACE(" FONT: Using message box font\n");
449         }
450         else
451         {
452             if (result->dialogEx)
453             {
454                 result->weight = GET_WORD(p); p++;
455                 result->italic = LOBYTE(GET_WORD(p)); p++;
456             }
457             result->faceName = p;
458             p += strlenW( result->faceName ) + 1;
459
460             TRACE(" FONT %d, %s, %d, %s\n",
461                   result->pointSize, debugstr_w( result->faceName ),
462                   result->weight, result->italic ? "TRUE" : "FALSE" );
463         }
464     }
465
466     /* First control is on dword boundary */
467     return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
468 }
469
470
471 /***********************************************************************
472  *           DIALOG_CreateIndirect
473  *       Creates a dialog box window
474  *
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)
478  */
479 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
480                                    HWND owner, DLGPROC dlgProc, LPARAM param,
481                                    BOOL unicode, BOOL modal )
482 {
483     HWND hwnd;
484     RECT rect;
485     POINT pos;
486     SIZE size;
487     DLG_TEMPLATE template;
488     DIALOGINFO * dlgInfo = NULL;
489     DWORD units = GetDialogBaseUnits();
490     BOOL ownerEnabled = TRUE;
491     HMENU hMenu = 0;
492     HFONT hUserFont = 0;
493     UINT flags = 0;
494     UINT xBaseUnit = LOWORD(units);
495     UINT yBaseUnit = HIWORD(units);
496
497       /* Parse dialog template */
498
499     if (!dlgTemplate) return 0;
500     dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
501
502       /* Load menu */
503
504     if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
505
506       /* Create custom font if needed */
507
508     if (template.style & DS_SETFONT)
509     {
510         HDC dc = GetDC(0);
511
512         if (template.pointSize == 0x7fff)
513         {
514             /* We get the message font from the non-client metrics */
515             NONCLIENTMETRICSW ncMetrics;
516
517             ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
518             if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
519                                       sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
520             {
521                 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
522             }
523         }
524         else
525         {
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,
532                                               template.faceName );
533         }
534
535         if (hUserFont)
536         {
537             SIZE charSize;
538             HFONT hOldFont = SelectObject( dc, hUserFont );
539             charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
540             if (charSize.cx)
541             {
542                 xBaseUnit = charSize.cx;
543                 yBaseUnit = charSize.cy;
544             }
545             SelectObject( dc, hOldFont );
546         }
547         ReleaseDC(0, dc);
548         TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
549     }
550
551     /* Create dialog main window */
552
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 );
563     pos.x = rect.left;
564     pos.y = rect.top;
565     size.cx = rect.right - rect.left;
566     size.cy = rect.bottom - rect.top;
567
568     if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
569     {
570         pos.x = pos.y = CW_USEDEFAULT;
571     }
572     else
573     {
574         HMONITOR monitor = 0;
575         MONITORINFO mon_info;
576
577         mon_info.cbSize = sizeof(mon_info);
578         if (template.style & DS_CENTER)
579         {
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;
584         }
585         else if (template.style & DS_CENTERMOUSE)
586         {
587             GetCursorPos( &pos );
588             monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
589             GetMonitorInfoW( monitor, &mon_info );
590         }
591         else
592         {
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 );
596         }
597         if ( !(template.style & WS_CHILD) )
598         {
599             INT dX, dY;
600
601             /* try to fit it into the desktop */
602
603             if (!monitor)
604             {
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 );
608             }
609             if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
610                 pos.x -= dX;
611             if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
612                 pos.y -= dY;
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;
615         }
616     }
617
618     if (modal)
619     {
620         ownerEnabled = DIALOG_DisableOwner( owner );
621         if (ownerEnabled) flags |= DF_OWNERENABLED;
622     }
623
624     if (unicode)
625     {
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 );
629     }
630     else
631     {
632         LPCSTR class = (LPCSTR)template.className;
633         LPCSTR caption = (LPCSTR)template.caption;
634         LPSTR class_tmp = NULL;
635         LPSTR caption_tmp = NULL;
636
637         if (HIWORD(class))
638         {
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 );
642             class = class_tmp;
643         }
644         if (HIWORD(caption))
645         {
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;
650         }
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 );
656     }
657
658     if (!hwnd)
659     {
660         if (hUserFont) DeleteObject( hUserFont );
661         if (hMenu) DestroyMenu( hMenu );
662         if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
663         return 0;
664     }
665
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->idResult    = IDOK;
676     dlgInfo->flags       = flags;
677
678     if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
679
680     if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
681     else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
682
683     if (dlgProc && dlgInfo->hUserFont)
684         SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
685
686     /* Create controls */
687
688     if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
689     {
690         /* Send initialisation messages and set focus */
691
692         if (dlgProc)
693         {
694             if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
695                 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
696             {
697                 /* By returning TRUE, app has requested a default focus assignment */
698                 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
699                 if( dlgInfo->hwndFocus )
700                     SetFocus( dlgInfo->hwndFocus );
701             }
702         }
703
704         if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
705         {
706            ShowWindow( hwnd, SW_SHOWNORMAL );   /* SW_SHOW doesn't always work */
707         }
708         return hwnd;
709     }
710     if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
711     if( IsWindow(hwnd) ) DestroyWindow( hwnd );
712     return 0;
713 }
714
715
716 /***********************************************************************
717  *              CreateDialogParamA (USER32.@)
718  */
719 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
720                                 DLGPROC dlgProc, LPARAM param )
721 {
722     HRSRC hrsrc;
723     LPCDLGTEMPLATEA ptr;
724
725     if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
726     if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
727     return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
728 }
729
730
731 /***********************************************************************
732  *              CreateDialogParamW (USER32.@)
733  */
734 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
735                                 DLGPROC dlgProc, LPARAM param )
736 {
737     HRSRC hrsrc;
738     LPCDLGTEMPLATEA ptr;
739
740     if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
741     if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
742     return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
743 }
744
745
746 /***********************************************************************
747  *              CreateDialogIndirectParamAorW (USER32.@)
748  */
749 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
750                                            HWND owner, DLGPROC dlgProc, LPARAM param,
751                                            DWORD flags )
752 {
753     return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
754 }
755
756 /***********************************************************************
757  *              CreateDialogIndirectParamA (USER32.@)
758  */
759 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
760                                         HWND owner, DLGPROC dlgProc, LPARAM param )
761 {
762     return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
763 }
764
765 /***********************************************************************
766  *              CreateDialogIndirectParamW (USER32.@)
767  */
768 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
769                                         HWND owner, DLGPROC dlgProc, LPARAM param )
770 {
771     return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
772 }
773
774
775 /***********************************************************************
776  *           DIALOG_DoDialogBox
777  */
778 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
779 {
780     DIALOGINFO * dlgInfo;
781     MSG msg;
782     INT retval;
783     HWND ownerMsg = GetAncestor( owner, GA_ROOT );
784     BOOL bFirstEmpty;
785
786     if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
787
788     bFirstEmpty = TRUE;
789     if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
790     {
791         for (;;)
792         {
793             if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
794             {
795                 if (bFirstEmpty)
796                 {
797                     /* ShowWindow the first time the queue goes empty */
798                     ShowWindow( hwnd, SW_SHOWNORMAL );
799                     bFirstEmpty = FALSE;
800                 }
801                 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
802                {
803                     /* No message present -> send ENTERIDLE and wait */
804                     SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
805                 }
806                 if (!GetMessageW( &msg, 0, 0, 0 )) break;
807             }
808
809             if (!IsWindow( hwnd )) return 0;
810             if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
811             {
812                 TranslateMessage( &msg );
813                 DispatchMessageW( &msg );
814             }
815             if (dlgInfo->flags & DF_END) break;
816         }
817     }
818     if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
819     retval = dlgInfo->idResult;
820     DestroyWindow( hwnd );
821     return retval;
822 }
823
824
825 /***********************************************************************
826  *              DialogBoxParamA (USER32.@)
827  */
828 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
829                                 HWND owner, DLGPROC dlgProc, LPARAM param )
830 {
831     HWND hwnd;
832     HRSRC hrsrc;
833     LPCDLGTEMPLATEA ptr;
834
835     if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
836     if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
837     hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
838     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
839     return 0;
840 }
841
842
843 /***********************************************************************
844  *              DialogBoxParamW (USER32.@)
845  */
846 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
847                                 HWND owner, DLGPROC dlgProc, LPARAM param )
848 {
849     HWND hwnd;
850     HRSRC hrsrc;
851     LPCDLGTEMPLATEW ptr;
852
853     if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
854     if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
855     hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
856     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
857     return 0;
858 }
859
860
861 /***********************************************************************
862  *              DialogBoxIndirectParamAorW (USER32.@)
863  */
864 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
865                                            HWND owner, DLGPROC dlgProc,
866                                            LPARAM param, DWORD flags )
867 {
868     HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
869     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
870     return -1;
871 }
872
873 /***********************************************************************
874  *              DialogBoxIndirectParamA (USER32.@)
875  */
876 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
877                                        HWND owner, DLGPROC dlgProc, LPARAM param )
878 {
879     return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
880 }
881
882
883 /***********************************************************************
884  *              DialogBoxIndirectParamW (USER32.@)
885  */
886 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
887                                        HWND owner, DLGPROC dlgProc, LPARAM param )
888 {
889     return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
890 }
891
892 /***********************************************************************
893  *              EndDialog (USER32.@)
894  */
895 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
896 {
897     BOOL wasEnabled = TRUE;
898     DIALOGINFO * dlgInfo;
899     HWND owner;
900
901     TRACE("%p %ld\n", hwnd, retval );
902
903     if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
904     {
905         ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
906         return FALSE;
907     }
908     dlgInfo->idResult = retval;
909     dlgInfo->flags |= DF_END;
910     wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
911
912     if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
913         DIALOG_EnableOwner( owner );
914
915     /* Windows sets the focus to the dialog itself in EndDialog */
916
917     if (IsChild(hwnd, GetFocus()))
918        SetFocus( hwnd );
919
920     /* Don't have to send a ShowWindow(SW_HIDE), just do
921        SetWindowPos with SWP_HIDEWINDOW as done in Windows */
922
923     SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
924                  | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
925
926     if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
927
928     /* unblock dialog loop */
929     PostMessageA(hwnd, WM_NULL, 0, 0);
930     return TRUE;
931 }
932
933
934 /***********************************************************************
935  *           DIALOG_IsAccelerator
936  */
937 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
938 {
939     HWND hwndControl = hwnd;
940     HWND hwndNext;
941     INT dlgCode;
942     WCHAR buffer[128];
943
944     do
945     {
946         DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
947         if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
948         {
949             dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
950             if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
951                  GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
952             {
953                 /* find the accelerator key */
954                 LPWSTR p = buffer - 2;
955
956                 do
957                 {
958                     p = strchrW( p + 2, '&' );
959                 }
960                 while (p != NULL && p[1] == '&');
961
962                 /* and check if it's the one we're looking for */
963                 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
964                 {
965                     if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
966                     {
967                         /* set focus to the control */
968                         SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
969                         /* and bump it on to next */
970                         SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
971                     }
972                     else if (dlgCode & DLGC_BUTTON)
973                     {
974                         /* send BM_CLICK message to the control */
975                         SendMessageW( hwndControl, BM_CLICK, 0, 0 );
976                     }
977                     return TRUE;
978                 }
979             }
980             hwndNext = GetWindow( hwndControl, GW_CHILD );
981         }
982         else hwndNext = 0;
983
984         if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
985
986         while (!hwndNext && hwndControl)
987         {
988             hwndControl = GetParent( hwndControl );
989             if (hwndControl == hwndDlg)
990             {
991                 if(hwnd==hwndDlg)   /* prevent endless loop */
992                 {
993                     hwndNext=hwnd;
994                     break;
995                 }
996                 hwndNext = GetWindow( hwndDlg, GW_CHILD );
997             }
998             else
999                 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1000         }
1001         hwndControl = hwndNext;
1002     }
1003     while (hwndControl && (hwndControl != hwnd));
1004
1005     return FALSE;
1006 }
1007
1008 /***********************************************************************
1009  *           DIALOG_FindMsgDestination
1010  *
1011  * The messages that IsDialogMessage sends may not go to the dialog
1012  * calling IsDialogMessage if that dialog is a child, and it has the
1013  * DS_CONTROL style set.
1014  * We propagate up until we hit one that does not have DS_CONTROL, or
1015  * whose parent is not a dialog.
1016  *
1017  * This is undocumented behaviour.
1018  */
1019 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1020 {
1021     while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1022     {
1023         WND *pParent;
1024         HWND hParent = GetParent(hwndDlg);
1025         if (!hParent) break;
1026
1027         pParent = WIN_GetPtr(hParent);
1028         if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1029
1030         if (!(pParent->flags & WIN_ISDIALOG))
1031         {
1032             WIN_ReleasePtr(pParent);
1033             break;
1034         }
1035         WIN_ReleasePtr(pParent);
1036
1037         hwndDlg = hParent;
1038     }
1039
1040     return hwndDlg;
1041 }
1042
1043 /***********************************************************************
1044  *              DIALOG_FixOneChildOnChangeFocus
1045  *
1046  * Callback helper for DIALOG_FixChildrenOnChangeFocus
1047  */
1048
1049 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1050         LPARAM lParam)
1051 {
1052     /* If a default pushbutton then no longer default */
1053     if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1054         SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1055     return TRUE;
1056 }
1057
1058 /***********************************************************************
1059  *              DIALOG_FixChildrenOnChangeFocus
1060  *
1061  * Following the change of focus that occurs for example after handling
1062  * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1063  * children may be required.
1064  */
1065 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1066 {
1067     INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1068     /* INT dlgcode_dlg  = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1069     /* Windows does ask for this.  I don't know why yet */
1070
1071     EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1072
1073     /* If the button that is getting the focus WAS flagged as the default
1074      * pushbutton then ask the dialog what it thinks the default is and
1075      * set that in the default style.
1076      */
1077     if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1078     {
1079         DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1080         if (HIWORD(def_id) == DC_HASDEFID)
1081         {
1082             HWND hwndDef;
1083             def_id = LOWORD(def_id);
1084             hwndDef = GetDlgItem (hwndDlg, def_id);
1085             if (hwndDef)
1086             {
1087                 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1088                 /* I know that if it is a button then it should already be a
1089                  * UNDEFPUSHBUTTON, since we have just told the buttons to 
1090                  * change style.  But maybe they ignored our request
1091                  */
1092                 if ((dlgcode_def & DLGC_BUTTON) &&
1093                         (dlgcode_def &  DLGC_UNDEFPUSHBUTTON))
1094                 {
1095                     SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1096                 }
1097             }
1098         }
1099     }
1100     else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1101     {
1102         SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1103         /* I wonder why it doesn't send a DM_SETDEFID */
1104     }
1105 }
1106
1107 /***********************************************************************
1108  *              IsDialogMessageW (USER32.@)
1109  */
1110 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1111 {
1112     INT dlgCode = 0;
1113
1114     if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1115
1116     hwndDlg = WIN_GetFullHandle( hwndDlg );
1117     if (is_desktop_window(hwndDlg)) return FALSE;
1118     if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1119
1120     hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1121
1122     switch(msg->message)
1123     {
1124     case WM_KEYDOWN:
1125         dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1126         if (dlgCode & (DLGC_WANTMESSAGE)) break;
1127
1128         switch(msg->wParam)
1129         {
1130         case VK_TAB:
1131             if (!(dlgCode & DLGC_WANTTAB))
1132             {
1133                 BOOL fIsDialog = TRUE;
1134                 WND *pWnd = WIN_GetPtr( hwndDlg );
1135
1136                 if (pWnd && pWnd != WND_OTHER_PROCESS)
1137                 {
1138                     fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1139                     WIN_ReleasePtr(pWnd);
1140                 }
1141
1142                 /* I am not sure under which circumstances the TAB is handled
1143                  * each way.  All I do know is that it does not always simply
1144                  * send WM_NEXTDLGCTL.  (Personally I have never yet seen it
1145                  * do so but I presume someone has)
1146                  */
1147                 if (fIsDialog)
1148                     SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1149                 else
1150                 {
1151                     /* It would appear that GetNextDlgTabItem can handle being
1152                      * passed hwndDlg rather than NULL but that is undocumented
1153                      * so let's do it properly
1154                      */
1155                     HWND hwndFocus = GetFocus();
1156                     HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1157                             hwndFocus == hwndDlg ? NULL : hwndFocus,
1158                             GetKeyState (VK_SHIFT) & 0x8000);
1159                     if (hwndNext)
1160                     {
1161                         dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1162                                 msg->wParam, (LPARAM)msg);
1163                         if (dlgCode & DLGC_HASSETSEL)
1164                         {
1165                             INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1166                             WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1167                             if (buffer)
1168                             {
1169                                 INT length;
1170                                 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1171                                 length = strlenW (buffer);
1172                                 HeapFree (GetProcessHeap(), 0, buffer);
1173                                 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1174                             }
1175                         }
1176                         SetFocus (hwndNext);
1177                         DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1178                     }
1179                     else
1180                         return FALSE;
1181                 }
1182                 return TRUE;
1183             }
1184             break;
1185
1186         case VK_RIGHT:
1187         case VK_DOWN:
1188         case VK_LEFT:
1189         case VK_UP:
1190             if (!(dlgCode & DLGC_WANTARROWS))
1191             {
1192                 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1193                 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1194                 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1195                 return TRUE;
1196             }
1197             break;
1198
1199         case VK_CANCEL:
1200         case VK_ESCAPE:
1201             SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1202             return TRUE;
1203
1204         case VK_EXECUTE:
1205         case VK_RETURN:
1206             {
1207                 DWORD dw;
1208                 if ((GetFocus() == msg->hwnd) &&
1209                     (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1210                 {
1211                     SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd); 
1212                 }
1213                 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1214                 {
1215                     HWND hwndDef = GetDlgItem(hwndDlg, LOWORD(dw));
1216                     if (!hwndDef || !IsWindowEnabled(hwndDef))
1217                         return TRUE;
1218                     SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1219                                     (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1220                 }
1221                 else
1222                 {
1223                     SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1224
1225                 }
1226             }
1227             return TRUE;
1228         }
1229         break;
1230
1231     case WM_CHAR:
1232         /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1233          * It does NOT get sent in the test program I have
1234          */
1235         dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1236         if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1237         if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1238         /* drop through */
1239
1240     case WM_SYSCHAR:
1241         if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1242         {
1243             /* don't translate or dispatch */
1244             return TRUE;
1245         }
1246         break;
1247     }
1248
1249     TranslateMessage( msg );
1250     DispatchMessageW( msg );
1251     return TRUE;
1252 }
1253
1254
1255 /***********************************************************************
1256  *              GetDlgCtrlID (USER32.@)
1257  */
1258 INT WINAPI GetDlgCtrlID( HWND hwnd )
1259 {
1260     return GetWindowLongPtrW( hwnd, GWLP_ID );
1261 }
1262
1263
1264 /***********************************************************************
1265  *              GetDlgItem (USER32.@)
1266  */
1267 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1268 {
1269     int i;
1270     HWND *list = WIN_ListChildren( hwndDlg );
1271     HWND ret = 0;
1272
1273     if (!list) return 0;
1274
1275     for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1276     ret = list[i];
1277     HeapFree( GetProcessHeap(), 0, list );
1278     return ret;
1279 }
1280
1281
1282 /*******************************************************************
1283  *              SendDlgItemMessageA (USER32.@)
1284  */
1285 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1286                                       WPARAM wParam, LPARAM lParam )
1287 {
1288     HWND hwndCtrl = GetDlgItem( hwnd, id );
1289     if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1290     else return 0;
1291 }
1292
1293
1294 /*******************************************************************
1295  *              SendDlgItemMessageW (USER32.@)
1296  */
1297 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1298                                       WPARAM wParam, LPARAM lParam )
1299 {
1300     HWND hwndCtrl = GetDlgItem( hwnd, id );
1301     if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1302     else return 0;
1303 }
1304
1305
1306 /*******************************************************************
1307  *              SetDlgItemTextA (USER32.@)
1308  */
1309 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1310 {
1311     return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1312 }
1313
1314
1315 /*******************************************************************
1316  *              SetDlgItemTextW (USER32.@)
1317  */
1318 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1319 {
1320     return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1321 }
1322
1323
1324 /***********************************************************************
1325  *              GetDlgItemTextA (USER32.@)
1326  */
1327 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1328 {
1329     if (str && (len > 0)) str[0] = '\0';
1330     return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1331                                          len, (LPARAM)str );
1332 }
1333
1334
1335 /***********************************************************************
1336  *              GetDlgItemTextW (USER32.@)
1337  */
1338 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1339 {
1340     if (str && (len > 0)) str[0] = '\0';
1341     return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1342                                          len, (LPARAM)str );
1343 }
1344
1345
1346 /*******************************************************************
1347  *              SetDlgItemInt (USER32.@)
1348  */
1349 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1350                              BOOL fSigned )
1351 {
1352     char str[20];
1353
1354     if (fSigned) sprintf( str, "%d", (INT)value );
1355     else sprintf( str, "%u", value );
1356     SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1357     return TRUE;
1358 }
1359
1360
1361 /***********************************************************************
1362  *              GetDlgItemInt (USER32.@)
1363  */
1364 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1365                                BOOL fSigned )
1366 {
1367     char str[30];
1368     char * endptr;
1369     long result = 0;
1370
1371     if (translated) *translated = FALSE;
1372     if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1373         return 0;
1374     if (fSigned)
1375     {
1376         result = strtol( str, &endptr, 10 );
1377         if (!endptr || (endptr == str))  /* Conversion was unsuccessful */
1378             return 0;
1379         if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1380             return 0;
1381     }
1382     else
1383     {
1384         result = strtoul( str, &endptr, 10 );
1385         if (!endptr || (endptr == str))  /* Conversion was unsuccessful */
1386             return 0;
1387         if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1388     }
1389     if (translated) *translated = TRUE;
1390     return (UINT)result;
1391 }
1392
1393
1394 /***********************************************************************
1395  *              CheckDlgButton (USER32.@)
1396  */
1397 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1398 {
1399     SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1400     return TRUE;
1401 }
1402
1403
1404 /***********************************************************************
1405  *              IsDlgButtonChecked (USER32.@)
1406  */
1407 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1408 {
1409     return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1410 }
1411
1412
1413 /***********************************************************************
1414  *           CheckRB
1415  *
1416  * Callback function used to check/uncheck radio buttons that fall
1417  * within a specific range of IDs.
1418  */
1419 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1420 {
1421     LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1422     RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1423
1424     if ((lChildID >= lpRadioGroup->firstID) &&
1425         (lChildID <= lpRadioGroup->lastID))
1426     {
1427         if (lChildID == lpRadioGroup->checkID)
1428         {
1429             SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1430         }
1431         else
1432         {
1433             SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1434         }
1435     }
1436
1437     return TRUE;
1438 }
1439
1440
1441 /***********************************************************************
1442  *              CheckRadioButton (USER32.@)
1443  */
1444 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1445                               int lastID, int checkID )
1446 {
1447     RADIOGROUP radioGroup;
1448
1449     radioGroup.firstID = firstID;
1450     radioGroup.lastID = lastID;
1451     radioGroup.checkID = checkID;
1452
1453     return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1454                             (LPARAM)&radioGroup);
1455 }
1456
1457
1458 /***********************************************************************
1459  *              GetDialogBaseUnits (USER.243)
1460  *              GetDialogBaseUnits (USER32.@)
1461  */
1462 DWORD WINAPI GetDialogBaseUnits(void)
1463 {
1464     static DWORD units;
1465
1466     if (!units)
1467     {
1468         HDC hdc;
1469         SIZE size;
1470
1471         if ((hdc = GetDC(0)))
1472         {
1473             size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1474             if (size.cx) units = MAKELONG( size.cx, size.cy );
1475             ReleaseDC( 0, hdc );
1476         }
1477         TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1478     }
1479     return units;
1480 }
1481
1482
1483 /***********************************************************************
1484  *              MapDialogRect (USER32.@)
1485  */
1486 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1487 {
1488     DIALOGINFO * dlgInfo;
1489     if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1490     rect->left   = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1491     rect->right  = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1492     rect->top    = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1493     rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1494     return TRUE;
1495 }
1496
1497
1498 /***********************************************************************
1499  *              GetNextDlgGroupItem (USER32.@)
1500  *
1501  * Corrections to MSDN documentation
1502  *
1503  * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1504  * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1505  * 2. Prev of NULL or hwndDlg fails
1506  */
1507 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1508 {
1509     HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1510     BOOL fLooped=FALSE;
1511     BOOL fSkipping=FALSE;
1512
1513     hwndDlg = WIN_GetFullHandle( hwndDlg );
1514     hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1515
1516     if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1517     if (!hwndCtrl && fPrevious) return 0;
1518
1519     if (hwndCtrl)
1520     {
1521         if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1522     }
1523     else
1524     {
1525         /* No ctrl specified -> start from the beginning */
1526         if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1527         /* MSDN is wrong. fPrevious does not result in the last child */
1528
1529         /* Maybe that first one is valid.  If so then we don't want to skip it*/
1530         if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1531         {
1532             return hwndCtrl;
1533         }
1534     }
1535
1536     /* Always go forward around the group and list of controls; for the 
1537      * previous control keep track; for the next break when you find one
1538      */
1539     retvalue = hwndCtrl;
1540     hwnd = hwndCtrl;
1541     while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1542            1)
1543     {
1544         while (!hwndNext)
1545         {
1546             /* Climb out until there is a next sibling of the ancestor or we
1547              * reach the top (in which case we loop back to the start)
1548              */
1549             if (hwndDlg == GetParent (hwnd))
1550             {
1551                 /* Wrap around to the beginning of the list, within the same
1552                  * group. (Once only)
1553                  */
1554                 if (fLooped) goto end;
1555                 fLooped = TRUE;
1556                 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1557             }
1558             else
1559             {
1560                 hwnd = GetParent (hwnd);
1561                 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1562             }
1563         }
1564         hwnd = hwndNext;
1565
1566         /* Wander down the leading edge of controlparents */
1567         while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1568                 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1569                 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1570             hwnd = hwndNext;
1571         /* Question.  If the control is a control parent but either has no
1572          * children or is not visible/enabled then if it has a WS_GROUP does
1573          * it count?  For that matter does it count anyway?
1574          * I believe it doesn't count.
1575          */
1576
1577         if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1578         {
1579             hwndLastGroup = hwnd;
1580             if (!fSkipping)
1581             {
1582                 /* Look for the beginning of the group */
1583                 fSkipping = TRUE;
1584             }
1585         }
1586
1587         if (hwnd == hwndCtrl)
1588         {
1589             if (!fSkipping) break;
1590             if (hwndLastGroup == hwnd) break;
1591             hwnd = hwndLastGroup;
1592             fSkipping = FALSE;
1593             fLooped = FALSE;
1594         }
1595
1596         if (!fSkipping &&
1597             (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1598              WS_VISIBLE)
1599         {
1600             retvalue = hwnd;
1601             if (!fPrevious) break;
1602         }
1603     }
1604 end:
1605     return retvalue;
1606 }
1607
1608
1609 /***********************************************************************
1610  *           DIALOG_GetNextTabItem
1611  *
1612  * Recursive helper for GetNextDlgTabItem
1613  */
1614 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1615 {
1616     LONG dsStyle;
1617     LONG exStyle;
1618     UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1619     HWND retWnd = 0;
1620     HWND hChildFirst = 0;
1621
1622     if(!hwndCtrl)
1623     {
1624         hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1625         if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1626     }
1627     else if (IsChild( hwndMain, hwndCtrl ))
1628     {
1629         hChildFirst = GetWindow(hwndCtrl,wndSearch);
1630         if(!hChildFirst)
1631         {
1632             if(GetParent(hwndCtrl) != hwndMain)
1633                 /* i.e. if we are not at the top level of the recursion */
1634                 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1635             else
1636                 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1637         }
1638     }
1639
1640     while(hChildFirst)
1641     {
1642         dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1643         exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1644         if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1645         {
1646             HWND retWnd;
1647             retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1648             if (retWnd) return (retWnd);
1649         }
1650         else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1651         {
1652             return (hChildFirst);
1653         }
1654         hChildFirst = GetWindow(hChildFirst,wndSearch);
1655     }
1656     if(hwndCtrl)
1657     {
1658         HWND hParent = GetParent(hwndCtrl);
1659         while(hParent)
1660         {
1661             if(hParent == hwndMain) break;
1662             retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1663             if(retWnd) break;
1664             hParent = GetParent(hParent);
1665         }
1666         if(!retWnd)
1667             retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1668     }
1669     return retWnd ? retWnd : hwndCtrl;
1670 }
1671
1672 /***********************************************************************
1673  *              GetNextDlgTabItem (USER32.@)
1674  */
1675 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1676                                    BOOL fPrevious )
1677 {
1678     hwndDlg = WIN_GetFullHandle( hwndDlg );
1679     hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1680
1681     /* Undocumented but tested under Win2000 and WinME */
1682     if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1683
1684     /* Contrary to MSDN documentation, tested under Win2000 and WinME
1685      * NB GetLastError returns whatever was set before the function was
1686      * called.
1687      */
1688     if (!hwndCtrl && fPrevious) return 0;
1689
1690     return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1691 }
1692
1693 /**********************************************************************
1694  *           DIALOG_DlgDirSelect
1695  *
1696  * Helper function for DlgDirSelect*
1697  */
1698 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1699                                  INT id, BOOL unicode, BOOL combo )
1700 {
1701     WCHAR *buffer, *ptr;
1702     INT item, size;
1703     BOOL ret;
1704     HWND listbox = GetDlgItem( hwnd, id );
1705
1706     TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1707     if (!listbox) return FALSE;
1708
1709     item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1710     if (item == LB_ERR) return FALSE;
1711
1712     size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1713     if (size == LB_ERR) return FALSE;
1714
1715     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1716
1717     SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1718
1719     if ((ret = (buffer[0] == '[')))  /* drive or directory */
1720     {
1721         if (buffer[1] == '-')  /* drive */
1722         {
1723             buffer[3] = ':';
1724             buffer[4] = 0;
1725             ptr = buffer + 2;
1726         }
1727         else
1728         {
1729             buffer[strlenW(buffer)-1] = '\\';
1730             ptr = buffer + 1;
1731         }
1732     }
1733     else
1734     {
1735         /* Filenames without a dot extension must have one tacked at the end */
1736         if (strchrW(buffer, '.') == NULL)
1737         {
1738             buffer[strlenW(buffer)+1] = '\0';
1739             buffer[strlenW(buffer)] = '.';
1740         }
1741         ptr = buffer;
1742     }
1743
1744     if (!unicode)
1745     {
1746         if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1747             ((LPSTR)str)[len-1] = 0;
1748     }
1749     else lstrcpynW( str, ptr, len );
1750     HeapFree( GetProcessHeap(), 0, buffer );
1751     TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1752     return ret;
1753 }
1754
1755
1756 /**********************************************************************
1757  *          DIALOG_DlgDirListW
1758  *
1759  * Helper function for DlgDirList*W
1760  */
1761 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1762                                INT idStatic, UINT attrib, BOOL combo )
1763 {
1764     HWND hwnd;
1765     LPWSTR orig_spec = spec;
1766     WCHAR any[] = {'*','.','*',0};
1767
1768 #define SENDMSG(msg,wparam,lparam) \
1769     ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1770                              : SendMessageW( hwnd, msg, wparam, lparam ))
1771
1772     TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1773
1774     /* If the path exists and is a directory, chdir to it */
1775     if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1776     else
1777     {
1778         WCHAR *p, *p2;
1779         p = spec;
1780         if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1781         if ((p2 = strrchrW( p, '\\' ))) p = p2;
1782         if ((p2 = strrchrW( p, '/' ))) p = p2;
1783         if (p != spec)
1784         {
1785             WCHAR sep = *p;
1786             *p = 0;
1787             if (!SetCurrentDirectoryW( spec ))
1788             {
1789                 *p = sep;  /* Restore the original spec */
1790                 return FALSE;
1791             }
1792             spec = p + 1;
1793         }
1794     }
1795
1796     TRACE( "mask=%s\n", debugstr_w(spec) );
1797
1798     if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1799     {
1800         if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1801
1802         SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1803         if (attrib & DDL_DIRECTORY)
1804         {
1805             if (!(attrib & DDL_EXCLUSIVE))
1806             {
1807                 SENDMSG( combo ? CB_DIR : LB_DIR,
1808                          attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1809                          (LPARAM)spec );
1810             }
1811             SENDMSG( combo ? CB_DIR : LB_DIR,
1812                    (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1813                    (LPARAM)any );
1814         }
1815         else
1816         {
1817             SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1818         }
1819     }
1820
1821     /* Convert path specification to uppercase */
1822     if (spec) CharUpperW(spec);
1823
1824     if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1825     {
1826         WCHAR temp[MAX_PATH];
1827         GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1828         CharLowerW( temp );
1829         /* Can't use PostMessage() here, because the string is on the stack */
1830         SetDlgItemTextW( hDlg, idStatic, temp );
1831     }
1832
1833     if (orig_spec && (spec != orig_spec))
1834     {
1835         /* Update the original file spec */
1836         WCHAR *p = spec;
1837         while ((*orig_spec++ = *p++));
1838     }
1839
1840     return TRUE;
1841 #undef SENDMSG
1842 }
1843
1844
1845 /**********************************************************************
1846  *          DIALOG_DlgDirListA
1847  *
1848  * Helper function for DlgDirList*A
1849  */
1850 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1851                                INT idStatic, UINT attrib, BOOL combo )
1852 {
1853     if (spec)
1854     {
1855         INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1856         LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1857         MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1858         ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1859         WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1860         HeapFree( GetProcessHeap(), 0, specW );
1861         return ret;
1862     }
1863     return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1864 }
1865
1866
1867 /**********************************************************************
1868  *              DlgDirSelectExA (USER32.@)
1869  */
1870 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1871 {
1872     return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1873 }
1874
1875
1876 /**********************************************************************
1877  *              DlgDirSelectExW (USER32.@)
1878  */
1879 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1880 {
1881     return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1882 }
1883
1884
1885 /**********************************************************************
1886  *              DlgDirSelectComboBoxExA (USER32.@)
1887  */
1888 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1889                                          INT id )
1890 {
1891     return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1892 }
1893
1894
1895 /**********************************************************************
1896  *              DlgDirSelectComboBoxExW (USER32.@)
1897  */
1898 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1899                                          INT id)
1900 {
1901     return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1902 }
1903
1904
1905 /**********************************************************************
1906  *              DlgDirListA (USER32.@)
1907  */
1908 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1909                             INT idStatic, UINT attrib )
1910 {
1911     return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1912 }
1913
1914
1915 /**********************************************************************
1916  *              DlgDirListW (USER32.@)
1917  */
1918 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1919                             INT idStatic, UINT attrib )
1920 {
1921     return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1922 }
1923
1924
1925 /**********************************************************************
1926  *              DlgDirListComboBoxA (USER32.@)
1927  */
1928 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1929                                     INT idStatic, UINT attrib )
1930 {
1931     return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1932 }
1933
1934
1935 /**********************************************************************
1936  *              DlgDirListComboBoxW (USER32.@)
1937  */
1938 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1939                                     INT idStatic, UINT attrib )
1940 {
1941     return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1942 }