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

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

dlg fix, keyboard additions, + misc other things

File size: 32.6 KB
Line 
1/* $Id: win32dlg.cpp,v 1.33 1999-11-11 13:17:31 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 "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 hwndCtrl = ::CreateWindowExA( info.exStyle | WS_EX_NOPARENTNOTIFY,
642 classNameA,
643 windowNameA,
644 info.style | WS_CHILD | WS_CLIPSIBLINGS,
645 info.x * xUnit / 4,
646 info.y * yUnit / 8,
647 info.cx * xUnit / 4,
648 info.cy * yUnit / 8,
649 getWindowHandle(), (HMENU)info.id,
650 hInst, info.data );
651 if(HIWORD(classNameA)) {
652 FreeAsciiString(classNameA);
653 }
654 if(windowNameA) {
655 FreeAsciiString(windowNameA);
656 }
657
658 if (!hwndCtrl) return FALSE;
659
660 /* Send initialisation messages to the control */
661 if (hUserFont) ::SendMessageA( hwndCtrl, WM_SETFONT, (WPARAM)hUserFont, 0 );
662
663 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
664 {
665 /* If there's already a default push-button, set it back */
666 /* to normal and use this one instead. */
667 if (hwndDefButton)
668 ::SendMessageA( hwndDefButton, BM_SETSTYLE,
669 BS_PUSHBUTTON,FALSE );
670 hwndDefButton = hwndCtrl;
671 idResult = ::GetWindowWord( hwndCtrl, GWW_ID );
672 }
673 dprintf(("Create CONTROL %d DONE", info.id));
674 }
675 return TRUE;
676}
677/***********************************************************************
678 * DEFDLG_Proc
679 *
680 * Implementation of DefDlgProc(). Only handle messages that need special
681 * handling for dialogs.
682 */
683LRESULT Win32Dialog::DefDlg_Proc(UINT msg, WPARAM wParam, LPARAM lParam)
684{
685 switch(msg)
686 {
687 case WM_ERASEBKGND:
688 {
689 RECT rect;
690 int rc;
691 /* Since WM_ERASEBKGND may receive either a window dc or a */
692 /* client dc, the area to be erased has to be retrieved from */
693 /* the device context. */
694 rc = GetClipBox( (HDC)wParam, &rect );
695 if ((rc == SIMPLEREGION) || (rc == COMPLEXREGION))
696 FillRect( (HDC)wParam, &rect, windowClass->getBackgroundBrush());
697 return 1;
698 }
699 case WM_NCDESTROY:
700 /* Free dialog heap (if created) */
701#if 0
702 if (dlgInfo->hDialogHeap)
703 {
704 GlobalUnlock16(dlgInfo->hDialogHeap);
705 GlobalFree16(dlgInfo->hDialogHeap);
706 dlgInfo->hDialogHeap = 0;
707 }
708#endif
709 /* Delete font */
710 if (hUserFont)
711 {
712 DeleteObject( hUserFont );
713 hUserFont = 0;
714 }
715
716 /* Delete menu */
717 if (hMenu)
718 {
719 DestroyMenu( hMenu );
720 hMenu = 0;
721 }
722
723 /* Delete window procedure */
724 Win32DlgProc = 0;
725 dialogFlags |= DF_END; /* just in case */
726
727 /* Window clean-up */
728 return DefWindowProcA(msg, wParam, lParam );
729
730 case WM_SHOWWINDOW:
731 if (!wParam) saveFocus();
732 return DefWindowProcA(msg, wParam, lParam );
733
734 case WM_ACTIVATE:
735 if (wParam) {
736 restoreFocus();
737 }
738 else saveFocus();
739 return 0;
740
741 case WM_SETFOCUS:
742 restoreFocus();
743 return 0;
744
745 case DM_SETDEFID:
746 if (dialogFlags & DF_END)
747 return 1;
748
749 setDefButton(wParam ? GetDlgItem( getWindowHandle(), wParam ) : 0 );
750 return 1;
751
752 case DM_GETDEFID:
753 {
754 HWND hwndDefId;
755 if (dialogFlags & DF_END) return 0;
756 if (idResult)
757 return MAKELONG( idResult, DC_HASDEFID );
758 if ((hwndDefId = findDefButton()) != 0)
759 return MAKELONG( GetDlgCtrlID( hwndDefId ), DC_HASDEFID);
760
761 return 0;
762 }
763
764 case WM_NEXTDLGCTL:
765 {
766 HWND hwndDest = (HWND)wParam;
767 if (!lParam)
768 hwndDest = GetNextDlgTabItem(getWindowHandle(), GetFocus(), wParam);
769 if (hwndDest) setFocus( hwndDest );
770 setDefButton( hwndDest );
771 return 0;
772 }
773
774 case WM_ENTERMENULOOP:
775 case WM_LBUTTONDOWN:
776 case WM_NCLBUTTONDOWN:
777 {
778 HWND hwndCurFocus = GetFocus();
779 if (hwndCurFocus)
780 {
781 Win32BaseWindow *wndFocus = Win32BaseWindow::GetWindowFromHandle(hwndFocus);
782
783 if(wndFocus)
784 {
785 /* always make combo box hide its listbox control */
786 if( WIDGETS_IsControl( wndFocus, COMBOBOX_CONTROL ) )
787 wndFocus->SendMessageA(CB_SHOWDROPDOWN, FALSE, 0 );
788 else
789 if( WIDGETS_IsControl( wndFocus, EDIT_CONTROL ) &&
790 WIDGETS_IsControl( wndFocus->getParent(), COMBOBOX_CONTROL ))
791 wndFocus->SendMessageA(CB_SHOWDROPDOWN, FALSE, 0 );
792 }
793 }
794 return DefWindowProcA( msg, wParam, lParam );
795 }
796
797 case WM_GETFONT:
798 return hUserFont;
799
800 case WM_CLOSE:
801 PostMessageA(WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( getWindowHandle(), IDCANCEL ) );
802 return 0;
803
804 case WM_NOTIFYFORMAT:
805 return DefWindowProcA(msg, wParam, lParam );
806 }
807 return 0;
808}
809//******************************************************************************
810//******************************************************************************
811LRESULT Win32Dialog::DefDlgProcA(UINT Msg, WPARAM wParam, LPARAM lParam)
812{
813 BOOL result = FALSE;
814
815 msgResult = 0;
816
817 if (Win32DlgProc) { /* Call dialog procedure */
818 result = Win32DlgProc(getWindowHandle(), Msg, wParam, lParam);
819 }
820
821 if (!result && IsWindow())
822 {
823 /* callback didn't process this message */
824 switch(Msg)
825 {
826 case WM_ERASEBKGND:
827 case WM_SHOWWINDOW:
828 case WM_ACTIVATE:
829 case WM_SETFOCUS:
830 case DM_SETDEFID:
831 case DM_GETDEFID:
832 case WM_NEXTDLGCTL:
833 case WM_GETFONT:
834 case WM_CLOSE:
835 case WM_NCDESTROY:
836 case WM_ENTERMENULOOP:
837 case WM_LBUTTONDOWN:
838 case WM_NCLBUTTONDOWN:
839 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
840
841 case WM_INITDIALOG:
842 case WM_VKEYTOITEM:
843 case WM_COMPAREITEM:
844 case WM_CHARTOITEM:
845 break;
846
847 default:
848 return DefWindowProcA(Msg, wParam, lParam );
849 }
850 }
851 return DefDlg_Epilog(Msg, result);
852}
853//******************************************************************************
854//******************************************************************************
855LRESULT Win32Dialog::DefDlgProcW(UINT Msg, WPARAM wParam, LPARAM lParam)
856{
857 BOOL result = FALSE;
858
859 msgResult = 0;
860
861 if (Win32DlgProc) { /* Call dialog procedure */
862 result = Win32DlgProc(getWindowHandle(), Msg, wParam, lParam);
863 }
864
865 if (!result && IsWindow())
866 {
867 /* callback didn't process this message */
868 switch(Msg)
869 {
870 case WM_ERASEBKGND:
871 case WM_SHOWWINDOW:
872 case WM_ACTIVATE:
873 case WM_SETFOCUS:
874 case DM_SETDEFID:
875 case DM_GETDEFID:
876 case WM_NEXTDLGCTL:
877 case WM_GETFONT:
878 case WM_CLOSE:
879 case WM_NCDESTROY:
880 case WM_ENTERMENULOOP:
881 case WM_LBUTTONDOWN:
882 case WM_NCLBUTTONDOWN:
883 return DefDlg_Proc(Msg, (WPARAM)wParam, lParam);
884
885 case WM_INITDIALOG:
886 case WM_VKEYTOITEM:
887 case WM_COMPAREITEM:
888 case WM_CHARTOITEM:
889 break;
890
891 default:
892 return DefWindowProcW(Msg, wParam, lParam );
893 }
894 }
895 return DefDlg_Epilog(Msg, result);
896}
897/***********************************************************************
898 * DEFDLG_Epilog
899 */
900LRESULT Win32Dialog::DefDlg_Epilog(UINT msg, BOOL fResult)
901{
902 /* see SDK 3.1 */
903 if ((msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) ||
904 msg == WM_CTLCOLOR || msg == WM_COMPAREITEM ||
905 msg == WM_VKEYTOITEM || msg == WM_CHARTOITEM ||
906 msg == WM_QUERYDRAGICON || msg == WM_INITDIALOG)
907 return fResult;
908
909 return msgResult;
910}
911/***********************************************************************
912 * DEFDLG_SetFocus
913 *
914 * Set the focus to a control of the dialog, selecting the text if
915 * the control is an edit dialog.
916 */
917void Win32Dialog::setFocus(HWND hwndCtrl )
918{
919 HWND hwndPrev = GetFocus();
920
921 if (IsChild( hwndPrev ))
922 {
923 if (::SendMessageA( hwndPrev, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
924 ::SendMessageA( hwndPrev, EM_SETSEL, TRUE, MAKELONG( -1, 0 ) );
925 }
926 if (::SendMessageA(hwndCtrl, WM_GETDLGCODE, 0, 0 ) & DLGC_HASSETSEL)
927 ::SendMessageA(hwndCtrl, EM_SETSEL, FALSE, MAKELONG( 0, -1 ) );
928 SetFocus( hwndCtrl );
929}
930
931
932/***********************************************************************
933 * DEFDLG_SaveFocus
934 */
935BOOL Win32Dialog::saveFocus()
936{
937 HWND hwndCurrentFocus = GetFocus();
938
939 if (!hwndCurrentFocus || !IsChild( hwndCurrentFocus )) return FALSE;
940
941 hwndFocus = hwndCurrentFocus;
942 /* Remove default button */
943 return TRUE;
944}
945
946
947/***********************************************************************
948 * DEFDLG_RestoreFocus
949 */
950BOOL Win32Dialog::restoreFocus()
951{
952 if (!hwndFocus || IsIconic()) return FALSE;
953
954 if (!::IsWindow( hwndFocus )) return FALSE;
955
956 /* Don't set the focus back to controls if EndDialog is already called.*/
957 if (!(dialogFlags & DF_END))
958 setFocus(hwndFocus);
959
960 /* This used to set infoPtr->hwndFocus to NULL for no apparent reason,
961 sometimes losing focus when receiving WM_SETFOCUS messages. */
962 return TRUE;
963}
964
965
966/***********************************************************************
967 * DEFDLG_FindDefButton
968 *
969 * Find the current default push-button.
970 */
971HWND Win32Dialog::findDefButton()
972{
973 HWND hwndChild = GetWindow( GW_CHILD );
974 while (hwndChild)
975 {
976 if (::SendMessageA( hwndChild, WM_GETDLGCODE, 0, 0 ) & DLGC_DEFPUSHBUTTON)
977 break;
978 hwndChild = ::GetWindow( hwndChild, GW_HWNDNEXT );
979 }
980 return hwndChild;
981}
982
983
984/***********************************************************************
985 * DEFDLG_SetDefButton
986 *
987 * Set the new default button to be hwndNew.
988 */
989BOOL Win32Dialog::setDefButton(HWND hwndNew )
990{
991 if (hwndNew &&
992 !(::SendMessageA(hwndNew, WM_GETDLGCODE, 0, 0 ) & DLGC_UNDEFPUSHBUTTON))
993 return FALSE; /* Destination is not a push button */
994
995 if (idResult) /* There's already a default pushbutton */
996 {
997 HWND hwndOld = GetDlgItem( getWindowHandle(), idResult );
998 if (::SendMessageA( hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON)
999 ::SendMessageA( hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
1000 }
1001 if (hwndNew)
1002 {
1003 ::SendMessageA( hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
1004 idResult = GetDlgCtrlID( hwndNew );
1005 }
1006 else idResult = 0;
1007 return TRUE;
1008}
1009//******************************************************************************
1010//******************************************************************************
1011BOOL Win32Dialog::endDialog(int retval)
1012{
1013 dialogFlags |= DF_END;
1014 idResult = retval;
1015 return TRUE;
1016}
1017//******************************************************************************
1018//******************************************************************************
1019LONG Win32Dialog::SetWindowLongA(int index, ULONG value)
1020{
1021 LONG oldval;
1022
1023 dprintf2(("Win32Dialog::SetWindowLongA %x %d %x", getWindowHandle(), index, value));
1024 switch(index)
1025 {
1026 case DWL_DLGPROC:
1027 oldval = (LONG)Win32DlgProc;
1028 Win32DlgProc = (DLGPROC)value;
1029 return oldval;
1030 case DWL_MSGRESULT:
1031 oldval = msgResult;
1032 msgResult = value;
1033 return oldval;
1034 case DWL_USER:
1035 oldval = userDlgData;
1036 userDlgData = value;
1037 return oldval;
1038 default:
1039 return Win32BaseWindow::SetWindowLongA(index, value);
1040 }
1041}
1042//******************************************************************************
1043//******************************************************************************
1044ULONG Win32Dialog::GetWindowLongA(int index)
1045{
1046 dprintf2(("Win32Dialog::GetWindowLongA %x %d", getWindowHandle(), index));
1047 switch(index)
1048 {
1049 case DWL_DLGPROC:
1050 return (ULONG)Win32DlgProc;
1051 case DWL_MSGRESULT:
1052 return msgResult;
1053 case DWL_USER:
1054 return userDlgData;
1055 default:
1056 return Win32BaseWindow::GetWindowLongA(index);
1057 }
1058}
1059//******************************************************************************
1060//******************************************************************************
1061BOOL DIALOG_Register()
1062{
1063 WNDCLASSA wndClass;
1064
1065 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
1066 wndClass.style = CS_GLOBALCLASS | CS_SAVEBITS;
1067 wndClass.lpfnWndProc = (WNDPROC)DefDlgProcA;
1068 wndClass.cbClsExtra = 0;
1069 wndClass.cbWndExtra = 0;
1070 wndClass.hCursor = (HCURSOR)IDC_ARROWA;
1071 wndClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
1072 wndClass.lpszClassName = DIALOG_CLASS_NAMEA;
1073
1074 return RegisterClassA(&wndClass);
1075}
1076//******************************************************************************
1077//******************************************************************************
1078BOOL DIALOG_Unregister()
1079{
1080 if (GlobalFindAtomA(DIALOG_CLASS_NAMEA))
1081 return UnregisterClassA(DIALOG_CLASS_NAMEA,(HINSTANCE)NULL);
1082 else return FALSE;
1083}
1084//******************************************************************************
1085//******************************************************************************
1086BOOL Win32Dialog::fInitialized = FALSE;
1087int Win32Dialog::xBaseUnit = 10;
1088int Win32Dialog::yBaseUnit = 20;
Note: See TracBrowser for help on using the repository browser.