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

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

Window size + paint fix + dialog fixes

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