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

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

minor logging updates

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