source: trunk/src/user32/pmwindow.cpp@ 2371

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

OSLibGetMsg bugfix + WM_QUIT translation fix

File size: 17.7 KB
Line 
1/* $Id: pmwindow.cpp,v 1.75 2000-01-08 14:15:07 sandervl Exp $ */
2/*
3 * Win32 Window Managment Code for OS/2
4 *
5 * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
6 * Copyright 1999 Daniela Engert (dani@ngrt.de)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12#define INCL_WIN
13#define INCL_GPI
14#define INCL_DEV /* Device Function definitions */
15#define INCL_GPICONTROL /* GPI control Functions */
16#define INCL_DOSPROCESS
17
18#include <os2wrap.h>
19#include <stdlib.h>
20#include <string.h>
21#include "win32type.h"
22#include <winconst.h>
23#include <wprocess.h>
24#include <misc.h>
25#include <win32wbase.h>
26#include <win32dlg.h>
27#include "win32wdesktop.h"
28#include "pmwindow.h"
29#include "oslibwin.h"
30#include "oslibutil.h"
31#include "oslibgdi.h"
32#include "oslibmsg.h"
33#include "dc.h"
34#include <thread.h>
35#include <wprocess.h>
36#include "caret.h"
37#include "timer.h"
38#include <codepage.h>
39#include <win\options.h>
40
41HMQ hmq = 0; /* Message queue handle */
42HAB hab = 0;
43
44RECTL desktopRectl = {0};
45ULONG ScreenWidth = 0;
46ULONG ScreenHeight = 0;
47ULONG ScreenBitsPerPel = 0;
48BOOL fOS2Look = TRUE;
49
50
51MRESULT EXPENTRY Win32WindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
52
53//******************************************************************************
54//Initialize PM; create hab, message queue and register special Win32 window classes
55//******************************************************************************
56BOOL InitPM()
57{
58 CLASSINFO FrameClassInfo;
59
60 hab = WinInitialize(0);
61 dprintf(("Winitialize returned %x", hab));
62 hmq = WinCreateMsgQueue(hab, 0);
63
64 if(!hab || !hmq)
65 {
66 UINT error;
67 //CB: only fail on real error
68 error = WinGetLastError(hab) & 0xFFFF; //error code
69 if (!hab || (error != PMERR_MSG_QUEUE_ALREADY_EXISTS))
70 {
71 dprintf(("WinInitialize or WinCreateMsgQueue failed %x %x", hab, hmq));
72 dprintf((" Error = %x",error));
73 return(FALSE);
74 }
75 else
76 {
77 if(!hab) {
78 hab = WinQueryAnchorBlock(HWND_DESKTOP);
79 dprintf(("WinQueryAnchorBlock returned %x", hab));
80 }
81 if(!hmq) {
82 hmq = HMQ_CURRENT;
83 }
84 }
85 }
86 SetThreadHAB(hab);
87 dprintf(("InitPM: hmq = %x", hmq));
88 SetThreadMessageQueue(hmq);
89
90 BOOL rc = WinSetCp(hmq, GetDisplayCodepage());
91 dprintf(("InitPM: WinSetCP was %sOK", rc ? "" : "not "));
92
93 if(!WinRegisterClass( /* Register window class */
94 hab, /* Anchor block handle */
95 (PSZ)WIN32_STDCLASS, /* Window class name */
96 (PFNWP)Win32WindowProc, /* Address of window procedure */
97 CS_HITTEST,
98 NROF_WIN32WNDBYTES)) {
99 dprintf(("WinRegisterClass Win32BaseWindow failed"));
100 return(FALSE);
101 }
102 if (!WinQueryClassInfo (hab, WC_FRAME, &FrameClassInfo)) {
103 dprintf (("WinQueryClassInfo WC_FRAME failed"));
104 return (FALSE);
105 }
106 FrameClassInfo.flClassStyle &= ~(CS_PUBLIC | CS_CLIPSIBLINGS);
107 if (!WinRegisterClass (hab,
108 WIN32_INNERFRAME,
109 FrameClassInfo.pfnWindowProc,
110 FrameClassInfo.flClassStyle,
111 FrameClassInfo.cbWindowData)) {
112 dprintf (("WinRegisterClass Win32InnerFrame failed"));
113 return (FALSE);
114 }
115
116 WinQueryWindowRect(HWND_DESKTOP, &desktopRectl);
117 ScreenWidth = desktopRectl.xRight;
118 ScreenHeight = desktopRectl.yTop;
119
120
121 HDC hdc; /* Device-context handle */
122 /* context data structure */
123 DEVOPENSTRUC dop = {NULL, "DISPLAY", NULL, NULL, NULL, NULL,
124 NULL, NULL, NULL};
125
126 /* create memory device context */
127 hdc = DevOpenDC(hab, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&dop, NULLHANDLE);
128 DevQueryCaps(hdc, CAPS_COLOR_BITCOUNT, 1, (PLONG)&ScreenBitsPerPel);
129 DevCloseDC(hdc);
130
131 dprintf(("InitPM: Desktop (%d,%d)", ScreenWidth, ScreenHeight));
132
133 fOS2Look = PROFILE_GetOdinIniInt(ODINCUSTOMIZATION,"OS2Look",1);
134
135 return OSLibInitMsgQueue();
136} /* End of main */
137//******************************************************************************
138//Win32 window message handler
139//******************************************************************************
140MRESULT EXPENTRY Win32WindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
141{
142 POSTMSG_PACKET *postmsg;
143 OSLIBPOINT point, ClientPoint;
144 Win32BaseWindow *win32wnd;
145 THDB *thdb;
146 APIRET rc = 0;
147 MSG winMsg, *pWinMsg;
148
149 //Restore our FS selector
150 SetWin32TIB();
151
152 thdb = GetThreadTHDB();
153 win32wnd = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
154
155 if(!thdb || (msg != WM_CREATE && win32wnd == NULL)) {
156 dprintf(("Invalid win32wnd pointer for window %x msg %x", hwnd, msg));
157 goto RunDefWndProc;
158 }
159
160 if((thdb->msgstate & 1) == 0)
161 {//message that was sent directly to our window proc handler; translate it here
162 QMSG qmsg;
163
164 qmsg.msg = msg;
165 qmsg.hwnd = hwnd;
166 qmsg.mp1 = mp1;
167 qmsg.mp2 = mp2;
168 qmsg.time = WinQueryMsgTime(thdb->hab);
169 WinQueryMsgPos(thdb->hab, &qmsg.ptl);
170 qmsg.reserved = 0;
171
172 if(OS2ToWinMsgTranslate((PVOID)thdb, &qmsg, &winMsg, FALSE, MSG_REMOVE) == FALSE)
173 {//message was not translated
174 memset(&winMsg, 0, sizeof(MSG));
175 }
176 pWinMsg = &winMsg;
177 }
178 else {
179 pWinMsg = &thdb->msg;
180 thdb->msgstate++;
181 }
182
183 if(msg == WIN32APP_POSTMSG) {
184 //probably win32 app user message
185 if((ULONG)mp1 == WIN32MSG_MAGICA) {
186 return (MRESULT)win32wnd->DispatchMsgA(pWinMsg);
187 }
188 else
189 if((ULONG)mp1 == WIN32MSG_MAGICW) {
190 return (MRESULT)win32wnd->DispatchMsgW(pWinMsg);
191 }
192 }
193 switch( msg )
194 {
195 //OS/2 msgs
196 case WM_CREATE:
197 {
198
199 if(thdb->newWindow == 0)
200 goto createfail;
201
202 //Processing is done in after WinCreateWindow returns
203 dprintf(("OS2: WM_CREATE %x", hwnd));
204 win32wnd = (Win32BaseWindow *)thdb->newWindow;
205 thdb->newWindow = 0;
206
207 if(win32wnd->MsgCreate(WinQueryWindow(hwnd, QW_PARENT), hwnd) == FALSE)
208 {
209 RestoreOS2TIB();
210 return (MRESULT)TRUE; //discontinue window creation
211 }
212 createfail:
213 RestoreOS2TIB();
214 return (MRESULT)FALSE;
215 }
216
217 case WM_QUIT:
218 dprintf(("OS2: WM_QUIT %x", hwnd));
219 win32wnd->MsgQuit();
220 break;
221
222 case WM_CLOSE:
223 dprintf(("OS2: WM_CLOSE %x", hwnd));
224 win32wnd->MsgClose();
225 break;
226
227 case WM_DESTROY:
228 dprintf(("OS2: WM_DESTROY %x", hwnd));
229 win32wnd->MsgDestroy();
230 break;
231
232 case WM_ENABLE:
233 dprintf(("OS2: WM_ENABLE %x", hwnd));
234 win32wnd->MsgEnable(SHORT1FROMMP(mp1));
235 break;
236
237 case WM_SHOW:
238 dprintf(("OS2: WM_SHOW %x %d", hwnd, mp1));
239 win32wnd->MsgShow((ULONG)mp1);
240 break;
241
242#if 1
243 case WM_ADJUSTWINDOWPOS:
244 {
245// PSWP pswp = (PSWP)mp1;
246
247// dprintf(("OS2: WM_ADJUSTWINDOWPOS %x %x %x (%d,%d) (%d,%d)", hwnd, pswp->hwnd, pswp->fl, pswp->x, pswp->y, pswp->cx, pswp->cy));
248 goto RunDefWndProc;
249 }
250#else
251 case WM_ADJUSTWINDOWPOS:
252 {
253 PSWP pswp = (PSWP)mp1;
254 SWP swpOld, swpNew;
255 WINDOWPOS wp;
256 ULONG parentHeight = 0;
257 HWND hParent = NULLHANDLE, hFrame = NULLHANDLE, hwndAfter;
258
259 dprintf(("OS2: WM_ADJUSTWINDOWPOS %x %x %x (%d,%d) (%d,%d)", hwnd, pswp->hwnd, pswp->fl, pswp->x, pswp->y, pswp->cx, pswp->cy));
260
261 if ((pswp->fl & (SWP_SIZE | SWP_MOVE | SWP_ZORDER)) == 0) goto RunDefWndProc;;
262
263 //SvL: TODO: Workaround. Why is this happening?
264 // When this flag is set the coordinates are 0, even though SWP_SIZE & SWP_MOVE are set.
265// if ((pswp->fl & SWP_NOADJUST)) goto RunDefWndProc;
266
267 if(!win32wnd->CanReceiveSizeMsgs()) goto RunDefWndProc;;
268
269 WinQueryWindowPos(hwnd, &swpOld);
270
271 if(pswp->fl & (SWP_MOVE | SWP_SIZE)) {
272 if (win32wnd->isChild()) {
273 if(win32wnd->getParent()) {
274 hParent = win32wnd->getParent()->getOS2WindowHandle();
275 }
276 else goto RunDefWndProc;;
277 }
278 }
279 hwndAfter = pswp->hwndInsertBehind;
280 hFrame = win32wnd->getOS2FrameWindowHandle();
281 OSLibMapSWPtoWINDOWPOS(pswp, &wp, &swpOld, hParent, hFrame);
282
283 wp.hwnd = win32wnd->getWindowHandle();
284 if ((pswp->fl & SWP_ZORDER) && (pswp->hwndInsertBehind > HWND_BOTTOM))
285 {
286 Win32BaseWindow *wndAfter = Win32BaseWindow::GetWindowFromOS2Handle(pswp->hwndInsertBehind);
287 if(wndAfter) wp.hwndInsertAfter = wndAfter->getWindowHandle();
288 }
289 if(win32wnd->MsgPosChanging((LPARAM)&wp) == 0)
290 {//app or default window handler changed wp
291 dprintf(("OS2: WM_ADJUSTWINDOWPOS, app changed windowpos struct"));
292 dprintf(("%x (%d,%d), (%d,%d)", pswp->fl, pswp->x, pswp->y, pswp->cx, pswp->cy));
293 OSLibMapWINDOWPOStoSWP(&wp, &swpNew, &swpOld, hParent, hFrame);
294 dprintf(("%x (%d,%d), (%d,%d)", swpNew.fl, swpNew.x, swpNew.y, swpNew.cx, swpNew.cy));
295 swpNew.fl |= SWP_NOADJUST;
296 swpNew.hwndInsertBehind = hwndAfter;
297 swpNew.hwnd = hFrame;
298
299 WinSetMultWindowPos(GetThreadHAB(), &swpNew, 1);
300 return (MRESULT)0;
301 }
302 break;
303 }
304#endif
305
306 case WM_WINDOWPOSCHANGED:
307 {
308 win32wnd->MsgPosChanged((LPARAM)&thdb->wp);
309 goto RunDefWndProc;
310 }
311
312 case WM_ACTIVATE:
313 {
314 HWND hwndActivate = (HWND)mp2;
315 BOOL fMinimized = FALSE;
316
317 dprintf(("OS2: WM_ACTIVATE %x %x", hwnd, hwndActivate));
318 if(WinQueryWindowULong(hwndActivate, OFFSET_WIN32PM_MAGIC) != WIN32PM_MAGIC) {
319 //another (non-win32) application's window
320 //set to NULL (allowed according to win32 SDK) to avoid problems
321 hwndActivate = NULL;
322 }
323 if(WinQueryWindowULong(hwnd, QWL_STYLE) & WS_MINIMIZED)
324 {
325 fMinimized = TRUE;
326 }
327
328 win32wnd->MsgActivate(SHORT1FROMMP(mp1), fMinimized, Win32BaseWindow::OS2ToWin32Handle(hwndActivate));
329 break;
330 }
331
332 case WM_SIZE:
333 {
334 dprintf(("OS2: WM_SIZE (%d,%d) (%d,%d)", SHORT1FROMMP(mp2), SHORT2FROMMP(mp2), SHORT1FROMMP(mp1), SHORT2FROMMP(mp2)));
335 break;
336 }
337
338 case WM_MINMAXFRAME:
339 {
340 dprintf(("OS2: WM_MINMAXFRAME"));
341 break;
342 }
343
344 case WM_OWNERPOSCHANGE:
345 {
346 dprintf(("OS2: WM_OWNERPOSCHANGE"));
347 goto RunDefWndProc;
348 }
349
350 case WM_CALCVALIDRECTS:
351 {
352 dprintf(("OS2: WM_CALCVALIDRECTS"));
353 goto RunDefWndProc;
354 }
355
356 case WM_SETFOCUS:
357 {
358 HWND hwndFocus = (HWND)mp1;
359
360 dprintf(("OS2: WM_SETFOCUS %x %x %d", win32wnd->getWindowHandle(), mp1, mp2));
361 if(WinQueryWindowULong(hwndFocus, OFFSET_WIN32PM_MAGIC) != WIN32PM_MAGIC) {
362 //another (non-win32) application's window
363 //set to NULL (allowed according to win32 SDK) to avoid problems
364 hwndFocus = NULL;
365 }
366 if((ULONG)mp2 == TRUE) {
367 HWND hwndFocusWin32 = Win32BaseWindow::OS2ToWin32Handle(hwndFocus);
368 recreateCaret (hwndFocusWin32);
369 win32wnd->MsgSetFocus(hwndFocusWin32);
370 }
371 else win32wnd->MsgKillFocus(Win32BaseWindow::OS2ToWin32Handle(hwndFocus));
372 break;
373 }
374
375 //**************************************************************************
376 //Mouse messages (OS/2 Window coordinates -> Win32 coordinates relative to screen
377 //**************************************************************************
378 case WM_BUTTON1DOWN:
379 case WM_BUTTON1UP:
380 case WM_BUTTON1DBLCLK:
381 case WM_BUTTON2DOWN:
382 case WM_BUTTON2UP:
383 case WM_BUTTON2DBLCLK:
384 case WM_BUTTON3DOWN:
385 case WM_BUTTON3UP:
386 case WM_BUTTON3DBLCLK:
387 win32wnd->MsgButton(pWinMsg);
388 rc = TRUE;
389 break;
390
391 case WM_BUTTON2MOTIONSTART:
392 case WM_BUTTON2MOTIONEND:
393 case WM_BUTTON2CLICK:
394 case WM_BUTTON1MOTIONSTART:
395 case WM_BUTTON1MOTIONEND:
396 case WM_BUTTON1CLICK:
397 case WM_BUTTON3MOTIONSTART:
398 case WM_BUTTON3MOTIONEND:
399 case WM_BUTTON3CLICK:
400 goto RunDefWndProc;
401
402 case WM_MOUSEMOVE:
403 {
404 //OS/2 Window coordinates -> Win32 Window coordinates
405 win32wnd->MsgMouseMove(pWinMsg);
406 break;
407 }
408
409 case WM_CONTROL:
410 goto RunDefWndProc;
411
412 case WM_COMMAND:
413 dprintf(("OS2: WM_COMMAND %x %x %x", hwnd, mp1, mp2));
414 win32wnd->DispatchMsgA(pWinMsg);
415 break;
416
417 case WM_SYSCOMMAND:
418 win32wnd->DispatchMsgA(pWinMsg);
419 break;
420
421 case WM_CHAR:
422 win32wnd->DispatchMsgA(pWinMsg);
423 break;
424
425 case WM_INITMENU:
426 win32wnd->MsgInitMenu(pWinMsg);
427 break;
428
429 case WM_TIMER:
430 win32wnd->DispatchMsgA(pWinMsg);
431 goto RunDefWndProc;
432
433 case WM_MENUSELECT:
434 case WM_MENUEND:
435 case WM_NEXTMENU:
436 goto RunDefWndProc;
437
438 case WM_SETWINDOWPARAMS:
439 {
440 WNDPARAMS *wndParams = (WNDPARAMS *)mp1;
441
442 dprintf(("OS2: WM_SETWINDOWPARAMS %x", hwnd));
443 if(wndParams->fsStatus & WPM_TEXT) {
444 win32wnd->MsgSetText(wndParams->pszText, wndParams->cchText);
445 }
446 goto RunDefWndProc;
447 }
448
449 case WM_QUERYWINDOWPARAMS:
450 {
451 PWNDPARAMS wndpars = (PWNDPARAMS)mp1;
452 ULONG textlen;
453 PSZ wintext;
454
455 if(wndpars->fsStatus & (WPM_CCHTEXT | WPM_TEXT))
456 {
457 if(wndpars->fsStatus & WPM_CCHTEXT)
458 wndpars->cchText = win32wnd->MsgGetTextLength();
459 if(wndpars->fsStatus & WPM_TEXT)
460 wndpars->pszText = win32wnd->MsgGetText();
461
462 wndpars->fsStatus = 0;
463 wndpars->cbCtlData = 0;
464 wndpars->cbPresParams = 0;
465 RestoreOS2TIB();
466 return (MRESULT)TRUE;
467 }
468 goto RunDefWndProc;
469 }
470
471 case WM_PAINT:
472 win32wnd->DispatchMsgA(pWinMsg);
473 goto RunDefWndProc;
474
475 case WM_HITTEST:
476 {
477 DWORD res;
478
479 // Only send this message if the window is enabled
480 if (!WinIsWindowEnabled(hwnd))
481 res = HT_ERROR;
482 else if (win32wnd->getIgnoreHitTest())
483 res = HT_NORMAL;
484 else
485 {
486 dprintf(("USER32: WM_HITTEST %x (%d,%d)",hwnd,(*(POINTS *)&mp1).x,(*(POINTS *)&mp1).y));
487
488 //CB: WinWindowFromPoint: PM sends WM_HITTEST -> loop -> stack overflow
489 win32wnd->setIgnoreHitTest(TRUE);
490 res = win32wnd->MsgHitTest(pWinMsg);
491 win32wnd->setIgnoreHitTest(FALSE);
492 }
493 RestoreOS2TIB();
494 return (MRESULT)res;
495 }
496
497 case WM_CONTEXTMENU:
498 {
499 win32wnd->DispatchMsgA(pWinMsg);
500
501 RestoreOS2TIB();
502 return (MRESULT)TRUE;
503 }
504
505 case WM_ERASEBACKGROUND:
506 {
507 dprintf(("OS2: WM_ERASEBACKGROUND %x", win32wnd->getWindowHandle()));
508 break;
509 }
510
511 case WM_FOCUSCHANGE:
512 dprintf(("OS2: WM_FOCUSCHANGE %x", win32wnd->getWindowHandle()));
513 goto RunDefWndProc;
514
515 case WM_SYSCOLORCHANGE:
516 case WM_SYSVALUECHANGED:
517 case WM_SETSELECTION:
518 case WM_PPAINT:
519 case WM_PSETFOCUS:
520 case WM_PSYSCOLORCHANGE:
521 case WM_PSIZE:
522 case WM_PACTIVATE:
523 case WM_PCONTROL:
524 case WM_HELP:
525 case WM_APPTERMINATENOTIFY:
526 case WM_PRESPARAMCHANGED:
527 case WM_DRAWITEM:
528 case WM_MEASUREITEM:
529 case WM_CONTROLPOINTER:
530 case WM_QUERYDLGCODE:
531 case WM_SUBSTITUTESTRING:
532 case WM_MATCHMNEMONIC:
533 case WM_SAVEAPPLICATION:
534 case WM_SEMANTICEVENT:
535 default:
536 //dprintf(("OS2: RunDefWndProc msg %x for %x", msg, hwnd));
537 RestoreOS2TIB();
538 return WinDefWindowProc( hwnd, msg, mp1, mp2 );
539 }
540 RestoreOS2TIB();
541 return (MRESULT)rc;
542
543RunDefWndProc:
544// dprintf(("OS2: RunDefWndProc msg %x for %x", msg, hwnd));
545 RestoreOS2TIB();
546
547 return WinDefWindowProc( hwnd, msg, mp1, mp2 );
548} /* End of Win32WindowProc */
549//******************************************************************************
550//******************************************************************************
551MRESULT EXPENTRY Win32SubclassWindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
552{
553 Win32BaseWindow* win32wnd;
554
555 //Restore our FS selector
556 SetWin32TIB();
557
558 win32wnd = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
559
560 if (!win32wnd)
561 {
562 dprintf(("Invalid win32wnd pointer for subclassed window %x!!", hwnd));
563 goto RunDefWndProc;
564 }
565
566 switch (msg)
567 {
568 case WM_WINDOWPOSCHANGED:
569 {
570 PSWP pswp = (PSWP)mp1;
571 SWP swpOld = *(pswp + 1);
572 WINDOWPOS wp;
573 HWND hParent = NULLHANDLE, hFrame = NULLHANDLE;
574
575 dprintf(("OS2Subclass: WM_WINDOWPOSCHANGED %x %x (%d,%d) (%d,%d)", hwnd, pswp->fl, pswp->x, pswp->y, pswp->cx, pswp->cy));
576 if ((pswp->fl & (SWP_SIZE | SWP_MOVE | SWP_ZORDER)) == 0) break;
577
578 hParent = hFrame = WinQueryWindow(hwnd, QW_PARENT);
579
580 OSLibMapSWPtoWINDOWPOS(pswp,&wp, &swpOld,hParent,hFrame);
581
582 win32wnd->setWindowRect(swpOld.x, swpOld.y, swpOld.x + swpOld.cx, swpOld.y + swpOld.cy);
583 win32wnd->setClientRect(swpOld.x, swpOld.y, swpOld.x + swpOld.cx, swpOld.y + swpOld.cy);
584 wp.x = swpOld.x;
585 wp.y = swpOld.y;
586 wp.cx = swpOld.cx;
587 wp.cy = swpOld.cy;
588
589 wp.hwnd = win32wnd->getWindowHandle();
590
591 win32wnd->MsgPosChanged((LPARAM)&wp);
592
593 goto RunOldWndProc;
594 }
595
596 default:
597 goto RunDefHandler;
598 }
599
600RunDefWndProc:
601 RestoreOS2TIB();
602 return WinDefWindowProc(hwnd,msg,mp1,mp2);
603
604RunOldWndProc:
605 RestoreOS2TIB();
606 return ((PFNWP)win32wnd->getOldWndProc())(hwnd,msg,mp1,mp2);
607
608RunDefHandler:
609 RestoreOS2TIB();
610 return Win32WindowProc(hwnd,msg,mp1,mp2);
611}
612
613PVOID SubclassWithDefHandler(HWND hwnd)
614{
615 return WinSubclassWindow(hwnd,Win32SubclassWindowProc);
616}
Note: See TracBrowser for help on using the repository browser.