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