usp10: Update tests in test_ScriptItemIzeShapePlace to match Windows results.
[wine/multimedia.git] / dlls / user / dialog.c
blobbaf72a68a97e374a6bec2db7f8d767cd9f3194bc
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
39 #include "controls.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
48 /* Dialog control information */
49 typedef struct
51 DWORD style;
52 DWORD exStyle;
53 DWORD helpId;
54 INT16 x;
55 INT16 y;
56 INT16 cx;
57 INT16 cy;
58 UINT_PTR id;
59 LPCWSTR className;
60 LPCWSTR windowName;
61 LPCVOID data;
62 } DLG_CONTROL_INFO;
64 /* Dialog template */
65 typedef struct
67 DWORD style;
68 DWORD exStyle;
69 DWORD helpId;
70 UINT16 nbItems;
71 INT16 x;
72 INT16 y;
73 INT16 cx;
74 INT16 cy;
75 LPCWSTR menuName;
76 LPCWSTR className;
77 LPCWSTR caption;
78 INT16 pointSize;
79 WORD weight;
80 BOOL italic;
81 LPCWSTR faceName;
82 BOOL dialogEx;
83 } DLG_TEMPLATE;
85 /* Radio button group */
86 typedef struct
88 UINT firstID;
89 UINT lastID;
90 UINT checkID;
91 } RADIOGROUP;
94 /*********************************************************************
95 * dialog class descriptor
97 const struct builtin_class_descr DIALOG_builtin_class =
99 DIALOG_CLASS_ATOMA, /* name */
100 CS_SAVEBITS | CS_DBLCLKS, /* style */
101 DefDlgProcA, /* procA */
102 DefDlgProcW, /* procW */
103 DLGWINDOWEXTRA, /* extra */
104 IDC_ARROW, /* cursor */
105 0 /* brush */
109 /***********************************************************************
110 * DIALOG_EnableOwner
112 * Helper function for modal dialogs to enable again the
113 * owner of the dialog box.
115 void DIALOG_EnableOwner( HWND hOwner )
117 /* Owner must be a top-level window */
118 if (hOwner)
119 hOwner = GetAncestor( hOwner, GA_ROOT );
120 if (!hOwner) return;
121 EnableWindow( hOwner, TRUE );
125 /***********************************************************************
126 * DIALOG_DisableOwner
128 * Helper function for modal dialogs to disable the
129 * owner of the dialog box. Returns TRUE if owner was enabled.
131 BOOL DIALOG_DisableOwner( HWND hOwner )
133 /* Owner must be a top-level window */
134 if (hOwner)
135 hOwner = GetAncestor( hOwner, GA_ROOT );
136 if (!hOwner) return FALSE;
137 if (IsWindowEnabled( hOwner ))
139 EnableWindow( hOwner, FALSE );
140 return TRUE;
142 else
143 return FALSE;
146 /***********************************************************************
147 * DIALOG_GetControl32
149 * Return the class and text of the control pointed to by ptr,
150 * fill the header structure and return a pointer to the next control.
152 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
153 BOOL dialogEx )
155 if (dialogEx)
157 info->helpId = GET_DWORD(p); p += 2;
158 info->exStyle = GET_DWORD(p); p += 2;
159 info->style = GET_DWORD(p); p += 2;
161 else
163 info->helpId = 0;
164 info->style = GET_DWORD(p); p += 2;
165 info->exStyle = GET_DWORD(p); p += 2;
167 info->x = GET_WORD(p); p++;
168 info->y = GET_WORD(p); p++;
169 info->cx = GET_WORD(p); p++;
170 info->cy = GET_WORD(p); p++;
172 if (dialogEx)
174 /* id is a DWORD for DIALOGEX */
175 info->id = GET_DWORD(p);
176 p += 2;
178 else
180 info->id = GET_WORD(p);
181 p++;
184 if (GET_WORD(p) == 0xffff)
186 static const WCHAR class_names[6][10] =
188 { 'B','u','t','t','o','n', }, /* 0x80 */
189 { 'E','d','i','t', }, /* 0x81 */
190 { 'S','t','a','t','i','c', }, /* 0x82 */
191 { 'L','i','s','t','B','o','x', }, /* 0x83 */
192 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
193 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
195 WORD id = GET_WORD(p+1);
196 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
197 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
198 if (id <= 5)
199 info->className = class_names[id];
200 else
202 info->className = NULL;
203 ERR("Unknown built-in class id %04x\n", id );
205 p += 2;
207 else
209 info->className = (LPCWSTR)p;
210 p += strlenW( info->className ) + 1;
213 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
215 info->windowName = MAKEINTRESOURCEW(GET_WORD(p + 1));
216 p += 2;
218 else
220 info->windowName = (LPCWSTR)p;
221 p += strlenW( info->windowName ) + 1;
224 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
225 debugstr_w( info->className ), debugstr_w( info->windowName ),
226 info->id, info->x, info->y, info->cx, info->cy,
227 info->style, info->exStyle, info->helpId );
229 if (GET_WORD(p))
231 if (TRACE_ON(dialog))
233 WORD i, count = GET_WORD(p) / sizeof(WORD);
234 TRACE(" BEGIN\n");
235 TRACE(" ");
236 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
237 TRACE("\n");
238 TRACE(" END\n" );
240 info->data = p + 1;
241 p += GET_WORD(p) / sizeof(WORD);
243 else info->data = NULL;
244 p++;
246 /* Next control is on dword boundary */
247 return (const WORD *)(((UINT_PTR)p + 3) & ~3);
251 /***********************************************************************
252 * DIALOG_CreateControls32
254 * Create the control windows for a dialog.
256 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
257 HINSTANCE hInst, BOOL unicode )
259 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd, TRUE );
260 DLG_CONTROL_INFO info;
261 HWND hwndCtrl, hwndDefButton = 0;
262 INT items = dlgTemplate->nbItems;
264 TRACE(" BEGIN\n" );
265 while (items--)
267 template = (LPCSTR)DIALOG_GetControl32( (const WORD *)template, &info,
268 dlgTemplate->dialogEx );
269 /* Is this it? */
270 if (info.style & WS_BORDER)
272 info.style &= ~WS_BORDER;
273 info.exStyle |= WS_EX_CLIENTEDGE;
275 if (unicode)
277 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
278 info.className, info.windowName,
279 info.style | WS_CHILD,
280 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
281 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
282 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
283 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
284 hwnd, (HMENU)info.id,
285 hInst, (LPVOID)info.data );
287 else
289 LPSTR class = (LPSTR)info.className;
290 LPSTR caption = (LPSTR)info.windowName;
292 if (HIWORD(class))
294 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
295 class = HeapAlloc( GetProcessHeap(), 0, len );
296 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
298 if (HIWORD(caption))
300 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
301 caption = HeapAlloc( GetProcessHeap(), 0, len );
302 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
304 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
305 class, caption, info.style | WS_CHILD,
306 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
307 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
308 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
309 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
310 hwnd, (HMENU)info.id,
311 hInst, (LPVOID)info.data );
312 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
313 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
315 if (!hwndCtrl)
317 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
318 return FALSE;
321 /* Send initialisation messages to the control */
322 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
323 (WPARAM)dlgInfo->hUserFont, 0 );
324 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
326 /* If there's already a default push-button, set it back */
327 /* to normal and use this one instead. */
328 if (hwndDefButton)
329 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
330 hwndDefButton = hwndCtrl;
331 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
334 TRACE(" END\n" );
335 return TRUE;
339 /***********************************************************************
340 * DIALOG_ParseTemplate32
342 * Fill a DLG_TEMPLATE structure from the dialog template, and return
343 * a pointer to the first control.
345 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
347 const WORD *p = (const WORD *)template;
348 WORD signature;
349 WORD dlgver;
351 signature = GET_WORD(p); p++;
352 dlgver = GET_WORD(p); p++;
354 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
356 result->dialogEx = TRUE;
357 result->helpId = GET_DWORD(p); p += 2;
358 result->exStyle = GET_DWORD(p); p += 2;
359 result->style = GET_DWORD(p); p += 2;
361 else
363 result->style = GET_DWORD(p - 2);
364 result->dialogEx = FALSE;
365 result->helpId = 0;
366 result->exStyle = GET_DWORD(p); p += 2;
368 result->nbItems = GET_WORD(p); p++;
369 result->x = GET_WORD(p); p++;
370 result->y = GET_WORD(p); p++;
371 result->cx = GET_WORD(p); p++;
372 result->cy = GET_WORD(p); p++;
373 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
374 result->dialogEx ? "EX" : "", result->x, result->y,
375 result->cx, result->cy, result->helpId );
376 TRACE(" STYLE 0x%08lx\n", result->style );
377 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
379 /* Get the menu name */
381 switch(GET_WORD(p))
383 case 0x0000:
384 result->menuName = NULL;
385 p++;
386 break;
387 case 0xffff:
388 result->menuName = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
389 p += 2;
390 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
391 break;
392 default:
393 result->menuName = (LPCWSTR)p;
394 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
395 p += strlenW( result->menuName ) + 1;
396 break;
399 /* Get the class name */
401 switch(GET_WORD(p))
403 case 0x0000:
404 result->className = DIALOG_CLASS_ATOMW;
405 p++;
406 break;
407 case 0xffff:
408 result->className = MAKEINTRESOURCEW(GET_WORD( p + 1 ));
409 p += 2;
410 TRACE(" CLASS %04x\n", LOWORD(result->className) );
411 break;
412 default:
413 result->className = (LPCWSTR)p;
414 TRACE(" CLASS %s\n", debugstr_w( result->className ));
415 p += strlenW( result->className ) + 1;
416 break;
419 /* Get the window caption */
421 result->caption = (LPCWSTR)p;
422 p += strlenW( result->caption ) + 1;
423 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
425 /* Get the font name */
427 result->pointSize = 0;
428 result->faceName = NULL;
429 result->weight = FW_DONTCARE;
430 result->italic = FALSE;
432 if (result->style & DS_SETFONT)
434 result->pointSize = GET_WORD(p);
435 p++;
436 if (result->dialogEx)
438 result->weight = GET_WORD(p); p++;
439 result->italic = LOBYTE(GET_WORD(p)); p++;
441 result->faceName = (LPCWSTR)p;
442 p += strlenW( result->faceName ) + 1;
443 TRACE(" FONT %d, %s, %d, %s\n",
444 result->pointSize, debugstr_w( result->faceName ),
445 result->weight, result->italic ? "TRUE" : "FALSE" );
448 /* First control is on dword boundary */
449 return (LPCSTR)(((UINT_PTR)p + 3) & ~3);
453 /***********************************************************************
454 * DIALOG_CreateIndirect
455 * Creates a dialog box window
457 * modal = TRUE if we are called from a modal dialog box.
458 * (it's more compatible to do it here, as under Windows the owner
459 * is never disabled if the dialog fails because of an invalid template)
461 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
462 HWND owner, DLGPROC dlgProc, LPARAM param,
463 BOOL unicode, BOOL modal )
465 HWND hwnd;
466 RECT rect;
467 DLG_TEMPLATE template;
468 DIALOGINFO * dlgInfo = NULL;
469 DWORD units = GetDialogBaseUnits();
470 BOOL ownerEnabled = TRUE;
471 HMENU hMenu = 0;
472 HFONT hUserFont = 0;
473 UINT flags = 0;
474 UINT xBaseUnit = LOWORD(units);
475 UINT yBaseUnit = HIWORD(units);
477 /* Parse dialog template */
479 if (!dlgTemplate) return 0;
480 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
482 /* Load menu */
484 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
486 /* Create custom font if needed */
488 if (template.style & DS_SETFONT)
490 /* We convert the size to pixels and then make it -ve. This works
491 * for both +ve and -ve template.pointSize */
492 HDC dc;
493 int pixels;
494 dc = GetDC(0);
495 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 );
500 if (hUserFont)
502 SIZE charSize;
503 HFONT hOldFont = SelectObject( dc, hUserFont );
504 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
505 if (charSize.cx)
507 xBaseUnit = charSize.cx;
508 yBaseUnit = charSize.cy;
510 SelectObject( dc, hOldFont );
512 ReleaseDC(0, dc);
513 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
516 /* Create dialog main window */
518 rect.left = rect.top = 0;
519 rect.right = MulDiv(template.cx, xBaseUnit, 4);
520 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
521 if (template.style & WS_CHILD)
522 template.style &= ~(WS_CAPTION|WS_SYSMENU);
523 if (template.style & DS_MODALFRAME)
524 template.exStyle |= WS_EX_DLGMODALFRAME;
525 if (template.style & DS_CONTROL)
526 template.exStyle |= WS_EX_CONTROLPARENT;
527 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
528 rect.right -= rect.left;
529 rect.bottom -= rect.top;
531 if (template.x == CW_USEDEFAULT16)
533 rect.left = rect.top = CW_USEDEFAULT;
535 else
537 if (template.style & DS_CENTER)
539 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
540 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
542 else
544 rect.left += MulDiv(template.x, xBaseUnit, 4);
545 rect.top += MulDiv(template.y, yBaseUnit, 8);
547 if ( !(template.style & WS_CHILD) )
549 INT dX, dY;
551 if( !(template.style & DS_ABSALIGN) )
552 ClientToScreen( owner, (POINT *)&rect );
554 /* try to fit it into the desktop */
556 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
557 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
558 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
559 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
560 if( rect.left < 0 ) rect.left = 0;
561 if( rect.top < 0 ) rect.top = 0;
565 if (modal)
567 ownerEnabled = DIALOG_DisableOwner( owner );
568 if (ownerEnabled) flags |= DF_OWNERENABLED;
571 if (unicode)
573 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
574 template.style & ~WS_VISIBLE,
575 rect.left, rect.top, rect.right, rect.bottom,
576 owner, hMenu, hInst, NULL );
578 else
580 LPSTR class = (LPSTR)template.className;
581 LPSTR caption = (LPSTR)template.caption;
583 if (HIWORD(class))
585 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
586 class = HeapAlloc( GetProcessHeap(), 0, len );
587 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
589 if (HIWORD(caption))
591 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
592 caption = HeapAlloc( GetProcessHeap(), 0, len );
593 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
595 hwnd = CreateWindowExA(template.exStyle, class, caption,
596 template.style & ~WS_VISIBLE,
597 rect.left, rect.top, rect.right, rect.bottom,
598 owner, hMenu, hInst, NULL );
599 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
600 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
603 if (!hwnd)
605 if (hUserFont) DeleteObject( hUserFont );
606 if (hMenu) DestroyMenu( hMenu );
607 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
608 return 0;
611 /* moved this from the top of the method to here as DIALOGINFO structure
612 will be valid only after WM_CREATE message has been handled in DefDlgProc
613 All the members of the structure get filled here using temp variables */
614 dlgInfo = DIALOG_get_info( hwnd, TRUE );
615 dlgInfo->hwndFocus = 0;
616 dlgInfo->hUserFont = hUserFont;
617 dlgInfo->hMenu = hMenu;
618 dlgInfo->xBaseUnit = xBaseUnit;
619 dlgInfo->yBaseUnit = yBaseUnit;
620 dlgInfo->idResult = 0;
621 dlgInfo->flags = flags;
622 dlgInfo->hDialogHeap = 0;
624 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
626 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
627 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
629 if (dlgInfo->hUserFont)
630 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
632 /* Create controls */
634 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
636 /* Send initialisation messages and set focus */
638 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
639 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
641 /* By returning TRUE, app has requested a default focus assignment */
642 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
643 if( dlgInfo->hwndFocus )
644 SetFocus( dlgInfo->hwndFocus );
647 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
649 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
651 return hwnd;
653 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
654 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
655 return 0;
659 /***********************************************************************
660 * CreateDialogParamA (USER32.@)
662 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
663 DLGPROC dlgProc, LPARAM param )
665 HRSRC hrsrc;
666 LPCDLGTEMPLATEA ptr;
668 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
669 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
670 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
674 /***********************************************************************
675 * CreateDialogParamW (USER32.@)
677 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
678 DLGPROC dlgProc, LPARAM param )
680 HRSRC hrsrc;
681 LPCDLGTEMPLATEA ptr;
683 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
684 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
685 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
689 /***********************************************************************
690 * CreateDialogIndirectParamAorW (USER32.@)
692 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
693 HWND owner, DLGPROC dlgProc, LPARAM param,
694 DWORD flags )
696 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
699 /***********************************************************************
700 * CreateDialogIndirectParamA (USER32.@)
702 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
703 HWND owner, DLGPROC dlgProc, LPARAM param )
705 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
708 /***********************************************************************
709 * CreateDialogIndirectParamW (USER32.@)
711 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
712 HWND owner, DLGPROC dlgProc, LPARAM param )
714 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
718 /***********************************************************************
719 * DIALOG_DoDialogBox
721 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
723 DIALOGINFO * dlgInfo;
724 MSG msg;
725 INT retval;
726 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
727 BOOL bFirstEmpty;
729 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return -1;
731 bFirstEmpty = TRUE;
732 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
734 for (;;)
736 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
738 if (bFirstEmpty)
740 /* ShowWindow the first time the queue goes empty */
741 ShowWindow( hwnd, SW_SHOWNORMAL );
742 bFirstEmpty = FALSE;
744 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
746 /* No message present -> send ENTERIDLE and wait */
747 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
749 if (!GetMessageW( &msg, 0, 0, 0 )) break;
752 if (!IsWindow( hwnd )) return -1;
753 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
755 TranslateMessage( &msg );
756 DispatchMessageW( &msg );
758 if (dlgInfo->flags & DF_END) break;
761 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
762 retval = dlgInfo->idResult;
763 DestroyWindow( hwnd );
764 return retval;
768 /***********************************************************************
769 * DialogBoxParamA (USER32.@)
771 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
772 HWND owner, DLGPROC dlgProc, LPARAM param )
774 HWND hwnd;
775 HRSRC hrsrc;
776 LPCDLGTEMPLATEA ptr;
778 if (!(hrsrc = FindResourceA( hInst, name, (LPSTR)RT_DIALOG ))) return 0;
779 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
780 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
781 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
782 return -1;
786 /***********************************************************************
787 * DialogBoxParamW (USER32.@)
789 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
790 HWND owner, DLGPROC dlgProc, LPARAM param )
792 HWND hwnd;
793 HRSRC hrsrc;
794 LPCDLGTEMPLATEW ptr;
796 if (!(hrsrc = FindResourceW( hInst, name, (LPWSTR)RT_DIALOG ))) return 0;
797 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
798 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
799 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
800 return -1;
804 /***********************************************************************
805 * DialogBoxIndirectParamAorW (USER32.@)
807 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
808 HWND owner, DLGPROC dlgProc,
809 LPARAM param, DWORD flags )
811 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
812 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
813 return -1;
816 /***********************************************************************
817 * DialogBoxIndirectParamA (USER32.@)
819 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
820 HWND owner, DLGPROC dlgProc, LPARAM param )
822 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
826 /***********************************************************************
827 * DialogBoxIndirectParamW (USER32.@)
829 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
830 HWND owner, DLGPROC dlgProc, LPARAM param )
832 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
835 /***********************************************************************
836 * EndDialog (USER32.@)
838 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
840 BOOL wasEnabled = TRUE;
841 DIALOGINFO * dlgInfo;
842 HWND owner;
844 TRACE("%p %d\n", hwnd, retval );
846 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE )))
848 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
849 return FALSE;
851 dlgInfo->idResult = retval;
852 dlgInfo->flags |= DF_END;
853 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
855 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
856 DIALOG_EnableOwner( owner );
858 /* Windows sets the focus to the dialog itself in EndDialog */
860 if (IsChild(hwnd, GetFocus()))
861 SetFocus( hwnd );
863 /* Don't have to send a ShowWindow(SW_HIDE), just do
864 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
866 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
867 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
869 if (hwnd == GetActiveWindow()) WINPOS_ActivateOtherWindow( hwnd );
871 /* unblock dialog loop */
872 PostMessageA(hwnd, WM_NULL, 0, 0);
873 return TRUE;
877 /***********************************************************************
878 * DIALOG_IsAccelerator
880 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
882 HWND hwndControl = hwnd;
883 HWND hwndNext;
884 INT dlgCode;
885 WCHAR buffer[128];
889 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
890 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
892 dlgCode = SendMessageW( hwndControl, WM_GETDLGCODE, 0, 0 );
893 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
894 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
896 /* find the accelerator key */
897 LPWSTR p = buffer - 2;
901 p = strchrW( p + 2, '&' );
903 while (p != NULL && p[1] == '&');
905 /* and check if it's the one we're looking for */
906 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
908 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
910 /* set focus to the control */
911 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
912 /* and bump it on to next */
913 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
915 else if (dlgCode & DLGC_BUTTON)
917 /* send BM_CLICK message to the control */
918 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
920 return TRUE;
923 hwndNext = GetWindow( hwndControl, GW_CHILD );
925 else hwndNext = 0;
927 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
929 while (!hwndNext && hwndControl)
931 hwndControl = GetParent( hwndControl );
932 if (hwndControl == hwndDlg)
934 if(hwnd==hwndDlg) /* prevent endless loop */
936 hwndNext=hwnd;
937 break;
939 hwndNext = GetWindow( hwndDlg, GW_CHILD );
941 else
942 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
944 hwndControl = hwndNext;
946 while (hwndControl && (hwndControl != hwnd));
948 return FALSE;
951 /***********************************************************************
952 * DIALOG_FindMsgDestination
954 * The messages that IsDialogMessage sends may not go to the dialog
955 * calling IsDialogMessage if that dialog is a child, and it has the
956 * DS_CONTROL style set.
957 * We propagate up until we hit one that does not have DS_CONTROL, or
958 * whose parent is not a dialog.
960 * This is undocumented behaviour.
962 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
964 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
966 WND *pParent;
967 HWND hParent = GetParent(hwndDlg);
968 if (!hParent) break;
970 pParent = WIN_GetPtr(hParent);
971 if (!pParent || pParent == WND_OTHER_PROCESS || pParent == WND_DESKTOP) break;
973 if (!(pParent->flags & WIN_ISDIALOG))
975 WIN_ReleasePtr(pParent);
976 break;
978 WIN_ReleasePtr(pParent);
980 hwndDlg = hParent;
983 return hwndDlg;
986 /***********************************************************************
987 * DIALOG_FixOneChildOnChangeFocus
989 * Callback helper for DIALOG_FixChildrenOnChangeFocus
992 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
993 LPARAM lParam)
995 /* If a default pushbutton then no longer default */
996 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
997 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
998 return TRUE;
1001 /***********************************************************************
1002 * DIALOG_FixChildrenOnChangeFocus
1004 * Following the change of focus that occurs for example after handling
1005 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
1006 * children may be required.
1008 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
1010 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
1011 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
1012 /* Windows does ask for this. I don't know why yet */
1014 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
1016 /* If the button that is getting the focus WAS flagged as the default
1017 * pushbutton then ask the dialog what it thinks the default is and
1018 * set that in the default style.
1020 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
1022 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
1023 if (HIWORD(def_id) == DC_HASDEFID)
1025 HWND hwndDef;
1026 def_id = LOWORD(def_id);
1027 hwndDef = GetDlgItem (hwndDlg, def_id);
1028 if (hwndDef)
1030 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
1031 /* I know that if it is a button then it should already be a
1032 * UNDEFPUSHBUTTON, since we have just told the buttons to
1033 * change style. But maybe they ignored our request
1035 if ((dlgcode_def & DLGC_BUTTON) &&
1036 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
1038 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1043 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
1045 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
1046 /* I wonder why it doesn't send a DM_SETDEFID */
1050 /***********************************************************************
1051 * IsDialogMessageW (USER32.@)
1053 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1055 INT dlgCode = 0;
1057 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1059 hwndDlg = WIN_GetFullHandle( hwndDlg );
1060 if (hwndDlg == GetDesktopWindow()) return FALSE;
1061 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1063 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1065 switch(msg->message)
1067 case WM_KEYDOWN:
1068 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1069 if (dlgCode & DLGC_WANTMESSAGE) break;
1071 switch(msg->wParam)
1073 case VK_TAB:
1074 if (!(dlgCode & DLGC_WANTTAB))
1076 BOOL fIsDialog = TRUE;
1077 WND *pWnd = WIN_GetPtr( hwndDlg );
1079 if (pWnd && pWnd != WND_OTHER_PROCESS)
1081 fIsDialog = (pWnd->flags & WIN_ISDIALOG) != 0;
1082 WIN_ReleasePtr(pWnd);
1085 /* I am not sure under which circumstances the TAB is handled
1086 * each way. All I do know is that it does not always simply
1087 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
1088 * do so but I presume someone has)
1090 if (fIsDialog)
1091 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1092 else
1094 /* It would appear that GetNextDlgTabItem can handle being
1095 * passed hwndDlg rather than NULL but that is undocumented
1096 * so let's do it properly
1098 HWND hwndFocus = GetFocus();
1099 HWND hwndNext = GetNextDlgTabItem (hwndDlg,
1100 hwndFocus == hwndDlg ? NULL : hwndFocus,
1101 GetKeyState (VK_SHIFT) & 0x8000);
1102 if (hwndNext)
1104 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
1105 msg->wParam, (LPARAM)msg);
1106 if (dlgCode & DLGC_HASSETSEL)
1108 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
1109 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
1110 if (buffer)
1112 INT length;
1113 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
1114 length = strlenW (buffer);
1115 HeapFree (GetProcessHeap(), 0, buffer);
1116 SendMessageW (hwndNext, EM_SETSEL, 0, length);
1119 SetFocus (hwndNext);
1120 DIALOG_FixChildrenOnChangeFocus (hwndDlg, hwndNext);
1122 else
1123 return FALSE;
1125 return TRUE;
1127 break;
1129 case VK_RIGHT:
1130 case VK_DOWN:
1131 case VK_LEFT:
1132 case VK_UP:
1133 if (!(dlgCode & DLGC_WANTARROWS))
1135 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1136 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1137 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1138 return TRUE;
1140 break;
1142 case VK_CANCEL:
1143 case VK_ESCAPE:
1144 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1145 return TRUE;
1147 case VK_EXECUTE:
1148 case VK_RETURN:
1150 DWORD dw;
1151 if ((GetFocus() == msg->hwnd) &&
1152 (SendMessageW (msg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
1154 SendMessageW (hwndDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(msg->hwnd),BN_CLICKED), (LPARAM)msg->hwnd);
1156 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0)))
1158 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1159 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1161 else
1163 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1167 return TRUE;
1169 break;
1171 case WM_CHAR:
1172 /* FIXME Under what circumstances does WM_GETDLGCODE get sent?
1173 * It does NOT get sent in the test program I have
1175 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1176 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1177 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1178 /* drop through */
1180 case WM_SYSCHAR:
1181 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1183 /* don't translate or dispatch */
1184 return TRUE;
1186 break;
1189 TranslateMessage( msg );
1190 DispatchMessageW( msg );
1191 return TRUE;
1195 /***********************************************************************
1196 * GetDlgCtrlID (USER32.@)
1198 INT WINAPI GetDlgCtrlID( HWND hwnd )
1200 return GetWindowLongPtrW( hwnd, GWLP_ID );
1204 /***********************************************************************
1205 * GetDlgItem (USER32.@)
1207 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1209 int i;
1210 HWND *list = WIN_ListChildren( hwndDlg );
1211 HWND ret = 0;
1213 if (!list) return 0;
1215 for (i = 0; list[i]; i++) if (GetWindowLongPtrW( list[i], GWLP_ID ) == id) break;
1216 ret = list[i];
1217 HeapFree( GetProcessHeap(), 0, list );
1218 return ret;
1222 /*******************************************************************
1223 * SendDlgItemMessageA (USER32.@)
1225 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1226 WPARAM wParam, LPARAM lParam )
1228 HWND hwndCtrl = GetDlgItem( hwnd, id );
1229 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1230 else return 0;
1234 /*******************************************************************
1235 * SendDlgItemMessageW (USER32.@)
1237 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1238 WPARAM wParam, LPARAM lParam )
1240 HWND hwndCtrl = GetDlgItem( hwnd, id );
1241 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1242 else return 0;
1246 /*******************************************************************
1247 * SetDlgItemTextA (USER32.@)
1249 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1251 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1255 /*******************************************************************
1256 * SetDlgItemTextW (USER32.@)
1258 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1260 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1264 /***********************************************************************
1265 * GetDlgItemTextA (USER32.@)
1267 UINT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, INT len )
1269 if (str && (len > 0)) str[0] = '\0';
1270 return (UINT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1271 len, (LPARAM)str );
1275 /***********************************************************************
1276 * GetDlgItemTextW (USER32.@)
1278 UINT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, INT len )
1280 if (str && (len > 0)) str[0] = '\0';
1281 return (UINT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1282 len, (LPARAM)str );
1286 /*******************************************************************
1287 * SetDlgItemInt (USER32.@)
1289 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1290 BOOL fSigned )
1292 char str[20];
1294 if (fSigned) sprintf( str, "%d", (INT)value );
1295 else sprintf( str, "%u", value );
1296 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1297 return TRUE;
1301 /***********************************************************************
1302 * GetDlgItemInt (USER32.@)
1304 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1305 BOOL fSigned )
1307 char str[30];
1308 char * endptr;
1309 long result = 0;
1311 if (translated) *translated = FALSE;
1312 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1313 return 0;
1314 if (fSigned)
1316 result = strtol( str, &endptr, 10 );
1317 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1318 return 0;
1319 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1320 return 0;
1322 else
1324 result = strtoul( str, &endptr, 10 );
1325 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1326 return 0;
1327 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1329 if (translated) *translated = TRUE;
1330 return (UINT)result;
1334 /***********************************************************************
1335 * CheckDlgButton (USER32.@)
1337 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1339 SendDlgItemMessageW( hwnd, id, BM_SETCHECK, check, 0 );
1340 return TRUE;
1344 /***********************************************************************
1345 * IsDlgButtonChecked (USER32.@)
1347 UINT WINAPI IsDlgButtonChecked( HWND hwnd, int id )
1349 return (UINT)SendDlgItemMessageW( hwnd, id, BM_GETCHECK, 0, 0 );
1353 /***********************************************************************
1354 * CheckRB
1356 * Callback function used to check/uncheck radio buttons that fall
1357 * within a specific range of IDs.
1359 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1361 LONG lChildID = GetWindowLongPtrW(hwndChild, GWLP_ID);
1362 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1364 if ((lChildID >= lpRadioGroup->firstID) &&
1365 (lChildID <= lpRadioGroup->lastID))
1367 if (lChildID == lpRadioGroup->checkID)
1369 SendMessageW(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1371 else
1373 SendMessageW(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1377 return TRUE;
1381 /***********************************************************************
1382 * CheckRadioButton (USER32.@)
1384 BOOL WINAPI CheckRadioButton( HWND hwndDlg, int firstID,
1385 int lastID, int checkID )
1387 RADIOGROUP radioGroup;
1389 radioGroup.firstID = firstID;
1390 radioGroup.lastID = lastID;
1391 radioGroup.checkID = checkID;
1393 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1394 (LPARAM)&radioGroup);
1398 /***********************************************************************
1399 * GetDialogBaseUnits (USER.243)
1400 * GetDialogBaseUnits (USER32.@)
1402 DWORD WINAPI GetDialogBaseUnits(void)
1404 static DWORD units;
1406 if (!units)
1408 HDC hdc;
1409 SIZE size;
1411 if ((hdc = GetDC(0)))
1413 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1414 if (size.cx) units = MAKELONG( size.cx, size.cy );
1415 ReleaseDC( 0, hdc );
1417 TRACE("base units = %d,%d\n", LOWORD(units), HIWORD(units) );
1419 return units;
1423 /***********************************************************************
1424 * MapDialogRect (USER32.@)
1426 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1428 DIALOGINFO * dlgInfo;
1429 if (!(dlgInfo = DIALOG_get_info( hwnd, FALSE ))) return FALSE;
1430 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1431 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1432 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1433 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1434 return TRUE;
1438 /***********************************************************************
1439 * GetNextDlgGroupItem (USER32.@)
1441 * Corrections to MSDN documentation
1443 * (Under Windows 2000 at least, where hwndDlg is not actually a dialog)
1444 * 1. hwndCtrl can be hwndDlg in which case it behaves as for NULL
1445 * 2. Prev of NULL or hwndDlg fails
1447 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1449 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1450 BOOL fLooped=FALSE;
1451 BOOL fSkipping=FALSE;
1453 hwndDlg = WIN_GetFullHandle( hwndDlg );
1454 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1456 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1457 if (!hwndCtrl && fPrevious) return 0;
1459 if (hwndCtrl)
1461 if (!IsChild (hwndDlg, hwndCtrl)) return 0;
1463 else
1465 /* No ctrl specified -> start from the beginning */
1466 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1467 /* MSDN is wrong. fPrevious does not result in the last child */
1469 /* Maybe that first one is valid. If so then we don't want to skip it*/
1470 if ((GetWindowLongW( hwndCtrl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1472 return hwndCtrl;
1476 /* Always go forward around the group and list of controls; for the
1477 * previous control keep track; for the next break when you find one
1479 retvalue = hwndCtrl;
1480 hwnd = hwndCtrl;
1481 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
1484 while (!hwndNext)
1486 /* Climb out until there is a next sibling of the ancestor or we
1487 * reach the top (in which case we loop back to the start)
1489 if (hwndDlg == GetParent (hwnd))
1491 /* Wrap around to the beginning of the list, within the same
1492 * group. (Once only)
1494 if (fLooped) goto end;
1495 fLooped = TRUE;
1496 hwndNext = GetWindow (hwndDlg, GW_CHILD);
1498 else
1500 hwnd = GetParent (hwnd);
1501 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
1504 hwnd = hwndNext;
1506 /* Wander down the leading edge of controlparents */
1507 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
1508 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
1509 (hwndNext = GetWindow (hwnd, GW_CHILD)))
1510 hwnd = hwndNext;
1511 /* Question. If the control is a control parent but either has no
1512 * children or is not visible/enabled then if it has a WS_GROUP does
1513 * it count? For that matter does it count anyway?
1514 * I believe it doesn't count.
1517 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
1519 hwndLastGroup = hwnd;
1520 if (!fSkipping)
1522 /* Look for the beginning of the group */
1523 fSkipping = TRUE;
1527 if (hwnd == hwndCtrl)
1529 if (!fSkipping) break;
1530 if (hwndLastGroup == hwnd) break;
1531 hwnd = hwndLastGroup;
1532 fSkipping = FALSE;
1533 fLooped = FALSE;
1536 if (!fSkipping &&
1537 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
1538 WS_VISIBLE)
1540 retvalue = hwnd;
1541 if (!fPrevious) break;
1544 end:
1545 return retvalue;
1549 /***********************************************************************
1550 * DIALOG_GetNextTabItem
1552 * Recursive helper for GetNextDlgTabItem
1554 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1556 LONG dsStyle;
1557 LONG exStyle;
1558 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1559 HWND retWnd = 0;
1560 HWND hChildFirst = 0;
1562 if(!hwndCtrl)
1564 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1565 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1567 else if (IsChild( hwndMain, hwndCtrl ))
1569 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1570 if(!hChildFirst)
1572 if(GetParent(hwndCtrl) != hwndMain)
1573 /* i.e. if we are not at the top level of the recursion */
1574 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1575 else
1576 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1580 while(hChildFirst)
1582 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1583 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1584 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1586 HWND retWnd;
1587 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1588 if (retWnd) return (retWnd);
1590 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1592 return (hChildFirst);
1594 hChildFirst = GetWindow(hChildFirst,wndSearch);
1596 if(hwndCtrl)
1598 HWND hParent = GetParent(hwndCtrl);
1599 while(hParent)
1601 if(hParent == hwndMain) break;
1602 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1603 if(retWnd) break;
1604 hParent = GetParent(hParent);
1606 if(!retWnd)
1607 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1609 return retWnd ? retWnd : hwndCtrl;
1612 /***********************************************************************
1613 * GetNextDlgTabItem (USER32.@)
1615 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1616 BOOL fPrevious )
1618 hwndDlg = WIN_GetFullHandle( hwndDlg );
1619 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1621 /* Undocumented but tested under Win2000 and WinME */
1622 if (hwndDlg == hwndCtrl) hwndCtrl = NULL;
1624 /* Contrary to MSDN documentation, tested under Win2000 and WinME
1625 * NB GetLastError returns whatever was set before the function was
1626 * called.
1628 if (!hwndCtrl && fPrevious) return 0;
1630 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1633 /**********************************************************************
1634 * DIALOG_DlgDirSelect
1636 * Helper function for DlgDirSelect*
1638 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1639 INT id, BOOL unicode, BOOL combo )
1641 WCHAR *buffer, *ptr;
1642 INT item, size;
1643 BOOL ret;
1644 HWND listbox = GetDlgItem( hwnd, id );
1646 TRACE("%p '%s' %d\n", hwnd, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str), id );
1647 if (!listbox) return FALSE;
1649 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1650 if (item == LB_ERR) return FALSE;
1652 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1653 if (size == LB_ERR) return FALSE;
1655 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1657 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1659 if ((ret = (buffer[0] == '['))) /* drive or directory */
1661 if (buffer[1] == '-') /* drive */
1663 buffer[3] = ':';
1664 buffer[4] = 0;
1665 ptr = buffer + 2;
1667 else
1669 buffer[strlenW(buffer)-1] = '\\';
1670 ptr = buffer + 1;
1673 else ptr = buffer;
1675 if (!unicode)
1677 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1678 ((LPSTR)str)[len-1] = 0;
1680 else lstrcpynW( str, ptr, len );
1681 HeapFree( GetProcessHeap(), 0, buffer );
1682 TRACE("Returning %d '%s'\n", ret, unicode ? debugstr_w(str) : debugstr_a((LPSTR)str) );
1683 return ret;
1687 /**********************************************************************
1688 * DIALOG_DlgDirListW
1690 * Helper function for DlgDirList*W
1692 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1693 INT idStatic, UINT attrib, BOOL combo )
1695 HWND hwnd;
1696 LPWSTR orig_spec = spec;
1697 WCHAR any[] = {'*','.','*',0};
1699 #define SENDMSG(msg,wparam,lparam) \
1700 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1701 : SendMessageW( hwnd, msg, wparam, lparam ))
1703 TRACE("%p %s %d %d %04x\n", hDlg, debugstr_w(spec), idLBox, idStatic, attrib );
1705 /* If the path exists and is a directory, chdir to it */
1706 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1707 else
1709 WCHAR *p, *p2;
1710 p = spec;
1711 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1712 if ((p2 = strrchrW( p, '/' ))) p = p2;
1713 if (p != spec)
1715 WCHAR sep = *p;
1716 *p = 0;
1717 if (!SetCurrentDirectoryW( spec ))
1719 *p = sep; /* Restore the original spec */
1720 return FALSE;
1722 spec = p + 1;
1726 TRACE( "mask=%s\n", debugstr_w(spec) );
1728 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1730 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1731 if (attrib & DDL_DIRECTORY)
1733 if (!(attrib & DDL_EXCLUSIVE))
1735 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1736 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1737 (LPARAM)spec ) == LB_ERR)
1738 return FALSE;
1740 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1741 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1742 (LPARAM)any ) == LB_ERR)
1743 return FALSE;
1745 else
1747 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1748 (LPARAM)spec ) == LB_ERR)
1749 return FALSE;
1753 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1755 WCHAR temp[MAX_PATH];
1756 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1757 CharLowerW( temp );
1758 /* Can't use PostMessage() here, because the string is on the stack */
1759 SetDlgItemTextW( hDlg, idStatic, temp );
1762 if (orig_spec && (spec != orig_spec))
1764 /* Update the original file spec */
1765 WCHAR *p = spec;
1766 while ((*orig_spec++ = *p++));
1769 return TRUE;
1770 #undef SENDMSG
1774 /**********************************************************************
1775 * DIALOG_DlgDirListA
1777 * Helper function for DlgDirList*A
1779 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1780 INT idStatic, UINT attrib, BOOL combo )
1782 if (spec)
1784 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1785 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1786 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1787 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1788 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1789 HeapFree( GetProcessHeap(), 0, specW );
1790 return ret;
1792 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1796 /**********************************************************************
1797 * DlgDirSelectExA (USER32.@)
1799 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1801 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, FALSE );
1805 /**********************************************************************
1806 * DlgDirSelectExW (USER32.@)
1808 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1810 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, FALSE );
1814 /**********************************************************************
1815 * DlgDirSelectComboBoxExA (USER32.@)
1817 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1818 INT id )
1820 return DIALOG_DlgDirSelect( hwnd, (LPWSTR)str, len, id, FALSE, TRUE );
1824 /**********************************************************************
1825 * DlgDirSelectComboBoxExW (USER32.@)
1827 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1828 INT id)
1830 return DIALOG_DlgDirSelect( hwnd, str, len, id, TRUE, TRUE );
1834 /**********************************************************************
1835 * DlgDirListA (USER32.@)
1837 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1838 INT idStatic, UINT attrib )
1840 return DIALOG_DlgDirListA( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1844 /**********************************************************************
1845 * DlgDirListW (USER32.@)
1847 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1848 INT idStatic, UINT attrib )
1850 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1854 /**********************************************************************
1855 * DlgDirListComboBoxA (USER32.@)
1857 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1858 INT idStatic, UINT attrib )
1860 return DIALOG_DlgDirListA( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1864 /**********************************************************************
1865 * DlgDirListComboBoxW (USER32.@)
1867 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1868 INT idStatic, UINT attrib )
1870 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );