Get full path of argv[0] before we change directories.
[wine.git] / windows / dialog.c
blob2d206bd536c1a50e181058e09c0e9bb9b25b3397
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 "wine/unicode.h"
20 #include "dialog.h"
21 #include "drive.h"
22 #include "heap.h"
23 #include "win.h"
24 #include "ldt.h"
25 #include "user.h"
26 #include "winproc.h"
27 #include "message.h"
28 #include "queue.h"
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(dialog);
34 /* Dialog control information */
35 typedef struct
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;
50 /* Dialog template */
51 typedef struct
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;
71 /* Radio button group */
72 typedef struct
74 UINT firstID;
75 UINT lastID;
76 UINT checkID;
77 } RADIOGROUP;
79 /* Dialog base units */
80 static WORD xBaseUnit = 0, yBaseUnit = 0;
82 /***********************************************************************
83 * DIALOG_GetCharSizeFromDC
86 * Calculates the *true* average size of English characters in the
87 * specified font as oppposed to the one returned by GetTextMetrics.
89 * Latest: the X font driver will now compute a proper average width
90 * so this code can be removed
92 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
94 BOOL Success = FALSE;
95 HFONT hFontPrev = 0;
96 pSize->cx = xBaseUnit;
97 pSize->cy = yBaseUnit;
98 if ( hDC )
100 /* select the font */
101 TEXTMETRICA tm;
102 memset(&tm,0,sizeof(tm));
103 if (hFont) hFontPrev = SelectFont(hDC,hFont);
104 if (GetTextMetricsA(hDC,&tm))
106 pSize->cx = tm.tmAveCharWidth;
107 pSize->cy = tm.tmHeight;
109 /* if variable width font */
110 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
112 SIZE total;
113 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
115 /* Calculate a true average as opposed to the one returned
116 * by tmAveCharWidth. This works better when dealing with
117 * proportional spaced fonts and (more important) that's
118 * how Microsoft's dialog creation code calculates the size
119 * of the font
121 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
123 /* round up */
124 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
125 Success = TRUE;
128 else
130 Success = TRUE;
132 /* Use the text metrics */
133 TRACE("Using tm: %ldx%ld (dlg: %dx%d) (%s)\n", tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
134 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
135 pSize->cx = tm.tmAveCharWidth;
136 pSize->cy = tm.tmHeight;
138 /* select the original font */
139 if (hFontPrev) SelectFont(hDC,hFontPrev);
141 return (Success);
144 /***********************************************************************
145 * DIALOG_GetCharSize
147 * A convenient variant of DIALOG_GetCharSizeFromDC.
149 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
151 HDC hDC = GetDC(0);
152 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
153 ReleaseDC(0, hDC);
154 return Success;
157 /***********************************************************************
158 * DIALOG_Init
160 * Initialisation of the dialog manager.
162 BOOL DIALOG_Init(void)
164 HDC16 hdc;
165 SIZE size;
167 /* Calculate the dialog base units */
169 if (!(hdc = CreateDC16( "DISPLAY", NULL, NULL, NULL ))) return FALSE;
170 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size )) return FALSE;
171 DeleteDC( hdc );
172 xBaseUnit = size.cx;
173 yBaseUnit = size.cy;
175 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
176 return TRUE;
180 /***********************************************************************
181 * DIALOG_GetControl16
183 * Return the class and text of the control pointed to by ptr,
184 * fill the header structure and return a pointer to the next control.
186 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
188 static char buffer[10];
189 int int_id;
191 info->x = GET_WORD(p); p += sizeof(WORD);
192 info->y = GET_WORD(p); p += sizeof(WORD);
193 info->cx = GET_WORD(p); p += sizeof(WORD);
194 info->cy = GET_WORD(p); p += sizeof(WORD);
195 info->id = GET_WORD(p); p += sizeof(WORD);
196 info->style = GET_DWORD(p); p += sizeof(DWORD);
197 info->exStyle = 0;
199 if (*p & 0x80)
201 switch((BYTE)*p)
203 case 0x80: strcpy( buffer, "BUTTON" ); break;
204 case 0x81: strcpy( buffer, "EDIT" ); break;
205 case 0x82: strcpy( buffer, "STATIC" ); break;
206 case 0x83: strcpy( buffer, "LISTBOX" ); break;
207 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
208 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
209 default: buffer[0] = '\0'; break;
211 info->className = buffer;
212 p++;
214 else
216 info->className = p;
217 p += strlen(p) + 1;
220 int_id = ((BYTE)*p == 0xff);
221 if (int_id)
223 /* Integer id, not documented (?). Only works for SS_ICON controls */
224 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
225 p += 3;
227 else
229 info->windowName = p;
230 p += strlen(p) + 1;
233 if (*p)
235 /* Additional CTLDATA available for this control. */
236 info->data = SEGPTR_ALLOC(*p);
237 memcpy( info->data, p + 1, *p );
239 else info->data = NULL;
241 p += *p + 1;
243 if(int_id)
244 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
245 info->className, LOWORD(info->windowName),
246 info->id, info->x, info->y, info->cx, info->cy,
247 info->style, (DWORD)SEGPTR_GET(info->data) );
248 else
249 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
250 info->className, info->windowName,
251 info->id, info->x, info->y, info->cx, info->cy,
252 info->style, (DWORD)SEGPTR_GET(info->data) );
254 return p;
258 /***********************************************************************
259 * DIALOG_GetControl32
261 * Return the class and text of the control pointed to by ptr,
262 * fill the header structure and return a pointer to the next control.
264 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
265 BOOL dialogEx )
267 if (dialogEx)
269 info->helpId = GET_DWORD(p); p += 2;
270 info->exStyle = GET_DWORD(p); p += 2;
271 info->style = GET_DWORD(p); p += 2;
273 else
275 info->helpId = 0;
276 info->style = GET_DWORD(p); p += 2;
277 info->exStyle = GET_DWORD(p); p += 2;
279 info->x = GET_WORD(p); p++;
280 info->y = GET_WORD(p); p++;
281 info->cx = GET_WORD(p); p++;
282 info->cy = GET_WORD(p); p++;
284 if (dialogEx)
286 /* id is a DWORD for DIALOGEX */
287 info->id = GET_DWORD(p);
288 p += 2;
290 else
292 info->id = GET_WORD(p);
293 p++;
296 if (GET_WORD(p) == 0xffff)
298 static const WCHAR class_names[6][10] =
300 { 'B','u','t','t','o','n', }, /* 0x80 */
301 { 'E','d','i','t', }, /* 0x81 */
302 { 'S','t','a','t','i','c', }, /* 0x82 */
303 { 'L','i','s','t','B','o','x', }, /* 0x83 */
304 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
305 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
307 WORD id = GET_WORD(p+1);
308 if ((id >= 0x80) && (id <= 0x85))
309 info->className = (LPCSTR)class_names[id - 0x80];
310 else
312 info->className = NULL;
313 ERR("Unknown built-in class id %04x\n", id );
315 p += 2;
317 else
319 info->className = (LPCSTR)p;
320 p += lstrlenW( (LPCWSTR)p ) + 1;
323 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
325 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
326 p += 2;
328 else
330 info->windowName = (LPCSTR)p;
331 p += lstrlenW( (LPCWSTR)p ) + 1;
334 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
335 debugstr_w( (LPCWSTR)info->className ),
336 debugres_w( (LPCWSTR)info->windowName ),
337 info->id, info->x, info->y, info->cx, info->cy,
338 info->style, info->exStyle, info->helpId );
340 if (GET_WORD(p))
342 if (TRACE_ON(dialog))
344 WORD i, count = GET_WORD(p) / sizeof(WORD);
345 TRACE(" BEGIN\n");
346 TRACE(" ");
347 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
348 DPRINTF("\n");
349 TRACE(" END\n" );
351 info->data = (LPVOID)(p + 1);
352 p += GET_WORD(p) / sizeof(WORD);
354 else info->data = NULL;
355 p++;
357 /* Next control is on dword boundary */
358 return (const WORD *)((((int)p) + 3) & ~3);
362 /***********************************************************************
363 * DIALOG_CreateControls
365 * Create the control windows for a dialog.
367 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
368 const DLG_TEMPLATE *dlgTemplate,
369 HINSTANCE hInst, BOOL win32 )
371 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
372 DLG_CONTROL_INFO info;
373 HWND hwndCtrl, hwndDefButton = 0;
374 INT items = dlgTemplate->nbItems;
376 TRACE(" BEGIN\n" );
377 while (items--)
379 if (!win32)
381 HINSTANCE16 instance;
382 template = DIALOG_GetControl16( template, &info );
383 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
384 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
386 if (!dlgInfo->hDialogHeap)
388 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
389 if (!dlgInfo->hDialogHeap)
391 ERR("Insufficient memory to create heap for edit control\n" );
392 continue;
394 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
396 instance = dlgInfo->hDialogHeap;
398 else instance = (HINSTANCE16)hInst;
400 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
401 info.className, info.windowName,
402 info.style | WS_CHILD,
403 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
404 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
405 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
406 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
407 pWnd->hwndSelf, (HMENU16)info.id,
408 instance, (LPVOID)SEGPTR_GET(info.data) );
410 if (info.data) SEGPTR_FREE(info.data);
412 else
414 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
415 dlgTemplate->dialogEx );
416 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
417 (LPCWSTR)info.className,
418 (LPCWSTR)info.windowName,
419 info.style | WS_CHILD,
420 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
421 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
422 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
423 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
424 pWnd->hwndSelf, (HMENU)info.id,
425 hInst, info.data );
427 if (!hwndCtrl) return FALSE;
429 /* Send initialisation messages to the control */
430 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
431 (WPARAM)dlgInfo->hUserFont, 0 );
432 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
434 /* If there's already a default push-button, set it back */
435 /* to normal and use this one instead. */
436 if (hwndDefButton)
437 SendMessageA( hwndDefButton, BM_SETSTYLE,
438 BS_PUSHBUTTON,FALSE );
439 hwndDefButton = hwndCtrl;
440 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
443 TRACE(" END\n" );
444 return TRUE;
448 /***********************************************************************
449 * DIALOG_ParseTemplate16
451 * Fill a DLG_TEMPLATE structure from the dialog template, and return
452 * a pointer to the first control.
454 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
456 result->style = GET_DWORD(p); p += sizeof(DWORD);
457 result->exStyle = 0;
458 result->nbItems = (unsigned char) *p++;
459 result->x = GET_WORD(p); p += sizeof(WORD);
460 result->y = GET_WORD(p); p += sizeof(WORD);
461 result->cx = GET_WORD(p); p += sizeof(WORD);
462 result->cy = GET_WORD(p); p += sizeof(WORD);
463 TRACE("DIALOG %d, %d, %d, %d\n",
464 result->x, result->y, result->cx, result->cy );
465 TRACE(" STYLE %08lx\n", result->style );
467 /* Get the menu name */
469 switch( (BYTE)*p )
471 case 0:
472 result->menuName = 0;
473 p++;
474 break;
475 case 0xff:
476 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
477 p += 3;
478 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
479 break;
480 default:
481 result->menuName = p;
482 TRACE(" MENU '%s'\n", p );
483 p += strlen(p) + 1;
484 break;
487 /* Get the class name */
489 if (*p)
491 result->className = p;
492 TRACE(" CLASS '%s'\n", result->className );
494 else result->className = DIALOG_CLASS_ATOM;
495 p += strlen(p) + 1;
497 /* Get the window caption */
499 result->caption = p;
500 p += strlen(p) + 1;
501 TRACE(" CAPTION '%s'\n", result->caption );
503 /* Get the font name */
505 if (result->style & DS_SETFONT)
507 result->pointSize = GET_WORD(p);
508 p += sizeof(WORD);
509 result->faceName = p;
510 p += strlen(p) + 1;
511 TRACE(" FONT %d,'%s'\n",
512 result->pointSize, result->faceName );
514 return p;
518 /***********************************************************************
519 * DIALOG_ParseTemplate32
521 * Fill a DLG_TEMPLATE structure from the dialog template, and return
522 * a pointer to the first control.
524 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
526 const WORD *p = (const WORD *)template;
528 result->style = GET_DWORD(p); p += 2;
529 if (result->style == 0xffff0001) /* DIALOGEX resource */
531 result->dialogEx = TRUE;
532 result->helpId = GET_DWORD(p); p += 2;
533 result->exStyle = GET_DWORD(p); p += 2;
534 result->style = GET_DWORD(p); p += 2;
536 else
538 result->dialogEx = FALSE;
539 result->helpId = 0;
540 result->exStyle = GET_DWORD(p); p += 2;
542 result->nbItems = GET_WORD(p); p++;
543 result->x = GET_WORD(p); p++;
544 result->y = GET_WORD(p); p++;
545 result->cx = GET_WORD(p); p++;
546 result->cy = GET_WORD(p); p++;
547 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
548 result->dialogEx ? "EX" : "", result->x, result->y,
549 result->cx, result->cy, result->helpId );
550 TRACE(" STYLE 0x%08lx\n", result->style );
551 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
553 /* Get the menu name */
555 switch(GET_WORD(p))
557 case 0x0000:
558 result->menuName = NULL;
559 p++;
560 break;
561 case 0xffff:
562 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
563 p += 2;
564 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
565 break;
566 default:
567 result->menuName = (LPCSTR)p;
568 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
569 p += lstrlenW( (LPCWSTR)p ) + 1;
570 break;
573 /* Get the class name */
575 switch(GET_WORD(p))
577 case 0x0000:
578 result->className = DIALOG_CLASS_ATOM;
579 p++;
580 break;
581 case 0xffff:
582 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
583 p += 2;
584 TRACE(" CLASS %04x\n", LOWORD(result->className) );
585 break;
586 default:
587 result->className = (LPCSTR)p;
588 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
589 p += lstrlenW( (LPCWSTR)p ) + 1;
590 break;
593 /* Get the window caption */
595 result->caption = (LPCSTR)p;
596 p += lstrlenW( (LPCWSTR)p ) + 1;
597 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
599 /* Get the font name */
601 if (result->style & DS_SETFONT)
603 result->pointSize = GET_WORD(p);
604 p++;
605 if (result->dialogEx)
607 result->weight = GET_WORD(p); p++;
608 result->italic = LOBYTE(GET_WORD(p)); p++;
610 else
612 result->weight = FW_DONTCARE;
613 result->italic = FALSE;
615 result->faceName = (LPCSTR)p;
616 p += lstrlenW( (LPCWSTR)p ) + 1;
617 TRACE(" FONT %d, %s, %d, %s\n",
618 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
619 result->weight, result->italic ? "TRUE" : "FALSE" );
622 /* First control is on dword boundary */
623 return (LPCSTR)((((int)p) + 3) & ~3);
627 /***********************************************************************
628 * DIALOG_CreateIndirect
630 HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
631 BOOL win32Template, HWND owner,
632 DLGPROC16 dlgProc, LPARAM param,
633 WINDOWPROCTYPE procType )
635 HMENU16 hMenu = 0;
636 HFONT16 hFont = 0;
637 HWND hwnd;
638 RECT rect;
639 WND * wndPtr;
640 DLG_TEMPLATE template;
641 DIALOGINFO * dlgInfo;
642 WORD xUnit = xBaseUnit;
643 WORD yUnit = yBaseUnit;
645 /* Parse dialog template */
647 if (!dlgTemplate) return 0;
648 if (win32Template)
649 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
650 else
651 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
653 /* Load menu */
655 if (template.menuName)
657 if (!win32Template)
659 LPSTR str = SEGPTR_STRDUP( template.menuName );
660 hMenu = LoadMenu16( hInst, SEGPTR_GET(str) );
661 SEGPTR_FREE( str );
663 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
666 /* Create custom font if needed */
668 if (template.style & DS_SETFONT)
670 /* The font height must be negative as it is a point size */
671 /* and must be converted to pixels first */
672 /* (see CreateFont() documentation in the Windows SDK). */
673 HDC dc = GetDC(0);
674 int pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
675 ReleaseDC(0, dc);
677 if (win32Template)
678 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
679 template.italic, FALSE, FALSE,
680 DEFAULT_CHARSET, 0, 0,
681 PROOF_QUALITY, FF_DONTCARE,
682 (LPCWSTR)template.faceName );
683 else
684 hFont = CreateFont16( -pixels, 0, 0, 0, FW_DONTCARE,
685 FALSE, FALSE, FALSE,
686 DEFAULT_CHARSET, 0, 0,
687 PROOF_QUALITY, FF_DONTCARE,
688 template.faceName );
689 if (hFont)
691 SIZE charSize;
692 if (DIALOG_GetCharSize(hFont,&charSize))
694 xUnit = charSize.cx;
695 yUnit = charSize.cy;
698 TRACE("units = %d,%d\n", xUnit, yUnit );
701 /* Create dialog main window */
703 rect.left = rect.top = 0;
704 rect.right = MulDiv(template.cx, xUnit, 4);
705 rect.bottom = MulDiv(template.cy, yUnit, 8);
706 if (template.style & DS_MODALFRAME)
707 template.exStyle |= WS_EX_DLGMODALFRAME;
708 AdjustWindowRectEx( &rect, template.style,
709 hMenu ? TRUE : FALSE , template.exStyle );
710 rect.right -= rect.left;
711 rect.bottom -= rect.top;
713 if ((INT16)template.x == CW_USEDEFAULT16)
715 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
717 else
719 if (template.style & DS_CENTER)
721 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
722 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
724 else
726 rect.left += MulDiv(template.x, xUnit, 4);
727 rect.top += MulDiv(template.y, yUnit, 8);
729 if ( !(template.style & WS_CHILD) )
731 INT16 dX, dY;
733 if( !(template.style & DS_ABSALIGN) )
734 ClientToScreen( owner, (POINT *)&rect );
736 /* try to fit it into the desktop */
738 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
739 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
740 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
741 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
742 if( rect.left < 0 ) rect.left = 0;
743 if( rect.top < 0 ) rect.top = 0;
747 if (!win32Template)
748 hwnd = CreateWindowEx16(template.exStyle, template.className,
749 template.caption, template.style & ~WS_VISIBLE,
750 rect.left, rect.top, rect.right, rect.bottom,
751 owner, hMenu, hInst, NULL );
752 else
753 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
754 (LPCWSTR)template.caption,
755 template.style & ~WS_VISIBLE,
756 rect.left, rect.top, rect.right, rect.bottom,
757 owner, hMenu, hInst, NULL );
759 if (!hwnd)
761 if (hFont) DeleteObject( hFont );
762 if (hMenu) DestroyMenu( hMenu );
763 return 0;
765 wndPtr = WIN_FindWndPtr( hwnd );
766 wndPtr->flags |= WIN_ISDIALOG;
767 wndPtr->helpContext = template.helpId;
769 /* Initialise dialog extra data */
771 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
772 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
773 dlgInfo->hUserFont = hFont;
774 dlgInfo->hMenu = hMenu;
775 dlgInfo->xBaseUnit = xUnit;
776 dlgInfo->yBaseUnit = yUnit;
777 dlgInfo->msgResult = 0;
778 dlgInfo->idResult = 0;
779 dlgInfo->flags = 0;
780 dlgInfo->hDialogHeap = 0;
782 if (dlgInfo->hUserFont)
783 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
785 /* Create controls */
787 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
788 hInst, win32Template ))
790 HWND hwndPreInitFocus;
792 /* Send initialisation messages and set focus */
794 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
796 hwndPreInitFocus = GetFocus();
797 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
799 /* check where the focus is again, some controls status might have changed in
800 WM_INITDIALOG */
801 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
802 SetFocus( dlgInfo->hwndFocus );
804 else
806 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
807 but the focus has not changed, set the focus where we expect it. */
808 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
810 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
811 SetFocus( dlgInfo->hwndFocus );
815 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
817 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
818 UpdateWindow( hwnd );
820 WIN_ReleaseWndPtr(wndPtr);
821 return hwnd;
823 WIN_ReleaseWndPtr(wndPtr);
824 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
825 return 0;
829 /***********************************************************************
830 * CreateDialog16 (USER.89)
832 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
833 HWND16 owner, DLGPROC16 dlgProc )
835 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
839 /***********************************************************************
840 * CreateDialogParam16 (USER.241)
842 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
843 HWND16 owner, DLGPROC16 dlgProc,
844 LPARAM param )
846 HWND16 hwnd = 0;
847 HRSRC16 hRsrc;
848 HGLOBAL16 hmem;
849 LPCVOID data;
851 TRACE("%04x,%08lx,%04x,%08lx,%ld\n",
852 hInst, (DWORD)dlgTemplate, owner, (DWORD)dlgProc, param );
854 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOG16 ))) return 0;
855 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
856 if (!(data = LockResource16( hmem ))) hwnd = 0;
857 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
858 dlgProc, param );
859 FreeResource16( hmem );
860 return hwnd;
863 /***********************************************************************
864 * CreateDialogParamA (USER32.73)
866 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
867 HWND owner, DLGPROC dlgProc,
868 LPARAM param )
870 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
871 if (!hrsrc) return 0;
872 return CreateDialogIndirectParamA( hInst,
873 (LPVOID)LoadResource(hInst, hrsrc),
874 owner, dlgProc, param );
878 /***********************************************************************
879 * CreateDialogParamW (USER32.74)
881 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
882 HWND owner, DLGPROC dlgProc,
883 LPARAM param )
885 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
886 if (!hrsrc) return 0;
887 return CreateDialogIndirectParamW( hInst,
888 (LPVOID)LoadResource(hInst, hrsrc),
889 owner, dlgProc, param );
893 /***********************************************************************
894 * CreateDialogIndirect16 (USER.219)
896 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
897 HWND16 owner, DLGPROC16 dlgProc )
899 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
903 /***********************************************************************
904 * CreateDialogIndirectParam16 (USER.242)
906 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
907 LPCVOID dlgTemplate,
908 HWND16 owner, DLGPROC16 dlgProc,
909 LPARAM param )
911 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
912 dlgProc, param, WIN_PROC_16 );
916 /***********************************************************************
917 * CreateDialogIndirectParamA (USER32.69)
919 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
920 LPCVOID dlgTemplate,
921 HWND owner, DLGPROC dlgProc,
922 LPARAM param )
924 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
925 (DLGPROC16)dlgProc, param, WIN_PROC_32A );
928 /***********************************************************************
929 * CreateDialogIndirectParamAorW (USER32.71)
931 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
932 LPCVOID dlgTemplate,
933 HWND owner, DLGPROC dlgProc,
934 LPARAM param )
935 { FIXME("assume WIN_PROC_32W\n");
936 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
937 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
940 /***********************************************************************
941 * CreateDialogIndirectParamW (USER32.72)
943 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
944 LPCVOID dlgTemplate,
945 HWND owner, DLGPROC dlgProc,
946 LPARAM param )
948 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
949 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
953 /***********************************************************************
954 * DIALOG_DoDialogBox
956 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
958 WND * wndPtr;
959 DIALOGINFO * dlgInfo;
960 MSG msg;
961 INT retval;
963 /* Owner must be a top-level window */
964 owner = WIN_GetTopParent( owner );
965 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
966 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
968 if (!dlgInfo->flags & DF_END) /* was EndDialog called in WM_INITDIALOG ? */
970 EnableWindow( owner, FALSE );
971 ShowWindow( hwnd, SW_SHOW );
972 while (MSG_InternalGetMessage(QMSG_WIN32A, &msg, hwnd, owner, MSGF_DIALOGBOX,
973 PM_REMOVE, !(wndPtr->dwStyle & DS_NOIDLEMSG), NULL ))
975 if (!IsDialogMessageA( hwnd, &msg))
977 TranslateMessage( &msg );
978 DispatchMessageA( &msg );
980 if (dlgInfo->flags & DF_END) break;
982 EnableWindow( owner, TRUE );
984 retval = dlgInfo->idResult;
985 WIN_ReleaseWndPtr(wndPtr);
986 DestroyWindow( hwnd );
987 return retval;
991 /***********************************************************************
992 * DialogBox16 (USER.87)
994 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
995 HWND16 owner, DLGPROC16 dlgProc )
997 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1001 /***********************************************************************
1002 * DialogBoxParam16 (USER.239)
1004 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, SEGPTR template,
1005 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1007 HWND16 hwnd = CreateDialogParam16( hInst, template, owner, dlgProc, param);
1008 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
1009 return -1;
1013 /***********************************************************************
1014 * DialogBoxParamA (USER32.139)
1016 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1017 HWND owner, DLGPROC dlgProc, LPARAM param )
1019 HWND hwnd = CreateDialogParamA( hInst, name, owner, dlgProc, param );
1020 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1021 return -1;
1025 /***********************************************************************
1026 * DialogBoxParamW (USER32.140)
1028 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1029 HWND owner, DLGPROC dlgProc, LPARAM param )
1031 HWND hwnd = CreateDialogParamW( hInst, name, owner, dlgProc, param );
1032 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1033 return -1;
1037 /***********************************************************************
1038 * DialogBoxIndirect16 (USER.218)
1040 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1041 HWND16 owner, DLGPROC16 dlgProc )
1043 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1047 /***********************************************************************
1048 * DialogBoxIndirectParam16 (USER.240)
1050 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1051 HWND16 owner, DLGPROC16 dlgProc,
1052 LPARAM param )
1054 HWND16 hwnd;
1055 LPCVOID ptr;
1057 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1058 hwnd = CreateDialogIndirectParam16( hInst, ptr, owner, dlgProc, param );
1059 GlobalUnlock16( dlgTemplate );
1060 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
1061 return -1;
1065 /***********************************************************************
1066 * DialogBoxIndirectParamA (USER32.136)
1068 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1069 HWND owner, DLGPROC dlgProc,
1070 LPARAM param )
1072 HWND hwnd = CreateDialogIndirectParamA( hInstance, template,
1073 owner, dlgProc, param );
1074 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1075 return -1;
1079 /***********************************************************************
1080 * DialogBoxIndirectParamW (USER32.138)
1082 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1083 HWND owner, DLGPROC dlgProc,
1084 LPARAM param )
1086 HWND hwnd = CreateDialogIndirectParamW( hInstance, template,
1087 owner, dlgProc, param );
1088 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1089 return -1;
1092 /***********************************************************************
1093 * DialogBoxIndirectParamAorW (USER32.138)
1095 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1096 HWND owner, DLGPROC dlgProc,
1097 LPARAM param, DWORD x )
1099 HWND hwnd;
1100 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1101 hInstance, template, owner, dlgProc, param, x);
1102 hwnd = CreateDialogIndirectParamW( hInstance, template,
1103 owner, dlgProc, param );
1104 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1105 return -1;
1108 /***********************************************************************
1109 * EndDialog16 (USER.88)
1111 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1113 return EndDialog( hwnd, retval );
1117 /***********************************************************************
1118 * EndDialog (USER32.173)
1120 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1122 WND * wndPtr = WIN_FindWndPtr( hwnd );
1123 DIALOGINFO * dlgInfo;
1124 HWND hOwner = 0;
1126 TRACE("%04x %d\n", hwnd, retval );
1128 if (!wndPtr)
1130 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1131 return FALSE;
1134 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1136 dlgInfo->idResult = retval;
1137 dlgInfo->flags |= DF_END;
1140 if(wndPtr->owner)
1141 hOwner = WIN_GetTopParent( wndPtr->owner->hwndSelf );
1143 /* Enable the owner first */
1144 if (hOwner && !IsWindowEnabled(hOwner))
1145 EnableWindow( hOwner, TRUE );
1147 /* Windows sets the focus to the dialog itself in EndDialog */
1149 if (IsChild(hwnd, GetFocus()))
1150 SetFocus(wndPtr->hwndSelf);
1152 /* Don't have to send a ShowWindow(SW_HIDE), just do
1153 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1155 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1156 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1158 WIN_ReleaseWndPtr(wndPtr);
1160 return TRUE;
1164 /***********************************************************************
1165 * DIALOG_IsAccelerator
1167 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1169 HWND hwndControl = hwnd;
1170 HWND hwndNext;
1171 WND *wndPtr;
1172 BOOL RetVal = FALSE;
1173 INT dlgCode;
1177 wndPtr = WIN_FindWndPtr( hwndControl );
1178 if ( (wndPtr != NULL) &&
1179 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1181 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1182 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1183 (wndPtr->text!=NULL))
1185 /* find the accelerator key */
1186 LPWSTR p = wndPtr->text - 2;
1189 p = strchrW( p + 2, '&' );
1191 while (p != NULL && p[1] == '&');
1193 /* and check if it's the one we're looking for */
1194 /* FIXME: convert vKey to unicode */
1195 if (p != NULL && toupperW( p[1] ) == (WCHAR)toupper( vKey ) )
1197 if ((dlgCode & DLGC_STATIC) ||
1198 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1200 /* set focus to the control */
1201 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1202 hwndControl, 1);
1203 /* and bump it on to next */
1204 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1206 else if (dlgCode & DLGC_BUTTON)
1208 /* send BM_CLICK message to the control */
1209 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1212 RetVal = TRUE;
1213 WIN_ReleaseWndPtr(wndPtr);
1214 break;
1217 hwndNext = GetWindow( hwndControl, GW_CHILD );
1219 else
1221 hwndNext = 0;
1223 WIN_ReleaseWndPtr(wndPtr);
1224 if (!hwndNext)
1226 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1228 while (!hwndNext && hwndControl)
1230 hwndControl = GetParent( hwndControl );
1231 if (hwndControl == hwndDlg)
1233 if(hwnd==hwndDlg){ /* prevent endless loop */
1234 hwndNext=hwnd;
1235 break;
1237 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1239 else
1241 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1244 hwndControl = hwndNext;
1246 while (hwndControl && (hwndControl != hwnd));
1248 return RetVal;
1251 /***********************************************************************
1252 * DIALOG_FindMsgDestination
1254 * The messages that IsDialogMessage send may not go to the dialog
1255 * calling IsDialogMessage if that dialog is a child, and it has the
1256 * DS_CONTROL style set.
1257 * We propagate up until we hit a that does not have DS_CONTROL, or
1258 * whose parent is not a dialog.
1260 * This is undocumented behaviour.
1262 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1264 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1266 WND *pParent;
1267 HWND hParent = GetParent(hwndDlg);
1268 if (!hParent) break;
1270 pParent = WIN_FindWndPtr(hParent);
1271 if (!pParent) break;
1273 if (!(pParent->flags & WIN_ISDIALOG))
1275 WIN_ReleaseWndPtr(pParent);
1276 break;
1278 WIN_ReleaseWndPtr(pParent);
1280 hwndDlg = hParent;
1283 return hwndDlg;
1286 /***********************************************************************
1287 * DIALOG_IsDialogMessage
1289 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1290 UINT message, WPARAM wParam,
1291 LPARAM lParam, BOOL *translate,
1292 BOOL *dispatch, INT dlgCode )
1294 *translate = *dispatch = FALSE;
1296 if (message == WM_PAINT)
1298 /* Apparently, we have to handle this one as well */
1299 *dispatch = TRUE;
1300 return TRUE;
1303 /* Only the key messages get special processing */
1304 if ((message != WM_KEYDOWN) &&
1305 (message != WM_SYSCHAR) &&
1306 (message != WM_CHAR))
1307 return FALSE;
1309 if (dlgCode & DLGC_WANTMESSAGE)
1311 *translate = *dispatch = TRUE;
1312 return TRUE;
1315 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1317 switch(message)
1319 case WM_KEYDOWN:
1320 switch(wParam)
1322 case VK_TAB:
1323 if (!(dlgCode & DLGC_WANTTAB))
1325 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1326 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1327 return TRUE;
1329 break;
1331 case VK_RIGHT:
1332 case VK_DOWN:
1333 case VK_LEFT:
1334 case VK_UP:
1335 if (!(dlgCode & DLGC_WANTARROWS))
1337 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1338 HWND hwndNext =
1339 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1340 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1341 return TRUE;
1343 break;
1345 case VK_ESCAPE:
1346 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1347 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1348 return TRUE;
1350 case VK_RETURN:
1352 DWORD dw = SendMessage16( hwndDlg, DM_GETDEFID, 0, 0 );
1353 if (HIWORD(dw) == DC_HASDEFID)
1355 SendMessageA( hwndDlg, WM_COMMAND,
1356 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1357 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1359 else
1361 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1362 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1366 return TRUE;
1368 *translate = TRUE;
1369 break; /* case WM_KEYDOWN */
1371 case WM_CHAR:
1372 if (dlgCode & DLGC_WANTCHARS) break;
1373 /* drop through */
1375 case WM_SYSCHAR:
1376 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1378 /* don't translate or dispatch */
1379 return TRUE;
1381 break;
1384 /* If we get here, the message has not been treated specially */
1385 /* and can be sent to its destination window. */
1386 *dispatch = TRUE;
1387 return TRUE;
1391 /***********************************************************************
1392 * IsDialogMessage16 (USER.90)
1394 BOOL16 WINAPI WIN16_IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1396 LPMSG16 msg = PTR_SEG_TO_LIN(msg16);
1397 BOOL ret, translate, dispatch;
1398 INT dlgCode = 0;
1400 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1401 return FALSE;
1403 if ((msg->message == WM_KEYDOWN) ||
1404 (msg->message == WM_SYSCHAR) ||
1405 (msg->message == WM_CHAR))
1407 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1409 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1410 msg->wParam, msg->lParam,
1411 &translate, &dispatch, dlgCode );
1412 if (translate) TranslateMessage16( msg );
1413 if (dispatch) DispatchMessage16( msg );
1414 return ret;
1418 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, LPMSG16 msg )
1420 LPMSG16 msg16 = SEGPTR_NEW(MSG16);
1421 BOOL ret;
1423 *msg16 = *msg;
1424 ret = WIN16_IsDialogMessage16( hwndDlg, SEGPTR_GET(msg16) );
1425 SEGPTR_FREE(msg16);
1426 return ret;
1429 /***********************************************************************
1430 * IsDialogMessageA (USER32.342)
1432 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1434 BOOL ret, translate, dispatch;
1435 INT dlgCode = 0;
1437 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1438 return FALSE;
1440 if ((msg->message == WM_KEYDOWN) ||
1441 (msg->message == WM_SYSCHAR) ||
1442 (msg->message == WM_CHAR))
1444 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1446 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1447 msg->wParam, msg->lParam,
1448 &translate, &dispatch, dlgCode );
1449 if (translate) TranslateMessage( msg );
1450 if (dispatch) DispatchMessageA( msg );
1451 return ret;
1455 /***********************************************************************
1456 * IsDialogMessageW (USER32.343)
1458 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1460 BOOL ret, translate, dispatch;
1461 INT dlgCode = 0;
1463 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1464 return FALSE;
1466 if ((msg->message == WM_KEYDOWN) ||
1467 (msg->message == WM_SYSCHAR) ||
1468 (msg->message == WM_CHAR))
1470 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1472 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1473 msg->wParam, msg->lParam,
1474 &translate, &dispatch, dlgCode );
1475 if (translate) TranslateMessage( msg );
1476 if (dispatch) DispatchMessageW( msg );
1477 return ret;
1481 /***********************************************************************
1482 * GetDlgCtrlID16 (USER.277)
1484 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1486 WND *wndPtr = WIN_FindWndPtr(hwnd);
1487 INT16 retvalue;
1489 if (!wndPtr) return 0;
1491 retvalue = wndPtr->wIDmenu;
1492 WIN_ReleaseWndPtr(wndPtr);
1493 return retvalue;
1497 /***********************************************************************
1498 * GetDlgCtrlID (USER32.234)
1500 INT WINAPI GetDlgCtrlID( HWND hwnd )
1502 INT retvalue;
1503 WND *wndPtr = WIN_FindWndPtr(hwnd);
1504 if (!wndPtr) return 0;
1505 retvalue = wndPtr->wIDmenu;
1506 WIN_ReleaseWndPtr(wndPtr);
1507 return retvalue;
1511 /***********************************************************************
1512 * GetDlgItem16 (USER.91)
1514 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1516 WND *pWnd;
1518 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1519 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1520 if (pWnd->wIDmenu == (UINT16)id)
1522 HWND16 retvalue = pWnd->hwndSelf;
1523 WIN_ReleaseWndPtr(pWnd);
1524 return retvalue;
1526 return 0;
1530 /***********************************************************************
1531 * GetDlgItem (USER32.235)
1533 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1535 WND *pWnd;
1537 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1538 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
1539 if (pWnd->wIDmenu == (UINT16)id)
1541 HWND retvalue = pWnd->hwndSelf;
1542 WIN_ReleaseWndPtr(pWnd);
1543 return retvalue;
1545 return 0;
1549 /*******************************************************************
1550 * SendDlgItemMessage16 (USER.101)
1552 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1553 WPARAM16 wParam, LPARAM lParam )
1555 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1556 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1557 else return 0;
1561 /*******************************************************************
1562 * SendDlgItemMessageA (USER32.452)
1564 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1565 WPARAM wParam, LPARAM lParam )
1567 HWND hwndCtrl = GetDlgItem( hwnd, id );
1568 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1569 else return 0;
1573 /*******************************************************************
1574 * SendDlgItemMessageW (USER32.453)
1576 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1577 WPARAM wParam, LPARAM lParam )
1579 HWND hwndCtrl = GetDlgItem( hwnd, id );
1580 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1581 else return 0;
1585 /*******************************************************************
1586 * SetDlgItemText16 (USER.92)
1588 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1590 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1594 /*******************************************************************
1595 * SetDlgItemTextA (USER32.478)
1597 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1599 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1603 /*******************************************************************
1604 * SetDlgItemTextW (USER32.479)
1606 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1608 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1612 /***********************************************************************
1613 * GetDlgItemText16 (USER.93)
1615 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1617 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1618 len, (LPARAM)str );
1622 /***********************************************************************
1623 * GetDlgItemTextA (USER32.237)
1625 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1627 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1628 len, (LPARAM)str );
1632 /***********************************************************************
1633 * GetDlgItemTextW (USER32.238)
1635 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1637 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1638 len, (LPARAM)str );
1642 /*******************************************************************
1643 * SetDlgItemInt16 (USER.94)
1645 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1647 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1651 /*******************************************************************
1652 * SetDlgItemInt (USER32.477)
1654 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1655 BOOL fSigned )
1657 char str[20];
1659 if (fSigned) sprintf( str, "%d", (INT)value );
1660 else sprintf( str, "%u", value );
1661 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1662 return TRUE;
1666 /***********************************************************************
1667 * GetDlgItemInt16 (USER.95)
1669 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1670 BOOL16 fSigned )
1672 UINT result;
1673 BOOL ok;
1675 if (translated) *translated = FALSE;
1676 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1677 if (!ok) return 0;
1678 if (fSigned)
1680 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1682 else
1684 if (result > 65535) return 0;
1686 if (translated) *translated = TRUE;
1687 return (UINT16)result;
1691 /***********************************************************************
1692 * GetDlgItemInt (USER32.236)
1694 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1695 BOOL fSigned )
1697 char str[30];
1698 char * endptr;
1699 long result = 0;
1701 if (translated) *translated = FALSE;
1702 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1703 return 0;
1704 if (fSigned)
1706 result = strtol( str, &endptr, 10 );
1707 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1708 return 0;
1709 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1710 return 0;
1712 else
1714 result = strtoul( str, &endptr, 10 );
1715 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1716 return 0;
1717 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1719 if (translated) *translated = TRUE;
1720 return (UINT)result;
1724 /***********************************************************************
1725 * CheckDlgButton16 (USER.97)
1727 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1729 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1730 return TRUE;
1734 /***********************************************************************
1735 * CheckDlgButton (USER32.45)
1737 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1739 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1740 return TRUE;
1744 /***********************************************************************
1745 * IsDlgButtonChecked16 (USER.98)
1747 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1749 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1753 /***********************************************************************
1754 * IsDlgButtonChecked (USER32.344)
1756 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1758 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1762 /***********************************************************************
1763 * CheckRadioButton16 (USER.96)
1765 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1766 UINT16 lastID, UINT16 checkID )
1768 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1772 /***********************************************************************
1773 * CheckRB
1775 * Callback function used to check/uncheck radio buttons that fall
1776 * within a specific range of IDs.
1778 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1780 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1781 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1783 if ((lChildID >= lpRadioGroup->firstID) &&
1784 (lChildID <= lpRadioGroup->lastID))
1786 if (lChildID == lpRadioGroup->checkID)
1788 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1790 else
1792 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1796 return TRUE;
1800 /***********************************************************************
1801 * CheckRadioButton (USER32.48)
1803 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1804 UINT lastID, UINT checkID )
1806 RADIOGROUP radioGroup;
1808 /* perform bounds checking for a radio button group */
1809 radioGroup.firstID = min(min(firstID, lastID), checkID);
1810 radioGroup.lastID = max(max(firstID, lastID), checkID);
1811 radioGroup.checkID = checkID;
1813 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1814 (LPARAM)&radioGroup);
1818 /***********************************************************************
1819 * GetDialogBaseUnits (USER.243) (USER32.233)
1821 DWORD WINAPI GetDialogBaseUnits(void)
1823 return MAKELONG( xBaseUnit, yBaseUnit );
1827 /***********************************************************************
1828 * MapDialogRect16 (USER.103)
1830 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1832 DIALOGINFO * dlgInfo;
1833 WND * wndPtr = WIN_FindWndPtr( hwnd );
1834 if (!wndPtr) return;
1835 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1836 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1837 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1838 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1839 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1840 WIN_ReleaseWndPtr(wndPtr);
1844 /***********************************************************************
1845 * MapDialogRect (USER32.382)
1847 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1849 DIALOGINFO * dlgInfo;
1850 WND * wndPtr = WIN_FindWndPtr( hwnd );
1851 if (!wndPtr) return FALSE;
1852 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1853 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1854 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1855 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1856 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1857 WIN_ReleaseWndPtr(wndPtr);
1858 return TRUE;
1862 /***********************************************************************
1863 * GetNextDlgGroupItem16 (USER.227)
1865 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1866 BOOL16 fPrevious )
1868 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1872 /***********************************************************************
1873 * GetNextDlgGroupItem (USER32.275)
1875 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1876 BOOL fPrevious )
1878 WND *pWnd = NULL,
1879 *pWndLast = NULL,
1880 *pWndCtrl = NULL,
1881 *pWndDlg = NULL;
1882 HWND retvalue;
1884 if(hwndCtrl)
1886 /* if the hwndCtrl is the child of the control in the hwndDlg then the hwndDlg has to be the parent of the hwndCtrl */
1887 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1888 hwndDlg = GetParent(hwndCtrl);
1891 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
1892 if (hwndCtrl)
1894 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
1896 retvalue = 0;
1897 goto END;
1899 /* Make sure hwndCtrl is a top-level child */
1900 while ((pWndCtrl->dwStyle & WS_CHILD) && (pWndCtrl->parent != pWndDlg))
1901 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
1902 if (pWndCtrl->parent != pWndDlg)
1904 retvalue = 0;
1905 goto END;
1908 else
1910 /* No ctrl specified -> start from the beginning */
1911 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
1913 retvalue = 0;
1914 goto END;
1916 if (fPrevious)
1917 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
1920 pWndLast = WIN_LockWndPtr(pWndCtrl);
1921 pWnd = WIN_LockWndPtr(pWndCtrl->next);
1923 while (1)
1925 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
1927 /* Wrap-around to the beginning of the group */
1928 WND *pWndTemp;
1930 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
1931 for ( pWndTemp = WIN_LockWndPtr( pWnd );
1932 pWndTemp;
1933 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
1935 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
1936 if (pWndTemp == pWndCtrl) break;
1938 WIN_ReleaseWndPtr( pWndTemp );
1940 if (pWnd == pWndCtrl) break;
1941 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
1943 WIN_UpdateWndPtr(&pWndLast,pWnd);
1944 if (!fPrevious) break;
1946 WIN_UpdateWndPtr(&pWnd,pWnd->next);
1948 retvalue = pWndLast->hwndSelf;
1950 WIN_ReleaseWndPtr(pWndLast);
1951 WIN_ReleaseWndPtr(pWnd);
1952 END:
1953 WIN_ReleaseWndPtr(pWndCtrl);
1954 WIN_ReleaseWndPtr(pWndDlg);
1956 return retvalue;
1960 /***********************************************************************
1961 * GetNextDlgTabItem16 (USER.228)
1963 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1964 BOOL16 fPrevious )
1966 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
1969 /***********************************************************************
1970 * DIALOG_GetNextTabItem
1972 * Helper for GetNextDlgTabItem
1974 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1976 LONG dsStyle;
1977 LONG exStyle;
1978 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1979 HWND retWnd = 0;
1980 HWND hChildFirst = 0;
1982 if(!hwndCtrl)
1984 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1985 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1987 else
1989 HWND hParent = GetParent(hwndCtrl);
1990 BOOL bValid = FALSE;
1991 while( hParent)
1993 if(hParent == hwndMain)
1995 bValid = TRUE;
1996 break;
1998 hParent = GetParent(hParent);
2000 if(bValid)
2002 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2003 if(!hChildFirst)
2005 if(GetParent(hwndCtrl) != hwndMain)
2006 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2007 else
2009 if(fPrevious)
2010 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2011 else
2012 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2017 while(hChildFirst)
2019 BOOL bCtrl = FALSE;
2020 while(hChildFirst)
2022 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2023 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2024 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2026 bCtrl=TRUE;
2027 break;
2029 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2030 break;
2031 hChildFirst = GetWindow(hChildFirst,wndSearch);
2033 if(hChildFirst)
2035 if(bCtrl)
2036 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2037 else
2038 retWnd = hChildFirst;
2040 if(retWnd) break;
2041 hChildFirst = GetWindow(hChildFirst,wndSearch);
2043 if(!retWnd && hwndCtrl)
2045 HWND hParent = GetParent(hwndCtrl);
2046 while(hParent)
2048 if(hParent == hwndMain) break;
2049 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2050 if(retWnd) break;
2051 hParent = GetParent(hParent);
2053 if(!retWnd)
2054 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2056 return retWnd;
2059 /***********************************************************************
2060 * GetNextDlgTabItem (USER32.276)
2062 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2063 BOOL fPrevious )
2065 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2068 /**********************************************************************
2069 * DIALOG_DlgDirSelect
2071 * Helper function for DlgDirSelect*
2073 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2074 INT id, BOOL win32, BOOL unicode,
2075 BOOL combo )
2077 char *buffer, *ptr;
2078 INT item, size;
2079 BOOL ret;
2080 HWND listbox = GetDlgItem( hwnd, id );
2082 TRACE("%04x '%s' %d\n", hwnd, str, id );
2083 if (!listbox) return FALSE;
2084 if (win32)
2086 item = SendMessageA(listbox, combo ? CB_GETCURSEL
2087 : LB_GETCURSEL, 0, 0 );
2088 if (item == LB_ERR) return FALSE;
2089 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
2090 : LB_GETTEXTLEN, 0, 0 );
2091 if (size == LB_ERR) return FALSE;
2093 else
2095 item = SendMessageA(listbox, combo ? CB_GETCURSEL16
2096 : LB_GETCURSEL16, 0, 0 );
2097 if (item == LB_ERR) return FALSE;
2098 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN16
2099 : LB_GETTEXTLEN16, 0, 0 );
2100 if (size == LB_ERR) return FALSE;
2103 if (!(buffer = SEGPTR_ALLOC( size+1 ))) return FALSE;
2105 if (win32)
2106 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
2107 item, (LPARAM)buffer );
2108 else
2109 SendMessage16( listbox, combo ? CB_GETLBTEXT16 : LB_GETTEXT16,
2110 item, (LPARAM)SEGPTR_GET(buffer) );
2112 if ((ret = (buffer[0] == '['))) /* drive or directory */
2114 if (buffer[1] == '-') /* drive */
2116 buffer[3] = ':';
2117 buffer[4] = 0;
2118 ptr = buffer + 2;
2120 else
2122 buffer[strlen(buffer)-1] = '\\';
2123 ptr = buffer + 1;
2126 else ptr = buffer;
2128 if (unicode) lstrcpynAtoW( (LPWSTR)str, ptr, len );
2129 else lstrcpynA( str, ptr, len );
2130 SEGPTR_FREE( buffer );
2131 TRACE("Returning %d '%s'\n", ret, str );
2132 return ret;
2136 /**********************************************************************
2137 * DIALOG_DlgDirList
2139 * Helper function for DlgDirList*
2141 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2142 INT idStatic, UINT attrib, BOOL combo )
2144 int drive;
2145 HWND hwnd;
2146 LPSTR orig_spec = spec;
2148 #define SENDMSG(msg,wparam,lparam) \
2149 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2150 : SendMessageA( hwnd, msg, wparam, lparam ))
2152 TRACE("%04x '%s' %d %d %04x\n",
2153 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2155 if (spec && spec[0] && (spec[1] == ':'))
2157 drive = toupper( spec[0] ) - 'A';
2158 spec += 2;
2159 if (!DRIVE_SetCurrentDrive( drive )) return FALSE;
2161 else drive = DRIVE_GetCurrentDrive();
2163 /* If the path exists and is a directory, chdir to it */
2164 if (!spec || !spec[0] || DRIVE_Chdir( drive, spec )) spec = "*.*";
2165 else
2167 char *p, *p2;
2168 p = spec;
2169 if ((p2 = strrchr( p, '\\' ))) p = p2;
2170 if ((p2 = strrchr( p, '/' ))) p = p2;
2171 if (p != spec)
2173 char sep = *p;
2174 *p = 0;
2175 if (!DRIVE_Chdir( drive, spec ))
2177 *p = sep; /* Restore the original spec */
2178 return FALSE;
2180 spec = p + 1;
2184 TRACE("path=%c:\\%s mask=%s\n",
2185 'A' + drive, DRIVE_GetDosCwd(drive), spec );
2187 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2189 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2190 if (attrib & DDL_DIRECTORY)
2192 if (!(attrib & DDL_EXCLUSIVE))
2194 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2195 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2196 (LPARAM)spec ) == LB_ERR)
2197 return FALSE;
2199 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2200 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2201 (LPARAM)"*.*" ) == LB_ERR)
2202 return FALSE;
2204 else
2206 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2207 (LPARAM)spec ) == LB_ERR)
2208 return FALSE;
2212 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2214 char temp[512];
2215 int drive = DRIVE_GetCurrentDrive();
2216 strcpy( temp, "A:\\" );
2217 temp[0] += drive;
2218 lstrcpynA( temp + 3, DRIVE_GetDosCwd(drive), sizeof(temp)-3 );
2219 CharLowerA( temp );
2220 /* Can't use PostMessage() here, because the string is on the stack */
2221 SetDlgItemTextA( hDlg, idStatic, temp );
2224 if (orig_spec && (spec != orig_spec))
2226 /* Update the original file spec */
2227 char *p = spec;
2228 while ((*orig_spec++ = *p++));
2231 return TRUE;
2232 #undef SENDMSG
2236 /**********************************************************************
2237 * DIALOG_DlgDirListW
2239 * Helper function for DlgDirList*W
2241 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2242 INT idStatic, UINT attrib, BOOL combo )
2244 if (spec)
2246 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2247 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2248 attrib, combo );
2249 lstrcpyAtoW( spec, specA );
2250 HeapFree( GetProcessHeap(), 0, specA );
2251 return ret;
2253 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2257 /**********************************************************************
2258 * DlgDirSelect (USER.99)
2260 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2262 return DlgDirSelectEx16( hwnd, str, 128, id );
2266 /**********************************************************************
2267 * DlgDirSelectComboBox (USER.194)
2269 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2271 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2275 /**********************************************************************
2276 * DlgDirSelectEx16 (USER.422)
2278 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2280 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, FALSE );
2284 /**********************************************************************
2285 * DlgDirSelectExA (USER32.149)
2287 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2289 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
2293 /**********************************************************************
2294 * DlgDirSelectExW (USER32.150)
2296 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2298 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
2302 /**********************************************************************
2303 * DlgDirSelectComboBoxEx16 (USER.423)
2305 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2306 INT16 id )
2308 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, TRUE );
2312 /**********************************************************************
2313 * DlgDirSelectComboBoxExA (USER32.147)
2315 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2316 INT id )
2318 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
2322 /**********************************************************************
2323 * DlgDirSelectComboBoxExW (USER32.148)
2325 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2326 INT id)
2328 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
2332 /**********************************************************************
2333 * DlgDirList16 (USER.100)
2335 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2336 INT16 idStatic, UINT16 attrib )
2338 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2342 /**********************************************************************
2343 * DlgDirListA (USER32.143)
2345 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2346 INT idStatic, UINT attrib )
2348 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2352 /**********************************************************************
2353 * DlgDirListW (USER32.146)
2355 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2356 INT idStatic, UINT attrib )
2358 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2362 /**********************************************************************
2363 * DlgDirListComboBox16 (USER.195)
2365 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2366 INT16 idStatic, UINT16 attrib )
2368 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2372 /**********************************************************************
2373 * DlgDirListComboBoxA (USER32.144)
2375 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2376 INT idStatic, UINT attrib )
2378 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2382 /**********************************************************************
2383 * DlgDirListComboBoxW (USER32.145)
2385 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2386 INT idStatic, UINT attrib )
2388 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );