Added atfork support.
[wine/multimedia.git] / windows / dialog.c
blobccd3e16737051deb71df2e205a5ff3c28b0227ae
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 info->data = (LPVOID)(*p ? p + 1 : NULL); /* FIXME: should be a segptr */
233 p += *p + 1;
235 if(int_id)
236 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
237 info->className, LOWORD(info->windowName),
238 info->id, info->x, info->y, info->cx, info->cy,
239 info->style, (DWORD)info->data);
240 else
241 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
242 info->className, info->windowName,
243 info->id, info->x, info->y, info->cx, info->cy,
244 info->style, (DWORD)info->data);
246 return p;
250 /***********************************************************************
251 * DIALOG_GetControl32
253 * Return the class and text of the control pointed to by ptr,
254 * fill the header structure and return a pointer to the next control.
256 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
257 BOOL dialogEx )
259 if (dialogEx)
261 info->helpId = GET_DWORD(p); p += 2;
262 info->exStyle = GET_DWORD(p); p += 2;
263 info->style = GET_DWORD(p); p += 2;
265 else
267 info->helpId = 0;
268 info->style = GET_DWORD(p); p += 2;
269 info->exStyle = GET_DWORD(p); p += 2;
271 info->x = GET_WORD(p); p++;
272 info->y = GET_WORD(p); p++;
273 info->cx = GET_WORD(p); p++;
274 info->cy = GET_WORD(p); p++;
276 if (dialogEx)
278 /* id is a DWORD for DIALOGEX */
279 info->id = GET_DWORD(p);
280 p += 2;
282 else
284 info->id = GET_WORD(p);
285 p++;
288 if (GET_WORD(p) == 0xffff)
290 static const WCHAR class_names[6][10] =
292 { 'B','u','t','t','o','n', }, /* 0x80 */
293 { 'E','d','i','t', }, /* 0x81 */
294 { 'S','t','a','t','i','c', }, /* 0x82 */
295 { 'L','i','s','t','B','o','x', }, /* 0x83 */
296 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
297 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
299 WORD id = GET_WORD(p+1);
300 if ((id >= 0x80) && (id <= 0x85))
301 info->className = (LPCSTR)class_names[id - 0x80];
302 else
304 info->className = NULL;
305 ERR("Unknown built-in class id %04x\n", id );
307 p += 2;
309 else
311 info->className = (LPCSTR)p;
312 p += lstrlenW( (LPCWSTR)p ) + 1;
315 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
317 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
318 p += 2;
320 else
322 info->windowName = (LPCSTR)p;
323 p += lstrlenW( (LPCWSTR)p ) + 1;
326 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
327 debugstr_w( (LPCWSTR)info->className ),
328 debugres_w( (LPCWSTR)info->windowName ),
329 info->id, info->x, info->y, info->cx, info->cy,
330 info->style, info->exStyle, info->helpId );
332 if (GET_WORD(p))
334 if (TRACE_ON(dialog))
336 WORD i, count = GET_WORD(p) / sizeof(WORD);
337 TRACE(" BEGIN\n");
338 TRACE(" ");
339 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
340 DPRINTF("\n");
341 TRACE(" END\n" );
343 info->data = (LPVOID)(p + 1);
344 p += GET_WORD(p) / sizeof(WORD);
346 else info->data = NULL;
347 p++;
349 /* Next control is on dword boundary */
350 return (const WORD *)((((int)p) + 3) & ~3);
354 /***********************************************************************
355 * DIALOG_CreateControls
357 * Create the control windows for a dialog.
359 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
360 const DLG_TEMPLATE *dlgTemplate,
361 HINSTANCE hInst, BOOL win32 )
363 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
364 DLG_CONTROL_INFO info;
365 HWND hwndCtrl, hwndDefButton = 0;
366 INT items = dlgTemplate->nbItems;
368 TRACE(" BEGIN\n" );
369 while (items--)
371 if (!win32)
373 HINSTANCE16 instance;
374 template = DIALOG_GetControl16( template, &info );
375 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
376 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
378 if (!dlgInfo->hDialogHeap)
380 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
381 if (!dlgInfo->hDialogHeap)
383 ERR("Insufficient memory to create heap for edit control\n" );
384 continue;
386 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
388 instance = dlgInfo->hDialogHeap;
390 else instance = (HINSTANCE16)hInst;
392 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
393 info.className, info.windowName,
394 info.style | WS_CHILD,
395 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
396 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
397 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
398 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
399 pWnd->hwndSelf, (HMENU16)info.id,
400 instance, info.data );
402 else
404 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
405 dlgTemplate->dialogEx );
406 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
407 (LPCWSTR)info.className,
408 (LPCWSTR)info.windowName,
409 info.style | WS_CHILD,
410 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
411 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
412 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
413 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
414 pWnd->hwndSelf, (HMENU)info.id,
415 hInst, info.data );
417 if (!hwndCtrl) return FALSE;
419 /* Send initialisation messages to the control */
420 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
421 (WPARAM)dlgInfo->hUserFont, 0 );
422 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
424 /* If there's already a default push-button, set it back */
425 /* to normal and use this one instead. */
426 if (hwndDefButton)
427 SendMessageA( hwndDefButton, BM_SETSTYLE,
428 BS_PUSHBUTTON,FALSE );
429 hwndDefButton = hwndCtrl;
430 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
433 TRACE(" END\n" );
434 return TRUE;
438 /***********************************************************************
439 * DIALOG_ParseTemplate16
441 * Fill a DLG_TEMPLATE structure from the dialog template, and return
442 * a pointer to the first control.
444 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
446 result->style = GET_DWORD(p); p += sizeof(DWORD);
447 result->exStyle = 0;
448 result->nbItems = (unsigned char) *p++;
449 result->x = GET_WORD(p); p += sizeof(WORD);
450 result->y = GET_WORD(p); p += sizeof(WORD);
451 result->cx = GET_WORD(p); p += sizeof(WORD);
452 result->cy = GET_WORD(p); p += sizeof(WORD);
453 TRACE("DIALOG %d, %d, %d, %d\n",
454 result->x, result->y, result->cx, result->cy );
455 TRACE(" STYLE %08lx\n", result->style );
457 /* Get the menu name */
459 switch( (BYTE)*p )
461 case 0:
462 result->menuName = 0;
463 p++;
464 break;
465 case 0xff:
466 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
467 p += 3;
468 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
469 break;
470 default:
471 result->menuName = p;
472 TRACE(" MENU '%s'\n", p );
473 p += strlen(p) + 1;
474 break;
477 /* Get the class name */
479 if (*p)
481 result->className = p;
482 TRACE(" CLASS '%s'\n", result->className );
484 else result->className = DIALOG_CLASS_ATOM;
485 p += strlen(p) + 1;
487 /* Get the window caption */
489 result->caption = p;
490 p += strlen(p) + 1;
491 TRACE(" CAPTION '%s'\n", result->caption );
493 /* Get the font name */
495 if (result->style & DS_SETFONT)
497 result->pointSize = GET_WORD(p);
498 p += sizeof(WORD);
499 result->faceName = p;
500 p += strlen(p) + 1;
501 TRACE(" FONT %d,'%s'\n",
502 result->pointSize, result->faceName );
504 return p;
508 /***********************************************************************
509 * DIALOG_ParseTemplate32
511 * Fill a DLG_TEMPLATE structure from the dialog template, and return
512 * a pointer to the first control.
514 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
516 const WORD *p = (const WORD *)template;
518 result->style = GET_DWORD(p); p += 2;
519 if (result->style == 0xffff0001) /* DIALOGEX resource */
521 result->dialogEx = TRUE;
522 result->helpId = GET_DWORD(p); p += 2;
523 result->exStyle = GET_DWORD(p); p += 2;
524 result->style = GET_DWORD(p); p += 2;
526 else
528 result->dialogEx = FALSE;
529 result->helpId = 0;
530 result->exStyle = GET_DWORD(p); p += 2;
532 result->nbItems = GET_WORD(p); p++;
533 result->x = GET_WORD(p); p++;
534 result->y = GET_WORD(p); p++;
535 result->cx = GET_WORD(p); p++;
536 result->cy = GET_WORD(p); p++;
537 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
538 result->dialogEx ? "EX" : "", result->x, result->y,
539 result->cx, result->cy, result->helpId );
540 TRACE(" STYLE 0x%08lx\n", result->style );
541 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
543 /* Get the menu name */
545 switch(GET_WORD(p))
547 case 0x0000:
548 result->menuName = NULL;
549 p++;
550 break;
551 case 0xffff:
552 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
553 p += 2;
554 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
555 break;
556 default:
557 result->menuName = (LPCSTR)p;
558 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
559 p += lstrlenW( (LPCWSTR)p ) + 1;
560 break;
563 /* Get the class name */
565 switch(GET_WORD(p))
567 case 0x0000:
568 result->className = DIALOG_CLASS_ATOM;
569 p++;
570 break;
571 case 0xffff:
572 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
573 p += 2;
574 TRACE(" CLASS %04x\n", LOWORD(result->className) );
575 break;
576 default:
577 result->className = (LPCSTR)p;
578 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
579 p += lstrlenW( (LPCWSTR)p ) + 1;
580 break;
583 /* Get the window caption */
585 result->caption = (LPCSTR)p;
586 p += lstrlenW( (LPCWSTR)p ) + 1;
587 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
589 /* Get the font name */
591 if (result->style & DS_SETFONT)
593 result->pointSize = GET_WORD(p);
594 p++;
595 if (result->dialogEx)
597 result->weight = GET_WORD(p); p++;
598 result->italic = LOBYTE(GET_WORD(p)); p++;
600 else
602 result->weight = FW_DONTCARE;
603 result->italic = FALSE;
605 result->faceName = (LPCSTR)p;
606 p += lstrlenW( (LPCWSTR)p ) + 1;
607 TRACE(" FONT %d, %s, %d, %s\n",
608 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
609 result->weight, result->italic ? "TRUE" : "FALSE" );
612 /* First control is on dword boundary */
613 return (LPCSTR)((((int)p) + 3) & ~3);
617 /***********************************************************************
618 * DIALOG_CreateIndirect
620 HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
621 BOOL win32Template, HWND owner,
622 DLGPROC16 dlgProc, LPARAM param,
623 WINDOWPROCTYPE procType )
625 HMENU16 hMenu = 0;
626 HFONT16 hFont = 0;
627 HWND hwnd;
628 RECT rect;
629 WND * wndPtr;
630 DLG_TEMPLATE template;
631 DIALOGINFO * dlgInfo;
632 WORD xUnit = xBaseUnit;
633 WORD yUnit = yBaseUnit;
635 /* Parse dialog template */
637 if (!dlgTemplate) return 0;
638 if (win32Template)
639 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
640 else
641 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
643 /* Load menu */
645 if (template.menuName)
647 if (!win32Template)
649 LPSTR str = SEGPTR_STRDUP( template.menuName );
650 hMenu = LoadMenu16( hInst, SEGPTR_GET(str) );
651 SEGPTR_FREE( str );
653 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
656 /* Create custom font if needed */
658 if (template.style & DS_SETFONT)
660 /* The font height must be negative as it is a point size */
661 /* and must be converted to pixels first */
662 /* (see CreateFont() documentation in the Windows SDK). */
663 HDC dc = GetDC(0);
664 int pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
665 ReleaseDC(0, dc);
667 if (win32Template)
668 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
669 template.italic, FALSE, FALSE,
670 DEFAULT_CHARSET, 0, 0,
671 PROOF_QUALITY, FF_DONTCARE,
672 (LPCWSTR)template.faceName );
673 else
674 hFont = CreateFont16( -pixels, 0, 0, 0, FW_DONTCARE,
675 FALSE, FALSE, FALSE,
676 DEFAULT_CHARSET, 0, 0,
677 PROOF_QUALITY, FF_DONTCARE,
678 template.faceName );
679 if (hFont)
681 SIZE charSize;
682 DIALOG_GetCharSize(hFont,&charSize);
683 xUnit = charSize.cx;
684 yUnit = charSize.cy;
686 TRACE("units = %d,%d\n", xUnit, yUnit );
689 /* Create dialog main window */
691 rect.left = rect.top = 0;
692 rect.right = MulDiv(template.cx, xUnit, 4);
693 rect.bottom = MulDiv(template.cy, yUnit, 8);
694 if (template.style & DS_MODALFRAME)
695 template.exStyle |= WS_EX_DLGMODALFRAME;
696 AdjustWindowRectEx( &rect, template.style,
697 hMenu ? TRUE : FALSE , template.exStyle );
698 rect.right -= rect.left;
699 rect.bottom -= rect.top;
701 if ((INT16)template.x == CW_USEDEFAULT16)
703 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
705 else
707 if (template.style & DS_CENTER)
709 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
710 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
712 else
714 rect.left += MulDiv(template.x, xUnit, 4);
715 rect.top += MulDiv(template.y, yUnit, 8);
717 if ( !(template.style & WS_CHILD) )
719 INT16 dX, dY;
721 if( !(template.style & DS_ABSALIGN) )
722 ClientToScreen( owner, (POINT *)&rect );
724 /* try to fit it into the desktop */
726 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
727 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
728 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
729 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
730 if( rect.left < 0 ) rect.left = 0;
731 if( rect.top < 0 ) rect.top = 0;
735 if (!win32Template)
736 hwnd = CreateWindowEx16(template.exStyle, template.className,
737 template.caption, template.style & ~WS_VISIBLE,
738 rect.left, rect.top, rect.right, rect.bottom,
739 owner, hMenu, hInst, NULL );
740 else
741 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
742 (LPCWSTR)template.caption,
743 template.style & ~WS_VISIBLE,
744 rect.left, rect.top, rect.right, rect.bottom,
745 owner, hMenu, hInst, NULL );
747 if (!hwnd)
749 if (hFont) DeleteObject( hFont );
750 if (hMenu) DestroyMenu( hMenu );
751 return 0;
753 wndPtr = WIN_FindWndPtr( hwnd );
754 wndPtr->flags |= WIN_ISDIALOG;
755 wndPtr->helpContext = template.helpId;
757 /* Initialise dialog extra data */
759 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
760 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
761 dlgInfo->hUserFont = hFont;
762 dlgInfo->hMenu = hMenu;
763 dlgInfo->xBaseUnit = xUnit;
764 dlgInfo->yBaseUnit = yUnit;
765 dlgInfo->msgResult = 0;
766 dlgInfo->idResult = 0;
767 dlgInfo->flags = 0;
768 dlgInfo->hDialogHeap = 0;
770 if (dlgInfo->hUserFont)
771 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
773 /* Create controls */
775 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
776 hInst, win32Template ))
778 HWND hwndPreInitFocus;
780 /* Send initialisation messages and set focus */
782 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
784 hwndPreInitFocus = GetFocus();
785 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
786 SetFocus( dlgInfo->hwndFocus );
787 else
789 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
790 but the focus has not changed, set the focus where we expect it. */
791 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
792 SetFocus( dlgInfo->hwndFocus );
795 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
797 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
798 UpdateWindow( hwnd );
800 WIN_ReleaseWndPtr(wndPtr);
801 return hwnd;
803 WIN_ReleaseWndPtr(wndPtr);
804 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
805 return 0;
809 /***********************************************************************
810 * CreateDialog16 (USER.89)
812 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
813 HWND16 owner, DLGPROC16 dlgProc )
815 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
819 /***********************************************************************
820 * CreateDialogParam16 (USER.241)
822 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
823 HWND16 owner, DLGPROC16 dlgProc,
824 LPARAM param )
826 HWND16 hwnd = 0;
827 HRSRC16 hRsrc;
828 HGLOBAL16 hmem;
829 LPCVOID data;
831 TRACE("%04x,%08lx,%04x,%08lx,%ld\n",
832 hInst, (DWORD)dlgTemplate, owner, (DWORD)dlgProc, param );
834 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOG16 ))) return 0;
835 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
836 if (!(data = LockResource16( hmem ))) hwnd = 0;
837 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
838 dlgProc, param );
839 FreeResource16( hmem );
840 return hwnd;
843 /***********************************************************************
844 * CreateDialogParamA (USER32.73)
846 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
847 HWND owner, DLGPROC dlgProc,
848 LPARAM param )
850 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
851 if (!hrsrc) return 0;
852 return CreateDialogIndirectParamA( hInst,
853 (LPVOID)LoadResource(hInst, hrsrc),
854 owner, dlgProc, param );
858 /***********************************************************************
859 * CreateDialogParamW (USER32.74)
861 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
862 HWND owner, DLGPROC dlgProc,
863 LPARAM param )
865 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
866 if (!hrsrc) return 0;
867 return CreateDialogIndirectParamW( hInst,
868 (LPVOID)LoadResource(hInst, hrsrc),
869 owner, dlgProc, param );
873 /***********************************************************************
874 * CreateDialogIndirect16 (USER.219)
876 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
877 HWND16 owner, DLGPROC16 dlgProc )
879 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
883 /***********************************************************************
884 * CreateDialogIndirectParam16 (USER.242)
886 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
887 LPCVOID dlgTemplate,
888 HWND16 owner, DLGPROC16 dlgProc,
889 LPARAM param )
891 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
892 dlgProc, param, WIN_PROC_16 );
896 /***********************************************************************
897 * CreateDialogIndirectParamA (USER32.69)
899 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
900 LPCVOID dlgTemplate,
901 HWND owner, DLGPROC dlgProc,
902 LPARAM param )
904 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
905 (DLGPROC16)dlgProc, param, WIN_PROC_32A );
908 /***********************************************************************
909 * CreateDialogIndirectParamAorW (USER32.71)
911 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
912 LPCVOID dlgTemplate,
913 HWND owner, DLGPROC dlgProc,
914 LPARAM param )
915 { FIXME("assume WIN_PROC_32W\n");
916 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
917 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
920 /***********************************************************************
921 * CreateDialogIndirectParamW (USER32.72)
923 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
924 LPCVOID dlgTemplate,
925 HWND owner, DLGPROC dlgProc,
926 LPARAM param )
928 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
929 (DLGPROC16)dlgProc, param, WIN_PROC_32W );
933 /***********************************************************************
934 * DIALOG_DoDialogBox
936 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
938 WND * wndPtr;
939 DIALOGINFO * dlgInfo;
940 MSG msg;
941 INT retval;
943 /* Owner must be a top-level window */
944 owner = WIN_GetTopParent( owner );
945 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
946 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
948 if (!dlgInfo->flags & DF_END) /* was EndDialog called in WM_INITDIALOG ? */
950 EnableWindow( owner, FALSE );
951 ShowWindow( hwnd, SW_SHOW );
952 while (MSG_InternalGetMessage(QMSG_WIN32A, &msg, hwnd, owner, MSGF_DIALOGBOX,
953 PM_REMOVE, !(wndPtr->dwStyle & DS_NOIDLEMSG), NULL ))
955 if (!IsDialogMessageA( hwnd, &msg))
957 TranslateMessage( &msg );
958 DispatchMessageA( &msg );
960 if (dlgInfo->flags & DF_END) break;
962 EnableWindow( owner, TRUE );
964 retval = dlgInfo->idResult;
965 WIN_ReleaseWndPtr(wndPtr);
966 DestroyWindow( hwnd );
967 return retval;
971 /***********************************************************************
972 * DialogBox16 (USER.87)
974 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, SEGPTR dlgTemplate,
975 HWND16 owner, DLGPROC16 dlgProc )
977 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
981 /***********************************************************************
982 * DialogBoxParam16 (USER.239)
984 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, SEGPTR template,
985 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
987 HWND16 hwnd = CreateDialogParam16( hInst, template, owner, dlgProc, param);
988 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
989 return -1;
993 /***********************************************************************
994 * DialogBoxParamA (USER32.139)
996 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
997 HWND owner, DLGPROC dlgProc, LPARAM param )
999 HWND hwnd = CreateDialogParamA( hInst, name, owner, dlgProc, param );
1000 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1001 return -1;
1005 /***********************************************************************
1006 * DialogBoxParamW (USER32.140)
1008 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1009 HWND owner, DLGPROC dlgProc, LPARAM param )
1011 HWND hwnd = CreateDialogParamW( hInst, name, owner, dlgProc, param );
1012 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1013 return -1;
1017 /***********************************************************************
1018 * DialogBoxIndirect16 (USER.218)
1020 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1021 HWND16 owner, DLGPROC16 dlgProc )
1023 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1027 /***********************************************************************
1028 * DialogBoxIndirectParam16 (USER.240)
1030 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1031 HWND16 owner, DLGPROC16 dlgProc,
1032 LPARAM param )
1034 HWND16 hwnd;
1035 LPCVOID ptr;
1037 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1038 hwnd = CreateDialogIndirectParam16( hInst, ptr, owner, dlgProc, param );
1039 GlobalUnlock16( dlgTemplate );
1040 if (hwnd) return (INT16)DIALOG_DoDialogBox( hwnd, owner );
1041 return -1;
1045 /***********************************************************************
1046 * DialogBoxIndirectParamA (USER32.136)
1048 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1049 HWND owner, DLGPROC dlgProc,
1050 LPARAM param )
1052 HWND hwnd = CreateDialogIndirectParamA( hInstance, template,
1053 owner, dlgProc, param );
1054 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1055 return -1;
1059 /***********************************************************************
1060 * DialogBoxIndirectParamW (USER32.138)
1062 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1063 HWND owner, DLGPROC dlgProc,
1064 LPARAM param )
1066 HWND hwnd = CreateDialogIndirectParamW( hInstance, template,
1067 owner, dlgProc, param );
1068 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1069 return -1;
1072 /***********************************************************************
1073 * DialogBoxIndirectParamAorW (USER32.138)
1075 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1076 HWND owner, DLGPROC dlgProc,
1077 LPARAM param, DWORD x )
1079 HWND hwnd;
1080 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1081 hInstance, template, owner, dlgProc, param, x);
1082 hwnd = CreateDialogIndirectParamW( hInstance, template,
1083 owner, dlgProc, param );
1084 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1085 return -1;
1088 /***********************************************************************
1089 * EndDialog16 (USER.88)
1091 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1093 return EndDialog( hwnd, retval );
1097 /***********************************************************************
1098 * EndDialog (USER32.173)
1100 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1102 WND * wndPtr = WIN_FindWndPtr( hwnd );
1103 DIALOGINFO * dlgInfo;
1104 HWND hOwner = 0;
1106 TRACE("%04x %d\n", hwnd, retval );
1108 if (!wndPtr)
1110 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1111 return FALSE;
1114 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1116 dlgInfo->idResult = retval;
1117 dlgInfo->flags |= DF_END;
1120 if(wndPtr->owner)
1121 hOwner = WIN_GetTopParent( wndPtr->owner->hwndSelf );
1123 /* Enable the owner first */
1124 if (hOwner && !IsWindowEnabled(hOwner))
1125 EnableWindow( hOwner, TRUE );
1127 /* Windows sets the focus to the dialog itself in EndDialog */
1129 if (IsChild(hwnd, GetFocus()))
1130 SetFocus(wndPtr->hwndSelf);
1132 /* Don't have to send a ShowWindow(SW_HIDE), just do
1133 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1135 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1136 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1138 WIN_ReleaseWndPtr(wndPtr);
1140 return TRUE;
1144 /***********************************************************************
1145 * DIALOG_IsAccelerator
1147 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1149 HWND hwndControl = hwnd;
1150 HWND hwndNext;
1151 WND *wndPtr;
1152 BOOL RetVal = FALSE;
1153 INT dlgCode;
1157 wndPtr = WIN_FindWndPtr( hwndControl );
1158 if ( (wndPtr != NULL) &&
1159 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1161 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1162 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1163 (wndPtr->text!=NULL))
1165 /* find the accelerator key */
1166 LPSTR p = wndPtr->text - 2;
1169 p = strchr( p + 2, '&' );
1171 while (p != NULL && p[1] == '&');
1173 /* and check if it's the one we're looking for */
1174 if (p != NULL && toupper( p[1] ) == toupper( vKey ) )
1176 if ((dlgCode & DLGC_STATIC) ||
1177 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1179 /* set focus to the control */
1180 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1181 hwndControl, 1);
1182 /* and bump it on to next */
1183 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1185 else if (dlgCode & DLGC_BUTTON)
1187 /* send BM_CLICK message to the control */
1188 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1191 RetVal = TRUE;
1192 WIN_ReleaseWndPtr(wndPtr);
1193 break;
1196 hwndNext = GetWindow( hwndControl, GW_CHILD );
1198 else
1200 hwndNext = 0;
1202 WIN_ReleaseWndPtr(wndPtr);
1203 if (!hwndNext)
1205 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1207 while (!hwndNext && hwndControl)
1209 hwndControl = GetParent( hwndControl );
1210 if (hwndControl == hwndDlg)
1212 if(hwnd==hwndDlg){ /* prevent endless loop */
1213 hwndNext=hwnd;
1214 break;
1216 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1218 else
1220 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1223 hwndControl = hwndNext;
1225 while (hwndControl && (hwndControl != hwnd));
1227 return RetVal;
1230 /***********************************************************************
1231 * DIALOG_FindMsgDestination
1233 * The messages that IsDialogMessage send may not go to the dialog
1234 * calling IsDialogMessage if that dialog is a child, and it has the
1235 * DS_CONTROL style set.
1236 * We propagate up until we hit a that does not have DS_CONTROL, or
1237 * whose parent is not a dialog.
1239 * This is undocumented behaviour.
1241 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1243 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1245 WND *pParent;
1246 HWND hParent = GetParent(hwndDlg);
1247 if (!hParent) break;
1249 pParent = WIN_FindWndPtr(hParent);
1250 if (!pParent) break;
1252 if (!(pParent->flags & WIN_ISDIALOG))
1254 WIN_ReleaseWndPtr(pParent);
1255 break;
1257 WIN_ReleaseWndPtr(pParent);
1259 hwndDlg = hParent;
1262 return hwndDlg;
1265 /***********************************************************************
1266 * DIALOG_IsDialogMessage
1268 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1269 UINT message, WPARAM wParam,
1270 LPARAM lParam, BOOL *translate,
1271 BOOL *dispatch, INT dlgCode )
1273 *translate = *dispatch = FALSE;
1275 if (message == WM_PAINT)
1277 /* Apparently, we have to handle this one as well */
1278 *dispatch = TRUE;
1279 return TRUE;
1282 /* Only the key messages get special processing */
1283 if ((message != WM_KEYDOWN) &&
1284 (message != WM_SYSCHAR) &&
1285 (message != WM_CHAR))
1286 return FALSE;
1288 if (dlgCode & DLGC_WANTMESSAGE)
1290 *translate = *dispatch = TRUE;
1291 return TRUE;
1294 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1296 switch(message)
1298 case WM_KEYDOWN:
1299 switch(wParam)
1301 case VK_TAB:
1302 if (!(dlgCode & DLGC_WANTTAB))
1304 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1305 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1306 return TRUE;
1308 break;
1310 case VK_RIGHT:
1311 case VK_DOWN:
1312 case VK_LEFT:
1313 case VK_UP:
1314 if (!(dlgCode & DLGC_WANTARROWS))
1316 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1317 HWND hwndNext =
1318 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1319 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1320 return TRUE;
1322 break;
1324 case VK_ESCAPE:
1325 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1326 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1327 return TRUE;
1329 case VK_RETURN:
1331 DWORD dw = SendMessage16( hwndDlg, DM_GETDEFID, 0, 0 );
1332 if (HIWORD(dw) == DC_HASDEFID)
1334 SendMessageA( hwndDlg, WM_COMMAND,
1335 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1336 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1338 else
1340 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1341 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1345 return TRUE;
1347 *translate = TRUE;
1348 break; /* case WM_KEYDOWN */
1350 case WM_CHAR:
1351 if (dlgCode & DLGC_WANTCHARS) break;
1352 /* drop through */
1354 case WM_SYSCHAR:
1355 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1357 /* don't translate or dispatch */
1358 return TRUE;
1360 break;
1363 /* If we get here, the message has not been treated specially */
1364 /* and can be sent to its destination window. */
1365 *dispatch = TRUE;
1366 return TRUE;
1370 /***********************************************************************
1371 * IsDialogMessage16 (USER.90)
1373 BOOL16 WINAPI WIN16_IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1375 LPMSG16 msg = PTR_SEG_TO_LIN(msg16);
1376 BOOL ret, translate, dispatch;
1377 INT dlgCode = 0;
1379 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1380 return FALSE;
1382 if ((msg->message == WM_KEYDOWN) ||
1383 (msg->message == WM_SYSCHAR) ||
1384 (msg->message == WM_CHAR))
1386 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1388 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1389 msg->wParam, msg->lParam,
1390 &translate, &dispatch, dlgCode );
1391 if (translate) TranslateMessage16( msg );
1392 if (dispatch) DispatchMessage16( msg );
1393 return ret;
1397 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, LPMSG16 msg )
1399 LPMSG16 msg16 = SEGPTR_NEW(MSG16);
1400 BOOL ret;
1402 *msg16 = *msg;
1403 ret = WIN16_IsDialogMessage16( hwndDlg, SEGPTR_GET(msg16) );
1404 SEGPTR_FREE(msg16);
1405 return ret;
1408 /***********************************************************************
1409 * IsDialogMessageA (USER32.342)
1411 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1413 BOOL ret, translate, dispatch;
1414 INT dlgCode = 0;
1416 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1417 return FALSE;
1419 if ((msg->message == WM_KEYDOWN) ||
1420 (msg->message == WM_SYSCHAR) ||
1421 (msg->message == WM_CHAR))
1423 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1425 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1426 msg->wParam, msg->lParam,
1427 &translate, &dispatch, dlgCode );
1428 if (translate) TranslateMessage( msg );
1429 if (dispatch) DispatchMessageA( msg );
1430 return ret;
1434 /***********************************************************************
1435 * IsDialogMessageW (USER32.343)
1437 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1439 BOOL ret, translate, dispatch;
1440 INT dlgCode = 0;
1442 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1443 return FALSE;
1445 if ((msg->message == WM_KEYDOWN) ||
1446 (msg->message == WM_SYSCHAR) ||
1447 (msg->message == WM_CHAR))
1449 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1451 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1452 msg->wParam, msg->lParam,
1453 &translate, &dispatch, dlgCode );
1454 if (translate) TranslateMessage( msg );
1455 if (dispatch) DispatchMessageW( msg );
1456 return ret;
1460 /***********************************************************************
1461 * GetDlgCtrlID16 (USER.277)
1463 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1465 WND *wndPtr = WIN_FindWndPtr(hwnd);
1466 INT16 retvalue;
1468 if (!wndPtr) return 0;
1470 retvalue = wndPtr->wIDmenu;
1471 WIN_ReleaseWndPtr(wndPtr);
1472 return retvalue;
1476 /***********************************************************************
1477 * GetDlgCtrlID (USER32.234)
1479 INT WINAPI GetDlgCtrlID( HWND hwnd )
1481 INT retvalue;
1482 WND *wndPtr = WIN_FindWndPtr(hwnd);
1483 if (!wndPtr) return 0;
1484 retvalue = wndPtr->wIDmenu;
1485 WIN_ReleaseWndPtr(wndPtr);
1486 return retvalue;
1490 /***********************************************************************
1491 * GetDlgItem16 (USER.91)
1493 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1495 WND *pWnd;
1497 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1498 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1499 if (pWnd->wIDmenu == (UINT16)id)
1501 HWND16 retvalue = pWnd->hwndSelf;
1502 WIN_ReleaseWndPtr(pWnd);
1503 return retvalue;
1505 return 0;
1509 /***********************************************************************
1510 * GetDlgItem (USER32.235)
1512 HWND WINAPI GetDlgItem( HWND hwndDlg, INT 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 HWND retvalue = pWnd->hwndSelf;
1521 WIN_ReleaseWndPtr(pWnd);
1522 return retvalue;
1524 return 0;
1528 /*******************************************************************
1529 * SendDlgItemMessage16 (USER.101)
1531 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1532 WPARAM16 wParam, LPARAM lParam )
1534 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1535 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1536 else return 0;
1540 /*******************************************************************
1541 * SendDlgItemMessageA (USER32.452)
1543 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1544 WPARAM wParam, LPARAM lParam )
1546 HWND hwndCtrl = GetDlgItem( hwnd, id );
1547 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1548 else return 0;
1552 /*******************************************************************
1553 * SendDlgItemMessageW (USER32.453)
1555 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1556 WPARAM wParam, LPARAM lParam )
1558 HWND hwndCtrl = GetDlgItem( hwnd, id );
1559 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1560 else return 0;
1564 /*******************************************************************
1565 * SetDlgItemText16 (USER.92)
1567 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1569 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1573 /*******************************************************************
1574 * SetDlgItemTextA (USER32.478)
1576 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1578 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1582 /*******************************************************************
1583 * SetDlgItemTextW (USER32.479)
1585 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1587 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1591 /***********************************************************************
1592 * GetDlgItemText16 (USER.93)
1594 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1596 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1597 len, (LPARAM)str );
1601 /***********************************************************************
1602 * GetDlgItemTextA (USER32.237)
1604 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1606 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1607 len, (LPARAM)str );
1611 /***********************************************************************
1612 * GetDlgItemTextW (USER32.238)
1614 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1616 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1617 len, (LPARAM)str );
1621 /*******************************************************************
1622 * SetDlgItemInt16 (USER.94)
1624 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1626 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1630 /*******************************************************************
1631 * SetDlgItemInt (USER32.477)
1633 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1634 BOOL fSigned )
1636 char str[20];
1638 if (fSigned) sprintf( str, "%d", (INT)value );
1639 else sprintf( str, "%u", value );
1640 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1641 return TRUE;
1645 /***********************************************************************
1646 * GetDlgItemInt16 (USER.95)
1648 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1649 BOOL16 fSigned )
1651 UINT result;
1652 BOOL ok;
1654 if (translated) *translated = FALSE;
1655 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1656 if (!ok) return 0;
1657 if (fSigned)
1659 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1661 else
1663 if (result > 65535) return 0;
1665 if (translated) *translated = TRUE;
1666 return (UINT16)result;
1670 /***********************************************************************
1671 * GetDlgItemInt (USER32.236)
1673 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1674 BOOL fSigned )
1676 char str[30];
1677 char * endptr;
1678 long result = 0;
1680 if (translated) *translated = FALSE;
1681 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1682 return 0;
1683 if (fSigned)
1685 result = strtol( str, &endptr, 10 );
1686 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1687 return 0;
1688 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1689 return 0;
1691 else
1693 result = strtoul( str, &endptr, 10 );
1694 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1695 return 0;
1696 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1698 if (translated) *translated = TRUE;
1699 return (UINT)result;
1703 /***********************************************************************
1704 * CheckDlgButton16 (USER.97)
1706 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1708 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1709 return TRUE;
1713 /***********************************************************************
1714 * CheckDlgButton (USER32.45)
1716 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1718 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1719 return TRUE;
1723 /***********************************************************************
1724 * IsDlgButtonChecked16 (USER.98)
1726 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1728 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1732 /***********************************************************************
1733 * IsDlgButtonChecked (USER32.344)
1735 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1737 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1741 /***********************************************************************
1742 * CheckRadioButton16 (USER.96)
1744 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1745 UINT16 lastID, UINT16 checkID )
1747 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1751 /***********************************************************************
1752 * CheckRB
1754 * Callback function used to check/uncheck radio buttons that fall
1755 * within a specific range of IDs.
1757 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1759 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1760 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1762 if ((lChildID >= lpRadioGroup->firstID) &&
1763 (lChildID <= lpRadioGroup->lastID))
1765 if (lChildID == lpRadioGroup->checkID)
1767 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1769 else
1771 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1775 return TRUE;
1779 /***********************************************************************
1780 * CheckRadioButton (USER32.48)
1782 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1783 UINT lastID, UINT checkID )
1785 RADIOGROUP radioGroup;
1787 /* perform bounds checking for a radio button group */
1788 radioGroup.firstID = min(min(firstID, lastID), checkID);
1789 radioGroup.lastID = max(max(firstID, lastID), checkID);
1790 radioGroup.checkID = checkID;
1792 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1793 (LPARAM)&radioGroup);
1797 /***********************************************************************
1798 * GetDialogBaseUnits (USER.243) (USER32.233)
1800 DWORD WINAPI GetDialogBaseUnits(void)
1802 return MAKELONG( xBaseUnit, yBaseUnit );
1806 /***********************************************************************
1807 * MapDialogRect16 (USER.103)
1809 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1811 DIALOGINFO * dlgInfo;
1812 WND * wndPtr = WIN_FindWndPtr( hwnd );
1813 if (!wndPtr) return;
1814 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1815 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1816 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1817 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1818 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1819 WIN_ReleaseWndPtr(wndPtr);
1823 /***********************************************************************
1824 * MapDialogRect (USER32.382)
1826 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1828 DIALOGINFO * dlgInfo;
1829 WND * wndPtr = WIN_FindWndPtr( hwnd );
1830 if (!wndPtr) return FALSE;
1831 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1832 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1833 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1834 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1835 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1836 WIN_ReleaseWndPtr(wndPtr);
1837 return TRUE;
1841 /***********************************************************************
1842 * GetNextDlgGroupItem16 (USER.227)
1844 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1845 BOOL16 fPrevious )
1847 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1851 /***********************************************************************
1852 * GetNextDlgGroupItem (USER32.275)
1854 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1855 BOOL fPrevious )
1857 WND *pWnd = NULL,
1858 *pWndLast = NULL,
1859 *pWndCtrl = NULL,
1860 *pWndDlg = NULL;
1861 HWND retvalue;
1863 if(hwndCtrl)
1865 /* if the hwndCtrl is the child of the control in the hwndDlg then the hwndDlg has to be the parent of the hwndCtrl */
1866 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1867 hwndDlg = GetParent(hwndCtrl);
1870 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
1871 if (hwndCtrl)
1873 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
1875 retvalue = 0;
1876 goto END;
1878 /* Make sure hwndCtrl is a top-level child */
1879 while ((pWndCtrl->dwStyle & WS_CHILD) && (pWndCtrl->parent != pWndDlg))
1880 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
1881 if (pWndCtrl->parent != pWndDlg)
1883 retvalue = 0;
1884 goto END;
1887 else
1889 /* No ctrl specified -> start from the beginning */
1890 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
1892 retvalue = 0;
1893 goto END;
1895 if (fPrevious)
1896 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
1899 pWndLast = WIN_LockWndPtr(pWndCtrl);
1900 pWnd = WIN_LockWndPtr(pWndCtrl->next);
1902 while (1)
1904 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
1906 /* Wrap-around to the beginning of the group */
1907 WND *pWndTemp;
1909 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
1910 for ( pWndTemp = WIN_LockWndPtr( pWnd );
1911 pWndTemp;
1912 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
1914 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
1915 if (pWndTemp == pWndCtrl) break;
1917 WIN_ReleaseWndPtr( pWndTemp );
1919 if (pWnd == pWndCtrl) break;
1920 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
1922 WIN_UpdateWndPtr(&pWndLast,pWnd);
1923 if (!fPrevious) break;
1925 WIN_UpdateWndPtr(&pWnd,pWnd->next);
1927 retvalue = pWndLast->hwndSelf;
1929 WIN_ReleaseWndPtr(pWndLast);
1930 WIN_ReleaseWndPtr(pWnd);
1931 END:
1932 WIN_ReleaseWndPtr(pWndCtrl);
1933 WIN_ReleaseWndPtr(pWndDlg);
1935 return retvalue;
1939 /***********************************************************************
1940 * GetNextDlgTabItem16 (USER.228)
1942 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1943 BOOL16 fPrevious )
1945 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
1948 /***********************************************************************
1949 * DIALOG_GetNextTabItem
1951 * Helper for GetNextDlgTabItem
1953 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1955 LONG dsStyle;
1956 LONG exStyle;
1957 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1958 HWND retWnd = 0;
1959 HWND hChildFirst = 0;
1961 if(!hwndCtrl)
1963 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1964 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1966 else
1968 HWND hParent = GetParent(hwndCtrl);
1969 BOOL bValid = FALSE;
1970 while( hParent)
1972 if(hParent == hwndMain)
1974 bValid = TRUE;
1975 break;
1977 hParent = GetParent(hParent);
1979 if(bValid)
1981 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1982 if(!hChildFirst)
1984 if(GetParent(hwndCtrl) != hwndMain)
1985 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1986 else
1988 if(fPrevious)
1989 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1990 else
1991 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1996 while(hChildFirst)
1998 BOOL bCtrl = FALSE;
1999 while(hChildFirst)
2001 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2002 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2003 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2005 bCtrl=TRUE;
2006 break;
2008 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2009 break;
2010 hChildFirst = GetWindow(hChildFirst,wndSearch);
2012 if(hChildFirst)
2014 if(bCtrl)
2015 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2016 else
2017 retWnd = hChildFirst;
2019 if(retWnd) break;
2020 hChildFirst = GetWindow(hChildFirst,wndSearch);
2022 if(!retWnd && hwndCtrl)
2024 HWND hParent = GetParent(hwndCtrl);
2025 while(hParent)
2027 if(hParent == hwndMain) break;
2028 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2029 if(retWnd) break;
2030 hParent = GetParent(hParent);
2032 if(!retWnd)
2033 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2035 return retWnd;
2038 /***********************************************************************
2039 * GetNextDlgTabItem (USER32.276)
2041 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2042 BOOL fPrevious )
2044 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2047 /**********************************************************************
2048 * DIALOG_DlgDirSelect
2050 * Helper function for DlgDirSelect*
2052 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2053 INT id, BOOL win32, BOOL unicode,
2054 BOOL combo )
2056 char *buffer, *ptr;
2057 INT item, size;
2058 BOOL ret;
2059 HWND listbox = GetDlgItem( hwnd, id );
2061 TRACE("%04x '%s' %d\n", hwnd, str, id );
2062 if (!listbox) return FALSE;
2063 if (win32)
2065 item = SendMessageA(listbox, combo ? CB_GETCURSEL
2066 : LB_GETCURSEL, 0, 0 );
2067 if (item == LB_ERR) return FALSE;
2068 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
2069 : LB_GETTEXTLEN, 0, 0 );
2070 if (size == LB_ERR) return FALSE;
2072 else
2074 item = SendMessageA(listbox, combo ? CB_GETCURSEL16
2075 : LB_GETCURSEL16, 0, 0 );
2076 if (item == LB_ERR) return FALSE;
2077 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN16
2078 : LB_GETTEXTLEN16, 0, 0 );
2079 if (size == LB_ERR) return FALSE;
2082 if (!(buffer = SEGPTR_ALLOC( size+1 ))) return FALSE;
2084 if (win32)
2085 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
2086 item, (LPARAM)buffer );
2087 else
2088 SendMessage16( listbox, combo ? CB_GETLBTEXT16 : LB_GETTEXT16,
2089 item, (LPARAM)SEGPTR_GET(buffer) );
2091 if ((ret = (buffer[0] == '['))) /* drive or directory */
2093 if (buffer[1] == '-') /* drive */
2095 buffer[3] = ':';
2096 buffer[4] = 0;
2097 ptr = buffer + 2;
2099 else
2101 buffer[strlen(buffer)-1] = '\\';
2102 ptr = buffer + 1;
2105 else ptr = buffer;
2107 if (unicode) lstrcpynAtoW( (LPWSTR)str, ptr, len );
2108 else lstrcpynA( str, ptr, len );
2109 SEGPTR_FREE( buffer );
2110 TRACE("Returning %d '%s'\n", ret, str );
2111 return ret;
2115 /**********************************************************************
2116 * DIALOG_DlgDirList
2118 * Helper function for DlgDirList*
2120 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2121 INT idStatic, UINT attrib, BOOL combo )
2123 int drive;
2124 HWND hwnd;
2125 LPSTR orig_spec = spec;
2127 #define SENDMSG(msg,wparam,lparam) \
2128 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2129 : SendMessageA( hwnd, msg, wparam, lparam ))
2131 TRACE("%04x '%s' %d %d %04x\n",
2132 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2134 if (spec && spec[0] && (spec[1] == ':'))
2136 drive = toupper( spec[0] ) - 'A';
2137 spec += 2;
2138 if (!DRIVE_SetCurrentDrive( drive )) return FALSE;
2140 else drive = DRIVE_GetCurrentDrive();
2142 /* If the path exists and is a directory, chdir to it */
2143 if (!spec || !spec[0] || DRIVE_Chdir( drive, spec )) spec = "*.*";
2144 else
2146 char *p, *p2;
2147 p = spec;
2148 if ((p2 = strrchr( p, '\\' ))) p = p2;
2149 if ((p2 = strrchr( p, '/' ))) p = p2;
2150 if (p != spec)
2152 char sep = *p;
2153 *p = 0;
2154 if (!DRIVE_Chdir( drive, spec ))
2156 *p = sep; /* Restore the original spec */
2157 return FALSE;
2159 spec = p + 1;
2163 TRACE("path=%c:\\%s mask=%s\n",
2164 'A' + drive, DRIVE_GetDosCwd(drive), spec );
2166 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2168 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2169 if (attrib & DDL_DIRECTORY)
2171 if (!(attrib & DDL_EXCLUSIVE))
2173 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2174 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2175 (LPARAM)spec ) == LB_ERR)
2176 return FALSE;
2178 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2179 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2180 (LPARAM)"*.*" ) == LB_ERR)
2181 return FALSE;
2183 else
2185 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2186 (LPARAM)spec ) == LB_ERR)
2187 return FALSE;
2191 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2193 char temp[512];
2194 int drive = DRIVE_GetCurrentDrive();
2195 strcpy( temp, "A:\\" );
2196 temp[0] += drive;
2197 lstrcpynA( temp + 3, DRIVE_GetDosCwd(drive), sizeof(temp)-3 );
2198 CharLowerA( temp );
2199 /* Can't use PostMessage() here, because the string is on the stack */
2200 SetDlgItemTextA( hDlg, idStatic, temp );
2203 if (orig_spec && (spec != orig_spec))
2205 /* Update the original file spec */
2206 char *p = spec;
2207 while ((*orig_spec++ = *p++));
2210 return TRUE;
2211 #undef SENDMSG
2215 /**********************************************************************
2216 * DIALOG_DlgDirListW
2218 * Helper function for DlgDirList*W
2220 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2221 INT idStatic, UINT attrib, BOOL combo )
2223 if (spec)
2225 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2226 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2227 attrib, combo );
2228 lstrcpyAtoW( spec, specA );
2229 HeapFree( GetProcessHeap(), 0, specA );
2230 return ret;
2232 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2236 /**********************************************************************
2237 * DlgDirSelect (USER.99)
2239 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2241 return DlgDirSelectEx16( hwnd, str, 128, id );
2245 /**********************************************************************
2246 * DlgDirSelectComboBox (USER.194)
2248 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2250 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2254 /**********************************************************************
2255 * DlgDirSelectEx16 (USER.422)
2257 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2259 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, FALSE );
2263 /**********************************************************************
2264 * DlgDirSelectExA (USER32.149)
2266 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2268 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
2272 /**********************************************************************
2273 * DlgDirSelectExW (USER32.150)
2275 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2277 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
2281 /**********************************************************************
2282 * DlgDirSelectComboBoxEx16 (USER.423)
2284 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2285 INT16 id )
2287 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, TRUE );
2291 /**********************************************************************
2292 * DlgDirSelectComboBoxExA (USER32.147)
2294 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2295 INT id )
2297 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
2301 /**********************************************************************
2302 * DlgDirSelectComboBoxExW (USER32.148)
2304 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2305 INT id)
2307 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
2311 /**********************************************************************
2312 * DlgDirList16 (USER.100)
2314 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2315 INT16 idStatic, UINT16 attrib )
2317 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2321 /**********************************************************************
2322 * DlgDirListA (USER32.143)
2324 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2325 INT idStatic, UINT attrib )
2327 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2331 /**********************************************************************
2332 * DlgDirListW (USER32.146)
2334 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2335 INT idStatic, UINT attrib )
2337 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2341 /**********************************************************************
2342 * DlgDirListComboBox16 (USER.195)
2344 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2345 INT16 idStatic, UINT16 attrib )
2347 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2351 /**********************************************************************
2352 * DlgDirListComboBoxA (USER32.144)
2354 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2355 INT idStatic, UINT attrib )
2357 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2361 /**********************************************************************
2362 * DlgDirListComboBoxW (USER32.145)
2364 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2365 INT idStatic, UINT attrib )
2367 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );