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

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

new mapping functions, fixed 1 pixel and window handle bugs

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