source: trunk/src/user32/win32dlg.cpp@ 10367

Last change on this file since 10367 was 10033, checked in by sandervl, 22 years ago

EndDialog: Enable owner window regardless of whether it was enabled before dialog creation & check for fake windows in GetWindowFromOS2Handle

File size: 38.5 KB
Line 
1/* $Id: win32dlg.cpp,v 1.81 2003-04-24 13:59:14 sandervl Exp $ */
2/*
3 * Win32 Dialog Code for OS/2
4 *
5 * Copyright 1999-2001 Sander van Leeuwen (sandervl@xs4all.nl) (Wine port & OS/2 adaption)
6 *
7 * Based on Wine code (990815; windows\dialog.c)
8 *
9 * Copyright 1993, 1994, 1996 Alexandre Julliard
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 */
14#include <os2win.h>
15#include <windowsx.h>
16#include <stdlib.h>
17#include <string.h>
18#include <misc.h>
19#include <win32dlg.h>
20#include <win\winproc.h>
21#include "oslibmsg.h"
22#include "oslibwin.h"
23#include "win32wdesktop.h"
24#include "controls.h"
25#include "syscolor.h"
26#include "hook.h"
27#include <math.h>
28#include <unicode.h>
29#include "user32api.h"
30
31#define DBG_LOCALLOG DBG_win32dlg
32#include "dbglocal.h"
33
34#define DEFAULT_DLGFONT "9.WarpSans"
35
36#define GET_SHORT(ptr) (*(SHORT *)(ptr))
37
38
39//******************************************************************************
40//******************************************************************************
41Win32Dialog::Win32Dialog(HINSTANCE hInst, LPCSTR dlgTemplate, HWND owner,
42 DLGPROC dlgProc, LPARAM param, BOOL isUnicode)
43 : Win32BaseWindow()
44{
45 RECT rect;
46 WORD style;
47 ATOM classAtom;
48
49 this->isUnicode = isUnicode;
50 hUserFont = 0;
51 hMenu = 0;
52 hwndFocus = 0;
53 Win32DlgProc = 0;
54 msgResult = 0;
55 userDlgData = 0;
56 idResult = 0;
57 dialogFlags = 0;
58 fDialogInit = FALSE;
59 memset(&dlgInfo, 0, sizeof(dlgInfo));
60
61 dprintf(("********* CREATE DIALOG ************"));
62 if(fInitialized == FALSE) {
63 if(DIALOG_Init() == FALSE) {
64 dprintf(("DIALOG_Init FAILED!"));
65 DebugInt3();
66 SetLastError(ERROR_GEN_FAILURE);
67 return;
68 }
69 fInitialized = TRUE;
70 }
71 xUnit = xBaseUnit;
72 yUnit = yBaseUnit;
73
74 /* Parse dialog template */
75 dlgTemplate = parseTemplate(dlgTemplate, &dlgInfo);
76
77 /* Load menu */
78 if (dlgInfo.menuName)
79 {
80 hMenu = LoadMenuW( hInst, (LPCWSTR)dlgInfo.menuName );
81 }
82
83 dprintf(("Dialog style %x", LOWORD(dlgInfo.style)));
84
85 /* Create custom font if needed */
86 if (dlgInfo.style & DS_SETFONT)
87 {
88 /* The font height must be negative as it is a point size */
89 /* and must be converted to pixels first */
90 /* (see CreateFont() documentation in the Windows SDK). */
91 int pixels;
92 if (((short)dlgInfo.pointSize) < 0)
93 pixels = -((short)dlgInfo.pointSize);
94 else
95 {
96 HDC hdc = GetDC(0);
97 pixels = dlgInfo.pointSize * GetDeviceCaps(hdc, LOGPIXELSY)/72;
98 ReleaseDC(0, hdc);
99 }
100
101 hUserFont = CreateFontW(-pixels, 0, 0, 0,
102 dlgInfo.weight, dlgInfo.italic, FALSE,
103 FALSE, DEFAULT_CHARSET, 0, 0, PROOF_QUALITY,
104 FF_DONTCARE, (LPCWSTR)dlgInfo.faceName );
105 if (hUserFont)
106 {
107 SIZE charSize;
108 getCharSize(hUserFont,&charSize);
109 xUnit = charSize.cx;
110 yUnit = charSize.cy;
111 }
112 }
113
114 //Set help id
115 setWindowContextHelpId(dlgInfo.helpId);
116
117 /* Create dialog main window */
118 rect.left = rect.top = 0;
119 rect.right = dlgInfo.cx * xUnit / 4;
120 rect.bottom = dlgInfo.cy * yUnit / 8;
121 if (dlgInfo.style & DS_MODALFRAME)
122 dlgInfo.exStyle |= WS_EX_DLGMODALFRAME;
123
124 AdjustWindowRectEx( &rect, dlgInfo.style, hMenu ? TRUE : FALSE , dlgInfo.exStyle );
125 rect.right -= rect.left;
126 rect.bottom -= rect.top;
127
128 if ((INT16)dlgInfo.x == CW_USEDEFAULT16)
129 {
130 rect.left = rect.top = CW_USEDEFAULT;
131 }
132 else
133 {
134 if (dlgInfo.style & DS_CENTER)
135 {
136 rect.left = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
137 rect.top = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
138 }
139 else
140 {
141 rect.left += dlgInfo.x * xUnit / 4;
142 rect.top += dlgInfo.y * yUnit / 8;
143 }
144 if ( !(dlgInfo.style & WS_CHILD) )
145 {
146 INT dX, dY;
147
148 if( !(dlgInfo.style & DS_ABSALIGN) && owner)
149 ClientToScreen(owner, (POINT *)&rect );
150
151 /* try to fit it into the desktop */
152
153 if( (dX = rect.left + rect.right + GetSystemMetrics(SM_CXDLGFRAME)
154 - GetSystemMetrics(SM_CXSCREEN)) > 0 ) rect.left -= dX;
155 if( (dY = rect.top + rect.bottom + GetSystemMetrics(SM_CYDLGFRAME)
156 - GetSystemMetrics(SM_CYSCREEN)) > 0 ) rect.top -= dY;
157 if( rect.left < 0 ) rect.left = 0;
158 if( rect.top < 0 ) rect.top = 0;
159 }
160 }
161
162 /* Create the dialog window */
163
164 /* Find the class atom */
165 if (!HIWORD(dlgInfo.className))
166 {
167 classAtom = (ATOM)LOWORD(dlgInfo.className);
168 }
169 else
170 if (!(classAtom = GlobalFindAtomW((LPWSTR)dlgInfo.className)))
171 {
172 SetLastError(ERROR_INVALID_PARAMETER);
173 return;
174 }
175 CREATESTRUCTA cs;
176 cs.lpCreateParams = NULL;
177 cs.hInstance = hInst;
178 cs.hMenu = hMenu;
179 cs.hwndParent = owner;
180 cs.x = rect.left;
181 cs.y = rect.top;
182 cs.cx = rect.right;
183 cs.cy = rect.bottom;
184 cs.style = dlgInfo.style & ~WS_VISIBLE;
185
186 if(!isUnicode) {
187 if(dlgInfo.caption) {
188 cs.lpszName = UnicodeToAsciiString((LPWSTR)dlgInfo.caption);
189 }
190 else cs.lpszName = 0;
191 if(dlgInfo.className) {
192 cs.lpszClass = UnicodeToAsciiString((LPWSTR)dlgInfo.className);
193 }
194 else cs.lpszClass = 0;
195 }
196 else {
197 cs.lpszName = dlgInfo.caption;
198 cs.lpszClass = dlgInfo.className;
199 }
200 cs.dwExStyle = dlgInfo.exStyle;
201 if (dlgInfo.style & DS_CONTEXTHELP) cs.dwExStyle |= WS_EX_CONTEXTHELP;
202
203 //Mask away WS_CAPTION style for dialogs with DS_CONTROL style
204 //(necessary for OFN_ENABLETEMPLATE file open dialogs)
205 //(verified this behaviour in NT4, SP6)
206 if (dlgInfo.style & DS_CONTROL) cs.style &= ~(WS_CAPTION);
207
208 fIsDialog = TRUE;
209 WINPROC_SetProc((HWINDOWPROC *)&Win32DlgProc, (WNDPROC)dlgProc, (isUnicode) ? WIN_PROC_32W : WIN_PROC_32A, WIN_PROC_WINDOW);
210
211 this->tmpParam = param;
212 this->tmpDlgTemplate = (LPSTR)dlgTemplate;
213
214 if (CreateWindowExA(&cs, classAtom) == FALSE)
215 {
216 if (hUserFont) DeleteObject( hUserFont );
217 if (hMenu) DestroyMenu( hMenu );
218 SetLastError(ERROR_OUTOFMEMORY); //TODO: Wrong error
219 return;
220 }
221 SetLastError(0);
222 return;
223}
224//******************************************************************************
225//******************************************************************************
226Win32Dialog::~Win32Dialog()
227{
228 if (hUserFont) DeleteObject( hUserFont );
229 if (hMenu) DestroyMenu( hMenu );
230
231 WINPROC_FreeProc(Win32DlgProc, WIN_PROC_WINDOW);
232}
233//******************************************************************************
234//******************************************************************************
235ULONG Win32Dialog::MsgCreate(HWND hwndOS2)
236{
237 CREATESTRUCTA *cs = tmpcs; //pointer to CREATESTRUCT used in CreateWindowExA method
238 LPARAM param = tmpParam;
239 LPSTR dlgTemplate = tmpDlgTemplate;
240
241 if(Win32BaseWindow::MsgCreate(hwndOS2) == FALSE) {
242 dprintf(("********* DIALOG CREATION FAILED! (main dialog window) ************"));
243 return FALSE;
244 }
245
246 if(!isUnicode) {
247 if(cs->lpszName) FreeAsciiString((LPSTR)cs->lpszName);
248 if(HIWORD(cs->lpszClass)) {
249 FreeAsciiString((LPSTR)cs->lpszClass);
250 }
251 }
252
253 if (hUserFont)
254 SendMessageA(getWindowHandle(), WM_SETFONT, (WPARAM)hUserFont, 0 );
255
256 /* Create controls */
257 if (createControls(dlgTemplate, hInstance))
258 {
259 dprintf(("********* DIALOG CONTROLS CREATED ************"));
260 /* Send initialisation messages and set focus */
261 hwndFocus = GetNextDlgTabItem( getWindowHandle(), 0, FALSE );
262 dprintf(("dlg ctor: GetNextDlgTabItem returned %x, capture hwnd = %x", hwndFocus, GetCapture()));
263
264 fDialogInit = TRUE; //WM_NCCALCSIZE can now be sent to dialog procedure
265
266 HWND hwndPreInitFocus = GetFocus();
267 if(SendMessageA(getWindowHandle(), WM_INITDIALOG, (WPARAM)hwndFocus, param))
268 {
269 //SvL: Experiments in NT4 show that dialogs that are children don't
270 // receive focus. Not sure if this is always true. (couldn't
271 // find any remarks about this in the SDK docs)
272 if(!(getStyle() & WS_CHILD))
273 {
274 /* check where the focus is again,
275 * some controls status might have changed in WM_INITDIALOG */
276 hwndFocus = GetNextDlgTabItem( getWindowHandle(), 0, FALSE );
277 if(hwndFocus) {
278 SetFocus(hwndFocus);
279 }
280 }
281 }
282 else
283 {
284 //SvL: Experiments in NT4 show that dialogs that are children don't
285 // receive focus. Not sure if this is always true. (couldn't
286 // find any remarks about this in the SDK docs)
287 if(!(getStyle() & WS_CHILD))
288 {
289 /* If the dlgproc has returned FALSE (indicating handling of keyboard focus)
290 but the focus has not changed, set the focus where we expect it. */
291 if ( (getStyle() & WS_VISIBLE) && ( GetFocus() == hwndPreInitFocus ) )
292 SetFocus( hwndFocus );
293 }
294 }
295
296 if (dlgInfo.style & WS_VISIBLE && !(getStyle() & WS_VISIBLE))
297 {
298 ShowWindow( SW_SHOWNORMAL ); /* SW_SHOW doesn't always work */
299////Note: this was removed in Wine
300//// UpdateWindow( getWindowHandle() );
301 }
302 SetLastError(ERROR_SUCCESS);
303 dprintf(("********* DIALOG CREATED ************"));
304 return TRUE;
305 }
306 dprintf(("********* DIALOG CREATION FAILED! ************"));
307 return FALSE;
308}
309//******************************************************************************
310//******************************************************************************
311BOOL Win32Dialog::MapDialogRect(LPRECT rect)
312{
313 rect->left = (rect->left * xUnit) / 4;
314 rect->right = (rect->right * xUnit) / 4;
315 rect->top = (rect->top * yUnit) / 8;
316 rect->bottom = (rect->bottom * yUnit) / 8;
317 return TRUE;
318}
319/***********************************************************************
320 * DIALOG_DoDialogBox
321 */
322INT Win32Dialog::doDialogBox()
323{
324 Win32BaseWindow *topOwner;
325 MSG msg;
326 INT retval;
327
328 dprintf(("doDialogBox %x", getWindowHandle()));
329 /* Owner must be a top-level window */
330 if(getOwner() == NULL) {
331 windowDesktop->addRef();
332 topOwner = windowDesktop;
333 }
334 else topOwner = GetWindowFromHandle(getOwner()->GetTopParent());
335
336 if(topOwner == NULL) {
337 dprintf(("Dialog box has no top owner!!!"));
338 return -1;
339 }
340
341 if (!dialogFlags & DF_END) /* was EndDialog called in WM_INITDIALOG ? */
342 {
343 HWND hwndOldDialog;
344 BOOL bOldOwner;
345
346 fIsModalDialog = TRUE;
347 topOwner->EnableWindow(FALSE);
348
349 bOldOwner = topOwner->IsModalDialogOwner();
350 topOwner->setModalDialogOwner(TRUE);
351 hwndOldDialog = topOwner->getOS2HwndModalDialog();
352 topOwner->setOS2HwndModalDialog(OS2HwndFrame);
353 ShowWindow(SW_SHOW);
354
355 //CB: 100% CPU usage, need a better solution with OSLibWinGetMsg
356 // is WM_ENTERIDLE used and leaving away breaks an application?
357 // this style was useful for Win3.1 but today there are threads
358 // solution: send only few WM_ENTERIDLE messages
359 while (TRUE)
360 {
361 if (!PeekMessageA(&msg,0,0,0,PM_NOREMOVE))
362 {
363 if(!(getStyle() & DS_NOIDLEMSG))
364 SendMessageA(topOwner->getWindowHandle(), WM_ENTERIDLE,MSGF_DIALOGBOX,getWindowHandle());
365 GetMessageA(&msg,0,0,0);
366 }
367 else PeekMessageA(&msg,0,0,0,PM_REMOVE);
368
369 /* Call message filters */
370 if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
371 {
372 LPMSG pmsg = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
373 if (pmsg)
374 {
375 BOOL ret;
376 *pmsg = msg;
377 ret = (HOOK_CallHooksA( WH_SYSMSGFILTER, MSGF_DIALOGBOX, 0,
378 (LPARAM) pmsg ) ||
379 HOOK_CallHooksA( WH_MSGFILTER, MSGF_DIALOGBOX, 0,
380 (LPARAM) pmsg ));
381
382 HeapFree( GetProcessHeap(), 0, pmsg );
383 if (ret)
384 {
385 /* Message filtered -> remove it from the queue */
386 /* if it's still there. */
387 continue;
388 }
389 }
390 }
391
392 if(msg.message == WM_QUIT)
393 {
394 dprintf(("Win32Dialog::doDialogBox: received WM_QUIT"));
395 break;
396 }
397 if (!IsDialogMessageA( getWindowHandle(), &msg))
398 {
399 TranslateMessage( &msg );
400 DispatchMessageA( &msg );
401 }
402 if (dialogFlags & DF_END)
403 break;
404 }
405 topOwner->setModalDialogOwner(bOldOwner);
406 topOwner->setOS2HwndModalDialog(hwndOldDialog);
407 if (!bOldOwner) topOwner->EnableWindow(TRUE);
408 }
409 RELEASE_WNDOBJ(topOwner);
410 retval = idResult;
411 DestroyWindow();
412 return retval;
413}
414/***********************************************************************
415 * DIALOG_Init
416 *
417 * Initialisation of the dialog manager.
418 */
419BOOL Win32Dialog::DIALOG_Init(void)
420{
421 HDC hdc;
422 SIZE size;
423
424 /* Calculate the dialog base units */
425 if (!(hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL ))) return FALSE;
426 if (!getCharSizeFromDC( hdc, 0, &size )) return FALSE;
427 DeleteDC( hdc );
428 xBaseUnit = size.cx;
429 yBaseUnit = size.cy;
430
431 return TRUE;
432}
433/***********************************************************************
434 * DIALOG_GetCharSizeFromDC
435 *
436 *
437 * Calculates the *true* average size of English characters in the
438 * specified font as oppposed to the one returned by GetTextMetrics.
439 */
440BOOL Win32Dialog::getCharSizeFromDC( HDC hDC, HFONT hUserFont, SIZE * pSize )
441{
442 BOOL Success = FALSE;
443 HFONT hFontPrev = 0;
444 pSize->cx = xBaseUnit;
445 pSize->cy = yBaseUnit;
446
447 if ( hDC )
448 {
449 /* select the font */
450 TEXTMETRICA tm;
451 memset(&tm,0,sizeof(tm));
452 if (hUserFont) hFontPrev = SelectFont(hDC,hUserFont);
453 if (GetTextMetricsA(hDC,&tm))
454 {
455 pSize->cx = tm.tmAveCharWidth;
456 pSize->cy = tm.tmHeight;
457
458 /* if variable width font */
459 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
460 {
461 SIZE total;
462 static const char szAvgChars[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
463
464 /* Calculate a true average as opposed to the one returned
465 * by tmAveCharWidth. This works better when dealing with
466 * proportional spaced fonts and (more important) that's
467 * how Microsoft's dialog creation code calculates the size
468 * of the font
469 */
470#ifdef __WIN32OS2__
471 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars)-1,&total))
472 {
473 /* round up */
474 pSize->cx = ((2*total.cx/(sizeof(szAvgChars)-1)) + 1)/2;
475 Success = TRUE;
476 }
477#else
478 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
479 {
480 /* round up */
481 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
482 Success = TRUE;
483 }
484#endif
485 }
486 else
487 {
488 Success = TRUE;
489 }
490 }
491
492 /* select the original font */
493 if (hFontPrev) SelectFont(hDC,hFontPrev);
494 }
495 return (Success);
496}
497/***********************************************************************
498 * DIALOG_GetCharSize
499 *
500 *
501 * Calculates the *true* average size of English characters in the
502 * specified font as oppposed to the one returned by GetTextMetrics.
503 * A convenient variant of DIALOG_GetCharSizeFromDC.
504 */
505BOOL Win32Dialog::getCharSize( HFONT hUserFont, SIZE * pSize )
506{
507 HDC hDC = CreateCompatibleDC(0);
508 BOOL Success = getCharSizeFromDC( hDC, hUserFont, pSize );
509 DeleteDC(hDC);
510 return Success;
511}
512/***********************************************************************
513 * DIALOG_ParseTemplate32
514 *
515 * Fill a DLG_TEMPLATE structure from the dialog template, and return
516 * a pointer to the first control.
517 */
518LPCSTR Win32Dialog::parseTemplate( LPCSTR dlgtemplate, DLG_TEMPLATE * result )
519{
520 const WORD *p = (const WORD *)dlgtemplate;
521
522 result->style = GET_DWORD(p); p += 2;
523 if (result->style == 0xffff0001) /* DIALOGEX resource */
524 {
525 result->dialogEx = TRUE;
526 result->helpId = GET_DWORD(p); p += 2;
527 result->exStyle = GET_DWORD(p); p += 2;
528 result->style = GET_DWORD(p); p += 2;
529 }
530 else
531 {
532 result->dialogEx = FALSE;
533 result->helpId = 0;
534 result->exStyle = GET_DWORD(p); p += 2;
535 }
536 result->nbItems = GET_WORD(p); p++;
537 //x & y are signed words
538 result->x = GET_SHORT(p); p++;
539 result->y = GET_SHORT(p); p++;
540 result->cx = GET_WORD(p); p++;
541 result->cy = GET_WORD(p); p++;
542
543 /* Get the menu name */
544
545 switch(GET_WORD(p))
546 {
547 case 0x0000:
548 result->menuName = NULL;
549 p++;
550 break;
551 case 0xffff:
552 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
553 p += 2;
554 break;
555 default:
556 result->menuName = (LPCSTR)p;
557 p += lstrlenW( (LPCWSTR)p ) + 1;
558 break;
559 }
560
561 /* Get the class name */
562 switch(GET_WORD(p))
563 {
564 case 0x0000:
565 result->className = (LPCSTR)DIALOG_CLASS_NAMEW;
566 p++;
567 break;
568 case 0xffff:
569 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
570 p += 2;
571 break;
572 default:
573 result->className = (LPCSTR)p;
574 p += lstrlenW( (LPCWSTR)p ) + 1;
575 break;
576 }
577
578 /* Get the window caption */
579
580 result->caption = (LPCSTR)p;
581 p += lstrlenW( (LPCWSTR)p ) + 1;
582
583 /* Get the font name */
584
585 if (result->style & DS_SETFONT)
586 {
587 result->pointSize = GET_WORD(p);
588 p++;
589 if (result->dialogEx)
590 {
591 result->weight = GET_WORD(p); p++;
592 result->italic = LOBYTE(GET_WORD(p)); p++;
593 }
594 else
595 {
596 result->weight = FW_DONTCARE;
597 result->italic = FALSE;
598 }
599 result->faceName = (LPCSTR)p;
600 p += lstrlenW( (LPCWSTR)p ) + 1;
601 }
602
603 /* First control is on dword boundary */
604 return (LPCSTR)((((int)p) + 3) & ~3);
605}
606/***********************************************************************
607 * DIALOG_GetControl32
608 *
609 * Return the class and text of the control pointed to by ptr,
610 * fill the header structure and return a pointer to the next control.
611 */
612WORD *Win32Dialog::getControl(const WORD *p, DLG_CONTROL_INFO *info, BOOL dialogEx)
613{
614 if (dialogEx)
615 {
616 info->helpId = GET_DWORD(p); p += 2;
617 info->exStyle = GET_DWORD(p); p += 2;
618 info->style = GET_DWORD(p); p += 2;
619 }
620 else
621 {
622 info->helpId = 0;
623 info->style = GET_DWORD(p); p += 2;
624 info->exStyle = GET_DWORD(p); p += 2;
625 }
626 //SvL: x & y are signed words!
627 info->x = GET_SHORT(p); p++;
628 info->y = GET_SHORT(p); p++;
629 info->cx = GET_WORD(p); p++;
630 info->cy = GET_WORD(p); p++;
631
632 if (dialogEx)
633 {
634 /* id is a DWORD for DIALOGEX */
635 info->id = GET_DWORD(p);
636 p += 2;
637 }
638 else
639 {
640 info->id = GET_WORD(p);
641 p++;
642 }
643
644 if (GET_WORD(p) == 0xffff)
645 {
646 static const WCHAR class_names[6][10] =
647 {
648 { 'B','u','t','t','o','n', }, /* 0x80 */
649 { 'E','d','i','t', }, /* 0x81 */
650 { 'S','t','a','t','i','c', }, /* 0x82 */
651 { 'L','i','s','t','B','o','x', }, /* 0x83 */
652 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
653 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
654 };
655 WORD id = GET_WORD(p+1);
656 if ((id >= 0x80) && (id <= 0x85))
657 info->className = (LPCSTR)class_names[id - 0x80];
658 else
659 {
660 info->className = NULL;
661 dprintf(("Unknown built-in class id %04x\n", id ));
662 }
663 p += 2;
664 }
665 else
666 {
667 info->className = (LPCSTR)p;
668 p += lstrlenW( (LPCWSTR)p ) + 1;
669 }
670
671 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
672 {
673 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
674 //Wine add 3 (bytes) here. But that causes problems with certain
675 //InstallShield installers.
676 p += 2;
677 }
678 else
679 {
680 info->windowName = (LPCSTR)p;
681 p += lstrlenW( (LPCWSTR)p ) + 1;
682 }
683
684 if (GET_WORD(p))
685 {
686 info->data = (LPVOID)(p + 1);
687 p += GET_WORD(p) / sizeof(WORD);
688 }
689 else info->data = NULL;
690 p++;
691
692 /* Next control is on dword boundary */
693 return (WORD *)((((int)p) + 3) & ~3);
694}
695
696
697/***********************************************************************
698 * DIALOG_CreateControls
699 *
700 * Create the control windows for a dialog.
701 */
702BOOL Win32Dialog::createControls(LPCSTR dlgtemplate, HINSTANCE hInst)
703{
704 DLG_CONTROL_INFO info;
705 HWND hwndCtrl, hwndDefButton = 0;
706 INT items = dlgInfo.nbItems;
707
708 while (items--)
709 {
710 dlgtemplate = (LPCSTR)getControl( (WORD *)dlgtemplate, &info, dlgInfo.dialogEx );
711
712 /* Is this it? */
713 if (info.style & WS_BORDER)
714 {
715 info.style &= ~WS_BORDER;
716 info.exStyle |= WS_EX_CLIENTEDGE;
717 }
718
719 dprintf(("Create CONTROL %d", info.id));
720
721 hwndCtrl = ::CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
722 (LPWSTR)info.className,
723 (LPWSTR)info.windowName,
724 info.style | WS_CHILD,
725 MulDiv(info.x, xUnit, 4),
726 MulDiv(info.y, yUnit, 8),
727 MulDiv(info.cx, xUnit, 4),
728 MulDiv(info.cy, yUnit, 8),
729 getWindowHandle(), (HMENU)info.id,
730 hInst, info.data );
731
732 if (!hwndCtrl) return FALSE;
733
734 /* Send initialisation messages to the control */
735 if (hUserFont) ::SendMessageA( hwndCtrl, WM_SETFONT, (WPARAM)hUserFont, 0 );
736
737 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
738 {
739 /* If there's already a default push-button, set it back */
740 /* to normal and use this one instead. */
741 if (hwndDefButton)
742 ::SendMessageA( hwndDefButton, BM_SETSTYLE,
743 BS_PUSHBUTTON,FALSE );
744 hwndDefButton = hwndCtrl;
745 idResult = ::GetWindowWord( hwndCtrl, GWW_ID );
746 }
747 dprintf(("Create CONTROL %d DONE", info.id));
748 }
749 return TRUE;
750}
751/***********************************************************************
752 * DEFDLG_Proc
753 *
754 * Implementation of DefDlgProc(). Only handle messages that need special
755 * handling for dialogs.
756 */
757LRESULT Win32Dialog::DefDlg_Proc(UINT msg, WPARAM wParam, LPARAM lParam)
758{
759 switch(msg)
760 {
761 case WM_ERASEBKGND:
762 {
763 RECT rect;
764 int rc;
765
766 if (!windowClass) return 0;
767
768 rc = GetClipBox( (HDC)wParam, &rect );
769 if ((rc == SIMPLEREGION) || (rc == COMPLEXREGION))
770 {
771 HBRUSH hBrush = SendMessageA(getWindowHandle(), WM_CTLCOLORDLG, wParam, getWindowHandle());
772 if(GetObjectType(hBrush) != OBJ_BRUSH) {
773 DefWndControlColor(CTLCOLOR_DLG, (HDC)wParam);
774 }
775 FillRect( (HDC)wParam, &rect, hBrush);
776 }
777
778 return 1;
779 }
780
781 case WM_NCDESTROY:
782 /* Free dialog heap (if created) */
783#if 0
784 if (dlgInfo->hDialogHeap)
785 {
786 GlobalUnlock16(dlgInfo->hDialogHeap);
787 GlobalFree16(dlgInfo->hDialogHeap);
788 dlgInfo->hDialogHeap = 0;
789 }
790#endif
791 /* Delete font */
792 if (hUserFont)
793 {
794 DeleteObject( hUserFont );
795 hUserFont = 0;
796 }
797
798 /* Delete menu */
799 if (hMenu)
800 {
801 DestroyMenu( hMenu );
802 hMenu = 0;
803 }
804
805 /* Delete window procedure */
806 Win32DlgProc = 0;
807 dialogFlags |= DF_END; /* just in case */
808
809 /* Window clean-up */
810 return DefWindowProcA(msg, wParam, lParam );
811
812 case WM_SHOWWINDOW:
813 if (!wParam) saveFocus();
814 return DefWindowProcA(msg, wParam, lParam );
815
816 case WM_ACTIVATE:
817 if (wParam) {
818 restoreFocus();
819 }
820 else saveFocus();
821 return 0;
822
823 case WM_SETFOCUS:
824 restoreFocus();
825 return 0;
826
827 case DM_SETDEFID:
828 if (dialogFlags & DF_END)
829 return 1;
830
831 setDefButton(wParam ? GetDlgItem( getWindowHandle(), wParam ) : 0 );
832 return 1;
833
834 case DM_GETDEFID:
835 {
836 HWND hwndDefId;
837 if (dialogFlags & DF_END) return 0;
838 if (idResult)
839 return MAKELONG( idResult, DC_HASDEFID );
840 if ((hwndDefId = findDefButton()) != 0)
841 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
842
843 return 0;
844 }
845
846 case WM_NEXTDLGCTL:
847 {
848 HWND hwndDest = (HWND)wParam;
849 if (!lParam)
850 hwndDest = GetNextDlgTabItem(getWindowHandle(), GetFocus(), wParam);
851 if (hwndDest) setFocus( hwndDest );
852 setDefButton( hwndDest );
853 return 0;
854 }
855
856 case WM_ENTERMENULOOP:
857 case WM_LBUTTONDOWN:
858 case WM_NCLBUTTONDOWN:
859 {
860 HWND hwndCurFocus = GetFocus();
861 if (hwndCurFocus)
862 {
863 Win32BaseWindow *wndFocus = Win32BaseWindow::GetWindowFromHandle(hwndFocus);
864
865 if(wndFocus)
866 {
867 /* always make combo box hide its listbox control */
868 if( CONTROLS_IsControl( wndFocus, COMBOBOX_CONTROL ) )
869 SendMessageA(wndFocus->getWindowHandle(), CB_SHOWDROPDOWN, FALSE, 0 );
870 else
871 if( CONTROLS_IsControl( wndFocus, EDIT_CONTROL ) &&
872 CONTROLS_IsControl( wndFocus->getParent(), COMBOBOX_CONTROL ))
873 SendMessageA(wndFocus->getWindowHandle(), CB_SHOWDROPDOWN, FALSE, 0 );
874 RELEASE_WNDOBJ(wndFocus);
875 }
876 }
877 return DefWindowProcA( msg, wParam, lParam );
878 }
879
880 case WM_GETFONT:
881 return hUserFont;
882
883 case WM_CLOSE:
884 PostMessageA(getWindowHandle(), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( getWindowHandle(), IDCANCEL ) );
885 return 0;
886
887 case WM_NOTIFYFORMAT:
888 return DefWindowProcA(msg, wParam, lParam );
889 }
890 return 0;
891}
892//******************************************************************************
893//******************************************************************************
894LRESULT Win32Dialog::DefDlgProcA(UINT Msg, WPARAM wParam, LPARAM lParam)
895{
896 BOOL result = FALSE;
897
898 msgResult = 0;
899
900 //Dialogs never receive these messages
901 if (Msg == WM_CREATE || Msg == WM_NCCREATE) {
902 return (LRESULT)1;
903 }
904 //Never send a WM_NCCALCSIZE to a dialog before it has received it's WM_INITDIALOG message
905 //(causes problems for sysinf32.exe)
906 if(!fDialogInit && Msg == WM_NCCALCSIZE) {
907 return DefWindowProcA(Msg, wParam, lParam );
908 }
909
910 if (Win32DlgProc) { /* Call dialog procedure */
911 result = Win32DlgProc(getWindowHandle(), Msg, wParam, lParam);
912 }
913
914 if (!result && IsWindow())
915 {
916 /* callback didn't process this message */
917 switch(Msg)
918 {
919 case WM_ERASEBKGND:
920 case WM_SHOWWINDOW:
921 case WM_ACTIVATE:
922 case WM_SETFOCUS:
923 case DM_SETDEFID:
924 case DM_GETDEFID:
925 case WM_NEXTDLGCTL:
926 case WM_GETFONT:
927 case WM_CLOSE:
928 case WM_NCDESTROY:
929 case WM_ENTERMENULOOP:
930 case WM_LBUTTONDOWN:
931 case WM_NCLBUTTONDOWN:
932 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
933
934 case WM_INITDIALOG:
935 case WM_VKEYTOITEM:
936 case WM_COMPAREITEM:
937 case WM_CHARTOITEM:
938 break;
939
940 default:
941 return DefWindowProcA(Msg, wParam, lParam );
942 }
943 }
944 return DefDlg_Epilog(Msg, result);
945}
946//******************************************************************************
947//******************************************************************************
948LRESULT Win32Dialog::DefDlgProcW(UINT Msg, WPARAM wParam, LPARAM lParam)
949{
950 BOOL result = FALSE;
951
952 msgResult = 0;
953
954 //Dialogs never receive these messages
955 if (Msg == WM_CREATE || Msg == WM_NCCREATE) {
956 return (LRESULT)1;
957 }
958 //Never send a WM_NCCALCSIZE to a dialog before it has received it's WM_INITDIALOG message
959 //(causes problems for sysinf32.exe)
960 if(!fDialogInit && Msg == WM_NCCALCSIZE) {
961 return DefWindowProcW(Msg, wParam, lParam );
962 }
963
964 if (Win32DlgProc) { /* Call dialog procedure */
965 result = Win32DlgProc(getWindowHandle(), Msg, wParam, lParam);
966 }
967
968 if (!result && IsWindow())
969 {
970 /* callback didn't process this message */
971 switch(Msg)
972 {
973 case WM_ERASEBKGND:
974 case WM_SHOWWINDOW:
975 case WM_ACTIVATE:
976 case WM_SETFOCUS:
977 case DM_SETDEFID:
978 case DM_GETDEFID:
979 case WM_NEXTDLGCTL:
980 case WM_GETFONT:
981 case WM_CLOSE:
982 case WM_NCDESTROY:
983 case WM_ENTERMENULOOP:
984 case WM_LBUTTONDOWN:
985 case WM_NCLBUTTONDOWN:
986 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
987
988 case WM_INITDIALOG:
989 case WM_VKEYTOITEM:
990 case WM_COMPAREITEM:
991 case WM_CHARTOITEM:
992 break;
993
994 default:
995 return DefWindowProcW(Msg, wParam, lParam );
996 }
997 }
998 return DefDlg_Epilog(Msg, result);
999}
1000/***********************************************************************
1001 * DEFDLG_Epilog
1002 */
1003LRESULT Win32Dialog::DefDlg_Epilog(UINT msg, BOOL fResult)
1004{
1005 /* see SDK 3.1 */
1006 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
1007 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
1008 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
1009 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
1010 return fResult;
1011
1012 return msgResult;
1013}
1014/***********************************************************************
1015 * DEFDLG_SetFocus
1016 *
1017 * Set the focus to a control of the dialog, selecting the text if
1018 * the control is an edit dialog.
1019 */
1020void Win32Dialog::setFocus(HWND hwndCtrl )
1021{
1022 HWND hwndPrev = GetFocus();
1023
1024#ifdef __WIN32OS2__
1025 if(hwndPrev == hwndCtrl) {
1026 //do nothing
1027 //Acrobat Reader 4.05 opening screen (when EULA has not been shown
1028 //local machine/software/adobe/acrobat reader/4.0/adobeviewer/EULA = 0)
1029 //All text selected in edit control because focus set when the control
1030 //already has focus.
1031 //Not entirely sure this is 100% correct, but at least now it behaves
1032 //like AR 4.05 in Windows NT 4.0 SP6
1033 return;
1034 }
1035#endif
1036
1037 if (IsChild( hwndPrev ))
1038 {
1039 if (::SendMessageA( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
1040 ::SendMessageA( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
1041 }
1042 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
1043 ::SendMessageA(hwndCtrl, EM_SETSEL, 0, -1 );
1044 SetFocus( hwndCtrl );
1045}
1046
1047
1048/***********************************************************************
1049 * DEFDLG_SaveFocus
1050 */
1051BOOL Win32Dialog::saveFocus()
1052{
1053 HWND hwndCurrentFocus = GetFocus();
1054
1055 if (!hwndCurrentFocus || !IsChild( hwndCurrentFocus )) return FALSE;
1056
1057 hwndFocus = hwndCurrentFocus;
1058 /* Remove default button */
1059 return TRUE;
1060}
1061
1062
1063/***********************************************************************
1064 * DEFDLG_RestoreFocus
1065 */
1066BOOL Win32Dialog::restoreFocus()
1067{
1068 if (!hwndFocus || IsWindowIconic()) return FALSE;
1069
1070 if (!::IsWindow( hwndFocus )) return FALSE;
1071
1072 /* Don't set the focus back to controls if EndDialog is already called.*/
1073 if (!(dialogFlags & DF_END))
1074 setFocus(hwndFocus);
1075
1076 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
1077 sometimes losing focus when receiving WM_SETFOCUS messages. */
1078 return TRUE;
1079}
1080
1081
1082/***********************************************************************
1083 * DEFDLG_FindDefButton
1084 *
1085 * Find the current default push-button.
1086 */
1087HWND Win32Dialog::findDefButton()
1088{
1089 HWND hwndChild = GetWindow( GW_CHILD );
1090 while (hwndChild)
1091 {
1092 if (::SendMessageA( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
1093 break;
1094 hwndChild = ::GetWindow( hwndChild, GW_HWNDNEXT );
1095 }
1096 return hwndChild;
1097}
1098
1099
1100/***********************************************************************
1101 * DEFDLG_SetDefButton
1102 *
1103 * Set the new default button to be hwndNew.
1104 */
1105BOOL Win32Dialog::setDefButton(HWND hwndNew )
1106{
1107 if (hwndNew &&
1108 !(::SendMessageA(hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
1109 return FALSE; /* Destination is not a push button */
1110
1111 if (idResult) /* There's already a default pushbutton */
1112 {
1113 HWND hwndOld = GetDlgItem( getWindowHandle(), idResult );
1114 if (::SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
1115 ::SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
1116 }
1117 if (hwndNew)
1118 {
1119 ::SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
1120 idResult = GetDlgCtrlID( hwndNew );
1121 }
1122 else idResult = 0;
1123 return TRUE;
1124}
1125//******************************************************************************
1126//******************************************************************************
1127BOOL Win32Dialog::endDialog(int retval)
1128{
1129 HWND hwnd = getWindowHandle();
1130
1131 dialogFlags |= DF_END;
1132 idResult = retval;
1133
1134// BOOL wasEnabled = (dlgInfo.flags & DF_OWNERENABLED);
1135
1136// if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1137// DIALOG_EnableOwner( owner );
1138
1139 /* Windows sets the focus to the dialog itself in EndDialog */
1140
1141#ifdef __WIN32OS2__
1142 if (::IsChild(hwnd, GetFocus()))
1143 {
1144 ::SetFocus( hwnd );
1145 //SvL: Enable the owner if the focus is on one of its children.
1146 // If we don't, then PM will switch focus to another app since
1147 // we hide the window below (hidden windows cannot have focus)
1148 HWND owner = ::GetWindow( hwnd, GW_OWNER);
1149 if(owner)
1150 ::EnableWindow(owner, TRUE);
1151 }
1152#else
1153 if (::IsChild(hwnd, GetFocus()))
1154 ::SetFocus( hwnd );
1155#endif
1156
1157 /* Don't have to send a ShowWindow(SW_HIDE), just do
1158 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1159
1160 ::SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1161 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1162
1163 /* unblock dialog loop */
1164 PostMessageA(hwnd, WM_NULL, 0, 0);
1165 return TRUE;
1166}
1167//******************************************************************************
1168//******************************************************************************
1169LONG Win32Dialog::SetWindowLong(int index, ULONG value, BOOL fUnicode)
1170{
1171 LONG oldval;
1172
1173 dprintf2(("Win32Dialog::SetWindowLongA %x %d %x", getWindowHandle(), index, value));
1174 switch(index)
1175 {
1176 case DWL_DLGPROC:
1177 {
1178 //Note: Type of SetWindowLong determines new window proc type
1179 // UNLESS the new window proc has already been registered
1180 // (use the old type in that case)
1181 // (VERIFIED in NT 4, SP6)
1182 WINDOWPROCTYPE type = WINPROC_GetProcType((HWINDOWPROC)value);
1183 if(type == WIN_PROC_INVALID) {
1184 type = (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A;
1185 }
1186 oldval = (LONG)WINPROC_GetProc(Win32DlgProc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
1187 WINPROC_SetProc((HWINDOWPROC *)&Win32DlgProc, (WNDPROC)value, type, WIN_PROC_WINDOW);
1188 return oldval;
1189 }
1190 case DWL_MSGRESULT:
1191 oldval = msgResult;
1192 msgResult = value;
1193 return oldval;
1194 case DWL_USER:
1195 oldval = userDlgData;
1196 userDlgData = value;
1197 return oldval;
1198 default:
1199 return Win32BaseWindow::SetWindowLong(index, value, fUnicode);
1200 }
1201}
1202//******************************************************************************
1203//******************************************************************************
1204ULONG Win32Dialog::GetWindowLong(int index, BOOL fUnicode)
1205{
1206 dprintf2(("Win32Dialog::GetWindowLongA %x %d", getWindowHandle(), index));
1207 switch(index)
1208 {
1209 case DWL_DLGPROC:
1210 return (ULONG)WINPROC_GetProc(Win32DlgProc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
1211 case DWL_MSGRESULT:
1212 return msgResult;
1213 case DWL_USER:
1214 return userDlgData;
1215 default:
1216 return Win32BaseWindow::GetWindowLong(index, fUnicode);
1217 }
1218}
1219//******************************************************************************
1220//******************************************************************************
1221BOOL DIALOG_Register()
1222{
1223 WNDCLASSA wndClass;
1224
1225 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
1226 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
1227 wndClass.lpfnWndProc = (WNDPROC)DefDlgProcA;
1228 wndClass.cbClsExtra = 0;
1229 wndClass.cbWndExtra = 0;
1230 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
1231 wndClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
1232 wndClass.lpszClassName = DIALOG_CLASS_NAMEA;
1233
1234 return RegisterClassA(&wndClass);
1235}
1236//******************************************************************************
1237//******************************************************************************
1238BOOL DIALOG_Unregister()
1239{
1240 if (GlobalFindAtomA(DIALOG_CLASS_NAMEA))
1241 return UnregisterClassA(DIALOG_CLASS_NAMEA,(HINSTANCE)NULL);
1242 else return FALSE;
1243}
1244//******************************************************************************
1245//******************************************************************************
1246BOOL Win32Dialog::fInitialized = FALSE;
1247int Win32Dialog::xBaseUnit = 10;
1248int Win32Dialog::yBaseUnit = 20;
Note: See TracBrowser for help on using the repository browser.