gdiplus: Fix swapped got-expected in brush test.
[wine/multimedia.git] / dlls / user32 / dialog.c
blob444a73dcb3d9bb7acdda3aaa4df1bb15c2a52be8
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
47 /* Dialog control information */
48 typedef struct
50 DWORD style;
51 DWORD exStyle;
52 DWORD helpId;
53 INT16 x;
54 INT16 y;
55 INT16 cx;
56 INT16 cy;
57 UINT_PTR id;
58 LPCWSTR className;
59 LPCWSTR windowName;
60 LPCVOID data;
61 } DLG_CONTROL_INFO;
63 /* Dialog template */
64 typedef struct
66 DWORD style;
67 DWORD exStyle;
68 DWORD helpId;
69 UINT16 nbItems;
70 INT16 x;
71 INT16 y;
72 INT16 cx;
73 INT16 cy;
74 LPCWSTR menuName;
75 LPCWSTR className;
76 LPCWSTR caption;
77 INT16 pointSize;
78 WORD weight;
79 BOOL italic;
80 LPCWSTR faceName;
81 BOOL dialogEx;
82 } DLG_TEMPLATE;
84 /* Radio button group */
85 typedef struct
87 UINT firstID;
88 UINT lastID;
89 UINT checkID;
90 } RADIOGROUP;
93 /*********************************************************************
94 * dialog class descriptor
96 const struct builtin_class_descr DIALOG_builtin_class =
98 (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
99 CS_SAVEBITS | CS_DBLCLKS, /* style */
100 DefDlgProcA, /* procA */
101 DefDlgProcW, /* procW */
102 DLGWINDOWEXTRA, /* extra */
103 IDC_ARROW, /* cursor */
104 0 /* brush */
108 /***********************************************************************
109 * DIALOG_EnableOwner
111 * Helper function for modal dialogs to enable again the
112 * owner of the dialog box.
114 void DIALOG_EnableOwner( HWND hOwner )
116 /* Owner must be a top-level window */
117 if (hOwner)
118 hOwner = GetAncestor( hOwner, GA_ROOT );
119 if (!hOwner) return;
120 EnableWindow( hOwner, TRUE );
124 /***********************************************************************
125 * DIALOG_DisableOwner
127 * Helper function for modal dialogs to disable the
128 * owner of the dialog box. Returns TRUE if owner was enabled.
130 BOOL DIALOG_DisableOwner( HWND hOwner )
132 /* Owner must be a top-level window */
133 if (hOwner)
134 hOwner = GetAncestor( hOwner, GA_ROOT );
135 if (!hOwner) return FALSE;
136 if (IsWindowEnabled( hOwner ))
138 EnableWindow( hOwner, FALSE );
139 return TRUE;
141 else
142 return FALSE;
145 /***********************************************************************
146 * DIALOG_GetControl32
148 * Return the class and text of the control pointed to by ptr,
149 * fill the header structure and return a pointer to the next control.
151 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
152 BOOL dialogEx )
154 if (dialogEx)
156 info->helpId = GET_DWORD(p); p += 2;
157 info->exStyle = GET_DWORD(p); p += 2;
158 info->style = GET_DWORD(p); p += 2;
160 else
162 info->helpId = 0;
163 info->style = GET_DWORD(p); p += 2;
164 info->exStyle = GET_DWORD(p); p += 2;
166 info->x = GET_WORD(p); p++;
167 info->y = GET_WORD(p); p++;
168 info->cx = GET_WORD(p); p++;
169 info->cy = GET_WORD(p); p++;
171 if (dialogEx)
173 /* id is a DWORD for DIALOGEX */
174 info->id = GET_DWORD(p);
175 p += 2;
177 else
179 info->id = GET_WORD(p);
180 p++;
183 if (GET_WORD(p) == 0xffff)
185 static const WCHAR class_names[6][10] =
187 { 'B','u','t','t','o','n', }, /* 0x80 */
188 { 'E','d','i','t', }, /* 0x81 */
189 { 'S','t','a','t','i','c', }, /* 0x82 */
190 { 'L','i','s','t','B','o','x', }, /* 0x83 */
191 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
192 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
194 WORD id = GET_WORD(p+1);
195 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
196 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
197 if (id <= 5)
198 info->className = class_names[id];
199 else
201 info->className = NULL;
202 ERR("Unknown built-in class id %04x\n", id );
204 p += 2;
206 else
208 info->className = (LPCWSTR)p;
209 p += strlenW( info->className ) + 1;
212 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
214 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
215 p += 2;
217 else
219 info->windowName = (LPCWSTR)p;
220 p += strlenW( info->windowName ) + 1;
223 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
224 debugstr_w( info->className ), debugstr_w( info->windowName ),
225 info->id, info->x, info->y, info->cx, info->cy,
226 info->style, info->exStyle, info->helpId );
228 if (GET_WORD(p))
230 if (TRACE_ON(dialog))
232 WORD i, count = GET_WORD(p) / sizeof(WORD);
233 TRACE(" BEGIN\n");
234 TRACE(" ");
235 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
236 TRACE("\n");
237 TRACE(" END\n" );
239 info->data = p + 1;
240 p += GET_WORD(p) / sizeof(WORD);
242 else info->data = NULL;
243 p++;
245 /* Next control is on dword boundary */
246 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
250 /***********************************************************************
251 * DIALOG_CreateControls32
253 * Create the control windows for a dialog.
255 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
256 HINSTANCE hInst, BOOL unicode )
258 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
259 DLG_CONTROL_INFO info;
260 HWND hwndCtrl, hwndDefButton = 0;
261 INT items = dlgTemplate->nbItems;
263 TRACE(" BEGIN\n" );
264 while (items--)
266 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
267 dlgTemplate->dialogEx );
268 /* Is this it? */
269 if (info.style & WS_BORDER)
271 info.style &= ~WS_BORDER;
272 info.exStyle |= WS_EX_CLIENTEDGE;
274 if (unicode)
276 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
277 info.className, info.windowName,
278 info.style | WS_CHILD,
279 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
281 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
282 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
283 hwnd, (HMENU)info.id,
284 hInst, (LPVOID)info.data );
286 else
288 LPCSTR class = (LPCSTR)info.className;
289 LPCSTR caption = (LPCSTR)info.windowName;
290 LPSTR class_tmp = NULL;
291 LPSTR caption_tmp = NULL;
293 if (HIWORD(class))
295 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
296 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
297 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
298 class = class_tmp;
300 if (HIWORD(caption))
302 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
303 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
304 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
305 caption = caption_tmp;
307 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
308 class, caption, info.style | WS_CHILD,
309 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
310 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
311 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
312 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
313 hwnd, (HMENU)info.id,
314 hInst, (LPVOID)info.data );
315 HeapFree( GetProcessHeap(), 0, class_tmp );
316 HeapFree( GetProcessHeap(), 0, caption_tmp );
318 if (!hwndCtrl)
320 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
321 return FALSE;
324 /* Send initialisation messages to the control */
325 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
326 (WPARAM)dlgInfo->hUserFont, 0 );
327 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
329 /* If there's already a default push-button, set it back */
330 /* to normal and use this one instead. */
331 if (hwndDefButton)
332 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
333 hwndDefButton = hwndCtrl;
334 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
337 TRACE(" END\n" );
338 return TRUE;
342 /***********************************************************************
343 * DIALOG_ParseTemplate32
345 * Fill a DLG_TEMPLATE structure from the dialog template, and return
346 * a pointer to the first control.
348 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
350 const WORD *p = (const WORD *)template;
351 WORD signature;
352 WORD dlgver;
354 signature = GET_WORD(p); p++;
355 dlgver = GET_WORD(p); p++;
357 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
359 result->dialogEx = TRUE;
360 result->helpId = GET_DWORD(p); p += 2;
361 result->exStyle = GET_DWORD(p); p += 2;
362 result->style = GET_DWORD(p); p += 2;
364 else
366 result->style = GET_DWORD(p - 2);
367 result->dialogEx = FALSE;
368 result->helpId = 0;
369 result->exStyle = GET_DWORD(p); p += 2;
371 result->nbItems = GET_WORD(p); p++;
372 result->x = GET_WORD(p); p++;
373 result->y = GET_WORD(p); p++;
374 result->cx = GET_WORD(p); p++;
375 result->cy = GET_WORD(p); p++;
376 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
377 result->dialogEx ? "EX" : "", result->x, result->y,
378 result->cx, result->cy, result->helpId );
379 TRACE(" STYLE 0x%08x\n", result->style );
380 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
382 /* Get the menu name */
384 switch(GET_WORD(p))
386 case 0x0000:
387 result->menuName = NULL;
388 p++;
389 break;
390 case 0xffff:
391 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
392 p += 2;
393 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
394 break;
395 default:
396 result->menuName = (LPCWSTR)p;
397 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
398 p += strlenW( result->menuName ) + 1;
399 break;
402 /* Get the class name */
404 switch(GET_WORD(p))
406 case 0x0000:
407 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
408 p++;
409 break;
410 case 0xffff:
411 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
412 p += 2;
413 TRACE(" CLASS %04x\n", LOWORD(result->className) );
414 break;
415 default:
416 result->className = (LPCWSTR)p;
417 TRACE(" CLASS %s\n", debugstr_w( result->className ));
418 p += strlenW( result->className ) + 1;
419 break;
422 /* Get the window caption */
424 result->caption = (LPCWSTR)p;
425 p += strlenW( result->caption ) + 1;
426 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
428 /* Get the font name */
430 result->pointSize = 0;
431 result->faceName = NULL;
432 result->weight = FW_DONTCARE;
433 result->italic = FALSE;
435 if (result->style & DS_SETFONT)
437 result->pointSize = GET_WORD(p);
438 p++;
439 if (result->dialogEx)
441 result->weight = GET_WORD(p); p++;
442 result->italic = LOBYTE(GET_WORD(p)); p++;
444 result->faceName = (LPCWSTR)p;
445 p += strlenW( result->faceName ) + 1;
446 TRACE(" FONT %d, %s, %d, %s\n",
447 result->pointSize, debugstr_w( result->faceName ),
448 result->weight, result->italic ? "TRUE" : "FALSE" );
451 /* First control is on dword boundary */
452 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
456 /***********************************************************************
457 * DIALOG_CreateIndirect
458 * Creates a dialog box window
460 * modal = TRUE if we are called from a modal dialog box.
461 * (it's more compatible to do it here, as under Windows the owner
462 * is never disabled if the dialog fails because of an invalid template)
464 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
465 HWND owner, DLGPROC dlgProc, LPARAM param,
466 BOOL unicode, BOOL modal )
468 HWND hwnd;
469 RECT rect;
470 POINT pos;
471 SIZE size;
472 DLG_TEMPLATE template;
473 DIALOGINFO * dlgInfo = NULL;
474 DWORD units = GetDialogBaseUnits();
475 BOOL ownerEnabled = TRUE;
476 HMENU hMenu = 0;
477 HFONT hUserFont = 0;
478 UINT flags = 0;
479 UINT xBaseUnit = LOWORD(units);
480 UINT yBaseUnit = HIWORD(units);
482 /* Parse dialog template */
484 if (!dlgTemplate) return 0;
485 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
487 /* Load menu */
489 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
491 /* Create custom font if needed */
493 if (template.style & DS_SETFONT)
495 /* We convert the size to pixels and then make it -ve. This works
496 * for both +ve and -ve template.pointSize */
497 HDC dc;
498 int pixels;
499 dc = GetDC(0);
500 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
501 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
502 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
503 PROOF_QUALITY, FF_DONTCARE,
504 template.faceName );
505 if (hUserFont)
507 SIZE charSize;
508 HFONT hOldFont = SelectObject( dc, hUserFont );
509 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
510 if (charSize.cx)
512 xBaseUnit = charSize.cx;
513 yBaseUnit = charSize.cy;
515 SelectObject( dc, hOldFont );
517 ReleaseDC(0, dc);
518 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
521 /* Create dialog main window */
523 rect.left = rect.top = 0;
524 rect.right = MulDiv(template.cx, xBaseUnit, 4);
525 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
526 if (template.style & WS_CHILD)
527 template.style &= ~(WS_CAPTION|WS_SYSMENU);
528 if (template.style & DS_MODALFRAME)
529 template.exStyle |= WS_EX_DLGMODALFRAME;
530 if (template.style & DS_CONTROL)
531 template.exStyle |= WS_EX_CONTROLPARENT;
532 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
533 pos.x = rect.left;
534 pos.y = rect.top;
535 size.cx = rect.right - rect.left;
536 size.cy = rect.bottom - rect.top;
538 if (template.x == CW_USEDEFAULT16)
540 pos.x = pos.y = CW_USEDEFAULT;
542 else
544 HMONITOR monitor = 0;
545 MONITORINFO mon_info;
547 mon_info.cbSize = sizeof(mon_info);
548 if (template.style & DS_CENTER)
550 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
551 GetMonitorInfoW( monitor, &mon_info );
552 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
553 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
555 else if (template.style & DS_CENTERMOUSE)
557 GetCursorPos( &pos );
558 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
559 GetMonitorInfoW( monitor, &mon_info );
561 else
563 pos.x += MulDiv(template.x, xBaseUnit, 4);
564 pos.y += MulDiv(template.y, yBaseUnit, 8);
565 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
567 if ( !(template.style & WS_CHILD) )
569 INT dX, dY;
571 /* try to fit it into the desktop */
573 if (!monitor)
575 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
576 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
577 GetMonitorInfoW( monitor, &mon_info );
579 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
580 pos.x -= dX;
581 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
582 pos.y -= dY;
583 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
584 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
588 if (modal)
590 ownerEnabled = DIALOG_DisableOwner( owner );
591 if (ownerEnabled) flags |= DF_OWNERENABLED;
594 if (unicode)
596 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
597 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
598 owner, hMenu, hInst, NULL );
600 else
602 LPCSTR class = (LPCSTR)template.className;
603 LPCSTR caption = (LPCSTR)template.caption;
604 LPSTR class_tmp = NULL;
605 LPSTR caption_tmp = NULL;
607 if (HIWORD(class))
609 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
610 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
611 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
612 class = class_tmp;
614 if (HIWORD(caption))
616 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
617 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
618 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
619 caption = caption_tmp;
621 hwnd = CreateWindowExA(template.exStyle, class, caption,
622 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
623 owner, hMenu, hInst, NULL );
624 HeapFree( GetProcessHeap(), 0, class_tmp );
625 HeapFree( GetProcessHeap(), 0, caption_tmp );
628 if (!hwnd)
630 if (hUserFont) DeleteObject( hUserFont );
631 if (hMenu) DestroyMenu( hMenu );
632 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
633 return 0;
636 /* moved this from the top of the method to here as DIALOGINFO structure
637 will be valid only after WM_CREATE message has been handled in DefDlgProc
638 All the members of the structure get filled here using temp variables */
639 dlgInfo = DIALOG_get_info( hwnd, TRUE );
640 dlgInfo->hwndFocus = 0;
641 dlgInfo->hUserFont = hUserFont;
642 dlgInfo->hMenu = hMenu;
643 dlgInfo->xBaseUnit = xBaseUnit;
644 dlgInfo->yBaseUnit = yBaseUnit;
645 dlgInfo->idResult = 0;
646 dlgInfo->flags = flags;
647 dlgInfo->hDialogHeap = 0;
649 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
651 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
652 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
654 if (dlgProc && dlgInfo->hUserFont)
655 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
657 /* Create controls */
659 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
661 /* Send initialisation messages and set focus */
663 if (dlgProc)
665 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
666 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
668 /* By returning TRUE, app has requested a default focus assignment */
669 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
670 if( dlgInfo->hwndFocus )
671 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 -1;
807 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return -1;
808 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
809 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
810 return 0;
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 -1;
825 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return -1;
826 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
827 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
828 return 0;
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 %ld\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_GetPtr(hParent);
999 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1001 if (!(pParent->flags & WIN_ISDIALOG))
1003 WIN_ReleasePtr(pParent);
1004 break;
1006 WIN_ReleasePtr(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 if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
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 (is_desktop_window(hwndDlg)) return FALSE;
1089 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1091 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1093 switch(msg->message)
1095 case WM_KEYDOWN:
1096 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1097 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1099 switch(msg->wParam)
1101 case VK_TAB:
1102 if (!(dlgCode & DLGC_WANTTAB))
1104 BOOL fIsDialog = TRUE;
1105 WND *pWnd = WIN_GetPtr( hwndDlg );
1107 if (pWnd && pWnd != WND_OTHER_PROCESS)
1109 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1110 WIN_ReleasePtr(pWnd);
1113 /* I am not sure under which circumstances the TAB is handled
1114 * each way. All I do know is that it does not always simply
1115 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1116 * do so but I presume someone has)
1118 if (fIsDialog)
1119 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1120 else
1122 /* It would appear that GetNextDlgTabItem can handle being
1123 * passed hwndDlg rather than NULL but that is undocumented
1124 * so let's do it properly
1126 HWND hwndFocus = GetFocus();
1127 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1128 hwndFocus == hwndDlg ? NULL : hwndFocus,
1129 GetKeyState (VK_SHIFT) & 0x8000);
1130 if (hwndNext)
1132 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1133 msg->wParam, (LPARAM)msg);
1134 if (dlgCode & DLGC_HASSETSEL)
1136 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1137 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1138 if (buffer)
1140 INT length;
1141 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1142 length = strlenW (buffer);
1143 HeapFree (GetProcessHeap(), 0, buffer);
1144 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1147 SetFocus (hwndNext);
1148 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1150 else
1151 return FALSE;
1153 return TRUE;
1155 break;
1157 case VK_RIGHT:
1158 case VK_DOWN:
1159 case VK_LEFT:
1160 case VK_UP:
1161 if (!(dlgCode & DLGC_WANTARROWS))
1163 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1164 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1165 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1166 return TRUE;
1168 break;
1170 case VK_CANCEL:
1171 case VK_ESCAPE:
1172 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1173 return TRUE;
1175 case VK_EXECUTE:
1176 case VK_RETURN:
1178 DWORD dw;
1179 if ((GetFocus() == msg->hwnd) &&
1180 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1182 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1184 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1186 HWND hwndDef = GetDlgItem(hwndDlg, LOWORD(dw));
1187 if (!hwndDef || !IsWindowEnabled(hwndDef))
1188 return TRUE;
1189 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1190 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1192 else
1194 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1198 return TRUE;
1200 break;
1202 case WM_CHAR:
1203 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1204 * It does NOT get sent in the test program I have
1206 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1207 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1208 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1209 /* drop through */
1211 case WM_SYSCHAR:
1212 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1214 /* don't translate or dispatch */
1215 return TRUE;
1217 break;
1220 TranslateMessage( msg );
1221 DispatchMessageW( msg );
1222 return TRUE;
1226 /***********************************************************************
1227 * GetDlgCtrlID (USER32.@)
1229 INT WINAPI GetDlgCtrlID( HWND hwnd )
1231 return GetWindowLongPtrW( hwnd, GWLP_ID );
1235 /***********************************************************************
1236 * GetDlgItem (USER32.@)
1238 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1240 int i;
1241 HWND *list = WIN_ListChildren( hwndDlg );
1242 HWND ret = 0;
1244 if (!list) return 0;
1246 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1247 ret = list[i];
1248 HeapFree( GetProcessHeap(), 0, list );
1249 return ret;
1253 /*******************************************************************
1254 * SendDlgItemMessageA (USER32.@)
1256 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1257 WPARAM wParam, LPARAM lParam )
1259 HWND hwndCtrl = GetDlgItem( hwnd, id );
1260 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1261 else return 0;
1265 /*******************************************************************
1266 * SendDlgItemMessageW (USER32.@)
1268 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1269 WPARAM wParam, LPARAM lParam )
1271 HWND hwndCtrl = GetDlgItem( hwnd, id );
1272 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1273 else return 0;
1277 /*******************************************************************
1278 * SetDlgItemTextA (USER32.@)
1280 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1282 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1286 /*******************************************************************
1287 * SetDlgItemTextW (USER32.@)
1289 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1291 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1295 /***********************************************************************
1296 * GetDlgItemTextA (USER32.@)
1298 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1300 if (str && (len > 0)) str[0] = '\0';
1301 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1302 len, (LPARAM)str );
1306 /***********************************************************************
1307 * GetDlgItemTextW (USER32.@)
1309 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1311 if (str && (len > 0)) str[0] = '\0';
1312 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1313 len, (LPARAM)str );
1317 /*******************************************************************
1318 * SetDlgItemInt (USER32.@)
1320 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1321 BOOL fSigned )
1323 char str[20];
1325 if (fSigned) sprintf( str, "%d", (INT)value );
1326 else sprintf( str, "%u", value );
1327 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1328 return TRUE;
1332 /***********************************************************************
1333 * GetDlgItemInt (USER32.@)
1335 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1336 BOOL fSigned )
1338 char str[30];
1339 char * endptr;
1340 long result = 0;
1342 if (translated) *translated = FALSE;
1343 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1344 return 0;
1345 if (fSigned)
1347 result = strtol( str, &endptr, 10 );
1348 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1349 return 0;
1350 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1351 return 0;
1353 else
1355 result = strtoul( str, &endptr, 10 );
1356 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1357 return 0;
1358 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1360 if (translated) *translated = TRUE;
1361 return (UINT)result;
1365 /***********************************************************************
1366 * CheckDlgButton (USER32.@)
1368 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1370 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1371 return TRUE;
1375 /***********************************************************************
1376 * IsDlgButtonChecked (USER32.@)
1378 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1380 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1384 /***********************************************************************
1385 * CheckRB
1387 * Callback function used to check/uncheck radio buttons that fall
1388 * within a specific range of IDs.
1390 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1392 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1393 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1395 if ((lChildID >= lpRadioGroup->firstID) &&
1396 (lChildID <= lpRadioGroup->lastID))
1398 if (lChildID == lpRadioGroup->checkID)
1400 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1402 else
1404 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1408 return TRUE;
1412 /***********************************************************************
1413 * CheckRadioButton (USER32.@)
1415 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1416 int lastID, int checkID )
1418 RADIOGROUP radioGroup;
1420 radioGroup.firstID = firstID;
1421 radioGroup.lastID = lastID;
1422 radioGroup.checkID = checkID;
1424 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1425 (LPARAM)&radioGroup);
1429 /***********************************************************************
1430 * GetDialogBaseUnits (USER.243)
1431 * GetDialogBaseUnits (USER32.@)
1433 DWORD WINAPI GetDialogBaseUnits(void)
1435 static DWORD units;
1437 if (!units)
1439 HDC hdc;
1440 SIZE size;
1442 if ((hdc = GetDC(0)))
1444 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1445 if (size.cx) units = MAKELONG( size.cx, size.cy );
1446 ReleaseDC( 0, hdc );
1448 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1450 return units;
1454 /***********************************************************************
1455 * MapDialogRect (USER32.@)
1457 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1459 DIALOGINFO * dlgInfo;
1460 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1461 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1462 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1463 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1464 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1465 return TRUE;
1469 /***********************************************************************
1470 * GetNextDlgGroupItem (USER32.@)
1472 * Corrections to MSDN documentation
1474 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1475 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1476 * 2. Prev of NULL or hwndDlg fails
1478 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1480 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1481 BOOL fLooped=FALSE;
1482 BOOL fSkipping=FALSE;
1484 hwndDlg = WIN_GetFullHandle( hwndDlg );
1485 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1487 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1488 if (!hwndCtrl && fPrevious) return 0;
1490 if (hwndCtrl)
1492 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1494 else
1496 /* No ctrl specified -> start from the beginning */
1497 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1498 /* MSDN is wrong. fPrevious does not result in the last child */
1500 /* Maybe that first one is valid. If so then we don't want to skip it*/
1501 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1503 return hwndCtrl;
1507 /* Always go forward around the group and list of controls; for the
1508 * previous control keep track; for the next break when you find one
1510 retvalue = hwndCtrl;
1511 hwnd = hwndCtrl;
1512 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1515 while (!hwndNext)
1517 /* Climb out until there is a next sibling of the ancestor or we
1518 * reach the top (in which case we loop back to the start)
1520 if (hwndDlg == GetParent (hwnd))
1522 /* Wrap around to the beginning of the list, within the same
1523 * group. (Once only)
1525 if (fLooped) goto end;
1526 fLooped = TRUE;
1527 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1529 else
1531 hwnd = GetParent (hwnd);
1532 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1535 hwnd = hwndNext;
1537 /* Wander down the leading edge of controlparents */
1538 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1539 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1540 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1541 hwnd = hwndNext;
1542 /* Question. If the control is a control parent but either has no
1543 * children or is not visible/enabled then if it has a WS_GROUP does
1544 * it count? For that matter does it count anyway?
1545 * I believe it doesn't count.
1548 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1550 hwndLastGroup = hwnd;
1551 if (!fSkipping)
1553 /* Look for the beginning of the group */
1554 fSkipping = TRUE;
1558 if (hwnd == hwndCtrl)
1560 if (!fSkipping) break;
1561 if (hwndLastGroup == hwnd) break;
1562 hwnd = hwndLastGroup;
1563 fSkipping = FALSE;
1564 fLooped = FALSE;
1567 if (!fSkipping &&
1568 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1569 WS_VISIBLE)
1571 retvalue = hwnd;
1572 if (!fPrevious) break;
1575 end:
1576 return retvalue;
1580 /***********************************************************************
1581 * DIALOG_GetNextTabItem
1583 * Recursive helper for GetNextDlgTabItem
1585 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1587 LONG dsStyle;
1588 LONG exStyle;
1589 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1590 HWND retWnd = 0;
1591 HWND hChildFirst = 0;
1593 if(!hwndCtrl)
1595 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1596 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1598 else if (IsChild( hwndMain, hwndCtrl ))
1600 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1601 if(!hChildFirst)
1603 if(GetParent(hwndCtrl) != hwndMain)
1604 /* i.e. if we are not at the top level of the recursion */
1605 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1606 else
1607 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1611 while(hChildFirst)
1613 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1614 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1615 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1617 HWND retWnd;
1618 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1619 if (retWnd) return (retWnd);
1621 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1623 return (hChildFirst);
1625 hChildFirst = GetWindow(hChildFirst,wndSearch);
1627 if(hwndCtrl)
1629 HWND hParent = GetParent(hwndCtrl);
1630 while(hParent)
1632 if(hParent == hwndMain) break;
1633 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1634 if(retWnd) break;
1635 hParent = GetParent(hParent);
1637 if(!retWnd)
1638 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1640 return retWnd ? retWnd : hwndCtrl;
1643 /***********************************************************************
1644 * GetNextDlgTabItem (USER32.@)
1646 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1647 BOOL fPrevious )
1649 hwndDlg = WIN_GetFullHandle( hwndDlg );
1650 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1652 /* Undocumented but tested under Win2000 and WinME */
1653 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1655 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1656 * NB GetLastError returns whatever was set before the function was
1657 * called.
1659 if (!hwndCtrl && fPrevious) return 0;
1661 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1664 /**********************************************************************
1665 * DIALOG_DlgDirSelect
1667 * Helper function for DlgDirSelect*
1669 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1670 INT id, BOOL unicode, BOOL combo )
1672 WCHAR *buffer, *ptr;
1673 INT item, size;
1674 BOOL ret;
1675 HWND listbox = GetDlgItem( hwnd, id );
1677 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1678 if (!listbox) return FALSE;
1680 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1681 if (item == LB_ERR) return FALSE;
1683 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1684 if (size == LB_ERR) return FALSE;
1686 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1688 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1690 if ((ret = (buffer[0] == '['))) /* drive or directory */
1692 if (buffer[1] == '-') /* drive */
1694 buffer[3] = ':';
1695 buffer[4] = 0;
1696 ptr = buffer + 2;
1698 else
1700 buffer[strlenW(buffer)-1] = '\\';
1701 ptr = buffer + 1;
1704 else
1706 /* Filenames without a dot extension must have one tacked at the end */
1707 if (strchrW(buffer, '.') == NULL)
1709 buffer[strlenW(buffer)+1] = '\0';
1710 buffer[strlenW(buffer)] = '.';
1712 ptr = buffer;
1715 if (!unicode)
1717 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1718 ((LPSTR)str)[len-1] = 0;
1720 else lstrcpynW( str, ptr, len );
1721 HeapFree( GetProcessHeap(), 0, buffer );
1722 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1723 return ret;
1727 /**********************************************************************
1728 * DIALOG_DlgDirListW
1730 * Helper function for DlgDirList*W
1732 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1733 INT idStatic, UINT attrib, BOOL combo )
1735 HWND hwnd;
1736 LPWSTR orig_spec = spec;
1737 WCHAR any[] = {'*','.','*',0};
1739 #define SENDMSG(msg,wparam,lparam) \
1740 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1741 : SendMessageW( hwnd, msg, wparam, lparam ))
1743 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1745 /* If the path exists and is a directory, chdir to it */
1746 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1747 else
1749 WCHAR *p, *p2;
1750 p = spec;
1751 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1752 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1753 if ((p2 = strrchrW( p, '/' ))) p = p2;
1754 if (p != spec)
1756 WCHAR sep = *p;
1757 *p = 0;
1758 if (!SetCurrentDirectoryW( spec ))
1760 *p = sep; /* Restore the original spec */
1761 return FALSE;
1763 spec = p + 1;
1767 TRACE( "mask=%s\n", debugstr_w(spec) );
1769 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1771 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1773 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1774 if (attrib & DDL_DIRECTORY)
1776 if (!(attrib & DDL_EXCLUSIVE))
1778 SENDMSG( combo ? CB_DIR : LB_DIR,
1779 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1780 (LPARAM)spec );
1782 SENDMSG( combo ? CB_DIR : LB_DIR,
1783 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1784 (LPARAM)any );
1786 else
1788 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1792 /* Convert path specification to uppercase */
1793 if (spec) CharUpperW(spec);
1795 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1797 WCHAR temp[MAX_PATH];
1798 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1799 CharLowerW( temp );
1800 /* Can't use PostMessage() here, because the string is on the stack */
1801 SetDlgItemTextW( hDlg, idStatic, temp );
1804 if (orig_spec && (spec != orig_spec))
1806 /* Update the original file spec */
1807 WCHAR *p = spec;
1808 while ((*orig_spec++ = *p++));
1811 return TRUE;
1812 #undef SENDMSG
1816 /**********************************************************************
1817 * DIALOG_DlgDirListA
1819 * Helper function for DlgDirList*A
1821 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1822 INT idStatic, UINT attrib, BOOL combo )
1824 if (spec)
1826 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1827 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1828 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1829 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1830 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1831 HeapFree( GetProcessHeap(), 0, specW );
1832 return ret;
1834 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1838 /**********************************************************************
1839 * DlgDirSelectExA (USER32.@)
1841 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1843 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1847 /**********************************************************************
1848 * DlgDirSelectExW (USER32.@)
1850 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1852 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1856 /**********************************************************************
1857 * DlgDirSelectComboBoxExA (USER32.@)
1859 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1860 INT id )
1862 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1866 /**********************************************************************
1867 * DlgDirSelectComboBoxExW (USER32.@)
1869 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1870 INT id)
1872 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1876 /**********************************************************************
1877 * DlgDirListA (USER32.@)
1879 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1880 INT idStatic, UINT attrib )
1882 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1886 /**********************************************************************
1887 * DlgDirListW (USER32.@)
1889 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1890 INT idStatic, UINT attrib )
1892 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1896 /**********************************************************************
1897 * DlgDirListComboBoxA (USER32.@)
1899 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1900 INT idStatic, UINT attrib )
1902 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1906 /**********************************************************************
1907 * DlgDirListComboBoxW (USER32.@)
1909 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1910 INT idStatic, UINT attrib )
1912 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );