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

Last change on this file since 2013 was 1762, checked in by cbratschi, 26 years ago

cursor handling fixed

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