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