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

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

dialog control creation fix

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