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