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

Last change on this file since 9523 was 9523, checked in by sandervl, 23 years ago

removed obsolete files

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