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