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

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

OemToCharBuffA + DefDlgProc/WM_ERASEBKGND fixes

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