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

Last change on this file since 7404 was 7404, checked in by sandervl, 24 years ago

dialog focus fix: don't set focus to dialog child if the dialog has a parent

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