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

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

negative dialog control origin bugfix

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