Allow for the dialog font size to be set directly in pixels.
[wine.git] / windows / dialog.c
blobb7a9d2e141b5b90dc5f7de995131e4f454624ae5
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
5 */
7 #include <ctype.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "windef.h"
14 #include "winnls.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "windowsx.h"
18 #include "wine/winuser16.h"
19 #include "wine/winbase16.h"
20 #include "wine/unicode.h"
21 #include "controls.h"
22 #include "heap.h"
23 #include "win.h"
24 #include "user.h"
25 #include "message.h"
26 #include "queue.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(dialog);
32 /* Dialog control information */
33 typedef struct
35 DWORD style;
36 DWORD exStyle;
37 DWORD helpId;
38 INT16 x;
39 INT16 y;
40 INT16 cx;
41 INT16 cy;
42 UINT id;
43 LPCSTR className;
44 LPCSTR windowName;
45 LPVOID data;
46 } DLG_CONTROL_INFO;
48 /* Dialog template */
49 typedef struct
51 DWORD style;
52 DWORD exStyle;
53 DWORD helpId;
54 UINT16 nbItems;
55 INT16 x;
56 INT16 y;
57 INT16 cx;
58 INT16 cy;
59 LPCSTR menuName;
60 LPCSTR className;
61 LPCSTR caption;
62 WORD pointSize;
63 WORD weight;
64 BOOL italic;
65 LPCSTR faceName;
66 BOOL dialogEx;
67 } DLG_TEMPLATE;
69 /* Radio button group */
70 typedef struct
72 UINT firstID;
73 UINT lastID;
74 UINT checkID;
75 } RADIOGROUP;
77 /* Dialog base units */
78 static WORD xBaseUnit = 0, yBaseUnit = 0;
81 /*********************************************************************
82 * dialog class descriptor
84 const struct builtin_class_descr DIALOG_builtin_class =
86 DIALOG_CLASS_ATOM, /* name */
87 CS_GLOBALCLASS | CS_SAVEBITS, /* style */
88 DefDlgProcA, /* procA */
89 DefDlgProcW, /* procW */
90 DLGWINDOWEXTRA, /* extra */
91 IDC_ARROWA, /* cursor */
92 0 /* brush */
96 /***********************************************************************
97 * DIALOG_EnableOwner
99 * Helper function for modal dialogs to enable again the
100 * owner of the dialog box.
102 void DIALOG_EnableOwner( HWND hOwner, BOOL ownerWasEnabled)
104 /* Owner must be a top-level window */
105 if (hOwner)
106 hOwner = WIN_GetTopParent( hOwner );
107 if (!hOwner) return;
108 if (ownerWasEnabled)
109 EnableWindow( hOwner, TRUE );
113 /***********************************************************************
114 * DIALOG_DisableOwner
116 * Helper function for modal dialogs to disable the
117 * owner of the dialog box. Returns TRUE if owner was enabled.
119 BOOL DIALOG_DisableOwner( HWND hOwner )
121 /* Owner must be a top-level window */
122 if (hOwner)
123 hOwner = WIN_GetTopParent( hOwner );
124 if (!hOwner) return FALSE;
125 if (IsWindowEnabled( hOwner ))
127 EnableWindow( hOwner, FALSE );
128 return TRUE;
130 else
131 return FALSE;
134 /***********************************************************************
135 * DIALOG_GetCharSizeFromDC
138 * Calculates the *true* average size of English characters in the
139 * specified font as oppposed to the one returned by GetTextMetrics.
141 * Latest: the X font driver will now compute a proper average width
142 * so this code can be removed
144 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
146 BOOL Success = FALSE;
147 HFONT hFontPrev = 0;
148 pSize->cx = xBaseUnit;
149 pSize->cy = yBaseUnit;
150 if ( hDC )
152 /* select the font */
153 TEXTMETRICA tm;
154 memset(&tm,0,sizeof(tm));
155 if (hFont) hFontPrev = SelectFont(hDC,hFont);
156 if (GetTextMetricsA(hDC,&tm))
158 pSize->cx = tm.tmAveCharWidth;
159 pSize->cy = tm.tmHeight;
161 /* if variable width font */
162 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
164 SIZE total;
165 const char* szAvgChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
167 /* Calculate a true average as opposed to the one returned
168 * by tmAveCharWidth. This works better when dealing with
169 * proportional spaced fonts and (more important) that's
170 * how Microsoft's dialog creation code calculates the size
171 * of the font
173 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
175 /* round up */
176 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
177 Success = TRUE;
180 else
182 Success = TRUE;
184 /* Use the text metrics */
185 TRACE("Using tm: %ldx%ld (dlg: %ld x %ld) (%s)\n",
186 tm.tmAveCharWidth, tm.tmHeight, pSize->cx, pSize->cy,
187 tm.tmPitchAndFamily & TMPF_FIXED_PITCH ? "variable" : "fixed");
188 pSize->cx = tm.tmAveCharWidth;
189 pSize->cy = tm.tmHeight;
191 /* select the original font */
192 if (hFontPrev) SelectFont(hDC,hFontPrev);
194 return (Success);
197 /***********************************************************************
198 * DIALOG_GetCharSize
200 * A convenient variant of DIALOG_GetCharSizeFromDC.
202 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
204 HDC hDC = GetDC(0);
205 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
206 ReleaseDC(0, hDC);
207 return Success;
210 /***********************************************************************
211 * DIALOG_Init
213 * Initialisation of the dialog manager.
215 BOOL DIALOG_Init(void)
217 HDC hdc;
218 SIZE size;
220 /* Calculate the dialog base units */
222 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
224 ERR("Could not create Display DC\n");
225 return FALSE;
228 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
230 DeleteDC( hdc );
231 ERR("Could not initialize base dialog units\n");
232 return FALSE;
235 DeleteDC( hdc );
236 xBaseUnit = size.cx;
237 yBaseUnit = size.cy;
239 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
240 return TRUE;
244 /***********************************************************************
245 * DIALOG_GetControl16
247 * Return the class and text of the control pointed to by ptr,
248 * fill the header structure and return a pointer to the next control.
250 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
252 static char buffer[10];
253 int int_id;
255 info->x = GET_WORD(p); p += sizeof(WORD);
256 info->y = GET_WORD(p); p += sizeof(WORD);
257 info->cx = GET_WORD(p); p += sizeof(WORD);
258 info->cy = GET_WORD(p); p += sizeof(WORD);
259 info->id = GET_WORD(p); p += sizeof(WORD);
260 info->style = GET_DWORD(p); p += sizeof(DWORD);
261 info->exStyle = 0;
263 if (*p & 0x80)
265 switch((BYTE)*p)
267 case 0x80: strcpy( buffer, "BUTTON" ); break;
268 case 0x81: strcpy( buffer, "EDIT" ); break;
269 case 0x82: strcpy( buffer, "STATIC" ); break;
270 case 0x83: strcpy( buffer, "LISTBOX" ); break;
271 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
272 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
273 default: buffer[0] = '\0'; break;
275 info->className = buffer;
276 p++;
278 else
280 info->className = p;
281 p += strlen(p) + 1;
284 int_id = ((BYTE)*p == 0xff);
285 if (int_id)
287 /* Integer id, not documented (?). Only works for SS_ICON controls */
288 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
289 p += 3;
291 else
293 info->windowName = p;
294 p += strlen(p) + 1;
297 if (*p)
299 /* Additional CTLDATA available for this control. */
300 info->data = SEGPTR_ALLOC(*p);
301 memcpy( info->data, p + 1, *p );
303 else info->data = NULL;
305 p += *p + 1;
307 if(int_id)
308 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %08lx\n",
309 info->className, LOWORD(info->windowName),
310 info->id, info->x, info->y, info->cx, info->cy,
311 info->style, (DWORD)SEGPTR_GET(info->data) );
312 else
313 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %08lx\n",
314 info->className, info->windowName,
315 info->id, info->x, info->y, info->cx, info->cy,
316 info->style, (DWORD)SEGPTR_GET(info->data) );
318 return p;
322 /***********************************************************************
323 * DIALOG_GetControl32
325 * Return the class and text of the control pointed to by ptr,
326 * fill the header structure and return a pointer to the next control.
328 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
329 BOOL dialogEx )
331 if (dialogEx)
333 info->helpId = GET_DWORD(p); p += 2;
334 info->exStyle = GET_DWORD(p); p += 2;
335 info->style = GET_DWORD(p); p += 2;
337 else
339 info->helpId = 0;
340 info->style = GET_DWORD(p); p += 2;
341 info->exStyle = GET_DWORD(p); p += 2;
343 info->x = GET_WORD(p); p++;
344 info->y = GET_WORD(p); p++;
345 info->cx = GET_WORD(p); p++;
346 info->cy = GET_WORD(p); p++;
348 if (dialogEx)
350 /* id is a DWORD for DIALOGEX */
351 info->id = GET_DWORD(p);
352 p += 2;
354 else
356 info->id = GET_WORD(p);
357 p++;
360 if (GET_WORD(p) == 0xffff)
362 static const WCHAR class_names[6][10] =
364 { 'B','u','t','t','o','n', }, /* 0x80 */
365 { 'E','d','i','t', }, /* 0x81 */
366 { 'S','t','a','t','i','c', }, /* 0x82 */
367 { 'L','i','s','t','B','o','x', }, /* 0x83 */
368 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
369 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
371 WORD id = GET_WORD(p+1);
372 if ((id >= 0x80) && (id <= 0x85))
373 info->className = (LPCSTR)class_names[id - 0x80];
374 else
376 info->className = NULL;
377 ERR("Unknown built-in class id %04x\n", id );
379 p += 2;
381 else
383 info->className = (LPCSTR)p;
384 p += strlenW( (LPCWSTR)p ) + 1;
387 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
389 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
390 p += 2;
392 else
394 info->windowName = (LPCSTR)p;
395 p += strlenW( (LPCWSTR)p ) + 1;
398 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
399 debugstr_w( (LPCWSTR)info->className ),
400 debugres_w( (LPCWSTR)info->windowName ),
401 info->id, info->x, info->y, info->cx, info->cy,
402 info->style, info->exStyle, info->helpId );
404 if (GET_WORD(p))
406 if (TRACE_ON(dialog))
408 WORD i, count = GET_WORD(p) / sizeof(WORD);
409 TRACE(" BEGIN\n");
410 TRACE(" ");
411 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
412 DPRINTF("\n");
413 TRACE(" END\n" );
415 info->data = (LPVOID)(p + 1);
416 p += GET_WORD(p) / sizeof(WORD);
418 else info->data = NULL;
419 p++;
421 /* Next control is on dword boundary */
422 return (const WORD *)((((int)p) + 3) & ~3);
426 /***********************************************************************
427 * DIALOG_CreateControls
429 * Create the control windows for a dialog.
431 static BOOL DIALOG_CreateControls( WND *pWnd, LPCSTR template,
432 const DLG_TEMPLATE *dlgTemplate,
433 HINSTANCE hInst, BOOL win32 )
435 DIALOGINFO *dlgInfo = (DIALOGINFO *)pWnd->wExtra;
436 DLG_CONTROL_INFO info;
437 HWND hwndCtrl, hwndDefButton = 0;
438 INT items = dlgTemplate->nbItems;
440 TRACE(" BEGIN\n" );
441 while (items--)
443 if (!win32)
445 HINSTANCE16 instance;
446 template = DIALOG_GetControl16( template, &info );
447 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
448 ((pWnd->dwStyle & DS_LOCALEDIT) != DS_LOCALEDIT))
450 if (!dlgInfo->hDialogHeap)
452 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
453 if (!dlgInfo->hDialogHeap)
455 ERR("Insufficient memory to create heap for edit control\n" );
456 continue;
458 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
460 instance = dlgInfo->hDialogHeap;
462 else instance = (HINSTANCE16)hInst;
464 hwndCtrl = CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
465 info.className, info.windowName,
466 info.style | WS_CHILD,
467 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
468 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
469 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
470 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
471 pWnd->hwndSelf, (HMENU16)info.id,
472 instance, (LPVOID)SEGPTR_GET(info.data) );
474 if (info.data) SEGPTR_FREE(info.data);
476 else
478 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
479 dlgTemplate->dialogEx );
480 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
481 (LPCWSTR)info.className,
482 (LPCWSTR)info.windowName,
483 info.style | WS_CHILD,
484 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
485 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
486 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
487 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
488 pWnd->hwndSelf, (HMENU)info.id,
489 hInst, info.data );
491 if (!hwndCtrl) return FALSE;
493 /* Send initialisation messages to the control */
494 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
495 (WPARAM)dlgInfo->hUserFont, 0 );
496 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
498 /* If there's already a default push-button, set it back */
499 /* to normal and use this one instead. */
500 if (hwndDefButton)
501 SendMessageA( hwndDefButton, BM_SETSTYLE,
502 BS_PUSHBUTTON,FALSE );
503 hwndDefButton = hwndCtrl;
504 dlgInfo->idResult = GetWindowWord( hwndCtrl, GWW_ID );
507 TRACE(" END\n" );
508 return TRUE;
512 /***********************************************************************
513 * DIALOG_ParseTemplate16
515 * Fill a DLG_TEMPLATE structure from the dialog template, and return
516 * a pointer to the first control.
518 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
520 result->style = GET_DWORD(p); p += sizeof(DWORD);
521 result->exStyle = 0;
522 result->nbItems = (unsigned char) *p++;
523 result->x = GET_WORD(p); p += sizeof(WORD);
524 result->y = GET_WORD(p); p += sizeof(WORD);
525 result->cx = GET_WORD(p); p += sizeof(WORD);
526 result->cy = GET_WORD(p); p += sizeof(WORD);
527 TRACE("DIALOG %d, %d, %d, %d\n",
528 result->x, result->y, result->cx, result->cy );
529 TRACE(" STYLE %08lx\n", result->style );
531 /* Get the menu name */
533 switch( (BYTE)*p )
535 case 0:
536 result->menuName = 0;
537 p++;
538 break;
539 case 0xff:
540 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
541 p += 3;
542 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
543 break;
544 default:
545 result->menuName = p;
546 TRACE(" MENU '%s'\n", p );
547 p += strlen(p) + 1;
548 break;
551 /* Get the class name */
553 if (*p)
555 result->className = p;
556 TRACE(" CLASS '%s'\n", result->className );
558 else result->className = DIALOG_CLASS_ATOM;
559 p += strlen(p) + 1;
561 /* Get the window caption */
563 result->caption = p;
564 p += strlen(p) + 1;
565 TRACE(" CAPTION '%s'\n", result->caption );
567 /* Get the font name */
569 if (result->style & DS_SETFONT)
571 result->pointSize = GET_WORD(p);
572 p += sizeof(WORD);
573 result->faceName = p;
574 p += strlen(p) + 1;
575 TRACE(" FONT %d,'%s'\n",
576 result->pointSize, result->faceName );
578 return p;
582 /***********************************************************************
583 * DIALOG_ParseTemplate32
585 * Fill a DLG_TEMPLATE structure from the dialog template, and return
586 * a pointer to the first control.
588 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
590 const WORD *p = (const WORD *)template;
592 result->style = GET_DWORD(p); p += 2;
593 if (result->style == 0xffff0001) /* DIALOGEX resource */
595 result->dialogEx = TRUE;
596 result->helpId = GET_DWORD(p); p += 2;
597 result->exStyle = GET_DWORD(p); p += 2;
598 result->style = GET_DWORD(p); p += 2;
600 else
602 result->dialogEx = FALSE;
603 result->helpId = 0;
604 result->exStyle = GET_DWORD(p); p += 2;
606 result->nbItems = GET_WORD(p); p++;
607 result->x = GET_WORD(p); p++;
608 result->y = GET_WORD(p); p++;
609 result->cx = GET_WORD(p); p++;
610 result->cy = GET_WORD(p); p++;
611 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
612 result->dialogEx ? "EX" : "", result->x, result->y,
613 result->cx, result->cy, result->helpId );
614 TRACE(" STYLE 0x%08lx\n", result->style );
615 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
617 /* Get the menu name */
619 switch(GET_WORD(p))
621 case 0x0000:
622 result->menuName = NULL;
623 p++;
624 break;
625 case 0xffff:
626 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
627 p += 2;
628 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
629 break;
630 default:
631 result->menuName = (LPCSTR)p;
632 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
633 p += strlenW( (LPCWSTR)p ) + 1;
634 break;
637 /* Get the class name */
639 switch(GET_WORD(p))
641 case 0x0000:
642 result->className = DIALOG_CLASS_ATOM;
643 p++;
644 break;
645 case 0xffff:
646 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
647 p += 2;
648 TRACE(" CLASS %04x\n", LOWORD(result->className) );
649 break;
650 default:
651 result->className = (LPCSTR)p;
652 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
653 p += strlenW( (LPCWSTR)p ) + 1;
654 break;
657 /* Get the window caption */
659 result->caption = (LPCSTR)p;
660 p += strlenW( (LPCWSTR)p ) + 1;
661 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
663 /* Get the font name */
665 if (result->style & DS_SETFONT)
667 result->pointSize = GET_WORD(p);
668 p++;
669 if (result->dialogEx)
671 result->weight = GET_WORD(p); p++;
672 result->italic = LOBYTE(GET_WORD(p)); p++;
674 else
676 result->weight = FW_DONTCARE;
677 result->italic = FALSE;
679 result->faceName = (LPCSTR)p;
680 p += strlenW( (LPCWSTR)p ) + 1;
681 TRACE(" FONT %d, %s, %d, %s\n",
682 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
683 result->weight, result->italic ? "TRUE" : "FALSE" );
686 /* First control is on dword boundary */
687 return (LPCSTR)((((int)p) + 3) & ~3);
691 /***********************************************************************
692 * DIALOG_CreateIndirect
693 * Creates a dialog box window
695 * modal = TRUE if we are called from a modal dialog box.
696 * (it's more compatible to do it here, as under Windows the owner
697 * is never disabled if the dialog fails because of an invalid template)
699 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
700 BOOL win32Template, HWND owner,
701 DLGPROC16 dlgProc, LPARAM param,
702 WINDOWPROCTYPE procType, BOOL modal )
704 HMENU16 hMenu = 0;
705 HFONT16 hFont = 0;
706 HWND hwnd;
707 RECT rect;
708 WND * wndPtr;
709 DLG_TEMPLATE template;
710 DIALOGINFO * dlgInfo;
711 WORD xUnit = xBaseUnit;
712 WORD yUnit = yBaseUnit;
713 BOOL ownerEnabled = TRUE;
715 /* Parse dialog template */
717 if (!dlgTemplate) return 0;
718 if (win32Template)
719 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
720 else
721 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
723 /* Load menu */
725 if (template.menuName)
727 if (!win32Template) hMenu = LoadMenu16( hInst, template.menuName );
728 else hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
731 /* Create custom font if needed */
733 if (template.style & DS_SETFONT)
735 /* The font height must be negative as it is a point size */
736 /* and must be converted to pixels first */
737 /* (see CreateFont() documentation in the Windows SDK). */
738 HDC dc;
739 int pixels;
740 if (((short)template.pointSize) < 0)
741 pixels = -((short)template.pointSize);
742 else
744 dc = GetDC(0);
745 pixels = template.pointSize * GetDeviceCaps(dc , LOGPIXELSY)/72;
746 ReleaseDC(0, dc);
748 if (win32Template)
749 hFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
750 template.italic, FALSE, FALSE,
751 DEFAULT_CHARSET, 0, 0,
752 PROOF_QUALITY, FF_DONTCARE,
753 (LPCWSTR)template.faceName );
754 else
755 hFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
756 FALSE, FALSE, FALSE,
757 DEFAULT_CHARSET, 0, 0,
758 PROOF_QUALITY, FF_DONTCARE,
759 template.faceName );
760 if (hFont)
762 SIZE charSize;
763 if (DIALOG_GetCharSize(hFont,&charSize))
765 xUnit = charSize.cx;
766 yUnit = charSize.cy;
769 TRACE("units = %d,%d\n", xUnit, yUnit );
772 /* Create dialog main window */
774 rect.left = rect.top = 0;
775 rect.right = MulDiv(template.cx, xUnit, 4);
776 rect.bottom = MulDiv(template.cy, yUnit, 8);
777 if (template.style & DS_MODALFRAME)
778 template.exStyle |= WS_EX_DLGMODALFRAME;
779 AdjustWindowRectEx( &rect, template.style,
780 hMenu ? TRUE : FALSE , template.exStyle );
781 rect.right -= rect.left;
782 rect.bottom -= rect.top;
784 if ((INT16)template.x == CW_USEDEFAULT16)
786 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
788 else
790 if (template.style & DS_CENTER)
792 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
793 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
795 else
797 rect.left += MulDiv(template.x, xUnit, 4);
798 rect.top += MulDiv(template.y, yUnit, 8);
800 if ( !(template.style & WS_CHILD) )
802 INT16 dX, dY;
804 if( !(template.style & DS_ABSALIGN) )
805 ClientToScreen( owner, (POINT *)&rect );
807 /* try to fit it into the desktop */
809 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
810 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
811 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
812 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
813 if( rect.left < 0 ) rect.left = 0;
814 if( rect.top < 0 ) rect.top = 0;
818 if (modal)
819 ownerEnabled = DIALOG_DisableOwner( owner );
821 if (!win32Template)
822 hwnd = CreateWindowEx16(template.exStyle, template.className,
823 template.caption, template.style & ~WS_VISIBLE,
824 rect.left, rect.top, rect.right, rect.bottom,
825 owner, hMenu, hInst, NULL );
826 else
827 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
828 (LPCWSTR)template.caption,
829 template.style & ~WS_VISIBLE,
830 rect.left, rect.top, rect.right, rect.bottom,
831 owner, hMenu, hInst, NULL );
833 if (!hwnd)
835 if (hFont) DeleteObject( hFont );
836 if (hMenu) DestroyMenu( hMenu );
837 if (modal)
838 DIALOG_EnableOwner(owner, ownerEnabled);
839 return 0;
841 wndPtr = WIN_FindWndPtr( hwnd );
842 wndPtr->flags |= WIN_ISDIALOG;
843 wndPtr->helpContext = template.helpId;
845 /* Initialise dialog extra data */
847 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
848 WINPROC_SetProc( &dlgInfo->dlgProc, (WNDPROC16)dlgProc, procType, WIN_PROC_WINDOW );
849 dlgInfo->hUserFont = hFont;
850 dlgInfo->hMenu = hMenu;
851 dlgInfo->xBaseUnit = xUnit;
852 dlgInfo->yBaseUnit = yUnit;
853 dlgInfo->msgResult = 0;
854 dlgInfo->idResult = 0;
855 dlgInfo->flags = ownerEnabled ? DF_OWNERENABLED: 0;
856 dlgInfo->hDialogHeap = 0;
858 if (dlgInfo->hUserFont)
859 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
861 /* Create controls */
863 if (DIALOG_CreateControls( wndPtr, dlgTemplate, &template,
864 hInst, win32Template ))
866 HWND hwndPreInitFocus;
868 /* Send initialisation messages and set focus */
870 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
872 hwndPreInitFocus = GetFocus();
873 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
875 /* check where the focus is again,
876 * some controls status might have changed in WM_INITDIALOG */
877 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
878 SetFocus( dlgInfo->hwndFocus );
880 else
882 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
883 but the focus has not changed, set the focus where we expect it. */
884 if ( (wndPtr->dwStyle & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
886 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
887 SetFocus( dlgInfo->hwndFocus );
891 if (template.style & WS_VISIBLE && !(wndPtr->dwStyle & WS_VISIBLE))
893 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
895 WIN_ReleaseWndPtr(wndPtr);
896 return hwnd;
898 WIN_ReleaseWndPtr(wndPtr);
899 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
900 if (modal)
901 DIALOG_EnableOwner(owner, ownerEnabled);
902 return 0;
906 /***********************************************************************
907 * CreateDialog16 (USER.89)
909 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
910 HWND16 owner, DLGPROC16 dlgProc )
912 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
916 /***********************************************************************
917 * CreateDialogParam16 (USER.241)
919 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
920 HWND16 owner, DLGPROC16 dlgProc,
921 LPARAM param )
923 HWND16 hwnd = 0;
924 HRSRC16 hRsrc;
925 HGLOBAL16 hmem;
926 LPCVOID data;
928 TRACE("%04x,%s,%04x,%08lx,%ld\n",
929 hInst, debugres_a(dlgTemplate), owner, (DWORD)dlgProc, param );
931 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
932 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
933 if (!(data = LockResource16( hmem ))) hwnd = 0;
934 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
935 dlgProc, param );
936 FreeResource16( hmem );
937 return hwnd;
940 /***********************************************************************
941 * CreateDialogParamA (USER32.73)
943 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
944 HWND owner, DLGPROC dlgProc,
945 LPARAM param )
947 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
948 if (!hrsrc) return 0;
949 return CreateDialogIndirectParamA( hInst,
950 (LPVOID)LoadResource(hInst, hrsrc),
951 owner, dlgProc, param );
955 /***********************************************************************
956 * CreateDialogParamW (USER32.74)
958 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
959 HWND owner, DLGPROC dlgProc,
960 LPARAM param )
962 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
963 if (!hrsrc) return 0;
964 return CreateDialogIndirectParamW( hInst,
965 (LPVOID)LoadResource(hInst, hrsrc),
966 owner, dlgProc, param );
970 /***********************************************************************
971 * CreateDialogIndirect16 (USER.219)
973 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
974 HWND16 owner, DLGPROC16 dlgProc )
976 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
980 /***********************************************************************
981 * CreateDialogIndirectParam16 (USER.242)
983 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
984 LPCVOID dlgTemplate,
985 HWND16 owner, DLGPROC16 dlgProc,
986 LPARAM param )
988 return DIALOG_CreateIndirect( hInst, dlgTemplate, FALSE, owner,
989 dlgProc, param, WIN_PROC_16, FALSE );
993 /***********************************************************************
994 * CreateDialogIndirectParamA (USER32.69)
996 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
997 LPCVOID dlgTemplate,
998 HWND owner, DLGPROC dlgProc,
999 LPARAM param )
1001 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1002 (DLGPROC16)dlgProc, param, WIN_PROC_32A, FALSE );
1005 /***********************************************************************
1006 * CreateDialogIndirectParamAorW (USER32.71)
1008 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
1009 LPCVOID dlgTemplate,
1010 HWND owner, DLGPROC dlgProc,
1011 LPARAM param )
1012 { FIXME("assume WIN_PROC_32W\n");
1013 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1014 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1017 /***********************************************************************
1018 * CreateDialogIndirectParamW (USER32.72)
1020 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1021 LPCVOID dlgTemplate,
1022 HWND owner, DLGPROC dlgProc,
1023 LPARAM param )
1025 return DIALOG_CreateIndirect( hInst, dlgTemplate, TRUE, owner,
1026 (DLGPROC16)dlgProc, param, WIN_PROC_32W, FALSE );
1030 /***********************************************************************
1031 * DIALOG_DoDialogBox
1033 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1035 WND * wndPtr;
1036 DIALOGINFO * dlgInfo;
1037 MSG msg;
1038 INT retval;
1039 HWND ownerMsg = WIN_GetTopParent( owner );
1041 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return -1;
1042 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1044 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1046 ShowWindow( hwnd, SW_SHOW );
1047 while (MSG_InternalGetMessage(QMSG_WIN32A, &msg, hwnd, ownerMsg, MSGF_DIALOGBOX,
1048 PM_REMOVE, !(wndPtr->dwStyle & DS_NOIDLEMSG), NULL ))
1050 if (!IsDialogMessageA( hwnd, &msg))
1052 TranslateMessage( &msg );
1053 DispatchMessageA( &msg );
1055 if (dlgInfo->flags & DF_END) break;
1058 DIALOG_EnableOwner( owner, (dlgInfo->flags & DF_OWNERENABLED) );
1059 retval = dlgInfo->idResult;
1060 WIN_ReleaseWndPtr(wndPtr);
1061 DestroyWindow( hwnd );
1062 return retval;
1066 /***********************************************************************
1067 * DialogBox16 (USER.87)
1069 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1070 HWND16 owner, DLGPROC16 dlgProc )
1072 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1076 /***********************************************************************
1077 * DialogBoxParam16 (USER.239)
1079 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1080 HWND16 owner, DLGPROC16 dlgProc, LPARAM param )
1082 HWND16 hwnd = 0;
1083 HRSRC16 hRsrc;
1084 HGLOBAL16 hmem;
1085 LPCVOID data;
1086 int ret = -1;
1088 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1089 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1090 if (!(data = LockResource16( hmem ))) hwnd = 0;
1091 else hwnd = DIALOG_CreateIndirect( hInst, data, FALSE, owner,
1092 dlgProc, param, WIN_PROC_16, TRUE );
1093 if (hwnd)
1094 ret =(INT16)DIALOG_DoDialogBox( hwnd, owner );
1095 if (data) GlobalUnlock16( hmem );
1096 FreeResource16( hmem );
1097 return ret;
1101 /***********************************************************************
1102 * DialogBoxParamA (USER32.139)
1104 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1105 HWND owner, DLGPROC dlgProc, LPARAM param )
1107 HWND hwnd;
1108 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1109 if (!hrsrc) return 0;
1110 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1111 TRUE, owner,
1112 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1113 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1114 return -1;
1118 /***********************************************************************
1119 * DialogBoxParamW (USER32.140)
1121 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1122 HWND owner, DLGPROC dlgProc, LPARAM param )
1124 HWND hwnd;
1125 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1126 if (!hrsrc) return 0;
1127 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1128 TRUE, owner,
1129 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1130 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1131 return -1;
1135 /***********************************************************************
1136 * DialogBoxIndirect16 (USER.218)
1138 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1139 HWND16 owner, DLGPROC16 dlgProc )
1141 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1145 /***********************************************************************
1146 * DialogBoxIndirectParam16 (USER.240)
1148 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1149 HWND16 owner, DLGPROC16 dlgProc,
1150 LPARAM param )
1152 HWND16 hwnd;
1153 LPCVOID ptr;
1155 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1156 hwnd = DIALOG_CreateIndirect( hInst, ptr, FALSE, owner,
1157 dlgProc, param, WIN_PROC_16, TRUE );
1158 GlobalUnlock16( dlgTemplate );
1159 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1160 return -1;
1164 /***********************************************************************
1165 * DialogBoxIndirectParamA (USER32.136)
1167 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1168 HWND owner, DLGPROC dlgProc,
1169 LPARAM param )
1171 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1172 (DLGPROC16) dlgProc, param, WIN_PROC_32A, TRUE );
1173 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1174 return -1;
1178 /***********************************************************************
1179 * DialogBoxIndirectParamW (USER32.138)
1181 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1182 HWND owner, DLGPROC dlgProc,
1183 LPARAM param )
1185 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1186 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1187 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1188 return -1;
1191 /***********************************************************************
1192 * DialogBoxIndirectParamAorW (USER32.138)
1194 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1195 HWND owner, DLGPROC dlgProc,
1196 LPARAM param, DWORD x )
1198 HWND hwnd;
1199 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1200 hInstance, template, owner, dlgProc, param, x);
1201 hwnd = DIALOG_CreateIndirect( hInstance, template, TRUE, owner,
1202 (DLGPROC16)dlgProc, param, WIN_PROC_32W, TRUE );
1203 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1204 return -1;
1207 /***********************************************************************
1208 * EndDialog16 (USER.88)
1210 BOOL16 WINAPI EndDialog16( HWND16 hwnd, INT16 retval )
1212 return EndDialog( hwnd, retval );
1216 /***********************************************************************
1217 * EndDialog (USER32.173)
1219 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1221 WND * wndPtr = WIN_FindWndPtr( hwnd );
1222 BOOL wasEnabled = TRUE;
1223 DIALOGINFO * dlgInfo;
1225 TRACE("%04x %d\n", hwnd, retval );
1227 if (!wndPtr)
1229 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1230 return FALSE;
1233 if ((dlgInfo = (DIALOGINFO *)wndPtr->wExtra))
1235 dlgInfo->idResult = retval;
1236 dlgInfo->flags |= DF_END;
1237 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1240 if(wndPtr->owner)
1241 DIALOG_EnableOwner( wndPtr->owner->hwndSelf, wasEnabled );
1243 /* Windows sets the focus to the dialog itself in EndDialog */
1245 if (IsChild(hwnd, GetFocus()))
1246 SetFocus(wndPtr->hwndSelf);
1248 /* Don't have to send a ShowWindow(SW_HIDE), just do
1249 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1251 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1252 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1254 WIN_ReleaseWndPtr(wndPtr);
1256 return TRUE;
1260 /***********************************************************************
1261 * DIALOG_IsAccelerator
1263 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM vKey )
1265 HWND hwndControl = hwnd;
1266 HWND hwndNext;
1267 WND *wndPtr;
1268 BOOL RetVal = FALSE;
1269 INT dlgCode;
1273 wndPtr = WIN_FindWndPtr( hwndControl );
1274 if ( (wndPtr != NULL) &&
1275 ((wndPtr->dwStyle & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) )
1277 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1278 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1279 (wndPtr->text!=NULL))
1281 /* find the accelerator key */
1282 LPWSTR p = wndPtr->text - 2;
1283 char a_char = vKey;
1284 WCHAR w_char = 0;
1288 p = strchrW( p + 2, '&' );
1290 while (p != NULL && p[1] == '&');
1292 /* and check if it's the one we're looking for */
1293 MultiByteToWideChar(CP_ACP, 0, &a_char, 1, &w_char, 1);
1294 if (p != NULL && toupperW( p[1] ) == toupperW( w_char ) )
1296 if ((dlgCode & DLGC_STATIC) ||
1297 (wndPtr->dwStyle & 0x0f) == BS_GROUPBOX )
1299 /* set focus to the control */
1300 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1301 hwndControl, 1);
1302 /* and bump it on to next */
1303 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1305 else if (dlgCode & DLGC_BUTTON)
1307 /* send BM_CLICK message to the control */
1308 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1311 RetVal = TRUE;
1312 WIN_ReleaseWndPtr(wndPtr);
1313 break;
1316 hwndNext = GetWindow( hwndControl, GW_CHILD );
1318 else
1320 hwndNext = 0;
1322 WIN_ReleaseWndPtr(wndPtr);
1323 if (!hwndNext)
1325 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1327 while (!hwndNext && hwndControl)
1329 hwndControl = GetParent( hwndControl );
1330 if (hwndControl == hwndDlg)
1332 if(hwnd==hwndDlg){ /* prevent endless loop */
1333 hwndNext=hwnd;
1334 break;
1336 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1338 else
1340 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1343 hwndControl = hwndNext;
1345 while (hwndControl && (hwndControl != hwnd));
1347 return RetVal;
1350 /***********************************************************************
1351 * DIALOG_FindMsgDestination
1353 * The messages that IsDialogMessage sends may not go to the dialog
1354 * calling IsDialogMessage if that dialog is a child, and it has the
1355 * DS_CONTROL style set.
1356 * We propagate up until we hit one that does not have DS_CONTROL, or
1357 * whose parent is not a dialog.
1359 * This is undocumented behaviour.
1361 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1363 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1365 WND *pParent;
1366 HWND hParent = GetParent(hwndDlg);
1367 if (!hParent) break;
1369 pParent = WIN_FindWndPtr(hParent);
1370 if (!pParent) break;
1372 if (!(pParent->flags & WIN_ISDIALOG))
1374 WIN_ReleaseWndPtr(pParent);
1375 break;
1377 WIN_ReleaseWndPtr(pParent);
1379 hwndDlg = hParent;
1382 return hwndDlg;
1385 /***********************************************************************
1386 * DIALOG_IsDialogMessage
1388 static BOOL DIALOG_IsDialogMessage( HWND hwnd, HWND hwndDlg,
1389 UINT message, WPARAM wParam,
1390 LPARAM lParam, BOOL *translate,
1391 BOOL *dispatch, INT dlgCode )
1393 *translate = *dispatch = FALSE;
1395 if (message == WM_PAINT)
1397 /* Apparently, we have to handle this one as well */
1398 *dispatch = TRUE;
1399 return TRUE;
1402 /* Only the key messages get special processing */
1403 if ((message != WM_KEYDOWN) &&
1404 (message != WM_SYSKEYDOWN) &&
1405 (message != WM_SYSCHAR) &&
1406 (message != WM_CHAR))
1407 return FALSE;
1409 if (dlgCode & DLGC_WANTMESSAGE)
1411 *translate = *dispatch = TRUE;
1412 return TRUE;
1415 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1417 switch(message)
1419 case WM_KEYDOWN:
1420 switch(wParam)
1422 case VK_TAB:
1423 if (!(dlgCode & DLGC_WANTTAB))
1425 SendMessageA( hwndDlg, WM_NEXTDLGCTL,
1426 (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1427 return TRUE;
1429 break;
1431 case VK_RIGHT:
1432 case VK_DOWN:
1433 case VK_LEFT:
1434 case VK_UP:
1435 if (!(dlgCode & DLGC_WANTARROWS))
1437 BOOL fPrevious = (wParam == VK_LEFT || wParam == VK_UP);
1438 HWND hwndNext =
1439 GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1440 SendMessageA( hwndDlg, WM_NEXTDLGCTL, hwndNext, 1 );
1441 return TRUE;
1443 break;
1445 case VK_ESCAPE:
1446 SendMessageA( hwndDlg, WM_COMMAND, IDCANCEL,
1447 (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1448 return TRUE;
1450 case VK_RETURN:
1452 DWORD dw = SendMessage16( hwndDlg, DM_GETDEFID, 0, 0 );
1453 if (HIWORD(dw) == DC_HASDEFID)
1455 SendMessageA( hwndDlg, WM_COMMAND,
1456 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1457 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1459 else
1461 SendMessageA( hwndDlg, WM_COMMAND, IDOK,
1462 (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1466 return TRUE;
1468 *translate = TRUE;
1469 break; /* case WM_KEYDOWN */
1471 case WM_CHAR:
1472 if (dlgCode & DLGC_WANTCHARS) break;
1473 /* drop through */
1475 case WM_SYSCHAR:
1476 if (DIALOG_IsAccelerator( hwnd, hwndDlg, wParam ))
1478 /* don't translate or dispatch */
1479 return TRUE;
1481 break;
1483 case WM_SYSKEYDOWN:
1484 *translate = TRUE;
1485 break;
1488 /* If we get here, the message has not been treated specially */
1489 /* and can be sent to its destination window. */
1490 *dispatch = TRUE;
1491 return TRUE;
1495 /***********************************************************************
1496 * IsDialogMessage16 (USER.90)
1498 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, SEGPTR msg16 )
1500 LPMSG16 msg = MapSL(msg16);
1501 BOOL ret, translate, dispatch;
1502 INT dlgCode = 0;
1504 if ((hwndDlg != msg->hwnd) && !IsChild16( hwndDlg, msg->hwnd ))
1505 return FALSE;
1507 if ((msg->message == WM_KEYDOWN) ||
1508 (msg->message == WM_CHAR))
1510 dlgCode = SendMessage16( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg16);
1512 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1513 msg->wParam, msg->lParam,
1514 &translate, &dispatch, dlgCode );
1515 if (translate) TranslateMessage16( msg );
1516 if (dispatch) DispatchMessage16( msg );
1517 return ret;
1521 /***********************************************************************
1522 * IsDialogMessageA (USER32.342)
1524 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG msg )
1526 BOOL ret, translate, dispatch;
1527 INT dlgCode = 0;
1529 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1530 return FALSE;
1532 if ((msg->message == WM_KEYDOWN) ||
1533 (msg->message == WM_CHAR))
1535 dlgCode = SendMessageA( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1537 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1538 msg->wParam, msg->lParam,
1539 &translate, &dispatch, dlgCode );
1540 if (translate) TranslateMessage( msg );
1541 if (dispatch) DispatchMessageA( msg );
1542 return ret;
1546 /***********************************************************************
1547 * IsDialogMessageW (USER32.343)
1549 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1551 BOOL ret, translate, dispatch;
1552 INT dlgCode = 0;
1554 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd ))
1555 return FALSE;
1557 if ((msg->message == WM_KEYDOWN) ||
1558 (msg->message == WM_CHAR))
1560 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, 0, (LPARAM)msg);
1562 ret = DIALOG_IsDialogMessage( msg->hwnd, hwndDlg, msg->message,
1563 msg->wParam, msg->lParam,
1564 &translate, &dispatch, dlgCode );
1565 if (translate) TranslateMessage( msg );
1566 if (dispatch) DispatchMessageW( msg );
1567 return ret;
1571 /***********************************************************************
1572 * GetDlgCtrlID16 (USER.277)
1574 INT16 WINAPI GetDlgCtrlID16( HWND16 hwnd )
1576 WND *wndPtr = WIN_FindWndPtr(hwnd);
1577 INT16 retvalue;
1579 if (!wndPtr) return 0;
1581 retvalue = wndPtr->wIDmenu;
1582 WIN_ReleaseWndPtr(wndPtr);
1583 return retvalue;
1587 /***********************************************************************
1588 * GetDlgCtrlID (USER32.234)
1590 INT WINAPI GetDlgCtrlID( HWND hwnd )
1592 INT retvalue;
1593 WND *wndPtr = WIN_FindWndPtr(hwnd);
1594 if (!wndPtr) return 0;
1595 retvalue = wndPtr->wIDmenu;
1596 WIN_ReleaseWndPtr(wndPtr);
1597 return retvalue;
1601 /***********************************************************************
1602 * GetDlgItem16 (USER.91)
1604 HWND16 WINAPI GetDlgItem16( HWND16 hwndDlg, INT16 id )
1606 WND *pWnd;
1608 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1609 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
1610 if (pWnd->wIDmenu == (UINT16)id)
1612 HWND16 retvalue = pWnd->hwndSelf;
1613 WIN_ReleaseWndPtr(pWnd);
1614 return retvalue;
1616 return 0;
1620 /***********************************************************************
1621 * GetDlgItem (USER32.235)
1623 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1625 WND *pWnd;
1627 if (!(pWnd = WIN_FindWndPtr( hwndDlg ))) return 0;
1628 for (WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd;WIN_UpdateWndPtr(&pWnd,pWnd->next))
1629 if (pWnd->wIDmenu == (UINT16)id)
1631 HWND retvalue = pWnd->hwndSelf;
1632 WIN_ReleaseWndPtr(pWnd);
1633 return retvalue;
1635 return 0;
1639 /*******************************************************************
1640 * SendDlgItemMessage16 (USER.101)
1642 LRESULT WINAPI SendDlgItemMessage16( HWND16 hwnd, INT16 id, UINT16 msg,
1643 WPARAM16 wParam, LPARAM lParam )
1645 HWND16 hwndCtrl = GetDlgItem16( hwnd, id );
1646 if (hwndCtrl) return SendMessage16( hwndCtrl, msg, wParam, lParam );
1647 else return 0;
1651 /*******************************************************************
1652 * SendDlgItemMessageA (USER32.452)
1654 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1655 WPARAM wParam, LPARAM lParam )
1657 HWND hwndCtrl = GetDlgItem( hwnd, id );
1658 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1659 else return 0;
1663 /*******************************************************************
1664 * SendDlgItemMessageW (USER32.453)
1666 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1667 WPARAM wParam, LPARAM lParam )
1669 HWND hwndCtrl = GetDlgItem( hwnd, id );
1670 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1671 else return 0;
1675 /*******************************************************************
1676 * SetDlgItemText16 (USER.92)
1678 void WINAPI SetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR lpString )
1680 SendDlgItemMessage16( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1684 /*******************************************************************
1685 * SetDlgItemTextA (USER32.478)
1687 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1689 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1693 /*******************************************************************
1694 * SetDlgItemTextW (USER32.479)
1696 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1698 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1702 /***********************************************************************
1703 * GetDlgItemText16 (USER.93)
1705 INT16 WINAPI GetDlgItemText16( HWND16 hwnd, INT16 id, SEGPTR str, UINT16 len )
1707 return (INT16)SendDlgItemMessage16( hwnd, id, WM_GETTEXT,
1708 len, (LPARAM)str );
1712 /***********************************************************************
1713 * GetDlgItemTextA (USER32.237)
1715 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1717 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1718 len, (LPARAM)str );
1722 /***********************************************************************
1723 * GetDlgItemTextW (USER32.238)
1725 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1727 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1728 len, (LPARAM)str );
1732 /*******************************************************************
1733 * SetDlgItemInt16 (USER.94)
1735 void WINAPI SetDlgItemInt16( HWND16 hwnd, INT16 id, UINT16 value, BOOL16 fSigned )
1737 SetDlgItemInt( hwnd, (UINT)(UINT16)id, value, fSigned );
1741 /*******************************************************************
1742 * SetDlgItemInt (USER32.477)
1744 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1745 BOOL fSigned )
1747 char str[20];
1749 if (fSigned) sprintf( str, "%d", (INT)value );
1750 else sprintf( str, "%u", value );
1751 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1752 return TRUE;
1756 /***********************************************************************
1757 * GetDlgItemInt16 (USER.95)
1759 UINT16 WINAPI GetDlgItemInt16( HWND16 hwnd, INT16 id, BOOL16 *translated,
1760 BOOL16 fSigned )
1762 UINT result;
1763 BOOL ok;
1765 if (translated) *translated = FALSE;
1766 result = GetDlgItemInt( hwnd, (UINT)(UINT16)id, &ok, fSigned );
1767 if (!ok) return 0;
1768 if (fSigned)
1770 if (((INT)result < -32767) || ((INT)result > 32767)) return 0;
1772 else
1774 if (result > 65535) return 0;
1776 if (translated) *translated = TRUE;
1777 return (UINT16)result;
1781 /***********************************************************************
1782 * GetDlgItemInt (USER32.236)
1784 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1785 BOOL fSigned )
1787 char str[30];
1788 char * endptr;
1789 long result = 0;
1791 if (translated) *translated = FALSE;
1792 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1793 return 0;
1794 if (fSigned)
1796 result = strtol( str, &endptr, 10 );
1797 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1798 return 0;
1799 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1800 return 0;
1802 else
1804 result = strtoul( str, &endptr, 10 );
1805 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1806 return 0;
1807 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1809 if (translated) *translated = TRUE;
1810 return (UINT)result;
1814 /***********************************************************************
1815 * CheckDlgButton16 (USER.97)
1817 BOOL16 WINAPI CheckDlgButton16( HWND16 hwnd, INT16 id, UINT16 check )
1819 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1820 return TRUE;
1824 /***********************************************************************
1825 * CheckDlgButton (USER32.45)
1827 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1829 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1830 return TRUE;
1834 /***********************************************************************
1835 * IsDlgButtonChecked16 (USER.98)
1837 UINT16 WINAPI IsDlgButtonChecked16( HWND16 hwnd, UINT16 id )
1839 return (UINT16)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1843 /***********************************************************************
1844 * IsDlgButtonChecked (USER32.344)
1846 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1848 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1852 /***********************************************************************
1853 * CheckRadioButton16 (USER.96)
1855 BOOL16 WINAPI CheckRadioButton16( HWND16 hwndDlg, UINT16 firstID,
1856 UINT16 lastID, UINT16 checkID )
1858 return CheckRadioButton( hwndDlg, firstID, lastID, checkID );
1862 /***********************************************************************
1863 * CheckRB
1865 * Callback function used to check/uncheck radio buttons that fall
1866 * within a specific range of IDs.
1868 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1870 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1871 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1873 if ((lChildID >= lpRadioGroup->firstID) &&
1874 (lChildID <= lpRadioGroup->lastID))
1876 if (lChildID == lpRadioGroup->checkID)
1878 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1880 else
1882 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1886 return TRUE;
1890 /***********************************************************************
1891 * CheckRadioButton (USER32.48)
1893 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1894 UINT lastID, UINT checkID )
1896 RADIOGROUP radioGroup;
1898 /* perform bounds checking for a radio button group */
1899 radioGroup.firstID = min(min(firstID, lastID), checkID);
1900 radioGroup.lastID = max(max(firstID, lastID), checkID);
1901 radioGroup.checkID = checkID;
1903 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1904 (LPARAM)&radioGroup);
1908 /***********************************************************************
1909 * GetDialogBaseUnits (USER.243) (USER32.233)
1911 DWORD WINAPI GetDialogBaseUnits(void)
1913 return MAKELONG( xBaseUnit, yBaseUnit );
1917 /***********************************************************************
1918 * MapDialogRect16 (USER.103)
1920 void WINAPI MapDialogRect16( HWND16 hwnd, LPRECT16 rect )
1922 DIALOGINFO * dlgInfo;
1923 WND * wndPtr = WIN_FindWndPtr( hwnd );
1924 if (!wndPtr) return;
1925 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1926 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1927 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1928 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1929 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1930 WIN_ReleaseWndPtr(wndPtr);
1934 /***********************************************************************
1935 * MapDialogRect (USER32.382)
1937 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1939 DIALOGINFO * dlgInfo;
1940 WND * wndPtr = WIN_FindWndPtr( hwnd );
1941 if (!wndPtr) return FALSE;
1942 dlgInfo = (DIALOGINFO *)wndPtr->wExtra;
1943 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1944 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1945 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1946 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1947 WIN_ReleaseWndPtr(wndPtr);
1948 return TRUE;
1952 /***********************************************************************
1953 * GetNextDlgGroupItem16 (USER.227)
1955 HWND16 WINAPI GetNextDlgGroupItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
1956 BOOL16 fPrevious )
1958 return (HWND16)GetNextDlgGroupItem( hwndDlg, hwndCtrl, fPrevious );
1962 /***********************************************************************
1963 * GetNextDlgGroupItem (USER32.275)
1965 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl,
1966 BOOL fPrevious )
1968 WND *pWnd = NULL,
1969 *pWndLast = NULL,
1970 *pWndCtrl = NULL,
1971 *pWndDlg = NULL;
1972 HWND retvalue;
1974 if(hwndCtrl)
1976 /* if the hwndCtrl is the child of the control in the hwndDlg,
1977 * then the hwndDlg has to be the parent of the hwndCtrl */
1978 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1979 hwndDlg = GetParent(hwndCtrl);
1982 if (!(pWndDlg = WIN_FindWndPtr( hwndDlg ))) return 0;
1983 if (hwndCtrl)
1985 if (!(pWndCtrl = WIN_FindWndPtr( hwndCtrl )))
1987 retvalue = 0;
1988 goto END;
1990 /* Make sure hwndCtrl is a top-level child */
1991 while ((pWndCtrl->dwStyle & WS_CHILD) && (pWndCtrl->parent != pWndDlg))
1992 WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->parent);
1993 if (pWndCtrl->parent != pWndDlg)
1995 retvalue = 0;
1996 goto END;
1999 else
2001 /* No ctrl specified -> start from the beginning */
2002 if (!(pWndCtrl = WIN_LockWndPtr(pWndDlg->child)))
2004 retvalue = 0;
2005 goto END;
2007 if (fPrevious)
2008 while (pWndCtrl->next) WIN_UpdateWndPtr(&pWndCtrl,pWndCtrl->next);
2011 pWndLast = WIN_LockWndPtr(pWndCtrl);
2012 pWnd = WIN_LockWndPtr(pWndCtrl->next);
2014 while (1)
2016 if (!pWnd || (pWnd->dwStyle & WS_GROUP))
2018 /* Wrap-around to the beginning of the group */
2019 WND *pWndTemp;
2021 WIN_UpdateWndPtr( &pWnd, pWndDlg->child );
2022 for ( pWndTemp = WIN_LockWndPtr( pWnd );
2023 pWndTemp;
2024 WIN_UpdateWndPtr( &pWndTemp, pWndTemp->next) )
2026 if (pWndTemp->dwStyle & WS_GROUP) WIN_UpdateWndPtr( &pWnd, pWndTemp );
2027 if (pWndTemp == pWndCtrl) break;
2029 WIN_ReleaseWndPtr( pWndTemp );
2031 if (pWnd == pWndCtrl) break;
2032 if ((pWnd->dwStyle & WS_VISIBLE) && !(pWnd->dwStyle & WS_DISABLED))
2034 WIN_UpdateWndPtr(&pWndLast,pWnd);
2035 if (!fPrevious) break;
2037 WIN_UpdateWndPtr(&pWnd,pWnd->next);
2039 retvalue = pWndLast->hwndSelf;
2041 WIN_ReleaseWndPtr(pWndLast);
2042 WIN_ReleaseWndPtr(pWnd);
2043 END:
2044 WIN_ReleaseWndPtr(pWndCtrl);
2045 WIN_ReleaseWndPtr(pWndDlg);
2047 return retvalue;
2051 /***********************************************************************
2052 * GetNextDlgTabItem16 (USER.228)
2054 HWND16 WINAPI GetNextDlgTabItem16( HWND16 hwndDlg, HWND16 hwndCtrl,
2055 BOOL16 fPrevious )
2057 return (HWND16)GetNextDlgTabItem( hwndDlg, hwndCtrl, fPrevious );
2060 /***********************************************************************
2061 * DIALOG_GetNextTabItem
2063 * Helper for GetNextDlgTabItem
2065 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
2067 LONG dsStyle;
2068 LONG exStyle;
2069 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
2070 HWND retWnd = 0;
2071 HWND hChildFirst = 0;
2073 if(!hwndCtrl)
2075 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
2076 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
2078 else
2080 HWND hParent = GetParent(hwndCtrl);
2081 BOOL bValid = FALSE;
2082 while( hParent)
2084 if(hParent == hwndMain)
2086 bValid = TRUE;
2087 break;
2089 hParent = GetParent(hParent);
2091 if(bValid)
2093 hChildFirst = GetWindow(hwndCtrl,wndSearch);
2094 if(!hChildFirst)
2096 if(GetParent(hwndCtrl) != hwndMain)
2097 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
2098 else
2100 if(fPrevious)
2101 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
2102 else
2103 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
2108 while(hChildFirst)
2110 BOOL bCtrl = FALSE;
2111 while(hChildFirst)
2113 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
2114 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
2115 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2117 bCtrl=TRUE;
2118 break;
2120 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
2121 break;
2122 hChildFirst = GetWindow(hChildFirst,wndSearch);
2124 if(hChildFirst)
2126 if(bCtrl)
2127 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
2128 else
2129 retWnd = hChildFirst;
2131 if(retWnd) break;
2132 hChildFirst = GetWindow(hChildFirst,wndSearch);
2134 if(!retWnd && hwndCtrl)
2136 HWND hParent = GetParent(hwndCtrl);
2137 while(hParent)
2139 if(hParent == hwndMain) break;
2140 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
2141 if(retWnd) break;
2142 hParent = GetParent(hParent);
2144 if(!retWnd)
2145 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
2147 return retWnd;
2150 /***********************************************************************
2151 * GetNextDlgTabItem (USER32.276)
2153 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
2154 BOOL fPrevious )
2156 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
2159 /**********************************************************************
2160 * DIALOG_DlgDirSelect
2162 * Helper function for DlgDirSelect*
2164 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
2165 INT id, BOOL win32, BOOL unicode,
2166 BOOL combo )
2168 char *buffer, *ptr;
2169 INT item, size;
2170 BOOL ret;
2171 HWND listbox = GetDlgItem( hwnd, id );
2173 TRACE("%04x '%s' %d\n", hwnd, str, id );
2174 if (!listbox) return FALSE;
2175 if (win32)
2177 item = SendMessageA(listbox, combo ? CB_GETCURSEL
2178 : LB_GETCURSEL, 0, 0 );
2179 if (item == LB_ERR) return FALSE;
2180 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN
2181 : LB_GETTEXTLEN, 0, 0 );
2182 if (size == LB_ERR) return FALSE;
2184 else
2186 item = SendMessageA(listbox, combo ? CB_GETCURSEL16
2187 : LB_GETCURSEL16, 0, 0 );
2188 if (item == LB_ERR) return FALSE;
2189 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN16
2190 : LB_GETTEXTLEN16, 0, 0 );
2191 if (size == LB_ERR) return FALSE;
2194 if (!(buffer = SEGPTR_ALLOC( size+1 ))) return FALSE;
2196 if (win32)
2197 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT,
2198 item, (LPARAM)buffer );
2199 else
2200 SendMessage16( listbox, combo ? CB_GETLBTEXT16 : LB_GETTEXT16,
2201 item, (LPARAM)SEGPTR_GET(buffer) );
2203 if ((ret = (buffer[0] == '['))) /* drive or directory */
2205 if (buffer[1] == '-') /* drive */
2207 buffer[3] = ':';
2208 buffer[4] = 0;
2209 ptr = buffer + 2;
2211 else
2213 buffer[strlen(buffer)-1] = '\\';
2214 ptr = buffer + 1;
2217 else ptr = buffer;
2219 if (unicode)
2221 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
2222 ((LPWSTR)str)[len-1] = 0;
2224 else lstrcpynA( str, ptr, len );
2225 SEGPTR_FREE( buffer );
2226 TRACE("Returning %d '%s'\n", ret, str );
2227 return ret;
2231 /**********************************************************************
2232 * DIALOG_DlgDirList
2234 * Helper function for DlgDirList*
2236 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
2237 INT idStatic, UINT attrib, BOOL combo )
2239 HWND hwnd;
2240 LPSTR orig_spec = spec;
2242 #define SENDMSG(msg,wparam,lparam) \
2243 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
2244 : SendMessageA( hwnd, msg, wparam, lparam ))
2246 TRACE("%04x '%s' %d %d %04x\n",
2247 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
2249 /* If the path exists and is a directory, chdir to it */
2250 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
2251 else
2253 char *p, *p2;
2254 p = spec;
2255 if ((p2 = strrchr( p, '\\' ))) p = p2;
2256 if ((p2 = strrchr( p, '/' ))) p = p2;
2257 if (p != spec)
2259 char sep = *p;
2260 *p = 0;
2261 if (!SetCurrentDirectoryA( spec ))
2263 *p = sep; /* Restore the original spec */
2264 return FALSE;
2266 spec = p + 1;
2270 TRACE( "mask=%s\n", spec );
2272 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
2274 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
2275 if (attrib & DDL_DIRECTORY)
2277 if (!(attrib & DDL_EXCLUSIVE))
2279 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2280 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
2281 (LPARAM)spec ) == LB_ERR)
2282 return FALSE;
2284 if (SENDMSG( combo ? CB_DIR : LB_DIR,
2285 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
2286 (LPARAM)"*.*" ) == LB_ERR)
2287 return FALSE;
2289 else
2291 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
2292 (LPARAM)spec ) == LB_ERR)
2293 return FALSE;
2297 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
2299 char temp[MAX_PATH];
2300 GetCurrentDirectoryA( sizeof(temp), temp );
2301 CharLowerA( temp );
2302 /* Can't use PostMessage() here, because the string is on the stack */
2303 SetDlgItemTextA( hDlg, idStatic, temp );
2306 if (orig_spec && (spec != orig_spec))
2308 /* Update the original file spec */
2309 char *p = spec;
2310 while ((*orig_spec++ = *p++));
2313 return TRUE;
2314 #undef SENDMSG
2318 /**********************************************************************
2319 * DIALOG_DlgDirListW
2321 * Helper function for DlgDirList*W
2323 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2324 INT idStatic, UINT attrib, BOOL combo )
2326 if (spec)
2328 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
2329 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
2330 attrib, combo );
2331 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
2332 HeapFree( GetProcessHeap(), 0, specA );
2333 return ret;
2335 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
2339 /**********************************************************************
2340 * DlgDirSelect (USER.99)
2342 BOOL16 WINAPI DlgDirSelect16( HWND16 hwnd, LPSTR str, INT16 id )
2344 return DlgDirSelectEx16( hwnd, str, 128, id );
2348 /**********************************************************************
2349 * DlgDirSelectComboBox (USER.194)
2351 BOOL16 WINAPI DlgDirSelectComboBox16( HWND16 hwnd, LPSTR str, INT16 id )
2353 return DlgDirSelectComboBoxEx16( hwnd, str, 128, id );
2357 /**********************************************************************
2358 * DlgDirSelectEx16 (USER.422)
2360 BOOL16 WINAPI DlgDirSelectEx16( HWND16 hwnd, LPSTR str, INT16 len, INT16 id )
2362 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, FALSE );
2366 /**********************************************************************
2367 * DlgDirSelectExA (USER32.149)
2369 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
2371 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, FALSE );
2375 /**********************************************************************
2376 * DlgDirSelectExW (USER32.150)
2378 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
2380 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, FALSE );
2384 /**********************************************************************
2385 * DlgDirSelectComboBoxEx16 (USER.423)
2387 BOOL16 WINAPI DlgDirSelectComboBoxEx16( HWND16 hwnd, LPSTR str, INT16 len,
2388 INT16 id )
2390 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE, TRUE );
2394 /**********************************************************************
2395 * DlgDirSelectComboBoxExA (USER32.147)
2397 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
2398 INT id )
2400 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE, TRUE );
2404 /**********************************************************************
2405 * DlgDirSelectComboBoxExW (USER32.148)
2407 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2408 INT id)
2410 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE, TRUE );
2414 /**********************************************************************
2415 * DlgDirList16 (USER.100)
2417 INT16 WINAPI DlgDirList16( HWND16 hDlg, LPSTR spec, INT16 idLBox,
2418 INT16 idStatic, UINT16 attrib )
2420 /* according to Win16 docs, DDL_DRIVES should make DDL_EXCLUSIVE
2421 * be set automatically (this is different in Win32, and
2422 * DIALOG_DlgDirList sends Win32 messages to the control,
2423 * so do it here) */
2424 if (attrib & DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
2425 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2429 /**********************************************************************
2430 * DlgDirListA (USER32.143)
2432 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2433 INT idStatic, UINT attrib )
2435 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2439 /**********************************************************************
2440 * DlgDirListW (USER32.146)
2442 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2443 INT idStatic, UINT attrib )
2445 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2449 /**********************************************************************
2450 * DlgDirListComboBox16 (USER.195)
2452 INT16 WINAPI DlgDirListComboBox16( HWND16 hDlg, LPSTR spec, INT16 idCBox,
2453 INT16 idStatic, UINT16 attrib )
2455 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2459 /**********************************************************************
2460 * DlgDirListComboBoxA (USER32.144)
2462 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2463 INT idStatic, UINT attrib )
2465 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2469 /**********************************************************************
2470 * DlgDirListComboBoxW (USER32.145)
2472 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2473 INT idStatic, UINT attrib )
2475 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );