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

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

create dialog controls with CreateWindowExW; use correct SendMessage during CreateWindow

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