- Enlarge window name buffer.
[reactos.git] / reactos / dll / win32 / user32 / windows / dialog.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/dialog.c
23 * PURPOSE: Input
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * Thomas Weidenmueller (w3seek@users.sourceforge.net)
26 * Steven Edwards (Steven_Ed4153@yahoo.com)
27 * UPDATE HISTORY:
28 * 07-26-2003 Code ported from wine
29 * 09-05-2001 CSH Created
30 */
31
32 /* INCLUDES ******************************************************************/
33
34 #include <user32.h>
35
36 #include <wine/debug.h>
37
38 /* MACROS/DEFINITIONS ********************************************************/
39
40 #define DF_END 0x0001
41 #define DF_OWNERENABLED 0x0002
42 #define CW_USEDEFAULT16 ((short)0x8000)
43 #define DWLP_ROS_DIALOGINFO (DWLP_USER+sizeof(ULONG_PTR))
44 #define GETDLGINFO(hwnd) (DIALOGINFO*)GetWindowLongPtrW((hwnd),DWLP_ROS_DIALOGINFO)
45 #define SETDLGINFO(hwnd, info) SetWindowLongPtrW((hwnd), DWLP_ROS_DIALOGINFO, (LONG_PTR)(info))
46 #define GET_WORD(ptr) (*(WORD *)(ptr))
47 #define GET_DWORD(ptr) (*(DWORD *)(ptr))
48 #define MAKEINTATOMA(atom) ((LPCSTR)((ULONG_PTR)((WORD)(atom))))
49 #define MAKEINTATOMW(atom) ((LPCWSTR)((ULONG_PTR)((WORD)(atom))))
50 #define DIALOG_CLASS_ATOMA MAKEINTATOMA(32770) /* Dialog */
51 #define DIALOG_CLASS_ATOMW MAKEINTATOMW(32770) /* Dialog */
52
53 void STDCALL WinPosActivateOtherWindow(HWND hwnd);
54
55 /* INTERNAL STRUCTS **********************************************************/
56
57 /* Dialog info structure */
58 typedef struct
59 {
60 HWND hwndFocus; /* Current control with focus */
61 HFONT hUserFont; /* Dialog font */
62 HMENU hMenu; /* Dialog menu */
63 UINT xBaseUnit; /* Dialog units (depends on the font) */
64 UINT yBaseUnit;
65 INT idResult; /* EndDialog() result / default pushbutton ID */
66 UINT flags; /* EndDialog() called for this dialog */
67 } DIALOGINFO;
68
69 /* Dialog control information */
70 typedef struct
71 {
72 DWORD style;
73 DWORD exStyle;
74 DWORD helpId;
75 short x;
76 short y;
77 short cx;
78 short cy;
79 UINT id;
80 LPCWSTR className;
81 LPCWSTR windowName;
82 BOOL windowNameFree;
83 LPCVOID data;
84 } DLG_CONTROL_INFO;
85
86 /* Dialog template */
87 typedef struct
88 {
89 DWORD style;
90 DWORD exStyle;
91 DWORD helpId;
92 WORD nbItems;
93 short x;
94 short y;
95 short cx;
96 short cy;
97 LPCWSTR menuName;
98 LPCWSTR className;
99 LPCWSTR caption;
100 WORD pointSize;
101 WORD weight;
102 BOOL italic;
103 LPCWSTR faceName;
104 BOOL dialogEx;
105 } DLG_TEMPLATE;
106
107 /* GetDlgItem structure */
108 typedef struct
109 {
110 INT nIDDlgItem;
111 HWND control;
112 } GETDLGITEMINFO;
113
114 /* CheckRadioButton structure */
115 typedef struct
116 {
117 UINT firstID;
118 UINT lastID;
119 UINT checkID;
120 } RADIOGROUP;
121
122
123 /*********************************************************************
124 * dialog class descriptor
125 */
126 const struct builtin_class_descr DIALOG_builtin_class =
127 {
128 DIALOG_CLASS_ATOMW, /* name */
129 CS_SAVEBITS | CS_DBLCLKS, /* style */
130 (WNDPROC) DefDlgProcW, /* procW */
131 (WNDPROC) DefDlgProcA, /* procA */
132 DLGWINDOWEXTRA, /* extra */
133 (LPCWSTR) IDC_ARROW, /* cursor */
134 0 /* brush */
135 };
136
137
138 /* INTERNAL FUNCTIONS ********************************************************/
139
140 /***********************************************************************
141 * DIALOG_EnableOwner
142 *
143 * Helper function for modal dialogs to enable again the
144 * owner of the dialog box.
145 */
146 void DIALOG_EnableOwner( HWND hOwner )
147 {
148 /* Owner must be a top-level window */
149 if (hOwner)
150 hOwner = GetAncestor( hOwner, GA_ROOT );
151 if (!hOwner) return;
152 EnableWindow( hOwner, TRUE );
153 }
154
155 /***********************************************************************
156 * DIALOG_DisableOwner
157 *
158 * Helper function for modal dialogs to disable the
159 * owner of the dialog box. Returns TRUE if owner was enabled.
160 */
161 BOOL DIALOG_DisableOwner( HWND hOwner )
162 {
163 /* Owner must be a top-level window */
164 if (hOwner)
165 hOwner = GetAncestor( hOwner, GA_ROOT );
166 if (!hOwner) return FALSE;
167 if (IsWindowEnabled( hOwner ))
168 {
169 EnableWindow( hOwner, FALSE );
170 return TRUE;
171 }
172 else
173 return FALSE;
174 }
175
176 /***********************************************************************
177 * DIALOG_GetControl32
178 *
179 * Return the class and text of the control pointed to by ptr,
180 * fill the header structure and return a pointer to the next control.
181 */
182 static const WORD *DIALOG_GetControl32( const WORD *p, DLG_CONTROL_INFO *info,
183 BOOL dialogEx )
184 {
185 if (dialogEx)
186 {
187 info->helpId = GET_DWORD(p); p += 2;
188 info->exStyle = GET_DWORD(p); p += 2;
189 info->style = GET_DWORD(p); p += 2;
190 }
191 else
192 {
193 info->helpId = 0;
194 info->style = GET_DWORD(p); p += 2;
195 info->exStyle = GET_DWORD(p); p += 2;
196 }
197 info->x = GET_WORD(p); p++;
198 info->y = GET_WORD(p); p++;
199 info->cx = GET_WORD(p); p++;
200 info->cy = GET_WORD(p); p++;
201
202 if (dialogEx)
203 {
204 /* id is a DWORD for DIALOGEX */
205 info->id = GET_DWORD(p);
206 p += 2;
207 }
208 else
209 {
210 info->id = GET_WORD(p);
211 p++;
212 }
213
214 if (GET_WORD(p) == 0xffff)
215 {
216 static const WCHAR class_names[6][10] =
217 {
218 { 'B','u','t','t','o','n', }, /* 0x80 */
219 { 'E','d','i','t', }, /* 0x81 */
220 { 'S','t','a','t','i','c', }, /* 0x82 */
221 { 'L','i','s','t','B','o','x', }, /* 0x83 */
222 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
223 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
224 };
225 WORD id = GET_WORD(p+1);
226 /* Windows treats dialog control class ids 0-5 same way as 0x80-0x85 */
227 if ((id >= 0x80) && (id <= 0x85)) id -= 0x80;
228 if (id <= 5)
229 info->className = class_names[id];
230 else
231 {
232 info->className = NULL;
233 /* FIXME: load other classes here? */
234 }
235 p += 2;
236 }
237 else
238 {
239 info->className = (LPCWSTR)p;
240 p += wcslen( info->className ) + 1;
241 }
242
243 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
244 {
245 info->windowName = HeapAlloc( GetProcessHeap(), 0, sizeof(L"#65535") );
246 swprintf((LPWSTR)info->windowName, L"#%d", GET_WORD(p + 1));
247 info->windowNameFree = TRUE;
248 p += 2;
249 }
250 else
251 {
252 info->windowName = (LPCWSTR)p;
253 info->windowNameFree = FALSE;
254 p += wcslen( info->windowName ) + 1;
255 }
256
257 if (GET_WORD(p))
258 {
259 info->data = p + 1;
260 p += GET_WORD(p) / sizeof(WORD);
261 }
262 else info->data = NULL;
263 p++;
264
265 /* Next control is on dword boundary */
266 return (const WORD *)((((int)p) + 3) & ~3);
267 }
268
269 /***********************************************************************
270 * DIALOG_CreateControls32
271 *
272 * Create the control windows for a dialog.
273 */
274 static BOOL DIALOG_CreateControls32( HWND hwnd, LPCSTR template, const DLG_TEMPLATE *dlgTemplate,
275 HINSTANCE hInst, BOOL unicode )
276 {
277 DIALOGINFO * dlgInfo;
278 DLG_CONTROL_INFO info;
279 HWND hwndCtrl, hwndDefButton = 0;
280 INT items = dlgTemplate->nbItems;
281
282 if (!(dlgInfo = GETDLGINFO(hwnd))) return -1;
283
284 while (items--)
285 {
286 template = (LPCSTR)DIALOG_GetControl32( (WORD *)template, &info,
287 dlgTemplate->dialogEx );
288 /* Is this it? */
289 if (info.style & WS_BORDER)
290 {
291 info.style &= ~WS_BORDER;
292 info.exStyle |= WS_EX_CLIENTEDGE;
293 }
294 if (unicode)
295 {
296 hwndCtrl = CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
297 info.className, info.windowName,
298 info.style | WS_CHILD,
299 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
300 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
301 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
302 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
303 hwnd, (HMENU)info.id,
304 hInst, (LPVOID)info.data );
305 }
306 else
307 {
308 LPSTR class = (LPSTR)info.className;
309 LPSTR caption = (LPSTR)info.windowName;
310
311 if (HIWORD(class))
312 {
313 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.className, -1, NULL, 0, NULL, NULL );
314 class = HeapAlloc( GetProcessHeap(), 0, len );
315 WideCharToMultiByte( CP_ACP, 0, info.className, -1, class, len, NULL, NULL );
316 }
317 if (HIWORD(caption))
318 {
319 DWORD len = WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, NULL, 0, NULL, NULL );
320 caption = HeapAlloc( GetProcessHeap(), 0, len );
321 WideCharToMultiByte( CP_ACP, 0, info.windowName, -1, caption, len, NULL, NULL );
322 }
323 hwndCtrl = CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
324 class, caption, info.style | WS_CHILD,
325 MulDiv(info.x, dlgInfo->xBaseUnit, 4),
326 MulDiv(info.y, dlgInfo->yBaseUnit, 8),
327 MulDiv(info.cx, dlgInfo->xBaseUnit, 4),
328 MulDiv(info.cy, dlgInfo->yBaseUnit, 8),
329 hwnd, (HMENU)info.id,
330 hInst, (LPVOID)info.data );
331 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
332 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
333 }
334
335 if (info.windowNameFree)
336 {
337 HeapFree( GetProcessHeap(), 0, (LPVOID)info.windowName );
338 }
339
340 if (!hwndCtrl)
341 {
342 if (dlgTemplate->style & DS_NOFAILCREATE) continue;
343 return FALSE;
344 }
345
346 /* Send initialisation messages to the control */
347 if (dlgInfo->hUserFont) SendMessageW( hwndCtrl, WM_SETFONT,
348 (WPARAM)dlgInfo->hUserFont, 0 );
349 if (SendMessageW(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
350 {
351 /* If there's already a default push-button, set it back */
352 /* to normal and use this one instead. */
353 if (hwndDefButton)
354 SendMessageW( hwndDefButton, BM_SETSTYLE, BS_PUSHBUTTON, FALSE );
355 hwndDefButton = hwndCtrl;
356 dlgInfo->idResult = GetWindowLongPtrA( hwndCtrl, GWLP_ID );
357 }
358 }
359 return TRUE;
360 }
361
362 /***********************************************************************
363 * DIALOG_FindMsgDestination
364 *
365 * The messages that IsDialogMessage sends may not go to the dialog
366 * calling IsDialogMessage if that dialog is a child, and it has the
367 * DS_CONTROL style set.
368 * We propagate up until we hit one that does not have DS_CONTROL, or
369 * whose parent is not a dialog.
370 *
371 * This is undocumented behaviour.
372 */
373 static HWND DIALOG_FindMsgDestination( HWND hwndDlg )
374 {
375 while (GetWindowLongA(hwndDlg, GWL_STYLE) & DS_CONTROL)
376 {
377 HWND hParent = GetParent(hwndDlg);
378 if (!hParent) break;
379
380 if (!IsWindow(hParent)) break;
381
382 if (!GETDLGINFO(hParent)) /* TODO: Correct? */
383 {
384 break;
385 }
386
387 hwndDlg = hParent;
388 }
389
390 return hwndDlg;
391 }
392
393 /***********************************************************************
394 * DIALOG_IsAccelerator
395 */
396 static BOOL DIALOG_IsAccelerator( HWND hwnd, HWND hwndDlg, WPARAM wParam )
397 {
398 HWND hwndControl = hwnd;
399 HWND hwndNext;
400 INT dlgCode;
401 WCHAR buffer[128];
402
403 do
404 {
405 DWORD style = GetWindowLongW( hwndControl, GWL_STYLE );
406 if ((style & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE)
407 {
408 dlgCode = SendMessageA( hwndControl, WM_GETDLGCODE, 0, 0 );
409 if ( (dlgCode & (DLGC_BUTTON | DLGC_STATIC)) &&
410 GetWindowTextW( hwndControl, buffer, sizeof(buffer)/sizeof(WCHAR) ))
411 {
412 /* find the accelerator key */
413 LPWSTR p = buffer - 2;
414
415 do
416 {
417 p = wcschr( p + 2, '&' );
418 }
419 while (p != NULL && p[1] == '&');
420
421 /* and check if it's the one we're looking for */
422 /* FIXME: usage of towupper correct? */
423 if (p != NULL && towupper( p[1] ) == towupper( wParam ) )
424 {
425 if ((dlgCode & DLGC_STATIC) || (style & 0x0f) == BS_GROUPBOX )
426 {
427 /* set focus to the control */
428 SendMessageW( hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, 1);
429 /* and bump it on to next */
430 SendMessageW( hwndDlg, WM_NEXTDLGCTL, 0, 0);
431 }
432 else if (dlgCode & DLGC_BUTTON)
433 {
434 /* send BM_CLICK message to the control */
435 SendMessageW( hwndControl, BM_CLICK, 0, 0 );
436 }
437 return TRUE;
438 }
439 }
440 hwndNext = GetWindow( hwndControl, GW_CHILD );
441 }
442 else hwndNext = 0;
443
444 if (!hwndNext) hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
445
446 while (!hwndNext && hwndControl)
447 {
448 hwndControl = GetParent( hwndControl );
449 if (hwndControl == hwndDlg)
450 {
451 if(hwnd==hwndDlg) /* prevent endless loop */
452 {
453 hwndNext=hwnd;
454 break;
455 }
456 hwndNext = GetWindow( hwndDlg, GW_CHILD );
457 }
458 else
459 hwndNext = GetWindow( hwndControl, GW_HWNDNEXT );
460 }
461 hwndControl = hwndNext;
462 }
463 while (hwndControl && (hwndControl != hwnd));
464
465 return FALSE;
466 }
467
468 /***********************************************************************
469 * DIALOG_DoDialogBox
470 */
471 INT DIALOG_DoDialogBox( HWND hwnd, HWND owner )
472 {
473 DIALOGINFO * dlgInfo;
474 MSG msg;
475 INT retval;
476 HWND ownerMsg = GetAncestor( owner, GA_ROOT );
477 BOOL bFirstEmpty;
478
479 if (!(dlgInfo = GETDLGINFO(hwnd))) return -1;
480
481 bFirstEmpty = TRUE;
482 if (!(dlgInfo->flags & DF_END)) /* was EndDialog called in WM_INITDIALOG ? */
483 {
484 for (;;)
485 {
486 if (!PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
487 {
488 if (bFirstEmpty)
489 {
490 /* ShowWindow the first time the queue goes empty */
491 ShowWindow( hwnd, SW_SHOWNORMAL );
492 bFirstEmpty = FALSE;
493 }
494 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & DS_NOIDLEMSG))
495 {
496 /* No message present -> send ENTERIDLE and wait */
497 SendMessageW( ownerMsg, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)hwnd );
498 }
499 if (!GetMessageW( &msg, 0, 0, 0 )) break;
500 }
501
502 if (!IsWindow( hwnd )) return -1;
503 if (!(dlgInfo->flags & DF_END) && !IsDialogMessageW( hwnd, &msg))
504 {
505 TranslateMessage( &msg );
506 DispatchMessageW( &msg );
507 }
508 if (dlgInfo->flags & DF_END) break;
509 }
510 }
511 if (dlgInfo->flags & DF_OWNERENABLED) DIALOG_EnableOwner( owner );
512 retval = dlgInfo->idResult;
513 DestroyWindow( hwnd );
514 return retval;
515 }
516
517 /***********************************************************************
518 * DIALOG_ParseTemplate32
519 *
520 * Fill a DLG_TEMPLATE structure from the dialog template, and return
521 * a pointer to the first control.
522 */
523 static LPCSTR DIALOG_ParseTemplate32( LPCSTR template, DLG_TEMPLATE * result )
524 {
525 const WORD *p = (const WORD *)template;
526 WORD signature;
527 WORD dlgver;
528
529 signature = GET_WORD(p); p++;
530 dlgver = GET_WORD(p); p++;
531
532 if (signature == 1 && dlgver == 0xffff) /* DIALOGEX resource */
533 {
534 result->dialogEx = TRUE;
535 result->helpId = GET_DWORD(p); p += 2;
536 result->exStyle = GET_DWORD(p); p += 2;
537 result->style = GET_DWORD(p); p += 2;
538 }
539 else
540 {
541 result->style = GET_DWORD(p - 2);
542 result->dialogEx = FALSE;
543 result->helpId = 0;
544 result->exStyle = GET_DWORD(p); p += 2;
545 }
546 result->nbItems = GET_WORD(p); p++;
547 result->x = GET_WORD(p); p++;
548 result->y = GET_WORD(p); p++;
549 result->cx = GET_WORD(p); p++;
550 result->cy = GET_WORD(p); p++;
551
552 /* Get the menu name */
553
554 switch(GET_WORD(p))
555 {
556 case 0x0000:
557 result->menuName = NULL;
558 p++;
559 break;
560 case 0xffff:
561 result->menuName = (LPCWSTR)(UINT)GET_WORD( p + 1 );
562 p += 2;
563 break;
564 default:
565 result->menuName = (LPCWSTR)p;
566 p += wcslen( result->menuName ) + 1;
567 break;
568 }
569
570 /* Get the class name */
571
572 switch(GET_WORD(p))
573 {
574 case 0x0000:
575 result->className = DIALOG_CLASS_ATOMW;
576 p++;
577 break;
578 case 0xffff:
579 result->className = (LPCWSTR)(UINT)GET_WORD( p + 1 );
580 p += 2;
581 break;
582 default:
583 result->className = (LPCWSTR)p;
584 p += wcslen( result->className ) + 1;
585 break;
586 }
587
588 /* Get the window caption */
589
590 result->caption = (LPCWSTR)p;
591 p += wcslen( result->caption ) + 1;
592
593 /* Get the font name */
594
595 result->pointSize = 0;
596 result->weight = FW_DONTCARE;
597 result->italic = FALSE;
598 result->faceName = NULL;
599
600 if (result->style & DS_SETFONT)
601 {
602 result->pointSize = GET_WORD(p);
603 p++;
604 if (result->dialogEx)
605 {
606 result->weight = GET_WORD(p); p++;
607 result->italic = LOBYTE(GET_WORD(p)); p++;
608 }
609 result->faceName = (LPCWSTR)p;
610 p += wcslen( result->faceName ) + 1;
611 }
612
613 /* First control is on dword boundary */
614 return (LPCSTR)((((int)p) + 3) & ~3);
615 }
616
617 /***********************************************************************
618 * DIALOG_CreateIndirect
619 * Creates a dialog box window
620 *
621 * modal = TRUE if we are called from a modal dialog box.
622 * (it's more compatible to do it here, as under Windows the owner
623 * is never disabled if the dialog fails because of an invalid template)
624 */
625 static HWND DIALOG_CreateIndirect( HINSTANCE hInst, LPCVOID dlgTemplate,
626 HWND owner, DLGPROC dlgProc, LPARAM param,
627 BOOL unicode, BOOL modal )
628 {
629 HWND hwnd;
630 RECT rect;
631 DLG_TEMPLATE template;
632 DIALOGINFO * dlgInfo = NULL;
633 DWORD units = GetDialogBaseUnits();
634 BOOL ownerEnabled = TRUE;
635 HMENU hMenu = 0;
636 HFONT hUserFont = 0;
637 UINT flags = 0;
638 UINT xBaseUnit = LOWORD(units);
639 UINT yBaseUnit = HIWORD(units);
640
641 /* Parse dialog template */
642
643 if (!dlgTemplate) return 0;
644 dlgTemplate = DIALOG_ParseTemplate32( dlgTemplate, &template );
645
646 /* Load menu */
647
648 if (template.menuName) hMenu = LoadMenuW( hInst, template.menuName );
649
650 /* Create custom font if needed */
651
652 if (template.style & DS_SETFONT)
653 {
654 /* We convert the size to pixels and then make it -ve. This works
655 * for both +ve and -ve template.pointSize */
656 HDC dc;
657 int pixels;
658 dc = GetDC(0);
659 pixels = MulDiv(template.pointSize, GetDeviceCaps(dc , LOGPIXELSY), 72);
660 hUserFont = CreateFontW( -pixels, 0, 0, 0, template.weight,
661 template.italic, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
662 PROOF_QUALITY, FF_DONTCARE,
663 template.faceName );
664 if (hUserFont)
665 {
666 SIZE charSize;
667 HFONT hOldFont = SelectObject( dc, hUserFont );
668 charSize.cx = GdiGetCharDimensions( dc, NULL, &charSize.cy );
669 if (charSize.cx)
670 {
671 xBaseUnit = charSize.cx;
672 yBaseUnit = charSize.cy;
673 }
674 SelectObject( dc, hOldFont );
675 }
676 ReleaseDC(0, dc);
677 TRACE("units = %d,%d\n", xBaseUnit, yBaseUnit );
678 }
679
680 /* Create dialog main window */
681
682 rect.left = rect.top = 0;
683 rect.right = MulDiv(template.cx, xBaseUnit, 4);
684 rect.bottom = MulDiv(template.cy, yBaseUnit, 8);
685 if (template.style & WS_CHILD)
686 template.style &= ~(WS_CAPTION|WS_SYSMENU);
687 if (template.style & DS_MODALFRAME)
688 template.exStyle |= WS_EX_DLGMODALFRAME;
689 if (template.style & DS_CONTROL)
690 template.exStyle |= WS_EX_CONTROLPARENT;
691 AdjustWindowRectEx( &rect, template.style, (hMenu != 0), template.exStyle );
692 rect.right -= rect.left;
693 rect.bottom -= rect.top;
694
695 if (template.x == CW_USEDEFAULT16)
696 {
697 rect.left = rect.top = CW_USEDEFAULT;
698 }
699 else
700 {
701 if (template.style & DS_CENTER)
702 {
703 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
704 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
705 }
706 else
707 {
708 rect.left += MulDiv(template.x, xBaseUnit, 4);
709 rect.top += MulDiv(template.y, yBaseUnit, 8);
710 }
711 if ( !(template.style & WS_CHILD) )
712 {
713 INT dX, dY;
714
715 if( !(template.style & DS_ABSALIGN) )
716 ClientToScreen( owner, (POINT *)&rect );
717
718 /* try to fit it into the desktop */
719
720 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
721 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
722 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
723 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
724 if( rect.left < 0 ) rect.left = 0;
725 if( rect.top < 0 ) rect.top = 0;
726 }
727 }
728
729 if (modal)
730 {
731 ownerEnabled = DIALOG_DisableOwner( owner );
732 if (ownerEnabled) flags |= DF_OWNERENABLED;
733 }
734
735 if (unicode)
736 {
737 hwnd = CreateWindowExW(template.exStyle, template.className, template.caption,
738 template.style & ~WS_VISIBLE,
739 rect.left, rect.top, rect.right, rect.bottom,
740 owner, hMenu, hInst, NULL );
741 }
742 else
743 {
744 LPSTR class = (LPSTR)template.className;
745 LPSTR caption = (LPSTR)template.caption;
746
747 if (HIWORD(class))
748 {
749 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.className, -1, NULL, 0, NULL, NULL );
750 class = HeapAlloc( GetProcessHeap(), 0, len );
751 WideCharToMultiByte( CP_ACP, 0, template.className, -1, class, len, NULL, NULL );
752 }
753 if (HIWORD(caption))
754 {
755 DWORD len = WideCharToMultiByte( CP_ACP, 0, template.caption, -1, NULL, 0, NULL, NULL );
756 caption = HeapAlloc( GetProcessHeap(), 0, len );
757 WideCharToMultiByte( CP_ACP, 0, template.caption, -1, caption, len, NULL, NULL );
758 }
759 hwnd = CreateWindowExA(template.exStyle, class, caption,
760 template.style & ~WS_VISIBLE,
761 rect.left, rect.top, rect.right, rect.bottom,
762 owner, hMenu, hInst, NULL );
763 if (HIWORD(class)) HeapFree( GetProcessHeap(), 0, class );
764 if (HIWORD(caption)) HeapFree( GetProcessHeap(), 0, caption );
765 }
766
767 if (!hwnd)
768 {
769 if (hUserFont) DeleteObject( hUserFont );
770 if (hMenu) DestroyMenu( hMenu );
771 if (modal && (flags & DF_OWNERENABLED)) DIALOG_EnableOwner(owner);
772 return 0;
773 }
774
775 /* moved this from the top of the method to here as DIALOGINFO structure
776 will be valid only after WM_CREATE message has been handled in DefDlgProc
777 All the members of the structure get filled here using temp variables */
778
779 // dlgInfo = DIALOG_get_info( hwnd, TRUE );
780
781 if (!(dlgInfo = HeapAlloc( GetProcessHeap(), 0, sizeof(*dlgInfo) ))) return 0;
782 SETDLGINFO(hwnd, dlgInfo);
783
784 dlgInfo->hwndFocus = 0;
785 dlgInfo->hUserFont = hUserFont;
786 dlgInfo->hMenu = hMenu;
787 dlgInfo->xBaseUnit = xBaseUnit;
788 dlgInfo->yBaseUnit = yBaseUnit;
789 dlgInfo->idResult = 0;
790 dlgInfo->flags = flags;
791 // dlgInfo->hDialogHeap = 0;
792
793 if (template.helpId) SetWindowContextHelpId( hwnd, template.helpId );
794
795 if (unicode) SetWindowLongPtrW( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
796 else SetWindowLongPtrA( hwnd, DWLP_DLGPROC, (ULONG_PTR)dlgProc );
797
798 if (dlgInfo->hUserFont)
799 SendMessageW( hwnd, WM_SETFONT, (WPARAM)dlgInfo->hUserFont, 0 );
800
801 /* Create controls */
802
803 if (DIALOG_CreateControls32( hwnd, dlgTemplate, &template, hInst, unicode ))
804 {
805 /* Send initialisation messages and set focus */
806
807 if (SendMessageW( hwnd, WM_INITDIALOG, (WPARAM)dlgInfo->hwndFocus, param ) &&
808 ((~template.style & DS_CONTROL) || (template.style & WS_VISIBLE)))
809 {
810 /* By returning TRUE, app has requested a default focus assignment */
811 dlgInfo->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE);
812 if( dlgInfo->hwndFocus )
813 SetFocus( dlgInfo->hwndFocus );
814 }
815
816 if (template.style & WS_VISIBLE && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
817 {
818 ShowWindow( hwnd, SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
819 }
820 return hwnd;
821 }
822 if (modal && ownerEnabled) DIALOG_EnableOwner(owner);
823 if( IsWindow(hwnd) ) DestroyWindow( hwnd );
824 return 0;
825 }
826
827 /***********************************************************************
828 * DEFDLG_SetFocus
829 *
830 * Set the focus to a control of the dialog, selecting the text if
831 * the control is an edit dialog.
832 */
833 static void DEFDLG_SetFocus( HWND hwndDlg, HWND hwndCtrl )
834 {
835 HWND hwndPrev = GetFocus();
836
837 if (IsChild( hwndDlg, hwndPrev ))
838 {
839 if (SendMessageW( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
840 SendMessageW( hwndPrev, EM_SETSEL, -1, 0 );
841 }
842 if (SendMessageW( hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
843 SendMessageW( hwndCtrl, EM_SETSEL, 0, -1 );
844 SetFocus( hwndCtrl );
845 }
846
847 /***********************************************************************
848 * DEFDLG_SaveFocus
849 */
850 static void DEFDLG_SaveFocus( HWND hwnd )
851 {
852 DIALOGINFO *infoPtr;
853 HWND hwndFocus = GetFocus();
854
855 if (!hwndFocus || !IsChild( hwnd, hwndFocus )) return;
856 if (!(infoPtr = GETDLGINFO(hwnd))) return;
857 infoPtr->hwndFocus = hwndFocus;
858 /* Remove default button */
859 }
860
861 /***********************************************************************
862 * DEFDLG_RestoreFocus
863 */
864 static void DEFDLG_RestoreFocus( HWND hwnd )
865 {
866 DIALOGINFO *infoPtr;
867
868 if (IsIconic( hwnd )) return;
869 if (!(infoPtr = GETDLGINFO(hwnd))) return;
870 /* Don't set the focus back to controls if EndDialog is already called.*/
871 if (infoPtr->flags & DF_END) return;
872 if (!IsWindow(infoPtr->hwndFocus) || infoPtr->hwndFocus == hwnd) {
873 /* If no saved focus control exists, set focus to the first visible,
874 non-disabled, WS_TABSTOP control in the dialog */
875 infoPtr->hwndFocus = GetNextDlgTabItem( hwnd, 0, FALSE );
876 if (!IsWindow( infoPtr->hwndFocus )) return;
877 }
878 SetFocus( infoPtr->hwndFocus );
879
880 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
881 sometimes losing focus when receiving WM_SETFOCUS messages. */
882 }
883
884 /***********************************************************************
885 * DEFDLG_FindDefButton
886 *
887 * Find the current default push-button.
888 */
889 static HWND DEFDLG_FindDefButton( HWND hwndDlg )
890 {
891 HWND hwndChild, hwndTmp;
892
893 hwndChild = GetWindow( hwndDlg, GW_CHILD );
894 while (hwndChild)
895 {
896 if (SendMessageW( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
897 break;
898
899 /* Recurse into WS_EX_CONTROLPARENT controls */
900 if (GetWindowLongW( hwndChild, GWL_EXSTYLE ) & WS_EX_CONTROLPARENT)
901 {
902 LONG dsStyle = GetWindowLongW( hwndChild, GWL_STYLE );
903 if ((dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED) &&
904 (hwndTmp = DEFDLG_FindDefButton(hwndChild)) != NULL)
905 return hwndTmp;
906 }
907 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
908 }
909 return hwndChild;
910 }
911
912 /***********************************************************************
913 * DEFDLG_SetDefId
914 *
915 * Set the default button id.
916 */
917 static BOOL DEFDLG_SetDefId( HWND hwndDlg, DIALOGINFO *dlgInfo, WPARAM wParam)
918 {
919 DWORD dlgcode=0; /* initialize just to avoid a warning */
920 HWND hwndOld, hwndNew = GetDlgItem(hwndDlg, wParam);
921 INT old_id = dlgInfo->idResult;
922
923 dlgInfo->idResult = wParam;
924 if (hwndNew &&
925 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
926 & (DLGC_UNDEFPUSHBUTTON | DLGC_BUTTON)))
927 return FALSE; /* Destination is not a push button */
928
929 /* Make sure the old default control is a valid push button ID */
930 hwndOld = GetDlgItem( hwndDlg, old_id );
931 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
932 hwndOld = DEFDLG_FindDefButton( hwndDlg );
933 if (hwndOld && hwndOld != hwndNew)
934 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
935
936 if (hwndNew)
937 {
938 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
939 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
940 }
941 return TRUE;
942 }
943
944 /***********************************************************************
945 * DEFDLG_SetDefButton
946 *
947 * Set the new default button to be hwndNew.
948 */
949 static BOOL DEFDLG_SetDefButton( HWND hwndDlg, DIALOGINFO *dlgInfo, HWND hwndNew )
950 {
951 DWORD dlgcode=0; /* initialize just to avoid a warning */
952 HWND hwndOld = GetDlgItem( hwndDlg, dlgInfo->idResult );
953
954 if (hwndNew &&
955 !((dlgcode=SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 ))
956 & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON)))
957 {
958 /**
959 * Need to draw only default push button rectangle.
960 * Since the next control is not a push button, need to draw the push
961 * button rectangle for the default control.
962 */
963 hwndNew = hwndOld;
964 dlgcode = SendMessageW(hwndNew, WM_GETDLGCODE, 0, 0 );
965 }
966
967 /* Make sure the old default control is a valid push button ID */
968 if (!hwndOld || !(SendMessageW( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
969 hwndOld = DEFDLG_FindDefButton( hwndDlg );
970 if (hwndOld && hwndOld != hwndNew)
971 SendMessageW( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
972
973 if (hwndNew)
974 {
975 if(dlgcode & DLGC_UNDEFPUSHBUTTON)
976 SendMessageW( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
977 }
978 return TRUE;
979 }
980
981 /***********************************************************************
982 * DEFDLG_Proc
983 *
984 * Implementation of DefDlgProc(). Only handle messages that need special
985 * handling for dialogs.
986 */
987 static LRESULT DEFDLG_Proc( HWND hwnd, UINT msg, WPARAM wParam,
988 LPARAM lParam, DIALOGINFO *dlgInfo )
989 {
990 switch(msg)
991 {
992 case WM_ERASEBKGND:
993 {
994 HBRUSH brush = (HBRUSH)SendMessageW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
995 if (!brush) brush = (HBRUSH)DefWindowProcW( hwnd, WM_CTLCOLORDLG, wParam, (LPARAM)hwnd );
996 if (brush)
997 {
998 RECT rect;
999 HDC hdc = (HDC)wParam;
1000 GetClientRect( hwnd, &rect );
1001 DPtoLP( hdc, (LPPOINT)&rect, 2 );
1002 FillRect( hdc, &rect, brush );
1003 }
1004 return 1;
1005 }
1006 case WM_NCDESTROY:
1007 if ((dlgInfo = (DIALOGINFO *)SetWindowLongPtrW( hwnd, DWLP_ROS_DIALOGINFO, 0 )))
1008 {
1009 /* Free dialog heap (if created) */
1010 /*if (dlgInfo->hDialogHeap)
1011 {
1012 GlobalUnlock16(dlgInfo->hDialogHeap);
1013 GlobalFree16(dlgInfo->hDialogHeap);
1014 }*/
1015 if (dlgInfo->hUserFont) DeleteObject( dlgInfo->hUserFont );
1016 if (dlgInfo->hMenu) DestroyMenu( dlgInfo->hMenu );
1017 HeapFree( GetProcessHeap(), 0, dlgInfo );
1018 }
1019 /* Window clean-up */
1020 return DefWindowProcA( hwnd, msg, wParam, lParam );
1021
1022 case WM_SHOWWINDOW:
1023 if (!wParam) DEFDLG_SaveFocus( hwnd );
1024 return DefWindowProcA( hwnd, msg, wParam, lParam );
1025
1026 case WM_ACTIVATE:
1027 if (wParam) DEFDLG_RestoreFocus( hwnd );
1028 else DEFDLG_SaveFocus( hwnd );
1029 return 0;
1030
1031 case WM_SETFOCUS:
1032 DEFDLG_RestoreFocus( hwnd );
1033 return 0;
1034
1035 case DM_SETDEFID:
1036 if (dlgInfo && !(dlgInfo->flags & DF_END))
1037 DEFDLG_SetDefId( hwnd, dlgInfo, wParam );
1038 return 1;
1039
1040 case DM_GETDEFID:
1041 if (dlgInfo && !(dlgInfo->flags & DF_END))
1042 {
1043 HWND hwndDefId;
1044 if (dlgInfo->idResult)
1045 return MAKELONG( dlgInfo->idResult, DC_HASDEFID );
1046 if ((hwndDefId = DEFDLG_FindDefButton( hwnd )))
1047 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
1048 }
1049 return 0;
1050
1051 case WM_NEXTDLGCTL:
1052 if (dlgInfo)
1053 {
1054 HWND hwndDest = (HWND)wParam;
1055 if (!lParam)
1056 hwndDest = GetNextDlgTabItem(hwnd, GetFocus(), wParam);
1057 if (hwndDest) DEFDLG_SetFocus( hwnd, hwndDest );
1058 DEFDLG_SetDefButton( hwnd, dlgInfo, hwndDest );
1059 }
1060 return 0;
1061
1062 case WM_ENTERMENULOOP:
1063 case WM_LBUTTONDOWN:
1064 case WM_NCLBUTTONDOWN:
1065 {
1066 HWND hwndFocus = GetFocus();
1067 if (hwndFocus)
1068 {
1069 /* always make combo box hide its listbox control */
1070 if (!SendMessageW( hwndFocus, CB_SHOWDROPDOWN, FALSE, 0 ))
1071 SendMessageW( GetParent(hwndFocus), CB_SHOWDROPDOWN, FALSE, 0 );
1072 }
1073 }
1074 return DefWindowProcA( hwnd, msg, wParam, lParam );
1075
1076 case WM_GETFONT:
1077 return dlgInfo ? (LRESULT)dlgInfo->hUserFont : 0;
1078
1079 case WM_CLOSE:
1080 PostMessageA( hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED),
1081 (LPARAM)GetDlgItem( hwnd, IDCANCEL ) );
1082 return 0;
1083 }
1084 return 0;
1085 }
1086
1087 /***********************************************************************
1088 * DEFDLG_Epilog
1089 */
1090 static LRESULT DEFDLG_Epilog(HWND hwnd, UINT msg, BOOL fResult)
1091 {
1092
1093 // TODO: where's wine's WM_CTLCOLOR from?
1094 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
1095 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
1096 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
1097 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
1098 return fResult;
1099
1100 return GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
1101 }
1102
1103 /***********************************************************************
1104 * DIALOG_GetNextTabItem
1105 *
1106 * Helper for GetNextDlgTabItem
1107 */
1108 static HWND DIALOG_GetNextTabItem( HWND hwndMain, HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
1109 {
1110 LONG dsStyle;
1111 LONG exStyle;
1112 UINT wndSearch = fPrevious ? GW_HWNDPREV : GW_HWNDNEXT;
1113 HWND retWnd = 0;
1114 HWND hChildFirst = 0;
1115
1116 if(!hwndCtrl)
1117 {
1118 hChildFirst = GetWindow(hwndDlg,GW_CHILD);
1119 if(fPrevious) hChildFirst = GetWindow(hChildFirst,GW_HWNDLAST);
1120 }
1121 else if (IsChild( hwndMain, hwndCtrl ))
1122 {
1123 hChildFirst = GetWindow(hwndCtrl,wndSearch);
1124 if(!hChildFirst)
1125 {
1126 if(GetParent(hwndCtrl) != hwndMain)
1127 /* i.e. if we are not at the top level of the recursion */
1128 hChildFirst = GetWindow(GetParent(hwndCtrl),wndSearch);
1129 else
1130 hChildFirst = GetWindow(hwndCtrl, fPrevious ? GW_HWNDLAST : GW_HWNDFIRST);
1131 }
1132 }
1133
1134 while(hChildFirst)
1135 {
1136 dsStyle = GetWindowLongA(hChildFirst,GWL_STYLE);
1137 exStyle = GetWindowLongA(hChildFirst,GWL_EXSTYLE);
1138 if( (exStyle & WS_EX_CONTROLPARENT) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1139 {
1140 HWND retWnd;
1141 retWnd = DIALOG_GetNextTabItem(hwndMain,hChildFirst,NULL,fPrevious );
1142 if (retWnd) return (retWnd);
1143 }
1144 else if( (dsStyle & WS_TABSTOP) && (dsStyle & WS_VISIBLE) && !(dsStyle & WS_DISABLED))
1145 {
1146 return (hChildFirst);
1147 }
1148 hChildFirst = GetWindow(hChildFirst,wndSearch);
1149 }
1150 if(hwndCtrl)
1151 {
1152 HWND hParent = GetParent(hwndCtrl);
1153 while(hParent)
1154 {
1155 if(hParent == hwndMain) break;
1156 retWnd = DIALOG_GetNextTabItem(hwndMain,GetParent(hParent),hParent,fPrevious );
1157 if(retWnd) break;
1158 hParent = GetParent(hParent);
1159 }
1160 if(!retWnd)
1161 retWnd = DIALOG_GetNextTabItem(hwndMain,hwndMain,NULL,fPrevious );
1162 }
1163 return retWnd ? retWnd : hwndCtrl;
1164
1165 }
1166
1167 /**********************************************************************
1168 * DIALOG_DlgDirListW
1169 *
1170 * Helper function for DlgDirList*W
1171 */
1172 static INT DIALOG_DlgDirListW( HWND hDlg, LPWSTR spec, INT idLBox,
1173 INT idStatic, UINT attrib, BOOL combo )
1174 {
1175 HWND hwnd;
1176 LPWSTR orig_spec = spec;
1177 WCHAR any[] = {'*','.','*',0};
1178
1179 #define SENDMSG(msg,wparam,lparam) \
1180 ((attrib & DDL_POSTMSGS) ? PostMessageW( hwnd, msg, wparam, lparam ) \
1181 : SendMessageW( hwnd, msg, wparam, lparam ))
1182
1183 // DPRINT("%p '%s' %d %d %04x\n",
1184 // hDlg, spec ? spec : "NULL", idLBox, idStatic, attrib );
1185
1186 /* If the path exists and is a directory, chdir to it */
1187 if (!spec || !spec[0] || SetCurrentDirectoryW( spec )) spec = any;
1188 else
1189 {
1190 WCHAR *p, *p2;
1191 p = spec;
1192 if ((p2 = strrchrW( p, '\\' ))) p = p2;
1193 if ((p2 = strrchrW( p, '/' ))) p = p2;
1194 if (p != spec)
1195 {
1196 WCHAR sep = *p;
1197 *p = 0;
1198 if (!SetCurrentDirectoryW( spec ))
1199 {
1200 *p = sep; /* Restore the original spec */
1201 return FALSE;
1202 }
1203 spec = p + 1;
1204 }
1205 }
1206
1207 DPRINT( "mask=%s\n", spec );
1208
1209 if (idLBox && ((hwnd = GetDlgItem( hDlg, idLBox )) != 0))
1210 {
1211 SENDMSG( combo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0 );
1212 if (attrib & DDL_DIRECTORY)
1213 {
1214 if (!(attrib & DDL_EXCLUSIVE))
1215 {
1216 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1217 attrib & ~(DDL_DIRECTORY | DDL_DRIVES),
1218 (LPARAM)spec ) == LB_ERR)
1219 return FALSE;
1220 }
1221 if (SENDMSG( combo ? CB_DIR : LB_DIR,
1222 (attrib & (DDL_DIRECTORY | DDL_DRIVES)) | DDL_EXCLUSIVE,
1223 (LPARAM)any ) == LB_ERR)
1224 return FALSE;
1225 }
1226 else
1227 {
1228 if (SENDMSG( combo ? CB_DIR : LB_DIR, attrib,
1229 (LPARAM)spec ) == LB_ERR)
1230 return FALSE;
1231 }
1232 }
1233
1234 if (idStatic && ((hwnd = GetDlgItem( hDlg, idStatic )) != 0))
1235 {
1236 WCHAR temp[MAX_PATH];
1237 GetCurrentDirectoryW( sizeof(temp)/sizeof(WCHAR), temp );
1238 CharLowerW( temp );
1239 /* Can't use PostMessage() here, because the string is on the stack */
1240 SetDlgItemTextW( hDlg, idStatic, temp );
1241 }
1242
1243 if (orig_spec && (spec != orig_spec))
1244 {
1245 /* Update the original file spec */
1246 WCHAR *p = spec;
1247 while ((*orig_spec++ = *p++));
1248 }
1249
1250 return TRUE;
1251 #undef SENDMSG
1252 }
1253
1254 /**********************************************************************
1255 * DIALOG_DlgDirListA
1256 *
1257 * Helper function for DlgDirList*A
1258 */
1259 static INT DIALOG_DlgDirListA( HWND hDlg, LPSTR spec, INT idLBox,
1260 INT idStatic, UINT attrib, BOOL combo )
1261 {
1262 if (spec)
1263 {
1264 INT ret, len = MultiByteToWideChar( CP_ACP, 0, spec, -1, NULL, 0 );
1265 LPWSTR specW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1266 MultiByteToWideChar( CP_ACP, 0, spec, -1, specW, len );
1267 ret = DIALOG_DlgDirListW( hDlg, specW, idLBox, idStatic, attrib, combo );
1268 WideCharToMultiByte( CP_ACP, 0, specW, -1, spec, 0x7fffffff, NULL, NULL );
1269 HeapFree( GetProcessHeap(), 0, specW );
1270 return ret;
1271 }
1272 return DIALOG_DlgDirListW( hDlg, NULL, idLBox, idStatic, attrib, combo );
1273 }
1274
1275 /**********************************************************************
1276 * DIALOG_DlgDirSelect
1277 *
1278 * Helper function for DlgDirSelect*
1279 */
1280 static BOOL DIALOG_DlgDirSelect( HWND hwnd, LPWSTR str, INT len,
1281 INT id, BOOL unicode, BOOL combo )
1282 {
1283 WCHAR *buffer, *ptr;
1284 INT item, size;
1285 BOOL ret;
1286 HWND listbox = GetDlgItem( hwnd, id );
1287
1288 DPRINT("%p '%s' %d\n", hwnd, str, id );
1289 if (!listbox) return FALSE;
1290
1291 item = SendMessageW(listbox, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0 );
1292 if (item == LB_ERR) return FALSE;
1293 size = SendMessageW(listbox, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN, 0, 0 );
1294 if (size == LB_ERR) return FALSE;
1295
1296 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size+1 ))) return FALSE;
1297
1298 SendMessageW( listbox, combo ? CB_GETLBTEXT : LB_GETTEXT, item, (LPARAM)buffer );
1299
1300 if ((ret = (buffer[0] == '['))) /* drive or directory */
1301 {
1302 if (buffer[1] == '-') /* drive */
1303 {
1304 buffer[3] = ':';
1305 buffer[4] = 0;
1306 ptr = buffer + 2;
1307 }
1308 else
1309 {
1310 buffer[strlenW(buffer)-1] = '\\';
1311 ptr = buffer + 1;
1312 }
1313 }
1314 else ptr = buffer;
1315
1316 if (unicode)
1317 {
1318 if (len > 0 && !WideCharToMultiByte( CP_ACP, 0, ptr, -1, (LPSTR)str, len, 0, 0 ))
1319 ((LPSTR)str)[len-1] = 0;
1320 }
1321 else lstrcpynW( str, ptr, len );
1322 HeapFree( GetProcessHeap(), 0, buffer );
1323 DPRINT("Returning %d '%s'\n", ret, str );
1324 return ret;
1325 }
1326
1327 /***********************************************************************
1328 * GetDlgItemEnumProc
1329 *
1330 * Callback for GetDlgItem
1331 */
1332 BOOL CALLBACK GetDlgItemEnumProc (HWND hwnd, LPARAM lParam )
1333 {
1334 GETDLGITEMINFO * info = (GETDLGITEMINFO *)lParam;
1335 if(info->nIDDlgItem == GetWindowLongW( hwnd, GWL_ID ))
1336 {
1337 info->control = hwnd;
1338 return FALSE;
1339 }
1340 return TRUE;
1341 }
1342
1343
1344 /* FUNCTIONS *****************************************************************/
1345
1346 /*
1347 * @implemented
1348 */
1349 HWND
1350 STDCALL
1351 CreateDialogIndirectParamAorW(
1352 HINSTANCE hInstance,
1353 LPCDLGTEMPLATE lpTemplate,
1354 HWND hWndParent,
1355 DLGPROC lpDialogFunc,
1356 LPARAM lParamInit,
1357 DWORD Flags)
1358 {
1359 /* FIXME:
1360 * This function might be obsolete since I don't think it is exported by NT
1361 * Also wine has one more parameter identifying weather it should call
1362 * the function with unicode or not
1363 */
1364 return DIALOG_CreateIndirect( hInstance, lpTemplate, hWndParent, lpDialogFunc, lParamInit , !Flags, FALSE );
1365 }
1366
1367
1368 /*
1369 * @implemented
1370 */
1371 HWND
1372 STDCALL
1373 CreateDialogIndirectParamA(
1374 HINSTANCE hInstance,
1375 LPCDLGTEMPLATE lpTemplate,
1376 HWND hWndParent,
1377 DLGPROC lpDialogFunc,
1378 LPARAM lParamInit)
1379 {
1380 return CreateDialogIndirectParamAorW( hInstance, lpTemplate, hWndParent, lpDialogFunc, lParamInit, 2 );
1381 }
1382
1383
1384 /*
1385 * @implemented
1386 */
1387 HWND
1388 STDCALL
1389 CreateDialogIndirectParamW(
1390 HINSTANCE hInstance,
1391 LPCDLGTEMPLATE lpTemplate,
1392 HWND hWndParent,
1393 DLGPROC lpDialogFunc,
1394 LPARAM lParamInit)
1395 {
1396 return CreateDialogIndirectParamAorW( hInstance, lpTemplate, hWndParent, lpDialogFunc, lParamInit, 0);
1397 }
1398
1399
1400 /*
1401 * @implemented
1402 */
1403 HWND
1404 STDCALL
1405 CreateDialogParamA(
1406 HINSTANCE hInstance,
1407 LPCSTR lpTemplateName,
1408 HWND hWndParent,
1409 DLGPROC lpDialogFunc,
1410 LPARAM dwInitParam)
1411 {
1412 HRSRC hrsrc;
1413 LPCDLGTEMPLATE ptr;
1414
1415 if (!(hrsrc = FindResourceA( hInstance, lpTemplateName, (LPCSTR)RT_DIALOG ))) return 0;
1416 if (!(ptr = (LPCDLGTEMPLATE)LoadResource(hInstance, hrsrc))) return 0;
1417 return CreateDialogIndirectParamA( hInstance, ptr, hWndParent, lpDialogFunc, dwInitParam );
1418 }
1419
1420
1421 /*
1422 * @implemented
1423 */
1424 HWND
1425 STDCALL
1426 CreateDialogParamW(
1427 HINSTANCE hInstance,
1428 LPCWSTR lpTemplateName,
1429 HWND hWndParent,
1430 DLGPROC lpDialogFunc,
1431 LPARAM dwInitParam)
1432 {
1433 HRSRC hrsrc;
1434 LPCDLGTEMPLATE ptr;
1435
1436 if (!(hrsrc = FindResourceW( hInstance, lpTemplateName, (LPCWSTR)RT_DIALOG ))) return 0;
1437 if (!(ptr = (LPCDLGTEMPLATE)LoadResource(hInstance, hrsrc))) return 0;
1438 return CreateDialogIndirectParamW( hInstance, ptr, hWndParent, lpDialogFunc, dwInitParam );
1439 }
1440
1441
1442 /*
1443 * @implemented
1444 */
1445 LRESULT
1446 STDCALL
1447 DefDlgProcA(
1448 HWND hDlg,
1449 UINT Msg,
1450 WPARAM wParam,
1451 LPARAM lParam)
1452 {
1453 WNDPROC dlgproc;
1454 BOOL result = FALSE;
1455 DIALOGINFO * dlgInfo;
1456
1457 /* if there's no dialog info property then call default windows proc?? */
1458 if (!(dlgInfo = GETDLGINFO(hDlg)))
1459 return DefWindowProcA( hDlg, Msg, wParam, lParam );
1460
1461 SetWindowLongPtrW( hDlg, DWLP_MSGRESULT, 0 );
1462
1463 if ((dlgproc = (WNDPROC)GetWindowLongPtrW( hDlg, DWLP_DLGPROC )))
1464 {
1465 /* Call dialog procedure */
1466 result = CallWindowProcA( dlgproc, hDlg, Msg, wParam, lParam );
1467 }
1468
1469 if (!result && IsWindow(hDlg))
1470 {
1471 /* callback didn't process this message */
1472
1473 switch(Msg)
1474 {
1475 case WM_ERASEBKGND:
1476 case WM_SHOWWINDOW:
1477 case WM_ACTIVATE:
1478 case WM_SETFOCUS:
1479 case DM_SETDEFID:
1480 case DM_GETDEFID:
1481 case WM_NEXTDLGCTL:
1482 case WM_GETFONT:
1483 case WM_CLOSE:
1484 case WM_NCDESTROY:
1485 case WM_ENTERMENULOOP:
1486 case WM_LBUTTONDOWN:
1487 case WM_NCLBUTTONDOWN:
1488 return DEFDLG_Proc( hDlg, Msg, wParam, lParam, dlgInfo );
1489 case WM_INITDIALOG:
1490 case WM_VKEYTOITEM:
1491 case WM_COMPAREITEM:
1492 case WM_CHARTOITEM:
1493 break;
1494
1495 default:
1496 return DefWindowProcA( hDlg, Msg, wParam, lParam );
1497 }
1498 }
1499 return DEFDLG_Epilog(hDlg, Msg, result);
1500 }
1501
1502
1503 /*
1504 * @implemented
1505 */
1506 LRESULT
1507 STDCALL
1508 DefDlgProcW(
1509 HWND hDlg,
1510 UINT Msg,
1511 WPARAM wParam,
1512 LPARAM lParam)
1513 {
1514 WNDPROC dlgproc;
1515 BOOL result = FALSE;
1516 DIALOGINFO * dlgInfo;
1517
1518 /* if there's no dialog info property then call default windows proc?? */
1519 if (!(dlgInfo = GETDLGINFO(hDlg)))
1520 return DefWindowProcW( hDlg, Msg, wParam, lParam );
1521
1522 SetWindowLongPtrW( hDlg, DWLP_MSGRESULT, 0 );
1523
1524 if ((dlgproc = (WNDPROC)GetWindowLongPtrW( hDlg, DWLP_DLGPROC )))
1525 {
1526 /* Call dialog procedure */
1527 result = CallWindowProcW( dlgproc, hDlg, Msg, wParam, lParam );
1528 }
1529
1530 if (!result && IsWindow(hDlg))
1531 {
1532 /* callback didn't process this message */
1533
1534 switch(Msg)
1535 {
1536 case WM_ERASEBKGND:
1537 case WM_SHOWWINDOW:
1538 case WM_ACTIVATE:
1539 case WM_SETFOCUS:
1540 case DM_SETDEFID:
1541 case DM_GETDEFID:
1542 case WM_NEXTDLGCTL:
1543 case WM_GETFONT:
1544 case WM_CLOSE:
1545 case WM_NCDESTROY:
1546 case WM_ENTERMENULOOP:
1547 case WM_LBUTTONDOWN:
1548 case WM_NCLBUTTONDOWN:
1549 return DEFDLG_Proc( hDlg, Msg, wParam, lParam, dlgInfo );
1550 case WM_INITDIALOG:
1551 case WM_VKEYTOITEM:
1552 case WM_COMPAREITEM:
1553 case WM_CHARTOITEM:
1554 break;
1555
1556 default:
1557 return DefWindowProcW( hDlg, Msg, wParam, lParam );
1558 }
1559 }
1560 return DEFDLG_Epilog(hDlg, Msg, result);
1561 }
1562
1563
1564 /*
1565 * @implemented
1566 */
1567 INT_PTR
1568 STDCALL
1569 DialogBoxIndirectParamAorW(
1570 HINSTANCE hInstance,
1571 LPCDLGTEMPLATE hDialogTemplate,
1572 HWND hWndParent,
1573 DLGPROC lpDialogFunc,
1574 LPARAM dwInitParam,
1575 DWORD Flags)
1576 {
1577 /* FIXME:
1578 * This function might be obsolete since I don't think it is exported by NT
1579 * Also wine has one more parameter identifying weather it should call
1580 * the function with unicode or not
1581 */
1582 HWND hWnd = DIALOG_CreateIndirect( hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam, !Flags, TRUE );
1583 if (hWnd) return DIALOG_DoDialogBox( hWnd, hWndParent );
1584 return -1;
1585 }
1586
1587
1588 /*
1589 * @implemented
1590 */
1591 INT_PTR
1592 STDCALL
1593 DialogBoxIndirectParamA(
1594 HINSTANCE hInstance,
1595 LPCDLGTEMPLATE hDialogTemplate,
1596 HWND hWndParent,
1597 DLGPROC lpDialogFunc,
1598 LPARAM dwInitParam)
1599 {
1600 return DialogBoxIndirectParamAorW( hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam, 2);
1601 }
1602
1603
1604 /*
1605 * @implemented
1606 */
1607 INT_PTR
1608 STDCALL
1609 DialogBoxIndirectParamW(
1610 HINSTANCE hInstance,
1611 LPCDLGTEMPLATE hDialogTemplate,
1612 HWND hWndParent,
1613 DLGPROC lpDialogFunc,
1614 LPARAM dwInitParam)
1615 {
1616 return DialogBoxIndirectParamAorW( hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam, 0);
1617 }
1618
1619
1620 /*
1621 * @implemented
1622 */
1623 INT_PTR
1624 STDCALL
1625 DialogBoxParamA(
1626 HINSTANCE hInstance,
1627 LPCSTR lpTemplateName,
1628 HWND hWndParent,
1629 DLGPROC lpDialogFunc,
1630 LPARAM dwInitParam)
1631 {
1632 HWND hwnd;
1633 HRSRC hrsrc;
1634 LPCDLGTEMPLATE ptr;
1635
1636 if (!(hrsrc = FindResourceA( hInstance, lpTemplateName, (LPCSTR)RT_DIALOG ))) return 0;
1637 if (!(ptr = (LPCDLGTEMPLATE)LoadResource(hInstance, hrsrc))) return 0;
1638 hwnd = DIALOG_CreateIndirect(hInstance, ptr, hWndParent, lpDialogFunc, dwInitParam, FALSE, TRUE);
1639 if (hwnd) return DIALOG_DoDialogBox(hwnd, hWndParent);
1640 return -1;
1641 }
1642
1643
1644 /*
1645 * @implemented
1646 */
1647 INT_PTR
1648 STDCALL
1649 DialogBoxParamW(
1650 HINSTANCE hInstance,
1651 LPCWSTR lpTemplateName,
1652 HWND hWndParent,
1653 DLGPROC lpDialogFunc,
1654 LPARAM dwInitParam)
1655 {
1656 HWND hwnd;
1657 HRSRC hrsrc;
1658 LPCDLGTEMPLATE ptr;
1659
1660 if (!(hrsrc = FindResourceW( hInstance, lpTemplateName, (LPCWSTR)RT_DIALOG ))) return 0;
1661 if (!(ptr = (LPCDLGTEMPLATE)LoadResource(hInstance, hrsrc))) return 0;
1662 hwnd = DIALOG_CreateIndirect(hInstance, ptr, hWndParent, lpDialogFunc, dwInitParam, TRUE, TRUE);
1663 if (hwnd) return DIALOG_DoDialogBox(hwnd, hWndParent);
1664 return -1;
1665 }
1666
1667
1668 /*
1669 * @implemented
1670 */
1671 int
1672 STDCALL
1673 DlgDirListA(
1674 HWND hDlg,
1675 LPSTR lpPathSpec,
1676 int nIDListBox,
1677 int nIDStaticPath,
1678 UINT uFileType)
1679 {
1680 return DIALOG_DlgDirListA( hDlg, lpPathSpec, nIDListBox, nIDStaticPath, uFileType, FALSE );
1681 }
1682
1683
1684 /*
1685 * @implemented
1686 */
1687 int
1688 STDCALL
1689 DlgDirListComboBoxA(
1690 HWND hDlg,
1691 LPSTR lpPathSpec,
1692 int nIDComboBox,
1693 int nIDStaticPath,
1694 UINT uFiletype)
1695 {
1696 return DIALOG_DlgDirListA( hDlg, lpPathSpec, nIDComboBox, nIDStaticPath, uFiletype, TRUE );
1697 }
1698
1699
1700 /*
1701 * @implemented
1702 */
1703 int
1704 STDCALL
1705 DlgDirListComboBoxW(
1706 HWND hDlg,
1707 LPWSTR lpPathSpec,
1708 int nIDComboBox,
1709 int nIDStaticPath,
1710 UINT uFiletype)
1711 {
1712 return DIALOG_DlgDirListW( hDlg, lpPathSpec, nIDComboBox, nIDStaticPath, uFiletype, TRUE );
1713 }
1714
1715
1716 /*
1717 * @implemented
1718 */
1719 int
1720 STDCALL
1721 DlgDirListW(
1722 HWND hDlg,
1723 LPWSTR lpPathSpec,
1724 int nIDListBox,
1725 int nIDStaticPath,
1726 UINT uFileType)
1727 {
1728 return DIALOG_DlgDirListW( hDlg, lpPathSpec, nIDListBox, nIDStaticPath, uFileType, FALSE );
1729 }
1730
1731
1732 /*
1733 * @implemented
1734 */
1735 BOOL
1736 STDCALL
1737 DlgDirSelectComboBoxExA(
1738 HWND hDlg,
1739 LPSTR lpString,
1740 int nCount,
1741 int nIDComboBox)
1742 {
1743 return DIALOG_DlgDirSelect( hDlg, (LPWSTR)lpString, nCount, nIDComboBox, FALSE, TRUE );
1744 }
1745
1746
1747 /*
1748 * @implemented
1749 */
1750 BOOL
1751 STDCALL
1752 DlgDirSelectComboBoxExW(
1753 HWND hDlg,
1754 LPWSTR lpString,
1755 int nCount,
1756 int nIDComboBox)
1757 {
1758 return DIALOG_DlgDirSelect( hDlg, (LPWSTR)lpString, nCount, nIDComboBox, TRUE, TRUE );
1759 }
1760
1761
1762 /*
1763 * @implemented
1764 */
1765 BOOL
1766 STDCALL
1767 DlgDirSelectExA(
1768 HWND hDlg,
1769 LPSTR lpString,
1770 int nCount,
1771 int nIDListBox)
1772 {
1773 return DIALOG_DlgDirSelect( hDlg, (LPWSTR)lpString, nCount, nIDListBox, FALSE, FALSE );
1774 }
1775
1776
1777 /*
1778 * @implemented
1779 */
1780 BOOL
1781 STDCALL
1782 DlgDirSelectExW(
1783 HWND hDlg,
1784 LPWSTR lpString,
1785 int nCount,
1786 int nIDListBox)
1787 {
1788 return DIALOG_DlgDirSelect( hDlg, lpString, nCount, nIDListBox, TRUE, FALSE );
1789 }
1790
1791
1792 /*
1793 * @implemented
1794 */
1795 BOOL
1796 STDCALL
1797 EndDialog(
1798 HWND hDlg,
1799 INT_PTR nResult)
1800 {
1801 BOOL wasEnabled = TRUE;
1802 DIALOGINFO * dlgInfo;
1803 HWND owner;
1804
1805 if (!(dlgInfo = GETDLGINFO(hDlg))) return FALSE;
1806
1807 dlgInfo->idResult = nResult;
1808 dlgInfo->flags |= DF_END;
1809 wasEnabled = (dlgInfo->flags & DF_OWNERENABLED);
1810
1811 if (wasEnabled && (owner = GetWindow( hDlg, GW_OWNER )))
1812 DIALOG_EnableOwner( owner );
1813
1814 /* Windows sets the focus to the dialog itself in EndDialog */
1815
1816 if (IsChild(hDlg, GetFocus()))
1817 SetFocus( hDlg );
1818
1819 /* Don't have to send a ShowWindow(SW_HIDE), just do
1820 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1821
1822 SetWindowPos(hDlg, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1823 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1824
1825 if (hDlg == GetActiveWindow()) WinPosActivateOtherWindow( hDlg );
1826
1827 /* unblock dialog loop */
1828 PostMessageA(hDlg, WM_NULL, 0, 0);
1829 return TRUE;
1830 }
1831
1832
1833 /*
1834 * @implemented
1835 */
1836 LONG
1837 STDCALL
1838 GetDialogBaseUnits(VOID)
1839 {
1840 static DWORD units;
1841
1842 if (!units)
1843 {
1844 HDC hdc;
1845 SIZE size;
1846
1847 if ((hdc = GetDC(0)))
1848 {
1849 size.cx = GdiGetCharDimensions( hdc, NULL, &size.cy );
1850 if (size.cx) units = MAKELONG( size.cx, size.cy );
1851 ReleaseDC( 0, hdc );
1852 }
1853 }
1854 return units;
1855 }
1856
1857
1858 /*
1859 * @implemented
1860 */
1861 int
1862 STDCALL
1863 GetDlgCtrlID(
1864 HWND hwndCtl)
1865 {
1866 return GetWindowLongPtrW( hwndCtl, GWLP_ID );
1867 }
1868
1869
1870 /*
1871 * @implemented
1872 */
1873 HWND
1874 STDCALL
1875 GetDlgItem(
1876 HWND hDlg,
1877 int nIDDlgItem)
1878 {
1879 GETDLGITEMINFO info;
1880 info.nIDDlgItem = nIDDlgItem;
1881 info.control = 0;
1882 if(hDlg && !EnumChildWindows(hDlg, (WNDENUMPROC)&GetDlgItemEnumProc, (LPARAM)&info))
1883 return info.control;
1884 else
1885 return 0;
1886 }
1887
1888
1889 /*
1890 * @implemented
1891 */
1892 UINT
1893 STDCALL
1894 GetDlgItemInt(
1895 HWND hDlg,
1896 int nIDDlgItem,
1897 BOOL *lpTranslated,
1898 BOOL bSigned)
1899 {
1900 char str[30];
1901 char * endptr;
1902 long result = 0;
1903
1904 if (lpTranslated) *lpTranslated = FALSE;
1905 if (!SendDlgItemMessageA(hDlg, nIDDlgItem, WM_GETTEXT, sizeof(str), (LPARAM)str))
1906 return 0;
1907 if (bSigned)
1908 {
1909 result = strtol( str, &endptr, 10 );
1910 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1911 return 0;
1912 /* FIXME: errno? */
1913 if (((result == 0) || (result == 0xFFFFFFFF))/* && (errno == ERANGE) */)
1914 return 0;
1915 }
1916 else
1917 {
1918 result = strtoul( str, &endptr, 10 );
1919 if (!endptr || (endptr == str)) /* Conversion was unsuccessful */
1920 return 0;
1921 /* FIXME: errno? */
1922 if ((result == 0xFFFFFFFF)/* && (errno == ERANGE) */) return 0;
1923 }
1924 if (lpTranslated) *lpTranslated = TRUE;
1925 return (UINT)result;
1926 }
1927
1928
1929 /*
1930 * @implemented
1931 */
1932 UINT
1933 STDCALL
1934 GetDlgItemTextA(
1935 HWND hDlg,
1936 int nIDDlgItem,
1937 LPSTR lpString,
1938 int nMaxCount)
1939 {
1940 if (lpString && (lpString > 0)) lpString[0] = '\0';
1941 return (UINT)SendDlgItemMessageA( hDlg, nIDDlgItem, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
1942 }
1943
1944
1945 /*
1946 * @implemented
1947 */
1948 UINT
1949 STDCALL
1950 GetDlgItemTextW(
1951 HWND hDlg,
1952 int nIDDlgItem,
1953 LPWSTR lpString,
1954 int nMaxCount)
1955 {
1956 if (lpString && (lpString > 0)) lpString[0] = '\0';
1957 return (UINT)SendDlgItemMessageW( hDlg, nIDDlgItem, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
1958 }
1959
1960 /*
1961 * @implemented
1962 */
1963 HWND
1964 STDCALL
1965 GetNextDlgGroupItem(
1966 HWND hDlg,
1967 HWND hCtl,
1968 BOOL bPrevious)
1969 {
1970 HWND hwnd, hwndNext, retvalue, hwndLastGroup = 0;
1971 BOOL fLooped=FALSE;
1972 BOOL fSkipping=FALSE;
1973
1974 if (hDlg == hCtl) hCtl = NULL;
1975 if (!hCtl && bPrevious) return 0;
1976
1977 /* if the hwndCtrl is the child of the control in the hwndDlg,
1978 * then the hwndDlg has to be the parent of the hwndCtrl */
1979
1980 if (hCtl)
1981 {
1982 if (!IsChild (hDlg, hCtl)) return 0;
1983 /* Make sure hwndCtrl is a top-level child */
1984
1985 }
1986 else
1987 {
1988 /* No ctrl specified -> start from the beginning */
1989 if (!(hCtl = GetWindow( hDlg, GW_CHILD ))) return 0;
1990 /* MSDN is wrong. fPrevious does not result in the last child */
1991
1992 /* No ctrl specified -> start from the beginning */
1993 if (!(hCtl = GetWindow( hDlg, GW_CHILD ))) return 0;
1994
1995 /* MSDN is wrong. fPrevious does not result in the last child */
1996
1997 /* Maybe that first one is valid. If so then we don't want to skip it*/
1998 if ((GetWindowLongW( hCtl, GWL_STYLE ) & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE)
1999 {
2000 return hCtl;
2001 }
2002
2003 }
2004
2005 /* Always go forward around the group and list of controls; for the
2006 * previous control keep track; for the next break when you find one
2007 */
2008 retvalue = hCtl;
2009 hwnd = hCtl;
2010 while (hwndNext = GetWindow (hwnd, GW_HWNDNEXT),
2011 1)
2012 {
2013 while (!hwndNext)
2014 {
2015 /* Climb out until there is a next sibling of the ancestor or we
2016 * reach the top (in which case we loop back to the start)
2017 */
2018 if (hDlg == GetParent (hwnd))
2019 {
2020 /* Wrap around to the beginning of the list, within the same
2021 * group. (Once only)
2022 */
2023 if (fLooped) goto end;
2024 fLooped = TRUE;
2025 hwndNext = GetWindow (hDlg, GW_CHILD);
2026 }
2027 else
2028 {
2029 hwnd = GetParent (hwnd);
2030 hwndNext = GetWindow (hwnd, GW_HWNDNEXT);
2031 }
2032 }
2033 hwnd = hwndNext;
2034
2035 /* Wander down the leading edge of controlparents */
2036 while ( (GetWindowLongW (hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
2037 ((GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE | WS_DISABLED)) == WS_VISIBLE) &&
2038 (hwndNext = GetWindow (hwnd, GW_CHILD)))
2039 hwnd = hwndNext;
2040 /* Question. If the control is a control parent but either has no
2041 * children or is not visible/enabled then if it has a WS_GROUP does
2042 * it count? For that matter does it count anyway?
2043 * I believe it doesn't count.
2044 */
2045
2046 if ((GetWindowLongW (hwnd, GWL_STYLE) & WS_GROUP))
2047 {
2048 hwndLastGroup = hwnd;
2049 if (!fSkipping)
2050 {
2051 /* Look for the beginning of the group */
2052 fSkipping = TRUE;
2053 }
2054 }
2055
2056 if (hwnd == hCtl)
2057 {
2058 if (!fSkipping) break;
2059 if (hwndLastGroup == hwnd) break;
2060 hwnd = hwndLastGroup;
2061 fSkipping = FALSE;
2062 fLooped = FALSE;
2063 }
2064
2065 if (!fSkipping &&
2066 (GetWindowLongW (hwnd, GWL_STYLE) & (WS_VISIBLE|WS_DISABLED)) ==
2067 WS_VISIBLE)
2068 {
2069 retvalue = hwnd;
2070 if (!bPrevious) break;
2071 }
2072 }
2073 end:
2074 return retvalue;
2075 }
2076
2077
2078 /*
2079 * @implemented
2080 */
2081 HWND
2082 STDCALL
2083 GetNextDlgTabItem(
2084 HWND hDlg,
2085 HWND hCtl,
2086 BOOL bPrevious)
2087 {
2088 /* Undocumented but tested under Win2000 and WinME */
2089 if (hDlg == hCtl) hCtl = NULL;
2090
2091 /* Contrary to MSDN documentation, tested under Win2000 and WinME
2092 * NB GetLastError returns whatever was set before the function was
2093 * called.
2094 */
2095 if (!hCtl && bPrevious) return 0;
2096
2097 return DIALOG_GetNextTabItem(hDlg, hDlg, hCtl, bPrevious);
2098 }
2099
2100
2101 #if 0
2102 BOOL
2103 STDCALL
2104 IsDialogMessage(
2105 HWND hDlg,
2106 LPMSG lpMsg)
2107 {
2108 return IsDialogMessageW(hDlg, lpMsg);
2109 }
2110 #endif
2111
2112 /***********************************************************************
2113 * DIALOG_FixOneChildOnChangeFocus
2114 *
2115 * Callback helper for DIALOG_FixChildrenOnChangeFocus
2116 */
2117
2118 static BOOL CALLBACK DIALOG_FixOneChildOnChangeFocus (HWND hwndChild,
2119 LPARAM lParam)
2120 {
2121 /* If a default pushbutton then no longer default */
2122 if (DLGC_DEFPUSHBUTTON & SendMessageW (hwndChild, WM_GETDLGCODE, 0, 0))
2123 SendMessageW (hwndChild, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
2124 return TRUE;
2125 }
2126
2127 /***********************************************************************
2128 * DIALOG_FixChildrenOnChangeFocus
2129 *
2130 * Following the change of focus that occurs for example after handling
2131 * a WM_KEYDOWN VK_TAB in IsDialogMessage, some tidying of the dialog's
2132 * children may be required.
2133 */
2134 static void DIALOG_FixChildrenOnChangeFocus (HWND hwndDlg, HWND hwndNext)
2135 {
2136 INT dlgcode_next = SendMessageW (hwndNext, WM_GETDLGCODE, 0, 0);
2137 /* INT dlgcode_dlg = SendMessageW (hwndDlg, WM_GETDLGCODE, 0, 0); */
2138 /* Windows does ask for this. I don't know why yet */
2139
2140 EnumChildWindows (hwndDlg, DIALOG_FixOneChildOnChangeFocus, 0);
2141
2142 /* If the button that is getting the focus WAS flagged as the default
2143 * pushbutton then ask the dialog what it thinks the default is and
2144 * set that in the default style.
2145 */
2146 if (dlgcode_next & DLGC_DEFPUSHBUTTON)
2147 {
2148 DWORD def_id = SendMessageW (hwndDlg, DM_GETDEFID, 0, 0);
2149 if (HIWORD(def_id) == DC_HASDEFID)
2150 {
2151 HWND hwndDef;
2152 def_id = LOWORD(def_id);
2153 hwndDef = GetDlgItem (hwndDlg, def_id);
2154 if (hwndDef)
2155 {
2156 INT dlgcode_def = SendMessageW (hwndDef, WM_GETDLGCODE, 0, 0);
2157 /* I know that if it is a button then it should already be a
2158 * UNDEFPUSHBUTTON, since we have just told the buttons to
2159 * change style. But maybe they ignored our request
2160 */
2161 if ((dlgcode_def & DLGC_BUTTON) &&
2162 (dlgcode_def & DLGC_UNDEFPUSHBUTTON))
2163 {
2164 SendMessageW (hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
2165 }
2166 }
2167 }
2168 }
2169 else if ((dlgcode_next & DLGC_BUTTON) && (dlgcode_next & DLGC_UNDEFPUSHBUTTON))
2170 {
2171 SendMessageW (hwndNext, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
2172 /* I wonder why it doesn't send a DM_SETDEFID */
2173 }
2174 }
2175
2176 /*
2177 * @implemented
2178 */
2179 BOOL
2180 STDCALL
2181 IsDialogMessageW(
2182 HWND hDlg,
2183 LPMSG lpMsg)
2184 {
2185 INT dlgCode = 0;
2186
2187 // FIXME: hooks
2188 if (CallMsgFilterW( lpMsg, MSGF_DIALOGBOX )) return TRUE;
2189
2190 if ((hDlg != lpMsg->hwnd) && !IsChild( hDlg, lpMsg->hwnd )) return FALSE;
2191
2192 if (hDlg == GetDesktopWindow()) return FALSE;
2193
2194 hDlg = DIALOG_FindMsgDestination(hDlg);
2195
2196 switch(lpMsg->message)
2197 {
2198 case WM_KEYDOWN:
2199 dlgCode = SendMessageW( lpMsg->hwnd, WM_GETDLGCODE, lpMsg->wParam, (LPARAM)lpMsg );
2200 if (dlgCode & DLGC_WANTMESSAGE) break;
2201
2202 switch(lpMsg->wParam)
2203 {
2204 case VK_TAB:
2205 if (!(dlgCode & DLGC_WANTTAB))
2206 {
2207 /* I am not sure under which circumstances the TAB is handled
2208 * each way. All I do know is that it does not always simply
2209 * send WM_NEXTDLGCTL. (Personally I have never yet seen it
2210 * do so but I presume someone has)
2211 */
2212 if (GETDLGINFO(hDlg))
2213 SendMessageW( hDlg, WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0 );
2214 else
2215 {
2216 /* It would appear that GetNextDlgTabItem can handle being
2217 * passed hwndDlg rather than NULL but that is undocumented
2218 * so let's do it properly
2219 */
2220 HWND hwndFocus = GetFocus();
2221 HWND hwndNext = GetNextDlgTabItem (hDlg,
2222 hwndFocus == hDlg ? NULL : hwndFocus,
2223 GetKeyState (VK_SHIFT) & 0x8000);
2224 if (hwndNext)
2225 {
2226 dlgCode = SendMessageW (hwndNext, WM_GETDLGCODE,
2227 lpMsg->wParam, (LPARAM)lpMsg);
2228 if (dlgCode & DLGC_HASSETSEL)
2229 {
2230 INT maxlen = 1 + SendMessageW (hwndNext, WM_GETTEXTLENGTH, 0, 0);
2231 WCHAR *buffer = HeapAlloc (GetProcessHeap(), 0, maxlen * sizeof(WCHAR));
2232 if (buffer)
2233 {
2234 INT length;
2235 SendMessageW (hwndNext, WM_GETTEXT, maxlen, (LPARAM) buffer);
2236 length = wcslen (buffer);
2237 HeapFree (GetProcessHeap(), 0, buffer);
2238 SendMessageW (hwndNext, EM_SETSEL, 0, length);
2239 }
2240 }
2241 SetFocus (hwndNext);
2242 DIALOG_FixChildrenOnChangeFocus (hDlg, hwndNext);
2243 }
2244 else
2245 return FALSE;
2246 }
2247 return TRUE;
2248 }
2249 break;
2250
2251 case VK_RIGHT:
2252 case VK_DOWN:
2253 case VK_LEFT:
2254 case VK_UP:
2255 if (!(dlgCode & DLGC_WANTARROWS))
2256 {
2257 BOOL fPrevious = (lpMsg->wParam == VK_LEFT || lpMsg->wParam == VK_UP);
2258 HWND hwndNext = GetNextDlgGroupItem (hDlg, GetFocus(), fPrevious );
2259 SendMessageW( hDlg, WM_NEXTDLGCTL, (WPARAM)hwndNext, 1 );
2260 return TRUE;
2261 }
2262 break;
2263
2264 case VK_CANCEL:
2265 case VK_ESCAPE:
2266 SendMessageW( hDlg, WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( hDlg, IDCANCEL ) );
2267 return TRUE;
2268
2269 case VK_EXECUTE:
2270 case VK_RETURN:
2271 {
2272 DWORD dw;
2273 if ((GetFocus() == lpMsg->hwnd) &&
2274 (SendMessageW (lpMsg->hwnd, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON))
2275 {
2276 SendMessageW (hDlg, WM_COMMAND, MAKEWPARAM (GetDlgCtrlID(lpMsg->hwnd),BN_CLICKED), (LPARAM)lpMsg->hwnd);
2277 }
2278 else if (DC_HASDEFID == HIWORD(dw = SendMessageW (hDlg, DM_GETDEFID, 0, 0)))
2279 {
2280 SendMessageW( hDlg, WM_COMMAND, MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
2281 (LPARAM)GetDlgItem(hDlg, LOWORD(dw)));
2282 }
2283 else
2284 {
2285 SendMessageW( hDlg, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hDlg, IDOK ) );
2286
2287 }
2288 }
2289 return TRUE;
2290 }
2291 break;
2292
2293 case WM_CHAR:
2294 dlgCode = SendMessageW( lpMsg->hwnd, WM_GETDLGCODE, lpMsg->wParam, (LPARAM)lpMsg );
2295 if (dlgCode & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)) break;
2296 if (lpMsg->wParam == '\t' && (dlgCode & DLGC_WANTTAB)) break;
2297 /* drop through */
2298
2299 case WM_SYSCHAR:
2300 if (DIALOG_IsAccelerator( lpMsg->hwnd, hDlg, lpMsg->wParam ))
2301 {
2302 /* don't translate or dispatch */
2303 return TRUE;
2304 }
2305 break;
2306 }
2307
2308 TranslateMessage( lpMsg );
2309 DispatchMessageW( lpMsg );
2310 return TRUE;
2311 }
2312
2313
2314 /*
2315 * @implemented
2316 */
2317 UINT
2318 STDCALL
2319 IsDlgButtonChecked(
2320 HWND hDlg,
2321 int nIDButton)
2322 {
2323 return (UINT)SendDlgItemMessageW( hDlg, nIDButton, BM_GETCHECK, 0, 0 );
2324 }
2325
2326
2327 /*
2328 * @implemented
2329 */
2330 BOOL
2331 STDCALL
2332 MapDialogRect(
2333 HWND hDlg,
2334 LPRECT lpRect)
2335 {
2336 DIALOGINFO * dlgInfo;
2337 if (!(dlgInfo = GETDLGINFO(hDlg))) return FALSE;
2338 lpRect->left = MulDiv(lpRect->left, dlgInfo->xBaseUnit, 4);
2339 lpRect->right = MulDiv(lpRect->right, dlgInfo->xBaseUnit, 4);
2340 lpRect->top = MulDiv(lpRect->top, dlgInfo->yBaseUnit, 8);
2341 lpRect->bottom = MulDiv(lpRect->bottom, dlgInfo->yBaseUnit, 8);
2342 return TRUE;
2343 }
2344
2345
2346 /*
2347 * @implemented
2348 */
2349 LRESULT
2350 STDCALL
2351 SendDlgItemMessageA(
2352 HWND hDlg,
2353 int nIDDlgItem,
2354 UINT Msg,
2355 WPARAM wParam,
2356 LPARAM lParam)
2357 {
2358 HWND hwndCtrl = GetDlgItem( hDlg, nIDDlgItem );
2359 if (hwndCtrl) return SendMessageA( hwndCtrl, Msg, wParam, lParam );
2360 else return 0;
2361 }
2362
2363
2364 /*
2365 * @implemented
2366 */
2367 LRESULT
2368 STDCALL
2369 SendDlgItemMessageW(
2370 HWND hDlg,
2371 int nIDDlgItem,
2372 UINT Msg,
2373 WPARAM wParam,
2374 LPARAM lParam)
2375 {
2376 HWND hwndCtrl = GetDlgItem( hDlg, nIDDlgItem );
2377 if (hwndCtrl) return SendMessageW( hwndCtrl, Msg, wParam, lParam );
2378 else return 0;
2379 }
2380
2381
2382 /*
2383 * @implemented
2384 */
2385 BOOL
2386 STDCALL
2387 SetDlgItemInt(
2388 HWND hDlg,
2389 int nIDDlgItem,
2390 UINT uValue,
2391 BOOL bSigned)
2392 {
2393 char str[20];
2394
2395 if (bSigned) sprintf( str, "%d", (INT)uValue );
2396 else sprintf( str, "%u", uValue );
2397 SendDlgItemMessageA( hDlg, nIDDlgItem, WM_SETTEXT, 0, (LPARAM)str );
2398 return TRUE;
2399 }
2400
2401
2402 /*
2403 * @implemented
2404 */
2405 BOOL
2406 STDCALL
2407 SetDlgItemTextA(
2408 HWND hDlg,
2409 int nIDDlgItem,
2410 LPCSTR lpString)
2411 {
2412 return SendDlgItemMessageA( hDlg, nIDDlgItem, WM_SETTEXT, 0, (LPARAM)lpString );
2413 }
2414
2415
2416 /*
2417 * @implemented
2418 */
2419 BOOL
2420 STDCALL
2421 SetDlgItemTextW(
2422 HWND hDlg,
2423 int nIDDlgItem,
2424 LPCWSTR lpString)
2425 {
2426 return SendDlgItemMessageW( hDlg, nIDDlgItem, WM_SETTEXT, 0, (LPARAM)lpString );
2427 }
2428
2429
2430 /*
2431 * @implemented
2432 */
2433 BOOL
2434 STDCALL
2435 CheckDlgButton(
2436 HWND hDlg,
2437 int nIDButton,
2438 UINT uCheck)
2439 {
2440 SendDlgItemMessageW( hDlg, nIDButton, BM_SETCHECK, uCheck, 0 );
2441 return TRUE;
2442 }
2443
2444 static BOOL CALLBACK CheckRB(HWND hwnd, LPARAM lParam)
2445 {
2446 LONG lChildID = GetWindowLongPtrW(hwnd, GWLP_ID);
2447 RADIOGROUP *lpRadioGroup = (RADIOGROUP *)lParam;
2448
2449 if((lChildID >= lpRadioGroup->firstID) &&
2450 (lChildID <= lpRadioGroup->lastID))
2451 {
2452 if (lChildID == lpRadioGroup->checkID)
2453 {
2454 SendMessageW(hwnd, BM_SETCHECK, BST_CHECKED, 0);
2455 }
2456 else
2457 {
2458 SendMessageW(hwnd, BM_SETCHECK, BST_UNCHECKED, 0);
2459 }
2460 }
2461
2462 return TRUE;
2463 }
2464
2465 /*
2466 * @implemented
2467 */
2468 BOOL
2469 STDCALL
2470 CheckRadioButton(
2471 HWND hDlg,
2472 int nIDFirstButton,
2473 int nIDLastButton,
2474 int nIDCheckButton)
2475 {
2476 RADIOGROUP radioGroup;
2477
2478 radioGroup.firstID = nIDFirstButton;
2479 radioGroup.lastID = nIDLastButton;
2480 radioGroup.checkID = nIDCheckButton;
2481
2482 return EnumChildWindows(hDlg, CheckRB, (LPARAM)&radioGroup);
2483 }