Implemented GetAncestor and removed WIN_GetTopParent.
[wine/multimedia.git] / windows / dialog.c
blob985c230c34b250968384a3f443ffd514f2756509
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>
14 #include "windef.h"
15 #include "winnls.h"
16 #include "winbase.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19 #include "windowsx.h"
20 #include "wine/winuser16.h"
21 #include "wine/winbase16.h"
22 #include "wine/unicode.h"
23 #include "wine/port.h"
24 #include "controls.h"
25 #include "heap.h"
26 #include "win.h"
27 #include "user.h"
28 #include "debugtools.h"
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;
82 /*********************************************************************
83 * dialog class descriptor
85 const struct builtin_class_descr DIALOG_builtin_class =
87 DIALOG_CLASS_ATOM, /* name */
88 CS_GLOBALCLASS | CS_SAVEBITS, /* style */
89 DefDlgProcA, /* procA */
90 DefDlgProcW, /* procW */
91 DLGWINDOWEXTRA, /* extra */
92 IDC_ARROWA, /* cursor */
93 0 /* brush */
97 /***********************************************************************
98 * DIALOG_EnableOwner
100 * Helper function for modal dialogs to enable again the
101 * owner of the dialog box.
103 void DIALOG_EnableOwner( HWND hOwner )
105 /* Owner must be a top-level window */
106 if (hOwner)
107 hOwner = GetAncestor( hOwner, GA_ROOT );
108 if (!hOwner) return;
109 EnableWindow( hOwner, TRUE );
113 /***********************************************************************
114 * DIALOG_DisableOwner
116 * Helper function for modal dialogs to disable the
117 * owner of the dialog box. Returns TRUE if owner was enabled.
119 BOOL DIALOG_DisableOwner( HWND hOwner )
121 /* Owner must be a top-level window */
122 if (hOwner)
123 hOwner = GetAncestor( hOwner, GA_ROOT );
124 if (!hOwner) return FALSE;
125 if (IsWindowEnabled( hOwner ))
127 EnableWindow( hOwner, FALSE );
128 return TRUE;
130 else
131 return FALSE;
134 /***********************************************************************
135 * DIALOG_GetCharSizeFromDC
138 * Calculates the *true* average size of English characters in the
139 * specified font as oppposed to the one returned by GetTextMetrics.
141 * Latest: the X font driver will now compute a proper average width
142 * so this code can be removed
144 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
146 BOOL Success = FALSE;
147 HFONT hFontPrev = 0;
148 pSize->cx = xBaseUnit;
149 pSize->cy = yBaseUnit;
150 if ( hDC )
152 /* select the font */
153 TEXTMETRICA tm;
154 memset(&tm,0,sizeof(tm));
155 if (hFont) hFontPrev = SelectFont(hDC,hFont);
156 if (GetTextMetricsA(hDC,&tm))
158 pSize->cx = tm.tmAveCharWidth;
159 pSize->cy = tm.tmHeight;
161 /* if variable width font */
162 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
164 SIZE total;
165 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
167 /* Calculate a true average as opposed to the one returned
168 * by tmAveCharWidth. This works better when dealing with
169 * proportional spaced fonts and (more important) that's
170 * how Microsoft's dialog creation code calculates the size
171 * of the font
173 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
175 /* round up */
176 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
177 Success = TRUE;
180 else
182 Success = TRUE;
184 /* Use the text metrics */
185 TRACE("Using tm: %ldx%ld (dlg: %ld x %ld) (%s)\n",
186 tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
187 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
188 pSize->cx = tm.tmAveCharWidth;
189 pSize->cy = tm.tmHeight;
191 /* select the original font */
192 if (hFontPrev) SelectFont(hDC,hFontPrev);
194 return (Success);
197 /***********************************************************************
198 * DIALOG_GetCharSize
200 * A convenient variant of DIALOG_GetCharSizeFromDC.
202 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
204 HDC hDC = GetDC(0);
205 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
206 ReleaseDC(0, hDC);
207 return Success;
210 /***********************************************************************
211 * DIALOG_Init
213 * Initialisation of the dialog manager.
215 BOOL DIALOG_Init(void)
217 HDC hdc;
218 SIZE size;
220 /* Calculate the dialog base units */
222 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
224 ERR("Could not create Display DC\n");
225 return FALSE;
228 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
230 DeleteDC( hdc );
231 ERR("Could not initialize base dialog units\n");
232 return FALSE;
235 DeleteDC( hdc );
236 xBaseUnit = size.cx;
237 yBaseUnit = size.cy;
239 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
240 return TRUE;
244 /***********************************************************************
245 * DIALOG_GetControl16
247 * Return the class and text of the control pointed to by ptr,
248 * fill the header structure and return a pointer to the next control.
250 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
252 static char buffer[10];
253 int int_id;
255 info->x = GET_WORD(p); p += sizeof(WORD);
256 info->y = GET_WORD(p); p += sizeof(WORD);
257 info->cx = GET_WORD(p); p += sizeof(WORD);
258 info->cy = GET_WORD(p); p += sizeof(WORD);
259 info->id = GET_WORD(p); p += sizeof(WORD);
260 info->style = GET_DWORD(p); p += sizeof(DWORD);
261 info->exStyle = 0;
263 if (*p & 0x80)
265 switch((BYTE)*p)
267 case 0x80: strcpy( buffer, "BUTTON" ); break;
268 case 0x81: strcpy( buffer, "EDIT" ); break;
269 case 0x82: strcpy( buffer, "STATIC" ); break;
270 case 0x83: strcpy( buffer, "LISTBOX" ); break;
271 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
272 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
273 default: buffer[0] = '\0'; break;
275 info->className = buffer;
276 p++;
278 else
280 info->className = p;
281 p += strlen(p) + 1;
284 int_id = ((BYTE)*p == 0xff);
285 if (int_id)
287 /* Integer id, not documented (?). Only works for SS_ICON controls */
288 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
289 p += 3;
291 else
293 info->windowName = p;
294 p += strlen(p) + 1;
297 if (*p)
299 /* Additional CTLDATA available for this control. */
300 info->data = SEGPTR_ALLOC(*p);
301 memcpy( info->data, p + 1, *p );
303 else info->data = NULL;
305 p += *p + 1;
307 if(int_id)
308 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
309 info->className, LOWORD(info->windowName),
310 info->id, info->x, info->y, info->cx, info->cy,
311 info->style, (DWORD)SEGPTR_GET(info->data) );
312 else
313 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
314 info->className, info->windowName,
315 info->id, info->x, info->y, info->cx, info->cy,
316 info->style, (DWORD)SEGPTR_GET(info->data) );
318 return p;
322 /***********************************************************************
323 * DIALOG_GetControl32
325 * Return the class and text of the control pointed to by ptr,
326 * fill the header structure and return a pointer to the next control.
328 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
329 BOOL dialogEx )
331 if (dialogEx)
333 info->helpId = GET_DWORD(p); p += 2;
334 info->exStyle = GET_DWORD(p); p += 2;
335 info->style = GET_DWORD(p); p += 2;
337 else
339 info->helpId = 0;
340 info->style = GET_DWORD(p); p += 2;
341 info->exStyle = GET_DWORD(p); p += 2;
343 info->x = GET_WORD(p); p++;
344 info->y = GET_WORD(p); p++;
345 info->cx = GET_WORD(p); p++;
346 info->cy = GET_WORD(p); p++;
348 if (dialogEx)
350 /* id is a DWORD for DIALOGEX */
351 info->id = GET_DWORD(p);
352 p += 2;
354 else
356 info->id = GET_WORD(p);
357 p++;
360 if (GET_WORD(p) == 0xffff)
362 static const WCHAR class_names[6][10] =
364 { 'B','u','t','t','o','n', }, /* 0x80 */
365 { 'E','d','i','t', }, /* 0x81 */
366 { 'S','t','a','t','i','c', }, /* 0x82 */
367 { 'L','i','s','t','B','o','x', }, /* 0x83 */
368 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
369 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
371 WORD id = GET_WORD(p+1);
372 if ((id >= 0x80) && (id <= 0x85))
373 info->className = (LPCSTR)class_names[id - 0x80];
374 else
376 info->className = NULL;
377 ERR("Unknown built-in class id %04x\n", id );
379 p += 2;
381 else
383 info->className = (LPCSTR)p;
384 p += strlenW( (LPCWSTR)p ) + 1;
387 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
389 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
390 p += 2;
392 else
394 info->windowName = (LPCSTR)p;
395 p += strlenW( (LPCWSTR)p ) + 1;
398 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
399 debugstr_w( (LPCWSTR)info->className ),
400 debugres_w( (LPCWSTR)info->windowName ),
401 info->id, info->x, info->y, info->cx, info->cy,
402 info->style, info->exStyle, info->helpId );
404 if (GET_WORD(p))
406 if (TRACE_ON(dialog))
408 WORD i, count = GET_WORD(p) / sizeof(WORD);
409 TRACE(" BEGIN\n");
410 TRACE(" ");
411 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
412 DPRINTF("\n");
413 TRACE(" END\n" );
415 info->data = (LPVOID)(p + 1);
416 p += GET_WORD(p) / sizeof(WORD);
418 else info->data = NULL;
419 p++;
421 /* Next control is on dword boundary */
422 return (const WORD *)((((int)p) + 3) & ~3);
426 /***********************************************************************
427 * DIALOG_CreateControls
429 * Create the control windows for a dialog.
431 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
432 const DLG_TEMPLATE *dlgTemplate,
433 HINSTANCE hInst, BOOL win32 )
435 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
436 DLG_CONTROL_INFO info;
437 HWND hwndCtrl, hwndDefButton = 0;
438 INT items = dlgTemplate->nbItems;
440 TRACE(" BEGIN\n" );
441 while (items--)
443 if (!win32)
445 HINSTANCE16 instance;
446 template = DIALOG_GetControl16( template, &info );
447 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
448 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
450 if (!dlgInfo->hDialogHeap)
452 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
453 if (!dlgInfo->hDialogHeap)
455 ERR("Insufficient memory to create heap for edit control\n" );
456 continue;
458 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
460 instance = dlgInfo->hDialogHeap;
462 else instance = (HINSTANCE16)hInst;
464 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
465 info.className, info.windowName,
466 info.style | WS_CHILD,
467 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
468 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
469 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
470 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
471 pWnd->hwndSelf, (HMENU16)info.id,
472 instance, (LPVOID)SEGPTR_GET(info.data) );
474 if (info.data) SEGPTR_FREE(info.data);
476 else
478 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
479 dlgTemplate->dialogEx );
480 /* Is this it? */
481 if (info.style & WS_BORDER)
483 info.style &= ~WS_BORDER;
484 info.exStyle |= WS_EX_CLIENTEDGE;
486 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
487 (LPCWSTR)info.className,
488 (LPCWSTR)info.windowName,
489 info.style | WS_CHILD,
490 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
491 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
492 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
493 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
494 pWnd->hwndSelf, (HMENU)info.id,
495 hInst, info.data );
497 if (!hwndCtrl) return FALSE;
499 /* Send initialisation messages to the control */
500 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
501 (WPARAM)dlgInfo->hUserFont, 0 );
502 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
504 /* If there's already a default push-button, set it back */
505 /* to normal and use this one instead. */
506 if (hwndDefButton)
507 SendMessageA( hwndDefButton, BM_SETSTYLE,
508 BS_PUSHBUTTON,FALSE );
509 hwndDefButton = hwndCtrl;
510 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
513 TRACE(" END\n" );
514 return TRUE;
518 /***********************************************************************
519 * DIALOG_ParseTemplate16
521 * Fill a DLG_TEMPLATE structure from the dialog template, and return
522 * a pointer to the first control.
524 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
526 result->style = GET_DWORD(p); p += sizeof(DWORD);
527 result->exStyle = 0;
528 result->nbItems = (unsigned char) *p++;
529 result->x = GET_WORD(p); p += sizeof(WORD);
530 result->y = GET_WORD(p); p += sizeof(WORD);
531 result->cx = GET_WORD(p); p += sizeof(WORD);
532 result->cy = GET_WORD(p); p += sizeof(WORD);
533 TRACE("DIALOG %d, %d, %d, %d\n",
534 result->x, result->y, result->cx, result->cy );
535 TRACE(" STYLE %08lx\n", result->style );
537 /* Get the menu name */
539 switch( (BYTE)*p )
541 case 0:
542 result->menuName = 0;
543 p++;
544 break;
545 case 0xff:
546 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
547 p += 3;
548 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
549 break;
550 default:
551 result->menuName = p;
552 TRACE(" MENU '%s'\n", p );
553 p += strlen(p) + 1;
554 break;
557 /* Get the class name */
559 if (*p)
561 result->className = p;
562 TRACE(" CLASS '%s'\n", result->className );
564 else result->className = DIALOG_CLASS_ATOM;
565 p += strlen(p) + 1;
567 /* Get the window caption */
569 result->caption = p;
570 p += strlen(p) + 1;
571 TRACE(" CAPTION '%s'\n", result->caption );
573 /* Get the font name */
575 if (result->style & DS_SETFONT)
577 result->pointSize = GET_WORD(p);
578 p += sizeof(WORD);
579 result->faceName = p;
580 p += strlen(p) + 1;
581 TRACE(" FONT %d,'%s'\n",
582 result->pointSize, result->faceName );
584 return p;
588 /***********************************************************************
589 * DIALOG_ParseTemplate32
591 * Fill a DLG_TEMPLATE structure from the dialog template, and return
592 * a pointer to the first control.
594 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
596 const WORD *p = (const WORD *)template;
598 result->style = GET_DWORD(p); p += 2;
599 if (result->style == 0xffff0001) /* DIALOGEX resource */
601 result->dialogEx = TRUE;
602 result->helpId = GET_DWORD(p); p += 2;
603 result->exStyle = GET_DWORD(p); p += 2;
604 result->style = GET_DWORD(p); p += 2;
606 else
608 result->dialogEx = FALSE;
609 result->helpId = 0;
610 result->exStyle = GET_DWORD(p); p += 2;
612 result->nbItems = GET_WORD(p); p++;
613 result->x = GET_WORD(p); p++;
614 result->y = GET_WORD(p); p++;
615 result->cx = GET_WORD(p); p++;
616 result->cy = GET_WORD(p); p++;
617 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
618 result->dialogEx ? "EX" : "", result->x, result->y,
619 result->cx, result->cy, result->helpId );
620 TRACE(" STYLE 0x%08lx\n", result->style );
621 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
623 /* Get the menu name */
625 switch(GET_WORD(p))
627 case 0x0000:
628 result->menuName = NULL;
629 p++;
630 break;
631 case 0xffff:
632 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
633 p += 2;
634 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
635 break;
636 default:
637 result->menuName = (LPCSTR)p;
638 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
639 p += strlenW( (LPCWSTR)p ) + 1;
640 break;
643 /* Get the class name */
645 switch(GET_WORD(p))
647 case 0x0000:
648 result->className = DIALOG_CLASS_ATOM;
649 p++;
650 break;
651 case 0xffff:
652 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
653 p += 2;
654 TRACE(" CLASS %04x\n", LOWORD(result->className) );
655 break;
656 default:
657 result->className = (LPCSTR)p;
658 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
659 p += strlenW( (LPCWSTR)p ) + 1;
660 break;
663 /* Get the window caption */
665 result->caption = (LPCSTR)p;
666 p += strlenW( (LPCWSTR)p ) + 1;
667 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
669 /* Get the font name */
671 if (result->style & DS_SETFONT)
673 result->pointSize = GET_WORD(p);
674 p++;
675 if (result->dialogEx)
677 result->weight = GET_WORD(p); p++;
678 result->italic = LOBYTE(GET_WORD(p)); p++;
680 else
682 result->weight = FW_DONTCARE;
683 result->italic = FALSE;
685 result->faceName = (LPCSTR)p;
686 p += strlenW( (LPCWSTR)p ) + 1;
687 TRACE(" FONT %d, %s, %d, %s\n",
688 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
689 result->weight, result->italic ? "TRUE" : "FALSE" );
692 /* First control is on dword boundary */
693 return (LPCSTR)((((int)p) + 3) & ~3);
697 /***********************************************************************
698 * DIALOG_CreateIndirect
699 * Creates a dialog box window
701 * modal = TRUE if we are called from a modal dialog box.
702 * (it's more compatible to do it here, as under Windows the owner
703 * is never disabled if the dialog fails because of an invalid template)
705 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
706 BOOL win32Template, HWND owner,
707 DLGPROC16 dlgProc, LPARAM param,
708 WINDOWPROCTYPE procType, BOOL modal )
710 HMENU16 hMenu = 0;
711 HFONT16 hFont = 0;
712 HWND hwnd;
713 RECT rect;
714 WND * wndPtr;
715 DLG_TEMPLATE template;
716 DIALOGINFO * dlgInfo;
717 WORD xUnit = xBaseUnit;
718 WORD yUnit = yBaseUnit;
719 BOOL ownerEnabled = TRUE;
721 /* Parse dialog template */
723 if (!dlgTemplate) return 0;
724 if (win32Template)
725 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
726 else
727 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
729 /* Load menu */
731 if (template.menuName)
733 if (!win32Template) hMenu = LoadMenu16( hInst, template.menuName );
734 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
737 /* Create custom font if needed */
739 if (template.style & DS_SETFONT)
741 /* The font height must be negative as it is a point size */
742 /* and must be converted to pixels first */
743 /* (see CreateFont() documentation in the Windows SDK). */
744 HDC dc;
745 int pixels;
746 if (((short)template.pointSize) < 0)
747 pixels = -((short)template.pointSize);
748 else
750 dc = GetDC(0);
751 pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
752 ReleaseDC(0, dc);
754 if (win32Template)
755 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
756 template.italic, FALSE, FALSE,
757 DEFAULT_CHARSET, 0, 0,
758 PROOF_QUALITY, FF_DONTCARE,
759 (LPCWSTR)template.faceName );
760 else
761 hFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
762 FALSE, FALSE, FALSE,
763 DEFAULT_CHARSET, 0, 0,
764 PROOF_QUALITY, FF_DONTCARE,
765 template.faceName );
766 if (hFont)
768 SIZE charSize;
769 if (DIALOG_GetCharSize(hFont,&charSize))
771 xUnit = charSize.cx;
772 yUnit = charSize.cy;
775 TRACE("units = %d,%d\n", xUnit, yUnit );
778 /* Create dialog main window */
780 rect.left = rect.top = 0;
781 rect.right = MulDiv(template.cx, xUnit, 4);
782 rect.bottom = MulDiv(template.cy, yUnit, 8);
783 if (template.style & DS_MODALFRAME)
784 template.exStyle |= WS_EX_DLGMODALFRAME;
785 AdjustWindowRectEx( &rect, template.style,
786 hMenu ? TRUE : FALSE , template.exStyle );
787 rect.right -= rect.left;
788 rect.bottom -= rect.top;
790 if ((INT16)template.x == CW_USEDEFAULT16)
792 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
794 else
796 if (template.style & DS_CENTER)
798 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
799 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
801 else
803 rect.left += MulDiv(template.x, xUnit, 4);
804 rect.top += MulDiv(template.y, yUnit, 8);
806 if ( !(template.style & WS_CHILD) )
808 INT16 dX, dY;
810 if( !(template.style & DS_ABSALIGN) )
811 ClientToScreen( owner, (POINT *)&rect );
813 /* try to fit it into the desktop */
815 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
816 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
817 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
818 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
819 if( rect.left < 0 ) rect.left = 0;
820 if( rect.top < 0 ) rect.top = 0;
824 if (modal)
825 ownerEnabled = DIALOG_DisableOwner( owner );
827 if (!win32Template)
828 hwnd = CreateWindowEx16(template.exStyle, template.className,
829 template.caption, template.style & ~WS_VISIBLE,
830 rect.left, rect.top, rect.right, rect.bottom,
831 owner, hMenu, hInst, NULL );
832 else
833 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
834 (LPCWSTR)template.caption,
835 template.style & ~WS_VISIBLE,
836 rect.left, rect.top, rect.right, rect.bottom,
837 owner, hMenu, hInst, NULL );
839 if (!hwnd)
841 if (hFont) DeleteObject( hFont );
842 if (hMenu) DestroyMenu( hMenu );
843 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
844 return 0;
846 wndPtr = WIN_FindWndPtr( hwnd );
847 wndPtr->flags |= WIN_ISDIALOG;
848 wndPtr->helpContext = template.helpId;
850 /* Initialise dialog extra data */
852 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
853 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
854 dlgInfo->hUserFont = hFont;
855 dlgInfo->hMenu = hMenu;
856 dlgInfo->xBaseUnit = xUnit;
857 dlgInfo->yBaseUnit = yUnit;
858 dlgInfo->msgResult = 0;
859 dlgInfo->idResult = 0;
860 dlgInfo->flags = ownerEnabled ? DF_OWNERENABLED: 0;
861 dlgInfo->hDialogHeap = 0;
863 if (dlgInfo->hUserFont)
864 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
866 /* Create controls */
868 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
869 hInst, win32Template ))
871 HWND hwndPreInitFocus;
873 /* Send initialisation messages and set focus */
875 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
877 hwndPreInitFocus = GetFocus();
878 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
880 /* check where the focus is again,
881 * some controls status might have changed in WM_INITDIALOG */
882 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
883 SetFocus( dlgInfo->hwndFocus );
885 else
887 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
888 but the focus has not changed, set the focus where we expect it. */
889 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
891 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
892 SetFocus( dlgInfo->hwndFocus );
896 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
898 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
900 WIN_ReleaseWndPtr(wndPtr);
901 return hwnd;
903 WIN_ReleaseWndPtr(wndPtr);
904 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
905 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
906 return 0;
910 /***********************************************************************
911 * CreateDialog (USER.89)
913 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
914 HWND16 owner, DLGPROC16 dlgProc )
916 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
920 /***********************************************************************
921 * CreateDialogParam (USER.241)
923 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
924 HWND16 owner, DLGPROC16 dlgProc,
925 LPARAM param )
927 HWND16 hwnd = 0;
928 HRSRC16 hRsrc;
929 HGLOBAL16 hmem;
930 LPCVOID data;
932 TRACE("%04x,%s,%04x,%08lx,%ld\n",
933 hInst, debugres_a(dlgTemplate), owner, (DWORD)dlgProc, param );
935 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
936 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
937 if (!(data = LockResource16( hmem ))) hwnd = 0;
938 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
939 dlgProc, param );
940 FreeResource16( hmem );
941 return hwnd;
944 /***********************************************************************
945 * CreateDialogParamA (USER32.@)
947 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
948 HWND owner, DLGPROC dlgProc,
949 LPARAM param )
951 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
952 if (!hrsrc) return 0;
953 return CreateDialogIndirectParamA( hInst,
954 (LPVOID)LoadResource(hInst, hrsrc),
955 owner, dlgProc, param );
959 /***********************************************************************
960 * CreateDialogParamW (USER32.@)
962 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
963 HWND owner, DLGPROC dlgProc,
964 LPARAM param )
966 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
967 if (!hrsrc) return 0;
968 return CreateDialogIndirectParamW( hInst,
969 (LPVOID)LoadResource(hInst, hrsrc),
970 owner, dlgProc, param );
974 /***********************************************************************
975 * CreateDialogIndirect (USER.219)
977 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
978 HWND16 owner, DLGPROC16 dlgProc )
980 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
984 /***********************************************************************
985 * CreateDialogIndirectParam (USER.242)
986 * CreateDialogIndirectParam16 (USER32.@)
988 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
989 LPCVOID dlgTemplate,
990 HWND16 owner, DLGPROC16 dlgProc,
991 LPARAM param )
993 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
994 dlgProc, param, WIN_PROC_16, FALSE );
998 /***********************************************************************
999 * CreateDialogIndirectParamA (USER32.@)
1001 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
1002 LPCVOID dlgTemplate,
1003 HWND owner, DLGPROC dlgProc,
1004 LPARAM param )
1006 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1007 (DLGPROC16)dlgProc, param, WIN_PROC_32A, FALSE );
1010 /***********************************************************************
1011 * CreateDialogIndirectParamAorW (USER32.@)
1013 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
1014 LPCVOID dlgTemplate,
1015 HWND owner, DLGPROC dlgProc,
1016 LPARAM param )
1017 { FIXME("assume WIN_PROC_32W\n");
1018 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1019 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1022 /***********************************************************************
1023 * CreateDialogIndirectParamW (USER32.@)
1025 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1026 LPCVOID dlgTemplate,
1027 HWND owner, DLGPROC dlgProc,
1028 LPARAM param )
1030 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1031 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1035 /***********************************************************************
1036 * DIALOG_DoDialogBox
1038 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1040 WND * wndPtr;
1041 DIALOGINFO * dlgInfo;
1042 MSG msg;
1043 INT retval;
1044 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
1046 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
1047 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1049 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1051 ShowWindow( hwnd, SW_SHOW );
1052 for (;;)
1054 if (!(wndPtr->dwStyle & DS_NOIDLEMSG))
1056 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1058 /* No message present -> send ENTERIDLE and wait */
1059 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
1060 if (!GetMessageW( &msg, 0, 0, 0 )) break;
1063 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
1065 if (CallMsgFilterW( &msg, MSGF_DIALOGBOX )) continue;
1067 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
1069 TranslateMessage( &msg );
1070 DispatchMessageW( &msg );
1072 if (dlgInfo->flags & DF_END) break;
1075 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
1076 retval = dlgInfo->idResult;
1077 WIN_ReleaseWndPtr(wndPtr);
1078 DestroyWindow( hwnd );
1079 return retval;
1083 /***********************************************************************
1084 * DialogBox (USER.87)
1086 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1087 HWND16 owner, DLGPROC16 dlgProc )
1089 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1093 /***********************************************************************
1094 * DialogBoxParam (USER.239)
1096 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1097 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1099 HWND16 hwnd = 0;
1100 HRSRC16 hRsrc;
1101 HGLOBAL16 hmem;
1102 LPCVOID data;
1103 int ret = -1;
1105 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1106 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1107 if (!(data = LockResource16( hmem ))) hwnd = 0;
1108 else hwnd = DIALOG_CreateIndirect( hInst, data, FALSE, owner,
1109 dlgProc, param, WIN_PROC_16, TRUE );
1110 if (hwnd)
1111 ret =(INT16)DIALOG_DoDialogBox( hwnd, owner );
1112 if (data) GlobalUnlock16( hmem );
1113 FreeResource16( hmem );
1114 return ret;
1118 /***********************************************************************
1119 * DialogBoxParamA (USER32.@)
1121 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1122 HWND owner, DLGPROC dlgProc, LPARAM param )
1124 HWND hwnd;
1125 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1126 if (!hrsrc) return 0;
1127 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1128 TRUE, owner,
1129 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1130 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1131 return -1;
1135 /***********************************************************************
1136 * DialogBoxParamW (USER32.@)
1138 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1139 HWND owner, DLGPROC dlgProc, LPARAM param )
1141 HWND hwnd;
1142 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1143 if (!hrsrc) return 0;
1144 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1145 TRUE, owner,
1146 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1147 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1148 return -1;
1152 /***********************************************************************
1153 * DialogBoxIndirect (USER.218)
1155 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1156 HWND16 owner, DLGPROC16 dlgProc )
1158 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1162 /***********************************************************************
1163 * DialogBoxIndirectParam (USER.240)
1164 * DialogBoxIndirectParam16 (USER32.@)
1166 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1167 HWND16 owner, DLGPROC16 dlgProc,
1168 LPARAM param )
1170 HWND16 hwnd;
1171 LPCVOID ptr;
1173 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1174 hwnd = DIALOG_CreateIndirect( hInst, ptr, FALSE, owner,
1175 dlgProc, param, WIN_PROC_16, TRUE );
1176 GlobalUnlock16( dlgTemplate );
1177 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1178 return -1;
1182 /***********************************************************************
1183 * DialogBoxIndirectParamA (USER32.@)
1185 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1186 HWND owner, DLGPROC dlgProc,
1187 LPARAM param )
1189 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1190 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1191 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1192 return -1;
1196 /***********************************************************************
1197 * DialogBoxIndirectParamW (USER32.@)
1199 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1200 HWND owner, DLGPROC dlgProc,
1201 LPARAM param )
1203 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1204 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1205 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1206 return -1;
1209 /***********************************************************************
1210 * DialogBoxIndirectParamAorW (USER32.@)
1212 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1213 HWND owner, DLGPROC dlgProc,
1214 LPARAM param, DWORD x )
1216 HWND hwnd;
1217 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1218 hInstance, template, owner, dlgProc, param, x);
1219 hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1220 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1221 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1222 return -1;
1225 /***********************************************************************
1226 * EndDialog (USER.88)
1228 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1230 return EndDialog( hwnd, retval );
1234 /***********************************************************************
1235 * EndDialog (USER32.@)
1237 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1239 WND * wndPtr = WIN_FindWndPtr( hwnd );
1240 BOOL wasEnabled = TRUE;
1241 DIALOGINFO * dlgInfo;
1242 HWND owner;
1244 TRACE("%04x %d\n", hwnd, retval );
1246 if (!wndPtr)
1248 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1249 return FALSE;
1252 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1254 dlgInfo->idResult = retval;
1255 dlgInfo->flags |= DF_END;
1256 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1258 WIN_ReleaseWndPtr(wndPtr);
1260 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1261 DIALOG_EnableOwner( owner );
1263 /* Windows sets the focus to the dialog itself in EndDialog */
1265 if (IsChild(hwnd, GetFocus()))
1266 SetFocus( hwnd );
1268 /* Don't have to send a ShowWindow(SW_HIDE), just do
1269 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1271 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1272 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1274 /* unblock dialog loop */
1275 PostMessageA(hwnd, WM_NULL, 0, 0);
1276 return TRUE;
1280 /***********************************************************************
1281 * DIALOG_IsAccelerator
1283 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1285 HWND hwndControl = hwnd;
1286 HWND hwndNext;
1287 INT dlgCode;
1288 WCHAR buffer[128];
1292 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
1293 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
1295 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1296 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1297 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
1299 /* find the accelerator key */
1300 LPWSTR p = buffer - 2;
1301 char a_char = vKey;
1302 WCHAR w_char = 0;
1306 p = strchrW( p + 2, '&' );
1308 while (p != NULL && p[1] == '&');
1310 /* and check if it's the one we're looking for */
1311 MultiByteToWideChar(CP_ACP, 0, &a_char, 1, &w_char, 1);
1312 if (p != NULL && toupperW( p[1] ) == toupperW( w_char ) )
1314 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
1316 /* set focus to the control */
1317 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndControl, 1);
1318 /* and bump it on to next */
1319 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1321 else if (dlgCode & DLGC_BUTTON)
1323 /* send BM_CLICK message to the control */
1324 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1326 return TRUE;
1329 hwndNext = GetWindow( hwndControl, GW_CHILD );
1331 else hwndNext = 0;
1333 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1335 while (!hwndNext && hwndControl)
1337 hwndControl = GetParent( hwndControl );
1338 if (hwndControl == hwndDlg)
1340 if(hwnd==hwndDlg) /* prevent endless loop */
1342 hwndNext=hwnd;
1343 break;
1345 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1347 else
1348 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1350 hwndControl = hwndNext;
1352 while (hwndControl && (hwndControl != hwnd));
1354 return FALSE;
1357 /***********************************************************************
1358 * DIALOG_FindMsgDestination
1360 * The messages that IsDialogMessage sends may not go to the dialog
1361 * calling IsDialogMessage if that dialog is a child, and it has the
1362 * DS_CONTROL style set.
1363 * We propagate up until we hit one that does not have DS_CONTROL, or
1364 * whose parent is not a dialog.
1366 * This is undocumented behaviour.
1368 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1370 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1372 WND *pParent;
1373 HWND hParent = GetParent(hwndDlg);
1374 if (!hParent) break;
1376 pParent = WIN_FindWndPtr(hParent);
1377 if (!pParent) break;
1379 if (!(pParent->flags & WIN_ISDIALOG))
1381 WIN_ReleaseWndPtr(pParent);
1382 break;
1384 WIN_ReleaseWndPtr(pParent);
1386 hwndDlg = hParent;
1389 return hwndDlg;
1392 /***********************************************************************
1393 * DIALOG_IsDialogMessage
1395 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1396 UINT message, WPARAM wParam,
1397 LPARAM lParam, BOOL *translate,
1398 BOOL *dispatch, INT dlgCode )
1400 *translate = *dispatch = FALSE;
1402 if (message == WM_PAINT)
1404 /* Apparently, we have to handle this one as well */
1405 *dispatch = TRUE;
1406 return TRUE;
1409 /* Only the key messages get special processing */
1410 if ((message != WM_KEYDOWN) &&
1411 (message != WM_SYSKEYDOWN) &&
1412 (message != WM_SYSCHAR) &&
1413 (message != WM_CHAR))
1414 return FALSE;
1416 if (dlgCode & DLGC_WANTMESSAGE)
1418 *translate = *dispatch = TRUE;
1419 return TRUE;
1422 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1424 switch(message)
1426 case WM_KEYDOWN:
1427 switch(wParam)
1429 case VK_TAB:
1430 if (!(dlgCode & DLGC_WANTTAB))
1432 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1433 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1434 return TRUE;
1436 break;
1438 case VK_RIGHT:
1439 case VK_DOWN:
1440 case VK_LEFT:
1441 case VK_UP:
1442 if (!(dlgCode & DLGC_WANTARROWS))
1444 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1445 HWND hwndNext =
1446 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1447 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1448 return TRUE;
1450 break;
1452 case VK_ESCAPE:
1453 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1454 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1455 return TRUE;
1457 case VK_RETURN:
1459 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1460 if (HIWORD(dw) == DC_HASDEFID)
1462 SendMessageA( hwndDlg, WM_COMMAND,
1463 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1464 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1466 else
1468 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1469 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1473 return TRUE;
1475 *translate = TRUE;
1476 break; /* case WM_KEYDOWN */
1478 case WM_CHAR:
1479 if (dlgCode & DLGC_WANTCHARS) break;
1480 /* drop through */
1482 case WM_SYSCHAR:
1483 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1485 /* don't translate or dispatch */
1486 return TRUE;
1488 break;
1490 case WM_SYSKEYDOWN:
1491 *translate = TRUE;
1492 break;
1495 /* If we get here, the message has not been treated specially */
1496 /* and can be sent to its destination window. */
1497 *dispatch = TRUE;
1498 return TRUE;
1502 /***********************************************************************
1503 * IsDialogMessage (USER.90)
1505 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1507 LPMSG16 msg = MapSL(msg16);
1508 BOOL ret, translate, dispatch;
1509 INT dlgCode = 0;
1511 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1512 return FALSE;
1514 if ((msg->message == WM_KEYDOWN) ||
1515 (msg->message == WM_CHAR))
1517 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1519 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1520 msg->wParam, msg->lParam,
1521 &translate, &dispatch, dlgCode );
1522 if (translate) TranslateMessage16( msg );
1523 if (dispatch) DispatchMessage16( msg );
1524 return ret;
1528 /***********************************************************************
1529 * IsDialogMessage (USER32.@)
1530 * IsDialogMessageA (USER32.@)
1532 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1534 BOOL ret, translate, dispatch;
1535 INT dlgCode = 0;
1537 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1538 return FALSE;
1540 if ((msg->message == WM_KEYDOWN) ||
1541 (msg->message == WM_CHAR))
1543 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1545 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1546 msg->wParam, msg->lParam,
1547 &translate, &dispatch, dlgCode );
1548 if (translate) TranslateMessage( msg );
1549 if (dispatch) DispatchMessageA( msg );
1550 return ret;
1554 /***********************************************************************
1555 * IsDialogMessageW (USER32.@)
1557 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1559 BOOL ret, translate, dispatch;
1560 INT dlgCode = 0;
1562 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1563 return FALSE;
1565 if ((msg->message == WM_KEYDOWN) ||
1566 (msg->message == WM_CHAR))
1568 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1570 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1571 msg->wParam, msg->lParam,
1572 &translate, &dispatch, dlgCode );
1573 if (translate) TranslateMessage( msg );
1574 if (dispatch) DispatchMessageW( msg );
1575 return ret;
1579 /***********************************************************************
1580 * GetDlgCtrlID (USER.277)
1582 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1584 return GetDlgCtrlID( hwnd );
1588 /***********************************************************************
1589 * GetDlgCtrlID (USER32.@)
1591 INT WINAPI GetDlgCtrlID( HWND hwnd )
1593 return GetWindowLongW( hwnd, GWL_ID );
1597 /***********************************************************************
1598 * GetDlgItem (USER.91)
1600 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1602 return GetDlgItem( hwndDlg, id );
1606 /***********************************************************************
1607 * GetDlgItem (USER32.@)
1609 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1611 int i;
1612 HWND *list = WIN_BuildWinArray( hwndDlg );
1613 HWND ret = 0;
1615 if (!list) return 0;
1617 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1618 ret = list[i];
1619 WIN_ReleaseWinArray( list );
1620 return ret;
1624 /*******************************************************************
1625 * SendDlgItemMessage (USER.101)
1627 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1628 WPARAM16 wParam, LPARAM lParam )
1630 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1631 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1632 else return 0;
1636 /*******************************************************************
1637 * SendDlgItemMessageA (USER32.@)
1639 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1640 WPARAM wParam, LPARAM lParam )
1642 HWND hwndCtrl = GetDlgItem( hwnd, id );
1643 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1644 else return 0;
1648 /*******************************************************************
1649 * SendDlgItemMessageW (USER32.@)
1651 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1652 WPARAM wParam, LPARAM lParam )
1654 HWND hwndCtrl = GetDlgItem( hwnd, id );
1655 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1656 else return 0;
1660 /*******************************************************************
1661 * SetDlgItemText (USER.92)
1663 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1665 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1669 /*******************************************************************
1670 * SetDlgItemTextA (USER32.@)
1672 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1674 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1678 /*******************************************************************
1679 * SetDlgItemTextW (USER32.@)
1681 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1683 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1687 /***********************************************************************
1688 * GetDlgItemText (USER.93)
1690 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1692 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1693 len, (LPARAM)str );
1697 /***********************************************************************
1698 * GetDlgItemTextA (USER32.@)
1700 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1702 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1703 len, (LPARAM)str );
1707 /***********************************************************************
1708 * GetDlgItemTextW (USER32.@)
1710 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1712 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1713 len, (LPARAM)str );
1717 /*******************************************************************
1718 * SetDlgItemInt (USER.94)
1720 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1722 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1726 /*******************************************************************
1727 * SetDlgItemInt (USER32.@)
1729 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1730 BOOL fSigned )
1732 char str[20];
1734 if (fSigned) sprintf( str, "%d", (INT)value );
1735 else sprintf( str, "%u", value );
1736 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1737 return TRUE;
1741 /***********************************************************************
1742 * GetDlgItemInt (USER.95)
1744 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1745 BOOL16 fSigned )
1747 UINT result;
1748 BOOL ok;
1750 if (translated) *translated = FALSE;
1751 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1752 if (!ok) return 0;
1753 if (fSigned)
1755 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1757 else
1759 if (result > 65535) return 0;
1761 if (translated) *translated = TRUE;
1762 return (UINT16)result;
1766 /***********************************************************************
1767 * GetDlgItemInt (USER32.@)
1769 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1770 BOOL fSigned )
1772 char str[30];
1773 char * endptr;
1774 long result = 0;
1776 if (translated) *translated = FALSE;
1777 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1778 return 0;
1779 if (fSigned)
1781 result = strtol( str, &endptr, 10 );
1782 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1783 return 0;
1784 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1785 return 0;
1787 else
1789 result = strtoul( str, &endptr, 10 );
1790 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1791 return 0;
1792 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1794 if (translated) *translated = TRUE;
1795 return (UINT)result;
1799 /***********************************************************************
1800 * CheckDlgButton (USER.97)
1802 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1804 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1805 return TRUE;
1809 /***********************************************************************
1810 * CheckDlgButton (USER32.@)
1812 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1814 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1815 return TRUE;
1819 /***********************************************************************
1820 * IsDlgButtonChecked (USER.98)
1822 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1824 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1828 /***********************************************************************
1829 * IsDlgButtonChecked (USER32.@)
1831 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1833 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1837 /***********************************************************************
1838 * CheckRadioButton (USER.96)
1840 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1841 UINT16 lastID, UINT16 checkID )
1843 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1847 /***********************************************************************
1848 * CheckRB
1850 * Callback function used to check/uncheck radio buttons that fall
1851 * within a specific range of IDs.
1853 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1855 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1856 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1858 if ((lChildID >= lpRadioGroup->firstID) &&
1859 (lChildID <= lpRadioGroup->lastID))
1861 if (lChildID == lpRadioGroup->checkID)
1863 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1865 else
1867 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1871 return TRUE;
1875 /***********************************************************************
1876 * CheckRadioButton (USER32.@)
1878 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1879 UINT lastID, UINT checkID )
1881 RADIOGROUP radioGroup;
1883 /* perform bounds checking for a radio button group */
1884 radioGroup.firstID = min(min(firstID, lastID), checkID);
1885 radioGroup.lastID = max(max(firstID, lastID), checkID);
1886 radioGroup.checkID = checkID;
1888 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1889 (LPARAM)&radioGroup);
1893 /***********************************************************************
1894 * GetDialogBaseUnits (USER.243)
1895 * GetDialogBaseUnits (USER32.@)
1897 DWORD WINAPI GetDialogBaseUnits(void)
1899 return MAKELONG( xBaseUnit, yBaseUnit );
1903 /***********************************************************************
1904 * MapDialogRect (USER.103)
1906 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1908 DIALOGINFO * dlgInfo;
1909 WND * wndPtr = WIN_FindWndPtr( hwnd );
1910 if (!wndPtr) return;
1911 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1912 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1913 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1914 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1915 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1916 WIN_ReleaseWndPtr(wndPtr);
1920 /***********************************************************************
1921 * MapDialogRect (USER32.@)
1923 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1925 DIALOGINFO * dlgInfo;
1926 WND * wndPtr = WIN_FindWndPtr( hwnd );
1927 if (!wndPtr) return FALSE;
1928 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1929 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1930 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1931 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1932 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1933 WIN_ReleaseWndPtr(wndPtr);
1934 return TRUE;
1938 /***********************************************************************
1939 * GetNextDlgGroupItem (USER.227)
1941 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1942 BOOL16 fPrevious )
1944 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1948 /***********************************************************************
1949 * GetNextDlgGroupItem (USER32.@)
1951 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1952 BOOL fPrevious )
1954 HWND hwnd, retvalue;
1956 if(hwndCtrl)
1958 /* if the hwndCtrl is the child of the control in the hwndDlg,
1959 * then the hwndDlg has to be the parent of the hwndCtrl */
1960 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1961 hwndDlg = GetParent(hwndCtrl);
1964 if (hwndCtrl)
1966 /* Make sure hwndCtrl is a top-level child */
1967 HWND parent = GetParent( hwndCtrl );
1968 while (parent && parent != hwndDlg) parent = GetParent(parent);
1969 if (parent != hwndDlg) return 0;
1971 else
1973 /* No ctrl specified -> start from the beginning */
1974 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1975 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1978 retvalue = hwndCtrl;
1979 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1980 while (1)
1982 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1984 /* Wrap-around to the beginning of the group */
1985 HWND tmp;
1987 hwnd = GetWindow( hwndDlg, GW_CHILD );
1988 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1990 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1991 if (tmp == hwndCtrl) break;
1994 if (hwnd == hwndCtrl) break;
1995 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1997 retvalue = hwnd;
1998 if (!fPrevious) break;
2000 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
2002 return retvalue;
2006 /***********************************************************************
2007 * GetNextDlgTabItem (USER.228)
2009 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
2010 BOOL16 fPrevious )
2012 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
2015 /***********************************************************************
2016 * DIALOG_GetNextTabItem
2018 * Helper for GetNextDlgTabItem
2020 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
2022 LONG dsStyle;
2023 LONG exStyle;
2024 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
2025 HWND retWnd = 0;
2026 HWND hChildFirst = 0;
2028 if(!hwndCtrl)
2030 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
2031 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
2033 else
2035 HWND hParent = GetParent(hwndCtrl);
2036 BOOL bValid = FALSE;
2037 while( hParent)
2039 if(hParent == hwndMain)
2041 bValid = TRUE;
2042 break;
2044 hParent = GetParent(hParent);
2046 if(bValid)
2048 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2049 if(!hChildFirst)
2051 if(GetParent(hwndCtrl) != hwndMain)
2052 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2053 else
2055 if(fPrevious)
2056 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2057 else
2058 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2063 while(hChildFirst)
2065 BOOL bCtrl = FALSE;
2066 while(hChildFirst)
2068 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2069 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2070 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2072 bCtrl=TRUE;
2073 break;
2075 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2076 break;
2077 hChildFirst = GetWindow(hChildFirst,wndSearch);
2079 if(hChildFirst)
2081 if(bCtrl)
2082 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2083 else
2084 retWnd = hChildFirst;
2086 if(retWnd) break;
2087 hChildFirst = GetWindow(hChildFirst,wndSearch);
2089 if(!retWnd && hwndCtrl)
2091 HWND hParent = GetParent(hwndCtrl);
2092 while(hParent)
2094 if(hParent == hwndMain) break;
2095 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2096 if(retWnd) break;
2097 hParent = GetParent(hParent);
2099 if(!retWnd)
2100 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2102 return retWnd;
2105 /***********************************************************************
2106 * GetNextDlgTabItem (USER32.@)
2108 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2109 BOOL fPrevious )
2111 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2114 /**********************************************************************
2115 * DIALOG_DlgDirSelect
2117 * Helper function for DlgDirSelect*
2119 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2120 INT id, BOOL unicode, BOOL combo )
2122 char *buffer, *ptr;
2123 INT item, size;
2124 BOOL ret;
2125 HWND listbox = GetDlgItem( hwnd, id );
2127 TRACE("%04x '%s' %d\n", hwnd, str, id );
2128 if (!listbox) return FALSE;
2130 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
2131 if (item == LB_ERR) return FALSE;
2132 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
2133 if (size == LB_ERR) return FALSE;
2135 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
2137 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
2139 if ((ret = (buffer[0] == '['))) /* drive or directory */
2141 if (buffer[1] == '-') /* drive */
2143 buffer[3] = ':';
2144 buffer[4] = 0;
2145 ptr = buffer + 2;
2147 else
2149 buffer[strlen(buffer)-1] = '\\';
2150 ptr = buffer + 1;
2153 else ptr = buffer;
2155 if (unicode)
2157 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
2158 ((LPWSTR)str)[len-1] = 0;
2160 else lstrcpynA( str, ptr, len );
2161 HeapFree( GetProcessHeap(), 0, buffer );
2162 TRACE("Returning %d '%s'\n", ret, str );
2163 return ret;
2167 /**********************************************************************
2168 * DIALOG_DlgDirList
2170 * Helper function for DlgDirList*
2172 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2173 INT idStatic, UINT attrib, BOOL combo )
2175 HWND hwnd;
2176 LPSTR orig_spec = spec;
2178 #define SENDMSG(msg,wparam,lparam) \
2179 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2180 : SendMessageA( hwnd, msg, wparam, lparam ))
2182 TRACE("%04x '%s' %d %d %04x\n",
2183 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2185 /* If the path exists and is a directory, chdir to it */
2186 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
2187 else
2189 char *p, *p2;
2190 p = spec;
2191 if ((p2 = strrchr( p, '\\' ))) p = p2;
2192 if ((p2 = strrchr( p, '/' ))) p = p2;
2193 if (p != spec)
2195 char sep = *p;
2196 *p = 0;
2197 if (!SetCurrentDirectoryA( spec ))
2199 *p = sep; /* Restore the original spec */
2200 return FALSE;
2202 spec = p + 1;
2206 TRACE( "mask=%s\n", spec );
2208 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2210 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2211 if (attrib & DDL_DIRECTORY)
2213 if (!(attrib & DDL_EXCLUSIVE))
2215 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2216 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2217 (LPARAM)spec ) == LB_ERR)
2218 return FALSE;
2220 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2221 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2222 (LPARAM)"*.*" ) == LB_ERR)
2223 return FALSE;
2225 else
2227 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2228 (LPARAM)spec ) == LB_ERR)
2229 return FALSE;
2233 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2235 char temp[MAX_PATH];
2236 GetCurrentDirectoryA( sizeof(temp), temp );
2237 CharLowerA( temp );
2238 /* Can't use PostMessage() here, because the string is on the stack */
2239 SetDlgItemTextA( hDlg, idStatic, temp );
2242 if (orig_spec && (spec != orig_spec))
2244 /* Update the original file spec */
2245 char *p = spec;
2246 while ((*orig_spec++ = *p++));
2249 return TRUE;
2250 #undef SENDMSG
2254 /**********************************************************************
2255 * DIALOG_DlgDirListW
2257 * Helper function for DlgDirList*W
2259 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2260 INT idStatic, UINT attrib, BOOL combo )
2262 if (spec)
2264 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2265 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2266 attrib, combo );
2267 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
2268 HeapFree( GetProcessHeap(), 0, specA );
2269 return ret;
2271 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2275 /**********************************************************************
2276 * DlgDirSelect (USER.99)
2278 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2280 return DlgDirSelectEx16( hwnd, str, 128, id );
2284 /**********************************************************************
2285 * DlgDirSelectComboBox (USER.194)
2287 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2289 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2293 /**********************************************************************
2294 * DlgDirSelectEx (USER.422)
2296 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2298 return DlgDirSelectExA( hwnd, str, len, id );
2302 /**********************************************************************
2303 * DlgDirSelectExA (USER32.@)
2305 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2307 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
2311 /**********************************************************************
2312 * DlgDirSelectExW (USER32.@)
2314 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2316 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
2320 /**********************************************************************
2321 * DlgDirSelectComboBoxEx (USER.423)
2323 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2324 INT16 id )
2326 return DlgDirSelectComboBoxExA( hwnd, str, len, id );
2330 /**********************************************************************
2331 * DlgDirSelectComboBoxExA (USER32.@)
2333 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2334 INT id )
2336 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
2340 /**********************************************************************
2341 * DlgDirSelectComboBoxExW (USER32.@)
2343 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2344 INT id)
2346 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
2350 /**********************************************************************
2351 * DlgDirList (USER.100)
2353 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2354 INT16 idStatic, UINT16 attrib )
2356 /* according to Win16 docs, DDL_DRIVES should make DDL_EXCLUSIVE
2357 * be set automatically (this is different in Win32, and
2358 * DIALOG_DlgDirList sends Win32 messages to the control,
2359 * so do it here) */
2360 if (attrib & DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
2361 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2365 /**********************************************************************
2366 * DlgDirListA (USER32.@)
2368 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2369 INT idStatic, UINT attrib )
2371 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2375 /**********************************************************************
2376 * DlgDirListW (USER32.@)
2378 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2379 INT idStatic, UINT attrib )
2381 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2385 /**********************************************************************
2386 * DlgDirListComboBox (USER.195)
2388 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2389 INT16 idStatic, UINT16 attrib )
2391 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2395 /**********************************************************************
2396 * DlgDirListComboBoxA (USER32.@)
2398 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2399 INT idStatic, UINT attrib )
2401 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2405 /**********************************************************************
2406 * DlgDirListComboBoxW (USER32.@)
2408 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2409 INT idStatic, UINT attrib )
2411 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );