Janitorial: C booleans must not be compared against TRUE.
[wine/multimedia.git] / windows / dialog.c
blobbe2ed628aac0b9b5e32b641fb2c3b78a51d3971a
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 <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
39 #include "controls.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
48 /* Dialog control information */
49 typedef struct
51 DWORD style;
52 DWORD exStyle;
53 DWORD helpId;
54 INT16 x;
55 INT16 y;
56 INT16 cx;
57 INT16 cy;
58 UINT id;
59 LPCWSTR className;
60 LPCWSTR windowName;
61 LPCVOID data;
62 } DLG_CONTROL_INFO;
64 /* Dialog template */
65 typedef struct
67 DWORD style;
68 DWORD exStyle;
69 DWORD helpId;
70 UINT16 nbItems;
71 INT16 x;
72 INT16 y;
73 INT16 cx;
74 INT16 cy;
75 LPCWSTR menuName;
76 LPCWSTR className;
77 LPCWSTR caption;
78 INT16 pointSize;
79 WORD weight;
80 BOOL italic;
81 LPCWSTR faceName;
82 BOOL dialogEx;
83 } DLG_TEMPLATE;
85 /* Radio button group */
86 typedef struct
88 UINT firstID;
89 UINT lastID;
90 UINT checkID;
91 } RADIOGROUP;
94 /*********************************************************************
95 * dialog class descriptor
97 const struct builtin_class_descr DIALOG_builtin_class =
99 DIALOG_CLASS_ATOMA, /* name */
100 CS_SAVEBITS | CS_DBLCLKS, /* style */
101 DefDlgProcA, /* procA */
102 DefDlgProcW, /* procW */
103 DLGWINDOWEXTRA, /* extra */
104 IDC_ARROW, /* cursor */
105 0 /* brush */
109 /***********************************************************************
110 * DIALOG_EnableOwner
112 * Helper function for modal dialogs to enable again the
113 * owner of the dialog box.
115 void DIALOG_EnableOwner( HWND hOwner )
117 /* Owner must be a top-level window */
118 if (hOwner)
119 hOwner = GetAncestor( hOwner, GA_ROOT );
120 if (!hOwner) return;
121 EnableWindow( hOwner, TRUE );
125 /***********************************************************************
126 * DIALOG_DisableOwner
128 * Helper function for modal dialogs to disable the
129 * owner of the dialog box. Returns TRUE if owner was enabled.
131 BOOL DIALOG_DisableOwner( HWND hOwner )
133 /* Owner must be a top-level window */
134 if (hOwner)
135 hOwner = GetAncestor( hOwner, GA_ROOT );
136 if (!hOwner) return FALSE;
137 if (IsWindowEnabled( hOwner ))
139 EnableWindow( hOwner, FALSE );
140 return TRUE;
142 else
143 return FALSE;
146 /***********************************************************************
147 * DIALOG_GetCharSize
149 * Despite most of MSDN insisting that the horizontal base unit is
150 * tmAveCharWidth it isn't. Knowledge base article Q145994
151 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
152 * says that we should take the average of the 52 English upper and lower
153 * case characters.
155 BOOL DIALOG_GetCharSize( HDC hDC, HFONT hFont, SIZE * pSize )
157 HFONT hFontPrev = 0;
158 const char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
159 SIZE sz;
160 TEXTMETRICA tm;
162 if(!hDC) return FALSE;
164 if(hFont) hFontPrev = SelectObject(hDC, hFont);
165 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
166 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
168 pSize->cy = tm.tmHeight;
169 pSize->cx = (sz.cx / 26 + 1) / 2;
171 if (hFontPrev) SelectObject(hDC, hFontPrev);
173 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
174 return TRUE;
178 /***********************************************************************
179 * DIALOG_GetControl32
181 * Return the class and text of the control pointed to by ptr,
182 * fill the header structure and return a pointer to the next control.
184 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
185 BOOL dialogEx )
187 if (dialogEx)
189 info->helpId = GET_DWORD(p); p += 2;
190 info->exStyle = GET_DWORD(p); p += 2;
191 info->style = GET_DWORD(p); p += 2;
193 else
195 info->helpId = 0;
196 info->style = GET_DWORD(p); p += 2;
197 info->exStyle = GET_DWORD(p); p += 2;
199 info->x = GET_WORD(p); p++;
200 info->y = GET_WORD(p); p++;
201 info->cx = GET_WORD(p); p++;
202 info->cy = GET_WORD(p); p++;
204 if (dialogEx)
206 /* id is a DWORD for DIALOGEX */
207 info->id = GET_DWORD(p);
208 p += 2;
210 else
212 info->id = GET_WORD(p);
213 p++;
216 if (GET_WORD(p) == 0xffff)
218 static const WCHAR class_names[6][10] =
220 { 'B','u','t','t','o','n', }, /* 0x80 */
221 { 'E','d','i','t', }, /* 0x81 */
222 { 'S','t','a','t','i','c', }, /* 0x82 */
223 { 'L','i','s','t','B','o','x', }, /* 0x83 */
224 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
225 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
227 WORD id = GET_WORD(p+1);
228 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
229 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
230 if (id <= 5)
231 info->className = class_names[id];
232 else
234 info->className = NULL;
235 ERR("Unknown built-in class id %04x\n", id );
237 p += 2;
239 else
241 info->className = (LPCWSTR)p;
242 p += strlenW( info->className ) + 1;
245 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
247 info->windowName = (LPCWSTR)(UINT)GET_WORD(p + 1);
248 p += 2;
250 else
252 info->windowName = (LPCWSTR)p;
253 p += strlenW( info->windowName ) + 1;
256 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
257 debugstr_w( info->className ), debugstr_w( info->windowName ),
258 info->id, info->x, info->y, info->cx, info->cy,
259 info->style, info->exStyle, info->helpId );
261 if (GET_WORD(p))
263 if (TRACE_ON(dialog))
265 WORD i, count = GET_WORD(p) / sizeof(WORD);
266 TRACE(" BEGIN\n");
267 TRACE(" ");
268 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
269 TRACE("\n");
270 TRACE(" END\n" );
272 info->data = p + 1;
273 p += GET_WORD(p) / sizeof(WORD);
275 else info->data = NULL;
276 p++;
278 /* Next control is on dword boundary */
279 return (const WORD *)((((int)p) + 3) & ~3);
283 /***********************************************************************
284 * DIALOG_CreateControls32
286 * Create the control windows for a dialog.
288 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
289 HINSTANCE hInst, BOOL unicode )
291 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
292 DLG_CONTROL_INFO info;
293 HWND hwndCtrl, hwndDefButton = 0;
294 INT items = dlgTemplate->nbItems;
296 TRACE(" BEGIN\n" );
297 while (items--)
299 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
300 dlgTemplate->dialogEx );
301 /* Is this it? */
302 if (info.style & WS_BORDER)
304 info.style &= ~WS_BORDER;
305 info.exStyle |= WS_EX_CLIENTEDGE;
307 if (unicode)
309 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
310 info.className, info.windowName,
311 info.style | WS_CHILD,
312 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
313 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
314 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
315 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
316 hwnd, (HMENU)info.id,
317 hInst, (LPVOID)info.data );
319 else
321 LPSTR class = (LPSTR)info.className;
322 LPSTR caption = (LPSTR)info.windowName;
324 if (HIWORD(class))
326 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
327 class = HeapAlloc( GetProcessHeap(), 0, len );
328 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
330 if (HIWORD(caption))
332 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
333 caption = HeapAlloc( GetProcessHeap(), 0, len );
334 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
336 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
337 class, caption, info.style | WS_CHILD,
338 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
339 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
340 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
341 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
342 hwnd, (HMENU)info.id,
343 hInst, (LPVOID)info.data );
344 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
345 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
347 if (!hwndCtrl)
349 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
350 return FALSE;
353 /* Send initialisation messages to the control */
354 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
355 (WPARAM)dlgInfo->hUserFont, 0 );
356 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
358 /* If there's already a default push-button, set it back */
359 /* to normal and use this one instead. */
360 if (hwndDefButton)
361 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
362 hwndDefButton = hwndCtrl;
363 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
366 TRACE(" END\n" );
367 return TRUE;
371 /***********************************************************************
372 * DIALOG_ParseTemplate32
374 * Fill a DLG_TEMPLATE structure from the dialog template, and return
375 * a pointer to the first control.
377 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
379 const WORD *p = (const WORD *)template;
380 WORD signature;
381 WORD dlgver;
383 signature = GET_WORD(p); p++;
384 dlgver = GET_WORD(p); p++;
386 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
388 result->dialogEx = TRUE;
389 result->helpId = GET_DWORD(p); p += 2;
390 result->exStyle = GET_DWORD(p); p += 2;
391 result->style = GET_DWORD(p); p += 2;
393 else
395 result->style = GET_DWORD(p - 2);
396 result->dialogEx = FALSE;
397 result->helpId = 0;
398 result->exStyle = GET_DWORD(p); p += 2;
400 result->nbItems = GET_WORD(p); p++;
401 result->x = GET_WORD(p); p++;
402 result->y = GET_WORD(p); p++;
403 result->cx = GET_WORD(p); p++;
404 result->cy = GET_WORD(p); p++;
405 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
406 result->dialogEx ? "EX" : "", result->x, result->y,
407 result->cx, result->cy, result->helpId );
408 TRACE(" STYLE 0x%08lx\n", result->style );
409 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
411 /* Get the menu name */
413 switch(GET_WORD(p))
415 case 0x0000:
416 result->menuName = NULL;
417 p++;
418 break;
419 case 0xffff:
420 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
421 p += 2;
422 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
423 break;
424 default:
425 result->menuName = (LPCWSTR)p;
426 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
427 p += strlenW( result->menuName ) + 1;
428 break;
431 /* Get the class name */
433 switch(GET_WORD(p))
435 case 0x0000:
436 result->className = DIALOG_CLASS_ATOMW;
437 p++;
438 break;
439 case 0xffff:
440 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
441 p += 2;
442 TRACE(" CLASS %04x\n", LOWORD(result->className) );
443 break;
444 default:
445 result->className = (LPCWSTR)p;
446 TRACE(" CLASS %s\n", debugstr_w( result->className ));
447 p += strlenW( result->className ) + 1;
448 break;
451 /* Get the window caption */
453 result->caption = (LPCWSTR)p;
454 p += strlenW( result->caption ) + 1;
455 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
457 /* Get the font name */
459 if (result->style & DS_SETFONT)
461 result->pointSize = GET_WORD(p);
462 p++;
463 if (result->dialogEx)
465 result->weight = GET_WORD(p); p++;
466 result->italic = LOBYTE(GET_WORD(p)); p++;
468 else
470 result->weight = FW_DONTCARE;
471 result->italic = FALSE;
473 result->faceName = (LPCWSTR)p;
474 p += strlenW( result->faceName ) + 1;
475 TRACE(" FONT %d, %s, %d, %s\n",
476 result->pointSize, debugstr_w( result->faceName ),
477 result->weight, result->italic ? "TRUE" : "FALSE" );
480 /* First control is on dword boundary */
481 return (LPCSTR)((((int)p) + 3) & ~3);
485 /***********************************************************************
486 * DIALOG_CreateIndirect
487 * Creates a dialog box window
489 * modal = TRUE if we are called from a modal dialog box.
490 * (it's more compatible to do it here, as under Windows the owner
491 * is never disabled if the dialog fails because of an invalid template)
493 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
494 HWND owner, DLGPROC dlgProc, LPARAM param,
495 BOOL unicode, BOOL modal )
497 HWND hwnd;
498 RECT rect;
499 DLG_TEMPLATE template;
500 DIALOGINFO * dlgInfo = NULL;
501 DWORD units = GetDialogBaseUnits();
502 BOOL ownerEnabled = TRUE;
503 HMENU hMenu = 0;
504 HFONT hUserFont = 0;
505 UINT flags = 0;
506 UINT xBaseUnit = LOWORD(units);
507 UINT yBaseUnit = HIWORD(units);
509 /* Parse dialog template */
511 if (!dlgTemplate) return 0;
512 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
514 /* Load menu */
516 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
518 /* Create custom font if needed */
520 if (template.style & DS_SETFONT)
522 /* We convert the size to pixels and then make it -ve. This works
523 * for both +ve and -ve template.pointSize */
524 HDC dc;
525 int pixels;
526 dc = GetDC(0);
527 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
528 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
529 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
530 PROOF_QUALITY, FF_DONTCARE,
531 template.faceName );
532 if (hUserFont)
534 SIZE charSize;
535 if (DIALOG_GetCharSize( dc, hUserFont, &charSize ))
537 xBaseUnit = charSize.cx;
538 yBaseUnit = charSize.cy;
541 ReleaseDC(0, dc);
542 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
545 /* Create dialog main window */
547 rect.left = rect.top = 0;
548 rect.right = MulDiv(template.cx, xBaseUnit, 4);
549 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
550 if (template.style & WS_CHILD)
551 template.style &= ~(WS_CAPTION|WS_SYSMENU);
552 if (template.style & DS_MODALFRAME)
553 template.exStyle |= WS_EX_DLGMODALFRAME;
554 if (template.style & DS_CONTROL)
555 template.exStyle |= WS_EX_CONTROLPARENT;
556 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
557 rect.right -= rect.left;
558 rect.bottom -= rect.top;
560 if (template.x == CW_USEDEFAULT16)
562 rect.left = rect.top = CW_USEDEFAULT;
564 else
566 if (template.style & DS_CENTER)
568 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
569 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
571 else
573 rect.left += MulDiv(template.x, xBaseUnit, 4);
574 rect.top += MulDiv(template.y, yBaseUnit, 8);
576 if ( !(template.style & WS_CHILD) )
578 INT dX, dY;
580 if( !(template.style & DS_ABSALIGN) )
581 ClientToScreen( owner, (POINT *)&rect );
583 /* try to fit it into the desktop */
585 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
586 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
587 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
588 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
589 if( rect.left < 0 ) rect.left = 0;
590 if( rect.top < 0 ) rect.top = 0;
594 if (modal)
596 ownerEnabled = DIALOG_DisableOwner( owner );
597 if (ownerEnabled) flags |= DF_OWNERENABLED;
600 if (unicode)
602 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
603 template.style & ~WS_VISIBLE,
604 rect.left, rect.top, rect.right, rect.bottom,
605 owner, hMenu, hInst, NULL );
607 else
609 LPSTR class = (LPSTR)template.className;
610 LPSTR caption = (LPSTR)template.caption;
612 if (HIWORD(class))
614 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
615 class = HeapAlloc( GetProcessHeap(), 0, len );
616 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
618 if (HIWORD(caption))
620 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
621 caption = HeapAlloc( GetProcessHeap(), 0, len );
622 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
624 hwnd = CreateWindowExA(template.exStyle, class, caption,
625 template.style & ~WS_VISIBLE,
626 rect.left, rect.top, rect.right, rect.bottom,
627 owner, hMenu, hInst, NULL );
628 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
629 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
632 if (!hwnd)
634 if (hUserFont) DeleteObject( hUserFont );
635 if (hMenu) DestroyMenu( hMenu );
636 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
637 return 0;
640 /* moved this from the top of the method to here as DIALOGINFO structure
641 will be valid only after WM_CREATE message has been handled in DefDlgProc
642 All the members of the structure get filled here using temp variables */
643 dlgInfo = DIALOG_get_info( hwnd, TRUE );
644 dlgInfo->hwndFocus = 0;
645 dlgInfo->hUserFont = hUserFont;
646 dlgInfo->hMenu = hMenu;
647 dlgInfo->xBaseUnit = xBaseUnit;
648 dlgInfo->yBaseUnit = yBaseUnit;
649 dlgInfo->idResult = 0;
650 dlgInfo->flags = flags;
651 dlgInfo->hDialogHeap = 0;
653 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
655 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
656 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
658 if (dlgInfo->hUserFont)
659 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
661 /* Create controls */
663 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
665 /* Send initialisation messages and set focus */
667 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
669 /* By returning TRUE, app has requested a default focus assignment */
670 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
671 if( dlgInfo->hwndFocus )
672 SetFocus( dlgInfo->hwndFocus );
675 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
677 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
679 return hwnd;
681 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
682 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
683 return 0;
687 /***********************************************************************
688 * CreateDialogParamA (USER32.@)
690 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
691 DLGPROC dlgProc, LPARAM param )
693 HRSRC hrsrc;
694 LPCDLGTEMPLATEA ptr;
696 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
697 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
698 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
702 /***********************************************************************
703 * CreateDialogParamW (USER32.@)
705 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
706 DLGPROC dlgProc, LPARAM param )
708 HRSRC hrsrc;
709 LPCDLGTEMPLATEA ptr;
711 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
712 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
713 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
717 /***********************************************************************
718 * CreateDialogIndirectParamAorW (USER32.@)
720 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
721 HWND owner, DLGPROC dlgProc, LPARAM param,
722 DWORD flags )
724 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
727 /***********************************************************************
728 * CreateDialogIndirectParamA (USER32.@)
730 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
731 HWND owner, DLGPROC dlgProc, LPARAM param )
733 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
736 /***********************************************************************
737 * CreateDialogIndirectParamW (USER32.@)
739 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
740 HWND owner, DLGPROC dlgProc, LPARAM param )
742 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
746 /***********************************************************************
747 * DIALOG_DoDialogBox
749 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
751 DIALOGINFO * dlgInfo;
752 MSG msg;
753 INT retval;
754 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
755 BOOL bFirstEmpty;
757 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
759 bFirstEmpty = TRUE;
760 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
762 for (;;)
764 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
766 if (bFirstEmpty)
768 /* ShowWindow the first time the queue goes empty */
769 ShowWindow( hwnd, SW_SHOWNORMAL );
770 bFirstEmpty = FALSE;
772 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
774 /* No message present -> send ENTERIDLE and wait */
775 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
777 if (!GetMessageW( &msg, 0, 0, 0 )) break;
780 if (!IsWindow( hwnd )) return -1;
781 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
783 TranslateMessage( &msg );
784 DispatchMessageW( &msg );
786 if (dlgInfo->flags & DF_END) break;
789 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
790 retval = dlgInfo->idResult;
791 DestroyWindow( hwnd );
792 return retval;
796 /***********************************************************************
797 * DialogBoxParamA (USER32.@)
799 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
800 HWND owner, DLGPROC dlgProc, LPARAM param )
802 HWND hwnd;
803 HRSRC hrsrc;
804 LPCDLGTEMPLATEA ptr;
806 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
807 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
808 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
809 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
810 return -1;
814 /***********************************************************************
815 * DialogBoxParamW (USER32.@)
817 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
818 HWND owner, DLGPROC dlgProc, LPARAM param )
820 HWND hwnd;
821 HRSRC hrsrc;
822 LPCDLGTEMPLATEW ptr;
824 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
825 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
826 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
827 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
828 return -1;
832 /***********************************************************************
833 * DialogBoxIndirectParamAorW (USER32.@)
835 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
836 HWND owner, DLGPROC dlgProc,
837 LPARAM param, DWORD flags )
839 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
840 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
841 return -1;
844 /***********************************************************************
845 * DialogBoxIndirectParamA (USER32.@)
847 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
848 HWND owner, DLGPROC dlgProc, LPARAM param )
850 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
854 /***********************************************************************
855 * DialogBoxIndirectParamW (USER32.@)
857 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
858 HWND owner, DLGPROC dlgProc, LPARAM param )
860 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
863 /***********************************************************************
864 * EndDialog (USER32.@)
866 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
868 BOOL wasEnabled = TRUE;
869 DIALOGINFO * dlgInfo;
870 HWND owner;
872 TRACE("%p %d\n", hwnd, retval );
874 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
876 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
877 return FALSE;
879 dlgInfo->idResult = retval;
880 dlgInfo->flags |= DF_END;
881 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
883 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
884 DIALOG_EnableOwner( owner );
886 /* Windows sets the focus to the dialog itself in EndDialog */
888 if (IsChild(hwnd, GetFocus()))
889 SetFocus( hwnd );
891 /* Don't have to send a ShowWindow(SW_HIDE), just do
892 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
894 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
895 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
897 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
899 /* unblock dialog loop */
900 PostMessageA(hwnd, WM_NULL, 0, 0);
901 return TRUE;
905 /***********************************************************************
906 * DIALOG_IsAccelerator
908 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
910 HWND hwndControl = hwnd;
911 HWND hwndNext;
912 INT dlgCode;
913 WCHAR buffer[128];
917 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
918 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
920 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
921 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
922 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
924 /* find the accelerator key */
925 LPWSTR p = buffer - 2;
929 p = strchrW( p + 2, '&' );
931 while (p != NULL && p[1] == '&');
933 /* and check if it's the one we're looking for */
934 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
936 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
938 /* set focus to the control */
939 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
940 /* and bump it on to next */
941 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
943 else if (dlgCode & DLGC_BUTTON)
945 /* send BM_CLICK message to the control */
946 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
948 return TRUE;
951 hwndNext = GetWindow( hwndControl, GW_CHILD );
953 else hwndNext = 0;
955 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
957 while (!hwndNext && hwndControl)
959 hwndControl = GetParent( hwndControl );
960 if (hwndControl == hwndDlg)
962 if(hwnd==hwndDlg) /* prevent endless loop */
964 hwndNext=hwnd;
965 break;
967 hwndNext = GetWindow( hwndDlg, GW_CHILD );
969 else
970 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
972 hwndControl = hwndNext;
974 while (hwndControl && (hwndControl != hwnd));
976 return FALSE;
979 /***********************************************************************
980 * DIALOG_FindMsgDestination
982 * The messages that IsDialogMessage sends may not go to the dialog
983 * calling IsDialogMessage if that dialog is a child, and it has the
984 * DS_CONTROL style set.
985 * We propagate up until we hit one that does not have DS_CONTROL, or
986 * whose parent is not a dialog.
988 * This is undocumented behaviour.
990 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
992 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
994 WND *pParent;
995 HWND hParent = GetParent(hwndDlg);
996 if (!hParent) break;
998 pParent = WIN_FindWndPtr(hParent);
999 if (!pParent) break;
1001 if (!(pParent->flags & WIN_ISDIALOG))
1003 WIN_ReleaseWndPtr(pParent);
1004 break;
1006 WIN_ReleaseWndPtr(pParent);
1008 hwndDlg = hParent;
1011 return hwndDlg;
1014 /***********************************************************************
1015 * DIALOG_FixOneChildOnChangeFocus
1017 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1020 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1021 LPARAM lParam)
1023 /* If a default pushbutton then no longer default */
1024 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1025 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1026 return TRUE;
1029 /***********************************************************************
1030 * DIALOG_FixChildrenOnChangeFocus
1032 * Following the change of focus that occurs for example after handling
1033 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1034 * children may be required.
1036 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1038 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1039 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1040 /* Windows does ask for this. I don't know why yet */
1042 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1044 /* If the button that is getting the focus WAS flagged as the default
1045 * pushbutton then ask the dialog what it thinks the default is and
1046 * set that in the default style.
1048 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1050 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1051 if (HIWORD(def_id) == DC_HASDEFID)
1053 HWND hwndDef;
1054 def_id = LOWORD(def_id);
1055 hwndDef = GetDlgItem (hwndDlg, def_id);
1056 if (hwndDef)
1058 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1059 /* I know that if it is a button then it should already be a
1060 * UNDEFPUSHBUTTON, since we have just told the buttons to
1061 * change style. But maybe they ignored our request
1063 if ((dlgcode_def & DLGC_BUTTON) &&
1064 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1066 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1071 else
1073 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1074 /* I wonder why it doesn't send a DM_SETDEFID */
1078 /***********************************************************************
1079 * IsDialogMessageW (USER32.@)
1081 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1083 INT dlgCode = 0;
1085 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1087 hwndDlg = WIN_GetFullHandle( hwndDlg );
1088 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1090 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1092 switch(msg->message)
1094 case WM_KEYDOWN:
1095 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1096 if (dlgCode & DLGC_WANTMESSAGE) break;
1098 switch(msg->wParam)
1100 case VK_TAB:
1101 if (!(dlgCode & DLGC_WANTTAB))
1103 BOOL fIsDialog = TRUE;
1104 WND *pWnd = WIN_GetPtr( hwndDlg );
1106 if (pWnd && pWnd != WND_OTHER_PROCESS)
1108 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1109 WIN_ReleasePtr(pWnd);
1112 /* I am not sure under which circumstances the TAB is handled
1113 * each way. All I do know is that it does not always simply
1114 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1115 * do so but I presume someone has)
1117 if (fIsDialog)
1118 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1119 else
1121 /* It would appear that GetNextDlgTabItem can handle being
1122 * passed hwndDlg rather than NULL but that is undocumented
1123 * so let's do it properly
1125 HWND hwndFocus = GetFocus();
1126 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1127 hwndFocus == hwndDlg ? NULL : hwndFocus,
1128 GetKeyState (VK_SHIFT) & 0x8000);
1129 if (hwndNext)
1131 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1132 msg->wParam, (LPARAM)msg);
1133 if (dlgCode & DLGC_HASSETSEL)
1135 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1136 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1137 if (buffer)
1139 INT length;
1140 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1141 length = strlenW (buffer);
1142 HeapFree (GetProcessHeap(), 0, buffer);
1143 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1146 SetFocus (hwndNext);
1147 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1149 else
1150 return FALSE;
1152 return TRUE;
1154 break;
1156 case VK_RIGHT:
1157 case VK_DOWN:
1158 case VK_LEFT:
1159 case VK_UP:
1160 if (!(dlgCode & DLGC_WANTARROWS))
1162 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1163 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1164 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1165 return TRUE;
1167 break;
1169 case VK_CANCEL:
1170 case VK_ESCAPE:
1171 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1172 return TRUE;
1174 case VK_EXECUTE:
1175 case VK_RETURN:
1177 DWORD dw;
1178 if ((GetFocus() == msg->hwnd) &&
1179 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1181 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1183 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1185 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1186 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1188 else
1190 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1194 return TRUE;
1196 break;
1198 case WM_CHAR:
1199 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1200 * It does NOT get sent in the test program I have
1202 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1203 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1204 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1205 /* drop through */
1207 case WM_SYSCHAR:
1208 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1210 /* don't translate or dispatch */
1211 return TRUE;
1213 break;
1216 TranslateMessage( msg );
1217 DispatchMessageW( msg );
1218 return TRUE;
1222 /***********************************************************************
1223 * GetDlgCtrlID (USER32.@)
1225 INT WINAPI GetDlgCtrlID( HWND hwnd )
1227 return GetWindowLongPtrW( hwnd, GWLP_ID );
1231 /***********************************************************************
1232 * GetDlgItem (USER32.@)
1234 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1236 int i;
1237 HWND *list = WIN_ListChildren( hwndDlg );
1238 HWND ret = 0;
1240 if (!list) return 0;
1242 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1243 ret = list[i];
1244 HeapFree( GetProcessHeap(), 0, list );
1245 return ret;
1249 /*******************************************************************
1250 * SendDlgItemMessageA (USER32.@)
1252 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1253 WPARAM wParam, LPARAM lParam )
1255 HWND hwndCtrl = GetDlgItem( hwnd, id );
1256 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1257 else return 0;
1261 /*******************************************************************
1262 * SendDlgItemMessageW (USER32.@)
1264 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1265 WPARAM wParam, LPARAM lParam )
1267 HWND hwndCtrl = GetDlgItem( hwnd, id );
1268 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1269 else return 0;
1273 /*******************************************************************
1274 * SetDlgItemTextA (USER32.@)
1276 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1278 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1282 /*******************************************************************
1283 * SetDlgItemTextW (USER32.@)
1285 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1287 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1291 /***********************************************************************
1292 * GetDlgItemTextA (USER32.@)
1294 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1296 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1297 len, (LPARAM)str );
1301 /***********************************************************************
1302 * GetDlgItemTextW (USER32.@)
1304 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1306 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1307 len, (LPARAM)str );
1311 /*******************************************************************
1312 * SetDlgItemInt (USER32.@)
1314 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1315 BOOL fSigned )
1317 char str[20];
1319 if (fSigned) sprintf( str, "%d", (INT)value );
1320 else sprintf( str, "%u", value );
1321 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1322 return TRUE;
1326 /***********************************************************************
1327 * GetDlgItemInt (USER32.@)
1329 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1330 BOOL fSigned )
1332 char str[30];
1333 char * endptr;
1334 long result = 0;
1336 if (translated) *translated = FALSE;
1337 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1338 return 0;
1339 if (fSigned)
1341 result = strtol( str, &endptr, 10 );
1342 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1343 return 0;
1344 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1345 return 0;
1347 else
1349 result = strtoul( str, &endptr, 10 );
1350 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1351 return 0;
1352 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1354 if (translated) *translated = TRUE;
1355 return (UINT)result;
1359 /***********************************************************************
1360 * CheckDlgButton (USER32.@)
1362 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1364 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1365 return TRUE;
1369 /***********************************************************************
1370 * IsDlgButtonChecked (USER32.@)
1372 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1374 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1378 /***********************************************************************
1379 * CheckRB
1381 * Callback function used to check/uncheck radio buttons that fall
1382 * within a specific range of IDs.
1384 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1386 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1387 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1389 if ((lChildID >= lpRadioGroup->firstID) &&
1390 (lChildID <= lpRadioGroup->lastID))
1392 if (lChildID == lpRadioGroup->checkID)
1394 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1396 else
1398 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1402 return TRUE;
1406 /***********************************************************************
1407 * CheckRadioButton (USER32.@)
1409 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1410 UINT lastID, UINT checkID )
1412 RADIOGROUP radioGroup;
1414 radioGroup.firstID = firstID;
1415 radioGroup.lastID = lastID;
1416 radioGroup.checkID = checkID;
1418 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1419 (LPARAM)&radioGroup);
1423 /***********************************************************************
1424 * GetDialogBaseUnits (USER.243)
1425 * GetDialogBaseUnits (USER32.@)
1427 DWORD WINAPI GetDialogBaseUnits(void)
1429 static DWORD units;
1431 if (!units)
1433 HDC hdc;
1434 SIZE size;
1436 if ((hdc = GetDC(0)))
1438 if (DIALOG_GetCharSize( hdc, 0, &size )) units = MAKELONG( size.cx, size.cy );
1439 ReleaseDC( 0, hdc );
1441 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1443 return units;
1447 /***********************************************************************
1448 * MapDialogRect (USER32.@)
1450 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1452 DIALOGINFO * dlgInfo;
1453 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1454 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1455 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1456 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1457 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1458 return TRUE;
1462 /***********************************************************************
1463 * GetNextDlgGroupItem (USER32.@)
1465 * Corrections to MSDN documentation
1467 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1468 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1469 * 2. Prev of NULL or hwndDlg fails
1471 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1473 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1474 BOOL fLooped=FALSE;
1475 BOOL fSkipping=FALSE;
1477 hwndDlg = WIN_GetFullHandle( hwndDlg );
1478 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1480 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1481 if (!hwndCtrl && fPrevious) return 0;
1483 if (hwndCtrl)
1485 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1487 else
1489 /* No ctrl specified -> start from the beginning */
1490 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1491 /* MSDN is wrong. fPrevious does not result in the last child */
1493 /* Maybe that first one is valid. If so then we don't want to skip it*/
1494 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1496 return hwndCtrl;
1500 /* Always go forward around the group and list of controls; for the
1501 * previous control keep track; for the next break when you find one
1503 retvalue = hwndCtrl;
1504 hwnd = hwndCtrl;
1505 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1508 while (!hwndNext)
1510 /* Climb out until there is a next sibling of the ancestor or we
1511 * reach the top (in which case we loop back to the start)
1513 if (hwndDlg == GetParent (hwnd))
1515 /* Wrap around to the beginning of the list, within the same
1516 * group. (Once only)
1518 if (fLooped) goto end;
1519 fLooped = TRUE;
1520 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1522 else
1524 hwnd = GetParent (hwnd);
1525 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1528 hwnd = hwndNext;
1530 /* Wander down the leading edge of controlparents */
1531 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1532 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1533 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1534 hwnd = hwndNext;
1535 /* Question. If the control is a control parent but either has no
1536 * children or is not visible/enabled then if it has a WS_GROUP does
1537 * it count? For that matter does it count anyway?
1538 * I believe it doesn't count.
1541 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1543 hwndLastGroup = hwnd;
1544 if (!fSkipping)
1546 /* Look for the beginning of the group */
1547 fSkipping = TRUE;
1551 if (hwnd == hwndCtrl)
1553 if (!fSkipping) break;
1554 if (hwndLastGroup == hwnd) break;
1555 hwnd = hwndLastGroup;
1556 fSkipping = FALSE;
1557 fLooped = FALSE;
1560 if (!fSkipping &&
1561 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1562 WS_VISIBLE)
1564 retvalue = hwnd;
1565 if (!fPrevious) break;
1568 end:
1569 return retvalue;
1573 /***********************************************************************
1574 * DIALOG_GetNextTabItem
1576 * Recursive helper for GetNextDlgTabItem
1578 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1580 LONG dsStyle;
1581 LONG exStyle;
1582 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1583 HWND retWnd = 0;
1584 HWND hChildFirst = 0;
1586 if(!hwndCtrl)
1588 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1589 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1591 else if (IsChild( hwndMain, hwndCtrl ))
1593 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1594 if(!hChildFirst)
1596 if(GetParent(hwndCtrl) != hwndMain)
1597 /* i.e. if we are not at the top level of the recursion */
1598 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1599 else
1600 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1604 while(hChildFirst)
1606 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1607 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1608 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1610 HWND retWnd;
1611 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1612 if (retWnd) return (retWnd);
1614 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1616 return (hChildFirst);
1618 hChildFirst = GetWindow(hChildFirst,wndSearch);
1620 if(hwndCtrl)
1622 HWND hParent = GetParent(hwndCtrl);
1623 while(hParent)
1625 if(hParent == hwndMain) break;
1626 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1627 if(retWnd) break;
1628 hParent = GetParent(hParent);
1630 if(!retWnd)
1631 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1633 return retWnd ? retWnd : hwndCtrl;
1636 /***********************************************************************
1637 * GetNextDlgTabItem (USER32.@)
1639 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1640 BOOL fPrevious )
1642 hwndDlg = WIN_GetFullHandle( hwndDlg );
1643 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1645 /* Undocumented but tested under Win2000 and WinME */
1646 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1648 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1649 * NB GetLastError returns whatever was set before the function was
1650 * called.
1652 if (!hwndCtrl && fPrevious) return 0;
1654 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1657 /**********************************************************************
1658 * DIALOG_DlgDirSelect
1660 * Helper function for DlgDirSelect*
1662 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1663 INT id, BOOL unicode, BOOL combo )
1665 WCHAR *buffer, *ptr;
1666 INT item, size;
1667 BOOL ret;
1668 HWND listbox = GetDlgItem( hwnd, id );
1670 TRACE("%p '%s' %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1671 if (!listbox) return FALSE;
1673 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1674 if (item == LB_ERR) return FALSE;
1676 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1677 if (size == LB_ERR) return FALSE;
1679 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1681 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1683 if ((ret = (buffer[0] == '['))) /* drive or directory */
1685 if (buffer[1] == '-') /* drive */
1687 buffer[3] = ':';
1688 buffer[4] = 0;
1689 ptr = buffer + 2;
1691 else
1693 buffer[strlenW(buffer)-1] = '\\';
1694 ptr = buffer + 1;
1697 else ptr = buffer;
1699 if (!unicode)
1701 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1702 ((LPSTR)str)[len-1] = 0;
1704 else lstrcpynW( str, ptr, len );
1705 HeapFree( GetProcessHeap(), 0, buffer );
1706 TRACE("Returning %d '%s'\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1707 return ret;
1711 /**********************************************************************
1712 * DIALOG_DlgDirListW
1714 * Helper function for DlgDirList*W
1716 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1717 INT idStatic, UINT attrib, BOOL combo )
1719 HWND hwnd;
1720 LPWSTR orig_spec = spec;
1721 WCHAR any[] = {'*','.','*',0};
1723 #define SENDMSG(msg,wparam,lparam) \
1724 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1725 : SendMessageW( hwnd, msg, wparam, lparam ))
1727 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1729 /* If the path exists and is a directory, chdir to it */
1730 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1731 else
1733 WCHAR *p, *p2;
1734 p = spec;
1735 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1736 if ((p2 = strrchrW( p, '/' ))) p = p2;
1737 if (p != spec)
1739 WCHAR sep = *p;
1740 *p = 0;
1741 if (!SetCurrentDirectoryW( spec ))
1743 *p = sep; /* Restore the original spec */
1744 return FALSE;
1746 spec = p + 1;
1750 TRACE( "mask=%s\n", debugstr_w(spec) );
1752 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1754 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1755 if (attrib & DDL_DIRECTORY)
1757 if (!(attrib & DDL_EXCLUSIVE))
1759 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1760 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1761 (LPARAM)spec ) == LB_ERR)
1762 return FALSE;
1764 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1765 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1766 (LPARAM)any ) == LB_ERR)
1767 return FALSE;
1769 else
1771 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1772 (LPARAM)spec ) == LB_ERR)
1773 return FALSE;
1777 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1779 WCHAR temp[MAX_PATH];
1780 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1781 CharLowerW( temp );
1782 /* Can't use PostMessage() here, because the string is on the stack */
1783 SetDlgItemTextW( hDlg, idStatic, temp );
1786 if (orig_spec && (spec != orig_spec))
1788 /* Update the original file spec */
1789 WCHAR *p = spec;
1790 while ((*orig_spec++ = *p++));
1793 return TRUE;
1794 #undef SENDMSG
1798 /**********************************************************************
1799 * DIALOG_DlgDirListA
1801 * Helper function for DlgDirList*A
1803 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1804 INT idStatic, UINT attrib, BOOL combo )
1806 if (spec)
1808 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1809 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1810 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1811 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1812 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1813 HeapFree( GetProcessHeap(), 0, specW );
1814 return ret;
1816 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1820 /**********************************************************************
1821 * DlgDirSelectExA (USER32.@)
1823 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1825 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1829 /**********************************************************************
1830 * DlgDirSelectExW (USER32.@)
1832 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1834 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1838 /**********************************************************************
1839 * DlgDirSelectComboBoxExA (USER32.@)
1841 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1842 INT id )
1844 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1848 /**********************************************************************
1849 * DlgDirSelectComboBoxExW (USER32.@)
1851 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1852 INT id)
1854 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1858 /**********************************************************************
1859 * DlgDirListA (USER32.@)
1861 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1862 INT idStatic, UINT attrib )
1864 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1868 /**********************************************************************
1869 * DlgDirListW (USER32.@)
1871 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1872 INT idStatic, UINT attrib )
1874 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1878 /**********************************************************************
1879 * DlgDirListComboBoxA (USER32.@)
1881 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1882 INT idStatic, UINT attrib )
1884 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1888 /**********************************************************************
1889 * DlgDirListComboBoxW (USER32.@)
1891 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1892 INT idStatic, UINT attrib )
1894 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );