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

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

Dialog fixes + ported Wine apis

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