Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / windows / dialog.c
blob60afc545568bd5ee490c78e0bd84930811bb0878
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "windef.h"
32 #include "winnls.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "windowsx.h"
37 #include "wine/winuser16.h"
38 #include "wine/winbase16.h"
39 #include "wine/unicode.h"
40 #include "controls.h"
41 #include "heap.h"
42 #include "win.h"
43 #include "user.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
49 /* Dialog control information */
50 typedef struct
52 DWORD style;
53 DWORD exStyle;
54 DWORD helpId;
55 INT16 x;
56 INT16 y;
57 INT16 cx;
58 INT16 cy;
59 UINT id;
60 LPCSTR className;
61 LPCSTR windowName;
62 LPCVOID data;
63 } DLG_CONTROL_INFO;
65 /* Dialog template */
66 typedef struct
68 DWORD style;
69 DWORD exStyle;
70 DWORD helpId;
71 UINT16 nbItems;
72 INT16 x;
73 INT16 y;
74 INT16 cx;
75 INT16 cy;
76 LPCSTR menuName;
77 LPCSTR className;
78 LPCSTR caption;
79 WORD pointSize;
80 WORD weight;
81 BOOL italic;
82 LPCSTR faceName;
83 BOOL dialogEx;
84 } DLG_TEMPLATE;
86 /* Radio button group */
87 typedef struct
89 UINT firstID;
90 UINT lastID;
91 UINT checkID;
92 } RADIOGROUP;
94 /* Dialog base units */
95 static WORD xBaseUnit = 0, yBaseUnit = 0;
98 /*********************************************************************
99 * dialog class descriptor
101 const struct builtin_class_descr DIALOG_builtin_class =
103 DIALOG_CLASS_ATOM, /* name */
104 CS_GLOBALCLASS | CS_SAVEBITS, /* style */
105 DefDlgProcA, /* procA */
106 DefDlgProcW, /* procW */
107 DLGWINDOWEXTRA, /* extra */
108 IDC_ARROWA, /* cursor */
109 0 /* brush */
113 /***********************************************************************
114 * DIALOG_EnableOwner
116 * Helper function for modal dialogs to enable again the
117 * owner of the dialog box.
119 void DIALOG_EnableOwner( HWND hOwner )
121 /* Owner must be a top-level window */
122 if (hOwner)
123 hOwner = GetAncestor( hOwner, GA_ROOT );
124 if (!hOwner) return;
125 EnableWindow( hOwner, TRUE );
129 /***********************************************************************
130 * DIALOG_DisableOwner
132 * Helper function for modal dialogs to disable the
133 * owner of the dialog box. Returns TRUE if owner was enabled.
135 BOOL DIALOG_DisableOwner( HWND hOwner )
137 /* Owner must be a top-level window */
138 if (hOwner)
139 hOwner = GetAncestor( hOwner, GA_ROOT );
140 if (!hOwner) return FALSE;
141 if (IsWindowEnabled( hOwner ))
143 EnableWindow( hOwner, FALSE );
144 return TRUE;
146 else
147 return FALSE;
150 /***********************************************************************
151 * DIALOG_GetCharSizeFromDC
153 * Despite most of MSDN insisting that the horizontal base unit is
154 * tmAveCharWidth it isn't. Knowledge base article Q145994
155 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
156 * says that we should take the average of the 52 English upper and lower
157 * case characters.
159 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
161 HFONT hFontPrev = 0;
162 char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
163 SIZE sz;
164 TEXTMETRICA tm;
166 pSize->cx = xBaseUnit;
167 pSize->cy = yBaseUnit;
169 if(!hDC) return FALSE;
171 if(hFont) hFontPrev = SelectFont(hDC, hFont);
172 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
173 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
175 pSize->cy = tm.tmHeight;
176 pSize->cx = (sz.cx / 26 + 1) / 2;
178 if (hFontPrev) SelectFont(hDC, hFontPrev);
180 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
181 return TRUE;
184 /***********************************************************************
185 * DIALOG_GetCharSize
187 * A convenient variant of DIALOG_GetCharSizeFromDC.
189 static BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
191 HDC hDC = GetDC(0);
192 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
193 ReleaseDC(0, hDC);
194 return Success;
197 /***********************************************************************
198 * DIALOG_Init
200 * Initialisation of the dialog manager.
202 BOOL DIALOG_Init(void)
204 HDC hdc;
205 SIZE size;
207 /* Calculate the dialog base units */
209 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
211 ERR("Could not create Display DC\n");
212 return FALSE;
215 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
217 DeleteDC( hdc );
218 ERR("Could not initialize base dialog units\n");
219 return FALSE;
222 DeleteDC( hdc );
223 xBaseUnit = size.cx;
224 yBaseUnit = size.cy;
226 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
227 return TRUE;
231 /***********************************************************************
232 * DIALOG_GetControl16
234 * Return the class and text of the control pointed to by ptr,
235 * fill the header structure and return a pointer to the next control.
237 static LPCSTR DIALOG_GetControl16( LPCSTR p, DLG_CONTROL_INFO *info )
239 static char buffer[10];
240 int int_id;
242 info->x = GET_WORD(p); p += sizeof(WORD);
243 info->y = GET_WORD(p); p += sizeof(WORD);
244 info->cx = GET_WORD(p); p += sizeof(WORD);
245 info->cy = GET_WORD(p); p += sizeof(WORD);
246 info->id = GET_WORD(p); p += sizeof(WORD);
247 info->style = GET_DWORD(p); p += sizeof(DWORD);
248 info->exStyle = 0;
250 if (*p & 0x80)
252 switch((BYTE)*p)
254 case 0x80: strcpy( buffer, "BUTTON" ); break;
255 case 0x81: strcpy( buffer, "EDIT" ); break;
256 case 0x82: strcpy( buffer, "STATIC" ); break;
257 case 0x83: strcpy( buffer, "LISTBOX" ); break;
258 case 0x84: strcpy( buffer, "SCROLLBAR" ); break;
259 case 0x85: strcpy( buffer, "COMBOBOX" ); break;
260 default: buffer[0] = '\0'; break;
262 info->className = buffer;
263 p++;
265 else
267 info->className = p;
268 p += strlen(p) + 1;
271 int_id = ((BYTE)*p == 0xff);
272 if (int_id)
274 /* Integer id, not documented (?). Only works for SS_ICON controls */
275 info->windowName = (LPCSTR)(UINT)GET_WORD(p+1);
276 p += 3;
278 else
280 info->windowName = p;
281 p += strlen(p) + 1;
284 if (*p) info->data = p + 1;
285 else info->data = NULL;
287 p += *p + 1;
289 if(int_id)
290 TRACE(" %s %04x %d, %d, %d, %d, %d, %08lx, %p\n",
291 info->className, LOWORD(info->windowName),
292 info->id, info->x, info->y, info->cx, info->cy,
293 info->style, info->data );
294 else
295 TRACE(" %s '%s' %d, %d, %d, %d, %d, %08lx, %p\n",
296 info->className, info->windowName,
297 info->id, info->x, info->y, info->cx, info->cy,
298 info->style, info->data );
300 return p;
304 /***********************************************************************
305 * DIALOG_GetControl32
307 * Return the class and text of the control pointed to by ptr,
308 * fill the header structure and return a pointer to the next control.
310 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
311 BOOL dialogEx )
313 if (dialogEx)
315 info->helpId = GET_DWORD(p); p += 2;
316 info->exStyle = GET_DWORD(p); p += 2;
317 info->style = GET_DWORD(p); p += 2;
319 else
321 info->helpId = 0;
322 info->style = GET_DWORD(p); p += 2;
323 info->exStyle = GET_DWORD(p); p += 2;
325 info->x = GET_WORD(p); p++;
326 info->y = GET_WORD(p); p++;
327 info->cx = GET_WORD(p); p++;
328 info->cy = GET_WORD(p); p++;
330 if (dialogEx)
332 /* id is a DWORD for DIALOGEX */
333 info->id = GET_DWORD(p);
334 p += 2;
336 else
338 info->id = GET_WORD(p);
339 p++;
342 if (GET_WORD(p) == 0xffff)
344 static const WCHAR class_names[6][10] =
346 { 'B','u','t','t','o','n', }, /* 0x80 */
347 { 'E','d','i','t', }, /* 0x81 */
348 { 'S','t','a','t','i','c', }, /* 0x82 */
349 { 'L','i','s','t','B','o','x', }, /* 0x83 */
350 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
351 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
353 WORD id = GET_WORD(p+1);
354 if ((id >= 0x80) && (id <= 0x85))
355 info->className = (LPCSTR)class_names[id - 0x80];
356 else
358 info->className = NULL;
359 ERR("Unknown built-in class id %04x\n", id );
361 p += 2;
363 else
365 info->className = (LPCSTR)p;
366 p += strlenW( (LPCWSTR)p ) + 1;
369 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
371 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
372 p += 2;
374 else
376 info->windowName = (LPCSTR)p;
377 p += strlenW( (LPCWSTR)p ) + 1;
380 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
381 debugstr_w( (LPCWSTR)info->className ),
382 debugstr_w( (LPCWSTR)info->windowName ),
383 info->id, info->x, info->y, info->cx, info->cy,
384 info->style, info->exStyle, info->helpId );
386 if (GET_WORD(p))
388 if (TRACE_ON(dialog))
390 WORD i, count = GET_WORD(p) / sizeof(WORD);
391 TRACE(" BEGIN\n");
392 TRACE(" ");
393 for (i = 0; i < count; i++) DPRINTF( "%04x,", GET_WORD(p+i+1) );
394 DPRINTF("\n");
395 TRACE(" END\n" );
397 info->data = p + 1;
398 p += GET_WORD(p) / sizeof(WORD);
400 else info->data = NULL;
401 p++;
403 /* Next control is on dword boundary */
404 return (const WORD *)((((int)p) + 3) & ~3);
408 /***********************************************************************
409 * DIALOG_CreateControls
411 * Create the control windows for a dialog.
413 static BOOL DIALOG_CreateControls( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
414 HINSTANCE hInst, BOOL win32 )
416 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
417 DLG_CONTROL_INFO info;
418 HWND hwndCtrl, hwndDefButton = 0;
419 INT items = dlgTemplate->nbItems;
421 TRACE(" BEGIN\n" );
422 while (items--)
424 if (!win32)
426 HINSTANCE16 instance;
427 SEGPTR segptr;
429 template = DIALOG_GetControl16( template, &info );
430 if (HIWORD(info.className) && !strcmp( info.className, "EDIT") &&
431 !(GetWindowLongW( hwnd, GWL_STYLE ) & DS_LOCALEDIT))
433 if (!dlgInfo->hDialogHeap)
435 dlgInfo->hDialogHeap = GlobalAlloc16(GMEM_FIXED, 0x10000);
436 if (!dlgInfo->hDialogHeap)
438 ERR("Insufficient memory to create heap for edit control\n" );
439 continue;
441 LocalInit16(dlgInfo->hDialogHeap, 0, 0xffff);
443 instance = dlgInfo->hDialogHeap;
445 else instance = (HINSTANCE16)hInst;
447 segptr = MapLS( info.data );
448 hwndCtrl = WIN_Handle32( CreateWindowEx16( info.exStyle | WS_EX_NOPARENTNOTIFY,
449 info.className, info.windowName,
450 info.style | WS_CHILD,
451 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
452 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
453 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
454 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
455 HWND_16(hwnd), (HMENU16)info.id,
456 instance, (LPVOID)segptr ));
457 UnMapLS( segptr );
459 else
461 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
462 dlgTemplate->dialogEx );
463 /* Is this it? */
464 if (info.style & WS_BORDER)
466 info.style &= ~WS_BORDER;
467 info.exStyle |= WS_EX_CLIENTEDGE;
469 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
470 (LPCWSTR)info.className,
471 (LPCWSTR)info.windowName,
472 info.style | WS_CHILD,
473 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
474 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
475 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
476 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
477 hwnd, (HMENU)info.id,
478 hInst, (LPVOID)info.data );
480 if (!hwndCtrl) return FALSE;
482 /* Send initialisation messages to the control */
483 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
484 (WPARAM)dlgInfo->hUserFont, 0 );
485 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
487 /* If there's already a default push-button, set it back */
488 /* to normal and use this one instead. */
489 if (hwndDefButton)
490 SendMessageA( hwndDefButton, BM_SETSTYLE,
491 BS_PUSHBUTTON,FALSE );
492 hwndDefButton = hwndCtrl;
493 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
496 TRACE(" END\n" );
497 return TRUE;
501 /***********************************************************************
502 * DIALOG_ParseTemplate16
504 * Fill a DLG_TEMPLATE structure from the dialog template, and return
505 * a pointer to the first control.
507 static LPCSTR DIALOG_ParseTemplate16( LPCSTR p, DLG_TEMPLATE * result )
509 result->style = GET_DWORD(p); p += sizeof(DWORD);
510 result->exStyle = 0;
511 result->nbItems = (unsigned char) *p++;
512 result->x = GET_WORD(p); p += sizeof(WORD);
513 result->y = GET_WORD(p); p += sizeof(WORD);
514 result->cx = GET_WORD(p); p += sizeof(WORD);
515 result->cy = GET_WORD(p); p += sizeof(WORD);
516 TRACE("DIALOG %d, %d, %d, %d\n",
517 result->x, result->y, result->cx, result->cy );
518 TRACE(" STYLE %08lx\n", result->style );
520 /* Get the menu name */
522 switch( (BYTE)*p )
524 case 0:
525 result->menuName = 0;
526 p++;
527 break;
528 case 0xff:
529 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
530 p += 3;
531 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
532 break;
533 default:
534 result->menuName = p;
535 TRACE(" MENU '%s'\n", p );
536 p += strlen(p) + 1;
537 break;
540 /* Get the class name */
542 if (*p)
544 result->className = p;
545 TRACE(" CLASS '%s'\n", result->className );
547 else result->className = DIALOG_CLASS_ATOM;
548 p += strlen(p) + 1;
550 /* Get the window caption */
552 result->caption = p;
553 p += strlen(p) + 1;
554 TRACE(" CAPTION '%s'\n", result->caption );
556 /* Get the font name */
558 if (result->style & DS_SETFONT)
560 result->pointSize = GET_WORD(p);
561 p += sizeof(WORD);
562 result->faceName = p;
563 p += strlen(p) + 1;
564 TRACE(" FONT %d,'%s'\n",
565 result->pointSize, result->faceName );
567 return p;
571 /***********************************************************************
572 * DIALOG_ParseTemplate32
574 * Fill a DLG_TEMPLATE structure from the dialog template, and return
575 * a pointer to the first control.
577 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
579 const WORD *p = (const WORD *)template;
581 result->style = GET_DWORD(p); p += 2;
582 if (result->style == 0xffff0001) /* DIALOGEX resource */
584 result->dialogEx = TRUE;
585 result->helpId = GET_DWORD(p); p += 2;
586 result->exStyle = GET_DWORD(p); p += 2;
587 result->style = GET_DWORD(p); p += 2;
589 else
591 result->dialogEx = FALSE;
592 result->helpId = 0;
593 result->exStyle = GET_DWORD(p); p += 2;
595 result->nbItems = GET_WORD(p); p++;
596 result->x = GET_WORD(p); p++;
597 result->y = GET_WORD(p); p++;
598 result->cx = GET_WORD(p); p++;
599 result->cy = GET_WORD(p); p++;
600 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
601 result->dialogEx ? "EX" : "", result->x, result->y,
602 result->cx, result->cy, result->helpId );
603 TRACE(" STYLE 0x%08lx\n", result->style );
604 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
606 /* Get the menu name */
608 switch(GET_WORD(p))
610 case 0x0000:
611 result->menuName = NULL;
612 p++;
613 break;
614 case 0xffff:
615 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
616 p += 2;
617 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
618 break;
619 default:
620 result->menuName = (LPCSTR)p;
621 TRACE(" MENU %s\n", debugstr_w( (LPCWSTR)p ));
622 p += strlenW( (LPCWSTR)p ) + 1;
623 break;
626 /* Get the class name */
628 switch(GET_WORD(p))
630 case 0x0000:
631 result->className = DIALOG_CLASS_ATOM;
632 p++;
633 break;
634 case 0xffff:
635 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
636 p += 2;
637 TRACE(" CLASS %04x\n", LOWORD(result->className) );
638 break;
639 default:
640 result->className = (LPCSTR)p;
641 TRACE(" CLASS %s\n", debugstr_w( (LPCWSTR)p ));
642 p += strlenW( (LPCWSTR)p ) + 1;
643 break;
646 /* Get the window caption */
648 result->caption = (LPCSTR)p;
649 p += strlenW( (LPCWSTR)p ) + 1;
650 TRACE(" CAPTION %s\n", debugstr_w( (LPCWSTR)result->caption ) );
652 /* Get the font name */
654 if (result->style & DS_SETFONT)
656 result->pointSize = GET_WORD(p);
657 p++;
658 if (result->dialogEx)
660 result->weight = GET_WORD(p); p++;
661 result->italic = LOBYTE(GET_WORD(p)); p++;
663 else
665 result->weight = FW_DONTCARE;
666 result->italic = FALSE;
668 result->faceName = (LPCSTR)p;
669 p += strlenW( (LPCWSTR)p ) + 1;
670 TRACE(" FONT %d, %s, %d, %s\n",
671 result->pointSize, debugstr_w( (LPCWSTR)result->faceName ),
672 result->weight, result->italic ? "TRUE" : "FALSE" );
675 /* First control is on dword boundary */
676 return (LPCSTR)((((int)p) + 3) & ~3);
680 /***********************************************************************
681 * DIALOG_CreateIndirect
682 * Creates a dialog box window
684 * modal = TRUE if we are called from a modal dialog box.
685 * (it's more compatible to do it here, as under Windows the owner
686 * is never disabled if the dialog fails because of an invalid template)
688 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCSTR dlgTemplate,
689 HWND owner, DLGPROC dlgProc, LPARAM param,
690 WINDOWPROCTYPE procType, BOOL modal )
692 HWND hwnd;
693 RECT rect;
694 WND * wndPtr;
695 DLG_TEMPLATE template;
696 DIALOGINFO * dlgInfo;
697 BOOL ownerEnabled = TRUE;
698 BOOL win32Template = (procType != WIN_PROC_16);
700 /* Parse dialog template */
702 if (!dlgTemplate) return 0;
703 if (win32Template)
704 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
705 else
706 dlgTemplate = DIALOG_ParseTemplate16( dlgTemplate, &template );
708 /* Initialise dialog extra data */
710 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
711 dlgInfo->hwndFocus = 0;
712 dlgInfo->hUserFont = 0;
713 dlgInfo->hMenu = 0;
714 dlgInfo->xBaseUnit = xBaseUnit;
715 dlgInfo->yBaseUnit = yBaseUnit;
716 dlgInfo->idResult = 0;
717 dlgInfo->flags = 0;
718 dlgInfo->hDialogHeap = 0;
720 /* Load menu */
722 if (template.menuName)
724 if (!win32Template) dlgInfo->hMenu = LoadMenu16( hInst, template.menuName );
725 else dlgInfo->hMenu = LoadMenuW( hInst, (LPCWSTR)template.menuName );
728 /* Create custom font if needed */
730 if (template.style & DS_SETFONT)
732 /* We convert the size to pixels and then make it -ve. This works
733 * for both +ve and -ve template.pointSize */
734 HDC dc;
735 int pixels;
736 dc = GetDC(0);
737 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
738 ReleaseDC(0, dc);
739 if (win32Template)
740 dlgInfo->hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
741 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
742 PROOF_QUALITY, FF_DONTCARE,
743 (LPCWSTR)template.faceName );
744 else
745 dlgInfo->hUserFont = CreateFontA( -pixels, 0, 0, 0, FW_DONTCARE,
746 FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
747 PROOF_QUALITY, FF_DONTCARE, template.faceName );
748 if (dlgInfo->hUserFont)
750 SIZE charSize;
751 if (DIALOG_GetCharSize( dlgInfo->hUserFont, &charSize ))
753 dlgInfo->xBaseUnit = charSize.cx;
754 dlgInfo->yBaseUnit = charSize.cy;
757 TRACE("units = %d,%d\n", dlgInfo->xBaseUnit, dlgInfo->yBaseUnit );
760 /* Create dialog main window */
762 rect.left = rect.top = 0;
763 rect.right = MulDiv(template.cx, dlgInfo->xBaseUnit, 4);
764 rect.bottom = MulDiv(template.cy, dlgInfo->yBaseUnit, 8);
765 if (template.style & DS_MODALFRAME)
766 template.exStyle |= WS_EX_DLGMODALFRAME;
767 AdjustWindowRectEx( &rect, template.style, (dlgInfo->hMenu != 0), template.exStyle );
768 rect.right -= rect.left;
769 rect.bottom -= rect.top;
771 if ((INT16)template.x == CW_USEDEFAULT16)
773 rect.left = rect.top = win32Template? CW_USEDEFAULT : CW_USEDEFAULT16;
775 else
777 if (template.style & DS_CENTER)
779 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
780 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
782 else
784 rect.left += MulDiv(template.x, dlgInfo->xBaseUnit, 4);
785 rect.top += MulDiv(template.y, dlgInfo->yBaseUnit, 8);
787 if ( !(template.style & WS_CHILD) )
789 INT16 dX, dY;
791 if( !(template.style & DS_ABSALIGN) )
792 ClientToScreen( owner, (POINT *)&rect );
794 /* try to fit it into the desktop */
796 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
797 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
798 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
799 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
800 if( rect.left < 0 ) rect.left = 0;
801 if( rect.top < 0 ) rect.top = 0;
805 if (modal)
807 ownerEnabled = DIALOG_DisableOwner( owner );
808 if (ownerEnabled) dlgInfo->flags |= DF_OWNERENABLED;
811 if (!win32Template)
812 hwnd = WIN_Handle32( CreateWindowEx16(template.exStyle, template.className,
813 template.caption, template.style & ~WS_VISIBLE,
814 rect.left, rect.top, rect.right, rect.bottom,
815 HWND_16(owner), dlgInfo->hMenu, hInst, NULL ));
816 else
817 hwnd = CreateWindowExW(template.exStyle, (LPCWSTR)template.className,
818 (LPCWSTR)template.caption,
819 template.style & ~WS_VISIBLE,
820 rect.left, rect.top, rect.right, rect.bottom,
821 owner, dlgInfo->hMenu, hInst, NULL );
823 if (!hwnd)
825 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
826 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
827 if (modal && (dlgInfo->flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
828 HeapFree( GetProcessHeap(), 0, dlgInfo );
829 return 0;
831 wndPtr = WIN_GetPtr( hwnd );
832 wndPtr->flags |= WIN_ISDIALOG;
833 WIN_ReleasePtr( wndPtr );
835 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
836 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
837 switch(procType)
839 case WIN_PROC_16: SetWindowLong16( HWND_16(hwnd), DWL_DLGPROC, (LONG)dlgProc ); break;
840 case WIN_PROC_32A: SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc ); break;
841 case WIN_PROC_32W: SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc ); break;
842 default: break;
845 if (dlgInfo->hUserFont)
846 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
848 /* Create controls */
850 if (DIALOG_CreateControls( hwnd, dlgTemplate, &template,
851 hInst, win32Template ))
853 HWND hwndPreInitFocus;
855 /* Send initialisation messages and set focus */
857 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
859 hwndPreInitFocus = GetFocus();
860 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
862 /* check where the focus is again,
863 * some controls status might have changed in WM_INITDIALOG */
864 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
865 if( dlgInfo->hwndFocus )
866 SetFocus( dlgInfo->hwndFocus );
868 else
870 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
871 but the focus has not changed, set the focus where we expect it. */
872 if ((GetFocus() == hwndPreInitFocus) &&
873 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
875 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
876 if( dlgInfo->hwndFocus )
877 SetFocus( dlgInfo->hwndFocus );
881 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
883 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
885 return hwnd;
887 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
888 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
889 return 0;
893 /***********************************************************************
894 * CreateDialog (USER.89)
896 HWND16 WINAPI CreateDialog16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
897 HWND16 owner, DLGPROC16 dlgProc )
899 return CreateDialogParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
903 /***********************************************************************
904 * CreateDialogParam (USER.241)
906 HWND16 WINAPI CreateDialogParam16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
907 HWND16 owner, DLGPROC16 dlgProc,
908 LPARAM param )
910 HWND16 hwnd = 0;
911 HRSRC16 hRsrc;
912 HGLOBAL16 hmem;
913 LPCVOID data;
915 TRACE("%04x,%s,%04x,%08lx,%ld\n",
916 hInst, debugstr_a(dlgTemplate), owner, (DWORD)dlgProc, param );
918 if (!(hRsrc = FindResource16( hInst, dlgTemplate, RT_DIALOGA ))) return 0;
919 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
920 if (!(data = LockResource16( hmem ))) hwnd = 0;
921 else hwnd = CreateDialogIndirectParam16( hInst, data, owner,
922 dlgProc, param );
923 FreeResource16( hmem );
924 return hwnd;
927 /***********************************************************************
928 * CreateDialogParamA (USER32.@)
930 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name,
931 HWND owner, DLGPROC dlgProc,
932 LPARAM param )
934 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
935 if (!hrsrc) return 0;
936 return CreateDialogIndirectParamA( hInst,
937 (LPVOID)LoadResource(hInst, hrsrc),
938 owner, dlgProc, param );
942 /***********************************************************************
943 * CreateDialogParamW (USER32.@)
945 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name,
946 HWND owner, DLGPROC dlgProc,
947 LPARAM param )
949 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
950 if (!hrsrc) return 0;
951 return CreateDialogIndirectParamW( hInst,
952 (LPVOID)LoadResource(hInst, hrsrc),
953 owner, dlgProc, param );
957 /***********************************************************************
958 * CreateDialogIndirect (USER.219)
960 HWND16 WINAPI CreateDialogIndirect16( HINSTANCE16 hInst, LPCVOID dlgTemplate,
961 HWND16 owner, DLGPROC16 dlgProc )
963 return CreateDialogIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0);
967 /***********************************************************************
968 * CreateDialogIndirectParam (USER.242)
969 * CreateDialogIndirectParam16 (USER32.@)
971 HWND16 WINAPI CreateDialogIndirectParam16( HINSTANCE16 hInst,
972 LPCVOID dlgTemplate,
973 HWND16 owner, DLGPROC16 dlgProc,
974 LPARAM param )
976 return HWND_16( DIALOG_CreateIndirect( hInst, dlgTemplate, WIN_Handle32(owner),
977 (DLGPROC)dlgProc, param, WIN_PROC_16, FALSE ));
981 /***********************************************************************
982 * CreateDialogIndirectParamA (USER32.@)
984 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst,
985 LPCVOID dlgTemplate,
986 HWND owner, DLGPROC dlgProc,
987 LPARAM param )
989 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32A, FALSE );
992 /***********************************************************************
993 * CreateDialogIndirectParamAorW (USER32.@)
995 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst,
996 LPCVOID dlgTemplate,
997 HWND owner, DLGPROC dlgProc,
998 LPARAM param )
999 { FIXME("assume WIN_PROC_32W\n");
1000 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1003 /***********************************************************************
1004 * CreateDialogIndirectParamW (USER32.@)
1006 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst,
1007 LPCVOID dlgTemplate,
1008 HWND owner, DLGPROC dlgProc,
1009 LPARAM param )
1011 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, WIN_PROC_32W, FALSE );
1015 /***********************************************************************
1016 * DIALOG_DoDialogBox
1018 static INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
1020 DIALOGINFO * dlgInfo;
1021 MSG msg;
1022 INT retval;
1023 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
1025 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
1027 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
1029 ShowWindow( hwnd, SW_SHOW );
1030 for (;;)
1032 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
1034 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1036 /* No message present -> send ENTERIDLE and wait */
1037 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
1038 if (!GetMessageW( &msg, 0, 0, 0 )) break;
1041 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
1043 if (!IsWindow( hwnd )) return -1;
1044 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
1046 TranslateMessage( &msg );
1047 DispatchMessageW( &msg );
1049 if (dlgInfo->flags & DF_END) break;
1052 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
1053 retval = dlgInfo->idResult;
1054 DestroyWindow( hwnd );
1055 return retval;
1059 /***********************************************************************
1060 * DialogBox (USER.87)
1062 INT16 WINAPI DialogBox16( HINSTANCE16 hInst, LPCSTR dlgTemplate,
1063 HWND16 owner, DLGPROC16 dlgProc )
1065 return DialogBoxParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1069 /***********************************************************************
1070 * DialogBoxParam (USER.239)
1072 INT16 WINAPI DialogBoxParam16( HINSTANCE16 hInst, LPCSTR template,
1073 HWND16 owner16, DLGPROC16 dlgProc, LPARAM param )
1075 HWND hwnd = 0;
1076 HRSRC16 hRsrc;
1077 HGLOBAL16 hmem;
1078 LPCVOID data;
1079 int ret = -1;
1081 if (!(hRsrc = FindResource16( hInst, template, RT_DIALOGA ))) return 0;
1082 if (!(hmem = LoadResource16( hInst, hRsrc ))) return 0;
1083 if ((data = LockResource16( hmem )))
1085 HWND owner = WIN_Handle32(owner16);
1086 hwnd = DIALOG_CreateIndirect( hInst, data, owner,
1087 (DLGPROC)dlgProc, param, WIN_PROC_16, TRUE );
1088 if (hwnd) ret = DIALOG_DoDialogBox( hwnd, owner );
1089 GlobalUnlock16( hmem );
1091 FreeResource16( hmem );
1092 return ret;
1096 /***********************************************************************
1097 * DialogBoxParamA (USER32.@)
1099 INT WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
1100 HWND owner, DLGPROC dlgProc, LPARAM param )
1102 HWND hwnd;
1103 HANDLE hrsrc = FindResourceA( hInst, name, RT_DIALOGA );
1104 if (!hrsrc) return 0;
1105 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1106 owner, dlgProc, param, WIN_PROC_32A, TRUE );
1107 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1108 return -1;
1112 /***********************************************************************
1113 * DialogBoxParamW (USER32.@)
1115 INT WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
1116 HWND owner, DLGPROC dlgProc, LPARAM param )
1118 HWND hwnd;
1119 HANDLE hrsrc = FindResourceW( hInst, name, RT_DIALOGW );
1120 if (!hrsrc) return 0;
1121 hwnd = DIALOG_CreateIndirect( hInst, (LPVOID)LoadResource(hInst, hrsrc),
1122 owner, dlgProc, param, WIN_PROC_32W, TRUE );
1123 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1124 return -1;
1128 /***********************************************************************
1129 * DialogBoxIndirect (USER.218)
1131 INT16 WINAPI DialogBoxIndirect16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1132 HWND16 owner, DLGPROC16 dlgProc )
1134 return DialogBoxIndirectParam16( hInst, dlgTemplate, owner, dlgProc, 0 );
1138 /***********************************************************************
1139 * DialogBoxIndirectParam (USER.240)
1140 * DialogBoxIndirectParam16 (USER32.@)
1142 INT16 WINAPI DialogBoxIndirectParam16( HINSTANCE16 hInst, HANDLE16 dlgTemplate,
1143 HWND16 owner16, DLGPROC16 dlgProc,
1144 LPARAM param )
1146 HWND hwnd, owner = WIN_Handle32( owner16 );
1147 LPCVOID ptr;
1149 if (!(ptr = GlobalLock16( dlgTemplate ))) return -1;
1150 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, (DLGPROC)dlgProc,
1151 param, WIN_PROC_16, TRUE );
1152 GlobalUnlock16( dlgTemplate );
1153 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1154 return -1;
1158 /***********************************************************************
1159 * DialogBoxIndirectParamA (USER32.@)
1161 INT WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCVOID template,
1162 HWND owner, DLGPROC dlgProc,
1163 LPARAM param )
1165 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1166 dlgProc, param, WIN_PROC_32A, TRUE );
1167 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1168 return -1;
1172 /***********************************************************************
1173 * DialogBoxIndirectParamW (USER32.@)
1175 INT WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCVOID template,
1176 HWND owner, DLGPROC dlgProc,
1177 LPARAM param )
1179 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner,
1180 dlgProc, param, WIN_PROC_32W, TRUE );
1181 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1182 return -1;
1185 /***********************************************************************
1186 * DialogBoxIndirectParamAorW (USER32.@)
1188 INT WINAPI DialogBoxIndirectParamAorW(HINSTANCE hInstance, LPCVOID template,
1189 HWND owner, DLGPROC dlgProc,
1190 LPARAM param, DWORD x )
1192 HWND hwnd;
1193 FIXME("0x%08x %p 0x%08x %p 0x%08lx 0x%08lx\n",
1194 hInstance, template, owner, dlgProc, param, x);
1195 hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, WIN_PROC_32W, TRUE );
1196 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
1197 return -1;
1200 /***********************************************************************
1201 * EndDialog (USER32.@)
1203 BOOL WINAPI EndDialog( HWND hwnd, INT retval )
1205 BOOL wasEnabled = TRUE;
1206 DIALOGINFO * dlgInfo;
1207 HWND owner;
1209 TRACE("%04x %d\n", hwnd, retval );
1211 if (!(dlgInfo = DIALOG_get_info( hwnd )))
1213 ERR("got invalid window handle (%04x); buggy app !?\n", hwnd);
1214 return FALSE;
1216 dlgInfo->idResult = retval;
1217 dlgInfo->flags |= DF_END;
1218 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1220 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1221 DIALOG_EnableOwner( owner );
1223 /* Windows sets the focus to the dialog itself in EndDialog */
1225 if (IsChild(hwnd, GetFocus()))
1226 SetFocus( hwnd );
1228 /* Don't have to send a ShowWindow(SW_HIDE), just do
1229 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1231 SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1232 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1234 /* unblock dialog loop */
1235 PostMessageA(hwnd, WM_NULL, 0, 0);
1236 return TRUE;
1240 /***********************************************************************
1241 * DIALOG_IsAccelerator
1243 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
1245 HWND hwndControl = hwnd;
1246 HWND hwndNext;
1247 INT dlgCode;
1248 WCHAR buffer[128];
1252 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
1253 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
1255 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
1256 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
1257 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
1259 /* find the accelerator key */
1260 LPWSTR p = buffer - 2;
1264 p = strchrW( p + 2, '&' );
1266 while (p != NULL && p[1] == '&');
1268 /* and check if it's the one we're looking for */
1269 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
1271 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
1273 /* set focus to the control */
1274 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
1275 /* and bump it on to next */
1276 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
1278 else if (dlgCode & DLGC_BUTTON)
1280 /* send BM_CLICK message to the control */
1281 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
1283 return TRUE;
1286 hwndNext = GetWindow( hwndControl, GW_CHILD );
1288 else hwndNext = 0;
1290 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1292 while (!hwndNext && hwndControl)
1294 hwndControl = GetParent( hwndControl );
1295 if (hwndControl == hwndDlg)
1297 if(hwnd==hwndDlg) /* prevent endless loop */
1299 hwndNext=hwnd;
1300 break;
1302 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1304 else
1305 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1307 hwndControl = hwndNext;
1309 while (hwndControl && (hwndControl != hwnd));
1311 return FALSE;
1314 /***********************************************************************
1315 * DIALOG_FindMsgDestination
1317 * The messages that IsDialogMessage sends may not go to the dialog
1318 * calling IsDialogMessage if that dialog is a child, and it has the
1319 * DS_CONTROL style set.
1320 * We propagate up until we hit one that does not have DS_CONTROL, or
1321 * whose parent is not a dialog.
1323 * This is undocumented behaviour.
1325 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1327 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1329 WND *pParent;
1330 HWND hParent = GetParent(hwndDlg);
1331 if (!hParent) break;
1333 pParent = WIN_FindWndPtr(hParent);
1334 if (!pParent) break;
1336 if (!(pParent->flags & WIN_ISDIALOG))
1338 WIN_ReleaseWndPtr(pParent);
1339 break;
1341 WIN_ReleaseWndPtr(pParent);
1343 hwndDlg = hParent;
1346 return hwndDlg;
1349 /***********************************************************************
1350 * IsDialogMessageW (USER32.@)
1352 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1354 INT dlgCode = 0;
1356 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1358 hwndDlg = WIN_GetFullHandle( hwndDlg );
1359 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1361 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1363 switch(msg->message)
1365 case WM_KEYDOWN:
1366 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1367 if (dlgCode & DLGC_WANTMESSAGE) break;
1369 switch(msg->wParam)
1371 case VK_TAB:
1372 if (!(dlgCode & DLGC_WANTTAB))
1374 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1375 return TRUE;
1377 break;
1379 case VK_RIGHT:
1380 case VK_DOWN:
1381 case VK_LEFT:
1382 case VK_UP:
1383 if (!(dlgCode & DLGC_WANTARROWS))
1385 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1386 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1387 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1388 return TRUE;
1390 break;
1392 case VK_CANCEL:
1393 case VK_ESCAPE:
1394 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1395 return TRUE;
1397 case VK_EXECUTE:
1398 case VK_RETURN:
1400 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1401 if (HIWORD(dw) == DC_HASDEFID)
1403 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1404 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1406 else
1408 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1412 return TRUE;
1414 break;
1416 case WM_CHAR:
1417 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1418 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1419 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1420 /* drop through */
1422 case WM_SYSCHAR:
1423 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1425 /* don't translate or dispatch */
1426 return TRUE;
1428 break;
1431 TranslateMessage( msg );
1432 DispatchMessageW( msg );
1433 return TRUE;
1437 /***********************************************************************
1438 * GetDlgCtrlID (USER32.@)
1440 INT WINAPI GetDlgCtrlID( HWND hwnd )
1442 return GetWindowLongW( hwnd, GWL_ID );
1446 /***********************************************************************
1447 * GetDlgItem (USER32.@)
1449 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1451 int i;
1452 HWND *list = WIN_ListChildren( hwndDlg );
1453 HWND ret = 0;
1455 if (!list) return 0;
1457 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1458 ret = list[i];
1459 HeapFree( GetProcessHeap(), 0, list );
1460 return ret;
1464 /*******************************************************************
1465 * SendDlgItemMessageA (USER32.@)
1467 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1468 WPARAM wParam, LPARAM lParam )
1470 HWND hwndCtrl = GetDlgItem( hwnd, id );
1471 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1472 else return 0;
1476 /*******************************************************************
1477 * SendDlgItemMessageW (USER32.@)
1479 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1480 WPARAM wParam, LPARAM lParam )
1482 HWND hwndCtrl = GetDlgItem( hwnd, id );
1483 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1484 else return 0;
1488 /*******************************************************************
1489 * SetDlgItemTextA (USER32.@)
1491 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1493 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1497 /*******************************************************************
1498 * SetDlgItemTextW (USER32.@)
1500 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1502 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1506 /***********************************************************************
1507 * GetDlgItemTextA (USER32.@)
1509 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1511 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1512 len, (LPARAM)str );
1516 /***********************************************************************
1517 * GetDlgItemTextW (USER32.@)
1519 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1521 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1522 len, (LPARAM)str );
1526 /*******************************************************************
1527 * SetDlgItemInt (USER32.@)
1529 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1530 BOOL fSigned )
1532 char str[20];
1534 if (fSigned) sprintf( str, "%d", (INT)value );
1535 else sprintf( str, "%u", value );
1536 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1537 return TRUE;
1541 /***********************************************************************
1542 * GetDlgItemInt (USER32.@)
1544 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1545 BOOL fSigned )
1547 char str[30];
1548 char * endptr;
1549 long result = 0;
1551 if (translated) *translated = FALSE;
1552 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1553 return 0;
1554 if (fSigned)
1556 result = strtol( str, &endptr, 10 );
1557 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1558 return 0;
1559 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1560 return 0;
1562 else
1564 result = strtoul( str, &endptr, 10 );
1565 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1566 return 0;
1567 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1569 if (translated) *translated = TRUE;
1570 return (UINT)result;
1574 /***********************************************************************
1575 * CheckDlgButton (USER32.@)
1577 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1579 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1580 return TRUE;
1584 /***********************************************************************
1585 * IsDlgButtonChecked (USER32.@)
1587 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1589 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1593 /***********************************************************************
1594 * CheckRB
1596 * Callback function used to check/uncheck radio buttons that fall
1597 * within a specific range of IDs.
1599 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1601 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1602 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1604 if ((lChildID >= lpRadioGroup->firstID) &&
1605 (lChildID <= lpRadioGroup->lastID))
1607 if (lChildID == lpRadioGroup->checkID)
1609 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1611 else
1613 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1617 return TRUE;
1621 /***********************************************************************
1622 * CheckRadioButton (USER32.@)
1624 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1625 UINT lastID, UINT checkID )
1627 RADIOGROUP radioGroup;
1629 /* perform bounds checking for a radio button group */
1630 radioGroup.firstID = min(min(firstID, lastID), checkID);
1631 radioGroup.lastID = max(max(firstID, lastID), checkID);
1632 radioGroup.checkID = checkID;
1634 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1635 (LPARAM)&radioGroup);
1639 /***********************************************************************
1640 * GetDialogBaseUnits (USER.243)
1641 * GetDialogBaseUnits (USER32.@)
1643 DWORD WINAPI GetDialogBaseUnits(void)
1645 return MAKELONG( xBaseUnit, yBaseUnit );
1649 /***********************************************************************
1650 * MapDialogRect (USER32.@)
1652 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1654 DIALOGINFO * dlgInfo;
1655 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1656 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1657 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1658 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1659 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1660 return TRUE;
1664 /***********************************************************************
1665 * GetNextDlgGroupItem (USER32.@)
1667 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1669 HWND hwnd, retvalue;
1671 hwndDlg = WIN_GetFullHandle( hwndDlg );
1672 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1674 if(hwndCtrl)
1676 /* if the hwndCtrl is the child of the control in the hwndDlg,
1677 * then the hwndDlg has to be the parent of the hwndCtrl */
1678 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1679 hwndDlg = GetParent(hwndCtrl);
1682 if (hwndCtrl)
1684 /* Make sure hwndCtrl is a top-level child */
1685 HWND parent = GetParent( hwndCtrl );
1686 while (parent && parent != hwndDlg) parent = GetParent(parent);
1687 if (parent != hwndDlg) return 0;
1689 else
1691 /* No ctrl specified -> start from the beginning */
1692 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1693 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1696 retvalue = hwndCtrl;
1697 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1698 while (1)
1700 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1702 /* Wrap-around to the beginning of the group */
1703 HWND tmp;
1705 hwnd = GetWindow( hwndDlg, GW_CHILD );
1706 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1708 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1709 if (tmp == hwndCtrl) break;
1712 if (hwnd == hwndCtrl) break;
1713 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1715 retvalue = hwnd;
1716 if (!fPrevious) break;
1718 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1720 return retvalue;
1724 /***********************************************************************
1725 * DIALOG_GetNextTabItem
1727 * Helper for GetNextDlgTabItem
1729 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1731 LONG dsStyle;
1732 LONG exStyle;
1733 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1734 HWND retWnd = 0;
1735 HWND hChildFirst = 0;
1737 if(!hwndCtrl)
1739 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1740 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1742 else if (IsChild( hwndMain, hwndCtrl ))
1744 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1745 if(!hChildFirst)
1747 if(GetParent(hwndCtrl) != hwndMain)
1748 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1749 else
1751 if(fPrevious)
1752 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1753 else
1754 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1759 while(hChildFirst)
1761 BOOL bCtrl = FALSE;
1762 while(hChildFirst)
1764 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1765 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1766 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1768 bCtrl=TRUE;
1769 break;
1771 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1772 break;
1773 hChildFirst = GetWindow(hChildFirst,wndSearch);
1775 if(hChildFirst)
1777 if(bCtrl)
1778 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,(HWND)NULL,fPrevious );
1779 else
1780 retWnd = hChildFirst;
1782 if(retWnd) break;
1783 hChildFirst = GetWindow(hChildFirst,wndSearch);
1785 if(!retWnd && hwndCtrl)
1787 HWND hParent = GetParent(hwndCtrl);
1788 while(hParent)
1790 if(hParent == hwndMain) break;
1791 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1792 if(retWnd) break;
1793 hParent = GetParent(hParent);
1795 if(!retWnd)
1796 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,(HWND)NULL,fPrevious );
1798 return retWnd;
1801 /***********************************************************************
1802 * GetNextDlgTabItem (USER32.@)
1804 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1805 BOOL fPrevious )
1807 hwndDlg = WIN_GetFullHandle( hwndDlg );
1808 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1809 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1812 /**********************************************************************
1813 * DIALOG_DlgDirSelect
1815 * Helper function for DlgDirSelect*
1817 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1818 INT id, BOOL unicode, BOOL combo )
1820 char *buffer, *ptr;
1821 INT item, size;
1822 BOOL ret;
1823 HWND listbox = GetDlgItem( hwnd, id );
1825 TRACE("%04x '%s' %d\n", hwnd, str, id );
1826 if (!listbox) return FALSE;
1828 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1829 if (item == LB_ERR) return FALSE;
1830 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1831 if (size == LB_ERR) return FALSE;
1833 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1835 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1837 if ((ret = (buffer[0] == '['))) /* drive or directory */
1839 if (buffer[1] == '-') /* drive */
1841 buffer[3] = ':';
1842 buffer[4] = 0;
1843 ptr = buffer + 2;
1845 else
1847 buffer[strlen(buffer)-1] = '\\';
1848 ptr = buffer + 1;
1851 else ptr = buffer;
1853 if (unicode)
1855 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1856 ((LPWSTR)str)[len-1] = 0;
1858 else lstrcpynA( str, ptr, len );
1859 HeapFree( GetProcessHeap(), 0, buffer );
1860 TRACE("Returning %d '%s'\n", ret, str );
1861 return ret;
1865 /**********************************************************************
1866 * DIALOG_DlgDirList
1868 * Helper function for DlgDirList*
1870 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
1871 INT idStatic, UINT attrib, BOOL combo )
1873 HWND hwnd;
1874 LPSTR orig_spec = spec;
1876 #define SENDMSG(msg,wparam,lparam) \
1877 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
1878 : SendMessageA( hwnd, msg, wparam, lparam ))
1880 TRACE("%04x '%s' %d %d %04x\n",
1881 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1883 /* If the path exists and is a directory, chdir to it */
1884 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
1885 else
1887 char *p, *p2;
1888 p = spec;
1889 if ((p2 = strrchr( p, '\\' ))) p = p2;
1890 if ((p2 = strrchr( p, '/' ))) p = p2;
1891 if (p != spec)
1893 char sep = *p;
1894 *p = 0;
1895 if (!SetCurrentDirectoryA( spec ))
1897 *p = sep; /* Restore the original spec */
1898 return FALSE;
1900 spec = p + 1;
1904 TRACE( "mask=%s\n", spec );
1906 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1908 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1909 if (attrib & DDL_DIRECTORY)
1911 if (!(attrib & DDL_EXCLUSIVE))
1913 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1914 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1915 (LPARAM)spec ) == LB_ERR)
1916 return FALSE;
1918 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1919 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1920 (LPARAM)"*.*" ) == LB_ERR)
1921 return FALSE;
1923 else
1925 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1926 (LPARAM)spec ) == LB_ERR)
1927 return FALSE;
1931 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1933 char temp[MAX_PATH];
1934 GetCurrentDirectoryA( sizeof(temp), temp );
1935 CharLowerA( temp );
1936 /* Can't use PostMessage() here, because the string is on the stack */
1937 SetDlgItemTextA( hDlg, idStatic, temp );
1940 if (orig_spec && (spec != orig_spec))
1942 /* Update the original file spec */
1943 char *p = spec;
1944 while ((*orig_spec++ = *p++));
1947 return TRUE;
1948 #undef SENDMSG
1952 /**********************************************************************
1953 * DIALOG_DlgDirListW
1955 * Helper function for DlgDirList*W
1957 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1958 INT idStatic, UINT attrib, BOOL combo )
1960 if (spec)
1962 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
1963 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
1964 attrib, combo );
1965 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
1966 HeapFree( GetProcessHeap(), 0, specA );
1967 return ret;
1969 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
1973 /**********************************************************************
1974 * DlgDirSelectExA (USER32.@)
1976 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1978 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1982 /**********************************************************************
1983 * DlgDirSelectExW (USER32.@)
1985 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1987 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1991 /**********************************************************************
1992 * DlgDirSelectComboBoxExA (USER32.@)
1994 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1995 INT id )
1997 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
2001 /**********************************************************************
2002 * DlgDirSelectComboBoxExW (USER32.@)
2004 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
2005 INT id)
2007 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
2011 /**********************************************************************
2012 * DlgDirListA (USER32.@)
2014 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
2015 INT idStatic, UINT attrib )
2017 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2021 /**********************************************************************
2022 * DlgDirListW (USER32.@)
2024 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
2025 INT idStatic, UINT attrib )
2027 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
2031 /**********************************************************************
2032 * DlgDirListComboBoxA (USER32.@)
2034 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
2035 INT idStatic, UINT attrib )
2037 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
2041 /**********************************************************************
2042 * DlgDirListComboBoxW (USER32.@)
2044 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
2045 INT idStatic, UINT attrib )
2047 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );