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

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

hittest workaround for static controls + registerwindowmessage rewrite

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