Implemented the GRADIENT_FILL_RECT{H|V} cases of GdiGradientFill.
[wine.git] / windows / dialog.c
blobf051450a70ea9bd2a3c572ffc56a46cfde333e3f
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 if ((id >= 0x80) && (id <= 0x85))
280 info->className = class_names[id - 0x80];
281 else
283 info->className = NULL;
284 ERR("Unknown built-in class id %04x\n", id );
286 p += 2;
288 else
290 info->className = (LPCWSTR)p;
291 p += strlenW( info->className ) + 1;
294 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
296 info->windowName = (LPCWSTR)(UINT)GET_WORD(p + 1);
297 p += 2;
299 else
301 info->windowName = (LPCWSTR)p;
302 p += strlenW( info->windowName ) + 1;
305 TRACE(" %s %s %d, %d, %d, %d, %d, %08lx, %08lx, %08lx\n",
306 debugstr_w( info->className ), debugstr_w( info->windowName ),
307 info->id, info->x, info->y, info->cx, info->cy,
308 info->style, info->exStyle, info->helpId );
310 if (GET_WORD(p))
312 if (TRACE_ON(dialog))
314 WORD i, count = GET_WORD(p) / sizeof(WORD);
315 TRACE(" BEGIN\n");
316 TRACE(" ");
317 for (i = 0; i < count; i++) TRACE( "%04x,", GET_WORD(p+i+1) );
318 TRACE("\n");
319 TRACE(" END\n" );
321 info->data = p + 1;
322 p += GET_WORD(p) / sizeof(WORD);
324 else info->data = NULL;
325 p++;
327 /* Next control is on dword boundary */
328 return (const WORD *)((((int)p) + 3) & ~3);
332 /***********************************************************************
333 * DIALOG_CreateControls32
335 * Create the control windows for a dialog.
337 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
338 HINSTANCE hInst, BOOL unicode )
340 DIALOGINFO *dlgInfo = DIALOG_get_info( hwnd );
341 DLG_CONTROL_INFO info;
342 HWND hwndCtrl, hwndDefButton = 0;
343 INT items = dlgTemplate->nbItems;
345 TRACE(" BEGIN\n" );
346 while (items--)
348 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
349 dlgTemplate->dialogEx );
350 /* Is this it? */
351 if (info.style & WS_BORDER)
353 info.style &= ~WS_BORDER;
354 info.exStyle |= WS_EX_CLIENTEDGE;
356 if (unicode)
358 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
359 info.className, info.windowName,
360 info.style | WS_CHILD,
361 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
362 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
363 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
364 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
365 hwnd, (HMENU)info.id,
366 hInst, (LPVOID)info.data );
368 else
370 LPSTR class = (LPSTR)info.className;
371 LPSTR caption = (LPSTR)info.windowName;
373 if (HIWORD(class))
375 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
376 class = HeapAlloc( GetProcessHeap(), 0, len );
377 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
379 if (HIWORD(caption))
381 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
382 caption = HeapAlloc( GetProcessHeap(), 0, len );
383 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
385 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
386 class, caption, info.style | WS_CHILD,
387 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
388 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
389 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
390 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
391 hwnd, (HMENU)info.id,
392 hInst, (LPVOID)info.data );
393 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
394 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
396 if (!hwndCtrl)
398 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
399 return FALSE;
402 /* Send initialisation messages to the control */
403 if (dlgInfo->hUserFont) SendMessageA( hwndCtrl, WM_SETFONT,
404 (WPARAM)dlgInfo->hUserFont, 0 );
405 if (SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
407 /* If there's already a default push-button, set it back */
408 /* to normal and use this one instead. */
409 if (hwndDefButton)
410 SendMessageA( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
411 hwndDefButton = hwndCtrl;
412 dlgInfo->idResult = GetWindowLongA( hwndCtrl, GWL_ID );
415 TRACE(" END\n" );
416 return TRUE;
420 /***********************************************************************
421 * DIALOG_ParseTemplate32
423 * Fill a DLG_TEMPLATE structure from the dialog template, and return
424 * a pointer to the first control.
426 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
428 const WORD *p = (const WORD *)template;
430 result->style = GET_DWORD(p); p += 2;
431 if (result->style == 0xffff0001) /* DIALOGEX resource */
433 result->dialogEx = TRUE;
434 result->helpId = GET_DWORD(p); p += 2;
435 result->exStyle = GET_DWORD(p); p += 2;
436 result->style = GET_DWORD(p); p += 2;
438 else
440 result->dialogEx = FALSE;
441 result->helpId = 0;
442 result->exStyle = GET_DWORD(p); p += 2;
444 result->nbItems = GET_WORD(p); p++;
445 result->x = GET_WORD(p); p++;
446 result->y = GET_WORD(p); p++;
447 result->cx = GET_WORD(p); p++;
448 result->cy = GET_WORD(p); p++;
449 TRACE("DIALOG%s %d, %d, %d, %d, %ld\n",
450 result->dialogEx ? "EX" : "", result->x, result->y,
451 result->cx, result->cy, result->helpId );
452 TRACE(" STYLE 0x%08lx\n", result->style );
453 TRACE(" EXSTYLE 0x%08lx\n", result->exStyle );
455 /* Get the menu name */
457 switch(GET_WORD(p))
459 case 0x0000:
460 result->menuName = NULL;
461 p++;
462 break;
463 case 0xffff:
464 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
465 p += 2;
466 TRACE(" MENU %04x\n", LOWORD(result->menuName) );
467 break;
468 default:
469 result->menuName = (LPCWSTR)p;
470 TRACE(" MENU %s\n", debugstr_w(result->menuName) );
471 p += strlenW( result->menuName ) + 1;
472 break;
475 /* Get the class name */
477 switch(GET_WORD(p))
479 case 0x0000:
480 result->className = DIALOG_CLASS_ATOMW;
481 p++;
482 break;
483 case 0xffff:
484 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
485 p += 2;
486 TRACE(" CLASS %04x\n", LOWORD(result->className) );
487 break;
488 default:
489 result->className = (LPCWSTR)p;
490 TRACE(" CLASS %s\n", debugstr_w( result->className ));
491 p += strlenW( result->className ) + 1;
492 break;
495 /* Get the window caption */
497 result->caption = (LPCWSTR)p;
498 p += strlenW( result->caption ) + 1;
499 TRACE(" CAPTION %s\n", debugstr_w( result->caption ) );
501 /* Get the font name */
503 if (result->style & DS_SETFONT)
505 result->pointSize = GET_WORD(p);
506 p++;
507 if (result->dialogEx)
509 result->weight = GET_WORD(p); p++;
510 result->italic = LOBYTE(GET_WORD(p)); p++;
512 else
514 result->weight = FW_DONTCARE;
515 result->italic = FALSE;
517 result->faceName = (LPCWSTR)p;
518 p += strlenW( result->faceName ) + 1;
519 TRACE(" FONT %d, %s, %d, %s\n",
520 result->pointSize, debugstr_w( result->faceName ),
521 result->weight, result->italic ? "TRUE" : "FALSE" );
524 /* First control is on dword boundary */
525 return (LPCSTR)((((int)p) + 3) & ~3);
529 /***********************************************************************
530 * DIALOG_CreateIndirect
531 * Creates a dialog box window
533 * modal = TRUE if we are called from a modal dialog box.
534 * (it's more compatible to do it here, as under Windows the owner
535 * is never disabled if the dialog fails because of an invalid template)
537 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
538 HWND owner, DLGPROC dlgProc, LPARAM param,
539 BOOL unicode, BOOL modal )
541 HWND hwnd;
542 RECT rect;
543 WND * wndPtr;
544 DLG_TEMPLATE template;
545 DIALOGINFO * dlgInfo;
546 BOOL ownerEnabled = TRUE;
548 /* Parse dialog template */
550 if (!dlgTemplate) return 0;
551 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
553 /* Initialise dialog extra data */
555 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
556 dlgInfo->hwndFocus = 0;
557 dlgInfo->hUserFont = 0;
558 dlgInfo->hMenu = 0;
559 dlgInfo->xBaseUnit = xBaseUnit;
560 dlgInfo->yBaseUnit = yBaseUnit;
561 dlgInfo->idResult = 0;
562 dlgInfo->flags = 0;
563 dlgInfo->hDialogHeap = 0;
565 /* Load menu */
567 if (template.menuName) dlgInfo->hMenu = LoadMenuW( hInst, template.menuName );
569 /* Create custom font if needed */
571 if (template.style & DS_SETFONT)
573 /* We convert the size to pixels and then make it -ve. This works
574 * for both +ve and -ve template.pointSize */
575 HDC dc;
576 int pixels;
577 dc = GetDC(0);
578 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
579 ReleaseDC(0, dc);
580 dlgInfo->hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
581 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
582 PROOF_QUALITY, FF_DONTCARE,
583 template.faceName );
584 if (dlgInfo->hUserFont)
586 SIZE charSize;
587 if (DIALOG_GetCharSize( dlgInfo->hUserFont, &charSize ))
589 dlgInfo->xBaseUnit = charSize.cx;
590 dlgInfo->yBaseUnit = charSize.cy;
593 TRACE("units = %d,%d\n", dlgInfo->xBaseUnit, dlgInfo->yBaseUnit );
596 /* Create dialog main window */
598 rect.left = rect.top = 0;
599 rect.right = MulDiv(template.cx, dlgInfo->xBaseUnit, 4);
600 rect.bottom = MulDiv(template.cy, dlgInfo->yBaseUnit, 8);
601 if (template.style & DS_MODALFRAME)
602 template.exStyle |= WS_EX_DLGMODALFRAME;
603 AdjustWindowRectEx( &rect, template.style, (dlgInfo->hMenu != 0), template.exStyle );
604 rect.right -= rect.left;
605 rect.bottom -= rect.top;
607 if (template.x == CW_USEDEFAULT16)
609 rect.left = rect.top = CW_USEDEFAULT;
611 else
613 if (template.style & DS_CENTER)
615 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
616 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
618 else
620 rect.left += MulDiv(template.x, dlgInfo->xBaseUnit, 4);
621 rect.top += MulDiv(template.y, dlgInfo->yBaseUnit, 8);
623 if ( !(template.style & WS_CHILD) )
625 INT dX, dY;
627 if( !(template.style & DS_ABSALIGN) )
628 ClientToScreen( owner, (POINT *)&rect );
630 /* try to fit it into the desktop */
632 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
633 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
634 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
635 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
636 if( rect.left < 0 ) rect.left = 0;
637 if( rect.top < 0 ) rect.top = 0;
641 if (modal)
643 ownerEnabled = DIALOG_DisableOwner( owner );
644 if (ownerEnabled) dlgInfo->flags |= DF_OWNERENABLED;
647 if (unicode)
649 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
650 template.style & ~WS_VISIBLE,
651 rect.left, rect.top, rect.right, rect.bottom,
652 owner, dlgInfo->hMenu, hInst, NULL );
654 else
656 LPSTR class = (LPSTR)template.className;
657 LPSTR caption = (LPSTR)template.caption;
659 if (HIWORD(class))
661 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
662 class = HeapAlloc( GetProcessHeap(), 0, len );
663 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
665 if (HIWORD(caption))
667 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
668 caption = HeapAlloc( GetProcessHeap(), 0, len );
669 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
671 hwnd = CreateWindowExA(template.exStyle, class, caption,
672 template.style & ~WS_VISIBLE,
673 rect.left, rect.top, rect.right, rect.bottom,
674 owner, dlgInfo->hMenu, hInst, NULL );
675 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
676 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
679 if (!hwnd)
681 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
682 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
683 if (modal && (dlgInfo->flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
684 HeapFree( GetProcessHeap(), 0, dlgInfo );
685 return 0;
687 wndPtr = WIN_GetPtr( hwnd );
688 wndPtr->flags |= WIN_ISDIALOG;
689 WIN_ReleasePtr( wndPtr );
691 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
692 SetWindowLongW( hwnd, DWL_WINE_DIALOGINFO, (LONG)dlgInfo );
694 if (unicode) SetWindowLongW( hwnd, DWL_DLGPROC, (LONG)dlgProc );
695 else SetWindowLongA( hwnd, DWL_DLGPROC, (LONG)dlgProc );
697 if (dlgInfo->hUserFont)
698 SendMessageA( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
700 /* Create controls */
702 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
704 HWND hwndPreInitFocus;
706 /* Send initialisation messages and set focus */
708 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
710 hwndPreInitFocus = GetFocus();
711 if (SendMessageA( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ))
713 /* check where the focus is again,
714 * some controls status might have changed in WM_INITDIALOG */
715 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
716 if( dlgInfo->hwndFocus )
717 SetFocus( dlgInfo->hwndFocus );
719 else
721 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
722 but the focus has not changed, set the focus where we expect it. */
723 if ((GetFocus() == hwndPreInitFocus) &&
724 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
726 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
727 if( dlgInfo->hwndFocus )
728 SetFocus( dlgInfo->hwndFocus );
732 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
734 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
736 return hwnd;
738 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
739 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
740 return 0;
744 /***********************************************************************
745 * CreateDialogParamA (USER32.@)
747 HWND WINAPI CreateDialogParamA( HINSTANCE hInst, LPCSTR name, HWND owner,
748 DLGPROC dlgProc, LPARAM param )
750 HRSRC hrsrc;
751 LPCDLGTEMPLATEA ptr;
753 if (!(hrsrc = FindResourceA( hInst, name, RT_DIALOGA ))) return 0;
754 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
755 return CreateDialogIndirectParamA( hInst, ptr, owner, dlgProc, param );
759 /***********************************************************************
760 * CreateDialogParamW (USER32.@)
762 HWND WINAPI CreateDialogParamW( HINSTANCE hInst, LPCWSTR name, HWND owner,
763 DLGPROC dlgProc, LPARAM param )
765 HRSRC hrsrc;
766 LPCDLGTEMPLATEA ptr;
768 if (!(hrsrc = FindResourceW( hInst, name, RT_DIALOGW ))) return 0;
769 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
770 return CreateDialogIndirectParamW( hInst, ptr, owner, dlgProc, param );
774 /***********************************************************************
775 * CreateDialogIndirectParamAorW (USER32.@)
777 HWND WINAPI CreateDialogIndirectParamAorW( HINSTANCE hInst, LPCVOID dlgTemplate,
778 HWND owner, DLGPROC dlgProc, LPARAM param,
779 DWORD flags )
781 return DIALOG_CreateIndirect( hInst, dlgTemplate, owner, dlgProc, param, !flags, FALSE );
784 /***********************************************************************
785 * CreateDialogIndirectParamA (USER32.@)
787 HWND WINAPI CreateDialogIndirectParamA( HINSTANCE hInst, LPCDLGTEMPLATEA dlgTemplate,
788 HWND owner, DLGPROC dlgProc, LPARAM param )
790 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 2 );
793 /***********************************************************************
794 * CreateDialogIndirectParamW (USER32.@)
796 HWND WINAPI CreateDialogIndirectParamW( HINSTANCE hInst, LPCDLGTEMPLATEW dlgTemplate,
797 HWND owner, DLGPROC dlgProc, LPARAM param )
799 return CreateDialogIndirectParamAorW( hInst, dlgTemplate, owner, dlgProc, param, 0 );
803 /***********************************************************************
804 * DIALOG_DoDialogBox
806 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
808 DIALOGINFO * dlgInfo;
809 MSG msg;
810 INT retval;
811 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
813 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return -1;
815 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
817 ShowWindow( hwnd, SW_SHOW );
818 for (;;)
820 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
822 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
824 /* No message present -> send ENTERIDLE and wait */
825 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
826 if (!GetMessageW( &msg, 0, 0, 0 )) break;
829 else if (!GetMessageW( &msg, 0, 0, 0 )) break;
831 if (!IsWindow( hwnd )) return -1;
832 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
834 TranslateMessage( &msg );
835 DispatchMessageW( &msg );
837 if (dlgInfo->flags & DF_END) break;
840 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
841 retval = dlgInfo->idResult;
842 DestroyWindow( hwnd );
843 return retval;
847 /***********************************************************************
848 * DialogBoxParamA (USER32.@)
850 INT_PTR WINAPI DialogBoxParamA( HINSTANCE hInst, LPCSTR name,
851 HWND owner, DLGPROC dlgProc, LPARAM param )
853 HWND hwnd;
854 HRSRC hrsrc;
855 LPCDLGTEMPLATEA ptr;
857 if (!(hrsrc = FindResourceA( hInst, name, RT_DIALOGA ))) return 0;
858 if (!(ptr = (LPCDLGTEMPLATEA)LoadResource(hInst, hrsrc))) return 0;
859 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, FALSE, TRUE );
860 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
861 return -1;
865 /***********************************************************************
866 * DialogBoxParamW (USER32.@)
868 INT_PTR WINAPI DialogBoxParamW( HINSTANCE hInst, LPCWSTR name,
869 HWND owner, DLGPROC dlgProc, LPARAM param )
871 HWND hwnd;
872 HRSRC hrsrc;
873 LPCDLGTEMPLATEW ptr;
875 if (!(hrsrc = FindResourceW( hInst, name, RT_DIALOGW ))) return 0;
876 if (!(ptr = (LPCDLGTEMPLATEW)LoadResource(hInst, hrsrc))) return 0;
877 hwnd = DIALOG_CreateIndirect( hInst, ptr, owner, dlgProc, param, TRUE, TRUE );
878 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
879 return -1;
883 /***********************************************************************
884 * DialogBoxIndirectParamAorW (USER32.@)
886 INT_PTR WINAPI DialogBoxIndirectParamAorW( HINSTANCE hInstance, LPCVOID template,
887 HWND owner, DLGPROC dlgProc,
888 LPARAM param, DWORD flags )
890 HWND hwnd = DIALOG_CreateIndirect( hInstance, template, owner, dlgProc, param, !flags, TRUE );
891 if (hwnd) return DIALOG_DoDialogBox( hwnd, owner );
892 return -1;
895 /***********************************************************************
896 * DialogBoxIndirectParamA (USER32.@)
898 INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance, LPCDLGTEMPLATEA template,
899 HWND owner, DLGPROC dlgProc, LPARAM param )
901 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 2 );
905 /***********************************************************************
906 * DialogBoxIndirectParamW (USER32.@)
908 INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance, LPCDLGTEMPLATEW template,
909 HWND owner, DLGPROC dlgProc, LPARAM param )
911 return DialogBoxIndirectParamAorW( hInstance, template, owner, dlgProc, param, 0 );
914 /***********************************************************************
915 * EndDialog (USER32.@)
917 BOOL WINAPI EndDialog( HWND hwnd, INT_PTR retval )
919 BOOL wasEnabled = TRUE;
920 DIALOGINFO * dlgInfo;
921 HWND owner;
923 TRACE("%p %d\n", hwnd, retval );
925 if (!(dlgInfo = DIALOG_get_info( hwnd )))
927 ERR("got invalid window handle (%p); buggy app !?\n", hwnd);
928 return FALSE;
930 dlgInfo->idResult = retval;
931 dlgInfo->flags |= DF_END;
932 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
934 if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
935 DIALOG_EnableOwner( owner );
937 /* Windows sets the focus to the dialog itself in EndDialog */
939 if (IsChild(hwnd, GetFocus()))
940 SetFocus( hwnd );
942 /* Don't have to send a ShowWindow(SW_HIDE), just do
943 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
945 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
946 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
948 /* unblock dialog loop */
949 PostMessageA(hwnd, WM_NULL, 0, 0);
950 return TRUE;
954 /***********************************************************************
955 * DIALOG_IsAccelerator
957 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
959 HWND hwndControl = hwnd;
960 HWND hwndNext;
961 INT dlgCode;
962 WCHAR buffer[128];
966 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
967 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
969 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
970 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
971 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
973 /* find the accelerator key */
974 LPWSTR p = buffer - 2;
978 p = strchrW( p + 2, '&' );
980 while (p != NULL && p[1] == '&');
982 /* and check if it's the one we're looking for */
983 if (p != NULL && toupperW( p[1] ) == toupperW( wParam ) )
985 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
987 /* set focus to the control */
988 SendMessageA( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
989 /* and bump it on to next */
990 SendMessageA( hwndDlg, WM_NEXTDLGCTL, 0, 0);
992 else if (dlgCode & DLGC_BUTTON)
994 /* send BM_CLICK message to the control */
995 SendMessageA( hwndControl, BM_CLICK, 0, 0 );
997 return TRUE;
1000 hwndNext = GetWindow( hwndControl, GW_CHILD );
1002 else hwndNext = 0;
1004 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1006 while (!hwndNext && hwndControl)
1008 hwndControl = GetParent( hwndControl );
1009 if (hwndControl == hwndDlg)
1011 if(hwnd==hwndDlg) /* prevent endless loop */
1013 hwndNext=hwnd;
1014 break;
1016 hwndNext = GetWindow( hwndDlg, GW_CHILD );
1018 else
1019 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
1021 hwndControl = hwndNext;
1023 while (hwndControl && (hwndControl != hwnd));
1025 return FALSE;
1028 /***********************************************************************
1029 * DIALOG_FindMsgDestination
1031 * The messages that IsDialogMessage sends may not go to the dialog
1032 * calling IsDialogMessage if that dialog is a child, and it has the
1033 * DS_CONTROL style set.
1034 * We propagate up until we hit one that does not have DS_CONTROL, or
1035 * whose parent is not a dialog.
1037 * This is undocumented behaviour.
1039 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
1041 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
1043 WND *pParent;
1044 HWND hParent = GetParent(hwndDlg);
1045 if (!hParent) break;
1047 pParent = WIN_FindWndPtr(hParent);
1048 if (!pParent) break;
1050 if (!(pParent->flags & WIN_ISDIALOG))
1052 WIN_ReleaseWndPtr(pParent);
1053 break;
1055 WIN_ReleaseWndPtr(pParent);
1057 hwndDlg = hParent;
1060 return hwndDlg;
1063 /***********************************************************************
1064 * IsDialogMessageW (USER32.@)
1066 BOOL WINAPI IsDialogMessageW( HWND hwndDlg, LPMSG msg )
1068 INT dlgCode = 0;
1070 if (CallMsgFilterW( msg, MSGF_DIALOGBOX )) return TRUE;
1072 hwndDlg = WIN_GetFullHandle( hwndDlg );
1073 if ((hwndDlg != msg->hwnd) && !IsChild( hwndDlg, msg->hwnd )) return FALSE;
1075 hwndDlg = DIALOG_FindMsgDestination(hwndDlg);
1077 switch(msg->message)
1079 case WM_KEYDOWN:
1080 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1081 if (dlgCode & DLGC_WANTMESSAGE) break;
1083 switch(msg->wParam)
1085 case VK_TAB:
1086 if (!(dlgCode & DLGC_WANTTAB))
1088 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
1089 return TRUE;
1091 break;
1093 case VK_RIGHT:
1094 case VK_DOWN:
1095 case VK_LEFT:
1096 case VK_UP:
1097 if (!(dlgCode & DLGC_WANTARROWS))
1099 BOOL fPrevious = (msg->wParam == VK_LEFT || msg->wParam == VK_UP);
1100 HWND hwndNext = GetNextDlgGroupItem (hwndDlg, GetFocus(), fPrevious );
1101 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
1102 return TRUE;
1104 break;
1106 case VK_CANCEL:
1107 case VK_ESCAPE:
1108 SendMessageW( hwndDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hwndDlg, IDCANCEL ) );
1109 return TRUE;
1111 case VK_EXECUTE:
1112 case VK_RETURN:
1114 DWORD dw = SendMessageW( hwndDlg, DM_GETDEFID, 0, 0 );
1115 if (HIWORD(dw) == DC_HASDEFID)
1117 SendMessageW( hwndDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
1118 (LPARAM)GetDlgItem(hwndDlg, LOWORD(dw)));
1120 else
1122 SendMessageW( hwndDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndDlg, IDOK ) );
1126 return TRUE;
1128 break;
1130 case WM_CHAR:
1131 dlgCode = SendMessageW( msg->hwnd, WM_GETDLGCODE, msg->wParam, (LPARAM)msg );
1132 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
1133 if (msg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
1134 /* drop through */
1136 case WM_SYSCHAR:
1137 if (DIALOG_IsAccelerator( WIN_GetFullHandle(msg->hwnd), hwndDlg, msg->wParam ))
1139 /* don't translate or dispatch */
1140 return TRUE;
1142 break;
1145 TranslateMessage( msg );
1146 DispatchMessageW( msg );
1147 return TRUE;
1151 /***********************************************************************
1152 * GetDlgCtrlID (USER32.@)
1154 INT WINAPI GetDlgCtrlID( HWND hwnd )
1156 return GetWindowLongW( hwnd, GWL_ID );
1160 /***********************************************************************
1161 * GetDlgItem (USER32.@)
1163 HWND WINAPI GetDlgItem( HWND hwndDlg, INT id )
1165 int i;
1166 HWND *list = WIN_ListChildren( hwndDlg );
1167 HWND ret = 0;
1169 if (!list) return 0;
1171 for (i = 0; list[i]; i++) if (GetWindowLongW( list[i], GWL_ID ) == id) break;
1172 ret = list[i];
1173 HeapFree( GetProcessHeap(), 0, list );
1174 return ret;
1178 /*******************************************************************
1179 * SendDlgItemMessageA (USER32.@)
1181 LRESULT WINAPI SendDlgItemMessageA( HWND hwnd, INT id, UINT msg,
1182 WPARAM wParam, LPARAM lParam )
1184 HWND hwndCtrl = GetDlgItem( hwnd, id );
1185 if (hwndCtrl) return SendMessageA( hwndCtrl, msg, wParam, lParam );
1186 else return 0;
1190 /*******************************************************************
1191 * SendDlgItemMessageW (USER32.@)
1193 LRESULT WINAPI SendDlgItemMessageW( HWND hwnd, INT id, UINT msg,
1194 WPARAM wParam, LPARAM lParam )
1196 HWND hwndCtrl = GetDlgItem( hwnd, id );
1197 if (hwndCtrl) return SendMessageW( hwndCtrl, msg, wParam, lParam );
1198 else return 0;
1202 /*******************************************************************
1203 * SetDlgItemTextA (USER32.@)
1205 BOOL WINAPI SetDlgItemTextA( HWND hwnd, INT id, LPCSTR lpString )
1207 return SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1211 /*******************************************************************
1212 * SetDlgItemTextW (USER32.@)
1214 BOOL WINAPI SetDlgItemTextW( HWND hwnd, INT id, LPCWSTR lpString )
1216 return SendDlgItemMessageW( hwnd, id, WM_SETTEXT, 0, (LPARAM)lpString );
1220 /***********************************************************************
1221 * GetDlgItemTextA (USER32.@)
1223 INT WINAPI GetDlgItemTextA( HWND hwnd, INT id, LPSTR str, UINT len )
1225 return (INT)SendDlgItemMessageA( hwnd, id, WM_GETTEXT,
1226 len, (LPARAM)str );
1230 /***********************************************************************
1231 * GetDlgItemTextW (USER32.@)
1233 INT WINAPI GetDlgItemTextW( HWND hwnd, INT id, LPWSTR str, UINT len )
1235 return (INT)SendDlgItemMessageW( hwnd, id, WM_GETTEXT,
1236 len, (LPARAM)str );
1240 /*******************************************************************
1241 * SetDlgItemInt (USER32.@)
1243 BOOL WINAPI SetDlgItemInt( HWND hwnd, INT id, UINT value,
1244 BOOL fSigned )
1246 char str[20];
1248 if (fSigned) sprintf( str, "%d", (INT)value );
1249 else sprintf( str, "%u", value );
1250 SendDlgItemMessageA( hwnd, id, WM_SETTEXT, 0, (LPARAM)str );
1251 return TRUE;
1255 /***********************************************************************
1256 * GetDlgItemInt (USER32.@)
1258 UINT WINAPI GetDlgItemInt( HWND hwnd, INT id, BOOL *translated,
1259 BOOL fSigned )
1261 char str[30];
1262 char * endptr;
1263 long result = 0;
1265 if (translated) *translated = FALSE;
1266 if (!SendDlgItemMessageA(hwnd, id, WM_GETTEXT, sizeof(str), (LPARAM)str))
1267 return 0;
1268 if (fSigned)
1270 result = strtol( str, &endptr, 10 );
1271 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1272 return 0;
1273 if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno==ERANGE))
1274 return 0;
1276 else
1278 result = strtoul( str, &endptr, 10 );
1279 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1280 return 0;
1281 if ((result == ULONG_MAX) && (errno == ERANGE)) return 0;
1283 if (translated) *translated = TRUE;
1284 return (UINT)result;
1288 /***********************************************************************
1289 * CheckDlgButton (USER32.@)
1291 BOOL WINAPI CheckDlgButton( HWND hwnd, INT id, UINT check )
1293 SendDlgItemMessageA( hwnd, id, BM_SETCHECK, check, 0 );
1294 return TRUE;
1298 /***********************************************************************
1299 * IsDlgButtonChecked (USER32.@)
1301 UINT WINAPI IsDlgButtonChecked( HWND hwnd, UINT id )
1303 return (UINT)SendDlgItemMessageA( hwnd, id, BM_GETCHECK, 0, 0 );
1307 /***********************************************************************
1308 * CheckRB
1310 * Callback function used to check/uncheck radio buttons that fall
1311 * within a specific range of IDs.
1313 static BOOL CALLBACK CheckRB(HWND hwndChild, LPARAM lParam)
1315 LONG lChildID = GetWindowLongA(hwndChild, GWL_ID);
1316 RADIOGROUP *lpRadioGroup = (RADIOGROUP *) lParam;
1318 if ((lChildID >= lpRadioGroup->firstID) &&
1319 (lChildID <= lpRadioGroup->lastID))
1321 if (lChildID == lpRadioGroup->checkID)
1323 SendMessageA(hwndChild, BM_SETCHECK, BST_CHECKED, 0);
1325 else
1327 SendMessageA(hwndChild, BM_SETCHECK, BST_UNCHECKED, 0);
1331 return TRUE;
1335 /***********************************************************************
1336 * CheckRadioButton (USER32.@)
1338 BOOL WINAPI CheckRadioButton( HWND hwndDlg, UINT firstID,
1339 UINT lastID, UINT checkID )
1341 RADIOGROUP radioGroup;
1343 /* perform bounds checking for a radio button group */
1344 radioGroup.firstID = min(min(firstID, lastID), checkID);
1345 radioGroup.lastID = max(max(firstID, lastID), checkID);
1346 radioGroup.checkID = checkID;
1348 return EnumChildWindows(hwndDlg, (WNDENUMPROC)CheckRB,
1349 (LPARAM)&radioGroup);
1353 /***********************************************************************
1354 * GetDialogBaseUnits (USER.243)
1355 * GetDialogBaseUnits (USER32.@)
1357 DWORD WINAPI GetDialogBaseUnits(void)
1359 return MAKELONG( xBaseUnit, yBaseUnit );
1363 /***********************************************************************
1364 * MapDialogRect (USER32.@)
1366 BOOL WINAPI MapDialogRect( HWND hwnd, LPRECT rect )
1368 DIALOGINFO * dlgInfo;
1369 if (!(dlgInfo = DIALOG_get_info( hwnd ))) return FALSE;
1370 rect->left = MulDiv(rect->left, dlgInfo->xBaseUnit, 4);
1371 rect->right = MulDiv(rect->right, dlgInfo->xBaseUnit, 4);
1372 rect->top = MulDiv(rect->top, dlgInfo->yBaseUnit, 8);
1373 rect->bottom = MulDiv(rect->bottom, dlgInfo->yBaseUnit, 8);
1374 return TRUE;
1378 /***********************************************************************
1379 * GetNextDlgGroupItem (USER32.@)
1381 HWND WINAPI GetNextDlgGroupItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1383 HWND hwnd, retvalue;
1385 hwndDlg = WIN_GetFullHandle( hwndDlg );
1386 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1388 if(hwndCtrl)
1390 /* if the hwndCtrl is the child of the control in the hwndDlg,
1391 * then the hwndDlg has to be the parent of the hwndCtrl */
1392 if(GetParent(hwndCtrl) != hwndDlg && GetParent(GetParent(hwndCtrl)) == hwndDlg)
1393 hwndDlg = GetParent(hwndCtrl);
1396 if (hwndCtrl)
1398 /* Make sure hwndCtrl is a top-level child */
1399 HWND parent = GetParent( hwndCtrl );
1400 while (parent && parent != hwndDlg) parent = GetParent(parent);
1401 if (parent != hwndDlg) return 0;
1403 else
1405 /* No ctrl specified -> start from the beginning */
1406 if (!(hwndCtrl = GetWindow( hwndDlg, GW_CHILD ))) return 0;
1407 if (fPrevious) hwndCtrl = GetWindow( hwndCtrl, GW_HWNDLAST );
1410 retvalue = hwndCtrl;
1411 hwnd = GetWindow( hwndCtrl, GW_HWNDNEXT );
1412 while (1)
1414 if (!hwnd || (GetWindowLongW( hwnd, GWL_STYLE ) & WS_GROUP))
1416 /* Wrap-around to the beginning of the group */
1417 HWND tmp;
1419 hwnd = GetWindow( hwndDlg, GW_CHILD );
1420 for (tmp = hwnd; tmp; tmp = GetWindow( tmp, GW_HWNDNEXT ) )
1422 if (GetWindowLongW( tmp, GWL_STYLE ) & WS_GROUP) hwnd = tmp;
1423 if (tmp == hwndCtrl) break;
1426 if (hwnd == hwndCtrl) break;
1427 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1429 retvalue = hwnd;
1430 if (!fPrevious) break;
1432 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
1434 return retvalue;
1438 /***********************************************************************
1439 * DIALOG_GetNextTabItem
1441 * Helper for GetNextDlgTabItem
1443 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1445 LONG dsStyle;
1446 LONG exStyle;
1447 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1448 HWND retWnd = 0;
1449 HWND hChildFirst = 0;
1451 if(!hwndCtrl)
1453 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1454 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1456 else if (IsChild( hwndMain, hwndCtrl ))
1458 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1459 if(!hChildFirst)
1461 if(GetParent(hwndCtrl) != hwndMain)
1462 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1463 else
1465 if(fPrevious)
1466 hChildFirst = GetWindow(hwndCtrl,GW_HWNDLAST);
1467 else
1468 hChildFirst = GetWindow(hwndCtrl,GW_HWNDFIRST);
1473 while(hChildFirst)
1475 BOOL bCtrl = FALSE;
1476 while(hChildFirst)
1478 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1479 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1480 if( (dsStyle & DS_CONTROL || exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1482 bCtrl=TRUE;
1483 break;
1485 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1486 break;
1487 hChildFirst = GetWindow(hChildFirst,wndSearch);
1489 if(hChildFirst)
1491 if(bCtrl)
1492 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1493 else
1494 retWnd = hChildFirst;
1496 if(retWnd) break;
1497 hChildFirst = GetWindow(hChildFirst,wndSearch);
1499 if(!retWnd && hwndCtrl)
1501 HWND hParent = GetParent(hwndCtrl);
1502 while(hParent)
1504 if(hParent == hwndMain) break;
1505 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1506 if(retWnd) break;
1507 hParent = GetParent(hParent);
1509 if(!retWnd)
1510 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1512 return retWnd;
1515 /***********************************************************************
1516 * GetNextDlgTabItem (USER32.@)
1518 HWND WINAPI GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl,
1519 BOOL fPrevious )
1521 hwndDlg = WIN_GetFullHandle( hwndDlg );
1522 hwndCtrl = WIN_GetFullHandle( hwndCtrl );
1523 return DIALOG_GetNextTabItem(hwndDlg,hwndDlg,hwndCtrl,fPrevious);
1526 /**********************************************************************
1527 * DIALOG_DlgDirSelect
1529 * Helper function for DlgDirSelect*
1531 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPSTR str, INT len,
1532 INT id, BOOL unicode, BOOL combo )
1534 char *buffer, *ptr;
1535 INT item, size;
1536 BOOL ret;
1537 HWND listbox = GetDlgItem( hwnd, id );
1539 TRACE("%p '%s' %d\n", hwnd, str, id );
1540 if (!listbox) return FALSE;
1542 item = SendMessageA(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1543 if (item == LB_ERR) return FALSE;
1544 size = SendMessageA(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1545 if (size == LB_ERR) return FALSE;
1547 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1549 SendMessageA( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1551 if ((ret = (buffer[0] == '['))) /* drive or directory */
1553 if (buffer[1] == '-') /* drive */
1555 buffer[3] = ':';
1556 buffer[4] = 0;
1557 ptr = buffer + 2;
1559 else
1561 buffer[strlen(buffer)-1] = '\\';
1562 ptr = buffer + 1;
1565 else ptr = buffer;
1567 if (unicode)
1569 if (len > 0 && !MultiByteToWideChar( CP_ACP, 0, ptr, -1, (LPWSTR)str, len ))
1570 ((LPWSTR)str)[len-1] = 0;
1572 else lstrcpynA( str, ptr, len );
1573 HeapFree( GetProcessHeap(), 0, buffer );
1574 TRACE("Returning %d '%s'\n", ret, str );
1575 return ret;
1579 /**********************************************************************
1580 * DIALOG_DlgDirList
1582 * Helper function for DlgDirList*
1584 static INT DIALOG_DlgDirList( HWND hDlg, LPSTR spec, INT idLBox,
1585 INT idStatic, UINT attrib, BOOL combo )
1587 HWND hwnd;
1588 LPSTR orig_spec = spec;
1590 #define SENDMSG(msg,wparam,lparam) \
1591 ((attrib & DDL_POSTMSGS) ? PostMessageA( hwnd, msg, wparam, lparam ) \
1592 : SendMessageA( hwnd, msg, wparam, lparam ))
1594 TRACE("%p '%s' %d %d %04x\n",
1595 hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1597 /* If the path exists and is a directory, chdir to it */
1598 if (!spec || !spec[0] || SetCurrentDirectoryA( spec )) spec = "*.*";
1599 else
1601 char *p, *p2;
1602 p = spec;
1603 if ((p2 = strrchr( p, '\\' ))) p = p2;
1604 if ((p2 = strrchr( p, '/' ))) p = p2;
1605 if (p != spec)
1607 char sep = *p;
1608 *p = 0;
1609 if (!SetCurrentDirectoryA( spec ))
1611 *p = sep; /* Restore the original spec */
1612 return FALSE;
1614 spec = p + 1;
1618 TRACE( "mask=%s\n", spec );
1620 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1622 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1623 if (attrib & DDL_DIRECTORY)
1625 if (!(attrib & DDL_EXCLUSIVE))
1627 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1628 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1629 (LPARAM)spec ) == LB_ERR)
1630 return FALSE;
1632 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1633 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1634 (LPARAM)"*.*" ) == LB_ERR)
1635 return FALSE;
1637 else
1639 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1640 (LPARAM)spec ) == LB_ERR)
1641 return FALSE;
1645 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1647 char temp[MAX_PATH];
1648 GetCurrentDirectoryA( sizeof(temp), temp );
1649 CharLowerA( temp );
1650 /* Can't use PostMessage() here, because the string is on the stack */
1651 SetDlgItemTextA( hDlg, idStatic, temp );
1654 if (orig_spec && (spec != orig_spec))
1656 /* Update the original file spec */
1657 char *p = spec;
1658 while ((*orig_spec++ = *p++));
1661 return TRUE;
1662 #undef SENDMSG
1666 /**********************************************************************
1667 * DIALOG_DlgDirListW
1669 * Helper function for DlgDirList*W
1671 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1672 INT idStatic, UINT attrib, BOOL combo )
1674 if (spec)
1676 LPSTR specA = HEAP_strdupWtoA( GetProcessHeap(), 0, spec );
1677 INT ret = DIALOG_DlgDirList( hDlg, specA, idLBox, idStatic,
1678 attrib, combo );
1679 MultiByteToWideChar( CP_ACP, 0, specA, -1, spec, 0x7fffffff );
1680 HeapFree( GetProcessHeap(), 0, specA );
1681 return ret;
1683 return DIALOG_DlgDirList( hDlg, NULL, idLBox, idStatic, attrib, combo );
1687 /**********************************************************************
1688 * DlgDirSelectExA (USER32.@)
1690 BOOL WINAPI DlgDirSelectExA( HWND hwnd, LPSTR str, INT len, INT id )
1692 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, FALSE );
1696 /**********************************************************************
1697 * DlgDirSelectExW (USER32.@)
1699 BOOL WINAPI DlgDirSelectExW( HWND hwnd, LPWSTR str, INT len, INT id )
1701 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, FALSE );
1705 /**********************************************************************
1706 * DlgDirSelectComboBoxExA (USER32.@)
1708 BOOL WINAPI DlgDirSelectComboBoxExA( HWND hwnd, LPSTR str, INT len,
1709 INT id )
1711 return DIALOG_DlgDirSelect( hwnd, str, len, id, FALSE, TRUE );
1715 /**********************************************************************
1716 * DlgDirSelectComboBoxExW (USER32.@)
1718 BOOL WINAPI DlgDirSelectComboBoxExW( HWND hwnd, LPWSTR str, INT len,
1719 INT id)
1721 return DIALOG_DlgDirSelect( hwnd, (LPSTR)str, len, id, TRUE, TRUE );
1725 /**********************************************************************
1726 * DlgDirListA (USER32.@)
1728 INT WINAPI DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1729 INT idStatic, UINT attrib )
1731 return DIALOG_DlgDirList( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1735 /**********************************************************************
1736 * DlgDirListW (USER32.@)
1738 INT WINAPI DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1739 INT idStatic, UINT attrib )
1741 return DIALOG_DlgDirListW( hDlg, spec, idLBox, idStatic, attrib, FALSE );
1745 /**********************************************************************
1746 * DlgDirListComboBoxA (USER32.@)
1748 INT WINAPI DlgDirListComboBoxA( HWND hDlg, LPSTR spec, INT idCBox,
1749 INT idStatic, UINT attrib )
1751 return DIALOG_DlgDirList( hDlg, spec, idCBox, idStatic, attrib, TRUE );
1755 /**********************************************************************
1756 * DlgDirListComboBoxW (USER32.@)
1758 INT WINAPI DlgDirListComboBoxW( HWND hDlg, LPWSTR spec, INT idCBox,
1759 INT idStatic, UINT attrib )
1761 return DIALOG_DlgDirListW( hDlg, spec, idCBox, idStatic, attrib, TRUE );