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

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

DS_CONTROL fix + workaround for file open dialog in showwindow

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