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

Last change on this file since 5173 was 5173, checked in by sandervl, 25 years ago

DestroyWindow during CreateWindow fix

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