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