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