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