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