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

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

client/frame rewrite

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