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