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

Last change on this file since 8849 was 8849, checked in by sandervl, 23 years ago

do not set focus to child dialog controls

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