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

Last change on this file since 10545 was 10545, checked in by sandervl, 21 years ago

updates

File size: 38.6 KB
Line 
1/* $Id: win32dlg.cpp,v 1.83 2004-03-18 15:38:42 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 dprintf(("Dialog base unit size (%d,%d)", size.cx, size.cy));
429 xBaseUnit = size.cx;
430 yBaseUnit = size.cy;
431
432 return TRUE;
433}
434/***********************************************************************
435 * DIALOG_GetCharSizeFromDC
436 *
437 *
438 * Calculates the *true* average size of English characters in the
439 * specified font as oppposed to the one returned by GetTextMetrics.
440 */
441BOOL Win32Dialog::getCharSizeFromDC( HDC hDC, HFONT hUserFont, SIZE * pSize )
442{
443 BOOL Success = FALSE;
444 HFONT hFontPrev = 0;
445 pSize->cx = xBaseUnit;
446 pSize->cy = yBaseUnit;
447
448 if ( hDC )
449 {
450 /* select the font */
451 TEXTMETRICA tm;
452 memset(&tm,0,sizeof(tm));
453 if (hUserFont) hFontPrev = SelectFont(hDC,hUserFont);
454 if (GetTextMetricsA(hDC,&tm))
455 {
456 pSize->cx = tm.tmAveCharWidth;
457 pSize->cy = tm.tmHeight;
458
459 /* if variable width font */
460 if (tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
461 {
462 SIZE total;
463 static const char szAvgChars[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
464
465 /* Calculate a true average as opposed to the one returned
466 * by tmAveCharWidth. This works better when dealing with
467 * proportional spaced fonts and (more important) that's
468 * how Microsoft's dialog creation code calculates the size
469 * of the font
470 */
471#ifdef __WIN32OS2__
472 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars)-1,&total))
473 {
474 /* round up */
475 pSize->cx = ((2*total.cx/(sizeof(szAvgChars)-1)) + 1)/2;
476 Success = TRUE;
477 }
478#else
479 if (GetTextExtentPointA(hDC,szAvgChars,sizeof(szAvgChars),&total))
480 {
481 /* round up */
482 pSize->cx = ((2*total.cx/sizeof(szAvgChars)) + 1)/2;
483 Success = TRUE;
484 }
485#endif
486 }
487 else
488 {
489 Success = TRUE;
490 }
491 }
492
493 /* select the original font */
494 if (hFontPrev) SelectFont(hDC,hFontPrev);
495 }
496 return (Success);
497}
498/***********************************************************************
499 * DIALOG_GetCharSize
500 *
501 *
502 * Calculates the *true* average size of English characters in the
503 * specified font as oppposed to the one returned by GetTextMetrics.
504 * A convenient variant of DIALOG_GetCharSizeFromDC.
505 */
506BOOL Win32Dialog::getCharSize( HFONT hUserFont, SIZE * pSize )
507{
508 HDC hDC = CreateCompatibleDC(0);
509 BOOL Success = getCharSizeFromDC( hDC, hUserFont, pSize );
510 DeleteDC(hDC);
511 return Success;
512}
513/***********************************************************************
514 * DIALOG_ParseTemplate32
515 *
516 * Fill a DLG_TEMPLATE structure from the dialog template, and return
517 * a pointer to the first control.
518 */
519LPCSTR Win32Dialog::parseTemplate( LPCSTR dlgtemplate, DLG_TEMPLATE * result )
520{
521 const WORD *p = (const WORD *)dlgtemplate;
522
523 result->style = GET_DWORD(p); p += 2;
524 if (result->style == 0xffff0001) /* DIALOGEX resource */
525 {
526 result->dialogEx = TRUE;
527 result->helpId = GET_DWORD(p); p += 2;
528 result->exStyle = GET_DWORD(p); p += 2;
529 result->style = GET_DWORD(p); p += 2;
530 }
531 else
532 {
533 result->dialogEx = FALSE;
534 result->helpId = 0;
535 result->exStyle = GET_DWORD(p); p += 2;
536 }
537 result->nbItems = GET_WORD(p); p++;
538 //x & y are signed words
539 result->x = GET_SHORT(p); p++;
540 result->y = GET_SHORT(p); p++;
541 result->cx = GET_WORD(p); p++;
542 result->cy = GET_WORD(p); p++;
543
544 /* Get the menu name */
545
546 switch(GET_WORD(p))
547 {
548 case 0x0000:
549 result->menuName = NULL;
550 p++;
551 break;
552 case 0xffff:
553 result->menuName = (LPCSTR)(UINT)GET_WORD( p + 1 );
554 p += 2;
555 break;
556 default:
557 result->menuName = (LPCSTR)p;
558 p += lstrlenW( (LPCWSTR)p ) + 1;
559 break;
560 }
561
562 /* Get the class name */
563 switch(GET_WORD(p))
564 {
565 case 0x0000:
566 result->className = (LPCSTR)DIALOG_CLASS_NAMEW;
567 p++;
568 break;
569 case 0xffff:
570 result->className = (LPCSTR)(UINT)GET_WORD( p + 1 );
571 p += 2;
572 break;
573 default:
574 result->className = (LPCSTR)p;
575 p += lstrlenW( (LPCWSTR)p ) + 1;
576 break;
577 }
578
579 /* Get the window caption */
580
581 result->caption = (LPCSTR)p;
582 p += lstrlenW( (LPCWSTR)p ) + 1;
583
584 /* Get the font name */
585
586 if (result->style & DS_SETFONT)
587 {
588 result->pointSize = GET_WORD(p);
589 p++;
590 if (result->dialogEx)
591 {
592 result->weight = GET_WORD(p); p++;
593 result->italic = LOBYTE(GET_WORD(p)); p++;
594 }
595 else
596 {
597 result->weight = FW_DONTCARE;
598 result->italic = FALSE;
599 }
600 result->faceName = (LPCSTR)p;
601 p += lstrlenW( (LPCWSTR)p ) + 1;
602 }
603
604 /* First control is on dword boundary */
605 return (LPCSTR)((((int)p) + 3) & ~3);
606}
607/***********************************************************************
608 * DIALOG_GetControl32
609 *
610 * Return the class and text of the control pointed to by ptr,
611 * fill the header structure and return a pointer to the next control.
612 */
613WORD *Win32Dialog::getControl(const WORD *p, DLG_CONTROL_INFO *info, BOOL dialogEx)
614{
615 if (dialogEx)
616 {
617 info->helpId = GET_DWORD(p); p += 2;
618 info->exStyle = GET_DWORD(p); p += 2;
619 info->style = GET_DWORD(p); p += 2;
620 }
621 else
622 {
623 info->helpId = 0;
624 info->style = GET_DWORD(p); p += 2;
625 info->exStyle = GET_DWORD(p); p += 2;
626 }
627 //SvL: x & y are signed words!
628 info->x = GET_SHORT(p); p++;
629 info->y = GET_SHORT(p); p++;
630 info->cx = GET_WORD(p); p++;
631 info->cy = GET_WORD(p); p++;
632
633 if (dialogEx)
634 {
635 /* id is a DWORD for DIALOGEX */
636 info->id = GET_DWORD(p);
637 p += 2;
638 }
639 else
640 {
641 info->id = GET_WORD(p);
642 p++;
643 }
644
645 if (GET_WORD(p) == 0xffff)
646 {
647 static const WCHAR class_names[6][10] =
648 {
649 { 'B','u','t','t','o','n', }, /* 0x80 */
650 { 'E','d','i','t', }, /* 0x81 */
651 { 'S','t','a','t','i','c', }, /* 0x82 */
652 { 'L','i','s','t','B','o','x', }, /* 0x83 */
653 { 'S','c','r','o','l','l','B','a','r', }, /* 0x84 */
654 { 'C','o','m','b','o','B','o','x', } /* 0x85 */
655 };
656 WORD id = GET_WORD(p+1);
657 if ((id >= 0x80) && (id <= 0x85))
658 info->className = (LPCSTR)class_names[id - 0x80];
659 else
660 {
661 info->className = NULL;
662 dprintf(("Unknown built-in class id %04x\n", id ));
663 }
664 p += 2;
665 }
666 else
667 {
668 info->className = (LPCSTR)p;
669 p += lstrlenW( (LPCWSTR)p ) + 1;
670 }
671
672 if (GET_WORD(p) == 0xffff) /* Is it an integer id? */
673 {
674 info->windowName = (LPCSTR)(UINT)GET_WORD(p + 1);
675 //Wine add 3 (bytes) here. But that causes problems with certain
676 //InstallShield installers.
677 p += 2;
678 }
679 else
680 {
681 info->windowName = (LPCSTR)p;
682 p += lstrlenW( (LPCWSTR)p ) + 1;
683 }
684
685 if (GET_WORD(p))
686 {
687 info->data = (LPVOID)(p + 1);
688 p += GET_WORD(p) / sizeof(WORD);
689 }
690 else info->data = NULL;
691 p++;
692
693 /* Next control is on dword boundary */
694 return (WORD *)((((int)p) + 3) & ~3);
695}
696
697
698/***********************************************************************
699 * DIALOG_CreateControls
700 *
701 * Create the control windows for a dialog.
702 */
703BOOL Win32Dialog::createControls(LPCSTR dlgtemplate, HINSTANCE hInst)
704{
705 DLG_CONTROL_INFO info;
706 HWND hwndCtrl, hwndDefButton = 0;
707 INT items = dlgInfo.nbItems;
708
709 while (items--)
710 {
711 dlgtemplate = (LPCSTR)getControl( (WORD *)dlgtemplate, &info, dlgInfo.dialogEx );
712
713 /* Is this it? */
714 if (info.style & WS_BORDER)
715 {
716 info.style &= ~WS_BORDER;
717 info.exStyle |= WS_EX_CLIENTEDGE;
718 }
719
720 dprintf(("Create CONTROL %d", info.id));
721
722 hwndCtrl = ::CreateWindowExW( info.exStyle | WS_EX_NOPARENTNOTIFY,
723 (LPWSTR)info.className,
724 (LPWSTR)info.windowName,
725 info.style | WS_CHILD,
726 MulDiv(info.x, xUnit, 4),
727 MulDiv(info.y, yUnit, 8),
728 MulDiv(info.cx, xUnit, 4),
729 MulDiv(info.cy, yUnit, 8),
730 getWindowHandle(), (HMENU)info.id,
731 hInst, info.data );
732
733 if (!hwndCtrl) return FALSE;
734
735 /* Send initialisation messages to the control */
736 if (hUserFont) ::SendMessageA( hwndCtrl, WM_SETFONT, (WPARAM)hUserFont, 0 );
737
738 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
739 {
740 /* If there's already a default push-button, set it back */
741 /* to normal and use this one instead. */
742 if (hwndDefButton)
743 ::SendMessageA( hwndDefButton, BM_SETSTYLE,
744 BS_PUSHBUTTON,FALSE );
745 hwndDefButton = hwndCtrl;
746 idResult = ::GetWindowWord( hwndCtrl, GWW_ID );
747 }
748 dprintf(("Create CONTROL %d DONE", info.id));
749 }
750 return TRUE;
751}
752/***********************************************************************
753 * DEFDLG_Proc
754 *
755 * Implementation of DefDlgProc(). Only handle messages that need special
756 * handling for dialogs.
757 */
758LRESULT Win32Dialog::DefDlg_Proc(UINT msg, WPARAM wParam, LPARAM lParam)
759{
760 switch(msg)
761 {
762 case WM_ERASEBKGND:
763 {
764 RECT rect;
765 int rc;
766
767 if (!windowClass) return 0;
768
769 rc = GetClipBox( (HDC)wParam, &rect );
770 if ((rc == SIMPLEREGION) || (rc == COMPLEXREGION))
771 {
772 HBRUSH hBrush = SendMessageA(getWindowHandle(), WM_CTLCOLORDLG, wParam, getWindowHandle());
773 if(GetObjectType(hBrush) != OBJ_BRUSH) {
774 DefWndControlColor(CTLCOLOR_DLG, (HDC)wParam);
775 }
776 FillRect( (HDC)wParam, &rect, hBrush);
777 }
778
779 return 1;
780 }
781
782 case WM_NCDESTROY:
783 /* Free dialog heap (if created) */
784#if 0
785 if (dlgInfo->hDialogHeap)
786 {
787 GlobalUnlock16(dlgInfo->hDialogHeap);
788 GlobalFree16(dlgInfo->hDialogHeap);
789 dlgInfo->hDialogHeap = 0;
790 }
791#endif
792 /* Delete font */
793 if (hUserFont)
794 {
795 DeleteObject( hUserFont );
796 hUserFont = 0;
797 }
798
799 /* Delete menu */
800 if (hMenu)
801 {
802 DestroyMenu( hMenu );
803 hMenu = 0;
804 }
805
806 /* Delete window procedure */
807 Win32DlgProc = 0;
808 dialogFlags |= DF_END; /* just in case */
809
810 /* Window clean-up */
811 return DefWindowProcA(msg, wParam, lParam );
812
813 case WM_SHOWWINDOW:
814 if (!wParam) saveFocus();
815 return DefWindowProcA(msg, wParam, lParam );
816
817 case WM_ACTIVATE:
818 if (wParam) {
819 restoreFocus();
820 }
821 else saveFocus();
822 return 0;
823
824 case WM_SETFOCUS:
825 restoreFocus();
826 return 0;
827
828 case DM_SETDEFID:
829 if (dialogFlags & DF_END)
830 return 1;
831
832 setDefButton(wParam ? GetDlgItem( getWindowHandle(), wParam ) : 0 );
833 return 1;
834
835 case DM_GETDEFID:
836 {
837 HWND hwndDefId;
838 if (dialogFlags & DF_END) return 0;
839 if (idResult)
840 return MAKELONG( idResult, DC_HASDEFID );
841 if ((hwndDefId = findDefButton()) != 0)
842 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
843
844 return 0;
845 }
846
847 case WM_NEXTDLGCTL:
848 {
849 HWND hwndDest = (HWND)wParam;
850 if (!lParam)
851 hwndDest = GetNextDlgTabItem(getWindowHandle(), GetFocus(), wParam);
852 if (hwndDest) setFocus( hwndDest );
853 setDefButton( hwndDest );
854 return 0;
855 }
856
857 case WM_ENTERMENULOOP:
858 case WM_LBUTTONDOWN:
859 case WM_NCLBUTTONDOWN:
860 {
861 HWND hwndCurFocus = GetFocus();
862 if (hwndCurFocus)
863 {
864 Win32BaseWindow *wndFocus = Win32BaseWindow::GetWindowFromHandle(hwndFocus);
865
866 if(wndFocus)
867 {
868 /* always make combo box hide its listbox control */
869 if( CONTROLS_IsControl( wndFocus, COMBOBOX_CONTROL ) )
870 SendMessageA(wndFocus->getWindowHandle(), CB_SHOWDROPDOWN, FALSE, 0 );
871 else
872 if( CONTROLS_IsControl( wndFocus, EDIT_CONTROL ) &&
873 CONTROLS_IsControl( wndFocus->getParent(), COMBOBOX_CONTROL ))
874 SendMessageA(wndFocus->getWindowHandle(), CB_SHOWDROPDOWN, FALSE, 0 );
875 RELEASE_WNDOBJ(wndFocus);
876 }
877 }
878 return DefWindowProcA( msg, wParam, lParam );
879 }
880
881 case WM_GETFONT:
882 return hUserFont;
883
884 case WM_CLOSE:
885 PostMessageA(getWindowHandle(), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( getWindowHandle(), IDCANCEL ) );
886 return 0;
887
888 case WM_NOTIFYFORMAT:
889 return DefWindowProcA(msg, wParam, lParam );
890 }
891 return 0;
892}
893//******************************************************************************
894//******************************************************************************
895LRESULT Win32Dialog::DefDlgProcA(UINT Msg, WPARAM wParam, LPARAM lParam)
896{
897 BOOL result = FALSE;
898
899 msgResult = 0;
900
901 //Dialogs never receive these messages
902 if (Msg == WM_CREATE || Msg == WM_NCCREATE) {
903 return (LRESULT)1;
904 }
905 //Never send a WM_NCCALCSIZE to a dialog before it has received it's WM_INITDIALOG message
906 //(causes problems for sysinf32.exe)
907 if(!fDialogInit && Msg == WM_NCCALCSIZE) {
908 return DefWindowProcA(Msg, wParam, lParam );
909 }
910
911 if (Win32DlgProc) { /* Call dialog procedure */
912 result = CallWindowProcA((WNDPROC)Win32DlgProc, getWindowHandle(), Msg, wParam, lParam);
913 }
914
915 if (!result && IsWindow())
916 {
917 /* callback didn't process this message */
918 switch(Msg)
919 {
920 case WM_ERASEBKGND:
921 case WM_SHOWWINDOW:
922 case WM_ACTIVATE:
923 case WM_SETFOCUS:
924 case DM_SETDEFID:
925 case DM_GETDEFID:
926 case WM_NEXTDLGCTL:
927 case WM_GETFONT:
928 case WM_CLOSE:
929 case WM_NCDESTROY:
930 case WM_ENTERMENULOOP:
931 case WM_LBUTTONDOWN:
932 case WM_NCLBUTTONDOWN:
933 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
934
935 case WM_INITDIALOG:
936 case WM_VKEYTOITEM:
937 case WM_COMPAREITEM:
938 case WM_CHARTOITEM:
939 break;
940
941 default:
942 return DefWindowProcA(Msg, wParam, lParam );
943 }
944 }
945 return DefDlg_Epilog(Msg, result);
946}
947//******************************************************************************
948//******************************************************************************
949LRESULT Win32Dialog::DefDlgProcW(UINT Msg, WPARAM wParam, LPARAM lParam)
950{
951 BOOL result = FALSE;
952
953 msgResult = 0;
954
955 //Dialogs never receive these messages
956 if (Msg == WM_CREATE || Msg == WM_NCCREATE) {
957 return (LRESULT)1;
958 }
959 //Never send a WM_NCCALCSIZE to a dialog before it has received it's WM_INITDIALOG message
960 //(causes problems for sysinf32.exe)
961 if(!fDialogInit && Msg == WM_NCCALCSIZE) {
962 return DefWindowProcW(Msg, wParam, lParam );
963 }
964
965 if (Win32DlgProc) { /* Call dialog procedure */
966 result = CallWindowProcW((WNDPROC)Win32DlgProc, getWindowHandle(), Msg, wParam, lParam);
967 }
968
969 if (!result && IsWindow())
970 {
971 /* callback didn't process this message */
972 switch(Msg)
973 {
974 case WM_ERASEBKGND:
975 case WM_SHOWWINDOW:
976 case WM_ACTIVATE:
977 case WM_SETFOCUS:
978 case DM_SETDEFID:
979 case DM_GETDEFID:
980 case WM_NEXTDLGCTL:
981 case WM_GETFONT:
982 case WM_CLOSE:
983 case WM_NCDESTROY:
984 case WM_ENTERMENULOOP:
985 case WM_LBUTTONDOWN:
986 case WM_NCLBUTTONDOWN:
987 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
988
989 case WM_INITDIALOG:
990 case WM_VKEYTOITEM:
991 case WM_COMPAREITEM:
992 case WM_CHARTOITEM:
993 break;
994
995 default:
996 return DefWindowProcW(Msg, wParam, lParam );
997 }
998 }
999 return DefDlg_Epilog(Msg, result);
1000}
1001/***********************************************************************
1002 * DEFDLG_Epilog
1003 */
1004LRESULT Win32Dialog::DefDlg_Epilog(UINT msg, BOOL fResult)
1005{
1006 /* see SDK 3.1 */
1007 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
1008 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
1009 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
1010 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
1011 return fResult;
1012
1013 return msgResult;
1014}
1015/***********************************************************************
1016 * DEFDLG_SetFocus
1017 *
1018 * Set the focus to a control of the dialog, selecting the text if
1019 * the control is an edit dialog.
1020 */
1021void Win32Dialog::setFocus(HWND hwndCtrl )
1022{
1023 HWND hwndPrev = GetFocus();
1024
1025#ifdef __WIN32OS2__
1026 if(hwndPrev == hwndCtrl) {
1027 //do nothing
1028 //Acrobat Reader 4.05 opening screen (when EULA has not been shown
1029 //local machine/software/adobe/acrobat reader/4.0/adobeviewer/EULA = 0)
1030 //All text selected in edit control because focus set when the control
1031 //already has focus.
1032 //Not entirely sure this is 100% correct, but at least now it behaves
1033 //like AR 4.05 in Windows NT 4.0 SP6
1034 return;
1035 }
1036#endif
1037
1038 if (IsChild( hwndPrev ))
1039 {
1040 if (::SendMessageA( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
1041 ::SendMessageA( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
1042 }
1043 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
1044 ::SendMessageA(hwndCtrl, EM_SETSEL, 0, -1 );
1045 SetFocus( hwndCtrl );
1046}
1047
1048
1049/***********************************************************************
1050 * DEFDLG_SaveFocus
1051 */
1052BOOL Win32Dialog::saveFocus()
1053{
1054 HWND hwndCurrentFocus = GetFocus();
1055
1056 if (!hwndCurrentFocus || !IsChild( hwndCurrentFocus )) return FALSE;
1057
1058 hwndFocus = hwndCurrentFocus;
1059 /* Remove default button */
1060 return TRUE;
1061}
1062
1063
1064/***********************************************************************
1065 * DEFDLG_RestoreFocus
1066 */
1067BOOL Win32Dialog::restoreFocus()
1068{
1069 if (!hwndFocus || IsWindowIconic()) return FALSE;
1070
1071 if (!::IsWindow( hwndFocus )) return FALSE;
1072
1073 /* Don't set the focus back to controls if EndDialog is already called.*/
1074 if (!(dialogFlags & DF_END))
1075 setFocus(hwndFocus);
1076
1077 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
1078 sometimes losing focus when receiving WM_SETFOCUS messages. */
1079 return TRUE;
1080}
1081
1082
1083/***********************************************************************
1084 * DEFDLG_FindDefButton
1085 *
1086 * Find the current default push-button.
1087 */
1088HWND Win32Dialog::findDefButton()
1089{
1090 HWND hwndChild = GetWindow( GW_CHILD );
1091 while (hwndChild)
1092 {
1093 if (::SendMessageA( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
1094 break;
1095 hwndChild = ::GetWindow( hwndChild, GW_HWNDNEXT );
1096 }
1097 return hwndChild;
1098}
1099
1100
1101/***********************************************************************
1102 * DEFDLG_SetDefButton
1103 *
1104 * Set the new default button to be hwndNew.
1105 */
1106BOOL Win32Dialog::setDefButton(HWND hwndNew )
1107{
1108 if (hwndNew &&
1109 !(::SendMessageA(hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
1110 return FALSE; /* Destination is not a push button */
1111
1112 if (idResult) /* There's already a default pushbutton */
1113 {
1114 HWND hwndOld = GetDlgItem( getWindowHandle(), idResult );
1115 if (::SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
1116 ::SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
1117 }
1118 if (hwndNew)
1119 {
1120 ::SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
1121 idResult = GetDlgCtrlID( hwndNew );
1122 }
1123 else idResult = 0;
1124 return TRUE;
1125}
1126//******************************************************************************
1127//******************************************************************************
1128BOOL Win32Dialog::endDialog(int retval)
1129{
1130 HWND hwnd = getWindowHandle();
1131
1132 dialogFlags |= DF_END;
1133 idResult = retval;
1134
1135// BOOL wasEnabled = (dlgInfo.flags & DF_OWNERENABLED);
1136
1137// if (wasEnabled && (owner = GetWindow( hwnd, GW_OWNER )))
1138// DIALOG_EnableOwner( owner );
1139
1140 /* Windows sets the focus to the dialog itself in EndDialog */
1141
1142#ifdef __WIN32OS2__
1143 if (::IsChild(hwnd, GetFocus()))
1144 {
1145 ::SetFocus( hwnd );
1146 //SvL: Enable the owner if the focus is on one of its children.
1147 // If we don't, then PM will switch focus to another app since
1148 // we hide the window below (hidden windows cannot have focus)
1149 HWND owner = ::GetWindow( hwnd, GW_OWNER);
1150 if(owner)
1151 ::EnableWindow(owner, TRUE);
1152 }
1153#else
1154 if (::IsChild(hwnd, GetFocus()))
1155 ::SetFocus( hwnd );
1156#endif
1157
1158 /* Don't have to send a ShowWindow(SW_HIDE), just do
1159 SetWindowPos with SWP_HIDEWINDOW as done in Windows */
1160
1161 ::SetWindowPos(hwnd, (HWND)0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE
1162 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
1163
1164 /* unblock dialog loop */
1165 PostMessageA(hwnd, WM_NULL, 0, 0);
1166 return TRUE;
1167}
1168//******************************************************************************
1169//******************************************************************************
1170LONG Win32Dialog::SetWindowLong(int index, ULONG value, BOOL fUnicode)
1171{
1172 LONG oldval;
1173
1174 dprintf2(("Win32Dialog::SetWindowLongA %x %d %x", getWindowHandle(), index, value));
1175 switch(index)
1176 {
1177 case DWL_DLGPROC:
1178 {
1179 //Note: Type of SetWindowLong determines new window proc type
1180 // UNLESS the new window proc has already been registered
1181 // (use the old type in that case)
1182 // (VERIFIED in NT 4, SP6)
1183 WINDOWPROCTYPE type = WINPROC_GetProcType((HWINDOWPROC)value);
1184 if(type == WIN_PROC_INVALID) {
1185 type = (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A;
1186 }
1187 oldval = (LONG)WINPROC_GetProc(Win32DlgProc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
1188 WINPROC_SetProc((HWINDOWPROC *)&Win32DlgProc, (WNDPROC)value, type, WIN_PROC_WINDOW);
1189 return oldval;
1190 }
1191 case DWL_MSGRESULT:
1192 oldval = msgResult;
1193 msgResult = value;
1194 return oldval;
1195 case DWL_USER:
1196 oldval = userDlgData;
1197 userDlgData = value;
1198 return oldval;
1199 default:
1200 return Win32BaseWindow::SetWindowLong(index, value, fUnicode);
1201 }
1202}
1203//******************************************************************************
1204//******************************************************************************
1205ULONG Win32Dialog::GetWindowLong(int index, BOOL fUnicode)
1206{
1207 dprintf2(("Win32Dialog::GetWindowLongA %x %d", getWindowHandle(), index));
1208 switch(index)
1209 {
1210 case DWL_DLGPROC:
1211 return (ULONG)WINPROC_GetProc(Win32DlgProc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
1212 case DWL_MSGRESULT:
1213 return msgResult;
1214 case DWL_USER:
1215 return userDlgData;
1216 default:
1217 return Win32BaseWindow::GetWindowLong(index, fUnicode);
1218 }
1219}
1220//******************************************************************************
1221//******************************************************************************
1222BOOL DIALOG_Register()
1223{
1224 WNDCLASSA wndClass;
1225
1226 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
1227 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
1228 wndClass.lpfnWndProc = (WNDPROC)DefDlgProcA;
1229 wndClass.cbClsExtra = 0;
1230 wndClass.cbWndExtra = 0;
1231 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
1232 wndClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
1233 wndClass.lpszClassName = DIALOG_CLASS_NAMEA;
1234
1235 return RegisterClassA(&wndClass);
1236}
1237//******************************************************************************
1238//******************************************************************************
1239BOOL DIALOG_Unregister()
1240{
1241 if (GlobalFindAtomA(DIALOG_CLASS_NAMEA))
1242 return UnregisterClassA(DIALOG_CLASS_NAMEA,(HINSTANCE)NULL);
1243 else return FALSE;
1244}
1245//******************************************************************************
1246//******************************************************************************
1247BOOL Win32Dialog::fInitialized = FALSE;
1248int Win32Dialog::xBaseUnit = 10;
1249int Win32Dialog::yBaseUnit = 20;
Note: See TracBrowser for help on using the repository browser.