Replace SendMessage16 by SendMessageW where possible.
[wine/gsoc_dplay.git] / windows / dialog.c
blob7b863e65cc346402de38ec8af7b8040d4cb9eba9
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, BOOL ownerWasEnabled)
105 /* Owner must be a top-level window */
106 if (hOwner)
107 hOwner = WIN_GetTopParent( hOwner );
108 if (!hOwner) return;
109 if (ownerWasEnabled)
110 EnableWindow( hOwner, TRUE );
114 /***********************************************************************
115 * DIALOG_DisableOwner
117 * Helper function for modal dialogs to disable the
118 * owner of the dialog box. Returns TRUE if owner was enabled.
120 BOOL DIALOG_DisableOwner( HWND hOwner )
122 /* Owner must be a top-level window */
123 if (hOwner)
124 hOwner = WIN_GetTopParent( hOwner );
125 if (!hOwner) return FALSE;
126 if (IsWindowEnabled( hOwner ))
128 EnableWindow( hOwner, FALSE );
129 return TRUE;
131 else
132 return FALSE;
135 /***********************************************************************
136 * DIALOG_GetCharSizeFromDC
139 * Calculates the *true* average size of English characters in the
140 * specified font as oppposed to the one returned by GetTextMetrics.
142 * Latest: the X font driver will now compute a proper average width
143 * so this code can be removed
145 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
147 BOOL Success = FALSE;
148 HFONT hFontPrev = 0;
149 pSize->cx = xBaseUnit;
150 pSize->cy = yBaseUnit;
151 if ( hDC )
153 /* select the font */
154 TEXTMETRICA tm;
155 memset(&tm,0,sizeof(tm));
156 if (hFont) hFontPrev = SelectFont(hDC,hFont);
157 if (GetTextMetricsA(hDC,&tm))
159 pSize->cx = tm.tmAveCharWidth;
160 pSize->cy = tm.tmHeight;
162 /* if variable width font */
163 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
165 SIZE total;
166 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
168 /* Calculate a true average as opposed to the one returned
169 * by tmAveCharWidth. This works better when dealing with
170 * proportional spaced fonts and (more important) that's
171 * how Microsoft's dialog creation code calculates the size
172 * of the font
174 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
176 /* round up */
177 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
178 Success = TRUE;
181 else
183 Success = TRUE;
185 /* Use the text metrics */
186 TRACE("Using tm: %ldx%ld (dlg: %ld x %ld) (%s)\n",
187 tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
188 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
189 pSize->cx = tm.tmAveCharWidth;
190 pSize->cy = tm.tmHeight;
192 /* select the original font */
193 if (hFontPrev) SelectFont(hDC,hFontPrev);
195 return (Success);
198 /***********************************************************************
199 * DIALOG_GetCharSize
201 * A convenient variant of DIALOG_GetCharSizeFromDC.
203 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
205 HDC hDC = GetDC(0);
206 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
207 ReleaseDC(0, hDC);
208 return Success;
211 /***********************************************************************
212 * DIALOG_Init
214 * Initialisation of the dialog manager.
216 BOOL DIALOG_Init(void)
218 HDC hdc;
219 SIZE size;
221 /* Calculate the dialog base units */
223 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
225 ERR("Could not create Display DC\n");
226 return FALSE;
229 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
231 DeleteDC( hdc );
232 ERR("Could not initialize base dialog units\n");
233 return FALSE;
236 DeleteDC( hdc );
237 xBaseUnit = size.cx;
238 yBaseUnit = size.cy;
240 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
241 return TRUE;
245 /***********************************************************************
246 * DIALOG_GetControl16
248 * Return the class and text of the control pointed to by ptr,
249 * fill the header structure and return a pointer to the next control.
251 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
253 static char buffer[10];
254 int int_id;
256 info->x = GET_WORD(p); p += sizeof(WORD);
257 info->y = GET_WORD(p); p += sizeof(WORD);
258 info->cx = GET_WORD(p); p += sizeof(WORD);
259 info->cy = GET_WORD(p); p += sizeof(WORD);
260 info->id = GET_WORD(p); p += sizeof(WORD);
261 info->style = GET_DWORD(p); p += sizeof(DWORD);
262 info->exStyle = 0;
264 if (*p & 0x80)
266 switch((BYTE)*p)
268 case 0x80: strcpy( buffer, "BUTTON" ); break;
269 case 0x81: strcpy( buffer, "EDIT" ); break;
270 case 0x82: strcpy( buffer, "STATIC" ); break;
271 case 0x83: strcpy( buffer, "LISTBOX" ); break;
272 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
273 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
274 default: buffer[0] = '\0'; break;
276 info->className = buffer;
277 p++;
279 else
281 info->className = p;
282 p += strlen(p) + 1;
285 int_id = ((BYTE)*p == 0xff);
286 if (int_id)
288 /* Integer id, not documented (?). Only works for SS_ICON controls */
289 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
290 p += 3;
292 else
294 info->windowName = p;
295 p += strlen(p) + 1;
298 if (*p)
300 /* Additional CTLDATA available for this control. */
301 info->data = SEGPTR_ALLOC(*p);
302 memcpy( info->data, p + 1, *p );
304 else info->data = NULL;
306 p += *p + 1;
308 if(int_id)
309 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
310 info->className, LOWORD(info->windowName),
311 info->id, info->x, info->y, info->cx, info->cy,
312 info->style, (DWORD)SEGPTR_GET(info->data) );
313 else
314 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
315 info->className, info->windowName,
316 info->id, info->x, info->y, info->cx, info->cy,
317 info->style, (DWORD)SEGPTR_GET(info->data) );
319 return p;
323 /***********************************************************************
324 * DIALOG_GetControl32
326 * Return the class and text of the control pointed to by ptr,
327 * fill the header structure and return a pointer to the next control.
329 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
330 BOOL dialogEx )
332 if (dialogEx)
334 info->helpId = GET_DWORD(p); p += 2;
335 info->exStyle = GET_DWORD(p); p += 2;
336 info->style = GET_DWORD(p); p += 2;
338 else
340 info->helpId = 0;
341 info->style = GET_DWORD(p); p += 2;
342 info->exStyle = GET_DWORD(p); p += 2;
344 info->x = GET_WORD(p); p++;
345 info->y = GET_WORD(p); p++;
346 info->cx = GET_WORD(p); p++;
347 info->cy = GET_WORD(p); p++;
349 if (dialogEx)
351 /* id is a DWORD for DIALOGEX */
352 info->id = GET_DWORD(p);
353 p += 2;
355 else
357 info->id = GET_WORD(p);
358 p++;
361 if (GET_WORD(p) == 0xffff)
363 static const WCHAR class_names[6][10] =
365 { 'B','u','t','t','o','n', }, /* 0x80 */
366 { 'E','d','i','t', }, /* 0x81 */
367 { 'S','t','a','t','i','c', }, /* 0x82 */
368 { 'L','i','s','t','B','o','x', }, /* 0x83 */
369 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
370 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
372 WORD id = GET_WORD(p+1);
373 if ((id >= 0x80) && (id <= 0x85))
374 info->className = (LPCSTR)class_names[id - 0x80];
375 else
377 info->className = NULL;
378 ERR("Unknown built-in class id %04x\n", id );
380 p += 2;
382 else
384 info->className = (LPCSTR)p;
385 p += strlenW( (LPCWSTR)p ) + 1;
388 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
390 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
391 p += 2;
393 else
395 info->windowName = (LPCSTR)p;
396 p += strlenW( (LPCWSTR)p ) + 1;
399 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
400 debugstr_w( (LPCWSTR)info->className ),
401 debugres_w( (LPCWSTR)info->windowName ),
402 info->id, info->x, info->y, info->cx, info->cy,
403 info->style, info->exStyle, info->helpId );
405 if (GET_WORD(p))
407 if (TRACE_ON(dialog))
409 WORD i, count = GET_WORD(p) / sizeof(WORD);
410 TRACE(" BEGIN\n");
411 TRACE(" ");
412 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
413 DPRINTF("\n");
414 TRACE(" END\n" );
416 info->data = (LPVOID)(p + 1);
417 p += GET_WORD(p) / sizeof(WORD);
419 else info->data = NULL;
420 p++;
422 /* Next control is on dword boundary */
423 return (const WORD *)((((int)p) + 3) & ~3);
427 /***********************************************************************
428 * DIALOG_CreateControls
430 * Create the control windows for a dialog.
432 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
433 const DLG_TEMPLATE *dlgTemplate,
434 HINSTANCE hInst, BOOL win32 )
436 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
437 DLG_CONTROL_INFO info;
438 HWND hwndCtrl, hwndDefButton = 0;
439 INT items = dlgTemplate->nbItems;
441 TRACE(" BEGIN\n" );
442 while (items--)
444 if (!win32)
446 HINSTANCE16 instance;
447 template = DIALOG_GetControl16( template, &info );
448 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
449 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
451 if (!dlgInfo->hDialogHeap)
453 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
454 if (!dlgInfo->hDialogHeap)
456 ERR("Insufficient memory to create heap for edit control\n" );
457 continue;
459 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
461 instance = dlgInfo->hDialogHeap;
463 else instance = (HINSTANCE16)hInst;
465 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
466 info.className, info.windowName,
467 info.style | WS_CHILD,
468 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
469 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
470 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
471 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
472 pWnd->hwndSelf, (HMENU16)info.id,
473 instance, (LPVOID)SEGPTR_GET(info.data) );
475 if (info.data) SEGPTR_FREE(info.data);
477 else
479 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
480 dlgTemplate->dialogEx );
481 /* Is this it? */
482 if (info.style & WS_BORDER)
484 info.style &= ~WS_BORDER;
485 info.exStyle |= WS_EX_CLIENTEDGE;
487 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
488 (LPCWSTR)info.className,
489 (LPCWSTR)info.windowName,
490 info.style | WS_CHILD,
491 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
492 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
493 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
494 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
495 pWnd->hwndSelf, (HMENU)info.id,
496 hInst, info.data );
498 if (!hwndCtrl) return FALSE;
500 /* Send initialisation messages to the control */
501 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
502 (WPARAM)dlgInfo->hUserFont, 0 );
503 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
505 /* If there's already a default push-button, set it back */
506 /* to normal and use this one instead. */
507 if (hwndDefButton)
508 SendMessageA( hwndDefButton, BM_SETSTYLE,
509 BS_PUSHBUTTON,FALSE );
510 hwndDefButton = hwndCtrl;
511 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
514 TRACE(" END\n" );
515 return TRUE;
519 /***********************************************************************
520 * DIALOG_ParseTemplate16
522 * Fill a DLG_TEMPLATE structure from the dialog template, and return
523 * a pointer to the first control.
525 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
527 result->style = GET_DWORD(p); p += sizeof(DWORD);
528 result->exStyle = 0;
529 result->nbItems = (unsigned char) *p++;
530 result->x = GET_WORD(p); p += sizeof(WORD);
531 result->y = GET_WORD(p); p += sizeof(WORD);
532 result->cx = GET_WORD(p); p += sizeof(WORD);
533 result->cy = GET_WORD(p); p += sizeof(WORD);
534 TRACE("DIALOG %d, %d, %d, %d\n",
535 result->x, result->y, result->cx, result->cy );
536 TRACE(" STYLE %08lx\n", result->style );
538 /* Get the menu name */
540 switch( (BYTE)*p )
542 case 0:
543 result->menuName = 0;
544 p++;
545 break;
546 case 0xff:
547 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
548 p += 3;
549 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
550 break;
551 default:
552 result->menuName = p;
553 TRACE(" MENU '%s'\n", p );
554 p += strlen(p) + 1;
555 break;
558 /* Get the class name */
560 if (*p)
562 result->className = p;
563 TRACE(" CLASS '%s'\n", result->className );
565 else result->className = DIALOG_CLASS_ATOM;
566 p += strlen(p) + 1;
568 /* Get the window caption */
570 result->caption = p;
571 p += strlen(p) + 1;
572 TRACE(" CAPTION '%s'\n", result->caption );
574 /* Get the font name */
576 if (result->style & DS_SETFONT)
578 result->pointSize = GET_WORD(p);
579 p += sizeof(WORD);
580 result->faceName = p;
581 p += strlen(p) + 1;
582 TRACE(" FONT %d,'%s'\n",
583 result->pointSize, result->faceName );
585 return p;
589 /***********************************************************************
590 * DIALOG_ParseTemplate32
592 * Fill a DLG_TEMPLATE structure from the dialog template, and return
593 * a pointer to the first control.
595 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
597 const WORD *p = (const WORD *)template;
599 result->style = GET_DWORD(p); p += 2;
600 if (result->style == 0xffff0001) /* DIALOGEX resource */
602 result->dialogEx = TRUE;
603 result->helpId = GET_DWORD(p); p += 2;
604 result->exStyle = GET_DWORD(p); p += 2;
605 result->style = GET_DWORD(p); p += 2;
607 else
609 result->dialogEx = FALSE;
610 result->helpId = 0;
611 result->exStyle = GET_DWORD(p); p += 2;
613 result->nbItems = GET_WORD(p); p++;
614 result->x = GET_WORD(p); p++;
615 result->y = GET_WORD(p); p++;
616 result->cx = GET_WORD(p); p++;
617 result->cy = GET_WORD(p); p++;
618 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
619 result->dialogEx ? "EX" : "", result->x, result->y,
620 result->cx, result->cy, result->helpId );
621 TRACE(" STYLE 0x%08lx\n", result->style );
622 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
624 /* Get the menu name */
626 switch(GET_WORD(p))
628 case 0x0000:
629 result->menuName = NULL;
630 p++;
631 break;
632 case 0xffff:
633 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
634 p += 2;
635 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
636 break;
637 default:
638 result->menuName = (LPCSTR)p;
639 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
640 p += strlenW( (LPCWSTR)p ) + 1;
641 break;
644 /* Get the class name */
646 switch(GET_WORD(p))
648 case 0x0000:
649 result->className = DIALOG_CLASS_ATOM;
650 p++;
651 break;
652 case 0xffff:
653 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
654 p += 2;
655 TRACE(" CLASS %04x\n", LOWORD(result->className) );
656 break;
657 default:
658 result->className = (LPCSTR)p;
659 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
660 p += strlenW( (LPCWSTR)p ) + 1;
661 break;
664 /* Get the window caption */
666 result->caption = (LPCSTR)p;
667 p += strlenW( (LPCWSTR)p ) + 1;
668 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
670 /* Get the font name */
672 if (result->style & DS_SETFONT)
674 result->pointSize = GET_WORD(p);
675 p++;
676 if (result->dialogEx)
678 result->weight = GET_WORD(p); p++;
679 result->italic = LOBYTE(GET_WORD(p)); p++;
681 else
683 result->weight = FW_DONTCARE;
684 result->italic = FALSE;
686 result->faceName = (LPCSTR)p;
687 p += strlenW( (LPCWSTR)p ) + 1;
688 TRACE(" FONT %d, %s, %d, %s\n",
689 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
690 result->weight, result->italic ? "TRUE" : "FALSE" );
693 /* First control is on dword boundary */
694 return (LPCSTR)((((int)p) + 3) & ~3);
698 /***********************************************************************
699 * DIALOG_CreateIndirect
700 * Creates a dialog box window
702 * modal = TRUE if we are called from a modal dialog box.
703 * (it's more compatible to do it here, as under Windows the owner
704 * is never disabled if the dialog fails because of an invalid template)
706 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
707 BOOL win32Template, HWND owner,
708 DLGPROC16 dlgProc, LPARAM param,
709 WINDOWPROCTYPE procType, BOOL modal )
711 HMENU16 hMenu = 0;
712 HFONT16 hFont = 0;
713 HWND hwnd;
714 RECT rect;
715 WND * wndPtr;
716 DLG_TEMPLATE template;
717 DIALOGINFO * dlgInfo;
718 WORD xUnit = xBaseUnit;
719 WORD yUnit = yBaseUnit;
720 BOOL ownerEnabled = TRUE;
722 /* Parse dialog template */
724 if (!dlgTemplate) return 0;
725 if (win32Template)
726 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
727 else
728 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
730 /* Load menu */
732 if (template.menuName)
734 if (!win32Template) hMenu = LoadMenu16( hInst, template.menuName );
735 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
738 /* Create custom font if needed */
740 if (template.style & DS_SETFONT)
742 /* The font height must be negative as it is a point size */
743 /* and must be converted to pixels first */
744 /* (see CreateFont() documentation in the Windows SDK). */
745 HDC dc;
746 int pixels;
747 if (((short)template.pointSize) < 0)
748 pixels = -((short)template.pointSize);
749 else
751 dc = GetDC(0);
752 pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
753 ReleaseDC(0, dc);
755 if (win32Template)
756 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
757 template.italic, FALSE, FALSE,
758 DEFAULT_CHARSET, 0, 0,
759 PROOF_QUALITY, FF_DONTCARE,
760 (LPCWSTR)template.faceName );
761 else
762 hFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
763 FALSE, FALSE, FALSE,
764 DEFAULT_CHARSET, 0, 0,
765 PROOF_QUALITY, FF_DONTCARE,
766 template.faceName );
767 if (hFont)
769 SIZE charSize;
770 if (DIALOG_GetCharSize(hFont,&charSize))
772 xUnit = charSize.cx;
773 yUnit = charSize.cy;
776 TRACE("units = %d,%d\n", xUnit, yUnit );
779 /* Create dialog main window */
781 rect.left = rect.top = 0;
782 rect.right = MulDiv(template.cx, xUnit, 4);
783 rect.bottom = MulDiv(template.cy, yUnit, 8);
784 if (template.style & DS_MODALFRAME)
785 template.exStyle |= WS_EX_DLGMODALFRAME;
786 AdjustWindowRectEx( &rect, template.style,
787 hMenu ? TRUE : FALSE , template.exStyle );
788 rect.right -= rect.left;
789 rect.bottom -= rect.top;
791 if ((INT16)template.x == CW_USEDEFAULT16)
793 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
795 else
797 if (template.style & DS_CENTER)
799 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
800 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
802 else
804 rect.left += MulDiv(template.x, xUnit, 4);
805 rect.top += MulDiv(template.y, yUnit, 8);
807 if ( !(template.style & WS_CHILD) )
809 INT16 dX, dY;
811 if( !(template.style & DS_ABSALIGN) )
812 ClientToScreen( owner, (POINT *)&rect );
814 /* try to fit it into the desktop */
816 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
817 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
818 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
819 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
820 if( rect.left < 0 ) rect.left = 0;
821 if( rect.top < 0 ) rect.top = 0;
825 if (modal)
826 ownerEnabled = DIALOG_DisableOwner( owner );
828 if (!win32Template)
829 hwnd = CreateWindowEx16(template.exStyle, template.className,
830 template.caption, template.style & ~WS_VISIBLE,
831 rect.left, rect.top, rect.right, rect.bottom,
832 owner, hMenu, hInst, NULL );
833 else
834 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
835 (LPCWSTR)template.caption,
836 template.style & ~WS_VISIBLE,
837 rect.left, rect.top, rect.right, rect.bottom,
838 owner, hMenu, hInst, NULL );
840 if (!hwnd)
842 if (hFont) DeleteObject( hFont );
843 if (hMenu) DestroyMenu( hMenu );
844 if (modal)
845 DIALOG_EnableOwner(owner, ownerEnabled);
846 return 0;
848 wndPtr = WIN_FindWndPtr( hwnd );
849 wndPtr->flags |= WIN_ISDIALOG;
850 wndPtr->helpContext = template.helpId;
852 /* Initialise dialog extra data */
854 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
855 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
856 dlgInfo->hUserFont = hFont;
857 dlgInfo->hMenu = hMenu;
858 dlgInfo->xBaseUnit = xUnit;
859 dlgInfo->yBaseUnit = yUnit;
860 dlgInfo->msgResult = 0;
861 dlgInfo->idResult = 0;
862 dlgInfo->flags = ownerEnabled ? DF_OWNERENABLED: 0;
863 dlgInfo->hDialogHeap = 0;
865 if (dlgInfo->hUserFont)
866 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
868 /* Create controls */
870 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
871 hInst, win32Template ))
873 HWND hwndPreInitFocus;
875 /* Send initialisation messages and set focus */
877 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
879 hwndPreInitFocus = GetFocus();
880 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
882 /* check where the focus is again,
883 * some controls status might have changed in WM_INITDIALOG */
884 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
885 SetFocus( dlgInfo->hwndFocus );
887 else
889 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
890 but the focus has not changed, set the focus where we expect it. */
891 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
893 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
894 SetFocus( dlgInfo->hwndFocus );
898 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
900 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
902 WIN_ReleaseWndPtr(wndPtr);
903 return hwnd;
905 WIN_ReleaseWndPtr(wndPtr);
906 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
907 if (modal)
908 DIALOG_EnableOwner(owner, ownerEnabled);
909 return 0;
913 /***********************************************************************
914 * CreateDialog (USER.89)
916 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
917 HWND16 owner, DLGPROC16 dlgProc )
919 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
923 /***********************************************************************
924 * CreateDialogParam (USER.241)
926 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
927 HWND16 owner, DLGPROC16 dlgProc,
928 LPARAM param )
930 HWND16 hwnd = 0;
931 HRSRC16 hRsrc;
932 HGLOBAL16 hmem;
933 LPCVOID data;
935 TRACE("%04x,%s,%04x,%08lx,%ld\n",
936 hInst, debugres_a(dlgTemplate), owner, (DWORD)dlgProc, param );
938 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
939 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
940 if (!(data = LockResource16( hmem ))) hwnd = 0;
941 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
942 dlgProc, param );
943 FreeResource16( hmem );
944 return hwnd;
947 /***********************************************************************
948 * CreateDialogParamA (USER32.@)
950 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
951 HWND owner, DLGPROC dlgProc,
952 LPARAM param )
954 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
955 if (!hrsrc) return 0;
956 return CreateDialogIndirectParamA( hInst,
957 (LPVOID)LoadResource(hInst, hrsrc),
958 owner, dlgProc, param );
962 /***********************************************************************
963 * CreateDialogParamW (USER32.@)
965 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
966 HWND owner, DLGPROC dlgProc,
967 LPARAM param )
969 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
970 if (!hrsrc) return 0;
971 return CreateDialogIndirectParamW( hInst,
972 (LPVOID)LoadResource(hInst, hrsrc),
973 owner, dlgProc, param );
977 /***********************************************************************
978 * CreateDialogIndirect (USER.219)
980 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
981 HWND16 owner, DLGPROC16 dlgProc )
983 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
987 /***********************************************************************
988 * CreateDialogIndirectParam (USER.242)
989 * CreateDialogIndirectParam16 (USER32.@)
991 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
992 LPCVOID dlgTemplate,
993 HWND16 owner, DLGPROC16 dlgProc,
994 LPARAM param )
996 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
997 dlgProc, param, WIN_PROC_16, FALSE );
1001 /***********************************************************************
1002 * CreateDialogIndirectParamA (USER32.@)
1004 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
1005 LPCVOID dlgTemplate,
1006 HWND owner, DLGPROC dlgProc,
1007 LPARAM param )
1009 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1010 (DLGPROC16)dlgProc, param, WIN_PROC_32A, FALSE );
1013 /***********************************************************************
1014 * CreateDialogIndirectParamAorW (USER32.@)
1016 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
1017 LPCVOID dlgTemplate,
1018 HWND owner, DLGPROC dlgProc,
1019 LPARAM param )
1020 { FIXME("assume WIN_PROC_32W\n");
1021 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1022 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1025 /***********************************************************************
1026 * CreateDialogIndirectParamW (USER32.@)
1028 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1029 LPCVOID dlgTemplate,
1030 HWND owner, DLGPROC dlgProc,
1031 LPARAM param )
1033 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1034 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1038 /***********************************************************************
1039 * DIALOG_DoDialogBox
1041 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1043 WND * wndPtr;
1044 DIALOGINFO * dlgInfo;
1045 MSG msg;
1046 INT retval;
1047 HWND ownerMsg = WIN_GetTopParent( owner );
1049 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
1050 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1052 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1054 ShowWindow( hwnd, SW_SHOW );
1055 for (;;)
1057 if (!(wndPtr->dwStyle & DS_NOIDLEMSG))
1059 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1061 /* No message present -> send ENTERIDLE and wait */
1062 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
1063 if (!GetMessageW( &msg, 0, 0, 0 )) break;
1066 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
1068 if (CallMsgFilterW( &msg, MSGF_DIALOGBOX )) continue;
1070 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
1072 TranslateMessage( &msg );
1073 DispatchMessageW( &msg );
1075 if (dlgInfo->flags & DF_END) break;
1078 DIALOG_EnableOwner( owner, (dlgInfo->flags & DF_OWNERENABLED) );
1079 retval = dlgInfo->idResult;
1080 WIN_ReleaseWndPtr(wndPtr);
1081 DestroyWindow( hwnd );
1082 return retval;
1086 /***********************************************************************
1087 * DialogBox (USER.87)
1089 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1090 HWND16 owner, DLGPROC16 dlgProc )
1092 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1096 /***********************************************************************
1097 * DialogBoxParam (USER.239)
1099 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1100 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1102 HWND16 hwnd = 0;
1103 HRSRC16 hRsrc;
1104 HGLOBAL16 hmem;
1105 LPCVOID data;
1106 int ret = -1;
1108 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1109 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1110 if (!(data = LockResource16( hmem ))) hwnd = 0;
1111 else hwnd = DIALOG_CreateIndirect( hInst, data, FALSE, owner,
1112 dlgProc, param, WIN_PROC_16, TRUE );
1113 if (hwnd)
1114 ret =(INT16)DIALOG_DoDialogBox( hwnd, owner );
1115 if (data) GlobalUnlock16( hmem );
1116 FreeResource16( hmem );
1117 return ret;
1121 /***********************************************************************
1122 * DialogBoxParamA (USER32.@)
1124 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1125 HWND owner, DLGPROC dlgProc, LPARAM param )
1127 HWND hwnd;
1128 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1129 if (!hrsrc) return 0;
1130 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1131 TRUE, owner,
1132 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1133 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1134 return -1;
1138 /***********************************************************************
1139 * DialogBoxParamW (USER32.@)
1141 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1142 HWND owner, DLGPROC dlgProc, LPARAM param )
1144 HWND hwnd;
1145 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1146 if (!hrsrc) return 0;
1147 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1148 TRUE, owner,
1149 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1150 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1151 return -1;
1155 /***********************************************************************
1156 * DialogBoxIndirect (USER.218)
1158 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1159 HWND16 owner, DLGPROC16 dlgProc )
1161 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1165 /***********************************************************************
1166 * DialogBoxIndirectParam (USER.240)
1167 * DialogBoxIndirectParam16 (USER32.@)
1169 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1170 HWND16 owner, DLGPROC16 dlgProc,
1171 LPARAM param )
1173 HWND16 hwnd;
1174 LPCVOID ptr;
1176 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1177 hwnd = DIALOG_CreateIndirect( hInst, ptr, FALSE, owner,
1178 dlgProc, param, WIN_PROC_16, TRUE );
1179 GlobalUnlock16( dlgTemplate );
1180 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1181 return -1;
1185 /***********************************************************************
1186 * DialogBoxIndirectParamA (USER32.@)
1188 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1189 HWND owner, DLGPROC dlgProc,
1190 LPARAM param )
1192 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1193 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1194 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1195 return -1;
1199 /***********************************************************************
1200 * DialogBoxIndirectParamW (USER32.@)
1202 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1203 HWND owner, DLGPROC dlgProc,
1204 LPARAM param )
1206 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1207 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1208 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1209 return -1;
1212 /***********************************************************************
1213 * DialogBoxIndirectParamAorW (USER32.@)
1215 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1216 HWND owner, DLGPROC dlgProc,
1217 LPARAM param, DWORD x )
1219 HWND hwnd;
1220 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1221 hInstance, template, owner, dlgProc, param, x);
1222 hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1223 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1224 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1225 return -1;
1228 /***********************************************************************
1229 * EndDialog (USER.88)
1231 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1233 return EndDialog( hwnd, retval );
1237 /***********************************************************************
1238 * EndDialog (USER32.@)
1240 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1242 WND * wndPtr = WIN_FindWndPtr( hwnd );
1243 BOOL wasEnabled = TRUE;
1244 DIALOGINFO * dlgInfo;
1246 TRACE("%04x %d\n", hwnd, retval );
1248 if (!wndPtr)
1250 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1251 return FALSE;
1254 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1256 dlgInfo->idResult = retval;
1257 dlgInfo->flags |= DF_END;
1258 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1261 if(wndPtr->owner)
1262 DIALOG_EnableOwner( wndPtr->owner->hwndSelf, wasEnabled );
1264 /* Windows sets the focus to the dialog itself in EndDialog */
1266 if (IsChild(hwnd, GetFocus()))
1267 SetFocus(wndPtr->hwndSelf);
1269 /* Don't have to send a ShowWindow(SW_HIDE), just do
1270 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1272 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1273 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1275 WIN_ReleaseWndPtr(wndPtr);
1276 /* unblock dialog loop */
1277 PostMessageA(hwnd, WM_NULL, 0, 0);
1278 return TRUE;
1282 /***********************************************************************
1283 * DIALOG_IsAccelerator
1285 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1287 HWND hwndControl = hwnd;
1288 HWND hwndNext;
1289 WND *wndPtr;
1290 BOOL RetVal = FALSE;
1291 INT dlgCode;
1295 wndPtr = WIN_FindWndPtr( hwndControl );
1296 if ( (wndPtr != NULL) &&
1297 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1299 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1300 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1301 (wndPtr->text!=NULL))
1303 /* find the accelerator key */
1304 LPWSTR p = wndPtr->text - 2;
1305 char a_char = vKey;
1306 WCHAR w_char = 0;
1310 p = strchrW( p + 2, '&' );
1312 while (p != NULL && p[1] == '&');
1314 /* and check if it's the one we're looking for */
1315 MultiByteToWideChar(CP_ACP, 0, &a_char, 1, &w_char, 1);
1316 if (p != NULL && toupperW( p[1] ) == toupperW( w_char ) )
1318 if ((dlgCode & DLGC_STATIC) ||
1319 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1321 /* set focus to the control */
1322 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1323 hwndControl, 1);
1324 /* and bump it on to next */
1325 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1327 else if (dlgCode & DLGC_BUTTON)
1329 /* send BM_CLICK message to the control */
1330 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1333 RetVal = TRUE;
1334 WIN_ReleaseWndPtr(wndPtr);
1335 break;
1338 hwndNext = GetWindow( hwndControl, GW_CHILD );
1340 else
1342 hwndNext = 0;
1344 WIN_ReleaseWndPtr(wndPtr);
1345 if (!hwndNext)
1347 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1349 while (!hwndNext && hwndControl)
1351 hwndControl = GetParent( hwndControl );
1352 if (hwndControl == hwndDlg)
1354 if(hwnd==hwndDlg){ /* prevent endless loop */
1355 hwndNext=hwnd;
1356 break;
1358 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1360 else
1362 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1365 hwndControl = hwndNext;
1367 while (hwndControl && (hwndControl != hwnd));
1369 return RetVal;
1372 /***********************************************************************
1373 * DIALOG_FindMsgDestination
1375 * The messages that IsDialogMessage sends may not go to the dialog
1376 * calling IsDialogMessage if that dialog is a child, and it has the
1377 * DS_CONTROL style set.
1378 * We propagate up until we hit one that does not have DS_CONTROL, or
1379 * whose parent is not a dialog.
1381 * This is undocumented behaviour.
1383 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1385 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1387 WND *pParent;
1388 HWND hParent = GetParent(hwndDlg);
1389 if (!hParent) break;
1391 pParent = WIN_FindWndPtr(hParent);
1392 if (!pParent) break;
1394 if (!(pParent->flags & WIN_ISDIALOG))
1396 WIN_ReleaseWndPtr(pParent);
1397 break;
1399 WIN_ReleaseWndPtr(pParent);
1401 hwndDlg = hParent;
1404 return hwndDlg;
1407 /***********************************************************************
1408 * DIALOG_IsDialogMessage
1410 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1411 UINT message, WPARAM wParam,
1412 LPARAM lParam, BOOL *translate,
1413 BOOL *dispatch, INT dlgCode )
1415 *translate = *dispatch = FALSE;
1417 if (message == WM_PAINT)
1419 /* Apparently, we have to handle this one as well */
1420 *dispatch = TRUE;
1421 return TRUE;
1424 /* Only the key messages get special processing */
1425 if ((message != WM_KEYDOWN) &&
1426 (message != WM_SYSKEYDOWN) &&
1427 (message != WM_SYSCHAR) &&
1428 (message != WM_CHAR))
1429 return FALSE;
1431 if (dlgCode & DLGC_WANTMESSAGE)
1433 *translate = *dispatch = TRUE;
1434 return TRUE;
1437 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1439 switch(message)
1441 case WM_KEYDOWN:
1442 switch(wParam)
1444 case VK_TAB:
1445 if (!(dlgCode & DLGC_WANTTAB))
1447 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1448 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1449 return TRUE;
1451 break;
1453 case VK_RIGHT:
1454 case VK_DOWN:
1455 case VK_LEFT:
1456 case VK_UP:
1457 if (!(dlgCode & DLGC_WANTARROWS))
1459 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1460 HWND hwndNext =
1461 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1462 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1463 return TRUE;
1465 break;
1467 case VK_ESCAPE:
1468 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1469 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1470 return TRUE;
1472 case VK_RETURN:
1474 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1475 if (HIWORD(dw) == DC_HASDEFID)
1477 SendMessageA( hwndDlg, WM_COMMAND,
1478 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1479 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1481 else
1483 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1484 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1488 return TRUE;
1490 *translate = TRUE;
1491 break; /* case WM_KEYDOWN */
1493 case WM_CHAR:
1494 if (dlgCode & DLGC_WANTCHARS) break;
1495 /* drop through */
1497 case WM_SYSCHAR:
1498 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1500 /* don't translate or dispatch */
1501 return TRUE;
1503 break;
1505 case WM_SYSKEYDOWN:
1506 *translate = TRUE;
1507 break;
1510 /* If we get here, the message has not been treated specially */
1511 /* and can be sent to its destination window. */
1512 *dispatch = TRUE;
1513 return TRUE;
1517 /***********************************************************************
1518 * IsDialogMessage (USER.90)
1520 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1522 LPMSG16 msg = MapSL(msg16);
1523 BOOL ret, translate, dispatch;
1524 INT dlgCode = 0;
1526 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1527 return FALSE;
1529 if ((msg->message == WM_KEYDOWN) ||
1530 (msg->message == WM_CHAR))
1532 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1534 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1535 msg->wParam, msg->lParam,
1536 &translate, &dispatch, dlgCode );
1537 if (translate) TranslateMessage16( msg );
1538 if (dispatch) DispatchMessage16( msg );
1539 return ret;
1543 /***********************************************************************
1544 * IsDialogMessage (USER32.@)
1545 * IsDialogMessageA (USER32.@)
1547 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1549 BOOL ret, translate, dispatch;
1550 INT dlgCode = 0;
1552 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1553 return FALSE;
1555 if ((msg->message == WM_KEYDOWN) ||
1556 (msg->message == WM_CHAR))
1558 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1560 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1561 msg->wParam, msg->lParam,
1562 &translate, &dispatch, dlgCode );
1563 if (translate) TranslateMessage( msg );
1564 if (dispatch) DispatchMessageA( msg );
1565 return ret;
1569 /***********************************************************************
1570 * IsDialogMessageW (USER32.@)
1572 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1574 BOOL ret, translate, dispatch;
1575 INT dlgCode = 0;
1577 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1578 return FALSE;
1580 if ((msg->message == WM_KEYDOWN) ||
1581 (msg->message == WM_CHAR))
1583 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1585 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1586 msg->wParam, msg->lParam,
1587 &translate, &dispatch, dlgCode );
1588 if (translate) TranslateMessage( msg );
1589 if (dispatch) DispatchMessageW( msg );
1590 return ret;
1594 /***********************************************************************
1595 * GetDlgCtrlID (USER.277)
1597 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1599 WND *wndPtr = WIN_FindWndPtr(hwnd);
1600 INT16 retvalue;
1602 if (!wndPtr) return 0;
1604 retvalue = wndPtr->wIDmenu;
1605 WIN_ReleaseWndPtr(wndPtr);
1606 return retvalue;
1610 /***********************************************************************
1611 * GetDlgCtrlID (USER32.@)
1613 INT WINAPI GetDlgCtrlID( HWND hwnd )
1615 INT retvalue;
1616 WND *wndPtr = WIN_FindWndPtr(hwnd);
1617 if (!wndPtr) return 0;
1618 retvalue = wndPtr->wIDmenu;
1619 WIN_ReleaseWndPtr(wndPtr);
1620 return retvalue;
1624 /***********************************************************************
1625 * GetDlgItem (USER.91)
1627 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1629 WND *pWnd;
1631 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1632 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1633 if (pWnd->wIDmenu == (UINT16)id)
1635 HWND16 retvalue = pWnd->hwndSelf;
1636 WIN_ReleaseWndPtr(pWnd);
1637 return retvalue;
1639 return 0;
1643 /***********************************************************************
1644 * GetDlgItem (USER32.@)
1646 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1648 WND *pWnd;
1650 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1651 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
1652 if (pWnd->wIDmenu == (UINT16)id)
1654 HWND retvalue = pWnd->hwndSelf;
1655 WIN_ReleaseWndPtr(pWnd);
1656 return retvalue;
1658 return 0;
1662 /*******************************************************************
1663 * SendDlgItemMessage (USER.101)
1665 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1666 WPARAM16 wParam, LPARAM lParam )
1668 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1669 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1670 else return 0;
1674 /*******************************************************************
1675 * SendDlgItemMessageA (USER32.@)
1677 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1678 WPARAM wParam, LPARAM lParam )
1680 HWND hwndCtrl = GetDlgItem( hwnd, id );
1681 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1682 else return 0;
1686 /*******************************************************************
1687 * SendDlgItemMessageW (USER32.@)
1689 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1690 WPARAM wParam, LPARAM lParam )
1692 HWND hwndCtrl = GetDlgItem( hwnd, id );
1693 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1694 else return 0;
1698 /*******************************************************************
1699 * SetDlgItemText (USER.92)
1701 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1703 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1707 /*******************************************************************
1708 * SetDlgItemTextA (USER32.@)
1710 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1712 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1716 /*******************************************************************
1717 * SetDlgItemTextW (USER32.@)
1719 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1721 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1725 /***********************************************************************
1726 * GetDlgItemText (USER.93)
1728 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1730 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1731 len, (LPARAM)str );
1735 /***********************************************************************
1736 * GetDlgItemTextA (USER32.@)
1738 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1740 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1741 len, (LPARAM)str );
1745 /***********************************************************************
1746 * GetDlgItemTextW (USER32.@)
1748 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1750 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1751 len, (LPARAM)str );
1755 /*******************************************************************
1756 * SetDlgItemInt (USER.94)
1758 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1760 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1764 /*******************************************************************
1765 * SetDlgItemInt (USER32.@)
1767 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1768 BOOL fSigned )
1770 char str[20];
1772 if (fSigned) sprintf( str, "%d", (INT)value );
1773 else sprintf( str, "%u", value );
1774 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1775 return TRUE;
1779 /***********************************************************************
1780 * GetDlgItemInt (USER.95)
1782 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1783 BOOL16 fSigned )
1785 UINT result;
1786 BOOL ok;
1788 if (translated) *translated = FALSE;
1789 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1790 if (!ok) return 0;
1791 if (fSigned)
1793 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1795 else
1797 if (result > 65535) return 0;
1799 if (translated) *translated = TRUE;
1800 return (UINT16)result;
1804 /***********************************************************************
1805 * GetDlgItemInt (USER32.@)
1807 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1808 BOOL fSigned )
1810 char str[30];
1811 char * endptr;
1812 long result = 0;
1814 if (translated) *translated = FALSE;
1815 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1816 return 0;
1817 if (fSigned)
1819 result = strtol( str, &endptr, 10 );
1820 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1821 return 0;
1822 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1823 return 0;
1825 else
1827 result = strtoul( str, &endptr, 10 );
1828 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1829 return 0;
1830 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1832 if (translated) *translated = TRUE;
1833 return (UINT)result;
1837 /***********************************************************************
1838 * CheckDlgButton (USER.97)
1840 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1842 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1843 return TRUE;
1847 /***********************************************************************
1848 * CheckDlgButton (USER32.@)
1850 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1852 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1853 return TRUE;
1857 /***********************************************************************
1858 * IsDlgButtonChecked (USER.98)
1860 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1862 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1866 /***********************************************************************
1867 * IsDlgButtonChecked (USER32.@)
1869 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1871 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1875 /***********************************************************************
1876 * CheckRadioButton (USER.96)
1878 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1879 UINT16 lastID, UINT16 checkID )
1881 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1885 /***********************************************************************
1886 * CheckRB
1888 * Callback function used to check/uncheck radio buttons that fall
1889 * within a specific range of IDs.
1891 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1893 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1894 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1896 if ((lChildID >= lpRadioGroup->firstID) &&
1897 (lChildID <= lpRadioGroup->lastID))
1899 if (lChildID == lpRadioGroup->checkID)
1901 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1903 else
1905 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1909 return TRUE;
1913 /***********************************************************************
1914 * CheckRadioButton (USER32.@)
1916 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1917 UINT lastID, UINT checkID )
1919 RADIOGROUP radioGroup;
1921 /* perform bounds checking for a radio button group */
1922 radioGroup.firstID = min(min(firstID, lastID), checkID);
1923 radioGroup.lastID = max(max(firstID, lastID), checkID);
1924 radioGroup.checkID = checkID;
1926 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1927 (LPARAM)&radioGroup);
1931 /***********************************************************************
1932 * GetDialogBaseUnits (USER.243)
1933 * GetDialogBaseUnits (USER32.@)
1935 DWORD WINAPI GetDialogBaseUnits(void)
1937 return MAKELONG( xBaseUnit, yBaseUnit );
1941 /***********************************************************************
1942 * MapDialogRect (USER.103)
1944 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1946 DIALOGINFO * dlgInfo;
1947 WND * wndPtr = WIN_FindWndPtr( hwnd );
1948 if (!wndPtr) return;
1949 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1950 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1951 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1952 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1953 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1954 WIN_ReleaseWndPtr(wndPtr);
1958 /***********************************************************************
1959 * MapDialogRect (USER32.@)
1961 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1963 DIALOGINFO * dlgInfo;
1964 WND * wndPtr = WIN_FindWndPtr( hwnd );
1965 if (!wndPtr) return FALSE;
1966 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1967 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1968 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1969 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1970 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1971 WIN_ReleaseWndPtr(wndPtr);
1972 return TRUE;
1976 /***********************************************************************
1977 * GetNextDlgGroupItem (USER.227)
1979 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1980 BOOL16 fPrevious )
1982 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1986 /***********************************************************************
1987 * GetNextDlgGroupItem (USER32.@)
1989 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1990 BOOL fPrevious )
1992 WND *pWnd = NULL,
1993 *pWndLast = NULL,
1994 *pWndCtrl = NULL,
1995 *pWndDlg = NULL;
1996 HWND retvalue;
1998 if(hwndCtrl)
2000 /* if the hwndCtrl is the child of the control in the hwndDlg,
2001 * then the hwndDlg has to be the parent of the hwndCtrl */
2002 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
2003 hwndDlg = GetParent(hwndCtrl);
2006 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
2007 if (hwndCtrl)
2009 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
2011 retvalue = 0;
2012 goto END;
2014 /* Make sure hwndCtrl is a top-level child */
2015 while (pWndCtrl->parent && (pWndCtrl->parent != pWndDlg))
2016 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
2017 if (pWndCtrl->parent != pWndDlg)
2019 retvalue = 0;
2020 goto END;
2023 else
2025 /* No ctrl specified -> start from the beginning */
2026 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
2028 retvalue = 0;
2029 goto END;
2031 if (fPrevious)
2032 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
2035 pWndLast = WIN_LockWndPtr(pWndCtrl);
2036 pWnd = WIN_LockWndPtr(pWndCtrl->next);
2038 while (1)
2040 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
2042 /* Wrap-around to the beginning of the group */
2043 WND *pWndTemp;
2045 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
2046 for ( pWndTemp = WIN_LockWndPtr( pWnd );
2047 pWndTemp;
2048 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
2050 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
2051 if (pWndTemp == pWndCtrl) break;
2053 WIN_ReleaseWndPtr( pWndTemp );
2055 if (pWnd == pWndCtrl) break;
2056 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
2058 WIN_UpdateWndPtr(&pWndLast,pWnd);
2059 if (!fPrevious) break;
2061 WIN_UpdateWndPtr(&pWnd,pWnd->next);
2063 retvalue = pWndLast->hwndSelf;
2065 WIN_ReleaseWndPtr(pWndLast);
2066 WIN_ReleaseWndPtr(pWnd);
2067 END:
2068 WIN_ReleaseWndPtr(pWndCtrl);
2069 WIN_ReleaseWndPtr(pWndDlg);
2071 return retvalue;
2075 /***********************************************************************
2076 * GetNextDlgTabItem (USER.228)
2078 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
2079 BOOL16 fPrevious )
2081 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
2084 /***********************************************************************
2085 * DIALOG_GetNextTabItem
2087 * Helper for GetNextDlgTabItem
2089 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
2091 LONG dsStyle;
2092 LONG exStyle;
2093 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
2094 HWND retWnd = 0;
2095 HWND hChildFirst = 0;
2097 if(!hwndCtrl)
2099 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
2100 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
2102 else
2104 HWND hParent = GetParent(hwndCtrl);
2105 BOOL bValid = FALSE;
2106 while( hParent)
2108 if(hParent == hwndMain)
2110 bValid = TRUE;
2111 break;
2113 hParent = GetParent(hParent);
2115 if(bValid)
2117 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2118 if(!hChildFirst)
2120 if(GetParent(hwndCtrl) != hwndMain)
2121 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2122 else
2124 if(fPrevious)
2125 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2126 else
2127 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2132 while(hChildFirst)
2134 BOOL bCtrl = FALSE;
2135 while(hChildFirst)
2137 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2138 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2139 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2141 bCtrl=TRUE;
2142 break;
2144 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2145 break;
2146 hChildFirst = GetWindow(hChildFirst,wndSearch);
2148 if(hChildFirst)
2150 if(bCtrl)
2151 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2152 else
2153 retWnd = hChildFirst;
2155 if(retWnd) break;
2156 hChildFirst = GetWindow(hChildFirst,wndSearch);
2158 if(!retWnd && hwndCtrl)
2160 HWND hParent = GetParent(hwndCtrl);
2161 while(hParent)
2163 if(hParent == hwndMain) break;
2164 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2165 if(retWnd) break;
2166 hParent = GetParent(hParent);
2168 if(!retWnd)
2169 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2171 return retWnd;
2174 /***********************************************************************
2175 * GetNextDlgTabItem (USER32.@)
2177 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2178 BOOL fPrevious )
2180 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2183 /**********************************************************************
2184 * DIALOG_DlgDirSelect
2186 * Helper function for DlgDirSelect*
2188 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2189 INT id, BOOL unicode, BOOL combo )
2191 char *buffer, *ptr;
2192 INT item, size;
2193 BOOL ret;
2194 HWND listbox = GetDlgItem( hwnd, id );
2196 TRACE("%04x '%s' %d\n", hwnd, str, id );
2197 if (!listbox) return FALSE;
2199 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
2200 if (item == LB_ERR) return FALSE;
2201 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
2202 if (size == LB_ERR) return FALSE;
2204 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
2206 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)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 HeapFree( GetProcessHeap(), 0, 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 DlgDirSelectExA( hwnd, str, len, id );
2371 /**********************************************************************
2372 * DlgDirSelectExA (USER32.@)
2374 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2376 return DIALOG_DlgDirSelect( hwnd, str, len, id, 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, FALSE );
2389 /**********************************************************************
2390 * DlgDirSelectComboBoxEx (USER.423)
2392 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2393 INT16 id )
2395 return DlgDirSelectComboBoxExA( hwnd, str, len, id );
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, 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 );
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 );