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

Last change on this file since 2803 was 2803, checked in by sandervl, 26 years ago

Added new logging feature

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