Check the value returned by DIALOG_GetCharSize.
[wine.git] / windows / dialog.c
blobe3a72aeafd2d52106f177db14394c68ffa62f195
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
5 */
7 #include <ctype.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "windef.h"
14 #include "wingdi.h"
15 #include "winuser.h"
16 #include "windowsx.h"
17 #include "wine/winuser16.h"
18 #include "wine/winbase16.h"
19 #include "dialog.h"
20 #include "drive.h"
21 #include "heap.h"
22 #include "win.h"
23 #include "ldt.h"
24 #include "user.h"
25 #include "winproc.h"
26 #include "message.h"
27 #include "queue.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(dialog);
33 /* Dialog control information */
34 typedef struct
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;
49 /* Dialog template */
50 typedef struct
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;
70 /* Radio button group */
71 typedef struct
73 UINT firstID;
74 UINT lastID;
75 UINT checkID;
76 } RADIOGROUP;
78 /* Dialog base units */
79 static WORD xBaseUnit = 0, yBaseUnit = 0;
81 /***********************************************************************
82 * DIALOG_GetCharSizeFromDC
85 * Calculates the *true* average size of English characters in the
86 * specified font as oppposed to the one returned by GetTextMetrics.
88 * Latest: the X font driver will now compute a proper average width
89 * so this code can be removed
91 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
93 BOOL Success = FALSE;
94 HFONT hFontPrev = 0;
95 pSize->cx = xBaseUnit;
96 pSize->cy = yBaseUnit;
97 if ( hDC )
99 /* select the font */
100 TEXTMETRICA tm;
101 memset(&tm,0,sizeof(tm));
102 if (hFont) hFontPrev = SelectFont(hDC,hFont);
103 if (GetTextMetricsA(hDC,&tm))
105 pSize->cx = tm.tmAveCharWidth;
106 pSize->cy = tm.tmHeight;
108 /* if variable width font */
109 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
111 SIZE total;
112 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
114 /* Calculate a true average as opposed to the one returned
115 * by tmAveCharWidth. This works better when dealing with
116 * proportional spaced fonts and (more important) that's
117 * how Microsoft's dialog creation code calculates the size
118 * of the font
120 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
122 /* round up */
123 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
124 Success = TRUE;
127 else
129 Success = TRUE;
131 /* Use the text metrics */
132 TRACE("Using tm: %ldx%ld (dlg: %dx%d) (%s)\n", tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
133 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
134 pSize->cx = tm.tmAveCharWidth;
135 pSize->cy = tm.tmHeight;
137 /* select the original font */
138 if (hFontPrev) SelectFont(hDC,hFontPrev);
140 return (Success);
143 /***********************************************************************
144 * DIALOG_GetCharSize
146 * A convenient variant of DIALOG_GetCharSizeFromDC.
148 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
150 HDC hDC = GetDC(0);
151 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
152 ReleaseDC(0, hDC);
153 return Success;
156 /***********************************************************************
157 * DIALOG_Init
159 * Initialisation of the dialog manager.
161 BOOL DIALOG_Init(void)
163 HDC16 hdc;
164 SIZE size;
166 /* Calculate the dialog base units */
168 if (!(hdc = CreateDC16( "DISPLAY", NULL, NULL, NULL ))) return FALSE;
169 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size )) return FALSE;
170 DeleteDC( hdc );
171 xBaseUnit = size.cx;
172 yBaseUnit = size.cy;
174 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
175 return TRUE;
179 /***********************************************************************
180 * DIALOG_GetControl16
182 * Return the class and text of the control pointed to by ptr,
183 * fill the header structure and return a pointer to the next control.
185 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
187 static char buffer[10];
188 int int_id;
190 info->x = GET_WORD(p); p += sizeof(WORD);
191 info->y = GET_WORD(p); p += sizeof(WORD);
192 info->cx = GET_WORD(p); p += sizeof(WORD);
193 info->cy = GET_WORD(p); p += sizeof(WORD);
194 info->id = GET_WORD(p); p += sizeof(WORD);
195 info->style = GET_DWORD(p); p += sizeof(DWORD);
196 info->exStyle = 0;
198 if (*p & 0x80)
200 switch((BYTE)*p)
202 case 0x80: strcpy( buffer, "BUTTON" ); break;
203 case 0x81: strcpy( buffer, "EDIT" ); break;
204 case 0x82: strcpy( buffer, "STATIC" ); break;
205 case 0x83: strcpy( buffer, "LISTBOX" ); break;
206 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
207 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
208 default: buffer[0] = '\0'; break;
210 info->className = buffer;
211 p++;
213 else
215 info->className = p;
216 p += strlen(p) + 1;
219 int_id = ((BYTE)*p == 0xff);
220 if (int_id)
222 /* Integer id, not documented (?). Only works for SS_ICON controls */
223 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
224 p += 3;
226 else
228 info->windowName = p;
229 p += strlen(p) + 1;
232 if (*p)
234 /* Additional CTLDATA available for this control. */
235 info->data = SEGPTR_ALLOC(*p);
236 memcpy( info->data, p + 1, *p );
238 else info->data = NULL;
240 p += *p + 1;
242 if(int_id)
243 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
244 info->className, LOWORD(info->windowName),
245 info->id, info->x, info->y, info->cx, info->cy,
246 info->style, (DWORD)SEGPTR_GET(info->data) );
247 else
248 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
249 info->className, info->windowName,
250 info->id, info->x, info->y, info->cx, info->cy,
251 info->style, (DWORD)SEGPTR_GET(info->data) );
253 return p;
257 /***********************************************************************
258 * DIALOG_GetControl32
260 * Return the class and text of the control pointed to by ptr,
261 * fill the header structure and return a pointer to the next control.
263 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
264 BOOL dialogEx )
266 if (dialogEx)
268 info->helpId = GET_DWORD(p); p += 2;
269 info->exStyle = GET_DWORD(p); p += 2;
270 info->style = GET_DWORD(p); p += 2;
272 else
274 info->helpId = 0;
275 info->style = GET_DWORD(p); p += 2;
276 info->exStyle = GET_DWORD(p); p += 2;
278 info->x = GET_WORD(p); p++;
279 info->y = GET_WORD(p); p++;
280 info->cx = GET_WORD(p); p++;
281 info->cy = GET_WORD(p); p++;
283 if (dialogEx)
285 /* id is a DWORD for DIALOGEX */
286 info->id = GET_DWORD(p);
287 p += 2;
289 else
291 info->id = GET_WORD(p);
292 p++;
295 if (GET_WORD(p) == 0xffff)
297 static const WCHAR class_names[6][10] =
299 { 'B','u','t','t','o','n', }, /* 0x80 */
300 { 'E','d','i','t', }, /* 0x81 */
301 { 'S','t','a','t','i','c', }, /* 0x82 */
302 { 'L','i','s','t','B','o','x', }, /* 0x83 */
303 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
304 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
306 WORD id = GET_WORD(p+1);
307 if ((id >= 0x80) && (id <= 0x85))
308 info->className = (LPCSTR)class_names[id - 0x80];
309 else
311 info->className = NULL;
312 ERR("Unknown built-in class id %04x\n", id );
314 p += 2;
316 else
318 info->className = (LPCSTR)p;
319 p += lstrlenW( (LPCWSTR)p ) + 1;
322 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
324 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
325 p += 2;
327 else
329 info->windowName = (LPCSTR)p;
330 p += lstrlenW( (LPCWSTR)p ) + 1;
333 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
334 debugstr_w( (LPCWSTR)info->className ),
335 debugres_w( (LPCWSTR)info->windowName ),
336 info->id, info->x, info->y, info->cx, info->cy,
337 info->style, info->exStyle, info->helpId );
339 if (GET_WORD(p))
341 if (TRACE_ON(dialog))
343 WORD i, count = GET_WORD(p) / sizeof(WORD);
344 TRACE(" BEGIN\n");
345 TRACE(" ");
346 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
347 DPRINTF("\n");
348 TRACE(" END\n" );
350 info->data = (LPVOID)(p + 1);
351 p += GET_WORD(p) / sizeof(WORD);
353 else info->data = NULL;
354 p++;
356 /* Next control is on dword boundary */
357 return (const WORD *)((((int)p) + 3) & ~3);
361 /***********************************************************************
362 * DIALOG_CreateControls
364 * Create the control windows for a dialog.
366 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
367 const DLG_TEMPLATE *dlgTemplate,
368 HINSTANCE hInst, BOOL win32 )
370 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
371 DLG_CONTROL_INFO info;
372 HWND hwndCtrl, hwndDefButton = 0;
373 INT items = dlgTemplate->nbItems;
375 TRACE(" BEGIN\n" );
376 while (items--)
378 if (!win32)
380 HINSTANCE16 instance;
381 template = DIALOG_GetControl16( template, &info );
382 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
383 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
385 if (!dlgInfo->hDialogHeap)
387 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
388 if (!dlgInfo->hDialogHeap)
390 ERR("Insufficient memory to create heap for edit control\n" );
391 continue;
393 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
395 instance = dlgInfo->hDialogHeap;
397 else instance = (HINSTANCE16)hInst;
399 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
400 info.className, info.windowName,
401 info.style | WS_CHILD,
402 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
403 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
404 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
405 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
406 pWnd->hwndSelf, (HMENU16)info.id,
407 instance, (LPVOID)SEGPTR_GET(info.data) );
409 if (info.data) SEGPTR_FREE(info.data);
411 else
413 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
414 dlgTemplate->dialogEx );
415 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
416 (LPCWSTR)info.className,
417 (LPCWSTR)info.windowName,
418 info.style | WS_CHILD,
419 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
420 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
421 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
422 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
423 pWnd->hwndSelf, (HMENU)info.id,
424 hInst, info.data );
426 if (!hwndCtrl) return FALSE;
428 /* Send initialisation messages to the control */
429 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
430 (WPARAM)dlgInfo->hUserFont, 0 );
431 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
433 /* If there's already a default push-button, set it back */
434 /* to normal and use this one instead. */
435 if (hwndDefButton)
436 SendMessageA( hwndDefButton, BM_SETSTYLE,
437 BS_PUSHBUTTON,FALSE );
438 hwndDefButton = hwndCtrl;
439 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
442 TRACE(" END\n" );
443 return TRUE;
447 /***********************************************************************
448 * DIALOG_ParseTemplate16
450 * Fill a DLG_TEMPLATE structure from the dialog template, and return
451 * a pointer to the first control.
453 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
455 result->style = GET_DWORD(p); p += sizeof(DWORD);
456 result->exStyle = 0;
457 result->nbItems = (unsigned char) *p++;
458 result->x = GET_WORD(p); p += sizeof(WORD);
459 result->y = GET_WORD(p); p += sizeof(WORD);
460 result->cx = GET_WORD(p); p += sizeof(WORD);
461 result->cy = GET_WORD(p); p += sizeof(WORD);
462 TRACE("DIALOG %d, %d, %d, %d\n",
463 result->x, result->y, result->cx, result->cy );
464 TRACE(" STYLE %08lx\n", result->style );
466 /* Get the menu name */
468 switch( (BYTE)*p )
470 case 0:
471 result->menuName = 0;
472 p++;
473 break;
474 case 0xff:
475 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
476 p += 3;
477 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
478 break;
479 default:
480 result->menuName = p;
481 TRACE(" MENU '%s'\n", p );
482 p += strlen(p) + 1;
483 break;
486 /* Get the class name */
488 if (*p)
490 result->className = p;
491 TRACE(" CLASS '%s'\n", result->className );
493 else result->className = DIALOG_CLASS_ATOM;
494 p += strlen(p) + 1;
496 /* Get the window caption */
498 result->caption = p;
499 p += strlen(p) + 1;
500 TRACE(" CAPTION '%s'\n", result->caption );
502 /* Get the font name */
504 if (result->style & DS_SETFONT)
506 result->pointSize = GET_WORD(p);
507 p += sizeof(WORD);
508 result->faceName = p;
509 p += strlen(p) + 1;
510 TRACE(" FONT %d,'%s'\n",
511 result->pointSize, result->faceName );
513 return p;
517 /***********************************************************************
518 * DIALOG_ParseTemplate32
520 * Fill a DLG_TEMPLATE structure from the dialog template, and return
521 * a pointer to the first control.
523 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
525 const WORD *p = (const WORD *)template;
527 result->style = GET_DWORD(p); p += 2;
528 if (result->style == 0xffff0001) /* DIALOGEX resource */
530 result->dialogEx = TRUE;
531 result->helpId = GET_DWORD(p); p += 2;
532 result->exStyle = GET_DWORD(p); p += 2;
533 result->style = GET_DWORD(p); p += 2;
535 else
537 result->dialogEx = FALSE;
538 result->helpId = 0;
539 result->exStyle = GET_DWORD(p); p += 2;
541 result->nbItems = GET_WORD(p); p++;
542 result->x = GET_WORD(p); p++;
543 result->y = GET_WORD(p); p++;
544 result->cx = GET_WORD(p); p++;
545 result->cy = GET_WORD(p); p++;
546 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
547 result->dialogEx ? "EX" : "", result->x, result->y,
548 result->cx, result->cy, result->helpId );
549 TRACE(" STYLE 0x%08lx\n", result->style );
550 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
552 /* Get the menu name */
554 switch(GET_WORD(p))
556 case 0x0000:
557 result->menuName = NULL;
558 p++;
559 break;
560 case 0xffff:
561 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
562 p += 2;
563 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
564 break;
565 default:
566 result->menuName = (LPCSTR)p;
567 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
568 p += lstrlenW( (LPCWSTR)p ) + 1;
569 break;
572 /* Get the class name */
574 switch(GET_WORD(p))
576 case 0x0000:
577 result->className = DIALOG_CLASS_ATOM;
578 p++;
579 break;
580 case 0xffff:
581 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
582 p += 2;
583 TRACE(" CLASS %04x\n", LOWORD(result->className) );
584 break;
585 default:
586 result->className = (LPCSTR)p;
587 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
588 p += lstrlenW( (LPCWSTR)p ) + 1;
589 break;
592 /* Get the window caption */
594 result->caption = (LPCSTR)p;
595 p += lstrlenW( (LPCWSTR)p ) + 1;
596 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
598 /* Get the font name */
600 if (result->style & DS_SETFONT)
602 result->pointSize = GET_WORD(p);
603 p++;
604 if (result->dialogEx)
606 result->weight = GET_WORD(p); p++;
607 result->italic = LOBYTE(GET_WORD(p)); p++;
609 else
611 result->weight = FW_DONTCARE;
612 result->italic = FALSE;
614 result->faceName = (LPCSTR)p;
615 p += lstrlenW( (LPCWSTR)p ) + 1;
616 TRACE(" FONT %d, %s, %d, %s\n",
617 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
618 result->weight, result->italic ? "TRUE" : "FALSE" );
621 /* First control is on dword boundary */
622 return (LPCSTR)((((int)p) + 3) & ~3);
626 /***********************************************************************
627 * DIALOG_CreateIndirect
629 HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
630 BOOL win32Template, HWND owner,
631 DLGPROC16 dlgProc, LPARAM param,
632 WINDOWPROCTYPE procType )
634 HMENU16 hMenu = 0;
635 HFONT16 hFont = 0;
636 HWND hwnd;
637 RECT rect;
638 WND * wndPtr;
639 DLG_TEMPLATE template;
640 DIALOGINFO * dlgInfo;
641 WORD xUnit = xBaseUnit;
642 WORD yUnit = yBaseUnit;
644 /* Parse dialog template */
646 if (!dlgTemplate) return 0;
647 if (win32Template)
648 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
649 else
650 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
652 /* Load menu */
654 if (template.menuName)
656 if (!win32Template)
658 LPSTR str = SEGPTR_STRDUP( template.menuName );
659 hMenu = LoadMenu16( hInst, SEGPTR_GET(str) );
660 SEGPTR_FREE( str );
662 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
665 /* Create custom font if needed */
667 if (template.style & DS_SETFONT)
669 /* The font height must be negative as it is a point size */
670 /* and must be converted to pixels first */
671 /* (see CreateFont() documentation in the Windows SDK). */
672 HDC dc = GetDC(0);
673 int pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
674 ReleaseDC(0, dc);
676 if (win32Template)
677 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
678 template.italic, FALSE, FALSE,
679 DEFAULT_CHARSET, 0, 0,
680 PROOF_QUALITY, FF_DONTCARE,
681 (LPCWSTR)template.faceName );
682 else
683 hFont = CreateFont16( -pixels, 0, 0, 0, FW_DONTCARE,
684 FALSE, FALSE, FALSE,
685 DEFAULT_CHARSET, 0, 0,
686 PROOF_QUALITY, FF_DONTCARE,
687 template.faceName );
688 if (hFont)
690 SIZE charSize;
691 if (DIALOG_GetCharSize(hFont,&charSize))
693 xUnit = charSize.cx;
694 yUnit = charSize.cy;
697 TRACE("units = %d,%d\n", xUnit, yUnit );
700 /* Create dialog main window */
702 rect.left = rect.top = 0;
703 rect.right = MulDiv(template.cx, xUnit, 4);
704 rect.bottom = MulDiv(template.cy, yUnit, 8);
705 if (template.style & DS_MODALFRAME)
706 template.exStyle |= WS_EX_DLGMODALFRAME;
707 AdjustWindowRectEx( &rect, template.style,
708 hMenu ? TRUE : FALSE , template.exStyle );
709 rect.right -= rect.left;
710 rect.bottom -= rect.top;
712 if ((INT16)template.x == CW_USEDEFAULT16)
714 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
716 else
718 if (template.style & DS_CENTER)
720 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
721 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
723 else
725 rect.left += MulDiv(template.x, xUnit, 4);
726 rect.top += MulDiv(template.y, yUnit, 8);
728 if ( !(template.style & WS_CHILD) )
730 INT16 dX, dY;
732 if( !(template.style & DS_ABSALIGN) )
733 ClientToScreen( owner, (POINT *)&rect );
735 /* try to fit it into the desktop */
737 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
738 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
739 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
740 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
741 if( rect.left < 0 ) rect.left = 0;
742 if( rect.top < 0 ) rect.top = 0;
746 if (!win32Template)
747 hwnd = CreateWindowEx16(template.exStyle, template.className,
748 template.caption, template.style & ~WS_VISIBLE,
749 rect.left, rect.top, rect.right, rect.bottom,
750 owner, hMenu, hInst, NULL );
751 else
752 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
753 (LPCWSTR)template.caption,
754 template.style & ~WS_VISIBLE,
755 rect.left, rect.top, rect.right, rect.bottom,
756 owner, hMenu, hInst, NULL );
758 if (!hwnd)
760 if (hFont) DeleteObject( hFont );
761 if (hMenu) DestroyMenu( hMenu );
762 return 0;
764 wndPtr = WIN_FindWndPtr( hwnd );
765 wndPtr->flags |= WIN_ISDIALOG;
766 wndPtr->helpContext = template.helpId;
768 /* Initialise dialog extra data */
770 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
771 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
772 dlgInfo->hUserFont = hFont;
773 dlgInfo->hMenu = hMenu;
774 dlgInfo->xBaseUnit = xUnit;
775 dlgInfo->yBaseUnit = yUnit;
776 dlgInfo->msgResult = 0;
777 dlgInfo->idResult = 0;
778 dlgInfo->flags = 0;
779 dlgInfo->hDialogHeap = 0;
781 if (dlgInfo->hUserFont)
782 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
784 /* Create controls */
786 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
787 hInst, win32Template ))
789 HWND hwndPreInitFocus;
791 /* Send initialisation messages and set focus */
793 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
795 hwndPreInitFocus = GetFocus();
796 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
798 /* check where the focus is again, some controls status might have changed in
799 WM_INITDIALOG */
800 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
801 SetFocus( dlgInfo->hwndFocus );
803 else
805 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
806 but the focus has not changed, set the focus where we expect it. */
807 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
809 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
810 SetFocus( dlgInfo->hwndFocus );
814 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
816 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
817 UpdateWindow( hwnd );
819 WIN_ReleaseWndPtr(wndPtr);
820 return hwnd;
822 WIN_ReleaseWndPtr(wndPtr);
823 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
824 return 0;
828 /***********************************************************************
829 * CreateDialog16 (USER.89)
831 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
832 HWND16 owner, DLGPROC16 dlgProc )
834 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
838 /***********************************************************************
839 * CreateDialogParam16 (USER.241)
841 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
842 HWND16 owner, DLGPROC16 dlgProc,
843 LPARAM param )
845 HWND16 hwnd = 0;
846 HRSRC16 hRsrc;
847 HGLOBAL16 hmem;
848 LPCVOID data;
850 TRACE("%04x,%08lx,%04x,%08lx,%ld\n",
851 hInst, (DWORD)dlgTemplate, owner, (DWORD)dlgProc, param );
853 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOG16 ))) return 0;
854 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
855 if (!(data = LockResource16( hmem ))) hwnd = 0;
856 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
857 dlgProc, param );
858 FreeResource16( hmem );
859 return hwnd;
862 /***********************************************************************
863 * CreateDialogParamA (USER32.73)
865 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
866 HWND owner, DLGPROC dlgProc,
867 LPARAM param )
869 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
870 if (!hrsrc) return 0;
871 return CreateDialogIndirectParamA( hInst,
872 (LPVOID)LoadResource(hInst, hrsrc),
873 owner, dlgProc, param );
877 /***********************************************************************
878 * CreateDialogParamW (USER32.74)
880 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
881 HWND owner, DLGPROC dlgProc,
882 LPARAM param )
884 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
885 if (!hrsrc) return 0;
886 return CreateDialogIndirectParamW( hInst,
887 (LPVOID)LoadResource(hInst, hrsrc),
888 owner, dlgProc, param );
892 /***********************************************************************
893 * CreateDialogIndirect16 (USER.219)
895 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
896 HWND16 owner, DLGPROC16 dlgProc )
898 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
902 /***********************************************************************
903 * CreateDialogIndirectParam16 (USER.242)
905 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
906 LPCVOID dlgTemplate,
907 HWND16 owner, DLGPROC16 dlgProc,
908 LPARAM param )
910 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
911 dlgProc, param, WIN_PROC_16 );
915 /***********************************************************************
916 * CreateDialogIndirectParamA (USER32.69)
918 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
919 LPCVOID dlgTemplate,
920 HWND owner, DLGPROC dlgProc,
921 LPARAM param )
923 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
924 (DLGPROC16)dlgProc, param, WIN_PROC_32A );
927 /***********************************************************************
928 * CreateDialogIndirectParamAorW (USER32.71)
930 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
931 LPCVOID dlgTemplate,
932 HWND owner, DLGPROC dlgProc,
933 LPARAM param )
934 { FIXME("assume WIN_PROC_32W\n");
935 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
936 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
939 /***********************************************************************
940 * CreateDialogIndirectParamW (USER32.72)
942 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
943 LPCVOID dlgTemplate,
944 HWND owner, DLGPROC dlgProc,
945 LPARAM param )
947 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
948 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
952 /***********************************************************************
953 * DIALOG_DoDialogBox
955 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
957 WND * wndPtr;
958 DIALOGINFO * dlgInfo;
959 MSG msg;
960 INT retval;
962 /* Owner must be a top-level window */
963 owner = WIN_GetTopParent( owner );
964 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
965 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
967 if (!dlgInfo->flags & DF_END) /* was EndDialog called in WM_INITDIALOG ? */
969 EnableWindow( owner, FALSE );
970 ShowWindow( hwnd, SW_SHOW );
971 while (MSG_InternalGetMessage(QMSG_WIN32A, &msg, hwnd, owner, MSGF_DIALOGBOX,
972 PM_REMOVE, !(wndPtr->dwStyle & DS_NOIDLEMSG), NULL ))
974 if (!IsDialogMessageA( hwnd, &msg))
976 TranslateMessage( &msg );
977 DispatchMessageA( &msg );
979 if (dlgInfo->flags & DF_END) break;
981 EnableWindow( owner, TRUE );
983 retval = dlgInfo->idResult;
984 WIN_ReleaseWndPtr(wndPtr);
985 DestroyWindow( hwnd );
986 return retval;
990 /***********************************************************************
991 * DialogBox16 (USER.87)
993 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
994 HWND16 owner, DLGPROC16 dlgProc )
996 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1000 /***********************************************************************
1001 * DialogBoxParam16 (USER.239)
1003 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, SEGPTR template,
1004 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1006 HWND16 hwnd = CreateDialogParam16( hInst, template, owner, dlgProc, param);
1007 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
1008 return -1;
1012 /***********************************************************************
1013 * DialogBoxParamA (USER32.139)
1015 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1016 HWND owner, DLGPROC dlgProc, LPARAM param )
1018 HWND hwnd = CreateDialogParamA( hInst, name, owner, dlgProc, param );
1019 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1020 return -1;
1024 /***********************************************************************
1025 * DialogBoxParamW (USER32.140)
1027 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1028 HWND owner, DLGPROC dlgProc, LPARAM param )
1030 HWND hwnd = CreateDialogParamW( hInst, name, owner, dlgProc, param );
1031 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1032 return -1;
1036 /***********************************************************************
1037 * DialogBoxIndirect16 (USER.218)
1039 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1040 HWND16 owner, DLGPROC16 dlgProc )
1042 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1046 /***********************************************************************
1047 * DialogBoxIndirectParam16 (USER.240)
1049 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1050 HWND16 owner, DLGPROC16 dlgProc,
1051 LPARAM param )
1053 HWND16 hwnd;
1054 LPCVOID ptr;
1056 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1057 hwnd = CreateDialogIndirectParam16( hInst, ptr, owner, dlgProc, param );
1058 GlobalUnlock16( dlgTemplate );
1059 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
1060 return -1;
1064 /***********************************************************************
1065 * DialogBoxIndirectParamA (USER32.136)
1067 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1068 HWND owner, DLGPROC dlgProc,
1069 LPARAM param )
1071 HWND hwnd = CreateDialogIndirectParamA( hInstance, template,
1072 owner, dlgProc, param );
1073 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1074 return -1;
1078 /***********************************************************************
1079 * DialogBoxIndirectParamW (USER32.138)
1081 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1082 HWND owner, DLGPROC dlgProc,
1083 LPARAM param )
1085 HWND hwnd = CreateDialogIndirectParamW( hInstance, template,
1086 owner, dlgProc, param );
1087 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1088 return -1;
1091 /***********************************************************************
1092 * DialogBoxIndirectParamAorW (USER32.138)
1094 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1095 HWND owner, DLGPROC dlgProc,
1096 LPARAM param, DWORD x )
1098 HWND hwnd;
1099 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1100 hInstance, template, owner, dlgProc, param, x);
1101 hwnd = CreateDialogIndirectParamW( hInstance, template,
1102 owner, dlgProc, param );
1103 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1104 return -1;
1107 /***********************************************************************
1108 * EndDialog16 (USER.88)
1110 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1112 return EndDialog( hwnd, retval );
1116 /***********************************************************************
1117 * EndDialog (USER32.173)
1119 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1121 WND * wndPtr = WIN_FindWndPtr( hwnd );
1122 DIALOGINFO * dlgInfo;
1123 HWND hOwner = 0;
1125 TRACE("%04x %d\n", hwnd, retval );
1127 if (!wndPtr)
1129 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1130 return FALSE;
1133 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1135 dlgInfo->idResult = retval;
1136 dlgInfo->flags |= DF_END;
1139 if(wndPtr->owner)
1140 hOwner = WIN_GetTopParent( wndPtr->owner->hwndSelf );
1142 /* Enable the owner first */
1143 if (hOwner && !IsWindowEnabled(hOwner))
1144 EnableWindow( hOwner, TRUE );
1146 /* Windows sets the focus to the dialog itself in EndDialog */
1148 if (IsChild(hwnd, GetFocus()))
1149 SetFocus(wndPtr->hwndSelf);
1151 /* Don't have to send a ShowWindow(SW_HIDE), just do
1152 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1154 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1155 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1157 WIN_ReleaseWndPtr(wndPtr);
1159 return TRUE;
1163 /***********************************************************************
1164 * DIALOG_IsAccelerator
1166 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1168 HWND hwndControl = hwnd;
1169 HWND hwndNext;
1170 WND *wndPtr;
1171 BOOL RetVal = FALSE;
1172 INT dlgCode;
1176 wndPtr = WIN_FindWndPtr( hwndControl );
1177 if ( (wndPtr != NULL) &&
1178 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1180 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1181 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1182 (wndPtr->text!=NULL))
1184 /* find the accelerator key */
1185 LPSTR p = wndPtr->text - 2;
1188 p = strchr( p + 2, '&' );
1190 while (p != NULL && p[1] == '&');
1192 /* and check if it's the one we're looking for */
1193 if (p != NULL && toupper( p[1] ) == toupper( vKey ) )
1195 if ((dlgCode & DLGC_STATIC) ||
1196 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1198 /* set focus to the control */
1199 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1200 hwndControl, 1);
1201 /* and bump it on to next */
1202 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1204 else if (dlgCode & DLGC_BUTTON)
1206 /* send BM_CLICK message to the control */
1207 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1210 RetVal = TRUE;
1211 WIN_ReleaseWndPtr(wndPtr);
1212 break;
1215 hwndNext = GetWindow( hwndControl, GW_CHILD );
1217 else
1219 hwndNext = 0;
1221 WIN_ReleaseWndPtr(wndPtr);
1222 if (!hwndNext)
1224 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1226 while (!hwndNext && hwndControl)
1228 hwndControl = GetParent( hwndControl );
1229 if (hwndControl == hwndDlg)
1231 if(hwnd==hwndDlg){ /* prevent endless loop */
1232 hwndNext=hwnd;
1233 break;
1235 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1237 else
1239 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1242 hwndControl = hwndNext;
1244 while (hwndControl && (hwndControl != hwnd));
1246 return RetVal;
1249 /***********************************************************************
1250 * DIALOG_FindMsgDestination
1252 * The messages that IsDialogMessage send may not go to the dialog
1253 * calling IsDialogMessage if that dialog is a child, and it has the
1254 * DS_CONTROL style set.
1255 * We propagate up until we hit a that does not have DS_CONTROL, or
1256 * whose parent is not a dialog.
1258 * This is undocumented behaviour.
1260 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1262 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1264 WND *pParent;
1265 HWND hParent = GetParent(hwndDlg);
1266 if (!hParent) break;
1268 pParent = WIN_FindWndPtr(hParent);
1269 if (!pParent) break;
1271 if (!(pParent->flags & WIN_ISDIALOG))
1273 WIN_ReleaseWndPtr(pParent);
1274 break;
1276 WIN_ReleaseWndPtr(pParent);
1278 hwndDlg = hParent;
1281 return hwndDlg;
1284 /***********************************************************************
1285 * DIALOG_IsDialogMessage
1287 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1288 UINT message, WPARAM wParam,
1289 LPARAM lParam, BOOL *translate,
1290 BOOL *dispatch, INT dlgCode )
1292 *translate = *dispatch = FALSE;
1294 if (message == WM_PAINT)
1296 /* Apparently, we have to handle this one as well */
1297 *dispatch = TRUE;
1298 return TRUE;
1301 /* Only the key messages get special processing */
1302 if ((message != WM_KEYDOWN) &&
1303 (message != WM_SYSCHAR) &&
1304 (message != WM_CHAR))
1305 return FALSE;
1307 if (dlgCode & DLGC_WANTMESSAGE)
1309 *translate = *dispatch = TRUE;
1310 return TRUE;
1313 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1315 switch(message)
1317 case WM_KEYDOWN:
1318 switch(wParam)
1320 case VK_TAB:
1321 if (!(dlgCode & DLGC_WANTTAB))
1323 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1324 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1325 return TRUE;
1327 break;
1329 case VK_RIGHT:
1330 case VK_DOWN:
1331 case VK_LEFT:
1332 case VK_UP:
1333 if (!(dlgCode & DLGC_WANTARROWS))
1335 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1336 HWND hwndNext =
1337 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1338 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1339 return TRUE;
1341 break;
1343 case VK_ESCAPE:
1344 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1345 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1346 return TRUE;
1348 case VK_RETURN:
1350 DWORD dw = SendMessage16( hwndDlg, DM_GETDEFID, 0, 0 );
1351 if (HIWORD(dw) == DC_HASDEFID)
1353 SendMessageA( hwndDlg, WM_COMMAND,
1354 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1355 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1357 else
1359 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1360 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1364 return TRUE;
1366 *translate = TRUE;
1367 break; /* case WM_KEYDOWN */
1369 case WM_CHAR:
1370 if (dlgCode & DLGC_WANTCHARS) break;
1371 /* drop through */
1373 case WM_SYSCHAR:
1374 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1376 /* don't translate or dispatch */
1377 return TRUE;
1379 break;
1382 /* If we get here, the message has not been treated specially */
1383 /* and can be sent to its destination window. */
1384 *dispatch = TRUE;
1385 return TRUE;
1389 /***********************************************************************
1390 * IsDialogMessage16 (USER.90)
1392 BOOL16 WINAPI WIN16_IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1394 LPMSG16 msg = PTR_SEG_TO_LIN(msg16);
1395 BOOL ret, translate, dispatch;
1396 INT dlgCode = 0;
1398 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1399 return FALSE;
1401 if ((msg->message == WM_KEYDOWN) ||
1402 (msg->message == WM_SYSCHAR) ||
1403 (msg->message == WM_CHAR))
1405 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1407 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1408 msg->wParam, msg->lParam,
1409 &translate, &dispatch, dlgCode );
1410 if (translate) TranslateMessage16( msg );
1411 if (dispatch) DispatchMessage16( msg );
1412 return ret;
1416 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, LPMSG16 msg )
1418 LPMSG16 msg16 = SEGPTR_NEW(MSG16);
1419 BOOL ret;
1421 *msg16 = *msg;
1422 ret = WIN16_IsDialogMessage16( hwndDlg, SEGPTR_GET(msg16) );
1423 SEGPTR_FREE(msg16);
1424 return ret;
1427 /***********************************************************************
1428 * IsDialogMessageA (USER32.342)
1430 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1432 BOOL ret, translate, dispatch;
1433 INT dlgCode = 0;
1435 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1436 return FALSE;
1438 if ((msg->message == WM_KEYDOWN) ||
1439 (msg->message == WM_SYSCHAR) ||
1440 (msg->message == WM_CHAR))
1442 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1444 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1445 msg->wParam, msg->lParam,
1446 &translate, &dispatch, dlgCode );
1447 if (translate) TranslateMessage( msg );
1448 if (dispatch) DispatchMessageA( msg );
1449 return ret;
1453 /***********************************************************************
1454 * IsDialogMessageW (USER32.343)
1456 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1458 BOOL ret, translate, dispatch;
1459 INT dlgCode = 0;
1461 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1462 return FALSE;
1464 if ((msg->message == WM_KEYDOWN) ||
1465 (msg->message == WM_SYSCHAR) ||
1466 (msg->message == WM_CHAR))
1468 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1470 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1471 msg->wParam, msg->lParam,
1472 &translate, &dispatch, dlgCode );
1473 if (translate) TranslateMessage( msg );
1474 if (dispatch) DispatchMessageW( msg );
1475 return ret;
1479 /***********************************************************************
1480 * GetDlgCtrlID16 (USER.277)
1482 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1484 WND *wndPtr = WIN_FindWndPtr(hwnd);
1485 INT16 retvalue;
1487 if (!wndPtr) return 0;
1489 retvalue = wndPtr->wIDmenu;
1490 WIN_ReleaseWndPtr(wndPtr);
1491 return retvalue;
1495 /***********************************************************************
1496 * GetDlgCtrlID (USER32.234)
1498 INT WINAPI GetDlgCtrlID( HWND hwnd )
1500 INT retvalue;
1501 WND *wndPtr = WIN_FindWndPtr(hwnd);
1502 if (!wndPtr) return 0;
1503 retvalue = wndPtr->wIDmenu;
1504 WIN_ReleaseWndPtr(wndPtr);
1505 return retvalue;
1509 /***********************************************************************
1510 * GetDlgItem16 (USER.91)
1512 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1514 WND *pWnd;
1516 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1517 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1518 if (pWnd->wIDmenu == (UINT16)id)
1520 HWND16 retvalue = pWnd->hwndSelf;
1521 WIN_ReleaseWndPtr(pWnd);
1522 return retvalue;
1524 return 0;
1528 /***********************************************************************
1529 * GetDlgItem (USER32.235)
1531 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1533 WND *pWnd;
1535 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1536 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
1537 if (pWnd->wIDmenu == (UINT16)id)
1539 HWND retvalue = pWnd->hwndSelf;
1540 WIN_ReleaseWndPtr(pWnd);
1541 return retvalue;
1543 return 0;
1547 /*******************************************************************
1548 * SendDlgItemMessage16 (USER.101)
1550 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1551 WPARAM16 wParam, LPARAM lParam )
1553 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1554 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1555 else return 0;
1559 /*******************************************************************
1560 * SendDlgItemMessageA (USER32.452)
1562 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1563 WPARAM wParam, LPARAM lParam )
1565 HWND hwndCtrl = GetDlgItem( hwnd, id );
1566 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1567 else return 0;
1571 /*******************************************************************
1572 * SendDlgItemMessageW (USER32.453)
1574 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1575 WPARAM wParam, LPARAM lParam )
1577 HWND hwndCtrl = GetDlgItem( hwnd, id );
1578 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1579 else return 0;
1583 /*******************************************************************
1584 * SetDlgItemText16 (USER.92)
1586 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1588 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1592 /*******************************************************************
1593 * SetDlgItemTextA (USER32.478)
1595 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1597 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1601 /*******************************************************************
1602 * SetDlgItemTextW (USER32.479)
1604 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1606 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1610 /***********************************************************************
1611 * GetDlgItemText16 (USER.93)
1613 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1615 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1616 len, (LPARAM)str );
1620 /***********************************************************************
1621 * GetDlgItemTextA (USER32.237)
1623 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1625 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1626 len, (LPARAM)str );
1630 /***********************************************************************
1631 * GetDlgItemTextW (USER32.238)
1633 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1635 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1636 len, (LPARAM)str );
1640 /*******************************************************************
1641 * SetDlgItemInt16 (USER.94)
1643 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1645 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1649 /*******************************************************************
1650 * SetDlgItemInt (USER32.477)
1652 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1653 BOOL fSigned )
1655 char str[20];
1657 if (fSigned) sprintf( str, "%d", (INT)value );
1658 else sprintf( str, "%u", value );
1659 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1660 return TRUE;
1664 /***********************************************************************
1665 * GetDlgItemInt16 (USER.95)
1667 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1668 BOOL16 fSigned )
1670 UINT result;
1671 BOOL ok;
1673 if (translated) *translated = FALSE;
1674 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1675 if (!ok) return 0;
1676 if (fSigned)
1678 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1680 else
1682 if (result > 65535) return 0;
1684 if (translated) *translated = TRUE;
1685 return (UINT16)result;
1689 /***********************************************************************
1690 * GetDlgItemInt (USER32.236)
1692 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1693 BOOL fSigned )
1695 char str[30];
1696 char * endptr;
1697 long result = 0;
1699 if (translated) *translated = FALSE;
1700 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1701 return 0;
1702 if (fSigned)
1704 result = strtol( str, &endptr, 10 );
1705 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1706 return 0;
1707 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1708 return 0;
1710 else
1712 result = strtoul( str, &endptr, 10 );
1713 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1714 return 0;
1715 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1717 if (translated) *translated = TRUE;
1718 return (UINT)result;
1722 /***********************************************************************
1723 * CheckDlgButton16 (USER.97)
1725 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1727 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1728 return TRUE;
1732 /***********************************************************************
1733 * CheckDlgButton (USER32.45)
1735 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1737 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1738 return TRUE;
1742 /***********************************************************************
1743 * IsDlgButtonChecked16 (USER.98)
1745 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1747 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1751 /***********************************************************************
1752 * IsDlgButtonChecked (USER32.344)
1754 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1756 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1760 /***********************************************************************
1761 * CheckRadioButton16 (USER.96)
1763 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1764 UINT16 lastID, UINT16 checkID )
1766 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1770 /***********************************************************************
1771 * CheckRB
1773 * Callback function used to check/uncheck radio buttons that fall
1774 * within a specific range of IDs.
1776 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1778 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1779 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1781 if ((lChildID >= lpRadioGroup->firstID) &&
1782 (lChildID <= lpRadioGroup->lastID))
1784 if (lChildID == lpRadioGroup->checkID)
1786 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1788 else
1790 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1794 return TRUE;
1798 /***********************************************************************
1799 * CheckRadioButton (USER32.48)
1801 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1802 UINT lastID, UINT checkID )
1804 RADIOGROUP radioGroup;
1806 /* perform bounds checking for a radio button group */
1807 radioGroup.firstID = min(min(firstID, lastID), checkID);
1808 radioGroup.lastID = max(max(firstID, lastID), checkID);
1809 radioGroup.checkID = checkID;
1811 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1812 (LPARAM)&radioGroup);
1816 /***********************************************************************
1817 * GetDialogBaseUnits (USER.243) (USER32.233)
1819 DWORD WINAPI GetDialogBaseUnits(void)
1821 return MAKELONG( xBaseUnit, yBaseUnit );
1825 /***********************************************************************
1826 * MapDialogRect16 (USER.103)
1828 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1830 DIALOGINFO * dlgInfo;
1831 WND * wndPtr = WIN_FindWndPtr( hwnd );
1832 if (!wndPtr) return;
1833 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1834 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1835 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1836 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1837 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1838 WIN_ReleaseWndPtr(wndPtr);
1842 /***********************************************************************
1843 * MapDialogRect (USER32.382)
1845 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1847 DIALOGINFO * dlgInfo;
1848 WND * wndPtr = WIN_FindWndPtr( hwnd );
1849 if (!wndPtr) return FALSE;
1850 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1851 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1852 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1853 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1854 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1855 WIN_ReleaseWndPtr(wndPtr);
1856 return TRUE;
1860 /***********************************************************************
1861 * GetNextDlgGroupItem16 (USER.227)
1863 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1864 BOOL16 fPrevious )
1866 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1870 /***********************************************************************
1871 * GetNextDlgGroupItem (USER32.275)
1873 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1874 BOOL fPrevious )
1876 WND *pWnd = NULL,
1877 *pWndLast = NULL,
1878 *pWndCtrl = NULL,
1879 *pWndDlg = NULL;
1880 HWND retvalue;
1882 if(hwndCtrl)
1884 /* if the hwndCtrl is the child of the control in the hwndDlg then the hwndDlg has to be the parent of the hwndCtrl */
1885 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1886 hwndDlg = GetParent(hwndCtrl);
1889 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
1890 if (hwndCtrl)
1892 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
1894 retvalue = 0;
1895 goto END;
1897 /* Make sure hwndCtrl is a top-level child */
1898 while ((pWndCtrl->dwStyle & WS_CHILD) && (pWndCtrl->parent != pWndDlg))
1899 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
1900 if (pWndCtrl->parent != pWndDlg)
1902 retvalue = 0;
1903 goto END;
1906 else
1908 /* No ctrl specified -> start from the beginning */
1909 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
1911 retvalue = 0;
1912 goto END;
1914 if (fPrevious)
1915 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
1918 pWndLast = WIN_LockWndPtr(pWndCtrl);
1919 pWnd = WIN_LockWndPtr(pWndCtrl->next);
1921 while (1)
1923 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
1925 /* Wrap-around to the beginning of the group */
1926 WND *pWndTemp;
1928 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
1929 for ( pWndTemp = WIN_LockWndPtr( pWnd );
1930 pWndTemp;
1931 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
1933 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
1934 if (pWndTemp == pWndCtrl) break;
1936 WIN_ReleaseWndPtr( pWndTemp );
1938 if (pWnd == pWndCtrl) break;
1939 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
1941 WIN_UpdateWndPtr(&pWndLast,pWnd);
1942 if (!fPrevious) break;
1944 WIN_UpdateWndPtr(&pWnd,pWnd->next);
1946 retvalue = pWndLast->hwndSelf;
1948 WIN_ReleaseWndPtr(pWndLast);
1949 WIN_ReleaseWndPtr(pWnd);
1950 END:
1951 WIN_ReleaseWndPtr(pWndCtrl);
1952 WIN_ReleaseWndPtr(pWndDlg);
1954 return retvalue;
1958 /***********************************************************************
1959 * GetNextDlgTabItem16 (USER.228)
1961 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1962 BOOL16 fPrevious )
1964 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
1967 /***********************************************************************
1968 * DIALOG_GetNextTabItem
1970 * Helper for GetNextDlgTabItem
1972 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1974 LONG dsStyle;
1975 LONG exStyle;
1976 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1977 HWND retWnd = 0;
1978 HWND hChildFirst = 0;
1980 if(!hwndCtrl)
1982 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1983 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1985 else
1987 HWND hParent = GetParent(hwndCtrl);
1988 BOOL bValid = FALSE;
1989 while( hParent)
1991 if(hParent == hwndMain)
1993 bValid = TRUE;
1994 break;
1996 hParent = GetParent(hParent);
1998 if(bValid)
2000 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2001 if(!hChildFirst)
2003 if(GetParent(hwndCtrl) != hwndMain)
2004 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2005 else
2007 if(fPrevious)
2008 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2009 else
2010 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2015 while(hChildFirst)
2017 BOOL bCtrl = FALSE;
2018 while(hChildFirst)
2020 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2021 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2022 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2024 bCtrl=TRUE;
2025 break;
2027 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2028 break;
2029 hChildFirst = GetWindow(hChildFirst,wndSearch);
2031 if(hChildFirst)
2033 if(bCtrl)
2034 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2035 else
2036 retWnd = hChildFirst;
2038 if(retWnd) break;
2039 hChildFirst = GetWindow(hChildFirst,wndSearch);
2041 if(!retWnd && hwndCtrl)
2043 HWND hParent = GetParent(hwndCtrl);
2044 while(hParent)
2046 if(hParent == hwndMain) break;
2047 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2048 if(retWnd) break;
2049 hParent = GetParent(hParent);
2051 if(!retWnd)
2052 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2054 return retWnd;
2057 /***********************************************************************
2058 * GetNextDlgTabItem (USER32.276)
2060 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2061 BOOL fPrevious )
2063 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2066 /**********************************************************************
2067 * DIALOG_DlgDirSelect
2069 * Helper function for DlgDirSelect*
2071 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2072 INT id, BOOL win32, BOOL unicode,
2073 BOOL combo )
2075 char *buffer, *ptr;
2076 INT item, size;
2077 BOOL ret;
2078 HWND listbox = GetDlgItem( hwnd, id );
2080 TRACE("%04x '%s' %d\n", hwnd, str, id );
2081 if (!listbox) return FALSE;
2082 if (win32)
2084 item = SendMessageA(listbox, combo ? CB_GETCURSEL
2085 : LB_GETCURSEL, 0, 0 );
2086 if (item == LB_ERR) return FALSE;
2087 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
2088 : LB_GETTEXTLEN, 0, 0 );
2089 if (size == LB_ERR) return FALSE;
2091 else
2093 item = SendMessageA(listbox, combo ? CB_GETCURSEL16
2094 : LB_GETCURSEL16, 0, 0 );
2095 if (item == LB_ERR) return FALSE;
2096 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN16
2097 : LB_GETTEXTLEN16, 0, 0 );
2098 if (size == LB_ERR) return FALSE;
2101 if (!(buffer = SEGPTR_ALLOC( size+1 ))) return FALSE;
2103 if (win32)
2104 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
2105 item, (LPARAM)buffer );
2106 else
2107 SendMessage16( listbox, combo ? CB_GETLBTEXT16 : LB_GETTEXT16,
2108 item, (LPARAM)SEGPTR_GET(buffer) );
2110 if ((ret = (buffer[0] == '['))) /* drive or directory */
2112 if (buffer[1] == '-') /* drive */
2114 buffer[3] = ':';
2115 buffer[4] = 0;
2116 ptr = buffer + 2;
2118 else
2120 buffer[strlen(buffer)-1] = '\\';
2121 ptr = buffer + 1;
2124 else ptr = buffer;
2126 if (unicode) lstrcpynAtoW( (LPWSTR)str, ptr, len );
2127 else lstrcpynA( str, ptr, len );
2128 SEGPTR_FREE( buffer );
2129 TRACE("Returning %d '%s'\n", ret, str );
2130 return ret;
2134 /**********************************************************************
2135 * DIALOG_DlgDirList
2137 * Helper function for DlgDirList*
2139 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2140 INT idStatic, UINT attrib, BOOL combo )
2142 int drive;
2143 HWND hwnd;
2144 LPSTR orig_spec = spec;
2146 #define SENDMSG(msg,wparam,lparam) \
2147 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2148 : SendMessageA( hwnd, msg, wparam, lparam ))
2150 TRACE("%04x '%s' %d %d %04x\n",
2151 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2153 if (spec && spec[0] && (spec[1] == ':'))
2155 drive = toupper( spec[0] ) - 'A';
2156 spec += 2;
2157 if (!DRIVE_SetCurrentDrive( drive )) return FALSE;
2159 else drive = DRIVE_GetCurrentDrive();
2161 /* If the path exists and is a directory, chdir to it */
2162 if (!spec || !spec[0] || DRIVE_Chdir( drive, spec )) spec = "*.*";
2163 else
2165 char *p, *p2;
2166 p = spec;
2167 if ((p2 = strrchr( p, '\\' ))) p = p2;
2168 if ((p2 = strrchr( p, '/' ))) p = p2;
2169 if (p != spec)
2171 char sep = *p;
2172 *p = 0;
2173 if (!DRIVE_Chdir( drive, spec ))
2175 *p = sep; /* Restore the original spec */
2176 return FALSE;
2178 spec = p + 1;
2182 TRACE("path=%c:\\%s mask=%s\n",
2183 'A' + drive, DRIVE_GetDosCwd(drive), spec );
2185 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2187 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2188 if (attrib & DDL_DIRECTORY)
2190 if (!(attrib & DDL_EXCLUSIVE))
2192 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2193 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2194 (LPARAM)spec ) == LB_ERR)
2195 return FALSE;
2197 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2198 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2199 (LPARAM)"*.*" ) == LB_ERR)
2200 return FALSE;
2202 else
2204 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2205 (LPARAM)spec ) == LB_ERR)
2206 return FALSE;
2210 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2212 char temp[512];
2213 int drive = DRIVE_GetCurrentDrive();
2214 strcpy( temp, "A:\\" );
2215 temp[0] += drive;
2216 lstrcpynA( temp + 3, DRIVE_GetDosCwd(drive), sizeof(temp)-3 );
2217 CharLowerA( temp );
2218 /* Can't use PostMessage() here, because the string is on the stack */
2219 SetDlgItemTextA( hDlg, idStatic, temp );
2222 if (orig_spec && (spec != orig_spec))
2224 /* Update the original file spec */
2225 char *p = spec;
2226 while ((*orig_spec++ = *p++));
2229 return TRUE;
2230 #undef SENDMSG
2234 /**********************************************************************
2235 * DIALOG_DlgDirListW
2237 * Helper function for DlgDirList*W
2239 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2240 INT idStatic, UINT attrib, BOOL combo )
2242 if (spec)
2244 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2245 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2246 attrib, combo );
2247 lstrcpyAtoW( spec, specA );
2248 HeapFree( GetProcessHeap(), 0, specA );
2249 return ret;
2251 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2255 /**********************************************************************
2256 * DlgDirSelect (USER.99)
2258 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2260 return DlgDirSelectEx16( hwnd, str, 128, id );
2264 /**********************************************************************
2265 * DlgDirSelectComboBox (USER.194)
2267 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2269 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2273 /**********************************************************************
2274 * DlgDirSelectEx16 (USER.422)
2276 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2278 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, FALSE );
2282 /**********************************************************************
2283 * DlgDirSelectExA (USER32.149)
2285 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2287 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
2291 /**********************************************************************
2292 * DlgDirSelectExW (USER32.150)
2294 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2296 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
2300 /**********************************************************************
2301 * DlgDirSelectComboBoxEx16 (USER.423)
2303 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2304 INT16 id )
2306 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, TRUE );
2310 /**********************************************************************
2311 * DlgDirSelectComboBoxExA (USER32.147)
2313 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2314 INT id )
2316 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
2320 /**********************************************************************
2321 * DlgDirSelectComboBoxExW (USER32.148)
2323 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2324 INT id)
2326 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
2330 /**********************************************************************
2331 * DlgDirList16 (USER.100)
2333 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2334 INT16 idStatic, UINT16 attrib )
2336 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2340 /**********************************************************************
2341 * DlgDirListA (USER32.143)
2343 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2344 INT idStatic, UINT attrib )
2346 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2350 /**********************************************************************
2351 * DlgDirListW (USER32.146)
2353 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2354 INT idStatic, UINT attrib )
2356 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2360 /**********************************************************************
2361 * DlgDirListComboBox16 (USER.195)
2363 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2364 INT16 idStatic, UINT16 attrib )
2366 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2370 /**********************************************************************
2371 * DlgDirListComboBoxA (USER32.144)
2373 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2374 INT idStatic, UINT attrib )
2376 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2380 /**********************************************************************
2381 * DlgDirListComboBoxW (USER32.145)
2383 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2384 INT idStatic, UINT attrib )
2386 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );