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

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

EB's unicode changes + seticon + mdi fixes

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