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