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

Last change on this file since 3662 was 3662, checked in by sandervl, 25 years ago

Major rewrite: frame/client -> frame

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