winedump: Print the network share name in .lnk files.
[wine/multimedia.git] / dlls / user32 / dialog.c
blobaf241a202e34c9029d937c0a10be13ee6d208b2b
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_EnableOwner
109 * Helper function for modal dialogs to enable again the
110 * owner of the dialog box.
112 static void DIALOG_EnableOwner( HWND hOwner )
114 /* Owner must be a top-level window */
115 if (hOwner)
116 hOwner = GetAncestor( hOwner, GA_ROOT );
117 if (!hOwner) return;
118 EnableWindow( hOwner, TRUE );
122 /***********************************************************************
123 * DIALOG_DisableOwner
125 * Helper function for modal dialogs to disable the
126 * owner of the dialog box. Returns TRUE if owner was enabled.
128 static BOOL DIALOG_DisableOwner( HWND hOwner )
130 /* Owner must be a top-level window */
131 if (hOwner)
132 hOwner = GetAncestor( hOwner, GA_ROOT );
133 if (!hOwner) return FALSE;
134 if (IsWindowEnabled( hOwner ))
136 EnableWindow( hOwner, FALSE );
137 return TRUE;
139 else
140 return FALSE;
143 /***********************************************************************
144 * DIALOG_GetControl32
146 * Return the class and text of the control pointed to by ptr,
147 * fill the header structure and return a pointer to the next control.
149 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
150 BOOL dialogEx )
152 if (dialogEx)
154 info->helpId = GET_DWORD(p); p += 2;
155 info->exStyle = GET_DWORD(p); p += 2;
156 info->style = GET_DWORD(p); p += 2;
158 else
160 info->helpId = 0;
161 info->style = GET_DWORD(p); p += 2;
162 info->exStyle = GET_DWORD(p); p += 2;
164 info->x = GET_WORD(p); p++;
165 info->y = GET_WORD(p); p++;
166 info->cx = GET_WORD(p); p++;
167 info->cy = GET_WORD(p); p++;
169 if (dialogEx)
171 /* id is a DWORD for DIALOGEX */
172 info->id = GET_DWORD(p);
173 p += 2;
175 else
177 info->id = GET_WORD(p);
178 p++;
181 if (GET_WORD(p) == 0xffff)
183 static const WCHAR class_names[6][10] =
185 { 'B','u','t','t','o','n', }, /* 0x80 */
186 { 'E','d','i','t', }, /* 0x81 */
187 { 'S','t','a','t','i','c', }, /* 0x82 */
188 { 'L','i','s','t','B','o','x', }, /* 0x83 */
189 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
190 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
192 WORD id = GET_WORD(p+1);
193 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
194 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
195 if (id <= 5)
196 info->className = class_names[id];
197 else
199 info->className = NULL;
200 ERR("Unknown built-in class id %04x\n", id );
202 p += 2;
204 else
206 info->className = p;
207 p += strlenW( info->className ) + 1;
210 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
212 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
213 p += 2;
215 else
217 info->windowName = p;
218 p += strlenW( info->windowName ) + 1;
221 TRACE(" %s %s %ld, %d, %d, %d, %d, %08x, %08x, %08x\n",
222 debugstr_w( info->className ), debugstr_w( info->windowName ),
223 info->id, info->x, info->y, info->cx, info->cy,
224 info->style, info->exStyle, info->helpId );
226 if (GET_WORD(p))
228 if (TRACE_ON(dialog))
230 WORD i, count = GET_WORD(p) / sizeof(WORD);
231 TRACE(" BEGIN\n");
232 TRACE(" ");
233 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
234 TRACE("\n");
235 TRACE(" END\n" );
237 info->data = p + 1;
238 p += GET_WORD(p) / sizeof(WORD);
240 else info->data = NULL;
241 p++;
243 /* Next control is on dword boundary */
244 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
248 /***********************************************************************
249 * DIALOG_CreateControls32
251 * Create the control windows for a dialog.
253 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
254 HINSTANCE hInst, BOOL unicode )
256 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
257 DLG_CONTROL_INFO info;
258 HWND hwndCtrl, hwndDefButton = 0;
259 INT items = dlgTemplate->nbItems;
261 TRACE(" BEGIN\n" );
262 while (items--)
264 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
265 dlgTemplate->dialogEx );
266 /* Is this it? */
267 if (info.style & WS_BORDER)
269 info.style &= ~WS_BORDER;
270 info.exStyle |= WS_EX_CLIENTEDGE;
272 if (unicode)
274 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
275 info.className, info.windowName,
276 info.style | WS_CHILD,
277 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
278 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
279 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
280 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
281 hwnd, (HMENU)info.id,
282 hInst, (LPVOID)info.data );
284 else
286 LPCSTR class = (LPCSTR)info.className;
287 LPCSTR caption = (LPCSTR)info.windowName;
288 LPSTR class_tmp = NULL;
289 LPSTR caption_tmp = NULL;
291 if (!IS_INTRESOURCE(class))
293 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
294 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
295 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class_tmp, len, NULL, NULL );
296 class = class_tmp;
298 if (!IS_INTRESOURCE(caption))
300 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
301 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
302 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption_tmp, len, NULL, NULL );
303 caption = caption_tmp;
305 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
306 class, caption, info.style | WS_CHILD,
307 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
308 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
309 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
310 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
311 hwnd, (HMENU)info.id,
312 hInst, (LPVOID)info.data );
313 HeapFree( GetProcessHeap(), 0, class_tmp );
314 HeapFree( GetProcessHeap(), 0, caption_tmp );
316 if (!hwndCtrl)
318 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
319 return FALSE;
322 /* Send initialisation messages to the control */
323 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
324 (WPARAM)dlgInfo->hUserFont, 0 );
325 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
327 /* If there's already a default push-button, set it back */
328 /* to normal and use this one instead. */
329 if (hwndDefButton)
330 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
331 hwndDefButton = hwndCtrl;
332 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
335 TRACE(" END\n" );
336 return TRUE;
340 /***********************************************************************
341 * DIALOG_ParseTemplate32
343 * Fill a DLG_TEMPLATE structure from the dialog template, and return
344 * a pointer to the first control.
346 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
348 const WORD *p = (const WORD *)template;
349 WORD signature;
350 WORD dlgver;
352 dlgver = GET_WORD(p); p++;
353 signature = GET_WORD(p); p++;
355 if (dlgver == 1 && signature == 0xffff) /* DIALOGEX resource */
357 result->dialogEx = TRUE;
358 result->helpId = GET_DWORD(p); p += 2;
359 result->exStyle = GET_DWORD(p); p += 2;
360 result->style = GET_DWORD(p); p += 2;
362 else
364 result->style = GET_DWORD(p - 2);
365 result->dialogEx = FALSE;
366 result->helpId = 0;
367 result->exStyle = GET_DWORD(p); p += 2;
369 result->nbItems = GET_WORD(p); p++;
370 result->x = GET_WORD(p); p++;
371 result->y = GET_WORD(p); p++;
372 result->cx = GET_WORD(p); p++;
373 result->cy = GET_WORD(p); p++;
374 TRACE("DIALOG%s %d, %d, %d, %d, %d\n",
375 result->dialogEx ? "EX" : "", result->x, result->y,
376 result->cx, result->cy, result->helpId );
377 TRACE(" STYLE 0x%08x\n", result->style );
378 TRACE(" EXSTYLE 0x%08x\n", result->exStyle );
380 /* Get the menu name */
382 switch(GET_WORD(p))
384 case 0x0000:
385 result->menuName = NULL;
386 p++;
387 break;
388 case 0xffff:
389 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
390 p += 2;
391 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
392 break;
393 default:
394 result->menuName = p;
395 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
396 p += strlenW( result->menuName ) + 1;
397 break;
400 /* Get the class name */
402 switch(GET_WORD(p))
404 case 0x0000:
405 result->className = (LPCWSTR)DIALOG_CLASS_ATOM;
406 p++;
407 break;
408 case 0xffff:
409 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
410 p += 2;
411 TRACE(" CLASS %04x\n", LOWORD(result->className) );
412 break;
413 default:
414 result->className = p;
415 TRACE(" CLASS %s\n", debugstr_w( result->className ));
416 p += strlenW( result->className ) + 1;
417 break;
420 /* Get the window caption */
422 result->caption = p;
423 p += strlenW( result->caption ) + 1;
424 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
426 /* Get the font name */
428 result->pointSize = 0;
429 result->faceName = NULL;
430 result->weight = FW_DONTCARE;
431 result->italic = FALSE;
433 if (result->style & DS_SETFONT)
435 result->pointSize = GET_WORD(p);
436 p++;
438 /* If pointSize is 0x7fff, it means that we need to use the font
439 * in NONCLIENTMETRICSW.lfMessageFont, and NOT read the weight,
440 * italic, and facename from the dialog template.
442 if (result->pointSize == 0x7fff)
444 /* We could call SystemParametersInfo here, but then we'd have
445 * to convert from pixel size to point size (which can be
446 * imprecise).
448 TRACE(" FONT: Using message box font\n");
450 else
452 if (result->dialogEx)
454 result->weight = GET_WORD(p); p++;
455 result->italic = LOBYTE(GET_WORD(p)); p++;
457 result->faceName = p;
458 p += strlenW( result->faceName ) + 1;
460 TRACE(" FONT %d, %s, %d, %s\n",
461 result->pointSize, debugstr_w( result->faceName ),
462 result->weight, result->italic ? "TRUE" : "FALSE" );
466 /* First control is on dword boundary */
467 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
471 /***********************************************************************
472 * DIALOG_CreateIndirect
473 * Creates a dialog box window
475 * modal = TRUE if we are called from a modal dialog box.
476 * (it's more compatible to do it here, as under Windows the owner
477 * is never disabled if the dialog fails because of an invalid template)
479 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
480 HWND owner, DLGPROC dlgProc, LPARAM param,
481 BOOL unicode, BOOL modal )
483 HWND hwnd;
484 RECT rect;
485 POINT pos;
486 SIZE size;
487 DLG_TEMPLATE template;
488 DIALOGINFO * dlgInfo = NULL;
489 DWORD units = GetDialogBaseUnits();
490 BOOL ownerEnabled = TRUE;
491 HMENU hMenu = 0;
492 HFONT hUserFont = 0;
493 UINT flags = 0;
494 UINT xBaseUnit = LOWORD(units);
495 UINT yBaseUnit = HIWORD(units);
497 /* Parse dialog template */
499 if (!dlgTemplate) return 0;
500 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
501 template.exStyle = 0; /* exstyle is ignored */
503 /* Load menu */
505 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
507 /* Create custom font if needed */
509 if (template.style & DS_SETFONT)
511 HDC dc = GetDC(0);
513 if (template.pointSize == 0x7fff)
515 /* We get the message font from the non-client metrics */
516 NONCLIENTMETRICSW ncMetrics;
518 ncMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
519 if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
520 sizeof(NONCLIENTMETRICSW), &ncMetrics, 0))
522 hUserFont = CreateFontIndirectW( &ncMetrics.lfMessageFont );
525 else
527 /* We convert the size to pixels and then make it -ve. This works
528 * for both +ve and -ve template.pointSize */
529 int pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
530 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
531 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
532 PROOF_QUALITY, FF_DONTCARE,
533 template.faceName );
536 if (hUserFont)
538 SIZE charSize;
539 HFONT hOldFont = SelectObject( dc, hUserFont );
540 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
541 if (charSize.cx)
543 xBaseUnit = charSize.cx;
544 yBaseUnit = charSize.cy;
546 SelectObject( dc, hOldFont );
548 ReleaseDC(0, dc);
549 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
552 /* Create dialog main window */
554 rect.left = rect.top = 0;
555 rect.right = MulDiv(template.cx, xBaseUnit, 4);
556 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
557 if (template.style & WS_CHILD)
558 template.style &= ~(WS_CAPTION|WS_SYSMENU);
559 if (template.style & DS_MODALFRAME)
560 template.exStyle |= WS_EX_DLGMODALFRAME;
561 if (template.style & DS_CONTROL)
562 template.exStyle |= WS_EX_CONTROLPARENT;
563 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
564 pos.x = rect.left;
565 pos.y = rect.top;
566 size.cx = rect.right - rect.left;
567 size.cy = rect.bottom - rect.top;
569 if (template.x == (SHORT)0x8000 /*CW_USEDEFAULT16*/)
571 pos.x = pos.y = CW_USEDEFAULT;
573 else
575 HMONITOR monitor = 0;
576 MONITORINFO mon_info;
578 mon_info.cbSize = sizeof(mon_info);
579 if (template.style & DS_CENTER)
581 monitor = MonitorFromWindow( owner ? owner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY );
582 GetMonitorInfoW( monitor, &mon_info );
583 pos.x = (mon_info.rcWork.left + mon_info.rcWork.right - size.cx) / 2;
584 pos.y = (mon_info.rcWork.top + mon_info.rcWork.bottom - size.cy) / 2;
586 else if (template.style & DS_CENTERMOUSE)
588 GetCursorPos( &pos );
589 monitor = MonitorFromPoint( pos, MONITOR_DEFAULTTOPRIMARY );
590 GetMonitorInfoW( monitor, &mon_info );
592 else
594 pos.x += MulDiv(template.x, xBaseUnit, 4);
595 pos.y += MulDiv(template.y, yBaseUnit, 8);
596 if (!(template.style & (WS_CHILD|DS_ABSALIGN))) ClientToScreen( owner, &pos );
598 if ( !(template.style & WS_CHILD) )
600 INT dX, dY;
602 /* try to fit it into the desktop */
604 if (!monitor)
606 SetRect( &rect, pos.x, pos.y, pos.x + size.cx, pos.y + size.cy );
607 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
608 GetMonitorInfoW( monitor, &mon_info );
610 if ((dX = pos.x + size.cx + GetSystemMetrics(SM_CXDLGFRAME) - mon_info.rcWork.right) > 0)
611 pos.x -= dX;
612 if ((dY = pos.y + size.cy + GetSystemMetrics(SM_CYDLGFRAME) - mon_info.rcWork.bottom) > 0)
613 pos.y -= dY;
614 if( pos.x < mon_info.rcWork.left ) pos.x = mon_info.rcWork.left;
615 if( pos.y < mon_info.rcWork.top ) pos.y = mon_info.rcWork.top;
619 if (modal)
621 ownerEnabled = DIALOG_DisableOwner( owner );
622 if (ownerEnabled) flags |= DF_OWNERENABLED;
625 if (unicode)
627 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
628 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
629 owner, hMenu, hInst, NULL );
631 else
633 LPCSTR class = (LPCSTR)template.className;
634 LPCSTR caption = (LPCSTR)template.caption;
635 LPSTR class_tmp = NULL;
636 LPSTR caption_tmp = NULL;
638 if (!IS_INTRESOURCE(class))
640 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
641 class_tmp = HeapAlloc( GetProcessHeap(), 0, len );
642 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class_tmp, len, NULL, NULL );
643 class = class_tmp;
645 if (!IS_INTRESOURCE(caption))
647 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
648 caption_tmp = HeapAlloc( GetProcessHeap(), 0, len );
649 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption_tmp, len, NULL, NULL );
650 caption = caption_tmp;
652 hwnd = CreateWindowExA(template.exStyle, class, caption,
653 template.style & ~WS_VISIBLE, pos.x, pos.y, size.cx, size.cy,
654 owner, hMenu, hInst, NULL );
655 HeapFree( GetProcessHeap(), 0, class_tmp );
656 HeapFree( GetProcessHeap(), 0, caption_tmp );
659 if (!hwnd)
661 if (hUserFont) DeleteObject( hUserFont );
662 if (hMenu) DestroyMenu( hMenu );
663 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
664 return 0;
667 /* moved this from the top of the method to here as DIALOGINFO structure
668 will be valid only after WM_CREATE message has been handled in DefDlgProc
669 All the members of the structure get filled here using temp variables */
670 dlgInfo = DIALOG_get_info( hwnd, TRUE );
671 dlgInfo->hwndFocus = 0;
672 dlgInfo->hUserFont = hUserFont;
673 dlgInfo->hMenu = hMenu;
674 dlgInfo->xBaseUnit = xBaseUnit;
675 dlgInfo->yBaseUnit = yBaseUnit;
676 dlgInfo->flags = flags;
678 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
680 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
681 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
683 if (dlgProc && dlgInfo->hUserFont)
684 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
686 /* Create controls */
688 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
690 /* Send initialisation messages and set focus */
692 if (dlgProc)
694 HWND focus = GetNextDlgTabItem( hwnd, 0, FALSE );
695 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)focus, param ) &&
696 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
698 /* By returning TRUE, app has requested a default focus assignment.
699 * WM_INITDIALOG may have changed the tab order, so find the first
700 * tabstop control again. */
701 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
702 if( dlgInfo->hwndFocus )
703 SetFocus( dlgInfo->hwndFocus );
707 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
709 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
711 return hwnd;
713 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
714 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
715 return 0;
719 /***********************************************************************
720 * CreateDialogParamA (USER32.@)
722 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
723 DLGPROC dlgProc, LPARAM param )
725 HRSRC hrsrc;
726 LPCDLGTEMPLATEA ptr;
728 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
729 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
730 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
734 /***********************************************************************
735 * CreateDialogParamW (USER32.@)
737 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
738 DLGPROC dlgProc, LPARAM param )
740 HRSRC hrsrc;
741 LPCDLGTEMPLATEA ptr;
743 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
744 if (!(ptr = LoadResource(hInst, hrsrc))) return 0;
745 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
749 /***********************************************************************
750 * CreateDialogIndirectParamAorW (USER32.@)
752 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
753 HWND owner, DLGPROC dlgProc, LPARAM param,
754 DWORD flags )
756 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
759 /***********************************************************************
760 * CreateDialogIndirectParamA (USER32.@)
762 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
763 HWND owner, DLGPROC dlgProc, LPARAM param )
765 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
768 /***********************************************************************
769 * CreateDialogIndirectParamW (USER32.@)
771 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
772 HWND owner, DLGPROC dlgProc, LPARAM param )
774 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
778 /***********************************************************************
779 * DIALOG_DoDialogBox
781 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
783 DIALOGINFO * dlgInfo;
784 MSG msg;
785 INT retval;
786 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
787 BOOL bFirstEmpty;
789 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
791 bFirstEmpty = TRUE;
792 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
794 for (;;)
796 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
798 if (bFirstEmpty)
800 /* ShowWindow the first time the queue goes empty */
801 ShowWindow( hwnd, SW_SHOWNORMAL );
802 bFirstEmpty = FALSE;
804 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
806 /* No message present -> send ENTERIDLE and wait */
807 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
809 if (!GetMessageW( &msg, 0, 0, 0 )) break;
812 if (!IsWindow( hwnd )) return 0;
813 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
815 TranslateMessage( &msg );
816 DispatchMessageW( &msg );
818 if (dlgInfo->flags & DF_END) break;
820 if (bFirstEmpty && msg.message == WM_TIMER)
822 ShowWindow( hwnd, SW_SHOWNORMAL );
823 bFirstEmpty = FALSE;
827 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
828 retval = dlgInfo->idResult;
829 DestroyWindow( hwnd );
830 return retval;
834 /***********************************************************************
835 * DialogBoxParamA (USER32.@)
837 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
838 HWND owner, DLGPROC dlgProc, LPARAM param )
840 HWND hwnd;
841 HRSRC hrsrc;
842 LPCDLGTEMPLATEA ptr;
844 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return -1;
845 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
846 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
847 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
848 return 0;
852 /***********************************************************************
853 * DialogBoxParamW (USER32.@)
855 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
856 HWND owner, DLGPROC dlgProc, LPARAM param )
858 HWND hwnd;
859 HRSRC hrsrc;
860 LPCDLGTEMPLATEW ptr;
862 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return -1;
863 if (!(ptr = LoadResource(hInst, hrsrc))) return -1;
864 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
865 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
866 return 0;
870 /***********************************************************************
871 * DialogBoxIndirectParamAorW (USER32.@)
873 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
874 HWND owner, DLGPROC dlgProc,
875 LPARAM param, DWORD flags )
877 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
878 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
879 return -1;
882 /***********************************************************************
883 * DialogBoxIndirectParamA (USER32.@)
885 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
886 HWND owner, DLGPROC dlgProc, LPARAM param )
888 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
892 /***********************************************************************
893 * DialogBoxIndirectParamW (USER32.@)
895 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
896 HWND owner, DLGPROC dlgProc, LPARAM param )
898 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
901 /***********************************************************************
902 * EndDialog (USER32.@)
904 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
906 BOOL wasEnabled = TRUE;
907 DIALOGINFO * dlgInfo;
908 HWND owner;
910 TRACE("%p %ld\n", hwnd, retval );
912 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
914 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
915 return FALSE;
917 dlgInfo->idResult = retval;
918 dlgInfo->flags |= DF_END;
919 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
921 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
922 DIALOG_EnableOwner( owner );
924 /* Windows sets the focus to the dialog itself in EndDialog */
926 if (IsChild(hwnd, GetFocus()))
927 SetFocus( hwnd );
929 /* Don't have to send a ShowWindow(SW_HIDE), just do
930 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
932 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
933 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
935 if (hwnd == GetActiveWindow()) 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 * IsDialogMessageW (USER32.@)
1119 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1121 INT dlgCode = 0;
1123 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1125 hwndDlg = WIN_GetFullHandle( hwndDlg );
1126 if (is_desktop_window(hwndDlg)) return FALSE;
1127 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1129 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1131 switch(msg->message)
1133 case WM_KEYDOWN:
1134 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1135 if (dlgCode & (DLGC_WANTMESSAGE)) break;
1137 switch(msg->wParam)
1139 case VK_TAB:
1140 if (!(dlgCode & DLGC_WANTTAB))
1142 BOOL fIsDialog = TRUE;
1143 WND *pWnd = WIN_GetPtr( hwndDlg );
1145 if (pWnd && pWnd != WND_OTHER_PROCESS)
1147 fIsDialog = (pWnd->dlgInfo != NULL);
1148 WIN_ReleasePtr(pWnd);
1151 /* I am not sure under which circumstances the TAB is handled
1152 * each way. All I do know is that it does not always simply
1153 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1154 * do so but I presume someone has)
1156 if (fIsDialog)
1157 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1158 else
1160 /* It would appear that GetNextDlgTabItem can handle being
1161 * passed hwndDlg rather than NULL but that is undocumented
1162 * so let's do it properly
1164 HWND hwndFocus = GetFocus();
1165 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1166 hwndFocus == hwndDlg ? NULL : hwndFocus,
1167 GetKeyState (VK_SHIFT) & 0x8000);
1168 if (hwndNext)
1170 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1171 msg->wParam, (LPARAM)msg);
1172 if (dlgCode & DLGC_HASSETSEL)
1174 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1175 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1176 if (buffer)
1178 INT length;
1179 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1180 length = strlenW (buffer);
1181 HeapFree (GetProcessHeap(), 0, buffer);
1182 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1185 SetFocus (hwndNext);
1186 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1188 else
1189 return FALSE;
1191 return TRUE;
1193 break;
1195 case VK_RIGHT:
1196 case VK_DOWN:
1197 case VK_LEFT:
1198 case VK_UP:
1199 if (!(dlgCode & DLGC_WANTARROWS))
1201 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1202 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1203 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1204 return TRUE;
1206 break;
1208 case VK_CANCEL:
1209 case VK_ESCAPE:
1210 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1211 return TRUE;
1213 case VK_EXECUTE:
1214 case VK_RETURN:
1216 DWORD dw;
1217 if ((GetFocus() == msg->hwnd) &&
1218 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1220 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1222 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1224 HWND hwndDef = GetDlgItem(hwndDlg, LOWORD(dw));
1225 if (!hwndDef || !IsWindowEnabled(hwndDef))
1226 return TRUE;
1227 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1228 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1230 else
1232 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1236 return TRUE;
1238 break;
1240 case WM_CHAR:
1241 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1242 * It does NOT get sent in the test program I have
1244 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1245 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1246 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1247 /* drop through */
1249 case WM_SYSCHAR:
1250 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1252 /* don't translate or dispatch */
1253 return TRUE;
1255 break;
1258 TranslateMessage( msg );
1259 DispatchMessageW( msg );
1260 return TRUE;
1264 /***********************************************************************
1265 * GetDlgCtrlID (USER32.@)
1267 INT WINAPI GetDlgCtrlID( HWND hwnd )
1269 return GetWindowLongPtrW( hwnd, GWLP_ID );
1273 /***********************************************************************
1274 * GetDlgItem (USER32.@)
1276 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1278 int i;
1279 HWND *list = WIN_ListChildren( hwndDlg );
1280 HWND ret = 0;
1282 if (!list) return 0;
1284 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1285 ret = list[i];
1286 HeapFree( GetProcessHeap(), 0, list );
1287 return ret;
1291 /*******************************************************************
1292 * SendDlgItemMessageA (USER32.@)
1294 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1295 WPARAM wParam, LPARAM lParam )
1297 HWND hwndCtrl = GetDlgItem( hwnd, id );
1298 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1299 else return 0;
1303 /*******************************************************************
1304 * SendDlgItemMessageW (USER32.@)
1306 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1307 WPARAM wParam, LPARAM lParam )
1309 HWND hwndCtrl = GetDlgItem( hwnd, id );
1310 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1311 else return 0;
1315 /*******************************************************************
1316 * SetDlgItemTextA (USER32.@)
1318 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1320 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1324 /*******************************************************************
1325 * SetDlgItemTextW (USER32.@)
1327 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1329 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1333 /***********************************************************************
1334 * GetDlgItemTextA (USER32.@)
1336 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1338 if (str && (len > 0)) str[0] = '\0';
1339 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1340 len, (LPARAM)str );
1344 /***********************************************************************
1345 * GetDlgItemTextW (USER32.@)
1347 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1349 if (str && (len > 0)) str[0] = '\0';
1350 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1351 len, (LPARAM)str );
1355 /*******************************************************************
1356 * SetDlgItemInt (USER32.@)
1358 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1359 BOOL fSigned )
1361 char str[20];
1363 if (fSigned) sprintf( str, "%d", (INT)value );
1364 else sprintf( str, "%u", value );
1365 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1366 return TRUE;
1370 /***********************************************************************
1371 * GetDlgItemInt (USER32.@)
1373 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1374 BOOL fSigned )
1376 char str[30];
1377 char * endptr;
1378 long result = 0;
1380 if (translated) *translated = FALSE;
1381 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1382 return 0;
1383 if (fSigned)
1385 result = strtol( str, &endptr, 10 );
1386 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1387 return 0;
1388 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1389 return 0;
1391 else
1393 result = strtoul( str, &endptr, 10 );
1394 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1395 return 0;
1396 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1398 if (translated) *translated = TRUE;
1399 return (UINT)result;
1403 /***********************************************************************
1404 * CheckDlgButton (USER32.@)
1406 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1408 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1409 return TRUE;
1413 /***********************************************************************
1414 * IsDlgButtonChecked (USER32.@)
1416 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1418 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1422 /***********************************************************************
1423 * CheckRB
1425 * Callback function used to check/uncheck radio buttons that fall
1426 * within a specific range of IDs.
1428 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1430 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1431 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1433 if ((lChildID >= lpRadioGroup->firstID) &&
1434 (lChildID <= lpRadioGroup->lastID))
1436 if (lChildID == lpRadioGroup->checkID)
1438 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1440 else
1442 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1446 return TRUE;
1450 /***********************************************************************
1451 * CheckRadioButton (USER32.@)
1453 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1454 int lastID, int checkID )
1456 RADIOGROUP radioGroup;
1458 radioGroup.firstID = firstID;
1459 radioGroup.lastID = lastID;
1460 radioGroup.checkID = checkID;
1462 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1463 (LPARAM)&radioGroup);
1467 /***********************************************************************
1468 * GetDialogBaseUnits (USER.243)
1469 * GetDialogBaseUnits (USER32.@)
1471 DWORD WINAPI GetDialogBaseUnits(void)
1473 static DWORD units;
1475 if (!units)
1477 HDC hdc;
1478 SIZE size;
1480 if ((hdc = GetDC(0)))
1482 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1483 if (size.cx) units = MAKELONG( size.cx, size.cy );
1484 ReleaseDC( 0, hdc );
1486 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1488 return units;
1492 /***********************************************************************
1493 * MapDialogRect (USER32.@)
1495 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1497 DIALOGINFO * dlgInfo;
1498 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1499 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1500 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1501 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1502 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1503 return TRUE;
1507 /***********************************************************************
1508 * GetNextDlgGroupItem (USER32.@)
1510 * Corrections to MSDN documentation
1512 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1513 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1514 * 2. Prev of NULL or hwndDlg fails
1516 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1518 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1519 BOOL fLooped=FALSE;
1520 BOOL fSkipping=FALSE;
1522 hwndDlg = WIN_GetFullHandle( hwndDlg );
1523 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1525 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1526 if (!hwndCtrl && fPrevious) return 0;
1528 if (hwndCtrl)
1530 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1532 else
1534 /* No ctrl specified -> start from the beginning */
1535 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1536 /* MSDN is wrong. fPrevious does not result in the last child */
1538 /* Maybe that first one is valid. If so then we don't want to skip it*/
1539 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1541 return hwndCtrl;
1545 /* Always go forward around the group and list of controls; for the
1546 * previous control keep track; for the next break when you find one
1548 retvalue = hwndCtrl;
1549 hwnd = hwndCtrl;
1550 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1553 while (!hwndNext)
1555 /* Climb out until there is a next sibling of the ancestor or we
1556 * reach the top (in which case we loop back to the start)
1558 if (hwndDlg == GetParent (hwnd))
1560 /* Wrap around to the beginning of the list, within the same
1561 * group. (Once only)
1563 if (fLooped) goto end;
1564 fLooped = TRUE;
1565 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1567 else
1569 hwnd = GetParent (hwnd);
1570 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1573 hwnd = hwndNext;
1575 /* Wander down the leading edge of controlparents */
1576 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1577 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1578 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1579 hwnd = hwndNext;
1580 /* Question. If the control is a control parent but either has no
1581 * children or is not visible/enabled then if it has a WS_GROUP does
1582 * it count? For that matter does it count anyway?
1583 * I believe it doesn't count.
1586 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1588 hwndLastGroup = hwnd;
1589 if (!fSkipping)
1591 /* Look for the beginning of the group */
1592 fSkipping = TRUE;
1596 if (hwnd == hwndCtrl)
1598 if (!fSkipping) break;
1599 if (hwndLastGroup == hwnd) break;
1600 hwnd = hwndLastGroup;
1601 fSkipping = FALSE;
1602 fLooped = FALSE;
1605 if (!fSkipping &&
1606 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1607 WS_VISIBLE)
1609 retvalue = hwnd;
1610 if (!fPrevious) break;
1613 end:
1614 return retvalue;
1618 /***********************************************************************
1619 * DIALOG_GetNextTabItem
1621 * Recursive helper for GetNextDlgTabItem
1623 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1625 LONG dsStyle;
1626 LONG exStyle;
1627 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1628 HWND retWnd = 0;
1629 HWND hChildFirst = 0;
1631 if(!hwndCtrl)
1633 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1634 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1636 else if (IsChild( hwndMain, hwndCtrl ))
1638 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1639 if(!hChildFirst)
1641 if(GetParent(hwndCtrl) != hwndMain)
1642 /* i.e. if we are not at the top level of the recursion */
1643 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1644 else
1645 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1649 while(hChildFirst)
1651 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1652 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1653 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1655 HWND retWnd;
1656 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1657 if (retWnd) return (retWnd);
1659 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1661 return (hChildFirst);
1663 hChildFirst = GetWindow(hChildFirst,wndSearch);
1665 if(hwndCtrl)
1667 HWND hParent = GetParent(hwndCtrl);
1668 while(hParent)
1670 if(hParent == hwndMain) break;
1671 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1672 if(retWnd) break;
1673 hParent = GetParent(hParent);
1675 if(!retWnd)
1676 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1678 return retWnd ? retWnd : hwndCtrl;
1681 /***********************************************************************
1682 * GetNextDlgTabItem (USER32.@)
1684 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1685 BOOL fPrevious )
1687 hwndDlg = WIN_GetFullHandle( hwndDlg );
1688 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1690 /* Undocumented but tested under Win2000 and WinME */
1691 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1693 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1694 * NB GetLastError returns whatever was set before the function was
1695 * called.
1697 if (!hwndCtrl && fPrevious) return 0;
1699 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1702 /**********************************************************************
1703 * DIALOG_DlgDirSelect
1705 * Helper function for DlgDirSelect*
1707 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1708 INT id, BOOL unicode, BOOL combo )
1710 WCHAR *buffer, *ptr;
1711 INT item, size;
1712 BOOL ret;
1713 HWND listbox = GetDlgItem( hwnd, id );
1715 TRACE("%p %s %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1716 if (!listbox) return FALSE;
1718 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1719 if (item == LB_ERR) return FALSE;
1721 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, item, 0 );
1722 if (size == LB_ERR) return FALSE;
1724 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size+2) * sizeof(WCHAR) ))) return FALSE;
1726 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1728 if ((ret = (buffer[0] == '['))) /* drive or directory */
1730 if (buffer[1] == '-') /* drive */
1732 buffer[3] = ':';
1733 buffer[4] = 0;
1734 ptr = buffer + 2;
1736 else
1738 buffer[strlenW(buffer)-1] = '\\';
1739 ptr = buffer + 1;
1742 else
1744 /* Filenames without a dot extension must have one tacked at the end */
1745 if (strchrW(buffer, '.') == NULL)
1747 buffer[strlenW(buffer)+1] = '\0';
1748 buffer[strlenW(buffer)] = '.';
1750 ptr = buffer;
1753 if (!unicode)
1755 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1756 ((LPSTR)str)[len-1] = 0;
1758 else lstrcpynW( str, ptr, len );
1759 HeapFree( GetProcessHeap(), 0, buffer );
1760 TRACE("Returning %d %s\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1761 return ret;
1765 /**********************************************************************
1766 * DIALOG_DlgDirListW
1768 * Helper function for DlgDirList*W
1770 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1771 INT idStatic, UINT attrib, BOOL combo )
1773 HWND hwnd;
1774 LPWSTR orig_spec = spec;
1775 WCHAR any[] = {'*','.','*',0};
1777 #define SENDMSG(msg,wparam,lparam) \
1778 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1779 : SendMessageW( hwnd, msg, wparam, lparam ))
1781 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1783 /* If the path exists and is a directory, chdir to it */
1784 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1785 else
1787 WCHAR *p, *p2;
1788 p = spec;
1789 if ((p2 = strchrW( p, ':' ))) p = p2 + 1;
1790 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1791 if ((p2 = strrchrW( p, '/' ))) p = p2;
1792 if (p != spec)
1794 WCHAR sep = *p;
1795 *p = 0;
1796 if (!SetCurrentDirectoryW( spec ))
1798 *p = sep; /* Restore the original spec */
1799 return FALSE;
1801 spec = p + 1;
1805 TRACE( "mask=%s\n", debugstr_w(spec) );
1807 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1809 if (attrib == DDL_DRIVES) attrib |= DDL_EXCLUSIVE;
1811 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1812 if (attrib & DDL_DIRECTORY)
1814 if (!(attrib & DDL_EXCLUSIVE))
1816 SENDMSG( combo ? CB_DIR : LB_DIR,
1817 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1818 (LPARAM)spec );
1820 SENDMSG( combo ? CB_DIR : LB_DIR,
1821 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1822 (LPARAM)any );
1824 else
1826 SENDMSG( combo ? CB_DIR : LB_DIR, attrib, (LPARAM)spec );
1830 /* Convert path specification to uppercase */
1831 if (spec) CharUpperW(spec);
1833 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1835 WCHAR temp[MAX_PATH];
1836 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1837 CharLowerW( temp );
1838 /* Can't use PostMessage() here, because the string is on the stack */
1839 SetDlgItemTextW( hDlg, idStatic, temp );
1842 if (orig_spec && (spec != orig_spec))
1844 /* Update the original file spec */
1845 WCHAR *p = spec;
1846 while ((*orig_spec++ = *p++));
1849 return TRUE;
1850 #undef SENDMSG
1854 /**********************************************************************
1855 * DIALOG_DlgDirListA
1857 * Helper function for DlgDirList*A
1859 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1860 INT idStatic, UINT attrib, BOOL combo )
1862 if (spec)
1864 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1865 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1866 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1867 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1868 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1869 HeapFree( GetProcessHeap(), 0, specW );
1870 return ret;
1872 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1876 /**********************************************************************
1877 * DlgDirSelectExA (USER32.@)
1879 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1881 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1885 /**********************************************************************
1886 * DlgDirSelectExW (USER32.@)
1888 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1890 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1894 /**********************************************************************
1895 * DlgDirSelectComboBoxExA (USER32.@)
1897 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1898 INT id )
1900 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1904 /**********************************************************************
1905 * DlgDirSelectComboBoxExW (USER32.@)
1907 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1908 INT id)
1910 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1914 /**********************************************************************
1915 * DlgDirListA (USER32.@)
1917 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1918 INT idStatic, UINT attrib )
1920 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1924 /**********************************************************************
1925 * DlgDirListW (USER32.@)
1927 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1928 INT idStatic, UINT attrib )
1930 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1934 /**********************************************************************
1935 * DlgDirListComboBoxA (USER32.@)
1937 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1938 INT idStatic, UINT attrib )
1940 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1944 /**********************************************************************
1945 * DlgDirListComboBoxW (USER32.@)
1947 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1948 INT idStatic, UINT attrib )
1950 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );