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