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