Make sure winsock.h gets included before stdlib.h.
[wine/multimedia.git] / windows / dialog.c
blob92cc328463d5958c35c538c54757dffb8795aba0
1 /*
2 * Dialog functions
4 * Copyright 1993, 1994, 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "windef.h"
32 #include "winnls.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "wine/winuser16.h"
37 #include "wine/unicode.h"
38 #include "controls.h"
39 #include "heap.h"
40 #include "win.h"
41 #include "user.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
47 /* Dialog control information */
48 typedef struct
50 DWORD style;
51 DWORD exStyle;
52 DWORD helpId;
53 INT16 x;
54 INT16 y;
55 INT16 cx;
56 INT16 cy;
57 UINT id;
58 LPCWSTR className;
59 LPCWSTR windowName;
60 LPCVOID data;
61 } DLG_CONTROL_INFO;
63 /* Dialog template */
64 typedef struct
66 DWORD style;
67 DWORD exStyle;
68 DWORD helpId;
69 UINT16 nbItems;
70 INT16 x;
71 INT16 y;
72 INT16 cx;
73 INT16 cy;
74 LPCWSTR menuName;
75 LPCWSTR className;
76 LPCWSTR caption;
77 WORD pointSize;
78 WORD weight;
79 BOOL italic;
80 LPCWSTR faceName;
81 BOOL dialogEx;
82 } DLG_TEMPLATE;
84 /* Radio button group */
85 typedef struct
87 UINT firstID;
88 UINT lastID;
89 UINT checkID;
90 } RADIOGROUP;
92 /* Dialog base units */
93 static WORD xBaseUnit = 0, yBaseUnit = 0;
96 /*********************************************************************
97 * dialog class descriptor
99 const struct builtin_class_descr DIALOG_builtin_class =
101 DIALOG_CLASS_ATOMA, /* name */
102 CS_GLOBALCLASS | CS_SAVEBITS | CS_DBLCLKS, /* style */
103 DefDlgProcA, /* procA */
104 DefDlgProcW, /* procW */
105 DLGWINDOWEXTRA, /* extra */
106 IDC_ARROWA, /* cursor */
107 0 /* brush */
111 /***********************************************************************
112 * DIALOG_EnableOwner
114 * Helper function for modal dialogs to enable again the
115 * owner of the dialog box.
117 void DIALOG_EnableOwner( HWND hOwner )
119 /* Owner must be a top-level window */
120 if (hOwner)
121 hOwner = GetAncestor( hOwner, GA_ROOT );
122 if (!hOwner) return;
123 EnableWindow( hOwner, TRUE );
127 /***********************************************************************
128 * DIALOG_DisableOwner
130 * Helper function for modal dialogs to disable the
131 * owner of the dialog box. Returns TRUE if owner was enabled.
133 BOOL DIALOG_DisableOwner( HWND hOwner )
135 /* Owner must be a top-level window */
136 if (hOwner)
137 hOwner = GetAncestor( hOwner, GA_ROOT );
138 if (!hOwner) return FALSE;
139 if (IsWindowEnabled( hOwner ))
141 EnableWindow( hOwner, FALSE );
142 return TRUE;
144 else
145 return FALSE;
148 /***********************************************************************
149 * DIALOG_GetCharSizeFromDC
151 * Despite most of MSDN insisting that the horizontal base unit is
152 * tmAveCharWidth it isn't. Knowledge base article Q145994
153 * "HOWTO: Calculate Dialog Units When Not Using the System Font",
154 * says that we should take the average of the 52 English upper and lower
155 * case characters.
157 static BOOL DIALOG_GetCharSizeFromDC( HDC hDC, HFONT hFont, SIZE * pSize )
159 HFONT hFontPrev = 0;
160 char *alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
161 SIZE sz;
162 TEXTMETRICA tm;
164 pSize->cx = xBaseUnit;
165 pSize->cy = yBaseUnit;
167 if(!hDC) return FALSE;
169 if(hFont) hFontPrev = SelectObject(hDC, hFont);
170 if(!GetTextMetricsA(hDC, &tm)) return FALSE;
171 if(!GetTextExtentPointA(hDC, alphabet, 52, &sz)) return FALSE;
173 pSize->cy = tm.tmHeight;
174 pSize->cx = (sz.cx / 26 + 1) / 2;
176 if (hFontPrev) SelectObject(hDC, hFontPrev);
178 TRACE("dlg base units: %ld x %ld\n", pSize->cx, pSize->cy);
179 return TRUE;
182 /***********************************************************************
183 * DIALOG_GetCharSize
185 * A convenient variant of DIALOG_GetCharSizeFromDC.
187 BOOL DIALOG_GetCharSize( HFONT hFont, SIZE * pSize )
189 HDC hDC = GetDC(0);
190 BOOL Success = DIALOG_GetCharSizeFromDC( hDC, hFont, pSize );
191 ReleaseDC(0, hDC);
192 return Success;
195 /***********************************************************************
196 * DIALOG_Init
198 * Initialisation of the dialog manager.
200 BOOL DIALOG_Init(void)
202 HDC hdc;
203 SIZE size;
205 /* Calculate the dialog base units */
207 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
209 ERR("Could not create Display DC\n");
210 return FALSE;
213 if (!DIALOG_GetCharSizeFromDC( hdc, 0, &size ))
215 DeleteDC( hdc );
216 ERR("Could not initialize base dialog units\n");
217 return FALSE;
220 DeleteDC( hdc );
221 xBaseUnit = size.cx;
222 yBaseUnit = size.cy;
224 TRACE("base units = %d,%d\n", xBaseUnit, yBaseUnit );
225 return TRUE;
229 /***********************************************************************
230 * DIALOG_GetControl32
232 * Return the class and text of the control pointed to by ptr,
233 * fill the header structure and return a pointer to the next control.
235 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
236 BOOL dialogEx )
238 if (dialogEx)
240 info->helpId = GET_DWORD(p); p += 2;
241 info->exStyle = GET_DWORD(p); p += 2;
242 info->style = GET_DWORD(p); p += 2;
244 else
246 info->helpId = 0;
247 info->style = GET_DWORD(p); p += 2;
248 info->exStyle = GET_DWORD(p); p += 2;
250 info->x = GET_WORD(p); p++;
251 info->y = GET_WORD(p); p++;
252 info->cx = GET_WORD(p); p++;
253 info->cy = GET_WORD(p); p++;
255 if (dialogEx)
257 /* id is a DWORD for DIALOGEX */
258 info->id = GET_DWORD(p);
259 p += 2;
261 else
263 info->id = GET_WORD(p);
264 p++;
267 if (GET_WORD(p) == 0xffff)
269 static const WCHAR class_names[6][10] =
271 { 'B','u','t','t','o','n', }, /* 0x80 */
272 { 'E','d','i','t', }, /* 0x81 */
273 { 'S','t','a','t','i','c', }, /* 0x82 */
274 { 'L','i','s','t','B','o','x', }, /* 0x83 */
275 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
276 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
278 WORD id = GET_WORD(p+1);
279 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
280 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
281 if (id <= 5)
282 info->className = class_names[id];
283 else
285 info->className = NULL;
286 ERR("Unknown built-in class id %04x\n", id );
288 p += 2;
290 else
292 info->className = (LPCWSTR)p;
293 p += strlenW( info->className ) + 1;
296 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
298 info->windowName = (LPCWSTR)(UINT)GET_WORD(p + 1);
299 p += 2;
301 else
303 info->windowName = (LPCWSTR)p;
304 p += strlenW( info->windowName ) + 1;
307 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
308 debugstr_w( info->className ), debugstr_w( info->windowName ),
309 info->id, info->x, info->y, info->cx, info->cy,
310 info->style, info->exStyle, info->helpId );
312 if (GET_WORD(p))
314 if (TRACE_ON(dialog))
316 WORD i, count = GET_WORD(p) / sizeof(WORD);
317 TRACE(" BEGIN\n");
318 TRACE(" ");
319 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
320 TRACE("\n");
321 TRACE(" END\n" );
323 info->data = p + 1;
324 p += GET_WORD(p) / sizeof(WORD);
326 else info->data = NULL;
327 p++;
329 /* Next control is on dword boundary */
330 return (const WORD *)((((int)p) + 3) & ~3);
334 /***********************************************************************
335 * DIALOG_CreateControls32
337 * Create the control windows for a dialog.
339 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
340 HINSTANCE hInst, BOOL unicode )
342 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
343 DLG_CONTROL_INFO info;
344 HWND hwndCtrl, hwndDefButton = 0;
345 INT items = dlgTemplate->nbItems;
347 TRACE(" BEGIN\n" );
348 while (items--)
350 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
351 dlgTemplate->dialogEx );
352 /* Is this it? */
353 if (info.style & WS_BORDER)
355 info.style &= ~WS_BORDER;
356 info.exStyle |= WS_EX_CLIENTEDGE;
358 if (unicode)
360 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
361 info.className, info.windowName,
362 info.style | WS_CHILD,
363 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
364 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
365 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
366 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
367 hwnd, (HMENU)info.id,
368 hInst, (LPVOID)info.data );
370 else
372 LPSTR class = (LPSTR)info.className;
373 LPSTR caption = (LPSTR)info.windowName;
375 if (HIWORD(class))
377 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
378 class = HeapAlloc( GetProcessHeap(), 0, len );
379 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
381 if (HIWORD(caption))
383 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
384 caption = HeapAlloc( GetProcessHeap(), 0, len );
385 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
387 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
388 class, caption, info.style | WS_CHILD,
389 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
390 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
391 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
392 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
393 hwnd, (HMENU)info.id,
394 hInst, (LPVOID)info.data );
395 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
396 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
398 if (!hwndCtrl)
400 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
401 return FALSE;
404 /* Send initialisation messages to the control */
405 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
406 (WPARAM)dlgInfo->hUserFont, 0 );
407 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
409 /* If there's already a default push-button, set it back */
410 /* to normal and use this one instead. */
411 if (hwndDefButton)
412 SendMessageA( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
413 hwndDefButton = hwndCtrl;
414 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
417 TRACE(" END\n" );
418 return TRUE;
422 /***********************************************************************
423 * DIALOG_ParseTemplate32
425 * Fill a DLG_TEMPLATE structure from the dialog template, and return
426 * a pointer to the first control.
428 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
430 const WORD *p = (const WORD *)template;
432 result->style = GET_DWORD(p); p += 2;
433 if (result->style == 0xffff0001) /* DIALOGEX resource */
435 result->dialogEx = TRUE;
436 result->helpId = GET_DWORD(p); p += 2;
437 result->exStyle = GET_DWORD(p); p += 2;
438 result->style = GET_DWORD(p); p += 2;
440 else
442 result->dialogEx = FALSE;
443 result->helpId = 0;
444 result->exStyle = GET_DWORD(p); p += 2;
446 result->nbItems = GET_WORD(p); p++;
447 result->x = GET_WORD(p); p++;
448 result->y = GET_WORD(p); p++;
449 result->cx = GET_WORD(p); p++;
450 result->cy = GET_WORD(p); p++;
451 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
452 result->dialogEx ? "EX" : "", result->x, result->y,
453 result->cx, result->cy, result->helpId );
454 TRACE(" STYLE 0x%08lx\n", result->style );
455 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
457 /* Get the menu name */
459 switch(GET_WORD(p))
461 case 0x0000:
462 result->menuName = NULL;
463 p++;
464 break;
465 case 0xffff:
466 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
467 p += 2;
468 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
469 break;
470 default:
471 result->menuName = (LPCWSTR)p;
472 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
473 p += strlenW( result->menuName ) + 1;
474 break;
477 /* Get the class name */
479 switch(GET_WORD(p))
481 case 0x0000:
482 result->className = DIALOG_CLASS_ATOMW;
483 p++;
484 break;
485 case 0xffff:
486 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
487 p += 2;
488 TRACE(" CLASS %04x\n", LOWORD(result->className) );
489 break;
490 default:
491 result->className = (LPCWSTR)p;
492 TRACE(" CLASS %s\n", debugstr_w( result->className ));
493 p += strlenW( result->className ) + 1;
494 break;
497 /* Get the window caption */
499 result->caption = (LPCWSTR)p;
500 p += strlenW( result->caption ) + 1;
501 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
503 /* Get the font name */
505 if (result->style & DS_SETFONT)
507 result->pointSize = GET_WORD(p);
508 p++;
509 if (result->dialogEx)
511 result->weight = GET_WORD(p); p++;
512 result->italic = LOBYTE(GET_WORD(p)); p++;
514 else
516 result->weight = FW_DONTCARE;
517 result->italic = FALSE;
519 result->faceName = (LPCWSTR)p;
520 p += strlenW( result->faceName ) + 1;
521 TRACE(" FONT %d, %s, %d, %s\n",
522 result->pointSize, debugstr_w( result->faceName ),
523 result->weight, result->italic ? "TRUE" : "FALSE" );
526 /* First control is on dword boundary */
527 return (LPCSTR)((((int)p) + 3) & ~3);
531 /***********************************************************************
532 * DIALOG_CreateIndirect
533 * Creates a dialog box window
535 * modal = TRUE if we are called from a modal dialog box.
536 * (it's more compatible to do it here, as under Windows the owner
537 * is never disabled if the dialog fails because of an invalid template)
539 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
540 HWND owner, DLGPROC dlgProc, LPARAM param,
541 BOOL unicode, BOOL modal )
543 HWND hwnd;
544 RECT rect;
545 WND * wndPtr;
546 DLG_TEMPLATE template;
547 DIALOGINFO * dlgInfo;
548 BOOL ownerEnabled = TRUE;
550 /* Parse dialog template */
552 if (!dlgTemplate) return 0;
553 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
555 /* Initialise dialog extra data */
557 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
558 dlgInfo->hwndFocus = 0;
559 dlgInfo->hUserFont = 0;
560 dlgInfo->hMenu = 0;
561 dlgInfo->xBaseUnit = xBaseUnit;
562 dlgInfo->yBaseUnit = yBaseUnit;
563 dlgInfo->idResult = 0;
564 dlgInfo->flags = 0;
565 dlgInfo->hDialogHeap = 0;
567 /* Load menu */
569 if (template.menuName) dlgInfo->hMenu = LoadMenuW( hInst, template.menuName );
571 /* Create custom font if needed */
573 if (template.style & DS_SETFONT)
575 /* We convert the size to pixels and then make it -ve. This works
576 * for both +ve and -ve template.pointSize */
577 HDC dc;
578 int pixels;
579 dc = GetDC(0);
580 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
581 ReleaseDC(0, dc);
582 dlgInfo->hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
583 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
584 PROOF_QUALITY, FF_DONTCARE,
585 template.faceName );
586 if (dlgInfo->hUserFont)
588 SIZE charSize;
589 if (DIALOG_GetCharSize( dlgInfo->hUserFont, &charSize ))
591 dlgInfo->xBaseUnit = charSize.cx;
592 dlgInfo->yBaseUnit = charSize.cy;
595 TRACE("units = %d,%d\n", dlgInfo->xBaseUnit, dlgInfo->yBaseUnit );
598 /* Create dialog main window */
600 rect.left = rect.top = 0;
601 rect.right = MulDiv(template.cx, dlgInfo->xBaseUnit, 4);
602 rect.bottom = MulDiv(template.cy, dlgInfo->yBaseUnit, 8);
603 if (template.style & DS_MODALFRAME)
604 template.exStyle |= WS_EX_DLGMODALFRAME;
605 AdjustWindowRectEx( &rect, template.style, (dlgInfo->hMenu != 0), template.exStyle );
606 rect.right -= rect.left;
607 rect.bottom -= rect.top;
609 if (template.x == CW_USEDEFAULT16)
611 rect.left = rect.top = CW_USEDEFAULT;
613 else
615 if (template.style & DS_CENTER)
617 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
618 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
620 else
622 rect.left += MulDiv(template.x, dlgInfo->xBaseUnit, 4);
623 rect.top += MulDiv(template.y, dlgInfo->yBaseUnit, 8);
625 if ( !(template.style & WS_CHILD) )
627 INT dX, dY;
629 if( !(template.style & DS_ABSALIGN) )
630 ClientToScreen( owner, (POINT *)&rect );
632 /* try to fit it into the desktop */
634 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
635 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
636 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
637 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
638 if( rect.left < 0 ) rect.left = 0;
639 if( rect.top < 0 ) rect.top = 0;
643 if (modal)
645 ownerEnabled = DIALOG_DisableOwner( owner );
646 if (ownerEnabled) dlgInfo->flags |= DF_OWNERENABLED;
649 if (unicode)
651 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
652 template.style & ~WS_VISIBLE,
653 rect.left, rect.top, rect.right, rect.bottom,
654 owner, dlgInfo->hMenu, hInst, NULL );
656 else
658 LPSTR class = (LPSTR)template.className;
659 LPSTR caption = (LPSTR)template.caption;
661 if (HIWORD(class))
663 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
664 class = HeapAlloc( GetProcessHeap(), 0, len );
665 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
667 if (HIWORD(caption))
669 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
670 caption = HeapAlloc( GetProcessHeap(), 0, len );
671 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
673 hwnd = CreateWindowExA(template.exStyle, class, caption,
674 template.style & ~WS_VISIBLE,
675 rect.left, rect.top, rect.right, rect.bottom,
676 owner, dlgInfo->hMenu, hInst, NULL );
677 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
678 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
681 if (!hwnd)
683 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
684 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
685 if (modal && (dlgInfo->flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
686 HeapFree( GetProcessHeap(), 0, dlgInfo );
687 return 0;
689 wndPtr = WIN_GetPtr( hwnd );
690 wndPtr->flags |= WIN_ISDIALOG;
691 WIN_ReleasePtr( wndPtr );
693 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
694 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
696 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
697 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
699 if (dlgInfo->hUserFont)
700 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
702 /* Create controls */
704 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
706 HWND hwndPreInitFocus;
708 /* Send initialisation messages and set focus */
710 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
712 hwndPreInitFocus = GetFocus();
713 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
715 /* check where the focus is again,
716 * some controls status might have changed in WM_INITDIALOG */
717 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
718 if( dlgInfo->hwndFocus )
719 SetFocus( dlgInfo->hwndFocus );
721 else
723 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
724 but the focus has not changed, set the focus where we expect it. */
725 if ((GetFocus() == hwndPreInitFocus) &&
726 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
728 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
729 if( dlgInfo->hwndFocus )
730 SetFocus( dlgInfo->hwndFocus );
734 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
736 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
738 return hwnd;
740 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
741 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
742 return 0;
746 /***********************************************************************
747 * CreateDialogParamA (USER32.@)
749 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
750 DLGPROC dlgProc, LPARAM param )
752 HRSRC hrsrc;
753 LPCDLGTEMPLATEA ptr;
755 if (!(hrsrc = FindResourceA( hInst, name, RT_DIALOGA ))) return 0;
756 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
757 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
761 /***********************************************************************
762 * CreateDialogParamW (USER32.@)
764 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
765 DLGPROC dlgProc, LPARAM param )
767 HRSRC hrsrc;
768 LPCDLGTEMPLATEA ptr;
770 if (!(hrsrc = FindResourceW( hInst, name, RT_DIALOGW ))) return 0;
771 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
772 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
776 /***********************************************************************
777 * CreateDialogIndirectParamAorW (USER32.@)
779 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
780 HWND owner, DLGPROC dlgProc, LPARAM param,
781 DWORD flags )
783 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
786 /***********************************************************************
787 * CreateDialogIndirectParamA (USER32.@)
789 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
790 HWND owner, DLGPROC dlgProc, LPARAM param )
792 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
795 /***********************************************************************
796 * CreateDialogIndirectParamW (USER32.@)
798 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
799 HWND owner, DLGPROC dlgProc, LPARAM param )
801 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
805 /***********************************************************************
806 * DIALOG_DoDialogBox
808 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
810 DIALOGINFO * dlgInfo;
811 MSG msg;
812 INT retval;
813 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
815 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
817 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
819 ShowWindow( hwnd, SW_SHOW );
820 for (;;)
822 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
824 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
826 /* No message present -> send ENTERIDLE and wait */
827 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
828 if (!GetMessageW( &msg, 0, 0, 0 )) break;
831 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
833 if (!IsWindow( hwnd )) return -1;
834 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
836 TranslateMessage( &msg );
837 DispatchMessageW( &msg );
839 if (dlgInfo->flags & DF_END) break;
842 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
843 retval = dlgInfo->idResult;
844 DestroyWindow( hwnd );
845 return retval;
849 /***********************************************************************
850 * DialogBoxParamA (USER32.@)
852 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
853 HWND owner, DLGPROC dlgProc, LPARAM param )
855 HWND hwnd;
856 HRSRC hrsrc;
857 LPCDLGTEMPLATEA ptr;
859 if (!(hrsrc = FindResourceA( hInst, name, RT_DIALOGA ))) return 0;
860 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
861 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
862 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
863 return -1;
867 /***********************************************************************
868 * DialogBoxParamW (USER32.@)
870 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
871 HWND owner, DLGPROC dlgProc, LPARAM param )
873 HWND hwnd;
874 HRSRC hrsrc;
875 LPCDLGTEMPLATEW ptr;
877 if (!(hrsrc = FindResourceW( hInst, name, RT_DIALOGW ))) return 0;
878 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
879 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
880 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
881 return -1;
885 /***********************************************************************
886 * DialogBoxIndirectParamAorW (USER32.@)
888 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
889 HWND owner, DLGPROC dlgProc,
890 LPARAM param, DWORD flags )
892 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
893 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
894 return -1;
897 /***********************************************************************
898 * DialogBoxIndirectParamA (USER32.@)
900 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
901 HWND owner, DLGPROC dlgProc, LPARAM param )
903 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
907 /***********************************************************************
908 * DialogBoxIndirectParamW (USER32.@)
910 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
911 HWND owner, DLGPROC dlgProc, LPARAM param )
913 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
916 /***********************************************************************
917 * EndDialog (USER32.@)
919 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
921 BOOL wasEnabled = TRUE;
922 DIALOGINFO * dlgInfo;
923 HWND owner;
925 TRACE("%p %d\n", hwnd, retval );
927 if (!(dlgInfo = DIALOG_get_info( hwnd )))
929 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
930 return FALSE;
932 dlgInfo->idResult = retval;
933 dlgInfo->flags |= DF_END;
934 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
936 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
937 DIALOG_EnableOwner( owner );
939 /* Windows sets the focus to the dialog itself in EndDialog */
941 if (IsChild(hwnd, GetFocus()))
942 SetFocus( hwnd );
944 /* Don't have to send a ShowWindow(SW_HIDE), just do
945 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
947 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
948 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
950 /* unblock dialog loop */
951 PostMessageA(hwnd, WM_NULL, 0, 0);
952 return TRUE;
956 /***********************************************************************
957 * DIALOG_IsAccelerator
959 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
961 HWND hwndControl = hwnd;
962 HWND hwndNext;
963 INT dlgCode;
964 WCHAR buffer[128];
968 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
969 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
971 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
972 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
973 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
975 /* find the accelerator key */
976 LPWSTR p = buffer - 2;
980 p = strchrW( p + 2, '&' );
982 while (p != NULL && p[1] == '&');
984 /* and check if it's the one we're looking for */
985 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
987 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
989 /* set focus to the control */
990 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
991 /* and bump it on to next */
992 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
994 else if (dlgCode & DLGC_BUTTON)
996 /* send BM_CLICK message to the control */
997 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
999 return TRUE;
1002 hwndNext = GetWindow( hwndControl, GW_CHILD );
1004 else hwndNext = 0;
1006 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1008 while (!hwndNext && hwndControl)
1010 hwndControl = GetParent( hwndControl );
1011 if (hwndControl == hwndDlg)
1013 if(hwnd==hwndDlg) /* prevent endless loop */
1015 hwndNext=hwnd;
1016 break;
1018 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1020 else
1021 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1023 hwndControl = hwndNext;
1025 while (hwndControl && (hwndControl != hwnd));
1027 return FALSE;
1030 /***********************************************************************
1031 * DIALOG_FindMsgDestination
1033 * The messages that IsDialogMessage sends may not go to the dialog
1034 * calling IsDialogMessage if that dialog is a child, and it has the
1035 * DS_CONTROL style set.
1036 * We propagate up until we hit one that does not have DS_CONTROL, or
1037 * whose parent is not a dialog.
1039 * This is undocumented behaviour.
1041 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1043 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1045 WND *pParent;
1046 HWND hParent = GetParent(hwndDlg);
1047 if (!hParent) break;
1049 pParent = WIN_FindWndPtr(hParent);
1050 if (!pParent) break;
1052 if (!(pParent->flags & WIN_ISDIALOG))
1054 WIN_ReleaseWndPtr(pParent);
1055 break;
1057 WIN_ReleaseWndPtr(pParent);
1059 hwndDlg = hParent;
1062 return hwndDlg;
1065 /***********************************************************************
1066 * IsDialogMessageW (USER32.@)
1068 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1070 INT dlgCode = 0;
1072 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1074 hwndDlg = WIN_GetFullHandle( hwndDlg );
1075 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1077 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1079 switch(msg->message)
1081 case WM_KEYDOWN:
1082 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1083 if (dlgCode & DLGC_WANTMESSAGE) break;
1085 switch(msg->wParam)
1087 case VK_TAB:
1088 if (!(dlgCode & DLGC_WANTTAB))
1090 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1091 return TRUE;
1093 break;
1095 case VK_RIGHT:
1096 case VK_DOWN:
1097 case VK_LEFT:
1098 case VK_UP:
1099 if (!(dlgCode & DLGC_WANTARROWS))
1101 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1102 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1103 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1104 return TRUE;
1106 break;
1108 case VK_CANCEL:
1109 case VK_ESCAPE:
1110 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1111 return TRUE;
1113 case VK_EXECUTE:
1114 case VK_RETURN:
1116 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1117 if (HIWORD(dw) == DC_HASDEFID)
1119 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1120 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1122 else
1124 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1128 return TRUE;
1130 break;
1132 case WM_CHAR:
1133 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1134 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1135 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1136 /* drop through */
1138 case WM_SYSCHAR:
1139 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1141 /* don't translate or dispatch */
1142 return TRUE;
1144 break;
1147 TranslateMessage( msg );
1148 DispatchMessageW( msg );
1149 return TRUE;
1153 /***********************************************************************
1154 * GetDlgCtrlID (USER32.@)
1156 INT WINAPI GetDlgCtrlID( HWND hwnd )
1158 return GetWindowLongW( hwnd, GWL_ID );
1162 /***********************************************************************
1163 * GetDlgItem (USER32.@)
1165 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1167 int i;
1168 HWND *list = WIN_ListChildren( hwndDlg );
1169 HWND ret = 0;
1171 if (!list) return 0;
1173 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1174 ret = list[i];
1175 HeapFree( GetProcessHeap(), 0, list );
1176 return ret;
1180 /*******************************************************************
1181 * SendDlgItemMessageA (USER32.@)
1183 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1184 WPARAM wParam, LPARAM lParam )
1186 HWND hwndCtrl = GetDlgItem( hwnd, id );
1187 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1188 else return 0;
1192 /*******************************************************************
1193 * SendDlgItemMessageW (USER32.@)
1195 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1196 WPARAM wParam, LPARAM lParam )
1198 HWND hwndCtrl = GetDlgItem( hwnd, id );
1199 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1200 else return 0;
1204 /*******************************************************************
1205 * SetDlgItemTextA (USER32.@)
1207 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1209 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1213 /*******************************************************************
1214 * SetDlgItemTextW (USER32.@)
1216 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1218 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1222 /***********************************************************************
1223 * GetDlgItemTextA (USER32.@)
1225 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1227 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1228 len, (LPARAM)str );
1232 /***********************************************************************
1233 * GetDlgItemTextW (USER32.@)
1235 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1237 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1238 len, (LPARAM)str );
1242 /*******************************************************************
1243 * SetDlgItemInt (USER32.@)
1245 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1246 BOOL fSigned )
1248 char str[20];
1250 if (fSigned) sprintf( str, "%d", (INT)value );
1251 else sprintf( str, "%u", value );
1252 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1253 return TRUE;
1257 /***********************************************************************
1258 * GetDlgItemInt (USER32.@)
1260 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1261 BOOL fSigned )
1263 char str[30];
1264 char * endptr;
1265 long result = 0;
1267 if (translated) *translated = FALSE;
1268 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1269 return 0;
1270 if (fSigned)
1272 result = strtol( str, &endptr, 10 );
1273 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1274 return 0;
1275 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1276 return 0;
1278 else
1280 result = strtoul( str, &endptr, 10 );
1281 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1282 return 0;
1283 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1285 if (translated) *translated = TRUE;
1286 return (UINT)result;
1290 /***********************************************************************
1291 * CheckDlgButton (USER32.@)
1293 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1295 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1296 return TRUE;
1300 /***********************************************************************
1301 * IsDlgButtonChecked (USER32.@)
1303 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1305 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1309 /***********************************************************************
1310 * CheckRB
1312 * Callback function used to check/uncheck radio buttons that fall
1313 * within a specific range of IDs.
1315 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1317 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1318 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1320 if ((lChildID >= lpRadioGroup->firstID) &&
1321 (lChildID <= lpRadioGroup->lastID))
1323 if (lChildID == lpRadioGroup->checkID)
1325 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1327 else
1329 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1333 return TRUE;
1337 /***********************************************************************
1338 * CheckRadioButton (USER32.@)
1340 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1341 UINT lastID, UINT checkID )
1343 RADIOGROUP radioGroup;
1345 /* perform bounds checking for a radio button group */
1346 radioGroup.firstID = min(min(firstID, lastID), checkID);
1347 radioGroup.lastID = max(max(firstID, lastID), checkID);
1348 radioGroup.checkID = checkID;
1350 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1351 (LPARAM)&radioGroup);
1355 /***********************************************************************
1356 * GetDialogBaseUnits (USER.243)
1357 * GetDialogBaseUnits (USER32.@)
1359 DWORD WINAPI GetDialogBaseUnits(void)
1361 return MAKELONG( xBaseUnit, yBaseUnit );
1365 /***********************************************************************
1366 * MapDialogRect (USER32.@)
1368 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1370 DIALOGINFO * dlgInfo;
1371 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1372 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1373 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1374 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1375 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1376 return TRUE;
1380 /***********************************************************************
1381 * GetNextDlgGroupItem (USER32.@)
1383 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1385 HWND hwnd, retvalue;
1387 hwndDlg = WIN_GetFullHandle( hwndDlg );
1388 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1390 if(hwndCtrl)
1392 /* if the hwndCtrl is the child of the control in the hwndDlg,
1393 * then the hwndDlg has to be the parent of the hwndCtrl */
1394 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1395 hwndDlg = GetParent(hwndCtrl);
1398 if (hwndCtrl)
1400 /* Make sure hwndCtrl is a top-level child */
1401 HWND parent = GetParent( hwndCtrl );
1402 while (parent && parent != hwndDlg) parent = GetParent(parent);
1403 if (parent != hwndDlg) return 0;
1405 else
1407 /* No ctrl specified -> start from the beginning */
1408 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1409 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1412 retvalue = hwndCtrl;
1413 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1414 while (1)
1416 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1418 /* Wrap-around to the beginning of the group */
1419 HWND tmp;
1421 hwnd = GetWindow( hwndDlg, GW_CHILD );
1422 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1424 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1425 if (tmp == hwndCtrl) break;
1428 if (hwnd == hwndCtrl) break;
1429 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1431 retvalue = hwnd;
1432 if (!fPrevious) break;
1434 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1436 return retvalue;
1440 /***********************************************************************
1441 * DIALOG_GetNextTabItem
1443 * Helper for GetNextDlgTabItem
1445 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1447 LONG dsStyle;
1448 LONG exStyle;
1449 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1450 HWND retWnd = 0;
1451 HWND hChildFirst = 0;
1453 if(!hwndCtrl)
1455 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1456 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1458 else if (IsChild( hwndMain, hwndCtrl ))
1460 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1461 if(!hChildFirst)
1463 if(GetParent(hwndCtrl) != hwndMain)
1464 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1465 else
1467 if(fPrevious)
1468 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1469 else
1470 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1475 while(hChildFirst)
1477 BOOL bCtrl = FALSE;
1478 while(hChildFirst)
1480 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1481 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1482 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1484 bCtrl=TRUE;
1485 break;
1487 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1488 break;
1489 hChildFirst = GetWindow(hChildFirst,wndSearch);
1491 if(hChildFirst)
1493 if(bCtrl)
1494 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1495 else
1496 retWnd = hChildFirst;
1498 if(retWnd) break;
1499 hChildFirst = GetWindow(hChildFirst,wndSearch);
1501 if(!retWnd && hwndCtrl)
1503 HWND hParent = GetParent(hwndCtrl);
1504 while(hParent)
1506 if(hParent == hwndMain) break;
1507 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1508 if(retWnd) break;
1509 hParent = GetParent(hParent);
1511 if(!retWnd)
1512 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1514 return retWnd;
1517 /***********************************************************************
1518 * GetNextDlgTabItem (USER32.@)
1520 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1521 BOOL fPrevious )
1523 hwndDlg = WIN_GetFullHandle( hwndDlg );
1524 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1525 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1528 /**********************************************************************
1529 * DIALOG_DlgDirSelect
1531 * Helper function for DlgDirSelect*
1533 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1534 INT id, BOOL unicode, BOOL combo )
1536 char *buffer, *ptr;
1537 INT item, size;
1538 BOOL ret;
1539 HWND listbox = GetDlgItem( hwnd, id );
1541 TRACE("%p '%s' %d\n", hwnd, str, id );
1542 if (!listbox) return FALSE;
1544 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1545 if (item == LB_ERR) return FALSE;
1546 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1547 if (size == LB_ERR) return FALSE;
1549 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1551 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1553 if ((ret = (buffer[0] == '['))) /* drive or directory */
1555 if (buffer[1] == '-') /* drive */
1557 buffer[3] = ':';
1558 buffer[4] = 0;
1559 ptr = buffer + 2;
1561 else
1563 buffer[strlen(buffer)-1] = '\\';
1564 ptr = buffer + 1;
1567 else ptr = buffer;
1569 if (unicode)
1571 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1572 ((LPWSTR)str)[len-1] = 0;
1574 else lstrcpynA( str, ptr, len );
1575 HeapFree( GetProcessHeap(), 0, buffer );
1576 TRACE("Returning %d '%s'\n", ret, str );
1577 return ret;
1581 /**********************************************************************
1582 * DIALOG_DlgDirList
1584 * Helper function for DlgDirList*
1586 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
1587 INT idStatic, UINT attrib, BOOL combo )
1589 HWND hwnd;
1590 LPSTR orig_spec = spec;
1592 #define SENDMSG(msg,wparam,lparam) \
1593 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
1594 : SendMessageA( hwnd, msg, wparam, lparam ))
1596 TRACE("%p '%s' %d %d %04x\n",
1597 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1599 /* If the path exists and is a directory, chdir to it */
1600 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
1601 else
1603 char *p, *p2;
1604 p = spec;
1605 if ((p2 = strrchr( p, '\\' ))) p = p2;
1606 if ((p2 = strrchr( p, '/' ))) p = p2;
1607 if (p != spec)
1609 char sep = *p;
1610 *p = 0;
1611 if (!SetCurrentDirectoryA( spec ))
1613 *p = sep; /* Restore the original spec */
1614 return FALSE;
1616 spec = p + 1;
1620 TRACE( "mask=%s\n", spec );
1622 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1624 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1625 if (attrib & DDL_DIRECTORY)
1627 if (!(attrib & DDL_EXCLUSIVE))
1629 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1630 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1631 (LPARAM)spec ) == LB_ERR)
1632 return FALSE;
1634 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1635 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1636 (LPARAM)"*.*" ) == LB_ERR)
1637 return FALSE;
1639 else
1641 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1642 (LPARAM)spec ) == LB_ERR)
1643 return FALSE;
1647 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1649 char temp[MAX_PATH];
1650 GetCurrentDirectoryA( sizeof(temp), temp );
1651 CharLowerA( temp );
1652 /* Can't use PostMessage() here, because the string is on the stack */
1653 SetDlgItemTextA( hDlg, idStatic, temp );
1656 if (orig_spec && (spec != orig_spec))
1658 /* Update the original file spec */
1659 char *p = spec;
1660 while ((*orig_spec++ = *p++));
1663 return TRUE;
1664 #undef SENDMSG
1668 /**********************************************************************
1669 * DIALOG_DlgDirListW
1671 * Helper function for DlgDirList*W
1673 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1674 INT idStatic, UINT attrib, BOOL combo )
1676 if (spec)
1678 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
1679 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
1680 attrib, combo );
1681 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
1682 HeapFree( GetProcessHeap(), 0, specA );
1683 return ret;
1685 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
1689 /**********************************************************************
1690 * DlgDirSelectExA (USER32.@)
1692 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1694 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1698 /**********************************************************************
1699 * DlgDirSelectExW (USER32.@)
1701 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1703 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1707 /**********************************************************************
1708 * DlgDirSelectComboBoxExA (USER32.@)
1710 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1711 INT id )
1713 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1717 /**********************************************************************
1718 * DlgDirSelectComboBoxExW (USER32.@)
1720 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1721 INT id)
1723 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
1727 /**********************************************************************
1728 * DlgDirListA (USER32.@)
1730 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1731 INT idStatic, UINT attrib )
1733 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1737 /**********************************************************************
1738 * DlgDirListW (USER32.@)
1740 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1741 INT idStatic, UINT attrib )
1743 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1747 /**********************************************************************
1748 * DlgDirListComboBoxA (USER32.@)
1750 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1751 INT idStatic, UINT attrib )
1753 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1757 /**********************************************************************
1758 * DlgDirListComboBoxW (USER32.@)
1760 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1761 INT idStatic, UINT attrib )
1763 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );