- Rename WIN_Handle16 to HWDN_16 and make it a macro.
[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     HANDLE 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     HANDLE 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  *              CreateDialogIndirectParam16 (USER32.@)
970  */
971 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
972                                            LPCVOID dlgTemplate,
973                                            HWND16 owner, DLGPROC16 dlgProc,
974                                            LPARAM param )
975 {
976     return HWND_16( DIALOG_CreateIndirect( hInst, dlgTemplate, WIN_Handle32(owner),
977                                                 (DLGPROC)dlgProc, param, WIN_PROC_16, FALSE ));
978 }
979
980
981 /***********************************************************************
982  *              CreateDialogIndirectParamA (USER32.@)
983  */
984 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
985                                             LPCVOID dlgTemplate,
986                                             HWND owner, DLGPROC dlgProc,
987                                             LPARAM param )
988 {
989     return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32A, FALSE );
990 }
991
992 /***********************************************************************
993  *              CreateDialogIndirectParamAorW (USER32.@)
994  */
995 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
996                                             LPCVOID dlgTemplate,
997                                             HWND owner, DLGPROC dlgProc,
998                                             LPARAM param )
999 {   FIXME("assume WIN_PROC_32W\n");
1000     return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1001 }
1002
1003 /***********************************************************************
1004  *              CreateDialogIndirectParamW (USER32.@)
1005  */
1006 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1007                                             LPCVOID dlgTemplate,
1008                                             HWND owner, DLGPROC dlgProc,
1009                                             LPARAM param )
1010 {
1011     return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1012 }
1013
1014
1015 /***********************************************************************
1016  *           DIALOG_DoDialogBox
1017  */
1018 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1019 {
1020     DIALOGINFO * dlgInfo;
1021     MSG msg;
1022     INT retval;
1023     HWND ownerMsg = GetAncestor( owner, GA_ROOT );
1024
1025     if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
1026
1027     if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1028     {
1029         ShowWindow( hwnd, SW_SHOW );
1030         for (;;)
1031         {
1032             if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
1033             {
1034                 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1035                 {
1036                     /* No message present -> send ENTERIDLE and wait */
1037                     SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
1038                     if (!GetMessageW( &msg, 0, 0, 0 )) break;
1039                 }
1040             }
1041             else if (!GetMessageW( &msg, 0, 0, 0 )) break;
1042
1043             if (!IsWindow( hwnd )) return -1;
1044             if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
1045             {
1046                 TranslateMessage( &msg );
1047                 DispatchMessageW( &msg );
1048             }
1049             if (dlgInfo->flags & DF_END) break;
1050         }
1051     }
1052     if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
1053     retval = dlgInfo->idResult;
1054     DestroyWindow( hwnd );
1055     return retval;
1056 }
1057
1058
1059 /***********************************************************************
1060  *              DialogBox (USER.87)
1061  */
1062 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1063                           HWND16 owner, DLGPROC16 dlgProc )
1064 {
1065     return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1066 }
1067
1068
1069 /***********************************************************************
1070  *              DialogBoxParam (USER.239)
1071  */
1072 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1073                                HWND16 owner16, DLGPROC16 dlgProc, LPARAM param )
1074 {
1075     HWND hwnd = 0;
1076     HRSRC16 hRsrc;
1077     HGLOBAL16 hmem;
1078     LPCVOID data;
1079     int ret = -1;
1080
1081     if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1082     if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1083     if ((data = LockResource16( hmem )))
1084     {
1085         HWND owner = WIN_Handle32(owner16);
1086         hwnd = DIALOG_CreateIndirect( hInst, data, owner,
1087                                       (DLGPROC)dlgProc, param, WIN_PROC_16, TRUE );
1088         if (hwnd) ret = DIALOG_DoDialogBox( hwnd, owner );
1089         GlobalUnlock16( hmem );
1090     }
1091     FreeResource16( hmem );
1092     return ret;
1093 }
1094
1095
1096 /***********************************************************************
1097  *              DialogBoxParamA (USER32.@)
1098  */
1099 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1100                                 HWND owner, DLGPROC dlgProc, LPARAM param )
1101 {
1102     HWND hwnd;
1103     HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1104     if (!hrsrc) return 0;
1105     hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1106                                   owner, dlgProc, param, WIN_PROC_32A, TRUE );
1107     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1108     return -1;
1109 }
1110
1111
1112 /***********************************************************************
1113  *              DialogBoxParamW (USER32.@)
1114  */
1115 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1116                                 HWND owner, DLGPROC dlgProc, LPARAM param )
1117 {
1118     HWND hwnd;
1119     HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1120     if (!hrsrc) return 0;
1121     hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1122                                   owner, dlgProc, param, WIN_PROC_32W, TRUE );
1123     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1124     return -1;
1125 }
1126
1127
1128 /***********************************************************************
1129  *              DialogBoxIndirect (USER.218)
1130  */
1131 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1132                                   HWND16 owner, DLGPROC16 dlgProc )
1133 {
1134     return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1135 }
1136
1137
1138 /***********************************************************************
1139  *              DialogBoxIndirectParam (USER.240)
1140  *              DialogBoxIndirectParam16 (USER32.@)
1141  */
1142 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1143                                        HWND16 owner16, DLGPROC16 dlgProc,
1144                                        LPARAM param )
1145 {
1146     HWND hwnd, owner = WIN_Handle32( owner16 );
1147     LPCVOID ptr;
1148
1149     if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1150     hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, (DLGPROC)dlgProc,
1151                                   param, WIN_PROC_16, TRUE );
1152     GlobalUnlock16( dlgTemplate );
1153     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1154     return -1;
1155 }
1156
1157
1158 /***********************************************************************
1159  *              DialogBoxIndirectParamA (USER32.@)
1160  */
1161 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1162                                        HWND owner, DLGPROC dlgProc,
1163                                        LPARAM param )
1164 {
1165     HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1166                                        dlgProc, param, WIN_PROC_32A, TRUE );
1167     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1168     return -1;
1169 }
1170
1171
1172 /***********************************************************************
1173  *              DialogBoxIndirectParamW (USER32.@)
1174  */
1175 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1176                                        HWND owner, DLGPROC dlgProc,
1177                                        LPARAM param )
1178 {
1179     HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1180                                        dlgProc, param, WIN_PROC_32W, TRUE );
1181     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1182     return -1;
1183 }
1184
1185 /***********************************************************************
1186  *              DialogBoxIndirectParamAorW (USER32.@)
1187  */
1188 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1189                                        HWND owner, DLGPROC dlgProc,
1190                                        LPARAM param, DWORD x )
1191 {
1192     HWND hwnd;
1193     FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1194       hInstance, template, owner, dlgProc, param, x);
1195     hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, WIN_PROC_32W, TRUE );
1196     if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1197     return -1;
1198 }
1199
1200 /***********************************************************************
1201  *              EndDialog (USER32.@)
1202  */
1203 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1204 {
1205     BOOL wasEnabled = TRUE;
1206     DIALOGINFO * dlgInfo;
1207     HWND owner;
1208
1209     TRACE("%04x %d\n", hwnd, retval );
1210
1211     if (!(dlgInfo = DIALOG_get_info( hwnd )))
1212     {
1213         ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1214         return FALSE;
1215     }
1216     dlgInfo->idResult = retval;
1217     dlgInfo->flags |= DF_END;
1218     wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1219
1220     if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1221         DIALOG_EnableOwner( owner );
1222
1223     /* Windows sets the focus to the dialog itself in EndDialog */
1224
1225     if (IsChild(hwnd, GetFocus()))
1226        SetFocus( hwnd );
1227
1228     /* Don't have to send a ShowWindow(SW_HIDE), just do
1229        SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1230
1231     SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1232                  | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1233
1234     /* unblock dialog loop */
1235     PostMessageA(hwnd, WM_NULL, 0, 0);
1236     return TRUE;
1237 }
1238
1239
1240 /***********************************************************************
1241  *           DIALOG_IsAccelerator
1242  */
1243 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
1244 {
1245     HWND hwndControl = hwnd;
1246     HWND hwndNext;
1247     INT dlgCode;
1248     WCHAR buffer[128];
1249
1250     do
1251     {
1252         DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
1253         if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
1254         {
1255             dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1256             if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1257                  GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
1258             {
1259                 /* find the accelerator key */
1260                 LPWSTR p = buffer - 2;
1261
1262                 do
1263                 {
1264                     p = strchrW( p + 2, '&' );
1265                 }
1266                 while (p != NULL && p[1] == '&');
1267
1268                 /* and check if it's the one we're looking for */
1269                 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
1270                 {
1271                     if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
1272                     {
1273                         /* set focus to the control */
1274                         SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
1275                         /* and bump it on to next */
1276                         SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1277                     }
1278                     else if (dlgCode & DLGC_BUTTON)
1279                     {
1280                         /* send BM_CLICK message to the control */
1281                         SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1282                     }
1283                     return TRUE;
1284                 }
1285             }
1286             hwndNext = GetWindow( hwndControl, GW_CHILD );
1287         }
1288         else hwndNext = 0;
1289
1290         if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1291
1292         while (!hwndNext && hwndControl)
1293         {
1294             hwndControl = GetParent( hwndControl );
1295             if (hwndControl == hwndDlg)
1296             {
1297                 if(hwnd==hwndDlg)   /* prevent endless loop */
1298                 {
1299                     hwndNext=hwnd;
1300                     break;
1301                 }
1302                 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1303             }
1304             else
1305                 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1306         }
1307         hwndControl = hwndNext;
1308     }
1309     while (hwndControl && (hwndControl != hwnd));
1310
1311     return FALSE;
1312 }
1313
1314 /***********************************************************************
1315  *           DIALOG_FindMsgDestination
1316  *
1317  * The messages that IsDialogMessage sends may not go to the dialog
1318  * calling IsDialogMessage if that dialog is a child, and it has the
1319  * DS_CONTROL style set.
1320  * We propagate up until we hit one that does not have DS_CONTROL, or
1321  * whose parent is not a dialog.
1322  *
1323  * This is undocumented behaviour.
1324  */
1325 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1326 {
1327     while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1328     {
1329         WND *pParent;
1330         HWND hParent = GetParent(hwndDlg);
1331         if (!hParent) break;
1332
1333         pParent = WIN_FindWndPtr(hParent);
1334         if (!pParent) break;
1335
1336         if (!(pParent->flags & WIN_ISDIALOG))
1337         {
1338             WIN_ReleaseWndPtr(pParent);
1339             break;
1340         }
1341         WIN_ReleaseWndPtr(pParent);
1342
1343         hwndDlg = hParent;
1344     }
1345
1346     return hwndDlg;
1347 }
1348
1349 /***********************************************************************
1350  *              IsDialogMessageW (USER32.@)
1351  */
1352 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1353 {
1354     INT dlgCode = 0;
1355
1356     if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1357
1358     hwndDlg = WIN_GetFullHandle( hwndDlg );
1359     if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1360
1361     hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1362
1363     switch(msg->message)
1364     {
1365     case WM_KEYDOWN:
1366         dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1367         if (dlgCode & DLGC_WANTMESSAGE) break;
1368
1369         switch(msg->wParam)
1370         {
1371         case VK_TAB:
1372             if (!(dlgCode & DLGC_WANTTAB))
1373             {
1374                 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1375                 return TRUE;
1376             }
1377             break;
1378
1379         case VK_RIGHT:
1380         case VK_DOWN:
1381         case VK_LEFT:
1382         case VK_UP:
1383             if (!(dlgCode & DLGC_WANTARROWS))
1384             {
1385                 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1386                 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1387                 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1388                 return TRUE;
1389             }
1390             break;
1391
1392         case VK_CANCEL:
1393         case VK_ESCAPE:
1394             SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1395             return TRUE;
1396
1397         case VK_EXECUTE:
1398         case VK_RETURN:
1399             {
1400                 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1401                 if (HIWORD(dw) == DC_HASDEFID)
1402                 {
1403                     SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1404                                     (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1405                 }
1406                 else
1407                 {
1408                     SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1409
1410                 }
1411             }
1412             return TRUE;
1413         }
1414         break;
1415
1416     case WM_CHAR:
1417         dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1418         if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1419         if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1420         /* drop through */
1421
1422     case WM_SYSCHAR:
1423         if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1424         {
1425             /* don't translate or dispatch */
1426             return TRUE;
1427         }
1428         break;
1429     }
1430
1431     TranslateMessage( msg );
1432     DispatchMessageW( msg );
1433     return TRUE;
1434 }
1435
1436
1437 /***********************************************************************
1438  *              GetDlgCtrlID (USER32.@)
1439  */
1440 INT WINAPI GetDlgCtrlID( HWND hwnd )
1441 {
1442     return GetWindowLongW( hwnd, GWL_ID );
1443 }
1444
1445
1446 /***********************************************************************
1447  *              GetDlgItem (USER32.@)
1448  */
1449 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1450 {
1451     int i;
1452     HWND *list = WIN_ListChildren( hwndDlg );
1453     HWND ret = 0;
1454
1455     if (!list) return 0;
1456
1457     for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1458     ret = list[i];
1459     HeapFree( GetProcessHeap(), 0, list );
1460     return ret;
1461 }
1462
1463
1464 /*******************************************************************
1465  *              SendDlgItemMessageA (USER32.@)
1466  */
1467 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1468                                       WPARAM wParam, LPARAM lParam )
1469 {
1470     HWND hwndCtrl = GetDlgItem( hwnd, id );
1471     if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1472     else return 0;
1473 }
1474
1475
1476 /*******************************************************************
1477  *              SendDlgItemMessageW (USER32.@)
1478  */
1479 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1480                                       WPARAM wParam, LPARAM lParam )
1481 {
1482     HWND hwndCtrl = GetDlgItem( hwnd, id );
1483     if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1484     else return 0;
1485 }
1486
1487
1488 /*******************************************************************
1489  *              SetDlgItemTextA (USER32.@)
1490  */
1491 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1492 {
1493     return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1494 }
1495
1496
1497 /*******************************************************************
1498  *              SetDlgItemTextW (USER32.@)
1499  */
1500 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1501 {
1502     return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1503 }
1504
1505
1506 /***********************************************************************
1507  *              GetDlgItemTextA (USER32.@)
1508  */
1509 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1510 {
1511     return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1512                                          len, (LPARAM)str );
1513 }
1514
1515
1516 /***********************************************************************
1517  *              GetDlgItemTextW (USER32.@)
1518  */
1519 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1520 {
1521     return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1522                                          len, (LPARAM)str );
1523 }
1524
1525
1526 /*******************************************************************
1527  *              SetDlgItemInt (USER32.@)
1528  */
1529 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1530                              BOOL fSigned )
1531 {
1532     char str[20];
1533
1534     if (fSigned) sprintf( str, "%d", (INT)value );
1535     else sprintf( str, "%u", value );
1536     SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1537     return TRUE;
1538 }
1539
1540
1541 /***********************************************************************
1542  *              GetDlgItemInt (USER32.@)
1543  */
1544 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1545                                BOOL fSigned )
1546 {
1547     char str[30];
1548     char * endptr;
1549     long result = 0;
1550
1551     if (translated) *translated = FALSE;
1552     if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1553         return 0;
1554     if (fSigned)
1555     {
1556         result = strtol( str, &endptr, 10 );
1557         if (!endptr || (endptr == str))  /* Conversion was unsuccessful */
1558             return 0;
1559         if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1560             return 0;
1561     }
1562     else
1563     {
1564         result = strtoul( str, &endptr, 10 );
1565         if (!endptr || (endptr == str))  /* Conversion was unsuccessful */
1566             return 0;
1567         if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1568     }
1569     if (translated) *translated = TRUE;
1570     return (UINT)result;
1571 }
1572
1573
1574 /***********************************************************************
1575  *              CheckDlgButton (USER32.@)
1576  */
1577 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1578 {
1579     SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1580     return TRUE;
1581 }
1582
1583
1584 /***********************************************************************
1585  *              IsDlgButtonChecked (USER32.@)
1586  */
1587 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1588 {
1589     return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1590 }
1591
1592
1593 /***********************************************************************
1594  *           CheckRB
1595  *
1596  * Callback function used to check/uncheck radio buttons that fall
1597  * within a specific range of IDs.
1598  */
1599 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1600 {
1601     LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1602     RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1603
1604     if ((lChildID >= lpRadioGroup->firstID) &&
1605         (lChildID <= lpRadioGroup->lastID))
1606     {
1607         if (lChildID == lpRadioGroup->checkID)
1608         {
1609             SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1610         }
1611         else
1612         {
1613             SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1614         }
1615     }
1616
1617     return TRUE;
1618 }
1619
1620
1621 /***********************************************************************
1622  *              CheckRadioButton (USER32.@)
1623  */
1624 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1625                               UINT lastID, UINT checkID )
1626 {
1627     RADIOGROUP radioGroup;
1628
1629     /* perform bounds checking for a radio button group */
1630     radioGroup.firstID = min(min(firstID, lastID), checkID);
1631     radioGroup.lastID = max(max(firstID, lastID), checkID);
1632     radioGroup.checkID = checkID;
1633
1634     return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1635                             (LPARAM)&radioGroup);
1636 }
1637
1638
1639 /***********************************************************************
1640  *              GetDialogBaseUnits (USER.243)
1641  *              GetDialogBaseUnits (USER32.@)
1642  */
1643 DWORD WINAPI GetDialogBaseUnits(void)
1644 {
1645     return MAKELONG( xBaseUnit, yBaseUnit );
1646 }
1647
1648
1649 /***********************************************************************
1650  *              MapDialogRect (USER32.@)
1651  */
1652 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1653 {
1654     DIALOGINFO * dlgInfo;
1655     if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1656     rect->left   = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1657     rect->right  = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1658     rect->top    = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1659     rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1660     return TRUE;
1661 }
1662
1663
1664 /***********************************************************************
1665  *              GetNextDlgGroupItem (USER32.@)
1666  */
1667 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1668 {
1669     HWND hwnd, retvalue;
1670
1671     hwndDlg = WIN_GetFullHandle( hwndDlg );
1672     hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1673
1674     if(hwndCtrl)
1675     {
1676         /* if the hwndCtrl is the child of the control in the hwndDlg,
1677          * then the hwndDlg has to be the parent of the hwndCtrl */
1678         if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1679             hwndDlg = GetParent(hwndCtrl);
1680     }
1681
1682     if (hwndCtrl)
1683     {
1684         /* Make sure hwndCtrl is a top-level child */
1685         HWND parent = GetParent( hwndCtrl );
1686         while (parent && parent != hwndDlg) parent = GetParent(parent);
1687         if (parent != hwndDlg) return 0;
1688     }
1689     else
1690     {
1691         /* No ctrl specified -> start from the beginning */
1692         if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1693         if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1694     }
1695
1696     retvalue = hwndCtrl;
1697     hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1698     while (1)
1699     {
1700         if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1701         {
1702             /* Wrap-around to the beginning of the group */
1703             HWND tmp;
1704
1705             hwnd = GetWindow( hwndDlg, GW_CHILD );
1706             for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1707             {
1708                 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1709                 if (tmp == hwndCtrl) break;
1710             }
1711         }
1712         if (hwnd == hwndCtrl) break;
1713         if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1714         {
1715             retvalue = hwnd;
1716             if (!fPrevious) break;
1717         }
1718         hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1719     }
1720     return retvalue;
1721 }
1722
1723
1724 /***********************************************************************
1725  *           DIALOG_GetNextTabItem
1726  *
1727  * Helper for GetNextDlgTabItem
1728  */
1729 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1730 {
1731     LONG dsStyle;
1732     LONG exStyle;
1733     UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1734     HWND retWnd = 0;
1735     HWND hChildFirst = 0;
1736
1737     if(!hwndCtrl)
1738     {
1739         hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1740         if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1741     }
1742     else if (IsChild( hwndMain, hwndCtrl ))
1743     {
1744         hChildFirst = GetWindow(hwndCtrl,wndSearch);
1745         if(!hChildFirst)
1746         {
1747             if(GetParent(hwndCtrl) != hwndMain)
1748                 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1749             else
1750             {
1751                 if(fPrevious)
1752                     hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1753                 else
1754                     hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1755             }
1756         }
1757     }
1758
1759     while(hChildFirst)
1760     {
1761         BOOL bCtrl = FALSE;
1762         while(hChildFirst)
1763         {
1764             dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1765             exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1766             if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1767             {
1768                 bCtrl=TRUE;
1769                 break;
1770             }
1771             else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1772                 break;
1773             hChildFirst = GetWindow(hChildFirst,wndSearch);
1774         }
1775         if(hChildFirst)
1776         {
1777             if(bCtrl)
1778                 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
1779             else
1780                 retWnd = hChildFirst;
1781         }
1782         if(retWnd) break;
1783         hChildFirst = GetWindow(hChildFirst,wndSearch);
1784     }
1785     if(!retWnd && hwndCtrl)
1786     {
1787         HWND hParent = GetParent(hwndCtrl);
1788         while(hParent)
1789         {
1790             if(hParent == hwndMain) break;
1791             retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1792             if(retWnd) break;
1793             hParent = GetParent(hParent);
1794         }
1795         if(!retWnd)
1796             retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
1797     }
1798     return retWnd;
1799 }
1800
1801 /***********************************************************************
1802  *              GetNextDlgTabItem (USER32.@)
1803  */
1804 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1805                                    BOOL fPrevious )
1806 {
1807     hwndDlg = WIN_GetFullHandle( hwndDlg );
1808     hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1809     return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1810 }
1811
1812 /**********************************************************************
1813  *           DIALOG_DlgDirSelect
1814  *
1815  * Helper function for DlgDirSelect*
1816  */
1817 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1818                                  INT id, BOOL unicode, BOOL combo )
1819 {
1820     char *buffer, *ptr;
1821     INT item, size;
1822     BOOL ret;
1823     HWND listbox = GetDlgItem( hwnd, id );
1824
1825     TRACE("%04x '%s' %d\n", hwnd, str, id );
1826     if (!listbox) return FALSE;
1827
1828     item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1829     if (item == LB_ERR) return FALSE;
1830     size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1831     if (size == LB_ERR) return FALSE;
1832
1833     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1834
1835     SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1836
1837     if ((ret = (buffer[0] == '[')))  /* drive or directory */
1838     {
1839         if (buffer[1] == '-')  /* drive */
1840         {
1841             buffer[3] = ':';
1842             buffer[4] = 0;
1843             ptr = buffer + 2;
1844         }
1845         else
1846         {
1847             buffer[strlen(buffer)-1] = '\\';
1848             ptr = buffer + 1;
1849         }
1850     }
1851     else ptr = buffer;
1852
1853     if (unicode)
1854     {
1855         if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1856             ((LPWSTR)str)[len-1] = 0;
1857     }
1858     else lstrcpynA( str, ptr, len );
1859     HeapFree( GetProcessHeap(), 0, buffer );
1860     TRACE("Returning %d '%s'\n", ret, str );
1861     return ret;
1862 }
1863
1864
1865 /**********************************************************************
1866  *          DIALOG_DlgDirList
1867  *
1868  * Helper function for DlgDirList*
1869  */
1870 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
1871                                 INT idStatic, UINT attrib, BOOL combo )
1872 {
1873     HWND hwnd;
1874     LPSTR orig_spec = spec;
1875
1876 #define SENDMSG(msg,wparam,lparam) \
1877     ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
1878                              : SendMessageA( hwnd, msg, wparam, lparam ))
1879
1880     TRACE("%04x '%s' %d %d %04x\n",
1881                     hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1882
1883     /* If the path exists and is a directory, chdir to it */
1884     if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
1885     else
1886     {
1887         char *p, *p2;
1888         p = spec;
1889         if ((p2 = strrchr( p, '\\' ))) p = p2;
1890         if ((p2 = strrchr( p, '/' ))) p = p2;
1891         if (p != spec)
1892         {
1893             char sep = *p;
1894             *p = 0;
1895             if (!SetCurrentDirectoryA( spec ))
1896             {
1897                 *p = sep;  /* Restore the original spec */
1898                 return FALSE;
1899             }
1900             spec = p + 1;
1901         }
1902     }
1903
1904     TRACE( "mask=%s\n", spec );
1905
1906     if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1907     {
1908         SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1909         if (attrib & DDL_DIRECTORY)
1910         {
1911             if (!(attrib & DDL_EXCLUSIVE))
1912             {
1913                 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1914                              attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1915                              (LPARAM)spec ) == LB_ERR)
1916                     return FALSE;
1917             }
1918             if (SENDMSG( combo ? CB_DIR : LB_DIR,
1919                        (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1920                          (LPARAM)"*.*" ) == LB_ERR)
1921                 return FALSE;
1922         }
1923         else
1924         {
1925             if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1926                          (LPARAM)spec ) == LB_ERR)
1927                 return FALSE;
1928         }
1929     }
1930
1931     if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1932     {
1933         char temp[MAX_PATH];
1934         GetCurrentDirectoryA( sizeof(temp), temp );
1935         CharLowerA( temp );
1936         /* Can't use PostMessage() here, because the string is on the stack */
1937         SetDlgItemTextA( hDlg, idStatic, temp );
1938     }
1939
1940     if (orig_spec && (spec != orig_spec))
1941     {
1942         /* Update the original file spec */
1943         char *p = spec;
1944         while ((*orig_spec++ = *p++));
1945     }
1946
1947     return TRUE;
1948 #undef SENDMSG
1949 }
1950
1951
1952 /**********************************************************************
1953  *          DIALOG_DlgDirListW
1954  *
1955  * Helper function for DlgDirList*W
1956  */
1957 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1958                                  INT idStatic, UINT attrib, BOOL combo )
1959 {
1960     if (spec)
1961     {
1962         LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
1963         INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
1964                                        attrib, combo );
1965         MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
1966         HeapFree( GetProcessHeap(), 0, specA );
1967         return ret;
1968     }
1969     return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
1970 }
1971
1972
1973 /**********************************************************************
1974  *              DlgDirSelectExA (USER32.@)
1975  */
1976 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1977 {
1978     return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1979 }
1980
1981
1982 /**********************************************************************
1983  *              DlgDirSelectExW (USER32.@)
1984  */
1985 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1986 {
1987     return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1988 }
1989
1990
1991 /**********************************************************************
1992  *              DlgDirSelectComboBoxExA (USER32.@)
1993  */
1994 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1995                                          INT id )
1996 {
1997     return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1998 }
1999
2000
2001 /**********************************************************************
2002  *              DlgDirSelectComboBoxExW (USER32.@)
2003  */
2004 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2005                                          INT id)
2006 {
2007     return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
2008 }
2009
2010
2011 /**********************************************************************
2012  *              DlgDirListA (USER32.@)
2013  */
2014 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2015                             INT idStatic, UINT attrib )
2016 {
2017     return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2018 }
2019
2020
2021 /**********************************************************************
2022  *              DlgDirListW (USER32.@)
2023  */
2024 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2025                             INT idStatic, UINT attrib )
2026 {
2027     return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2028 }
2029
2030
2031 /**********************************************************************
2032  *              DlgDirListComboBoxA (USER32.@)
2033  */
2034 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2035                                     INT idStatic, UINT attrib )
2036 {
2037     return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2038 }
2039
2040
2041 /**********************************************************************
2042  *              DlgDirListComboBoxW (USER32.@)
2043  */
2044 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2045                                     INT idStatic, UINT attrib )
2046 {
2047     return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2048 }