include: Add constants for CryptProtectMemory/CryptUnprotectMemory.
[wine.git] / dlls / user32 / dialog.c
blob8b77ae2158b1c5af0b98636fc82fbfc6da3801fb
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/unicode.h"
38 #include "controls.h"
39 #include "win.h"
40 #include "user_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
46 /* Dialog control information */
47 typedef struct
49 DWORD style;
50 DWORD exStyle;
51 DWORD helpId;
52 INT16 x;
53 INT16 y;
54 INT16 cx;
55 INT16 cy;
56 UINT_PTR id;
57 LPCWSTR className;
58 LPCWSTR windowName;
59 LPCVOID data;
60 } DLG_CONTROL_INFO;
62 /* Dialog template */
63 typedef struct
65 DWORD style;
66 DWORD exStyle;
67 DWORD helpId;
68 UINT16 nbItems;
69 INT16 x;
70 INT16 y;
71 INT16 cx;
72 INT16 cy;
73 LPCWSTR menuName;
74 LPCWSTR className;
75 LPCWSTR caption;
76 INT16 pointSize;
77 WORD weight;
78 BOOL italic;
79 LPCWSTR faceName;
80 BOOL dialogEx;
81 } DLG_TEMPLATE;
83 /* Radio button group */
84 typedef struct
86 UINT firstID;
87 UINT lastID;
88 UINT checkID;
89 } RADIOGROUP;
92 /*********************************************************************
93 * dialog class descriptor
95 const struct builtin_class_descr DIALOG_builtin_class =
97 (LPCWSTR)DIALOG_CLASS_ATOM, /* name */
98 CS_SAVEBITS | CS_DBLCLKS, /* style */
99 WINPROC_DIALOG, /* proc */
100 DLGWINDOWEXTRA, /* extra */
101 IDC_ARROW, /* cursor */
102 0 /* brush */
106 /***********************************************************************
107 * DIALOG_GetControl32
109 * Return the class and text of the control pointed to by ptr,
110 * fill the header structure and return a pointer to the next control.
112 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
113 BOOL dialogEx )
115 if (dialogEx)
117 info->helpId = GET_DWORD(p); p += 2;
118 info->exStyle = GET_DWORD(p); p += 2;
119 info->style = GET_DWORD(p); p += 2;
121 else
123 info->helpId = 0;
124 info->style = GET_DWORD(p); p += 2;
125 info->exStyle = GET_DWORD(p); p += 2;
127 info->x = GET_WORD(p); p++;
128 info->y = GET_WORD(p); p++;
129 info->cx = GET_WORD(p); p++;
130 info->cy = GET_WORD(p); p++;
132 if (dialogEx)
134 /* id is a DWORD for DIALOGEX */
135 info->id = GET_DWORD(p);
136 p += 2;
138 else
140 info->id = GET_WORD(p);
141 p++;
144 if (GET_WORD(p) == 0xffff)
146 static const WCHAR class_names[6][10] =
148 { 'B','u','t','t','o','n', }, /* 0x80 */
149 { 'E','d','i','t', }, /* 0x81 */
150 { 'S','t','a','t','i','c', }, /* 0x82 */
151 { 'L','i','s','t','B','o','x', }, /* 0x83 */
152 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
153 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
155 WORD id = GET_WORD(p+1);
156 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
157 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
158 if (id <= 5)
159 info->className = class_names[id];
160 else
162 info->className = NULL;
163 ERR("Unknown built-in class id %04x\n", id );
165 p += 2;
167 else
169 info->className = p;
170 p += strlenW( info->className ) + 1;
173 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
175 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
176 p += 2;
178 else
180 info->windowName = p;
181 p += strlenW( info->windowName ) + 1;
184 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
185 debugstr_w( info->className ), debugstr_w( info->windowName ),
186 info->id, info->x, info->y, info->cx, info->cy,
187 info->style, info->exStyle, info->helpId );
189 if (GET_WORD(p))
191 if (TRACE_ON(dialog))
193 WORD i, count = GET_WORD(p) / sizeof(WORD);
194 TRACE(" BEGIN\n");
195 TRACE(" ");
196 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
197 TRACE("\n");
198 TRACE(" END\n" );
200 info->data = p + 1;
201 p += GET_WORD(p) / sizeof(WORD);
203 else info->data = NULL;
204 p++;
206 /* Next control is on dword boundary */
207 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
211 /***********************************************************************
212 * DIALOG_CreateControls32
214 * Create the control windows for a dialog.
216 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
217 HINSTANCE hInst, BOOL unicode )
219 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
220 DLG_CONTROL_INFO info;
221 HWND hwndCtrl, hwndDefButton = 0;
222 INT items = dlgTemplate->nbItems;
224 TRACE(" BEGIN\n" );
225 while (items--)
227 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
228 dlgTemplate->dialogEx );
229 info.style &= ~WS_POPUP;
230 info.style |= WS_CHILD;
232 if (info.style & WS_BORDER)
234 info.style &= ~WS_BORDER;
235 info.exStyle |= WS_EX_CLIENTEDGE;
237 if (unicode)
239 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
240 info.className, info.windowName,
241 info.style | WS_CHILD,
242 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
243 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
244 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
245 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
246 hwnd, (HMENU)info.id,
247 hInst, (LPVOID)info.data );
249 else
251 LPCSTR class = (LPCSTR)info.className;
252 LPCSTR caption = (LPCSTR)info.windowName;
253 LPSTR class_tmp = NULL;
254 LPSTR caption_tmp = NULL;
256 if (!IS_INTRESOURCE(class))
258 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
259 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
260 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
261 class = class_tmp;
263 if (!IS_INTRESOURCE(caption))
265 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
266 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
267 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
268 caption = caption_tmp;
270 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
271 class, caption, info.style | WS_CHILD,
272 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
273 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
274 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
275 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
276 hwnd, (HMENU)info.id,
277 hInst, (LPVOID)info.data );
278 HeapFree( GetProcessHeap(), 0, class_tmp );
279 HeapFree( GetProcessHeap(), 0, caption_tmp );
281 if (!hwndCtrl)
283 WARN("control %s %s creation failed\n", debugstr_w(info.className),
284 debugstr_w(info.windowName));
285 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
286 return FALSE;
289 /* Send initialisation messages to the control */
290 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
291 (WPARAM)dlgInfo->hUserFont, 0 );
292 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
294 /* If there's already a default push-button, set it back */
295 /* to normal and use this one instead. */
296 if (hwndDefButton)
297 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
298 hwndDefButton = hwndCtrl;
299 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
302 TRACE(" END\n" );
303 return TRUE;
307 /***********************************************************************
308 * DIALOG_ParseTemplate32
310 * Fill a DLG_TEMPLATE structure from the dialog template, and return
311 * a pointer to the first control.
313 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
315 const WORD *p = (const WORD *)template;
316 WORD signature;
317 WORD dlgver;
319 dlgver = GET_WORD(p); p++;
320 signature = GET_WORD(p); p++;
322 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
324 result->dialogEx = TRUE;
325 result->helpId = GET_DWORD(p); p += 2;
326 result->exStyle = GET_DWORD(p); p += 2;
327 result->style = GET_DWORD(p); p += 2;
329 else
331 result->style = GET_DWORD(p - 2);
332 result->dialogEx = FALSE;
333 result->helpId = 0;
334 result->exStyle = GET_DWORD(p); p += 2;
336 result->nbItems = GET_WORD(p); p++;
337 result->x = GET_WORD(p); p++;
338 result->y = GET_WORD(p); p++;
339 result->cx = GET_WORD(p); p++;
340 result->cy = GET_WORD(p); p++;
341 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
342 result->dialogEx ? "EX" : "", result->x, result->y,
343 result->cx, result->cy, result->helpId );
344 TRACE(" STYLE 0x%08x\n", result->style );
345 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
347 /* Get the menu name */
349 switch(GET_WORD(p))
351 case 0x0000:
352 result->menuName = NULL;
353 p++;
354 break;
355 case 0xffff:
356 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
357 p += 2;
358 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
359 break;
360 default:
361 result->menuName = p;
362 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
363 p += strlenW( result->menuName ) + 1;
364 break;
367 /* Get the class name */
369 switch(GET_WORD(p))
371 case 0x0000:
372 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
373 p++;
374 break;
375 case 0xffff:
376 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
377 p += 2;
378 TRACE(" CLASS %04x\n", LOWORD(result->className) );
379 break;
380 default:
381 result->className = p;
382 TRACE(" CLASS %s\n", debugstr_w( result->className ));
383 p += strlenW( result->className ) + 1;
384 break;
387 /* Get the window caption */
389 result->caption = p;
390 p += strlenW( result->caption ) + 1;
391 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
393 /* Get the font name */
395 result->pointSize = 0;
396 result->faceName = NULL;
397 result->weight = FW_DONTCARE;
398 result->italic = FALSE;
400 if (result->style & DS_SETFONT)
402 result->pointSize = GET_WORD(p);
403 p++;
405 /* If pointSize is 0x7fff, it means that we need to use the font
406 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
407 * italic, and facename from the dialog template.
409 if (result->pointSize == 0x7fff)
411 /* We could call SystemParametersInfo here, but then we'd have
412 * to convert from pixel size to point size (which can be
413 * imprecise).
415 TRACE(" FONT: Using message box font\n");
417 else
419 if (result->dialogEx)
421 result->weight = GET_WORD(p); p++;
422 result->italic = LOBYTE(GET_WORD(p)); p++;
424 result->faceName = p;
425 p += strlenW( result->faceName ) + 1;
427 TRACE(" FONT %d, %s, %d, %s\n",
428 result->pointSize, debugstr_w( result->faceName ),
429 result->weight, result->italic ? "TRUE" : "FALSE" );
433 /* First control is on dword boundary */
434 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
438 /***********************************************************************
439 * DIALOG_CreateIndirect
440 * Creates a dialog box window
442 * modal = TRUE if we are called from a modal dialog box.
443 * (it's more compatible to do it here, as under Windows the owner
444 * is never disabled if the dialog fails because of an invalid template)
446 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
447 HWND owner, DLGPROC dlgProc, LPARAM param,
448 BOOL unicode, HWND *modal_owner )
450 HWND hwnd;
451 RECT rect;
452 POINT pos;
453 SIZE size;
454 DLG_TEMPLATE template;
455 DIALOGINFO * dlgInfo;
456 DWORD units = GetDialogBaseUnits();
457 HWND disabled_owner = NULL;
458 HMENU hMenu = 0;
459 HFONT hUserFont = 0;
460 UINT flags = 0;
461 UINT xBaseUnit = LOWORD(units);
462 UINT yBaseUnit = HIWORD(units);
464 /* Parse dialog template */
466 if (!dlgTemplate) return 0;
467 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
469 /* Load menu */
471 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
473 /* Create custom font if needed */
475 if (template.style & DS_SETFONT)
477 HDC dc = GetDC(0);
479 if (template.pointSize == 0x7fff)
481 /* We get the message font from the non-client metrics */
482 NONCLIENTMETRICSW ncMetrics;
484 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
485 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
486 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
488 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
491 else
493 /* We convert the size to pixels and then make it -ve. This works
494 * for both +ve and -ve template.pointSize */
495 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
496 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
497 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
498 PROOF_QUALITY, FF_DONTCARE,
499 template.faceName );
502 if (hUserFont)
504 SIZE charSize;
505 HFONT hOldFont = SelectObject( dc, hUserFont );
506 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
507 if (charSize.cx)
509 xBaseUnit = charSize.cx;
510 yBaseUnit = charSize.cy;
512 SelectObject( dc, hOldFont );
514 ReleaseDC(0, dc);
515 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
518 /* Create dialog main window */
520 SetRect(&rect, 0, 0, MulDiv(template.cx, xBaseUnit, 4), MulDiv(template.cy, yBaseUnit, 8));
522 if (template.style & DS_CONTROL)
523 template.style &= ~(WS_CAPTION|WS_SYSMENU);
524 template.style |= DS_3DLOOK;
525 if (template.style & DS_MODALFRAME)
526 template.exStyle |= WS_EX_DLGMODALFRAME;
527 if ((template.style & DS_CONTROL) || !(template.style & WS_CHILD))
528 template.exStyle |= WS_EX_CONTROLPARENT;
529 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
530 pos.x = rect.left;
531 pos.y = rect.top;
532 size.cx = rect.right - rect.left;
533 size.cy = rect.bottom - rect.top;
535 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
537 pos.x = pos.y = CW_USEDEFAULT;
539 else
541 HMONITOR monitor = 0;
542 MONITORINFO mon_info;
544 mon_info.cbSize = sizeof(mon_info);
545 if (template.style & DS_CENTER)
547 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
548 GetMonitorInfoW( monitor, &mon_info );
549 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
550 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
552 else if (template.style & DS_CENTERMOUSE)
554 GetCursorPos( &pos );
555 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
556 GetMonitorInfoW( monitor, &mon_info );
558 else
560 pos.x += MulDiv(template.x, xBaseUnit, 4);
561 pos.y += MulDiv(template.y, yBaseUnit, 8);
562 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
564 if ( !(template.style & WS_CHILD) )
566 INT dX, dY;
568 /* try to fit it into the desktop */
570 if (!monitor)
572 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
573 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
574 GetMonitorInfoW( monitor, &mon_info );
576 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
577 pos.x -= dX;
578 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
579 pos.y -= dY;
580 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
581 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
585 if (modal_owner && owner)
587 HWND parent;
589 * Owner needs to be top level window. We need to duplicate the logic from server,
590 * because we need to disable it before creating dialog window. Note that we do that
591 * even if dialog has WS_CHILD, but only for modal dialogs, which matched what
592 * Windows does.
594 while ((GetWindowLongW( owner, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
596 parent = GetParent( owner );
597 if (!parent || parent == GetDesktopWindow()) break;
598 owner = parent;
600 *modal_owner = owner;
601 if (IsWindowEnabled( owner ))
603 disabled_owner = owner;
604 EnableWindow( disabled_owner, FALSE );
608 if (unicode)
610 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
611 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
612 owner, hMenu, hInst, NULL );
614 else
616 LPCSTR class = (LPCSTR)template.className;
617 LPCSTR caption = (LPCSTR)template.caption;
618 LPSTR class_tmp = NULL;
619 LPSTR caption_tmp = NULL;
621 if (!IS_INTRESOURCE(class))
623 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
624 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
625 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
626 class = class_tmp;
628 if (!IS_INTRESOURCE(caption))
630 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
631 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
632 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
633 caption = caption_tmp;
635 hwnd = CreateWindowExA(template.exStyle, class, caption,
636 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
637 owner, hMenu, hInst, NULL );
638 HeapFree( GetProcessHeap(), 0, class_tmp );
639 HeapFree( GetProcessHeap(), 0, caption_tmp );
642 if (!hwnd)
644 if (hUserFont) DeleteObject( hUserFont );
645 if (hMenu) DestroyMenu( hMenu );
646 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
647 return 0;
650 /* moved this from the top of the method to here as DIALOGINFO structure
651 will be valid only after WM_CREATE message has been handled in DefDlgProc
652 All the members of the structure get filled here using temp variables */
653 dlgInfo = DIALOG_get_info( hwnd, TRUE );
654 dlgInfo->hwndFocus = 0;
655 dlgInfo->hUserFont = hUserFont;
656 dlgInfo->hMenu = hMenu;
657 dlgInfo->xBaseUnit = xBaseUnit;
658 dlgInfo->yBaseUnit = yBaseUnit;
659 dlgInfo->flags = flags;
661 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
663 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
664 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
666 if (dlgProc && dlgInfo->hUserFont)
667 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
669 /* Create controls */
671 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
673 /* Send initialisation messages and set focus */
675 if (dlgProc)
677 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
678 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
679 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) && IsWindow( hwnd ) &&
680 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
682 /* By returning TRUE, app has requested a default focus assignment.
683 * WM_INITDIALOG may have changed the tab order, so find the first
684 * tabstop control again. */
685 focus = GetNextDlgTabItem( hwnd, 0, FALSE );
686 if (!focus) focus = GetNextDlgGroupItem( hwnd, 0, FALSE );
687 if (focus)
689 if (SendMessageW( focus, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
690 SendMessageW( focus, EM_SETSEL, 0, MAXLONG );
691 SetFocus( focus );
696 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
698 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
700 return hwnd;
702 if (disabled_owner) EnableWindow( disabled_owner, TRUE );
703 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
704 return 0;
708 /***********************************************************************
709 * CreateDialogParamA (USER32.@)
711 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
712 DLGPROC dlgProc, LPARAM param )
714 HRSRC hrsrc;
715 LPCDLGTEMPLATEA ptr;
717 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
718 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
719 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
723 /***********************************************************************
724 * CreateDialogParamW (USER32.@)
726 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
727 DLGPROC dlgProc, LPARAM param )
729 HRSRC hrsrc;
730 LPCDLGTEMPLATEA ptr;
732 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
733 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
734 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
738 /***********************************************************************
739 * CreateDialogIndirectParamAorW (USER32.@)
741 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
742 HWND owner, DLGPROC dlgProc, LPARAM param,
743 DWORD flags )
745 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
748 /***********************************************************************
749 * CreateDialogIndirectParamA (USER32.@)
751 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
752 HWND owner, DLGPROC dlgProc, LPARAM param )
754 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
757 /***********************************************************************
758 * CreateDialogIndirectParamW (USER32.@)
760 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
761 HWND owner, DLGPROC dlgProc, LPARAM param )
763 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
767 /***********************************************************************
768 * DIALOG_DoDialogBox
770 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
772 DIALOGINFO * dlgInfo;
773 MSG msg;
774 INT retval;
775 BOOL bFirstEmpty;
777 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
779 bFirstEmpty = TRUE;
780 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
782 for (;;)
784 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
786 if (bFirstEmpty)
788 /* ShowWindow the first time the queue goes empty */
789 ShowWindow( hwnd, SW_SHOWNORMAL );
790 bFirstEmpty = FALSE;
792 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
794 /* No message present -> send ENTERIDLE and wait */
795 SendMessageW( owner, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
797 GetMessageW( &msg, 0, 0, 0 );
800 if (msg.message == WM_QUIT)
802 PostQuitMessage( msg.wParam );
803 if (!IsWindow( hwnd )) return 0;
804 break;
806 if (!IsWindow( hwnd )) return 0;
807 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
809 TranslateMessage( &msg );
810 DispatchMessageW( &msg );
812 if (!IsWindow( hwnd )) return 0;
813 if (dlgInfo->flags & DF_END) break;
815 if (bFirstEmpty && msg.message == WM_TIMER)
817 ShowWindow( hwnd, SW_SHOWNORMAL );
818 bFirstEmpty = FALSE;
822 retval = dlgInfo->idResult;
823 DestroyWindow( hwnd );
824 return retval;
828 /***********************************************************************
829 * DialogBoxParamA (USER32.@)
831 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
832 HWND owner, DLGPROC dlgProc, LPARAM param )
834 HWND hwnd;
835 HRSRC hrsrc;
836 LPCDLGTEMPLATEA ptr;
838 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
839 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
840 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, &owner );
841 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
842 return 0;
846 /***********************************************************************
847 * DialogBoxParamW (USER32.@)
849 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
850 HWND owner, DLGPROC dlgProc, LPARAM param )
852 HWND hwnd;
853 HRSRC hrsrc;
854 LPCDLGTEMPLATEW ptr;
856 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
857 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
858 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, &owner );
859 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
860 return 0;
864 /***********************************************************************
865 * DialogBoxIndirectParamAorW (USER32.@)
867 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
868 HWND owner, DLGPROC dlgProc,
869 LPARAM param, DWORD flags )
871 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, &owner );
872 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
873 return -1;
876 /***********************************************************************
877 * DialogBoxIndirectParamA (USER32.@)
879 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
880 HWND owner, DLGPROC dlgProc, LPARAM param )
882 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
886 /***********************************************************************
887 * DialogBoxIndirectParamW (USER32.@)
889 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
890 HWND owner, DLGPROC dlgProc, LPARAM param )
892 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
895 /***********************************************************************
896 * EndDialog (USER32.@)
898 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
900 DIALOGINFO * dlgInfo;
901 HWND owner;
903 TRACE("%p %ld\n", hwnd, retval );
905 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
907 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
908 return FALSE;
910 dlgInfo->idResult = retval;
911 dlgInfo->flags |= DF_END;
913 owner = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
914 if (owner)
915 EnableWindow( owner, TRUE );
917 /* Windows sets the focus to the dialog itself in EndDialog */
919 if (IsChild(hwnd, GetFocus()))
920 SetFocus( hwnd );
922 /* Don't have to send a ShowWindow(SW_HIDE), just do
923 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
925 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
926 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
928 if (hwnd == GetActiveWindow())
930 /* If this dialog was given an owner then set the focus to that owner. */
931 if (owner)
932 SetForegroundWindow( owner );
933 else
934 WINPOS_ActivateOtherWindow( hwnd );
937 /* unblock dialog loop */
938 PostMessageA(hwnd, WM_NULL, 0, 0);
939 return TRUE;
943 /***********************************************************************
944 * DIALOG_IsAccelerator
946 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
948 HWND hwndControl = hwnd;
949 HWND hwndNext;
950 INT dlgCode;
951 WCHAR buffer[128];
955 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
956 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
958 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
959 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
960 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
962 /* find the accelerator key */
963 LPWSTR p = buffer - 2;
967 p = strchrW( p + 2, '&' );
969 while (p != NULL && p[1] == '&');
971 /* and check if it's the one we're looking for */
972 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
974 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
976 /* set focus to the control */
977 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
978 /* and bump it on to next */
979 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
981 else if (dlgCode & DLGC_BUTTON)
983 /* send BM_CLICK message to the control */
984 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
986 return TRUE;
989 hwndNext = GetWindow( hwndControl, GW_CHILD );
991 else hwndNext = 0;
993 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
995 while (!hwndNext && hwndControl)
997 hwndControl = GetParent( hwndControl );
998 if (hwndControl == hwndDlg)
1000 if(hwnd==hwndDlg) /* prevent endless loop */
1002 hwndNext=hwnd;
1003 break;
1005 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1007 else
1008 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1010 hwndControl = hwndNext;
1012 while (hwndControl && (hwndControl != hwnd));
1014 return FALSE;
1017 /***********************************************************************
1018 * DIALOG_FindMsgDestination
1020 * The messages that IsDialogMessage sends may not go to the dialog
1021 * calling IsDialogMessage if that dialog is a child, and it has the
1022 * DS_CONTROL style set.
1023 * We propagate up until we hit one that does not have DS_CONTROL, or
1024 * whose parent is not a dialog.
1026 * This is undocumented behaviour.
1028 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1030 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1032 WND *pParent;
1033 HWND hParent = GetParent(hwndDlg);
1034 if (!hParent) break;
1036 pParent = WIN_GetPtr(hParent);
1037 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
1039 if (!pParent->dlgInfo)
1041 WIN_ReleasePtr(pParent);
1042 break;
1044 WIN_ReleasePtr(pParent);
1046 hwndDlg = hParent;
1049 return hwndDlg;
1052 /***********************************************************************
1053 * DIALOG_FixOneChildOnChangeFocus
1055 * Callback helper for DIALOG_FixChildrenOnChangeFocus
1058 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
1059 LPARAM lParam)
1061 /* If a default pushbutton then no longer default */
1062 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
1063 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
1064 return TRUE;
1067 /***********************************************************************
1068 * DIALOG_FixChildrenOnChangeFocus
1070 * Following the change of focus that occurs for example after handling
1071 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1072 * children may be required.
1074 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1076 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1077 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1078 /* Windows does ask for this. I don't know why yet */
1080 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1082 /* If the button that is getting the focus WAS flagged as the default
1083 * pushbutton then ask the dialog what it thinks the default is and
1084 * set that in the default style.
1086 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1088 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1089 if (HIWORD(def_id) == DC_HASDEFID)
1091 HWND hwndDef;
1092 def_id = LOWORD(def_id);
1093 hwndDef = GetDlgItem (hwndDlg, def_id);
1094 if (hwndDef)
1096 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1097 /* I know that if it is a button then it should already be a
1098 * UNDEFPUSHBUTTON, since we have just told the buttons to
1099 * change style. But maybe they ignored our request
1101 if ((dlgcode_def & DLGC_BUTTON) &&
1102 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1104 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1109 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1111 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1112 /* I wonder why it doesn't send a DM_SETDEFID */
1116 /***********************************************************************
1117 * DIALOG_IdToHwnd
1119 * A recursive version of GetDlgItem
1121 * RETURNS
1122 * The HWND for a Child ID.
1124 static HWND DIALOG_IdToHwnd( HWND hwndDlg, INT id )
1126 int i;
1127 HWND *list = WIN_ListChildren( hwndDlg );
1128 HWND ret = 0;
1130 if (!list) return 0;
1132 for (i = 0; list[i]; i++)
1134 if (GetWindowLongPtrW( list[i], GWLP_ID ) == id)
1136 ret = list[i];
1137 break;
1140 /* Recurse into every child */
1141 if ((ret = DIALOG_IdToHwnd( list[i], id ))) break;
1144 HeapFree( GetProcessHeap(), 0, list );
1145 return ret;
1148 /***********************************************************************
1149 * IsDialogMessageW (USER32.@)
1151 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1153 INT dlgCode;
1155 if (!IsWindow( hwndDlg ))
1156 return FALSE;
1158 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1160 hwndDlg = WIN_GetFullHandle( hwndDlg );
1161 if (is_desktop_window(hwndDlg)) return FALSE;
1162 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1164 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1166 switch(msg->message)
1168 case WM_KEYDOWN:
1169 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1170 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1172 switch(msg->wParam)
1174 case VK_TAB:
1175 if (!(dlgCode & DLGC_WANTTAB))
1177 BOOL fIsDialog = TRUE;
1178 WND *pWnd = WIN_GetPtr( hwndDlg );
1180 if (pWnd && pWnd != WND_OTHER_PROCESS)
1182 fIsDialog = (pWnd->dlgInfo != NULL);
1183 WIN_ReleasePtr(pWnd);
1186 /* I am not sure under which circumstances the TAB is handled
1187 * each way. All I do know is that it does not always simply
1188 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1189 * do so but I presume someone has)
1191 if (fIsDialog)
1192 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1193 else
1195 /* It would appear that GetNextDlgTabItem can handle being
1196 * passed hwndDlg rather than NULL but that is undocumented
1197 * so let's do it properly
1199 HWND hwndFocus = GetFocus();
1200 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1201 hwndFocus == hwndDlg ? NULL : hwndFocus,
1202 GetKeyState (VK_SHIFT) & 0x8000);
1203 if (hwndNext)
1205 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1206 msg->wParam, (LPARAM)msg);
1207 if (dlgCode & DLGC_HASSETSEL)
1209 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1210 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1211 if (buffer)
1213 INT length;
1214 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1215 length = strlenW (buffer);
1216 HeapFree (GetProcessHeap(), 0, buffer);
1217 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1220 SetFocus (hwndNext);
1221 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1223 else
1224 return FALSE;
1226 return TRUE;
1228 break;
1230 case VK_RIGHT:
1231 case VK_DOWN:
1232 case VK_LEFT:
1233 case VK_UP:
1234 if (!(dlgCode & DLGC_WANTARROWS))
1236 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1237 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1238 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1239 return TRUE;
1241 break;
1243 case VK_CANCEL:
1244 case VK_ESCAPE:
1245 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1246 return TRUE;
1248 case VK_EXECUTE:
1249 case VK_RETURN:
1251 DWORD dw;
1252 if ((GetFocus() == msg->hwnd) &&
1253 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1255 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1257 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1259 HWND hwndDef = DIALOG_IdToHwnd(hwndDlg, LOWORD(dw));
1260 if (!hwndDef || IsWindowEnabled(hwndDef))
1261 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ), (LPARAM)hwndDef);
1263 else
1265 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1269 return TRUE;
1271 break;
1273 case WM_CHAR:
1274 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1275 * It does NOT get sent in the test program I have
1277 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1278 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1279 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1280 /* drop through */
1282 case WM_SYSCHAR:
1283 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1285 /* don't translate or dispatch */
1286 return TRUE;
1288 break;
1291 TranslateMessage( msg );
1292 DispatchMessageW( msg );
1293 return TRUE;
1297 /***********************************************************************
1298 * GetDlgCtrlID (USER32.@)
1300 INT WINAPI GetDlgCtrlID( HWND hwnd )
1302 return GetWindowLongPtrW( hwnd, GWLP_ID );
1306 /***********************************************************************
1307 * GetDlgItem (USER32.@)
1309 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1311 int i;
1312 HWND *list = WIN_ListChildren( hwndDlg );
1313 HWND ret = 0;
1315 if (!list) return 0;
1317 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1318 ret = list[i];
1319 HeapFree( GetProcessHeap(), 0, list );
1320 return ret;
1324 /*******************************************************************
1325 * SendDlgItemMessageA (USER32.@)
1327 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1328 WPARAM wParam, LPARAM lParam )
1330 HWND hwndCtrl = GetDlgItem( hwnd, id );
1331 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1332 else return 0;
1336 /*******************************************************************
1337 * SendDlgItemMessageW (USER32.@)
1339 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1340 WPARAM wParam, LPARAM lParam )
1342 HWND hwndCtrl = GetDlgItem( hwnd, id );
1343 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1344 else return 0;
1348 /*******************************************************************
1349 * SetDlgItemTextA (USER32.@)
1351 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1353 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1357 /*******************************************************************
1358 * SetDlgItemTextW (USER32.@)
1360 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1362 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1366 /***********************************************************************
1367 * GetDlgItemTextA (USER32.@)
1369 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1371 if (str && (len > 0)) str[0] = '\0';
1372 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1373 len, (LPARAM)str );
1377 /***********************************************************************
1378 * GetDlgItemTextW (USER32.@)
1380 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1382 if (str && (len > 0)) str[0] = '\0';
1383 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1384 len, (LPARAM)str );
1388 /*******************************************************************
1389 * SetDlgItemInt (USER32.@)
1391 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1392 BOOL fSigned )
1394 char str[20];
1396 if (fSigned) sprintf( str, "%d", (INT)value );
1397 else sprintf( str, "%u", value );
1398 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1399 return TRUE;
1403 /***********************************************************************
1404 * GetDlgItemInt (USER32.@)
1406 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1407 BOOL fSigned )
1409 char str[30];
1410 char * endptr;
1411 LONG_PTR result = 0;
1413 if (translated) *translated = FALSE;
1414 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1415 return 0;
1416 if (fSigned)
1418 result = strtol( str, &endptr, 10 );
1419 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1420 return 0;
1421 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1422 return 0;
1424 else
1426 result = strtoul( str, &endptr, 10 );
1427 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1428 return 0;
1429 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1431 if (translated) *translated = TRUE;
1432 return (UINT)result;
1436 /***********************************************************************
1437 * CheckDlgButton (USER32.@)
1439 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1441 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1442 return TRUE;
1446 /***********************************************************************
1447 * IsDlgButtonChecked (USER32.@)
1449 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1451 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1455 /***********************************************************************
1456 * CheckRB
1458 * Callback function used to check/uncheck radio buttons that fall
1459 * within a specific range of IDs.
1461 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1463 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1464 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1466 if ((lChildID >= lpRadioGroup->firstID) &&
1467 (lChildID <= lpRadioGroup->lastID))
1469 if (lChildID == lpRadioGroup->checkID)
1471 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1473 else
1475 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1479 return TRUE;
1483 /***********************************************************************
1484 * CheckRadioButton (USER32.@)
1486 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1487 int lastID, int checkID )
1489 RADIOGROUP radioGroup;
1491 radioGroup.firstID = firstID;
1492 radioGroup.lastID = lastID;
1493 radioGroup.checkID = checkID;
1495 return EnumChildWindows(hwndDlg, CheckRB, (LPARAM)&radioGroup);
1499 /***********************************************************************
1500 * GetDialogBaseUnits (USER.243)
1501 * GetDialogBaseUnits (USER32.@)
1503 DWORD WINAPI GetDialogBaseUnits(void)
1505 static DWORD units;
1507 if (!units)
1509 HDC hdc;
1510 SIZE size;
1512 if ((hdc = GetDC(0)))
1514 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1515 if (size.cx) units = MAKELONG( size.cx, size.cy );
1516 ReleaseDC( 0, hdc );
1518 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1520 return units;
1524 /***********************************************************************
1525 * MapDialogRect (USER32.@)
1527 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1529 DIALOGINFO * dlgInfo;
1530 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1531 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1532 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1533 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1534 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1535 return TRUE;
1539 /***********************************************************************
1540 * GetNextDlgGroupItem (USER32.@)
1542 * Corrections to MSDN documentation
1544 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1545 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1546 * 2. Prev of NULL or hwndDlg fails
1548 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1550 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1551 BOOL fLooped=FALSE;
1552 BOOL fSkipping=FALSE;
1554 hwndDlg = WIN_GetFullHandle( hwndDlg );
1555 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1557 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1558 if (!hwndCtrl && fPrevious) return 0;
1560 if (hwndCtrl)
1562 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1564 else
1566 /* No ctrl specified -> start from the beginning */
1567 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1568 /* MSDN is wrong. fPrevious does not result in the last child */
1570 /* Maybe that first one is valid. If so then we don't want to skip it*/
1571 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1573 return hwndCtrl;
1577 /* Always go forward around the group and list of controls; for the
1578 * previous control keep track; for the next break when you find one
1580 retvalue = hwndCtrl;
1581 hwnd = hwndCtrl;
1582 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1585 while (!hwndNext)
1587 /* Climb out until there is a next sibling of the ancestor or we
1588 * reach the top (in which case we loop back to the start)
1590 if (hwndDlg == GetParent (hwnd))
1592 /* Wrap around to the beginning of the list, within the same
1593 * group. (Once only)
1595 if (fLooped) goto end;
1596 fLooped = TRUE;
1597 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1599 else
1601 hwnd = GetParent (hwnd);
1602 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1605 hwnd = hwndNext;
1607 /* Wander down the leading edge of controlparents */
1608 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1609 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1610 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1611 hwnd = hwndNext;
1612 /* Question. If the control is a control parent but either has no
1613 * children or is not visible/enabled then if it has a WS_GROUP does
1614 * it count? For that matter does it count anyway?
1615 * I believe it doesn't count.
1618 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1620 hwndLastGroup = hwnd;
1621 if (!fSkipping)
1623 /* Look for the beginning of the group */
1624 fSkipping = TRUE;
1628 if (hwnd == hwndCtrl)
1630 if (!fSkipping) break;
1631 if (hwndLastGroup == hwnd) break;
1632 hwnd = hwndLastGroup;
1633 fSkipping = FALSE;
1634 fLooped = FALSE;
1637 if (!fSkipping &&
1638 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1639 WS_VISIBLE)
1641 retvalue = hwnd;
1642 if (!fPrevious) break;
1645 end:
1646 return retvalue;
1650 /***********************************************************************
1651 * DIALOG_GetNextTabItem
1653 * Recursive helper for GetNextDlgTabItem
1655 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1657 LONG dsStyle;
1658 LONG exStyle;
1659 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1660 HWND retWnd = 0;
1661 HWND hChildFirst = 0;
1663 if(!hwndCtrl)
1665 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1666 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1668 else if (IsChild( hwndMain, hwndCtrl ))
1670 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1671 if(!hChildFirst)
1673 if(GetParent(hwndCtrl) != hwndMain)
1674 /* i.e. if we are not at the top level of the recursion */
1675 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1676 else
1677 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1681 while(hChildFirst)
1683 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1684 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1685 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1687 HWND retWnd;
1688 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1689 if (retWnd) return (retWnd);
1691 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1693 return (hChildFirst);
1695 hChildFirst = GetWindow(hChildFirst,wndSearch);
1697 if(hwndCtrl)
1699 HWND hParent = GetParent(hwndCtrl);
1700 while(hParent)
1702 if(hParent == hwndMain) break;
1703 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1704 if(retWnd) break;
1705 hParent = GetParent(hParent);
1707 if(!retWnd)
1708 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1710 return retWnd ? retWnd : hwndCtrl;
1713 /***********************************************************************
1714 * GetNextDlgTabItem (USER32.@)
1716 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1717 BOOL fPrevious )
1719 hwndDlg = WIN_GetFullHandle( hwndDlg );
1720 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1722 /* Undocumented but tested under Win2000 and WinME */
1723 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1725 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1726 * NB GetLastError returns whatever was set before the function was
1727 * called.
1729 if (!hwndCtrl && fPrevious) return 0;
1731 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1734 /**********************************************************************
1735 * DIALOG_DlgDirSelect
1737 * Helper function for DlgDirSelect*
1739 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1740 INT id, BOOL unicode, BOOL combo )
1742 WCHAR *buffer, *ptr;
1743 INT item, size;
1744 BOOL ret;
1745 HWND listbox = GetDlgItem( hwnd, id );
1747 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1748 if (!listbox) return FALSE;
1750 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1751 if (item == LB_ERR) return FALSE;
1753 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1754 if (size == LB_ERR) return FALSE;
1756 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1758 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1760 if ((ret = (buffer[0] == '['))) /* drive or directory */
1762 if (buffer[1] == '-') /* drive */
1764 buffer[3] = ':';
1765 buffer[4] = 0;
1766 ptr = buffer + 2;
1768 else
1770 buffer[strlenW(buffer)-1] = '\\';
1771 ptr = buffer + 1;
1774 else
1776 /* Filenames without a dot extension must have one tacked at the end */
1777 if (strchrW(buffer, '.') == NULL)
1779 buffer[strlenW(buffer)+1] = '\0';
1780 buffer[strlenW(buffer)] = '.';
1782 ptr = buffer;
1785 if (!unicode)
1787 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1788 ((LPSTR)str)[len-1] = 0;
1790 else lstrcpynW( str, ptr, len );
1791 HeapFree( GetProcessHeap(), 0, buffer );
1792 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1793 return ret;
1797 /**********************************************************************
1798 * DIALOG_DlgDirListW
1800 * Helper function for DlgDirList*W
1802 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1803 INT idStatic, UINT attrib, BOOL combo )
1805 HWND hwnd;
1806 LPWSTR orig_spec = spec;
1807 WCHAR any[] = {'*','.','*',0};
1809 #define SENDMSG(msg,wparam,lparam) \
1810 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1811 : SendMessageW( hwnd, msg, wparam, lparam ))
1813 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1815 /* If the path exists and is a directory, chdir to it */
1816 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1817 else
1819 WCHAR *p, *p2;
1820 p = spec;
1821 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1822 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1823 if ((p2 = strrchrW( p, '/' ))) p = p2;
1824 if (p != spec)
1826 WCHAR sep = *p;
1827 *p = 0;
1828 if (!SetCurrentDirectoryW( spec ))
1830 *p = sep; /* Restore the original spec */
1831 return FALSE;
1833 spec = p + 1;
1837 TRACE( "mask=%s\n", debugstr_w(spec) );
1839 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1841 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1843 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1844 if (attrib & DDL_DIRECTORY)
1846 if (!(attrib & DDL_EXCLUSIVE))
1848 SENDMSG( combo ? CB_DIR : LB_DIR,
1849 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1850 (LPARAM)spec );
1852 SENDMSG( combo ? CB_DIR : LB_DIR,
1853 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1854 (LPARAM)any );
1856 else
1858 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1862 /* Convert path specification to uppercase */
1863 if (spec) CharUpperW(spec);
1865 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1867 WCHAR temp[MAX_PATH];
1868 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1869 CharLowerW( temp );
1870 /* Can't use PostMessage() here, because the string is on the stack */
1871 SetDlgItemTextW( hDlg, idStatic, temp );
1874 if (orig_spec && (spec != orig_spec))
1876 /* Update the original file spec */
1877 WCHAR *p = spec;
1878 while ((*orig_spec++ = *p++));
1881 return TRUE;
1882 #undef SENDMSG
1886 /**********************************************************************
1887 * DIALOG_DlgDirListA
1889 * Helper function for DlgDirList*A
1891 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1892 INT idStatic, UINT attrib, BOOL combo )
1894 if (spec)
1896 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1897 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1898 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1899 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1900 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1901 HeapFree( GetProcessHeap(), 0, specW );
1902 return ret;
1904 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1908 /**********************************************************************
1909 * DlgDirSelectExA (USER32.@)
1911 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1913 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1917 /**********************************************************************
1918 * DlgDirSelectExW (USER32.@)
1920 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1922 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1926 /**********************************************************************
1927 * DlgDirSelectComboBoxExA (USER32.@)
1929 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1930 INT id )
1932 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1936 /**********************************************************************
1937 * DlgDirSelectComboBoxExW (USER32.@)
1939 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1940 INT id)
1942 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1946 /**********************************************************************
1947 * DlgDirListA (USER32.@)
1949 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1950 INT idStatic, UINT attrib )
1952 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1956 /**********************************************************************
1957 * DlgDirListW (USER32.@)
1959 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1960 INT idStatic, UINT attrib )
1962 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1966 /**********************************************************************
1967 * DlgDirListComboBoxA (USER32.@)
1969 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1970 INT idStatic, UINT attrib )
1972 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1976 /**********************************************************************
1977 * DlgDirListComboBoxW (USER32.@)
1979 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1980 INT idStatic, UINT attrib )
1982 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );