- Minor API files update.
[wine.git] / windows / dialog.c
blob27d8c77b28d6db5882e0843bc64cc4dd4a0c81c7
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 "message.h"
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(dialog);
34 /* Dialog control information */
35 typedef struct
37 DWORD style;
38 DWORD exStyle;
39 DWORD helpId;
40 INT16 x;
41 INT16 y;
42 INT16 cx;
43 INT16 cy;
44 UINT id;
45 LPCSTR className;
46 LPCSTR windowName;
47 LPVOID data;
48 } DLG_CONTROL_INFO;
50 /* Dialog template */
51 typedef struct
53 DWORD style;
54 DWORD exStyle;
55 DWORD helpId;
56 UINT16 nbItems;
57 INT16 x;
58 INT16 y;
59 INT16 cx;
60 INT16 cy;
61 LPCSTR menuName;
62 LPCSTR className;
63 LPCSTR caption;
64 WORD pointSize;
65 WORD weight;
66 BOOL italic;
67 LPCSTR faceName;
68 BOOL dialogEx;
69 } DLG_TEMPLATE;
71 /* Radio button group */
72 typedef struct
74 UINT firstID;
75 UINT lastID;
76 UINT checkID;
77 } RADIOGROUP;
79 /* Dialog base units */
80 static WORD xBaseUnit = 0, yBaseUnit = 0;
83 /*********************************************************************
84 * dialog class descriptor
86 const struct builtin_class_descr DIALOG_builtin_class =
88 DIALOG_CLASS_ATOM, /* name */
89 CS_GLOBALCLASS | CS_SAVEBITS, /* style */
90 DefDlgProcA, /* procA */
91 DefDlgProcW, /* procW */
92 DLGWINDOWEXTRA, /* extra */
93 IDC_ARROWA, /* cursor */
94 0 /* brush */
98 /***********************************************************************
99 * DIALOG_EnableOwner
101 * Helper function for modal dialogs to enable again the
102 * owner of the dialog box.
104 void DIALOG_EnableOwner( HWND hOwner, BOOL ownerWasEnabled)
106 /* Owner must be a top-level window */
107 if (hOwner)
108 hOwner = WIN_GetTopParent( hOwner );
109 if (!hOwner) return;
110 if (ownerWasEnabled)
111 EnableWindow( hOwner, TRUE );
115 /***********************************************************************
116 * DIALOG_DisableOwner
118 * Helper function for modal dialogs to disable the
119 * owner of the dialog box. Returns TRUE if owner was enabled.
121 BOOL DIALOG_DisableOwner( HWND hOwner )
123 /* Owner must be a top-level window */
124 if (hOwner)
125 hOwner = WIN_GetTopParent( hOwner );
126 if (!hOwner) return FALSE;
127 if (IsWindowEnabled( hOwner ))
129 EnableWindow( hOwner, FALSE );
130 return TRUE;
132 else
133 return FALSE;
136 /***********************************************************************
137 * DIALOG_GetCharSizeFromDC
140 * Calculates the *true* average size of English characters in the
141 * specified font as oppposed to the one returned by GetTextMetrics.
143 * Latest: the X font driver will now compute a proper average width
144 * so this code can be removed
146 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
148 BOOL Success = FALSE;
149 HFONT hFontPrev = 0;
150 pSize->cx = xBaseUnit;
151 pSize->cy = yBaseUnit;
152 if ( hDC )
154 /* select the font */
155 TEXTMETRICA tm;
156 memset(&tm,0,sizeof(tm));
157 if (hFont) hFontPrev = SelectFont(hDC,hFont);
158 if (GetTextMetricsA(hDC,&tm))
160 pSize->cx = tm.tmAveCharWidth;
161 pSize->cy = tm.tmHeight;
163 /* if variable width font */
164 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
166 SIZE total;
167 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
169 /* Calculate a true average as opposed to the one returned
170 * by tmAveCharWidth. This works better when dealing with
171 * proportional spaced fonts and (more important) that's
172 * how Microsoft's dialog creation code calculates the size
173 * of the font
175 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
177 /* round up */
178 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
179 Success = TRUE;
182 else
184 Success = TRUE;
186 /* Use the text metrics */
187 TRACE("Using tm: %ldx%ld (dlg: %ld x %ld) (%s)\n",
188 tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
189 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
190 pSize->cx = tm.tmAveCharWidth;
191 pSize->cy = tm.tmHeight;
193 /* select the original font */
194 if (hFontPrev) SelectFont(hDC,hFontPrev);
196 return (Success);
199 /***********************************************************************
200 * DIALOG_GetCharSize
202 * A convenient variant of DIALOG_GetCharSizeFromDC.
204 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
206 HDC hDC = GetDC(0);
207 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
208 ReleaseDC(0, hDC);
209 return Success;
212 /***********************************************************************
213 * DIALOG_Init
215 * Initialisation of the dialog manager.
217 BOOL DIALOG_Init(void)
219 HDC hdc;
220 SIZE size;
222 /* Calculate the dialog base units */
224 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
226 ERR("Could not create Display DC\n");
227 return FALSE;
230 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
232 DeleteDC( hdc );
233 ERR("Could not initialize base dialog units\n");
234 return FALSE;
237 DeleteDC( hdc );
238 xBaseUnit = size.cx;
239 yBaseUnit = size.cy;
241 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
242 return TRUE;
246 /***********************************************************************
247 * DIALOG_GetControl16
249 * Return the class and text of the control pointed to by ptr,
250 * fill the header structure and return a pointer to the next control.
252 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
254 static char buffer[10];
255 int int_id;
257 info->x = GET_WORD(p); p += sizeof(WORD);
258 info->y = GET_WORD(p); p += sizeof(WORD);
259 info->cx = GET_WORD(p); p += sizeof(WORD);
260 info->cy = GET_WORD(p); p += sizeof(WORD);
261 info->id = GET_WORD(p); p += sizeof(WORD);
262 info->style = GET_DWORD(p); p += sizeof(DWORD);
263 info->exStyle = 0;
265 if (*p & 0x80)
267 switch((BYTE)*p)
269 case 0x80: strcpy( buffer, "BUTTON" ); break;
270 case 0x81: strcpy( buffer, "EDIT" ); break;
271 case 0x82: strcpy( buffer, "STATIC" ); break;
272 case 0x83: strcpy( buffer, "LISTBOX" ); break;
273 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
274 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
275 default: buffer[0] = '\0'; break;
277 info->className = buffer;
278 p++;
280 else
282 info->className = p;
283 p += strlen(p) + 1;
286 int_id = ((BYTE)*p == 0xff);
287 if (int_id)
289 /* Integer id, not documented (?). Only works for SS_ICON controls */
290 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
291 p += 3;
293 else
295 info->windowName = p;
296 p += strlen(p) + 1;
299 if (*p)
301 /* Additional CTLDATA available for this control. */
302 info->data = SEGPTR_ALLOC(*p);
303 memcpy( info->data, p + 1, *p );
305 else info->data = NULL;
307 p += *p + 1;
309 if(int_id)
310 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
311 info->className, LOWORD(info->windowName),
312 info->id, info->x, info->y, info->cx, info->cy,
313 info->style, (DWORD)SEGPTR_GET(info->data) );
314 else
315 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
316 info->className, info->windowName,
317 info->id, info->x, info->y, info->cx, info->cy,
318 info->style, (DWORD)SEGPTR_GET(info->data) );
320 return p;
324 /***********************************************************************
325 * DIALOG_GetControl32
327 * Return the class and text of the control pointed to by ptr,
328 * fill the header structure and return a pointer to the next control.
330 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
331 BOOL dialogEx )
333 if (dialogEx)
335 info->helpId = GET_DWORD(p); p += 2;
336 info->exStyle = GET_DWORD(p); p += 2;
337 info->style = GET_DWORD(p); p += 2;
339 else
341 info->helpId = 0;
342 info->style = GET_DWORD(p); p += 2;
343 info->exStyle = GET_DWORD(p); p += 2;
345 info->x = GET_WORD(p); p++;
346 info->y = GET_WORD(p); p++;
347 info->cx = GET_WORD(p); p++;
348 info->cy = GET_WORD(p); p++;
350 if (dialogEx)
352 /* id is a DWORD for DIALOGEX */
353 info->id = GET_DWORD(p);
354 p += 2;
356 else
358 info->id = GET_WORD(p);
359 p++;
362 if (GET_WORD(p) == 0xffff)
364 static const WCHAR class_names[6][10] =
366 { 'B','u','t','t','o','n', }, /* 0x80 */
367 { 'E','d','i','t', }, /* 0x81 */
368 { 'S','t','a','t','i','c', }, /* 0x82 */
369 { 'L','i','s','t','B','o','x', }, /* 0x83 */
370 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
371 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
373 WORD id = GET_WORD(p+1);
374 if ((id >= 0x80) && (id <= 0x85))
375 info->className = (LPCSTR)class_names[id - 0x80];
376 else
378 info->className = NULL;
379 ERR("Unknown built-in class id %04x\n", id );
381 p += 2;
383 else
385 info->className = (LPCSTR)p;
386 p += strlenW( (LPCWSTR)p ) + 1;
389 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
391 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
392 p += 2;
394 else
396 info->windowName = (LPCSTR)p;
397 p += strlenW( (LPCWSTR)p ) + 1;
400 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
401 debugstr_w( (LPCWSTR)info->className ),
402 debugres_w( (LPCWSTR)info->windowName ),
403 info->id, info->x, info->y, info->cx, info->cy,
404 info->style, info->exStyle, info->helpId );
406 if (GET_WORD(p))
408 if (TRACE_ON(dialog))
410 WORD i, count = GET_WORD(p) / sizeof(WORD);
411 TRACE(" BEGIN\n");
412 TRACE(" ");
413 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
414 DPRINTF("\n");
415 TRACE(" END\n" );
417 info->data = (LPVOID)(p + 1);
418 p += GET_WORD(p) / sizeof(WORD);
420 else info->data = NULL;
421 p++;
423 /* Next control is on dword boundary */
424 return (const WORD *)((((int)p) + 3) & ~3);
428 /***********************************************************************
429 * DIALOG_CreateControls
431 * Create the control windows for a dialog.
433 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
434 const DLG_TEMPLATE *dlgTemplate,
435 HINSTANCE hInst, BOOL win32 )
437 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
438 DLG_CONTROL_INFO info;
439 HWND hwndCtrl, hwndDefButton = 0;
440 INT items = dlgTemplate->nbItems;
442 TRACE(" BEGIN\n" );
443 while (items--)
445 if (!win32)
447 HINSTANCE16 instance;
448 template = DIALOG_GetControl16( template, &info );
449 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
450 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
452 if (!dlgInfo->hDialogHeap)
454 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
455 if (!dlgInfo->hDialogHeap)
457 ERR("Insufficient memory to create heap for edit control\n" );
458 continue;
460 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
462 instance = dlgInfo->hDialogHeap;
464 else instance = (HINSTANCE16)hInst;
466 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
467 info.className, info.windowName,
468 info.style | WS_CHILD,
469 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
470 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
471 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
472 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
473 pWnd->hwndSelf, (HMENU16)info.id,
474 instance, (LPVOID)SEGPTR_GET(info.data) );
476 if (info.data) SEGPTR_FREE(info.data);
478 else
480 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
481 dlgTemplate->dialogEx );
482 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
483 (LPCWSTR)info.className,
484 (LPCWSTR)info.windowName,
485 info.style | WS_CHILD,
486 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
487 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
488 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
489 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
490 pWnd->hwndSelf, (HMENU)info.id,
491 hInst, info.data );
493 if (!hwndCtrl) return FALSE;
495 /* Send initialisation messages to the control */
496 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
497 (WPARAM)dlgInfo->hUserFont, 0 );
498 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
500 /* If there's already a default push-button, set it back */
501 /* to normal and use this one instead. */
502 if (hwndDefButton)
503 SendMessageA( hwndDefButton, BM_SETSTYLE,
504 BS_PUSHBUTTON,FALSE );
505 hwndDefButton = hwndCtrl;
506 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
509 TRACE(" END\n" );
510 return TRUE;
514 /***********************************************************************
515 * DIALOG_ParseTemplate16
517 * Fill a DLG_TEMPLATE structure from the dialog template, and return
518 * a pointer to the first control.
520 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
522 result->style = GET_DWORD(p); p += sizeof(DWORD);
523 result->exStyle = 0;
524 result->nbItems = (unsigned char) *p++;
525 result->x = GET_WORD(p); p += sizeof(WORD);
526 result->y = GET_WORD(p); p += sizeof(WORD);
527 result->cx = GET_WORD(p); p += sizeof(WORD);
528 result->cy = GET_WORD(p); p += sizeof(WORD);
529 TRACE("DIALOG %d, %d, %d, %d\n",
530 result->x, result->y, result->cx, result->cy );
531 TRACE(" STYLE %08lx\n", result->style );
533 /* Get the menu name */
535 switch( (BYTE)*p )
537 case 0:
538 result->menuName = 0;
539 p++;
540 break;
541 case 0xff:
542 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
543 p += 3;
544 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
545 break;
546 default:
547 result->menuName = p;
548 TRACE(" MENU '%s'\n", p );
549 p += strlen(p) + 1;
550 break;
553 /* Get the class name */
555 if (*p)
557 result->className = p;
558 TRACE(" CLASS '%s'\n", result->className );
560 else result->className = DIALOG_CLASS_ATOM;
561 p += strlen(p) + 1;
563 /* Get the window caption */
565 result->caption = p;
566 p += strlen(p) + 1;
567 TRACE(" CAPTION '%s'\n", result->caption );
569 /* Get the font name */
571 if (result->style & DS_SETFONT)
573 result->pointSize = GET_WORD(p);
574 p += sizeof(WORD);
575 result->faceName = p;
576 p += strlen(p) + 1;
577 TRACE(" FONT %d,'%s'\n",
578 result->pointSize, result->faceName );
580 return p;
584 /***********************************************************************
585 * DIALOG_ParseTemplate32
587 * Fill a DLG_TEMPLATE structure from the dialog template, and return
588 * a pointer to the first control.
590 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
592 const WORD *p = (const WORD *)template;
594 result->style = GET_DWORD(p); p += 2;
595 if (result->style == 0xffff0001) /* DIALOGEX resource */
597 result->dialogEx = TRUE;
598 result->helpId = GET_DWORD(p); p += 2;
599 result->exStyle = GET_DWORD(p); p += 2;
600 result->style = GET_DWORD(p); p += 2;
602 else
604 result->dialogEx = FALSE;
605 result->helpId = 0;
606 result->exStyle = GET_DWORD(p); p += 2;
608 result->nbItems = GET_WORD(p); p++;
609 result->x = GET_WORD(p); p++;
610 result->y = GET_WORD(p); p++;
611 result->cx = GET_WORD(p); p++;
612 result->cy = GET_WORD(p); p++;
613 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
614 result->dialogEx ? "EX" : "", result->x, result->y,
615 result->cx, result->cy, result->helpId );
616 TRACE(" STYLE 0x%08lx\n", result->style );
617 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
619 /* Get the menu name */
621 switch(GET_WORD(p))
623 case 0x0000:
624 result->menuName = NULL;
625 p++;
626 break;
627 case 0xffff:
628 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
629 p += 2;
630 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
631 break;
632 default:
633 result->menuName = (LPCSTR)p;
634 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
635 p += strlenW( (LPCWSTR)p ) + 1;
636 break;
639 /* Get the class name */
641 switch(GET_WORD(p))
643 case 0x0000:
644 result->className = DIALOG_CLASS_ATOM;
645 p++;
646 break;
647 case 0xffff:
648 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
649 p += 2;
650 TRACE(" CLASS %04x\n", LOWORD(result->className) );
651 break;
652 default:
653 result->className = (LPCSTR)p;
654 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
655 p += strlenW( (LPCWSTR)p ) + 1;
656 break;
659 /* Get the window caption */
661 result->caption = (LPCSTR)p;
662 p += strlenW( (LPCWSTR)p ) + 1;
663 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
665 /* Get the font name */
667 if (result->style & DS_SETFONT)
669 result->pointSize = GET_WORD(p);
670 p++;
671 if (result->dialogEx)
673 result->weight = GET_WORD(p); p++;
674 result->italic = LOBYTE(GET_WORD(p)); p++;
676 else
678 result->weight = FW_DONTCARE;
679 result->italic = FALSE;
681 result->faceName = (LPCSTR)p;
682 p += strlenW( (LPCWSTR)p ) + 1;
683 TRACE(" FONT %d, %s, %d, %s\n",
684 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
685 result->weight, result->italic ? "TRUE" : "FALSE" );
688 /* First control is on dword boundary */
689 return (LPCSTR)((((int)p) + 3) & ~3);
693 /***********************************************************************
694 * DIALOG_CreateIndirect
695 * Creates a dialog box window
697 * modal = TRUE if we are called from a modal dialog box.
698 * (it's more compatible to do it here, as under Windows the owner
699 * is never disabled if the dialog fails because of an invalid template)
701 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
702 BOOL win32Template, HWND owner,
703 DLGPROC16 dlgProc, LPARAM param,
704 WINDOWPROCTYPE procType, BOOL modal )
706 HMENU16 hMenu = 0;
707 HFONT16 hFont = 0;
708 HWND hwnd;
709 RECT rect;
710 WND * wndPtr;
711 DLG_TEMPLATE template;
712 DIALOGINFO * dlgInfo;
713 WORD xUnit = xBaseUnit;
714 WORD yUnit = yBaseUnit;
715 BOOL ownerEnabled = TRUE;
717 /* Parse dialog template */
719 if (!dlgTemplate) return 0;
720 if (win32Template)
721 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
722 else
723 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
725 /* Load menu */
727 if (template.menuName)
729 if (!win32Template) hMenu = LoadMenu16( hInst, template.menuName );
730 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
733 /* Create custom font if needed */
735 if (template.style & DS_SETFONT)
737 /* The font height must be negative as it is a point size */
738 /* and must be converted to pixels first */
739 /* (see CreateFont() documentation in the Windows SDK). */
740 HDC dc;
741 int pixels;
742 if (((short)template.pointSize) < 0)
743 pixels = -((short)template.pointSize);
744 else
746 dc = GetDC(0);
747 pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
748 ReleaseDC(0, dc);
750 if (win32Template)
751 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
752 template.italic, FALSE, FALSE,
753 DEFAULT_CHARSET, 0, 0,
754 PROOF_QUALITY, FF_DONTCARE,
755 (LPCWSTR)template.faceName );
756 else
757 hFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
758 FALSE, FALSE, FALSE,
759 DEFAULT_CHARSET, 0, 0,
760 PROOF_QUALITY, FF_DONTCARE,
761 template.faceName );
762 if (hFont)
764 SIZE charSize;
765 if (DIALOG_GetCharSize(hFont,&charSize))
767 xUnit = charSize.cx;
768 yUnit = charSize.cy;
771 TRACE("units = %d,%d\n", xUnit, yUnit );
774 /* Create dialog main window */
776 rect.left = rect.top = 0;
777 rect.right = MulDiv(template.cx, xUnit, 4);
778 rect.bottom = MulDiv(template.cy, yUnit, 8);
779 if (template.style & DS_MODALFRAME)
780 template.exStyle |= WS_EX_DLGMODALFRAME;
781 AdjustWindowRectEx( &rect, template.style,
782 hMenu ? TRUE : FALSE , template.exStyle );
783 rect.right -= rect.left;
784 rect.bottom -= rect.top;
786 if ((INT16)template.x == CW_USEDEFAULT16)
788 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
790 else
792 if (template.style & DS_CENTER)
794 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
795 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
797 else
799 rect.left += MulDiv(template.x, xUnit, 4);
800 rect.top += MulDiv(template.y, yUnit, 8);
802 if ( !(template.style & WS_CHILD) )
804 INT16 dX, dY;
806 if( !(template.style & DS_ABSALIGN) )
807 ClientToScreen( owner, (POINT *)&rect );
809 /* try to fit it into the desktop */
811 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
812 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
813 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
814 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
815 if( rect.left < 0 ) rect.left = 0;
816 if( rect.top < 0 ) rect.top = 0;
820 if (modal)
821 ownerEnabled = DIALOG_DisableOwner( owner );
823 if (!win32Template)
824 hwnd = CreateWindowEx16(template.exStyle, template.className,
825 template.caption, template.style & ~WS_VISIBLE,
826 rect.left, rect.top, rect.right, rect.bottom,
827 owner, hMenu, hInst, NULL );
828 else
829 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
830 (LPCWSTR)template.caption,
831 template.style & ~WS_VISIBLE,
832 rect.left, rect.top, rect.right, rect.bottom,
833 owner, hMenu, hInst, NULL );
835 if (!hwnd)
837 if (hFont) DeleteObject( hFont );
838 if (hMenu) DestroyMenu( hMenu );
839 if (modal)
840 DIALOG_EnableOwner(owner, ownerEnabled);
841 return 0;
843 wndPtr = WIN_FindWndPtr( hwnd );
844 wndPtr->flags |= WIN_ISDIALOG;
845 wndPtr->helpContext = template.helpId;
847 /* Initialise dialog extra data */
849 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
850 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
851 dlgInfo->hUserFont = hFont;
852 dlgInfo->hMenu = hMenu;
853 dlgInfo->xBaseUnit = xUnit;
854 dlgInfo->yBaseUnit = yUnit;
855 dlgInfo->msgResult = 0;
856 dlgInfo->idResult = 0;
857 dlgInfo->flags = ownerEnabled ? DF_OWNERENABLED: 0;
858 dlgInfo->hDialogHeap = 0;
860 if (dlgInfo->hUserFont)
861 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
863 /* Create controls */
865 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
866 hInst, win32Template ))
868 HWND hwndPreInitFocus;
870 /* Send initialisation messages and set focus */
872 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
874 hwndPreInitFocus = GetFocus();
875 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
877 /* check where the focus is again,
878 * some controls status might have changed in WM_INITDIALOG */
879 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
880 SetFocus( dlgInfo->hwndFocus );
882 else
884 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
885 but the focus has not changed, set the focus where we expect it. */
886 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
888 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
889 SetFocus( dlgInfo->hwndFocus );
893 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
895 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
897 WIN_ReleaseWndPtr(wndPtr);
898 return hwnd;
900 WIN_ReleaseWndPtr(wndPtr);
901 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
902 if (modal)
903 DIALOG_EnableOwner(owner, ownerEnabled);
904 return 0;
908 /***********************************************************************
909 * CreateDialog (USER.89)
911 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
912 HWND16 owner, DLGPROC16 dlgProc )
914 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
918 /***********************************************************************
919 * CreateDialogParam (USER.241)
921 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
922 HWND16 owner, DLGPROC16 dlgProc,
923 LPARAM param )
925 HWND16 hwnd = 0;
926 HRSRC16 hRsrc;
927 HGLOBAL16 hmem;
928 LPCVOID data;
930 TRACE("%04x,%s,%04x,%08lx,%ld\n",
931 hInst, debugres_a(dlgTemplate), owner, (DWORD)dlgProc, param );
933 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
934 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
935 if (!(data = LockResource16( hmem ))) hwnd = 0;
936 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
937 dlgProc, param );
938 FreeResource16( hmem );
939 return hwnd;
942 /***********************************************************************
943 * CreateDialogParamA (USER32.@)
945 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
946 HWND owner, DLGPROC dlgProc,
947 LPARAM param )
949 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
950 if (!hrsrc) return 0;
951 return CreateDialogIndirectParamA( hInst,
952 (LPVOID)LoadResource(hInst, hrsrc),
953 owner, dlgProc, param );
957 /***********************************************************************
958 * CreateDialogParamW (USER32.@)
960 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
961 HWND owner, DLGPROC dlgProc,
962 LPARAM param )
964 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
965 if (!hrsrc) return 0;
966 return CreateDialogIndirectParamW( hInst,
967 (LPVOID)LoadResource(hInst, hrsrc),
968 owner, dlgProc, param );
972 /***********************************************************************
973 * CreateDialogIndirect (USER.219)
975 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
976 HWND16 owner, DLGPROC16 dlgProc )
978 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
982 /***********************************************************************
983 * CreateDialogIndirectParam (USER.242)
984 * CreateDialogIndirectParam16 (USER32.@)
986 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
987 LPCVOID dlgTemplate,
988 HWND16 owner, DLGPROC16 dlgProc,
989 LPARAM param )
991 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
992 dlgProc, param, WIN_PROC_16, FALSE );
996 /***********************************************************************
997 * CreateDialogIndirectParamA (USER32.@)
999 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
1000 LPCVOID dlgTemplate,
1001 HWND owner, DLGPROC dlgProc,
1002 LPARAM param )
1004 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1005 (DLGPROC16)dlgProc, param, WIN_PROC_32A, FALSE );
1008 /***********************************************************************
1009 * CreateDialogIndirectParamAorW (USER32.@)
1011 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
1012 LPCVOID dlgTemplate,
1013 HWND owner, DLGPROC dlgProc,
1014 LPARAM param )
1015 { FIXME("assume WIN_PROC_32W\n");
1016 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1017 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1020 /***********************************************************************
1021 * CreateDialogIndirectParamW (USER32.@)
1023 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1024 LPCVOID dlgTemplate,
1025 HWND owner, DLGPROC dlgProc,
1026 LPARAM param )
1028 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1029 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1033 /***********************************************************************
1034 * DIALOG_DoDialogBox
1036 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1038 WND * wndPtr;
1039 DIALOGINFO * dlgInfo;
1040 MSG msg;
1041 INT retval;
1042 HWND ownerMsg = WIN_GetTopParent( owner );
1044 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
1045 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1047 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1049 ShowWindow( hwnd, SW_SHOW );
1050 while (MSG_InternalGetMessage(&msg, hwnd, ownerMsg, 0, 0, MSGF_DIALOGBOX,
1051 PM_REMOVE, !(wndPtr->dwStyle & DS_NOIDLEMSG), NULL ))
1053 if (!(dlgInfo->flags & DF_END) && (!IsDialogMessageA( hwnd, &msg)))
1055 TranslateMessage( &msg );
1056 DispatchMessageA( &msg );
1058 if (dlgInfo->flags & DF_END) break;
1061 DIALOG_EnableOwner( owner, (dlgInfo->flags & DF_OWNERENABLED) );
1062 retval = dlgInfo->idResult;
1063 WIN_ReleaseWndPtr(wndPtr);
1064 DestroyWindow( hwnd );
1065 return retval;
1069 /***********************************************************************
1070 * DialogBox (USER.87)
1072 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1073 HWND16 owner, DLGPROC16 dlgProc )
1075 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1079 /***********************************************************************
1080 * DialogBoxParam (USER.239)
1082 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1083 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1085 HWND16 hwnd = 0;
1086 HRSRC16 hRsrc;
1087 HGLOBAL16 hmem;
1088 LPCVOID data;
1089 int ret = -1;
1091 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1092 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1093 if (!(data = LockResource16( hmem ))) hwnd = 0;
1094 else hwnd = DIALOG_CreateIndirect( hInst, data, FALSE, owner,
1095 dlgProc, param, WIN_PROC_16, TRUE );
1096 if (hwnd)
1097 ret =(INT16)DIALOG_DoDialogBox( hwnd, owner );
1098 if (data) GlobalUnlock16( hmem );
1099 FreeResource16( hmem );
1100 return ret;
1104 /***********************************************************************
1105 * DialogBoxParamA (USER32.@)
1107 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1108 HWND owner, DLGPROC dlgProc, LPARAM param )
1110 HWND hwnd;
1111 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1112 if (!hrsrc) return 0;
1113 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1114 TRUE, owner,
1115 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1116 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1117 return -1;
1121 /***********************************************************************
1122 * DialogBoxParamW (USER32.@)
1124 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1125 HWND owner, DLGPROC dlgProc, LPARAM param )
1127 HWND hwnd;
1128 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1129 if (!hrsrc) return 0;
1130 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1131 TRUE, owner,
1132 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1133 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1134 return -1;
1138 /***********************************************************************
1139 * DialogBoxIndirect (USER.218)
1141 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1142 HWND16 owner, DLGPROC16 dlgProc )
1144 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1148 /***********************************************************************
1149 * DialogBoxIndirectParam (USER.240)
1150 * DialogBoxIndirectParam16 (USER32.@)
1152 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1153 HWND16 owner, DLGPROC16 dlgProc,
1154 LPARAM param )
1156 HWND16 hwnd;
1157 LPCVOID ptr;
1159 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1160 hwnd = DIALOG_CreateIndirect( hInst, ptr, FALSE, owner,
1161 dlgProc, param, WIN_PROC_16, TRUE );
1162 GlobalUnlock16( dlgTemplate );
1163 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1164 return -1;
1168 /***********************************************************************
1169 * DialogBoxIndirectParamA (USER32.@)
1171 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1172 HWND owner, DLGPROC dlgProc,
1173 LPARAM param )
1175 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1176 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1177 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1178 return -1;
1182 /***********************************************************************
1183 * DialogBoxIndirectParamW (USER32.@)
1185 INT WINAPI DialogBoxIndirectParamW(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_32W, TRUE );
1191 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1192 return -1;
1195 /***********************************************************************
1196 * DialogBoxIndirectParamAorW (USER32.@)
1198 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1199 HWND owner, DLGPROC dlgProc,
1200 LPARAM param, DWORD x )
1202 HWND hwnd;
1203 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1204 hInstance, template, owner, dlgProc, param, x);
1205 hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1206 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1207 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1208 return -1;
1211 /***********************************************************************
1212 * EndDialog (USER.88)
1214 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1216 return EndDialog( hwnd, retval );
1220 /***********************************************************************
1221 * EndDialog (USER32.@)
1223 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1225 WND * wndPtr = WIN_FindWndPtr( hwnd );
1226 BOOL wasEnabled = TRUE;
1227 DIALOGINFO * dlgInfo;
1229 TRACE("%04x %d\n", hwnd, retval );
1231 if (!wndPtr)
1233 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1234 return FALSE;
1237 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1239 dlgInfo->idResult = retval;
1240 dlgInfo->flags |= DF_END;
1241 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1244 if(wndPtr->owner)
1245 DIALOG_EnableOwner( wndPtr->owner->hwndSelf, wasEnabled );
1247 /* Windows sets the focus to the dialog itself in EndDialog */
1249 if (IsChild(hwnd, GetFocus()))
1250 SetFocus(wndPtr->hwndSelf);
1252 /* Don't have to send a ShowWindow(SW_HIDE), just do
1253 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1255 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1256 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1258 WIN_ReleaseWndPtr(wndPtr);
1259 /* unblock dialog loop */
1260 PostMessageA(hwnd, WM_NULL, 0, 0);
1261 return TRUE;
1265 /***********************************************************************
1266 * DIALOG_IsAccelerator
1268 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1270 HWND hwndControl = hwnd;
1271 HWND hwndNext;
1272 WND *wndPtr;
1273 BOOL RetVal = FALSE;
1274 INT dlgCode;
1278 wndPtr = WIN_FindWndPtr( hwndControl );
1279 if ( (wndPtr != NULL) &&
1280 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1282 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1283 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1284 (wndPtr->text!=NULL))
1286 /* find the accelerator key */
1287 LPWSTR p = wndPtr->text - 2;
1288 char a_char = vKey;
1289 WCHAR w_char = 0;
1293 p = strchrW( p + 2, '&' );
1295 while (p != NULL && p[1] == '&');
1297 /* and check if it's the one we're looking for */
1298 MultiByteToWideChar(CP_ACP, 0, &a_char, 1, &w_char, 1);
1299 if (p != NULL && toupperW( p[1] ) == toupperW( w_char ) )
1301 if ((dlgCode & DLGC_STATIC) ||
1302 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1304 /* set focus to the control */
1305 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1306 hwndControl, 1);
1307 /* and bump it on to next */
1308 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1310 else if (dlgCode & DLGC_BUTTON)
1312 /* send BM_CLICK message to the control */
1313 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1316 RetVal = TRUE;
1317 WIN_ReleaseWndPtr(wndPtr);
1318 break;
1321 hwndNext = GetWindow( hwndControl, GW_CHILD );
1323 else
1325 hwndNext = 0;
1327 WIN_ReleaseWndPtr(wndPtr);
1328 if (!hwndNext)
1330 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1332 while (!hwndNext && hwndControl)
1334 hwndControl = GetParent( hwndControl );
1335 if (hwndControl == hwndDlg)
1337 if(hwnd==hwndDlg){ /* prevent endless loop */
1338 hwndNext=hwnd;
1339 break;
1341 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1343 else
1345 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1348 hwndControl = hwndNext;
1350 while (hwndControl && (hwndControl != hwnd));
1352 return RetVal;
1355 /***********************************************************************
1356 * DIALOG_FindMsgDestination
1358 * The messages that IsDialogMessage sends may not go to the dialog
1359 * calling IsDialogMessage if that dialog is a child, and it has the
1360 * DS_CONTROL style set.
1361 * We propagate up until we hit one that does not have DS_CONTROL, or
1362 * whose parent is not a dialog.
1364 * This is undocumented behaviour.
1366 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1368 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1370 WND *pParent;
1371 HWND hParent = GetParent(hwndDlg);
1372 if (!hParent) break;
1374 pParent = WIN_FindWndPtr(hParent);
1375 if (!pParent) break;
1377 if (!(pParent->flags & WIN_ISDIALOG))
1379 WIN_ReleaseWndPtr(pParent);
1380 break;
1382 WIN_ReleaseWndPtr(pParent);
1384 hwndDlg = hParent;
1387 return hwndDlg;
1390 /***********************************************************************
1391 * DIALOG_IsDialogMessage
1393 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1394 UINT message, WPARAM wParam,
1395 LPARAM lParam, BOOL *translate,
1396 BOOL *dispatch, INT dlgCode )
1398 *translate = *dispatch = FALSE;
1400 if (message == WM_PAINT)
1402 /* Apparently, we have to handle this one as well */
1403 *dispatch = TRUE;
1404 return TRUE;
1407 /* Only the key messages get special processing */
1408 if ((message != WM_KEYDOWN) &&
1409 (message != WM_SYSKEYDOWN) &&
1410 (message != WM_SYSCHAR) &&
1411 (message != WM_CHAR))
1412 return FALSE;
1414 if (dlgCode & DLGC_WANTMESSAGE)
1416 *translate = *dispatch = TRUE;
1417 return TRUE;
1420 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1422 switch(message)
1424 case WM_KEYDOWN:
1425 switch(wParam)
1427 case VK_TAB:
1428 if (!(dlgCode & DLGC_WANTTAB))
1430 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1431 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1432 return TRUE;
1434 break;
1436 case VK_RIGHT:
1437 case VK_DOWN:
1438 case VK_LEFT:
1439 case VK_UP:
1440 if (!(dlgCode & DLGC_WANTARROWS))
1442 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1443 HWND hwndNext =
1444 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1445 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1446 return TRUE;
1448 break;
1450 case VK_ESCAPE:
1451 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1452 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1453 return TRUE;
1455 case VK_RETURN:
1457 DWORD dw = SendMessage16( hwndDlg, DM_GETDEFID, 0, 0 );
1458 if (HIWORD(dw) == DC_HASDEFID)
1460 SendMessageA( hwndDlg, WM_COMMAND,
1461 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1462 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1464 else
1466 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1467 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1471 return TRUE;
1473 *translate = TRUE;
1474 break; /* case WM_KEYDOWN */
1476 case WM_CHAR:
1477 if (dlgCode & DLGC_WANTCHARS) break;
1478 /* drop through */
1480 case WM_SYSCHAR:
1481 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1483 /* don't translate or dispatch */
1484 return TRUE;
1486 break;
1488 case WM_SYSKEYDOWN:
1489 *translate = TRUE;
1490 break;
1493 /* If we get here, the message has not been treated specially */
1494 /* and can be sent to its destination window. */
1495 *dispatch = TRUE;
1496 return TRUE;
1500 /***********************************************************************
1501 * IsDialogMessage (USER.90)
1503 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1505 LPMSG16 msg = MapSL(msg16);
1506 BOOL ret, translate, dispatch;
1507 INT dlgCode = 0;
1509 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1510 return FALSE;
1512 if ((msg->message == WM_KEYDOWN) ||
1513 (msg->message == WM_CHAR))
1515 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1517 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1518 msg->wParam, msg->lParam,
1519 &translate, &dispatch, dlgCode );
1520 if (translate) TranslateMessage16( msg );
1521 if (dispatch) DispatchMessage16( msg );
1522 return ret;
1526 /***********************************************************************
1527 * IsDialogMessageA (USER32.@)
1529 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1531 BOOL ret, translate, dispatch;
1532 INT dlgCode = 0;
1534 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1535 return FALSE;
1537 if ((msg->message == WM_KEYDOWN) ||
1538 (msg->message == WM_CHAR))
1540 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1542 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1543 msg->wParam, msg->lParam,
1544 &translate, &dispatch, dlgCode );
1545 if (translate) TranslateMessage( msg );
1546 if (dispatch) DispatchMessageA( msg );
1547 return ret;
1551 /***********************************************************************
1552 * IsDialogMessageW (USER32.@)
1554 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1556 BOOL ret, translate, dispatch;
1557 INT dlgCode = 0;
1559 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1560 return FALSE;
1562 if ((msg->message == WM_KEYDOWN) ||
1563 (msg->message == WM_CHAR))
1565 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1567 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1568 msg->wParam, msg->lParam,
1569 &translate, &dispatch, dlgCode );
1570 if (translate) TranslateMessage( msg );
1571 if (dispatch) DispatchMessageW( msg );
1572 return ret;
1576 /***********************************************************************
1577 * GetDlgCtrlID (USER.277)
1579 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1581 WND *wndPtr = WIN_FindWndPtr(hwnd);
1582 INT16 retvalue;
1584 if (!wndPtr) return 0;
1586 retvalue = wndPtr->wIDmenu;
1587 WIN_ReleaseWndPtr(wndPtr);
1588 return retvalue;
1592 /***********************************************************************
1593 * GetDlgCtrlID (USER32.@)
1595 INT WINAPI GetDlgCtrlID( HWND hwnd )
1597 INT retvalue;
1598 WND *wndPtr = WIN_FindWndPtr(hwnd);
1599 if (!wndPtr) return 0;
1600 retvalue = wndPtr->wIDmenu;
1601 WIN_ReleaseWndPtr(wndPtr);
1602 return retvalue;
1606 /***********************************************************************
1607 * GetDlgItem (USER.91)
1609 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1611 WND *pWnd;
1613 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1614 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1615 if (pWnd->wIDmenu == (UINT16)id)
1617 HWND16 retvalue = pWnd->hwndSelf;
1618 WIN_ReleaseWndPtr(pWnd);
1619 return retvalue;
1621 return 0;
1625 /***********************************************************************
1626 * GetDlgItem (USER32.@)
1628 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1630 WND *pWnd;
1632 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1633 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
1634 if (pWnd->wIDmenu == (UINT16)id)
1636 HWND retvalue = pWnd->hwndSelf;
1637 WIN_ReleaseWndPtr(pWnd);
1638 return retvalue;
1640 return 0;
1644 /*******************************************************************
1645 * SendDlgItemMessage (USER.101)
1647 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1648 WPARAM16 wParam, LPARAM lParam )
1650 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1651 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1652 else return 0;
1656 /*******************************************************************
1657 * SendDlgItemMessageA (USER32.@)
1659 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1660 WPARAM wParam, LPARAM lParam )
1662 HWND hwndCtrl = GetDlgItem( hwnd, id );
1663 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1664 else return 0;
1668 /*******************************************************************
1669 * SendDlgItemMessageW (USER32.@)
1671 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1672 WPARAM wParam, LPARAM lParam )
1674 HWND hwndCtrl = GetDlgItem( hwnd, id );
1675 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1676 else return 0;
1680 /*******************************************************************
1681 * SetDlgItemText (USER.92)
1683 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1685 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1689 /*******************************************************************
1690 * SetDlgItemTextA (USER32.@)
1692 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1694 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1698 /*******************************************************************
1699 * SetDlgItemTextW (USER32.@)
1701 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1703 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1707 /***********************************************************************
1708 * GetDlgItemText (USER.93)
1710 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1712 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1713 len, (LPARAM)str );
1717 /***********************************************************************
1718 * GetDlgItemTextA (USER32.@)
1720 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1722 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1723 len, (LPARAM)str );
1727 /***********************************************************************
1728 * GetDlgItemTextW (USER32.@)
1730 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1732 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1733 len, (LPARAM)str );
1737 /*******************************************************************
1738 * SetDlgItemInt (USER.94)
1740 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1742 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1746 /*******************************************************************
1747 * SetDlgItemInt (USER32.@)
1749 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1750 BOOL fSigned )
1752 char str[20];
1754 if (fSigned) sprintf( str, "%d", (INT)value );
1755 else sprintf( str, "%u", value );
1756 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1757 return TRUE;
1761 /***********************************************************************
1762 * GetDlgItemInt (USER.95)
1764 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1765 BOOL16 fSigned )
1767 UINT result;
1768 BOOL ok;
1770 if (translated) *translated = FALSE;
1771 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1772 if (!ok) return 0;
1773 if (fSigned)
1775 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1777 else
1779 if (result > 65535) return 0;
1781 if (translated) *translated = TRUE;
1782 return (UINT16)result;
1786 /***********************************************************************
1787 * GetDlgItemInt (USER32.@)
1789 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1790 BOOL fSigned )
1792 char str[30];
1793 char * endptr;
1794 long result = 0;
1796 if (translated) *translated = FALSE;
1797 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1798 return 0;
1799 if (fSigned)
1801 result = strtol( str, &endptr, 10 );
1802 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1803 return 0;
1804 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1805 return 0;
1807 else
1809 result = strtoul( str, &endptr, 10 );
1810 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1811 return 0;
1812 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1814 if (translated) *translated = TRUE;
1815 return (UINT)result;
1819 /***********************************************************************
1820 * CheckDlgButton (USER.97)
1822 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1824 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1825 return TRUE;
1829 /***********************************************************************
1830 * CheckDlgButton (USER32.@)
1832 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1834 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1835 return TRUE;
1839 /***********************************************************************
1840 * IsDlgButtonChecked (USER.98)
1842 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1844 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1848 /***********************************************************************
1849 * IsDlgButtonChecked (USER32.@)
1851 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1853 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1857 /***********************************************************************
1858 * CheckRadioButton (USER.96)
1860 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1861 UINT16 lastID, UINT16 checkID )
1863 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1867 /***********************************************************************
1868 * CheckRB
1870 * Callback function used to check/uncheck radio buttons that fall
1871 * within a specific range of IDs.
1873 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1875 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1876 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1878 if ((lChildID >= lpRadioGroup->firstID) &&
1879 (lChildID <= lpRadioGroup->lastID))
1881 if (lChildID == lpRadioGroup->checkID)
1883 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1885 else
1887 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1891 return TRUE;
1895 /***********************************************************************
1896 * CheckRadioButton (USER32.@)
1898 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1899 UINT lastID, UINT checkID )
1901 RADIOGROUP radioGroup;
1903 /* perform bounds checking for a radio button group */
1904 radioGroup.firstID = min(min(firstID, lastID), checkID);
1905 radioGroup.lastID = max(max(firstID, lastID), checkID);
1906 radioGroup.checkID = checkID;
1908 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1909 (LPARAM)&radioGroup);
1913 /***********************************************************************
1914 * GetDialogBaseUnits (USER.243) (USER32.@)
1916 DWORD WINAPI GetDialogBaseUnits(void)
1918 return MAKELONG( xBaseUnit, yBaseUnit );
1922 /***********************************************************************
1923 * MapDialogRect (USER.103)
1925 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1927 DIALOGINFO * dlgInfo;
1928 WND * wndPtr = WIN_FindWndPtr( hwnd );
1929 if (!wndPtr) return;
1930 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1931 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1932 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1933 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1934 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1935 WIN_ReleaseWndPtr(wndPtr);
1939 /***********************************************************************
1940 * MapDialogRect (USER32.@)
1942 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1944 DIALOGINFO * dlgInfo;
1945 WND * wndPtr = WIN_FindWndPtr( hwnd );
1946 if (!wndPtr) return FALSE;
1947 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1948 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1949 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1950 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1951 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1952 WIN_ReleaseWndPtr(wndPtr);
1953 return TRUE;
1957 /***********************************************************************
1958 * GetNextDlgGroupItem (USER.227)
1960 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1961 BOOL16 fPrevious )
1963 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1967 /***********************************************************************
1968 * GetNextDlgGroupItem (USER32.@)
1970 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1971 BOOL fPrevious )
1973 WND *pWnd = NULL,
1974 *pWndLast = NULL,
1975 *pWndCtrl = NULL,
1976 *pWndDlg = NULL;
1977 HWND retvalue;
1979 if(hwndCtrl)
1981 /* if the hwndCtrl is the child of the control in the hwndDlg,
1982 * then the hwndDlg has to be the parent of the hwndCtrl */
1983 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1984 hwndDlg = GetParent(hwndCtrl);
1987 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
1988 if (hwndCtrl)
1990 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
1992 retvalue = 0;
1993 goto END;
1995 /* Make sure hwndCtrl is a top-level child */
1996 while (pWndCtrl->parent && (pWndCtrl->parent != pWndDlg))
1997 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
1998 if (pWndCtrl->parent != pWndDlg)
2000 retvalue = 0;
2001 goto END;
2004 else
2006 /* No ctrl specified -> start from the beginning */
2007 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
2009 retvalue = 0;
2010 goto END;
2012 if (fPrevious)
2013 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
2016 pWndLast = WIN_LockWndPtr(pWndCtrl);
2017 pWnd = WIN_LockWndPtr(pWndCtrl->next);
2019 while (1)
2021 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
2023 /* Wrap-around to the beginning of the group */
2024 WND *pWndTemp;
2026 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
2027 for ( pWndTemp = WIN_LockWndPtr( pWnd );
2028 pWndTemp;
2029 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
2031 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
2032 if (pWndTemp == pWndCtrl) break;
2034 WIN_ReleaseWndPtr( pWndTemp );
2036 if (pWnd == pWndCtrl) break;
2037 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
2039 WIN_UpdateWndPtr(&pWndLast,pWnd);
2040 if (!fPrevious) break;
2042 WIN_UpdateWndPtr(&pWnd,pWnd->next);
2044 retvalue = pWndLast->hwndSelf;
2046 WIN_ReleaseWndPtr(pWndLast);
2047 WIN_ReleaseWndPtr(pWnd);
2048 END:
2049 WIN_ReleaseWndPtr(pWndCtrl);
2050 WIN_ReleaseWndPtr(pWndDlg);
2052 return retvalue;
2056 /***********************************************************************
2057 * GetNextDlgTabItem (USER.228)
2059 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
2060 BOOL16 fPrevious )
2062 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
2065 /***********************************************************************
2066 * DIALOG_GetNextTabItem
2068 * Helper for GetNextDlgTabItem
2070 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
2072 LONG dsStyle;
2073 LONG exStyle;
2074 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
2075 HWND retWnd = 0;
2076 HWND hChildFirst = 0;
2078 if(!hwndCtrl)
2080 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
2081 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
2083 else
2085 HWND hParent = GetParent(hwndCtrl);
2086 BOOL bValid = FALSE;
2087 while( hParent)
2089 if(hParent == hwndMain)
2091 bValid = TRUE;
2092 break;
2094 hParent = GetParent(hParent);
2096 if(bValid)
2098 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2099 if(!hChildFirst)
2101 if(GetParent(hwndCtrl) != hwndMain)
2102 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2103 else
2105 if(fPrevious)
2106 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2107 else
2108 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2113 while(hChildFirst)
2115 BOOL bCtrl = FALSE;
2116 while(hChildFirst)
2118 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2119 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2120 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2122 bCtrl=TRUE;
2123 break;
2125 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2126 break;
2127 hChildFirst = GetWindow(hChildFirst,wndSearch);
2129 if(hChildFirst)
2131 if(bCtrl)
2132 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2133 else
2134 retWnd = hChildFirst;
2136 if(retWnd) break;
2137 hChildFirst = GetWindow(hChildFirst,wndSearch);
2139 if(!retWnd && hwndCtrl)
2141 HWND hParent = GetParent(hwndCtrl);
2142 while(hParent)
2144 if(hParent == hwndMain) break;
2145 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2146 if(retWnd) break;
2147 hParent = GetParent(hParent);
2149 if(!retWnd)
2150 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2152 return retWnd;
2155 /***********************************************************************
2156 * GetNextDlgTabItem (USER32.@)
2158 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2159 BOOL fPrevious )
2161 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2164 /**********************************************************************
2165 * DIALOG_DlgDirSelect
2167 * Helper function for DlgDirSelect*
2169 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2170 INT id, BOOL win32, BOOL unicode,
2171 BOOL combo )
2173 char *buffer, *ptr;
2174 INT item, size;
2175 BOOL ret;
2176 HWND listbox = GetDlgItem( hwnd, id );
2178 TRACE("%04x '%s' %d\n", hwnd, str, id );
2179 if (!listbox) return FALSE;
2180 if (win32)
2182 item = SendMessageA(listbox, combo ? CB_GETCURSEL
2183 : LB_GETCURSEL, 0, 0 );
2184 if (item == LB_ERR) return FALSE;
2185 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
2186 : LB_GETTEXTLEN, 0, 0 );
2187 if (size == LB_ERR) return FALSE;
2189 else
2191 item = SendMessageA(listbox, combo ? CB_GETCURSEL16
2192 : LB_GETCURSEL16, 0, 0 );
2193 if (item == LB_ERR) return FALSE;
2194 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN16
2195 : LB_GETTEXTLEN16, 0, 0 );
2196 if (size == LB_ERR) return FALSE;
2199 if (!(buffer = SEGPTR_ALLOC( size+1 ))) return FALSE;
2201 if (win32)
2202 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
2203 item, (LPARAM)buffer );
2204 else
2205 SendMessage16( listbox, combo ? CB_GETLBTEXT16 : LB_GETTEXT16,
2206 item, (LPARAM)SEGPTR_GET(buffer) );
2208 if ((ret = (buffer[0] == '['))) /* drive or directory */
2210 if (buffer[1] == '-') /* drive */
2212 buffer[3] = ':';
2213 buffer[4] = 0;
2214 ptr = buffer + 2;
2216 else
2218 buffer[strlen(buffer)-1] = '\\';
2219 ptr = buffer + 1;
2222 else ptr = buffer;
2224 if (unicode)
2226 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
2227 ((LPWSTR)str)[len-1] = 0;
2229 else lstrcpynA( str, ptr, len );
2230 SEGPTR_FREE( buffer );
2231 TRACE("Returning %d '%s'\n", ret, str );
2232 return ret;
2236 /**********************************************************************
2237 * DIALOG_DlgDirList
2239 * Helper function for DlgDirList*
2241 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2242 INT idStatic, UINT attrib, BOOL combo )
2244 HWND hwnd;
2245 LPSTR orig_spec = spec;
2247 #define SENDMSG(msg,wparam,lparam) \
2248 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2249 : SendMessageA( hwnd, msg, wparam, lparam ))
2251 TRACE("%04x '%s' %d %d %04x\n",
2252 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2254 /* If the path exists and is a directory, chdir to it */
2255 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
2256 else
2258 char *p, *p2;
2259 p = spec;
2260 if ((p2 = strrchr( p, '\\' ))) p = p2;
2261 if ((p2 = strrchr( p, '/' ))) p = p2;
2262 if (p != spec)
2264 char sep = *p;
2265 *p = 0;
2266 if (!SetCurrentDirectoryA( spec ))
2268 *p = sep; /* Restore the original spec */
2269 return FALSE;
2271 spec = p + 1;
2275 TRACE( "mask=%s\n", spec );
2277 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2279 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2280 if (attrib & DDL_DIRECTORY)
2282 if (!(attrib & DDL_EXCLUSIVE))
2284 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2285 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2286 (LPARAM)spec ) == LB_ERR)
2287 return FALSE;
2289 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2290 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2291 (LPARAM)"*.*" ) == LB_ERR)
2292 return FALSE;
2294 else
2296 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2297 (LPARAM)spec ) == LB_ERR)
2298 return FALSE;
2302 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2304 char temp[MAX_PATH];
2305 GetCurrentDirectoryA( sizeof(temp), temp );
2306 CharLowerA( temp );
2307 /* Can't use PostMessage() here, because the string is on the stack */
2308 SetDlgItemTextA( hDlg, idStatic, temp );
2311 if (orig_spec && (spec != orig_spec))
2313 /* Update the original file spec */
2314 char *p = spec;
2315 while ((*orig_spec++ = *p++));
2318 return TRUE;
2319 #undef SENDMSG
2323 /**********************************************************************
2324 * DIALOG_DlgDirListW
2326 * Helper function for DlgDirList*W
2328 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2329 INT idStatic, UINT attrib, BOOL combo )
2331 if (spec)
2333 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2334 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2335 attrib, combo );
2336 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
2337 HeapFree( GetProcessHeap(), 0, specA );
2338 return ret;
2340 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2344 /**********************************************************************
2345 * DlgDirSelect (USER.99)
2347 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2349 return DlgDirSelectEx16( hwnd, str, 128, id );
2353 /**********************************************************************
2354 * DlgDirSelectComboBox (USER.194)
2356 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2358 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2362 /**********************************************************************
2363 * DlgDirSelectEx (USER.422)
2365 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2367 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, FALSE );
2371 /**********************************************************************
2372 * DlgDirSelectExA (USER32.@)
2374 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2376 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
2380 /**********************************************************************
2381 * DlgDirSelectExW (USER32.@)
2383 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2385 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
2389 /**********************************************************************
2390 * DlgDirSelectComboBoxEx (USER.423)
2392 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2393 INT16 id )
2395 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, TRUE );
2399 /**********************************************************************
2400 * DlgDirSelectComboBoxExA (USER32.@)
2402 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2403 INT id )
2405 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
2409 /**********************************************************************
2410 * DlgDirSelectComboBoxExW (USER32.@)
2412 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2413 INT id)
2415 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
2419 /**********************************************************************
2420 * DlgDirList (USER.100)
2422 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2423 INT16 idStatic, UINT16 attrib )
2425 /* according to Win16 docs, DDL_DRIVES should make DDL_EXCLUSIVE
2426 * be set automatically (this is different in Win32, and
2427 * DIALOG_DlgDirList sends Win32 messages to the control,
2428 * so do it here) */
2429 if (attrib & DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
2430 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2434 /**********************************************************************
2435 * DlgDirListA (USER32.@)
2437 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2438 INT idStatic, UINT attrib )
2440 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2444 /**********************************************************************
2445 * DlgDirListW (USER32.@)
2447 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2448 INT idStatic, UINT attrib )
2450 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2454 /**********************************************************************
2455 * DlgDirListComboBox (USER.195)
2457 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2458 INT16 idStatic, UINT16 attrib )
2460 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2464 /**********************************************************************
2465 * DlgDirListComboBoxA (USER32.@)
2467 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2468 INT idStatic, UINT attrib )
2470 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2474 /**********************************************************************
2475 * DlgDirListComboBoxW (USER32.@)
2477 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2478 INT idStatic, UINT attrib )
2480 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );