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

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

frame bug fixes

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