source: trunk/src/user32/win32wbase.cpp@ 9143

Last change on this file since 9143 was 9143, checked in by sandervl, 23 years ago

PF: Added support for WS_EX_TOPMOST

File size: 139.3 KB
Line 
1/* $Id: win32wbase.cpp,v 1.337 2002-08-27 09:30:50 sandervl Exp $ */
2/*
3 * Win32 Window Base Class for OS/2
4 *
5 * Copyright 1998-2002 Sander van Leeuwen (sandervl@xs4all.nl)
6 * Copyright 1999 Daniela Engert (dani@ngrt.de)
7 * Copyright 1999-2000 Christoph Bratschi (cbratschi@datacomm.ch)
8 *
9 * Parts based on Wine Windows code (windows\win.c)
10 * Corel version: corel20000212
11 *
12 * Copyright 1993, 1994, 1996 Alexandre Julliard
13 * 1995 Alex Korobka
14 *
15 * TODO: Not thread/process safe
16 *
17 * NOTE: To access a window object, you must call GetWindowFromOS2Handle or
18 * GetWindowFromHandle. Both these methods increase the reference count
19 * of the object. When you're done with the object, you MUST call
20 * the release method!
21 * This mechanism prevents premature destruction of objects when there
22 * are still clients using it.
23 *
24 * NOTE: Client rectangle always relative to frame window
25 * Window rectangle in parent coordinates (relative to parent's client window)
26 * (screen coord. if no parent)
27 *
28 * NOTE: Status of window:
29 * Before a window has processed WM_NCCREATE:
30 * - GetTopWindow can't return that window handle
31 * - GetWindow(parent, GW_CHILD) can't return that window handle
32 * - IsChild works
33 * TODO: Does this affect more functions?? (other GetWindow ops)
34 * (verified in NT4, SP6)
35 *
36 * Project Odin Software License can be found in LICENSE.TXT
37 *
38 */
39#include <os2win.h>
40#include <win.h>
41#include <stdlib.h>
42#include <string.h>
43#include <stdarg.h>
44#include <assert.h>
45#include <misc.h>
46#include <heapstring.h>
47#include <winuser32.h>
48#include <custombuild.h>
49#include "win32wbase.h"
50#include "wndmsg.h"
51#include "oslibwin.h"
52#include "oslibmsg.h"
53#include "oslibutil.h"
54#include "oslibgdi.h"
55#include "oslibres.h"
56#include "oslibdos.h"
57#include "syscolor.h"
58#include "win32wndhandle.h"
59#include "dc.h"
60#include "win32wdesktop.h"
61#include "pmwindow.h"
62#include "controls.h"
63#include <wprocess.h>
64#include <win\hook.h>
65#include <menu.h>
66#define INCL_TIMERWIN32
67#include "timer.h"
68#include "user32api.h"
69
70#define DBG_LOCALLOG DBG_win32wbase
71#include "dbglocal.h"
72
73/* bits in the dwKeyData */
74#define KEYDATA_ALT 0x2000
75#define KEYDATA_PREVSTATE 0x4000
76
77void PrintWindowStyle(DWORD dwStyle, DWORD dwExStyle);
78
79static fDestroyAll = FALSE;
80//For quick lookup of current process id
81static ULONG currentProcessId = -1;
82static int iF10Key = 0;
83static int iMenuSysKey = 0;
84
85//******************************************************************************
86//******************************************************************************
87Win32BaseWindow::Win32BaseWindow()
88 : GenericObject(&windows, &critsect), ChildWindow(&critsect)
89{
90 Init();
91}
92//******************************************************************************
93//******************************************************************************
94Win32BaseWindow::Win32BaseWindow(HWND hwndOS2, ATOM classAtom)
95 : GenericObject(&windows, &critsect), ChildWindow(&critsect)
96{
97 Init();
98 OS2Hwnd = OS2HwndFrame = hwndOS2;
99
100 /* Find the window class */
101 windowClass = Win32WndClass::FindClass(NULL, (LPSTR)classAtom);
102 if (!windowClass)
103 {
104 char buffer[32];
105 GlobalGetAtomNameA( classAtom, buffer, sizeof(buffer) );
106 dprintf(("Bad class '%s'", buffer ));
107 DebugInt3();
108 }
109
110 //Allocate window words
111 nrUserWindowBytes = windowClass->getExtraWndBytes();
112 if(nrUserWindowBytes) {
113 userWindowBytes = (char *)_smalloc(nrUserWindowBytes);
114 memset(userWindowBytes, 0, nrUserWindowBytes);
115 }
116
117 WINPROC_SetProc((HWINDOWPROC *)&win32wndproc, windowClass->getWindowProc(), WINPROC_GetProcType(windowClass->getWindowProc()), WIN_PROC_WINDOW);
118 hInstance = NULL;
119 dwStyle = WS_VISIBLE;
120 dwOldStyle = dwStyle;
121 dwExStyle = 0;
122
123 //We pretend this window has no parent and won't change size
124 //(dangerous assumption!!)
125 OSLibWinQueryWindowClientRect(OS2Hwnd, &rectClient);
126 OSLibQueryWindowRectAbsolute (OS2Hwnd, &rectWindow);
127
128 fFakeWindow = TRUE;
129}
130//******************************************************************************
131//******************************************************************************
132Win32BaseWindow::Win32BaseWindow(CREATESTRUCTA *lpCreateStructA, ATOM classAtom, BOOL isUnicode)
133 : GenericObject(&windows, &critsect), ChildWindow(&critsect)
134{
135 Init();
136 this->isUnicode = isUnicode;
137 // call member function
138 CreateWindowExA(lpCreateStructA, classAtom);
139}
140//******************************************************************************
141//******************************************************************************
142void Win32BaseWindow::Init()
143{
144 isUnicode = FALSE;
145 fFirstShow = TRUE;
146 fIsDialog = FALSE;
147 fIsModalDialogOwner = FALSE;
148 OS2HwndModalDialog = 0;
149 fParentChange = FALSE;
150 fDestroyWindowCalled = FALSE;
151 fChildDestructionInProgress = FALSE;
152 fTaskList = FALSE;
153 fParentDC = FALSE;
154 fComingToTop = FALSE;
155 fMinMaxChange = FALSE;
156 fVisibleRegionChanged = FALSE;
157 fEraseBkgndFlag = TRUE;
158 fFakeWindow = FALSE;
159 fIsDragDropActive= FALSE;
160
161 state = STATE_INIT;
162 windowNameA = NULL;
163 windowNameW = NULL;
164 windowNameLength = 0;
165
166 userWindowBytes = NULL;;
167 nrUserWindowBytes= 0;
168
169 OS2Hwnd = 0;
170 OS2HwndFrame = 0;
171 hSysMenu = 0;
172 Win32Hwnd = 0;
173
174 // allocate a Win32 HWND, return it in Win32Hwnd and associate object
175 // pointer with it
176 if(HwAllocateWindowHandle(&Win32Hwnd, (ULONG)this) == FALSE)
177 {
178 dprintf(("Win32BaseWindow::Init HwAllocateWindowHandle failed!!"));
179 DebugInt3();
180 }
181
182 posx = posy = 0;
183 width = height = 0;
184
185 dwExStyle = 0;
186 dwStyle = 0;
187 dwOldStyle = 0;
188 win32wndproc = 0;
189 hInstance = 0;
190 dwIDMenu = 0; //0xFFFFFFFF; //default -1
191 userData = 0;
192 contextHelpId = 0;
193 hotkey = 0;
194
195 hwndLinkAfter = HWND_BOTTOM;
196 flags = 0;
197 lastHitTestVal = HTCLIENT;
198 owner = NULL;
199 windowClass = 0;
200
201 hIcon = 0;
202 hIconSm = 0;
203
204 horzScrollInfo = NULL;
205 vertScrollInfo = NULL;
206
207 propertyList = NULL;
208
209 cbExtra = 0;
210 pExtra = NULL;
211
212 ownDC = 0;
213 hWindowRegion = 0;
214 hClipRegion = 0;
215
216 hTaskList = 0;
217
218 if(currentProcessId == -1)
219 {
220 currentProcessId = GetCurrentProcessId();
221 }
222 dwThreadId = GetCurrentThreadId();
223 dwProcessId = currentProcessId;
224
225 memset(&windowpos, 0, sizeof(windowpos));
226 //min and max position are initially -1 (verified in NT4, SP6)
227 windowpos.ptMinPosition.x = -1;
228 windowpos.ptMinPosition.y = -1;
229 windowpos.ptMaxPosition.x = -1;
230 windowpos.ptMaxPosition.y = -1;
231
232 lpVisRgnNotifyProc = NULL;
233 dwVisRgnNotifyParam = NULL;
234}
235//******************************************************************************
236//todo get rid of resources (menu, icon etc)
237//******************************************************************************
238Win32BaseWindow::~Win32BaseWindow()
239{
240 if(getRefCount() < 0) {
241 DebugInt3();
242 }
243
244 if(hTaskList) {
245 OSLibWinRemoveFromTasklist(hTaskList);
246 }
247
248 OSLibWinSetVisibleRegionNotify(OS2Hwnd, FALSE);
249 OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, 0);
250 OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32PM_MAGIC, 0);
251
252 if(fDestroyAll) {
253 dprintf(("Destroying window %x %s", getWindowHandle(), windowNameA));
254 setParent(NULL); //or else we'll crash in the dtor of the ChildWindow class
255 }
256 else
257 if(getParent() && getParent()->getFirstChild() == this && getNextChild() == NULL)
258 {
259 //if we're the last child that's being destroyed and our
260 //parent window was also destroyed, then we
261 if(getParent()->IsWindowDestroyed())
262 {
263 Win32BaseWindow *wndparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
264 RELEASE_WNDOBJ(wndparent);
265 setParent(NULL); //or else we'll crash in the dtor of the ChildWindow class
266 }
267 }
268 else
269 {
270 Win32BaseWindow *wndparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
271 if(wndparent && !fDestroyAll) {
272 RELEASE_WNDOBJ(wndparent);
273 }
274 }
275 if(owner && !fDestroyAll) {
276 RELEASE_WNDOBJ(owner);
277 }
278
279 /* Decrement class window counter */
280 if(windowClass) {
281 RELEASE_CLASSOBJ(windowClass);
282 }
283
284 if(isOwnDC())
285 releaseOwnDC(ownDC);
286
287 if(Win32Hwnd)
288 HwFreeWindowHandle(Win32Hwnd);
289
290 if(userWindowBytes)
291 free(userWindowBytes);
292
293 if(windowNameA) {
294 free(windowNameA);
295 windowNameA = NULL;
296 }
297 if(windowNameW) {
298 free(windowNameW);
299 windowNameW = NULL;
300 }
301 if(vertScrollInfo) {
302 free(vertScrollInfo);
303 vertScrollInfo = NULL;
304 }
305 if(horzScrollInfo) {
306 free(horzScrollInfo);
307 horzScrollInfo = NULL;
308 }
309 if(propertyList) {
310 removeWindowProps();
311 }
312}
313//******************************************************************************
314//******************************************************************************
315void Win32BaseWindow::DestroyAll()
316{
317 fDestroyAll = TRUE;
318 GenericObject::DestroyAll(windows);
319}
320//******************************************************************************
321//******************************************************************************
322BOOL Win32BaseWindow::isChild()
323{
324 return ((dwStyle & WS_CHILD) != 0);
325}
326//******************************************************************************
327//******************************************************************************
328BOOL Win32BaseWindow::IsWindowUnicode()
329{
330 dprintf2(("IsWindowUnicode %x %d", getWindowHandle(), WINPROC_GetProcType(getWindowProc()) == WIN_PROC_32W));
331 return (WINPROC_GetProcType(getWindowProc()) == WIN_PROC_32W);
332}
333//******************************************************************************
334//******************************************************************************
335BOOL Win32BaseWindow::CreateWindowExA(CREATESTRUCTA *cs, ATOM classAtom)
336{
337 char buffer[256];
338
339#ifdef DEBUG
340 PrintWindowStyle(cs->style, cs->dwExStyle);
341#endif
342
343 //If window has no owner/parent window, then it will be added to the tasklist
344 //(depending on visibility state)
345 if (!cs->hwndParent) fTaskList = TRUE;
346
347 sw = SW_SHOW;
348 SetLastError(0);
349
350 /* Find the parent window */
351 if (cs->hwndParent)
352 {
353 Win32BaseWindow *window = GetWindowFromHandle(cs->hwndParent);
354 if(!window) {
355 dprintf(("Bad parent %04x\n", cs->hwndParent ));
356 SetLastError(ERROR_INVALID_PARAMETER);
357 return FALSE;
358 }
359 /* Make sure parent is valid */
360 if (!window->IsWindow() )
361 {
362 RELEASE_WNDOBJ(window);
363 dprintf(("Bad parent %04x\n", cs->hwndParent ));
364 SetLastError(ERROR_INVALID_PARAMETER);
365 return FALSE;
366 }
367 if (window->getExStyle() & WS_EX_TOPMOST)
368 cs->dwExStyle |= WS_EX_TOPMOST;
369
370 RELEASE_WNDOBJ(window);
371 /* Windows does this for overlapped windows
372 * (I don't know about other styles.) */
373 if (cs->hwndParent == GetDesktopWindow() && (!(cs->style & WS_CHILD) || (cs->style & WS_POPUP)))
374 {
375 cs->hwndParent = 0;
376 }
377 }
378 else
379 if ((cs->style & WS_CHILD) && !(cs->style & WS_POPUP)) {
380 dprintf(("No parent for child window" ));
381 SetLastError(ERROR_INVALID_PARAMETER);
382 return FALSE; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
383 }
384
385 /* Find the window class */
386 windowClass = Win32WndClass::FindClass(cs->hInstance, (LPSTR)classAtom);
387 if (!windowClass)
388 {
389 GlobalGetAtomNameA( classAtom, buffer, sizeof(buffer) );
390 dprintf(("Bad class '%s'", buffer ));
391 SetLastError(ERROR_INVALID_PARAMETER);
392 return 0;
393 }
394
395#ifdef DEBUG
396 if(HIWORD(cs->lpszClass))
397 {
398 if(isUnicode) dprintf(("Window class %ls", cs->lpszClass));
399 else dprintf(("Window class %s", cs->lpszClass));
400 }
401 else dprintf(("Window class %x", cs->lpszClass));
402#endif
403
404 /* Fix the lpszClass field: from existing programs, it seems ok to call a CreateWindowXXX
405 * with an atom as the class name, put some programs expect to have a *REAL* string in
406 * lpszClass when the CREATESTRUCT is sent with WM_CREATE
407 */
408 if (!HIWORD(cs->lpszClass) ) {
409 if (isUnicode) {
410 GlobalGetAtomNameW( classAtom, (LPWSTR)buffer, sizeof(buffer) );
411 }
412 else {
413 GlobalGetAtomNameA( classAtom, buffer, sizeof(buffer) );
414 }
415 cs->lpszClass = buffer;
416 }
417
418 /* Fix the coordinates */
419 fXDefault = FALSE;
420 fCXDefault = FALSE;
421 if ((cs->x == CW_USEDEFAULT) || (cs->x == CW_USEDEFAULT16))
422 {
423 /* Never believe Microsoft's documentation... CreateWindowEx doc says
424 * that if an overlapped window is created with WS_VISIBLE style bit
425 * set and the x parameter is set to CW_USEDEFAULT, the system ignores
426 * the y parameter. However, looking at NT reveals that
427 *
428 * 1) not only if checks for CW_USEDEFAULT but also for CW_USEDEFAULT16
429 * 2) it does not ignore the y parameter as the docs claim; instead, it
430 * uses it as second parameter to ShowWindow() unless y is either
431 * CW_USEDEFAULT or CW_USEDEFAULT16.
432 *
433 * The fact that we didn't do 2) caused bogus windows pop up when wine
434 * was running apps that were using this obscure feature. Example -
435 * calc.exe that comes with Win98 (only Win98, it's different from
436 * the one that comes with Win95 and NT)
437 */
438 if ((cs->y != CW_USEDEFAULT) && (cs->y != CW_USEDEFAULT16)) sw = cs->y;
439
440 /* We have saved cs->y, now we can trash it */
441 cs->x = 0;
442 cs->y = 0;
443 fXDefault = TRUE;
444 }
445 if ((cs->cx == CW_USEDEFAULT) || (cs->cx == CW_USEDEFAULT16))
446 {
447 cs->cx = 600; /* FIXME */
448 cs->cy = 400;
449 fCXDefault = TRUE;
450 }
451 if (cs->style & (WS_POPUP | WS_CHILD))
452 {
453 fXDefault = FALSE;
454 if (fCXDefault)
455 {
456 fCXDefault = FALSE;
457 cs->cx = cs->cy = 0;
458 }
459 }
460 if (fXDefault && !fCXDefault) fXDefault = FALSE; //CB: only x positioning doesn't work (calc.exe,cdrlabel.exe)
461
462
463 /* Correct the window style - stage 1
464 *
465 * These are patches that appear to affect both the style loaded into the
466 * WIN structure and passed in the CreateStruct to the WM_CREATE etc.
467 *
468 * WS_EX_WINDOWEDGE appears to be enforced based on the other styles, so
469 * why does the user get to set it?
470 */
471
472 /* This has been tested for WS_CHILD | WS_VISIBLE. It has not been
473 * tested for WS_POPUP
474 */
475 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
476 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
477 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
478 cs->dwExStyle |= WS_EX_WINDOWEDGE;
479 else
480 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
481
482 //Allocate window words
483 nrUserWindowBytes = windowClass->getExtraWndBytes();
484 if(nrUserWindowBytes) {
485 userWindowBytes = (char *)_smalloc(nrUserWindowBytes);
486 memset(userWindowBytes, 0, nrUserWindowBytes);
487 }
488
489 // check if it's the standard child window case
490 if ((cs->style & WS_CHILD) && cs->hwndParent)
491 {
492 SetParent(cs->hwndParent);
493 owner = NULL;
494 //SvL: Shell positioning shouldn't be done for child windows! (breaks Notes)
495 fXDefault = fCXDefault = FALSE;
496 }
497 else
498 {
499 // either no child window or a popup window
500
501 SetParent(0);
502 if (!cs->hwndParent || (cs->hwndParent == windowDesktop->getWindowHandle())) {
503 owner = NULL;
504 }
505 else
506 {
507 // we're a popup window
508
509 Win32BaseWindow *wndparent = GetWindowFromHandle(cs->hwndParent);
510 if(wndparent) {
511 owner = GetWindowFromHandle(wndparent->GetTopParent());
512 RELEASE_WNDOBJ(wndparent);
513 }
514 else owner = NULL;
515
516 if(owner == NULL)
517 {
518 dprintf(("HwGetWindowHandleData couldn't find owner window %x!!!", cs->hwndParent));
519 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
520 return FALSE;
521 }
522 }
523 }
524
525 WINPROC_SetProc((HWINDOWPROC *)&win32wndproc, windowClass->getWindowProc(), WINPROC_GetProcType(windowClass->getWindowProc()), WIN_PROC_WINDOW);
526 hInstance = cs->hInstance;
527 dwStyle = cs->style & ~WS_VISIBLE;
528 dwOldStyle = dwStyle;
529 dwExStyle = cs->dwExStyle;
530
531 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
532
533 /* Correct the window style phase 2 */
534 if (!(cs->style & WS_CHILD))
535 {
536 dwStyle |= WS_CLIPSIBLINGS;
537 if (!(cs->style & WS_POPUP))
538 {
539 dwStyle |= WS_CAPTION;
540 flags |= WIN_NEED_SIZE;
541 }
542 }
543 if (cs->dwExStyle & WS_EX_DLGMODALFRAME) dwStyle &= ~WS_THICKFRAME;
544
545 //WinZip 8.0 crashes when a dialog created after opening a zipfile receives
546 //the WM_SIZE message (before WM_INITDIALOG)
547 //Opera doesn't like this either.
548 if(IsDialog()) {
549 flags |= WIN_NEED_SIZE;
550 }
551
552 //copy pointer of CREATESTRUCT for usage in MsgCreate method
553 tmpcs = cs;
554
555 //Store our window object pointer in thread local memory, so PMWINDOW.CPP can retrieve it
556 TEB *teb = GetThreadTEB();
557 if(teb == NULL) {
558 dprintf(("Window creation failed - teb == NULL")); //this is VERY bad
559 ExitProcess(666);
560 return FALSE;
561 }
562 teb->o.odin.newWindow = (ULONG)this;
563
564 DWORD dwOSWinStyle, dwOSFrameStyle;
565 OSLibWinConvertStyle(dwStyle,dwExStyle,&dwOSWinStyle, &dwOSFrameStyle);
566
567 // create PM windows - frame and client window
568 HWND hwndOS2Frame = (getParent()) ? getParent()->getOS2WindowHandle() : OSLIB_HWND_DESKTOP;
569 OS2Hwnd = OSLibWinCreateWindow(hwndOS2Frame,
570 dwOSWinStyle,
571 dwOSFrameStyle,
572 (char *)windowNameA,
573 (owner) ? owner->getOS2WindowHandle() : hwndOS2Frame,
574 (hwndLinkAfter == HWND_BOTTOM) ? TRUE : FALSE,
575 0,
576 fTaskList,
577 fXDefault | fCXDefault,
578 windowClass->getStyle(),
579 &OS2HwndFrame);
580 if(OS2Hwnd == 0) {
581 dprintf(("Window creation failed!! OS LastError %0x", OSLibWinGetLastError()));
582 SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
583 return FALSE;
584 }
585 OSLibWinSetVisibleRegionNotify(OS2Hwnd, TRUE);
586 state = STATE_CREATED;
587 SetLastError(0);
588 return TRUE;
589}
590//******************************************************************************
591//******************************************************************************
592BOOL Win32BaseWindow::MsgCreate(HWND hwndOS2)
593{
594 CREATESTRUCTA *cs = tmpcs; //pointer to CREATESTRUCT used in CreateWindowExA method
595 POINT maxSize, maxPos, minTrack, maxTrack;
596 HWND hwnd = getWindowHandle();
597 LRESULT (* CALLBACK localSend32)(HWND, UINT, WPARAM, LPARAM);
598
599 OS2Hwnd = hwndOS2;
600
601 if(OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, getWindowHandle()) == FALSE) {
602 dprintf(("WM_CREATE: WinSetWindowULong %X failed!!", OS2Hwnd));
603 SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
604 return FALSE;
605 }
606 if(OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32PM_MAGIC, WIN32PM_MAGIC) == FALSE) {
607 dprintf(("WM_CREATE: WinSetWindowULong2 %X failed!!", OS2Hwnd));
608 SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
609 return FALSE;
610 }
611 if(OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32FLAGS, 0) == FALSE) {
612 dprintf(("WM_CREATE: WinSetWindowULong2 %X failed!!", OS2Hwnd));
613 SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
614 return FALSE;
615 }
616
617 if (HOOK_IsHooked( WH_CBT ))
618 {
619 CBT_CREATEWNDA cbtc;
620 LRESULT ret;
621
622 cbtc.lpcs = cs;
623 cbtc.hwndInsertAfter = hwndLinkAfter;
624 ret = (isUnicode) ? HOOK_CallHooksW(WH_CBT, HCBT_CREATEWND, getWindowHandle(), (LPARAM)&cbtc)
625 : HOOK_CallHooksA(WH_CBT, HCBT_CREATEWND, getWindowHandle(), (LPARAM)&cbtc);
626 if(ret)
627 {
628 dprintf(("CBT-hook returned non-0 !!"));
629 SetLastError(ERROR_CAN_NOT_COMPLETE); //todo: wrong error
630 return FALSE;
631 }
632 //todo: if hook changes parent, we need to do so too!!!!!!!!!!
633 }
634
635 if (cs->style & WS_HSCROLL)
636 {
637 horzScrollInfo = (SCROLLBAR_INFO*)malloc(sizeof(SCROLLBAR_INFO));
638 horzScrollInfo->MinVal = horzScrollInfo->CurVal = horzScrollInfo->Page = 0;
639 horzScrollInfo->MaxVal = 100;
640 horzScrollInfo->flags = ESB_ENABLE_BOTH;
641 }
642
643 if (cs->style & WS_VSCROLL)
644 {
645 vertScrollInfo = (SCROLLBAR_INFO*)malloc(sizeof(SCROLLBAR_INFO));
646 vertScrollInfo->MinVal = vertScrollInfo->CurVal = vertScrollInfo->Page = 0;
647 vertScrollInfo->MaxVal = 100;
648 vertScrollInfo->flags = ESB_ENABLE_BOTH;
649 }
650
651 // initially allocate the window name fields
652 if(HIWORD(cs->lpszName))
653 {
654 if (!isUnicode)
655 {
656 windowNameLength = strlen(cs->lpszName);
657 windowNameA = (LPSTR)_smalloc(windowNameLength+1);
658 memcpy(windowNameA,cs->lpszName,windowNameLength+1);
659 windowNameW = (LPWSTR)_smalloc((windowNameLength+1)*sizeof(WCHAR));
660 lstrcpynAtoW(windowNameW,windowNameA,windowNameLength+1);
661 windowNameA[windowNameLength] = 0;
662 windowNameW[windowNameLength] = 0;
663 }
664 else
665 {
666 // Wide
667 windowNameLength = lstrlenW((LPWSTR)cs->lpszName);
668 windowNameW = (LPWSTR)_smalloc( (windowNameLength+1)*sizeof(WCHAR) );
669 memcpy(windowNameW,(LPWSTR)cs->lpszName, (windowNameLength+1)*sizeof(WCHAR) );
670
671 // windowNameW[lstrlenW((LPWSTR)cs->lpszName)] = 0; // need ?
672
673 // Ascii
674 windowNameA = (LPSTR)_smalloc(windowNameLength+1);
675 WideCharToMultiByte(CP_ACP,
676 0,
677 windowNameW,
678 windowNameLength,
679 windowNameA,
680 windowNameLength + 1,
681 0,
682 NULL);
683 windowNameA[windowNameLength] = 0;
684 }
685
686 if(fOS2Look) {
687 OSLibWinSetTitleBarText(OS2HwndFrame, windowNameA);
688 }
689 }
690
691//SvL: This completely messes up MS Word 97 (no button bar, no menu)
692#if 0
693 //adjust CW_USEDEFAULT position
694 if (fXDefault | fCXDefault)
695 {
696 RECT rect;
697
698 //SvL: Returns invalid rectangle (not the expected shell default size)
699 OSLibWinQueryWindowRect(OS2Hwnd,&rect,RELATIVE_TO_SCREEN);
700 if (getParent()) mapWin32Rect(OSLIB_HWND_DESKTOP,getParent()->getOS2WindowHandle(),&rect);
701 if (fXDefault)
702 {
703 cs->x = rect.left;
704 cs->y = rect.top;
705 if (!fCXDefault)
706 {
707 //CB: todo: adjust pos to screen rect
708 }
709 }
710 if (fCXDefault)
711 {
712 cs->cx = rect.right-rect.left;
713 cs->cy = rect.bottom-rect.top;
714 }
715 }
716#endif
717
718 fakeWinBase.hwndThis = OS2Hwnd;
719 fakeWinBase.pWindowClass = windowClass;
720
721 //Set icon from window or class
722 if (hIcon)
723 OSLibWinSetIcon(OS2HwndFrame,hIcon);
724 else
725 if (windowClass->getIcon())
726 OSLibWinSetIcon(OS2HwndFrame,windowClass->getIcon());
727
728 /* Get class or window DC if needed */
729 if(windowClass->getStyle() & CS_OWNDC) {
730 dprintf(("Class with CS_OWNDC style"));
731 ownDC = GetDCEx(getWindowHandle(), NULL, DCX_USESTYLE);
732 }
733 else
734 if (windowClass->getStyle() & CS_PARENTDC) {
735 fParentDC = TRUE;
736 ownDC = 0;
737 }
738 else
739 if (windowClass->getStyle() & CS_CLASSDC) {
740 dprintf(("WARNING: Class with CS_CLASSDC style!"));
741 //not a good solution, but it's a bit difficult to share a single
742 //DC among different windows... DevOpenDC apparently can't be used
743 //for window DCs and WinOpenWindowDC must be associated with a window
744 ownDC = GetDCEx(getWindowHandle(), NULL, DCX_USESTYLE);
745 }
746 /* Set the window menu */
747 if ((dwStyle & (WS_CAPTION | WS_CHILD)) == WS_CAPTION )
748 {
749 if (cs->hMenu) {
750 ::SetMenu(getWindowHandle(), cs->hMenu);
751 }
752 else {
753 if (windowClass->getMenuNameA()) {
754 cs->hMenu = LoadMenuA(windowClass->getInstance(),windowClass->getMenuNameA());
755#if 0 //CB: hack for treeview test cases bug
756if (!cs->hMenu) cs->hMenu = LoadMenuA(windowClass->getInstance(),"MYAPP");
757#endif
758 if (cs->hMenu) ::SetMenu(getWindowHandle(), cs->hMenu );
759 }
760 }
761 }
762 else
763 {
764 setWindowId((DWORD)cs->hMenu);
765 }
766 hSysMenu = (dwStyle & WS_SYSMENU) ? MENU_GetSysMenu(Win32Hwnd,0):0;
767
768 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
769 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
770 {
771 GetMinMaxInfo(&maxSize, &maxPos, &minTrack, &maxTrack);
772 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
773 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
774 if (cs->cx < minTrack.x) cs->cx = minTrack.x;
775 if (cs->cy < minTrack.y) cs->cy = minTrack.y;
776 }
777
778 if(cs->style & WS_CHILD)
779 {
780 if(cs->cx < 0) cs->cx = 0;
781 if(cs->cy < 0) cs->cy = 0;
782 }
783 else
784 {
785 if (cs->cx <= 0) cs->cx = 1;
786 if (cs->cy <= 0) cs->cy = 1;
787 }
788
789 //set client & window rectangles from CreateWindowEx CREATESTRUCT
790 rectWindow.left = cs->x;
791 rectWindow.right = cs->x+cs->cx;
792 rectWindow.top = cs->y;
793 rectWindow.bottom = cs->y+cs->cy;
794 rectClient = rectWindow;
795 OffsetRect(&rectClient, -rectClient.left, -rectClient.top);
796
797 /* Send the WM_CREATE message
798 * Perhaps we shouldn't allow width/height changes as well.
799 * See p327 in "Internals".
800 */
801 maxPos.x = rectWindow.left; maxPos.y = rectWindow.top;
802
803 if(fTaskList) {
804 hTaskList = OSLibWinAddToTaskList(OS2HwndFrame, windowNameA, (cs->style & WS_VISIBLE) ? 1 : 0);
805 }
806
807 localSend32 = (isUnicode) ? ::SendMessageW : ::SendMessageA;
808
809 state = STATE_PRE_WMNCCREATE;
810 if(localSend32(getWindowHandle(), WM_NCCREATE,0,(LPARAM)cs))
811 {
812 RECT tmpRect;
813
814 //CB: recheck flags
815 if (cs->style & (WS_POPUP | WS_CHILD))
816 {
817 fXDefault = FALSE;
818 if (fCXDefault)
819 {
820 fCXDefault = FALSE;
821 cs->cx = cs->cy = 0;
822 rectWindow.right = rectWindow.left;
823 rectWindow.bottom = rectWindow.top;
824 }
825 }
826 tmpRect = rectWindow;
827 state = STATE_POST_WMNCCREATE;
828
829 //set the window size and update the client
830 SetWindowPos(hwndLinkAfter, tmpRect.left, tmpRect.top, tmpRect.right-tmpRect.left, tmpRect.bottom-tmpRect.top,SWP_NOACTIVATE | SWP_NOREDRAW | SWP_FRAMECHANGED);
831
832 state = STATE_PRE_WMCREATE;
833 if (cs->style & WS_VISIBLE) dwStyle |= WS_VISIBLE; //program could change position in WM_CREATE
834 if( (localSend32(getWindowHandle(), WM_CREATE, 0, (LPARAM)cs )) != -1 )
835 {
836 state = STATE_POST_WMCREATE;
837
838 if(!(flags & WIN_NEED_SIZE))
839 {
840 SendMessageA(getWindowHandle(), WM_SIZE, SIZE_RESTORED,
841 MAKELONG(rectClient.right-rectClient.left,
842 rectClient.bottom-rectClient.top));
843
844 if(!::IsWindow(hwnd))
845 {
846 dprintf(("Createwindow: WM_SIZE destroyed window"));
847 goto end;
848 }
849 SendMessageA(getWindowHandle(), WM_MOVE,0,MAKELONG(rectClient.left,rectClient.top));
850 if(!::IsWindow(hwnd))
851 {
852 dprintf(("Createwindow: WM_MOVE destroyed window"));
853 goto end;
854 }
855 }
856 if (getStyle() & (WS_MINIMIZE | WS_MAXIMIZE))
857 {
858 RECT newPos;
859 UINT swFlag = (getStyle() & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
860 setStyle(getStyle() & ~(WS_MAXIMIZE | WS_MINIMIZE));
861 MinMaximize(swFlag, &newPos);
862 swFlag = ((getStyle() & WS_CHILD) || GetActiveWindow()) ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
863 : SWP_NOZORDER | SWP_FRAMECHANGED;
864 SetWindowPos(0, newPos.left, newPos.top, newPos.right, newPos.bottom, swFlag);
865 if(!::IsWindow(hwnd))
866 {
867 dprintf(("Createwindow: min/max destroyed window"));
868 goto end;
869 }
870 }
871
872 if( (getStyle() & WS_CHILD) && !(getExStyle() & WS_EX_NOPARENTNOTIFY) )
873 {
874 /* Notify the parent window only */
875 if(getParent() && getParent()->IsWindowDestroyed() == FALSE)
876 {
877 SendMessageA(getParent()->getWindowHandle(), WM_PARENTNOTIFY, MAKEWPARAM(WM_CREATE, getWindowId()), (LPARAM)getWindowHandle());
878 }
879 if(!::IsWindow(hwnd))
880 {
881 dprintf(("Createwindow: WM_PARENTNOTIFY destroyed window"));
882 goto end;
883 }
884 }
885
886 if(cs->style & WS_VISIBLE) {
887 dwStyle &= ~WS_VISIBLE;
888 ShowWindow(sw);
889 }
890
891 /* Call WH_SHELL hook */
892 if (!(getStyle() & WS_CHILD) && !owner)
893 HOOK_CallHooksA(WH_SHELL, HSHELL_WINDOWCREATED, getWindowHandle(), 0);
894
895 //Call custom Odin hook for window creation (for all windows)
896 HOOK_CallOdinHookA(HODIN_WINDOWCREATED, hwnd, 0);
897
898 SetLastError(0);
899 return TRUE;
900 }
901 }
902 dprintf(("Window creation FAILED (NCCREATE cancelled creation)"));
903 SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
904end:
905 return FALSE;
906}
907//******************************************************************************
908//******************************************************************************
909ULONG Win32BaseWindow::MsgQuit()
910{
911 return SendMessageA(getWindowHandle(), WM_QUIT, 0, 0);
912}
913//******************************************************************************
914//******************************************************************************
915ULONG Win32BaseWindow::MsgClose()
916{
917 return SendMessageA(getWindowHandle(), WM_CLOSE,0,0);
918}
919//******************************************************************************
920//******************************************************************************
921ULONG Win32BaseWindow::MsgDestroy()
922{
923 ULONG rc;
924 Win32BaseWindow *child;
925 HWND hwnd = getWindowHandle();
926
927 state = STATE_DESTROYED;
928
929 if(fDestroyWindowCalled == FALSE)
930 {//this window was destroyed because DestroyWindow was called for its parent
931 //so: send a WM_PARENTNOTIFY now as that hasn't happened yet
932 if((getStyle() & WS_CHILD) && !(getExStyle() & WS_EX_NOPARENTNOTIFY))
933 {
934 if(getParent() && getParent()->IsWindowDestroyed() == FALSE)
935 {
936 /* Notify the parent window only */
937 SendMessageA(getParent()->getWindowHandle(), WM_PARENTNOTIFY, MAKEWPARAM(WM_DESTROY, getWindowId()), (LPARAM)getWindowHandle());
938 }
939//// else DebugInt3();
940 }
941 }
942 SendMessageA(getWindowHandle(),WM_DESTROY, 0, 0);
943 if(::IsWindow(hwnd) == FALSE) {
944 //object already destroyed, so return immediately
945 return 1;
946 }
947 SendMessageA(getWindowHandle(),WM_NCDESTROY, 0, 0);
948
949 TIMER_KillTimerFromWindow(getWindowHandle());
950
951 if(getRefCount() == 0 && getFirstChild() == NULL && state == STATE_CREATED) {
952 delete this;
953 }
954 else {
955 //make sure no message can ever arrive for this window again (PM or from other win32 windows)
956 dprintf(("Mark window %x (%x) as deleted; refcount %d", getWindowHandle(), this, getRefCount()));
957 markDeleted();
958 OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, 0);
959 OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32PM_MAGIC, 0);
960 if(Win32Hwnd) {
961 HwFreeWindowHandle(Win32Hwnd);
962 Win32Hwnd = 0;
963 }
964 }
965 return 1;
966}
967//******************************************************************************
968//******************************************************************************
969ULONG Win32BaseWindow::MsgEnable(BOOL fEnable)
970{
971 if(fEnable) {
972 dwStyle &= ~WS_DISABLED;
973 }
974 else dwStyle |= WS_DISABLED;
975
976 return SendMessageA(getWindowHandle(),WM_ENABLE, fEnable, 0);
977}
978//******************************************************************************
979//TODO: SW_PARENTCLOSING/OPENING flag (lParam)
980//******************************************************************************
981ULONG Win32BaseWindow::MsgShow(BOOL fShow)
982{
983 if(!CanReceiveSizeMsgs() || fDestroyWindowCalled) {
984 return 1;
985 }
986
987 if(fShow) {
988 setStyle(getStyle() | WS_VISIBLE);
989 if(getStyle() & WS_MINIMIZE) {
990 return ShowWindow(SW_RESTORE);
991 }
992 }
993 else setStyle(getStyle() & ~WS_VISIBLE);
994
995 //already sent from ShowWindow
996//// return SendMessageA(getWindowHandle(),WM_SHOWWINDOW, fShow, 0);
997 return 0;
998}
999//******************************************************************************
1000//******************************************************************************
1001ULONG Win32BaseWindow::MsgPosChanging(LPARAM lp)
1002{
1003 //SvL: Notes crashes when switching views (calls DestroyWindow -> PM sends
1004 // a WM_WINDOWPOSCHANGED msg -> crash)
1005 if(!CanReceiveSizeMsgs() || fDestroyWindowCalled)
1006 return 0;
1007
1008 return SendMessageA(getWindowHandle(),WM_WINDOWPOSCHANGING, 0, lp);
1009}
1010//******************************************************************************
1011//******************************************************************************
1012ULONG Win32BaseWindow::MsgPosChanged(LPARAM lp)
1013{
1014 //SvL: Notes crashes when switching views (calls DestroyWindow -> PM sends
1015 // a WM_WINDOWPOSCHANGED msg -> crash)
1016 if(!CanReceiveSizeMsgs() || fDestroyWindowCalled)
1017 return 1;
1018
1019 return SendMessageA(getWindowHandle(),WM_WINDOWPOSCHANGED, 0, lp);
1020}
1021//******************************************************************************
1022//******************************************************************************
1023ULONG Win32BaseWindow::MsgScroll(ULONG msg, ULONG scrollCode, ULONG scrollPos)
1024{
1025 //According to the SDK docs, the scrollbar handle (lParam) is 0 when the standard
1026 //window scrollbars send these messages
1027 return SendMessageA(getWindowHandle(),msg, MAKELONG(scrollCode, scrollPos), 0);
1028}
1029//******************************************************************************
1030//******************************************************************************
1031ULONG Win32BaseWindow::MsgActivate(BOOL fActivate, BOOL fMinimized, HWND hwnd, HWND hwndOS2Win)
1032{
1033 ULONG rc, procidhwnd = -1, threadidhwnd = 0;
1034
1035 //SvL: Don't send WM_(NC)ACTIVATE messages when the window is being destroyed
1036 if(fDestroyWindowCalled) {
1037 return 0;
1038 }
1039
1040 //According to SDK docs, if app returns FALSE & window is being deactivated,
1041 //default processing is cancelled
1042 //TODO: According to Wine we should proceed anyway if window is sysmodal
1043 if(SendMessageA(getWindowHandle(),WM_NCACTIVATE, fActivate, 0) == FALSE && !fActivate)
1044 {
1045 dprintf(("WARNING: WM_NCACTIVATE return code = FALSE -> cancel processing"));
1046 return 0;
1047 }
1048 /* child windows get a WM_CHILDACTIVATE message */
1049 if((getStyle() & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1050 {
1051 if(fActivate) {//WM_CHILDACTIVE is for activation only
1052 SendMessageA(getWindowHandle(),WM_CHILDACTIVATE, 0, 0L);
1053 }
1054 return 0;
1055 }
1056
1057 return SendMessageA(getWindowHandle(),WM_ACTIVATE, MAKELONG((fActivate) ? WA_ACTIVE : WA_INACTIVE, fMinimized), hwnd);
1058}
1059//******************************************************************************
1060//******************************************************************************
1061ULONG Win32BaseWindow::MsgChildActivate(BOOL fActivate)
1062{
1063 //SvL: Don't send WM_(NC)ACTIVATE messages when the window is being destroyed
1064 if(fDestroyWindowCalled) {
1065 return 0;
1066 }
1067
1068 //According to SDK docs, if app returns FALSE & window is being deactivated,
1069 //default processing is cancelled
1070 //TODO: According to Wine we should proceed anyway if window is sysmodal
1071 if(SendMessageA(getWindowHandle(),WM_NCACTIVATE, fActivate, 0) == FALSE && !fActivate)
1072 {
1073 dprintf(("WARNING: WM_NCACTIVATE return code = FALSE -> cancel processing"));
1074 return 0;
1075 }
1076 /* child windows get a WM_CHILDACTIVATE message */
1077 if((getStyle() & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1078 {
1079 if(fActivate) {//WM_CHILDACTIVE is for activation only
1080 SendMessageA(getWindowHandle(),WM_CHILDACTIVATE, 0, 0L);
1081 }
1082 return 0;
1083 }
1084 DebugInt3();
1085 return 0;
1086}
1087//******************************************************************************
1088//******************************************************************************
1089ULONG Win32BaseWindow::DispatchMsgA(MSG *msg)
1090{
1091 return SendMessageA(getWindowHandle(),msg->message, msg->wParam, msg->lParam);
1092}
1093//******************************************************************************
1094//******************************************************************************
1095ULONG Win32BaseWindow::DispatchMsgW(MSG *msg)
1096{
1097 return SendMessageW(getWindowHandle(), msg->message, msg->wParam, msg->lParam);
1098}
1099//******************************************************************************
1100//******************************************************************************
1101ULONG Win32BaseWindow::MsgSetFocus(HWND hwnd)
1102{
1103 //SvL: Don't send WM_(NC)ACTIVATE messages when the window is being destroyed
1104 if(fDestroyWindowCalled) {
1105 return 0;
1106 }
1107
1108 return SendMessageA(getWindowHandle(),WM_SETFOCUS, hwnd, 0);
1109}
1110//******************************************************************************
1111//******************************************************************************
1112ULONG Win32BaseWindow::MsgKillFocus(HWND hwnd)
1113{
1114 //SvL: Don't send WM_(NC)ACTIVATE messages when the window is being destroyed
1115 if(fDestroyWindowCalled) {
1116 return 0;
1117 }
1118 return SendMessageA(getWindowHandle(),WM_KILLFOCUS, hwnd, 0);
1119}
1120//******************************************************************************
1121//******************************************************************************
1122ULONG Win32BaseWindow::MsgButton(MSG *msg)
1123{
1124 BOOL fClick = FALSE;
1125
1126 dprintf(("MsgButton %d at (%d,%d)", msg->message, msg->pt.x, msg->pt.y));
1127 switch(msg->message)
1128 {
1129 case WM_LBUTTONDBLCLK:
1130 case WM_RBUTTONDBLCLK:
1131 case WM_MBUTTONDBLCLK:
1132 if (!(windowClass && windowClass->getClassLongA(GCL_STYLE) & CS_DBLCLKS))
1133 {
1134 msg->message = msg->message - (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN); //dblclick -> down
1135 return MsgButton(msg);
1136 }
1137 break;
1138 case WM_NCLBUTTONDBLCLK:
1139 case WM_NCRBUTTONDBLCLK:
1140 case WM_NCMBUTTONDBLCLK:
1141 //Docs say CS_DBLCLKS style doesn't matter for non-client double clicks
1142 fClick = TRUE;
1143 break;
1144
1145 case WM_LBUTTONDOWN:
1146 case WM_RBUTTONDOWN:
1147 case WM_MBUTTONDOWN:
1148 case WM_NCLBUTTONDOWN:
1149 case WM_NCRBUTTONDOWN:
1150 case WM_NCMBUTTONDOWN:
1151 fClick = TRUE;
1152 break;
1153 }
1154
1155 if(fClick)
1156 {
1157 HWND hwndTop;
1158
1159 /* Activate the window if needed */
1160 hwndTop = GetTopParent();
1161
1162 HWND hwndActive = GetActiveWindow();
1163 if (hwndTop && (getWindowHandle() != hwndActive))
1164 {
1165 LONG ret = SendMessageA(getWindowHandle(),WM_MOUSEACTIVATE, hwndTop,
1166 MAKELONG( lastHitTestVal, msg->message) );
1167
1168 dprintf2(("WM_MOUSEACTIVATE returned %d", ret));
1169#if 0
1170 if ((ret == MA_ACTIVATEANDEAT) || (ret == MA_NOACTIVATEANDEAT))
1171 eatMsg = TRUE;
1172#endif
1173 //SvL: 0 is not documented, but experiments in NT4 show that
1174 // the window will get activated when it returns this.
1175 // (FreeCell is an example)
1176 if(((ret == MA_ACTIVATE) || (ret == MA_ACTIVATEANDEAT) || (ret == 0))
1177 && (hwndTop != GetForegroundWindow()) )
1178 {
1179 Win32BaseWindow *win32top = Win32BaseWindow::GetWindowFromHandle(hwndTop);
1180
1181 //SvL: Calling OSLibSetActiveWindow(hwndTop); causes focus problems
1182 if (win32top) {
1183 OSLibWinSetFocus(win32top->getOS2FrameWindowHandle());
1184 RELEASE_WNDOBJ(win32top);
1185 }
1186 }
1187 }
1188 }
1189
1190 SendMessageA(getWindowHandle(),WM_SETCURSOR, getWindowHandle(), MAKELONG(lastHitTestVal, msg->message));
1191
1192 switch(msg->message)
1193 {
1194 case WM_LBUTTONDOWN:
1195 case WM_MBUTTONDOWN:
1196 case WM_RBUTTONDOWN:
1197 {
1198 if (getParent())
1199 {
1200 POINTS pt = MAKEPOINTS(msg->lParam);
1201 POINT point;
1202
1203 point.x = pt.x;
1204 point.y = pt.y;
1205 MapWindowPoints(getWindowHandle(), getParent()->getWindowHandle(), &point, 1);
1206 NotifyParent(msg->message, msg->wParam, MAKELPARAM(point.x,point.y));
1207 }
1208 break;
1209 }
1210 }
1211 return SendMessageA(getWindowHandle(),msg->message, msg->wParam, msg->lParam);
1212}
1213//******************************************************************************
1214//******************************************************************************
1215ULONG Win32BaseWindow::MsgPaint(ULONG tmp, ULONG select)
1216{
1217 if (select && IsWindowIconic())
1218 return SendMessageA(getWindowHandle(),WM_PAINTICON, 1, 0);
1219 else
1220 return SendMessageA(getWindowHandle(),WM_PAINT, 0, 0);
1221}
1222//******************************************************************************
1223//TODO: Is the clipper region of the window DC equal to the invalidated rectangle?
1224// (or are we simply erasing too much here)
1225//******************************************************************************
1226ULONG Win32BaseWindow::MsgEraseBackGround(HDC hdc)
1227{
1228 ULONG rc;
1229 HDC hdcErase = hdc;
1230
1231 if (hdcErase == 0)
1232 hdcErase = GetDC(getWindowHandle());
1233
1234 if(IsWindowIconic())
1235 rc = SendMessageA(getWindowHandle(),WM_ICONERASEBKGND, hdcErase, 0);
1236 else
1237 rc = SendMessageA(getWindowHandle(),WM_ERASEBKGND, hdcErase, 0);
1238 if (hdc == 0)
1239 ReleaseDC(getWindowHandle(), hdcErase);
1240 return (rc);
1241}
1242//******************************************************************************
1243//******************************************************************************
1244ULONG Win32BaseWindow::MsgMouseMove(MSG *msg)
1245{
1246 //TODO: hiword should be 0 if window enters menu mode (SDK docs)
1247 //SDK: WM_SETCURSOR is not sent if the mouse is captured
1248 if(GetCapture() == 0) {
1249 SendMessageA(getWindowHandle(),WM_SETCURSOR, Win32Hwnd, MAKELONG(lastHitTestVal, msg->message));
1250 }
1251
1252 //translated message == WM_(NC)MOUSEMOVE
1253 return SendMessageA(getWindowHandle(),msg->message, msg->wParam, msg->lParam);
1254}
1255//******************************************************************************
1256//******************************************************************************
1257ULONG Win32BaseWindow::MsgChar(MSG *msg)
1258{
1259 return DispatchMsgA(msg);
1260}
1261//******************************************************************************
1262//TODO: Should use update region, not rectangle
1263//******************************************************************************
1264ULONG Win32BaseWindow::MsgNCPaint(PRECT pUpdateRect)
1265{
1266 HRGN hrgn;
1267 ULONG rc;
1268 RECT client = rectClient;
1269
1270 if ((pUpdateRect->left >= client.left) && (pUpdateRect->left < client.right) &&
1271 (pUpdateRect->right >= client.left) && (pUpdateRect->right < client.right) &&
1272 (pUpdateRect->top >= client.top) && (pUpdateRect->top < client.bottom) &&
1273 (pUpdateRect->bottom >= client.top) && (pUpdateRect->bottom < client.bottom)
1274 && (!(getStyle() & WS_MINIMIZE)))
1275 {
1276 return 0;
1277 }
1278
1279 dprintf(("MsgNCPaint (%d,%d)(%d,%d)", pUpdateRect->left, pUpdateRect->top, pUpdateRect->right, pUpdateRect->bottom));
1280 hrgn = CreateRectRgnIndirect(pUpdateRect);
1281
1282 rc = SendMessageA(getWindowHandle(),WM_NCPAINT, hrgn, 0);
1283 //Send WM_PAINTICON here if minimized, because client window will
1284 //not receive a (valid) WM_PAINT message
1285 if (getStyle() & WS_MINIMIZE)
1286 {
1287 rc = SendMessageA(getWindowHandle(),WM_PAINTICON, 1, 0);
1288 }
1289
1290 DeleteObject(hrgn);
1291
1292 return rc;
1293}
1294//******************************************************************************
1295//Called when either the frame's size or position has changed (lpWndPos != NULL)
1296//or when the frame layout has changed (i.e. scrollbars added/removed) (lpWndPos == NULL)
1297//******************************************************************************
1298ULONG Win32BaseWindow::MsgFormatFrame(WINDOWPOS *lpWndPos)
1299{
1300 RECT oldWindowRect = rectWindow, client = rectClient, newWindowRect;
1301 RECT newClientRect;
1302 WINDOWPOS wndPos;
1303 ULONG rc;
1304
1305 if(lpWndPos)
1306 {
1307 //set new window rectangle
1308 setWindowRect(lpWndPos->x, lpWndPos->y, lpWndPos->x+lpWndPos->cx,
1309 lpWndPos->y+lpWndPos->cy);
1310 newWindowRect = rectWindow;
1311 }
1312 else {
1313 wndPos.hwnd = getWindowHandle();
1314 wndPos.hwndInsertAfter = 0;
1315 newWindowRect= rectWindow;
1316 wndPos.x = newWindowRect.left;
1317 wndPos.y = newWindowRect.top;
1318 wndPos.cx = newWindowRect.right - newWindowRect.left;
1319 wndPos.cy = newWindowRect.bottom - newWindowRect.top;
1320 wndPos.flags = SWP_FRAMECHANGED;
1321 lpWndPos = &wndPos;
1322 }
1323
1324 newClientRect = rectClient;
1325 rc = SendNCCalcSize(TRUE, &newWindowRect, &oldWindowRect, &client, lpWndPos, &newClientRect);
1326 rectClient = newClientRect; //must update rectClient here
1327
1328 dprintf(("MsgFormatFrame: old client rect (%d,%d)(%d,%d), new client (%d,%d)(%d,%d)", client.left, client.top, client.right, client.bottom, rectClient.left, rectClient.top, rectClient.right, rectClient.bottom));
1329 dprintf(("MsgFormatFrame: old window rect (%d,%d)(%d,%d), new window (%d,%d)(%d,%d)", oldWindowRect.left, oldWindowRect.top, oldWindowRect.right, oldWindowRect.bottom, rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
1330
1331 if(!CanReceiveSizeMsgs() || !EqualRect(&client, &rectClient)) {
1332 OSLibWinSetClientPos(getOS2WindowHandle(), rectClient.left, rectClient.top, getClientWidth(), getClientHeight(), getWindowHeight());
1333 }
1334
1335#if 1
1336//this doesn't always work
1337// if(CanReceiveSizeMsgs() && (client.left != rectClient.left || client.top != rectClient.top))
1338 if(CanReceiveSizeMsgs() && ((oldWindowRect.right - oldWindowRect.left < rectClient.left
1339 || oldWindowRect.bottom - oldWindowRect.top < rectClient.top) ||
1340 (EqualRect(&oldWindowRect, &rectWindow) && (client.left != rectClient.left || client.top != rectClient.top))))
1341 {
1342 Win32BaseWindow *child = (Win32BaseWindow *)getFirstChild();
1343
1344 //client rectangle has moved -> inform children
1345 dprintf(("MsgFormatFrame -> client rectangle has changed, move children"));
1346 while(child) {
1347 ::SetWindowPos(child->getWindowHandle(),
1348 HWND_TOP, child->getWindowRect()->left,
1349 child->getWindowRect()->top, 0, 0,
1350 SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);
1351 child = (Win32BaseWindow *)child->getNextChild();
1352 }
1353 }
1354#endif
1355 if(fOS2Look && ((dwStyle & WS_CAPTION) == WS_CAPTION))
1356 {
1357 RECT rect = {0};
1358 int height = getWindowHeight();
1359 RECTLOS2 rectOS2;
1360
1361 AdjustRectOuter(&rect, FALSE);
1362
1363 rect.left = -rect.left;
1364 rect.top = rect.bottom - rect.top;
1365 rect.right = rectWindow.right - rectWindow.left - rect.right;
1366
1367 rectOS2.xLeft = rect.left;
1368 rectOS2.xRight = rect.right;
1369 rectOS2.yBottom = height - rect.top;
1370 rectOS2.yTop = height - rect.bottom;
1371 OSLibWinPositionFrameControls(getOS2FrameWindowHandle(), &rectOS2, dwStyle, dwExStyle, IconForWindow(ICON_SMALL));
1372 }
1373 return rc;
1374}
1375//******************************************************************************
1376//******************************************************************************
1377ULONG Win32BaseWindow::MsgSetText(LPSTR lpsz, LONG cch)
1378{
1379 return SendMessageA(getWindowHandle(),WM_SETTEXT, 0, (LPARAM)lpsz);
1380}
1381//******************************************************************************
1382//******************************************************************************
1383ULONG Win32BaseWindow::MsgGetTextLength()
1384{
1385 return SendMessageA(getWindowHandle(),WM_GETTEXTLENGTH, 0, 0);
1386}
1387//******************************************************************************
1388//******************************************************************************
1389void Win32BaseWindow::MsgGetText(char *wndtext, ULONG textlength)
1390{
1391 SendMessageA(getWindowHandle(),WM_GETTEXT, textlength, (LPARAM)wndtext);
1392}
1393//******************************************************************************
1394//******************************************************************************
1395BOOL Win32BaseWindow::isMDIClient()
1396{
1397 return FALSE;
1398}
1399//******************************************************************************
1400//******************************************************************************
1401BOOL Win32BaseWindow::isMDIChild()
1402{
1403 return FALSE;
1404}
1405//******************************************************************************
1406//TODO: Not complete
1407//******************************************************************************
1408BOOL Win32BaseWindow::isFrameWindow()
1409{
1410 if(getParent() == NULL)
1411 return TRUE;
1412
1413 return FALSE;
1414}
1415//******************************************************************************
1416//******************************************************************************
1417BOOL Win32BaseWindow::isDesktopWindow()
1418{
1419 return FALSE;
1420}
1421//******************************************************************************
1422//******************************************************************************
1423BOOL Win32BaseWindow::IsWindowIconic()
1424{
1425 return ((getStyle() & WS_MINIMIZE) && windowClass->getIcon());
1426}
1427//******************************************************************************
1428//******************************************************************************
1429SCROLLBAR_INFO *Win32BaseWindow::getScrollInfo(int nBar)
1430{
1431 switch(nBar)
1432 {
1433 case SB_HORZ:
1434 if (!horzScrollInfo)
1435 {
1436 horzScrollInfo = (SCROLLBAR_INFO*)malloc(sizeof(SCROLLBAR_INFO));
1437 if (!horzScrollInfo) break;
1438 horzScrollInfo->MinVal = horzScrollInfo->CurVal = horzScrollInfo->Page = 0;
1439 horzScrollInfo->MaxVal = 100;
1440 horzScrollInfo->flags = ESB_ENABLE_BOTH;
1441 }
1442 return horzScrollInfo;
1443
1444 case SB_VERT:
1445 if (!vertScrollInfo)
1446 {
1447 vertScrollInfo = (SCROLLBAR_INFO*)malloc(sizeof(SCROLLBAR_INFO));
1448 if (!vertScrollInfo) break;
1449 vertScrollInfo->MinVal = vertScrollInfo->CurVal = vertScrollInfo->Page = 0;
1450 vertScrollInfo->MaxVal = 100;
1451 vertScrollInfo->flags = ESB_ENABLE_BOTH;
1452 }
1453 return vertScrollInfo;
1454 }
1455
1456 return NULL;
1457}
1458//******************************************************************************
1459//******************************************************************************
1460LRESULT Win32BaseWindow::DefWndControlColor(UINT ctlType, HDC hdc)
1461{
1462 //SvL: Set background color to default button color (not window (white))
1463 if(ctlType == CTLCOLOR_BTN)
1464 {
1465 SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
1466 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
1467 return GetSysColorBrush(COLOR_BTNFACE);
1468 }
1469 //SvL: Set background color to default dialog color if window is dialog
1470 if((ctlType == CTLCOLOR_DLG || ctlType == CTLCOLOR_STATIC) && IsDialog()) {
1471 SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
1472 SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
1473 return GetSysColorBrush(COLOR_BTNFACE);
1474 }
1475 if( ctlType == CTLCOLOR_SCROLLBAR)
1476 {
1477 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
1478 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
1479 SetTextColor( hdc, GetSysColor(COLOR_3DFACE));
1480 SetBkColor( hdc, bk);
1481
1482 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
1483 * we better use 0x55aa bitmap brush to make scrollbar's background
1484 * look different from the window background.
1485 */
1486 if (bk == GetSysColor(COLOR_WINDOW)) {
1487 return GetPattern55AABrush();
1488 }
1489
1490 UnrealizeObject( hb );
1491 return (LRESULT)hb;
1492 }
1493
1494 SetTextColor( hdc, GetSysColor(COLOR_WINDOWTEXT));
1495
1496 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
1497 {
1498 SetBkColor( hdc, GetSysColor(COLOR_WINDOW) );
1499 }
1500 else
1501 {
1502 SetBkColor( hdc, GetSysColor(COLOR_3DFACE) );
1503 return (LRESULT)GetSysColorBrush(COLOR_3DFACE);
1504 }
1505 return (LRESULT)GetSysColorBrush(COLOR_WINDOW);
1506}
1507//******************************************************************************
1508//******************************************************************************
1509LRESULT Win32BaseWindow::DefWndPrint(HDC hdc,ULONG uFlags)
1510{
1511 /*
1512 * Visibility flag.
1513 */
1514 if ( (uFlags & PRF_CHECKVISIBLE) &&
1515 !IsWindowVisible(getWindowHandle()) )
1516 return 0;
1517
1518 /*
1519 * Unimplemented flags.
1520 */
1521 if ( (uFlags & PRF_CHILDREN) ||
1522 (uFlags & PRF_OWNED) ||
1523 (uFlags & PRF_NONCLIENT) )
1524 {
1525 dprintf(("WM_PRINT message with unsupported flags\n"));
1526 }
1527
1528 /*
1529 * Background
1530 */
1531 if ( uFlags & PRF_ERASEBKGND)
1532 SendMessageA(getWindowHandle(),WM_ERASEBKGND, (WPARAM)hdc, 0);
1533
1534 /*
1535 * Client area
1536 */
1537 if ( uFlags & PRF_CLIENT)
1538 SendMessageA(getWindowHandle(),WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
1539
1540
1541 return 0;
1542}
1543//******************************************************************************
1544//******************************************************************************
1545LRESULT Win32BaseWindow::DefWindowProcA(UINT Msg, WPARAM wParam, LPARAM lParam)
1546{
1547 switch(Msg)
1548 {
1549 case WM_CLOSE:
1550 dprintf(("DefWindowProcA: WM_CLOSE %x", getWindowHandle()));
1551 DestroyWindow();
1552 return 0;
1553
1554 case WM_GETTEXTLENGTH:
1555 return windowNameLength;
1556
1557 case WM_GETTEXT:
1558 if (!lParam || !wParam)
1559 return 0;
1560 if (!windowNameA)
1561 ((LPSTR)lParam)[0] = 0;
1562 else
1563 memcpy((LPSTR)lParam, windowNameA, min(windowNameLength+1, wParam) );
1564 return min(windowNameLength, wParam);
1565
1566 case WM_SETTEXT:
1567 {
1568 LPCSTR lpsz = (LPCSTR)lParam;
1569
1570 // reallocate if new buffer is larger
1571 if (!lParam)
1572 {
1573 free(windowNameA);
1574 free(windowNameW);
1575 windowNameLength = 0;
1576 windowNameA = NULL;
1577 windowNameW = NULL;
1578 }
1579 else
1580 {
1581 // determine length of new text
1582 int iTextLength = strlen(lpsz);
1583
1584 if (windowNameLength < iTextLength)
1585 {
1586 if (windowNameA)
1587 {
1588 free(windowNameA);
1589 windowNameA = NULL;
1590 }
1591
1592 if (windowNameW)
1593 {
1594 free(windowNameW);
1595 windowNameW = NULL;
1596 }
1597 }
1598
1599 windowNameLength = iTextLength;
1600 if(!windowNameA)
1601 windowNameA = (LPSTR)_smalloc(windowNameLength+1);
1602 memcpy(windowNameA, lpsz, windowNameLength+1);
1603 if(!windowNameW)
1604 windowNameW = (LPWSTR)_smalloc((windowNameLength+1)*sizeof(WCHAR));
1605 lstrcpynAtoW(windowNameW, windowNameA, windowNameLength+1);
1606 }
1607
1608 dprintf(("WM_SETTEXT of %x to %s\n", Win32Hwnd, lParam));
1609 if ((dwStyle & WS_CAPTION) == WS_CAPTION)
1610 {
1611 HandleNCPaint((HRGN)1);
1612 if(hTaskList) {
1613 OSLibWinChangeTaskList(hTaskList, OS2HwndFrame, getWindowNameA(), (getStyle() & WS_VISIBLE) ? 1 : 0);
1614 }
1615 if(fOS2Look) {
1616 OSLibWinSetTitleBarText(OS2HwndFrame, getWindowNameA());
1617 }
1618 }
1619
1620 return TRUE;
1621 }
1622
1623 case WM_SETREDRAW:
1624 {
1625 if (wParam)
1626 {
1627 setStyle(getStyle() | WS_VISIBLE);
1628 dprintf(("Enable window update for %x", getWindowHandle()));
1629 OSLibWinEnableWindowUpdate(OS2HwndFrame, OS2Hwnd, TRUE);
1630 }
1631 else
1632 {
1633 if (getStyle() & WS_VISIBLE)
1634 {
1635 setStyle(getStyle() & ~WS_VISIBLE);
1636 dprintf(("Disable window update for %x", getWindowHandle()));
1637 OSLibWinEnableWindowUpdate(OS2HwndFrame, OS2Hwnd, FALSE);
1638 }
1639 }
1640 return 0;
1641 }
1642
1643 case WM_CTLCOLORMSGBOX:
1644 case WM_CTLCOLOREDIT:
1645 case WM_CTLCOLORLISTBOX:
1646 case WM_CTLCOLORBTN:
1647 case WM_CTLCOLORDLG:
1648 case WM_CTLCOLORSTATIC:
1649 case WM_CTLCOLORSCROLLBAR:
1650 return DefWndControlColor(Msg - WM_CTLCOLORMSGBOX, (HDC)wParam);
1651
1652 case WM_CTLCOLOR:
1653 return DefWndControlColor(HIWORD(lParam), (HDC)wParam);
1654
1655 case WM_VKEYTOITEM:
1656 case WM_CHARTOITEM:
1657 return -1;
1658
1659 case WM_PARENTNOTIFY:
1660 return 0;
1661
1662 case WM_MOUSEACTIVATE:
1663 {
1664 dprintf(("DefWndProc: WM_MOUSEACTIVATE for %x Msg %s", Win32Hwnd, GetMsgText(HIWORD(lParam))));
1665 if(getStyle() & WS_CHILD && !(getExStyle() & WS_EX_NOPARENTNOTIFY) )
1666 {
1667 if(getParent()) {
1668 LRESULT rc = SendMessageA(getParent()->getWindowHandle(), WM_MOUSEACTIVATE, wParam, lParam );
1669 if(rc) return rc;
1670 }
1671 }
1672 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
1673 }
1674
1675 case WM_ACTIVATE:
1676 /* The default action in Windows is to set the keyboard focus to
1677 * the window, if it's being activated and not minimized */
1678 if (LOWORD(wParam) != WA_INACTIVE) {
1679 if(!(getStyle() & WS_MINIMIZE))
1680 SetFocus(getWindowHandle());
1681 }
1682 return 0;
1683
1684 case WM_SETCURSOR:
1685 {
1686 dprintf(("DefWndProc: WM_SETCURSOR for %x Msg %s", Win32Hwnd, GetMsgText(HIWORD(lParam))));
1687 if((getStyle() & WS_CHILD))
1688 {
1689 if(getParent()) {
1690 LRESULT rc = SendMessageA(getParent()->getWindowHandle(), WM_SETCURSOR, wParam, lParam);
1691 if(rc) return rc;
1692 }
1693 }
1694 if (wParam == getWindowHandle())
1695 {
1696 HCURSOR hCursor;
1697
1698 switch(LOWORD(lParam))
1699 {
1700 case HTCLIENT:
1701 hCursor = windowClass ? windowClass->getCursor():LoadCursorA(0,IDC_ARROWA);
1702 break;
1703
1704 case HTLEFT:
1705 case HTRIGHT:
1706 hCursor = LoadCursorA(0,IDC_SIZEWEA);
1707 break;
1708
1709 case HTTOP:
1710 case HTBOTTOM:
1711 hCursor = LoadCursorA(0,IDC_SIZENSA);
1712 break;
1713
1714 case HTTOPLEFT:
1715 case HTBOTTOMRIGHT:
1716 hCursor = LoadCursorA(0,IDC_SIZENWSEA);
1717 break;
1718
1719 case HTTOPRIGHT:
1720 case HTBOTTOMLEFT:
1721 hCursor = LoadCursorA(0,IDC_SIZENESWA);
1722 break;
1723
1724 default:
1725 hCursor = LoadCursorA(0,IDC_ARROWA);
1726 break;
1727 }
1728
1729 if (hCursor)
1730 {
1731 SetCursor(hCursor);
1732 return 1;
1733 }
1734 else return 0;
1735 }
1736 else return 0;
1737 }
1738
1739 case WM_MOUSEMOVE:
1740 return 0;
1741
1742 case WM_WINDOWPOSCHANGED:
1743 {
1744 PWINDOWPOS wpos = (PWINDOWPOS)lParam;
1745 WPARAM wp = SIZE_RESTORED;
1746
1747 if (!(wpos->flags & SWP_NOMOVE) && !(wpos->flags & SWP_NOCLIENTMOVE))
1748 {
1749 SendMessageA(getWindowHandle(),WM_MOVE, 0, MAKELONG(rectClient.left,rectClient.top));
1750 }
1751 if (!(wpos->flags & SWP_NOSIZE) && !(wpos->flags & SWP_NOCLIENTSIZE))
1752 {
1753 if (dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
1754 else
1755 if (dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
1756
1757 SendMessageA(getWindowHandle(),WM_SIZE, wp, MAKELONG(rectClient.right - rectClient.left,
1758 rectClient.bottom - rectClient.top));
1759 }
1760 return 0;
1761 }
1762 case WM_WINDOWPOSCHANGING:
1763 return HandleWindowPosChanging((WINDOWPOS *)lParam);
1764
1765 case WM_ERASEBKGND:
1766 case WM_ICONERASEBKGND:
1767 {
1768 RECT rect;
1769 int rc;
1770
1771 if (!windowClass || !windowClass->getBackgroundBrush()) return 0;
1772
1773 rc = GetClipBox( (HDC)wParam, &rect );
1774 if ((rc == SIMPLEREGION) || (rc == COMPLEXREGION))
1775 {
1776 HBRUSH hBrush = windowClass->getBackgroundBrush();
1777
1778 if (hBrush <= (HBRUSH)(SYSCOLOR_GetLastColor()+1))
1779 hBrush = GetSysColorBrush(hBrush-1);
1780
1781 FillRect( (HDC)wParam, &rect, hBrush);
1782 }
1783 return 1;
1784 }
1785
1786 case WM_PRINT:
1787 return DefWndPrint(wParam,lParam);
1788
1789 case WM_SYNCPAINT:
1790 RedrawWindow(getWindowHandle(), NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN);
1791 return 0;
1792
1793 case WM_PAINTICON:
1794 case WM_PAINT:
1795 {
1796 PAINTSTRUCT ps;
1797 HDC hdc = BeginPaint(getWindowHandle(), &ps );
1798 if( hdc )
1799 {
1800 if( (getStyle() & WS_MINIMIZE) && (getWindowClass()->getIcon() || hIcon))
1801 {
1802 int x = (rectWindow.right - rectWindow.left - GetSystemMetrics(SM_CXICON))/2;
1803 int y = (rectWindow.bottom - rectWindow.top - GetSystemMetrics(SM_CYICON))/2;
1804 dprintf(("Painting class icon: vis rect=(%i,%i - %i,%i)\n", ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom ));
1805 DrawIcon(hdc, x, y, hIcon ? hIcon:getWindowClass()->getIcon() );
1806 }
1807 EndPaint(getWindowHandle(), &ps );
1808 }
1809 return 0;
1810 }
1811
1812 case WM_GETDLGCODE:
1813 return 0;
1814
1815 case WM_NCPAINT:
1816 return HandleNCPaint((HRGN)wParam);
1817
1818 case WM_NCACTIVATE:
1819 return HandleNCActivate(wParam);
1820
1821 case WM_NCCREATE:
1822 return(TRUE);
1823
1824 case WM_NCDESTROY:
1825 return 0;
1826
1827 case WM_NCCALCSIZE:
1828 return HandleNCCalcSize((BOOL)wParam,(RECT*)lParam);
1829
1830 case WM_NCLBUTTONDOWN:
1831 return HandleNCLButtonDown(wParam,lParam);
1832
1833 case WM_LBUTTONDBLCLK:
1834 case WM_NCLBUTTONDBLCLK:
1835 return HandleNCLButtonDblClk(wParam,lParam);
1836
1837 case WM_NCRBUTTONDOWN:
1838 case WM_NCRBUTTONDBLCLK:
1839 case WM_NCMBUTTONDOWN:
1840 case WM_NCMBUTTONDBLCLK:
1841 if (lastHitTestVal == HTERROR) MessageBeep(MB_ICONEXCLAMATION);
1842 return 0;
1843
1844 case WM_NCRBUTTONUP:
1845 return HandleNCRButtonUp(wParam,lParam);
1846
1847 case WM_NCMBUTTONUP:
1848 return 0;
1849
1850 case WM_NCHITTEST:
1851 {
1852 POINT point;
1853 LRESULT retvalue;
1854
1855 point.x = (SHORT)LOWORD(lParam);
1856 point.y = (SHORT)HIWORD(lParam);
1857
1858 retvalue = HandleNCHitTest(point);
1859#if 0 //CB: let the Corel people fix the bugs first
1860 if(retvalue == HTMENU)
1861 MENU_TrackMouseMenuBar_MouseMove(Win32Hwnd,point,TRUE);
1862 else
1863 MENU_TrackMouseMenuBar_MouseMove(Win32Hwnd,point,FALSE);
1864#endif
1865 return retvalue;
1866 }
1867
1868 case WM_SYSCOMMAND:
1869 {
1870 POINT point;
1871
1872 point.x = LOWORD(lParam);
1873 point.y = HIWORD(lParam);
1874 return HandleSysCommand(wParam,&point);
1875 }
1876
1877 case WM_SYSKEYDOWN:
1878 {
1879 if( HIWORD(lParam) & KEYDATA_ALT )
1880 {
1881 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
1882 if( wParam == VK_MENU && !iMenuSysKey )
1883 iMenuSysKey = 1;
1884 else
1885 iMenuSysKey = 0;
1886
1887 iF10Key = 0;
1888
1889 if( wParam == VK_F4 ) /* try to close the window */
1890 {
1891 HWND top = GetTopParent();
1892 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
1893 PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
1894 }
1895 }
1896 else if( wParam == VK_F10 )
1897 iF10Key = 1;
1898 else
1899 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
1900 SendMessageW(getWindowHandle(), WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE );
1901
1902 Win32BaseWindow *siblingWindow;
1903 HWND sibling;
1904 char nameBuffer [40], mnemonic;
1905 int nameLength;
1906
1907 GetWindowTextA (nameBuffer, 40);
1908
1909 // search all sibling to see it this key is their mnemonic
1910 sibling = GetWindow (GW_HWNDFIRST);
1911 while (sibling != 0) {
1912 siblingWindow = GetWindowFromHandle (sibling);
1913 nameLength = siblingWindow->GetWindowTextA (nameBuffer, 40);
1914
1915 // find the siblings mnemonic
1916 mnemonic = '\0';
1917 for (int i=0 ; i<nameLength ; i++) {
1918 if (IsDBCSLeadByte(nameBuffer[i])) {
1919 // Skip DBCS
1920 continue;
1921 }
1922 if (nameBuffer [i] == '&') {
1923 mnemonic = nameBuffer [i+1];
1924 if ((mnemonic >= 'a') && (mnemonic <= 'z'))
1925 mnemonic -= 32; // make it uppercase
1926 break; // stop searching
1927 }
1928 }
1929
1930 // key matches siblings mnemonic, send mouseclick
1931 if (mnemonic == (char) wParam) {
1932 ::SendMessageA(siblingWindow->getWindowHandle(), BM_CLICK, 0, 0);
1933 }
1934 sibling = siblingWindow->GetNextWindow (GW_HWNDNEXT);
1935 RELEASE_WNDOBJ(siblingWindow);
1936 }
1937
1938 return 0;
1939 }
1940
1941 case WM_KEYUP:
1942 case WM_SYSKEYUP:
1943 /* Press and release F10 or ALT */
1944 if (((wParam == VK_MENU) && iMenuSysKey) ||
1945 ((wParam == VK_F10) && iF10Key))
1946 ::SendMessageW( GetTopWindow(), WM_SYSCOMMAND, SC_KEYMENU, 0L );
1947 iMenuSysKey = iF10Key = 0;
1948 break;
1949
1950 case WM_SYSCHAR:
1951 {
1952 iMenuSysKey = 0;
1953 if (wParam == VK_RETURN && (getStyle() & WS_MINIMIZE))
1954 {
1955 PostMessageA(getWindowHandle(), WM_SYSCOMMAND,
1956 (WPARAM)SC_RESTORE, 0L );
1957 break;
1958 }
1959 if((HIWORD(lParam) & KEYDATA_ALT) && wParam)
1960 {
1961 if (wParam == VK_TAB || wParam == VK_ESCAPE || wParam == VK_F4)
1962 break;
1963 if (wParam == VK_SPACE && (getStyle() & WS_CHILD)) {
1964 ::SendMessageW(GetParent(), Msg, wParam, lParam );
1965 }
1966 else ::SendMessageA(getWindowHandle(), WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)wParam );
1967 }
1968#if 0
1969 else /* check for Ctrl-Esc */
1970 if (wParam != VK_ESCAPE) MessageBeep(0);
1971 break;
1972#endif
1973 }
1974
1975 case WM_SETHOTKEY:
1976 hotkey = wParam;
1977 return 1; //CB: always successful
1978
1979 case WM_GETHOTKEY:
1980 return hotkey;
1981
1982 case WM_RBUTTONUP:
1983 {
1984 POINT pt;
1985 pt.x = SLOWORD(lParam);
1986 pt.y = SHIWORD(lParam);
1987 ClientToScreen(getWindowHandle(), &pt);
1988 SendMessageA( getWindowHandle(), WM_CONTEXTMENU, getWindowHandle(),MAKELPARAM(pt.x, pt.y) );
1989 }
1990 break;
1991
1992 case WM_CONTEXTMENU:
1993 if ((dwStyle & WS_CHILD) && getParent())
1994 SendMessageA(getParent()->getWindowHandle(), WM_CONTEXTMENU,wParam,lParam);
1995 else
1996 {
1997 LONG hitcode;
1998 POINT pt;
1999 if (!GetSysMenu()) return 0;
2000 pt.x = SLOWORD(lParam);
2001 pt.y = SHIWORD(lParam);
2002 hitcode = HandleNCHitTest(pt);
2003
2004 /* Track system popup if click was in the caption area. */
2005 if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
2006 TrackPopupMenu(GetSysMenu(),
2007 TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
2008 pt.x, pt.y, 0, getWindowHandle(), NULL);
2009 }
2010 return 0;
2011
2012 case WM_SHOWWINDOW:
2013 if (!lParam) return 0; /* sent from ShowWindow */
2014 if (!(dwStyle & WS_POPUP) || !owner) return 0;
2015 if ((dwStyle & WS_VISIBLE) && wParam) return 0;
2016 else if (!(dwStyle & WS_VISIBLE) && !wParam) return 0;
2017 ShowWindow(wParam ? SW_SHOW:SW_HIDE);
2018 return 0;
2019
2020 case WM_CANCELMODE:
2021 if (getParent() == windowDesktop) EndMenu();
2022 if (GetCapture() == Win32Hwnd) ReleaseCapture();
2023 return 0;
2024
2025 case WM_DROPOBJECT:
2026 return DRAG_FILE;
2027
2028 case WM_QUERYDROPOBJECT:
2029 return (dwExStyle & WS_EX_ACCEPTFILES) ? 1:0;
2030
2031 case WM_QUERYDRAGICON:
2032 {
2033 HICON hDragIcon = windowClass->getCursor();
2034 UINT len;
2035
2036 if(hDragIcon) return (LRESULT)hDragIcon;
2037 for(len = 1; len < 64; len++)
2038 {
2039 hDragIcon = LoadIconA(hInstance,MAKEINTRESOURCEA(len));
2040 if(hDragIcon)
2041 return (LRESULT)hDragIcon;
2042 }
2043 return (LRESULT)LoadIconA(0,IDI_APPLICATIONA);
2044 }
2045
2046 case WM_QUERYOPEN:
2047 case WM_QUERYENDSESSION:
2048 return 1;
2049
2050 case WM_NOTIFYFORMAT:
2051 return IsWindowUnicode() ? NFR_UNICODE:NFR_ANSI;
2052
2053 case WM_SETICON:
2054 case WM_GETICON:
2055 {
2056 LRESULT result = 0;
2057
2058 /* Set the appropriate icon members in the window structure. */
2059 if (wParam == ICON_SMALL)
2060 {
2061 result = hIconSm;
2062 if (Msg == WM_SETICON)
2063 hIconSm = (HICON)lParam;
2064 }
2065 else
2066 {
2067 result = hIcon;
2068 if (Msg == WM_SETICON)
2069 {
2070 hIcon = (HICON)lParam;
2071 if ((dwStyle & WS_CAPTION) == WS_CAPTION)
2072 OSLibWinSetIcon(OS2HwndFrame,hIcon);
2073 }
2074 }
2075 if ((Msg == WM_SETICON) && ((dwStyle & WS_CAPTION) == WS_CAPTION))
2076 HandleNCPaint((HRGN)1);
2077
2078 return result;
2079 }
2080
2081 case WM_HELP:
2082 if (getParent()) SendMessageA(getParent()->getWindowHandle(), Msg,wParam,lParam);
2083 break;
2084
2085 case WM_NOTIFY:
2086 return 0; //comctl32 controls expect this
2087
2088 default:
2089 return 0;
2090 }
2091 return 0;
2092}
2093//******************************************************************************
2094//******************************************************************************
2095LRESULT Win32BaseWindow::DefWindowProcW(UINT Msg, WPARAM wParam, LPARAM lParam)
2096{
2097 switch(Msg)
2098 {
2099 case WM_GETTEXTLENGTH:
2100 return windowNameLength;
2101
2102 case WM_GETTEXT:
2103 if (!lParam || !wParam)
2104 return 0;
2105 if (!windowNameW)
2106 ((LPWSTR)lParam)[0] = 0;
2107 else
2108 memcpy((LPSTR)lParam, windowNameW, min( sizeof(WCHAR) * (windowNameLength+1), wParam) );
2109 return min(windowNameLength, wParam);
2110
2111 case WM_SETTEXT:
2112 {
2113 LPWSTR lpsz = (LPWSTR)lParam;
2114
2115 // reallocate if new buffer is larger
2116 if (!lParam)
2117 {
2118 free(windowNameA);
2119 free(windowNameW);
2120 windowNameLength = 0;
2121 windowNameA = NULL;
2122 windowNameW = NULL;
2123 }
2124 else
2125 {
2126 // determine length of new text
2127 int iTextLength = lstrlenW(lpsz);
2128
2129 if (windowNameLength < iTextLength)
2130 {
2131 if (windowNameA)
2132 {
2133 free(windowNameA);
2134 windowNameA = NULL;
2135 }
2136
2137 if (windowNameW)
2138 {
2139 free(windowNameW);
2140 windowNameW = NULL;
2141 }
2142 }
2143
2144 windowNameLength = iTextLength;
2145 if(!windowNameW)
2146 windowNameW = (LPWSTR)_smalloc((windowNameLength+1)*sizeof(WCHAR));
2147 memcpy(windowNameW, lpsz, (windowNameLength+1) * sizeof(WCHAR));
2148 if(!windowNameA)
2149 windowNameA = (LPSTR)_smalloc(windowNameLength+1);
2150 lstrcpynWtoA(windowNameA, windowNameW, windowNameLength+1);
2151 }
2152
2153 dprintf(("WM_SETTEXT of %x\n",Win32Hwnd));
2154 if ((dwStyle & WS_CAPTION) == WS_CAPTION)
2155 {
2156 HandleNCPaint((HRGN)1);
2157 if(hTaskList) {
2158 OSLibWinChangeTaskList(hTaskList, OS2HwndFrame, getWindowNameA(), (getStyle() & WS_VISIBLE) ? 1 : 0);
2159 }
2160 if(fOS2Look) {
2161 OSLibWinSetTitleBarText(OS2HwndFrame, getWindowNameA());
2162 }
2163 }
2164
2165 return TRUE;
2166 }
2167
2168 default:
2169 return DefWindowProcA(Msg, wParam, lParam);
2170 }
2171}
2172//******************************************************************************
2173//******************************************************************************
2174void Win32BaseWindow::NotifyParent(UINT Msg, WPARAM wParam, LPARAM lParam)
2175{
2176 Win32BaseWindow *window = this;
2177 Win32BaseWindow *parentwindow;
2178
2179 while(window)
2180 {
2181 if(window->getStyle() & WS_CHILD && !(window->getExStyle() & WS_EX_NOPARENTNOTIFY) )
2182 {
2183 /* Notify the parent window only */
2184 parentwindow = window->getParent();
2185 if(parentwindow) {
2186 SendMessageA(parentwindow->getWindowHandle(), WM_PARENTNOTIFY, MAKEWPARAM(Msg, getWindowId()), lParam );
2187 }
2188 }
2189 else break;
2190
2191 window = parentwindow;
2192 }
2193}
2194//******************************************************************************
2195// Returns the big or small icon for the window, falling back to the
2196// class as windows does.
2197//******************************************************************************
2198HICON Win32BaseWindow::IconForWindow(WPARAM fType)
2199{
2200 HICON hWndIcon;
2201
2202 if (fType == ICON_BIG)
2203 {
2204 if (hIcon)
2205 hWndIcon = hIcon;
2206 else
2207 if (windowClass && windowClass->getIcon())
2208 hWndIcon = windowClass->getIcon();
2209 else
2210 if (!(dwStyle & DS_MODALFRAME))
2211 hWndIcon = LoadImageA(0,MAKEINTRESOURCEA(OIC_ODINICON),IMAGE_ICON,0,0,LR_DEFAULTCOLOR);
2212 else hWndIcon = 0;
2213 }
2214 else
2215 {
2216 if (hIconSm)
2217 hWndIcon = hIconSm;
2218 else
2219 if (hIcon)
2220 hWndIcon = hIcon;
2221 else
2222 if (windowClass && windowClass->getIconSm())
2223 hWndIcon = windowClass->getIconSm();
2224 else
2225 if (windowClass && windowClass->getIcon())
2226 hWndIcon = windowClass->getIcon();
2227 else
2228 if (!(dwStyle & DS_MODALFRAME))
2229 hWndIcon = LoadImageA(0,MAKEINTRESOURCEA(OIC_ODINICON),IMAGE_ICON,0,0,LR_DEFAULTCOLOR);
2230 else hWndIcon = 0;
2231 }
2232
2233 return hWndIcon;
2234}
2235//******************************************************************************
2236//******************************************************************************
2237BOOL Win32BaseWindow::ShowWindow(ULONG nCmdShow)
2238{
2239 ULONG swp = 0;
2240 HWND hWinAfter;
2241 BOOL rc,wasVisible,showFlag;
2242 RECT newPos = {0, 0, 0, 0};
2243
2244 dprintf(("ShowWindow %x %x", getWindowHandle(), nCmdShow));
2245 wasVisible = (getStyle() & WS_VISIBLE) != 0;
2246
2247 dwOldStyle = getStyle();
2248
2249 switch(nCmdShow)
2250 {
2251 case SW_HIDE:
2252 if (!wasVisible) goto END;
2253
2254 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER;
2255 break;
2256
2257 case SW_SHOWMINNOACTIVE:
2258 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
2259 /* fall through */
2260 case SW_SHOWMINIMIZED:
2261 swp |= SWP_SHOWWINDOW;
2262 /* fall through */
2263 case SW_MINIMIZE:
2264 swp |= SWP_FRAMECHANGED;
2265 if( !(getStyle() & WS_MINIMIZE) ) {
2266 swp |= MinMaximize(SW_MINIMIZE, &newPos );
2267 fMinMaxChange = TRUE; //-> invalidate entire window in WM_CALCINVALIDRECT
2268 }
2269 else swp |= SWP_NOSIZE | SWP_NOMOVE;
2270 break;
2271
2272 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
2273 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
2274 if( !(getStyle() & WS_MAXIMIZE) ) {
2275 swp |= MinMaximize(SW_MAXIMIZE, &newPos );
2276 fMinMaxChange = TRUE; //-> invalidate entire window in WM_CALCINVALIDRECT
2277 }
2278 else swp |= SWP_NOSIZE | SWP_NOMOVE;
2279 break;
2280
2281 case SW_SHOWNA:
2282 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
2283 /* fall through */
2284 case SW_SHOW:
2285 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
2286
2287 /*
2288 * ShowWindow has a little peculiar behavior that if the
2289 * window is already the topmost window, it will not
2290 * activate it.
2291 */
2292 if (::GetTopWindow((HWND)0)==getWindowHandle() && (wasVisible || GetActiveWindow() == getWindowHandle()))
2293 swp |= SWP_NOACTIVATE;
2294
2295 break;
2296
2297 case SW_SHOWNOACTIVATE:
2298 swp |= SWP_NOZORDER;
2299 if (GetActiveWindow())
2300 swp |= SWP_NOACTIVATE;
2301 /* fall through */
2302 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
2303 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
2304 case SW_RESTORE:
2305 dprintf(("ShowWindow:restoring window"));
2306
2307 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
2308 if( getStyle() & (WS_MINIMIZE | WS_MAXIMIZE) ) {
2309 swp |= MinMaximize(SW_RESTORE, &newPos );
2310 fMinMaxChange = TRUE; //-> invalidate entire window in WM_CALCINVALIDRECT
2311 }
2312 else swp |= SWP_NOSIZE | SWP_NOMOVE;
2313 break;
2314 }
2315
2316 showFlag = (nCmdShow != SW_HIDE);
2317 if (showFlag != wasVisible)
2318 {
2319 SendMessageA(getWindowHandle(),WM_SHOWWINDOW, showFlag, 0 );
2320 if (!::IsWindow( getWindowHandle() )) goto END;
2321 }
2322
2323 /* We can't activate a child window */
2324 if((getStyle() & WS_CHILD) && !(getExStyle() & WS_EX_MDICHILD))
2325 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
2326
2327 if (!(getStyle() & WS_MINIMIZE)) {
2328 SetWindowPos(HWND_TOP, newPos.left, newPos.top, newPos.right, newPos.bottom, LOWORD(swp));
2329 }
2330 else OSLibWinMinimizeWindow(getOS2FrameWindowHandle());
2331
2332 if(!(swp & SWP_NOACTIVATE) && (!(getStyle() & WS_MINIMIZE))) {
2333 OSLibWinSetActiveWindow(OS2HwndFrame);
2334 }
2335
2336 if (flags & WIN_NEED_SIZE)
2337 {
2338 /* should happen only in CreateWindowEx() */
2339 int wParam = SIZE_RESTORED;
2340
2341 flags &= ~WIN_NEED_SIZE;
2342 if (dwStyle & WS_MAXIMIZE)
2343 wParam = SIZE_MAXIMIZED;
2344 else
2345 if (dwStyle & WS_MINIMIZE)
2346 wParam = SIZE_MINIMIZED;
2347
2348 SendMessageA(getWindowHandle(),WM_SIZE, wParam,
2349 MAKELONG(rectClient.right-rectClient.left,
2350 rectClient.bottom-rectClient.top));
2351 SendMessageA(getWindowHandle(),WM_MOVE,0,MAKELONG(rectClient.left,rectClient.top));
2352 }
2353//testestest
2354 //temporary workaround for file dialogs with template dialog child
2355 //they don't redraw when switching directories
2356 //For some reason the new child's (syslistview32) update rectangle stays
2357 //empty after its parent is made visible with ShowWindow
2358 //TODO: find real cause
2359 if(!wasVisible) {
2360 InvalidateRect(getWindowHandle(), NULL, TRUE);
2361 }
2362//testestest
2363END:
2364 fMinMaxChange = FALSE;
2365 return wasVisible;
2366}
2367//******************************************************************************
2368//******************************************************************************
2369BOOL Win32BaseWindow::SetWindowPos(HWND hwndInsertAfter, int x, int y, int cx, int cy, UINT fuFlags)
2370{
2371 BOOL rc = FALSE;
2372 Win32BaseWindow *window;
2373 HWND hParent = 0;
2374 RECT oldClientRect = rectClient;
2375
2376 if (fuFlags &
2377 ~(SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER |
2378 SWP_NOREDRAW | SWP_NOACTIVATE | SWP_FRAMECHANGED |
2379 SWP_SHOWWINDOW | SWP_HIDEWINDOW | SWP_NOCOPYBITS |
2380 SWP_NOOWNERZORDER | SWP_NOSENDCHANGING | SWP_DEFERERASE |
2381 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE))
2382 {
2383 dprintf(("ERROR: SetWindowPos; UNKNOWN flag"));
2384 return FALSE;
2385 }
2386
2387 if( fuFlags & (SWP_DEFERERASE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)) {
2388 dprintf(("WARNING: SetWindowPos; unsupported flag"));
2389 }
2390
2391 if(IsWindowDestroyed()) {
2392 //changing the position of a window that's being destroyed can cause crashes in PMMERGE
2393 dprintf(("SetWindowPos; window already destroyed"));
2394 return TRUE;
2395 }
2396
2397#if 0
2398 /* Fix redundant flags */
2399 if(getStyle() & WS_VISIBLE) {
2400 fuFlags &= ~SWP_SHOWWINDOW;
2401 }
2402 else
2403 {
2404 if (!(fuFlags & SWP_SHOWWINDOW))
2405 fuFlags |= SWP_NOREDRAW;
2406 fuFlags &= ~SWP_HIDEWINDOW;
2407 }
2408
2409//// if(cx < 0) cx = 0;
2410//// if(cy < 0) cy = 0;
2411
2412 if((rectWindow.right - rectWindow.left == cx) && (rectWindow.bottom - rectWindow.top == cy)) {
2413 fuFlags |= SWP_NOSIZE; /* Already the right size */
2414 }
2415
2416 if((rectWindow.left == x) && (rectWindow.top == y)) {
2417 fuFlags |= SWP_NOMOVE; /* Already the right position */
2418 }
2419
2420 if(getWindowHandle() == GetActiveWindow()) {
2421 fuFlags |= SWP_NOACTIVATE; /* Already active */
2422 }
2423 else
2424 if((getStyle() & (WS_POPUP | WS_CHILD)) != WS_CHILD )
2425 {
2426 if(!(fuFlags & SWP_NOACTIVATE)) /* Bring to the top when activating */
2427 {
2428 fuFlags &= ~SWP_NOZORDER;
2429 hwndInsertAfter = HWND_TOP;
2430 }
2431 }
2432 /* TODO: Check hwndInsertAfter */
2433
2434#endif
2435
2436 //Note: Solitaire crashes when receiving WM_SIZE messages before WM_CREATE
2437 if(state < STATE_POST_WMNCCREATE)
2438 {//don't change size; modify internal structures only
2439 //TODO: not 100% correct yet (activate)
2440 dprintf2(("state < STATE_POST_WMNCCREATE"));
2441 if(!(fuFlags & SWP_NOZORDER)) {
2442 hwndLinkAfter = hwndInsertAfter;
2443 }
2444 if(!(fuFlags & SWP_NOMOVE)) {
2445 rectWindow.bottom = (rectWindow.bottom - rectWindow.top) + y;
2446 rectWindow.top = y;
2447 rectWindow.right = (rectWindow.right - rectWindow.left) + x;
2448 rectWindow.left = x;
2449 }
2450 if(!(fuFlags & SWP_NOSIZE)) {
2451 rectWindow.bottom = rectWindow.top + cy;
2452 rectWindow.right = rectWindow.left + cx;
2453 }
2454 return TRUE;
2455 }
2456
2457 WINDOWPOS wpos;
2458 SWP swp, swpOld;
2459 wpos.flags = fuFlags;
2460 wpos.cy = cy;
2461 wpos.cx = cx;
2462 wpos.x = x;
2463 wpos.y = y;
2464 wpos.hwndInsertAfter = hwndInsertAfter;
2465 wpos.hwnd = getWindowHandle();
2466
2467 if(~fuFlags & (SWP_NOMOVE | SWP_NOSIZE))
2468 {
2469 if (isChild())
2470 {
2471 if(!getParent()) {
2472 dprintf(("WARNING: Win32BaseWindow::SetWindowPos window %x is child but has no parent!!", getWindowHandle()));
2473 }
2474 }
2475 OSLibWinQueryWindowPos(OS2HwndFrame, &swpOld);
2476 }
2477
2478 if(getParent()) {
2479 OSLibMapWINDOWPOStoSWP(&wpos, &swp, &swpOld, getParent()->getClientHeight(),
2480 OS2HwndFrame);
2481 }
2482 else OSLibMapWINDOWPOStoSWP(&wpos, &swp, &swpOld, OSLibQueryScreenHeight(), OS2HwndFrame);
2483
2484 if (swp.fl == 0) {
2485 dprintf2(("swp.fl == 0"));
2486 if(fuFlags & SWP_FRAMECHANGED)
2487 {
2488 NotifyFrameChanged(&wpos, &oldClientRect);
2489 }
2490 return TRUE;
2491 }
2492
2493// if ((swp.fl & SWPOS_ZORDER) && (swp.hwndInsertBehind > HWNDOS_BOTTOM))
2494 if ((swp.hwndInsertBehind > HWNDOS_BOTTOM))
2495 {
2496 Win32BaseWindow *wndBehind = Win32BaseWindow::GetWindowFromHandle(swp.hwndInsertBehind);
2497 if(wndBehind) {
2498 swp.hwndInsertBehind = wndBehind->getOS2FrameWindowHandle();
2499 RELEASE_WNDOBJ(wndBehind);
2500 }
2501 else {
2502 dprintf(("ERROR: SetWindowPos: hwndInsertBehind %x invalid!",swp.hwndInsertBehind));
2503 swp.hwndInsertBehind = 0;
2504 }
2505 }
2506 swp.hwnd = OS2HwndFrame;
2507
2508 if(fuFlags & SWP_SHOWWINDOW && !IsWindowVisible(getWindowHandle())) {
2509 setStyle(getStyle() | WS_VISIBLE);
2510 if(hTaskList) {
2511 dprintf(("Adding window %x to tasklist", getWindowHandle()));
2512 OSLibWinChangeTaskList(hTaskList, OS2HwndFrame, getWindowNameA(), 1);
2513 }
2514 }
2515 else
2516 if((fuFlags & SWP_HIDEWINDOW) && IsWindowVisible(getWindowHandle())) {
2517 setStyle(getStyle() & ~WS_VISIBLE);
2518 if(hTaskList && !(getStyle() & WS_MINIMIZE)) {
2519 dprintf(("Removing window %x from tasklist", getWindowHandle()));
2520 OSLibWinChangeTaskList(hTaskList, OS2HwndFrame, getWindowNameA(), 0);
2521 }
2522 }
2523 dprintf (("WinSetWindowPos %x %x (%d,%d)(%d,%d) %x", swp.hwnd, swp.hwndInsertBehind, swp.x, swp.y, swp.cx, swp.cy, swp.fl));
2524 rc = OSLibWinSetMultWindowPos(&swp, 1);
2525
2526 if(rc == FALSE)
2527 {
2528 dprintf(("OSLibWinSetMultWindowPos failed! Error %x",OSLibWinGetLastError()));
2529 return 0;
2530 }
2531
2532 if((fuFlags & SWP_FRAMECHANGED) && (fuFlags & (SWP_NOMOVE | SWP_NOSIZE) == (SWP_NOMOVE | SWP_NOSIZE)))
2533 {
2534 NotifyFrameChanged(&wpos, &oldClientRect);
2535 }
2536 if(!(getStyle() & (WS_MAXIMIZE|WS_MINIMIZE))) {
2537 //Restore position always changes when the window position is changed
2538 dprintf(("Save new restore position (%d,%d)(%d,%d)", rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
2539 windowpos.rcNormalPosition = rectWindow;
2540 }
2541 return (rc);
2542}
2543//******************************************************************************
2544//Called by ScrollWindowEx (dc.cpp) to notify child window that it has moved
2545//******************************************************************************
2546BOOL Win32BaseWindow::ScrollWindow(int dx, int dy)
2547{
2548 rectWindow.left += dx;
2549 rectWindow.right += dx;
2550 rectWindow.top += dy;
2551 rectWindow.bottom += dy;
2552 SendMessageA(getWindowHandle(),WM_MOVE, 0, MAKELONG(rectClient.left, rectClient.top));
2553 return TRUE;
2554}
2555//******************************************************************************
2556//******************************************************************************
2557void Win32BaseWindow::NotifyFrameChanged(WINDOWPOS *wpos, RECT *oldClientRect)
2558{
2559 HRGN hrgn, hrgnClient;
2560 RECT rect;
2561
2562 MsgFormatFrame(NULL);
2563
2564 if(RECT_WIDTH(rectClient) != RECT_WIDTH(*oldClientRect) ||
2565 RECT_HEIGHT(rectClient) != RECT_HEIGHT(*oldClientRect))
2566 {
2567 wpos->flags &= ~(SWP_NOSIZE|SWP_NOCLIENTSIZE);
2568 wpos->cx = RECT_WIDTH(rectWindow);
2569 wpos->cy = RECT_HEIGHT(rectWindow);
2570 }
2571
2572 if(rectClient.left != oldClientRect->left ||
2573 rectClient.top != oldClientRect->top)
2574 {
2575 wpos->flags &= ~(SWP_NOMOVE|SWP_NOCLIENTMOVE);
2576 wpos->x = rectWindow.left;
2577 wpos->y = rectWindow.top;
2578 }
2579
2580 WINDOWPOS wpOld = *wpos;
2581 if(!(wpos->flags & SWP_NOSENDCHANGING))
2582 SendMessageA(getWindowHandle(),WM_WINDOWPOSCHANGING, 0, (LPARAM)wpos);
2583
2584 if ((wpos->hwndInsertAfter != wpOld.hwndInsertAfter) ||
2585 (wpos->x != wpOld.x) || (wpos->y != wpOld.y) || (wpos->cx != wpOld.cx) || (wpos->cy != wpOld.cy) || (wpos->flags != wpOld.flags))
2586 {
2587 dprintf(("WARNING, NotifyFrameChanged: TODO -> adjust flags!!!!"));
2588 SetWindowPos(wpos->hwndInsertAfter, wpos->x, wpos->y, wpos->cx, wpos->cy, wpos->flags | SWP_NOSENDCHANGING);
2589 }
2590 else SendMessageA(getWindowHandle(),WM_WINDOWPOSCHANGED, 0, (LPARAM)wpos);
2591
2592 //Calculate invalid areas
2593 rect = rectWindow;
2594 OffsetRect(&rect, -rectWindow.left, -rectWindow.top);
2595 hrgn = CreateRectRgnIndirect(&rect);
2596 if (!hrgn) {
2597 dprintf(("ERROR: NotifyFrameChanged, CreateRectRgnIndirect failed!!"));
2598 return;
2599 }
2600 rect = rectClient;
2601 hrgnClient = CreateRectRgnIndirect(&rect);
2602 if (!hrgn) {
2603 dprintf(("ERROR: NotifyFrameChanged, CreateRectRgnIndirect failed!!"));
2604 return;
2605 }
2606 CombineRgn(hrgn, hrgn, hrgnClient, RGN_DIFF);
2607 DeleteObject(hrgnClient);
2608
2609 if(!EqualRect(oldClientRect, &rectClient)) {
2610 UnionRect(oldClientRect, oldClientRect, &rectClient);
2611 hrgnClient = CreateRectRgnIndirect(oldClientRect);
2612 if (!hrgn) {
2613 dprintf(("ERROR: NotifyFrameChanged, CreateRectRgnIndirect failed!!"));
2614 return;
2615 }
2616 CombineRgn(hrgn, hrgn, hrgnClient, RGN_OR);
2617 DeleteObject(hrgnClient);
2618 }
2619 RedrawWindow(getWindowHandle(), NULL, hrgn, RDW_ALLCHILDREN |
2620 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME);
2621 DeleteObject(hrgn);
2622}
2623//******************************************************************************
2624//TODO: Check how this api really works in NT
2625//******************************************************************************
2626BOOL Win32BaseWindow::SetWindowPlacement(WINDOWPLACEMENT *wndpl)
2627{
2628 dprintf(("SetWindowPlacement %x min (%d,%d)", getWindowHandle(), wndpl->ptMinPosition.x, wndpl->ptMinPosition.y));
2629 dprintf(("SetWindowPlacement %x max (%d,%d)", getWindowHandle(), wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y));
2630 dprintf(("SetWindowPlacement %x norm (%d,%d)(%d,%d)", getWindowHandle(), wndpl->rcNormalPosition.left, wndpl->rcNormalPosition.top, wndpl->rcNormalPosition.right, wndpl->rcNormalPosition.bottom));
2631 windowpos.ptMinPosition = wndpl->ptMinPosition;
2632 windowpos.ptMaxPosition = wndpl->ptMaxPosition;
2633 windowpos.rcNormalPosition = wndpl->rcNormalPosition;
2634
2635 if(getStyle() & WS_MINIMIZE )
2636 {
2637 //TODO: Why can't this be (0,0)?
2638 if(wndpl->flags & WPF_SETMINPOSITION && !(!windowpos.ptMinPosition.x && !windowpos.ptMinPosition.y)) {
2639 SetWindowPos(0, windowpos.ptMinPosition.x, windowpos.ptMinPosition.y,
2640 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2641 }
2642 }
2643 else
2644 if(getStyle() & WS_MAXIMIZE )
2645 {
2646 //TODO: Why can't this be (0,0)?
2647 if(windowpos.ptMaxPosition.x != 0 || windowpos.ptMaxPosition.y != 0 )
2648 SetWindowPos(0, windowpos.ptMaxPosition.x, windowpos.ptMaxPosition.y,
2649 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
2650 }
2651 else {
2652 SetWindowPos(0, windowpos.rcNormalPosition.left, windowpos.rcNormalPosition.top,
2653 windowpos.rcNormalPosition.right - windowpos.rcNormalPosition.left,
2654 windowpos.rcNormalPosition.bottom - windowpos.rcNormalPosition.top,
2655 SWP_NOZORDER | SWP_NOACTIVATE );
2656 }
2657 ShowWindow(wndpl->showCmd);
2658 if( ::IsWindow(getWindowHandle()) && getStyle() & WS_MINIMIZE )
2659 {
2660 /* SDK: ...valid only the next time... */
2661 if(wndpl->flags & WPF_RESTORETOMAXIMIZED)
2662 setFlags(getFlags() | WIN_RESTORE_MAX);
2663 }
2664 return TRUE;
2665}
2666//******************************************************************************
2667//******************************************************************************
2668BOOL Win32BaseWindow::GetWindowPlacement(LPWINDOWPLACEMENT wndpl)
2669{
2670 wndpl->length = sizeof(*wndpl);
2671 if(getStyle() & WS_MINIMIZE )
2672 wndpl->showCmd = SW_SHOWMINIMIZED;
2673 else wndpl->showCmd = (getStyle() & WS_MAXIMIZE) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL;
2674
2675 //TODO: Verify if this is correct -> SDK docs claim this flag must always be set to 0
2676 if(getFlags() & WIN_RESTORE_MAX )
2677 wndpl->flags = WPF_RESTORETOMAXIMIZED;
2678 else wndpl->flags = 0;
2679
2680 wndpl->ptMinPosition = windowpos.ptMinPosition;
2681 wndpl->ptMaxPosition = windowpos.ptMaxPosition;
2682 //Must be in parent coordinates (or screen if no parent); verified in NT4, SP6
2683 wndpl->rcNormalPosition = windowpos.rcNormalPosition;
2684
2685 return TRUE;
2686}
2687//******************************************************************************
2688//Also destroys all the child windows (destroy children first, parent last)
2689//TODO: Don't rely on PM to do the right thing. Send WM_(NC)DESTROY &
2690// destroy children ourselves (see Wine)
2691//******************************************************************************
2692BOOL Win32BaseWindow::DestroyWindow()
2693{
2694 HWND hwnd = getWindowHandle();
2695
2696 dprintf(("DestroyWindow %x", hwnd));
2697
2698#if 0
2699 /* Look whether the focus is within the tree of windows we will
2700 * be destroying.
2701 */
2702 HWND hwndFocus = GetFocus();
2703 if (hwndFocus == hwnd || ::IsChild( hwnd, hwndFocus ))
2704 {
2705 HWND parent = GetAncestor( hwnd, GA_PARENT );
2706 if (parent == GetDesktopWindow()) parent = 0;
2707 SetFocus( parent );
2708 }
2709#endif
2710 /* Call hooks */
2711 if(HOOK_CallHooksA( WH_CBT, HCBT_DESTROYWND, getWindowHandle(), 0L))
2712 {
2713 return FALSE;
2714 }
2715
2716 if(!(getStyle() & WS_CHILD) && getOwner() == NULL)
2717 {
2718 HOOK_CallHooksA(WH_SHELL, HSHELL_WINDOWDESTROYED, getWindowHandle(), 0L);
2719 /* FIXME: clean up palette - see "Internals" p.352 */
2720 }
2721
2722 if((getStyle() & WS_CHILD) && !(getExStyle() & WS_EX_NOPARENTNOTIFY))
2723 {
2724 if(getParent() && getParent()->IsWindowDestroyed() == FALSE)
2725 {
2726 /* Notify the parent window only */
2727 SendMessageA(getParent()->getWindowHandle(), WM_PARENTNOTIFY, MAKEWPARAM(WM_DESTROY, getWindowId()), (LPARAM)getWindowHandle());
2728 if(!::IsWindow(hwnd) )
2729 {
2730 return TRUE;
2731 }
2732 }
2733//// else DebugInt3();
2734 }
2735 /* Hide the window */
2736 if(IsWindowVisible(getWindowHandle()))
2737 {
2738 SetWindowPos(0, 0, 0, 0, 0, SWP_HIDEWINDOW |
2739 SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE);
2740 if(!::IsWindow(hwnd))
2741 {
2742 return TRUE;
2743 }
2744 }
2745 dprintf(("DestroyWindow %x -> HIDDEN", hwnd));
2746
2747 // check the handle for the last active popup window
2748 Win32BaseWindow* owner = getOwner();
2749 if (NULL != owner)
2750 {
2751 if (owner->getLastActive() == hwnd)
2752 owner->setLastActive( owner->getWindowHandle() );
2753
2754 //SvL: Not sure this is correct, but it solves the problem of reassigning
2755 // activation. A disabled window will never be activated ->
2756 // possible that the wrong window is chosen by PM.
2757 // PM chooses another window to be activated before WM_DESTROY is
2758 // sent. VPC enables the owner when it receives that message,
2759 // but by then it's too late.
2760 // (MFC created modeless dialog)
2761 //TODO: This might be the wrong place to do it. EndDialog is called,
2762 // so perhaps it should be done there. (although Wine only
2763 // enables the owner if the dialog is modal)
2764 ::EnableWindow(owner->getWindowHandle(), 1);
2765 }
2766
2767 fDestroyWindowCalled = TRUE;
2768
2769//hack alert; PM crashes if child calls DestroyWindow for parent/owner in WM_DESTROY
2770// handler; must postpone it
2771 if(fChildDestructionInProgress) {
2772 dprintf(("Postponing parent destruction because of dying child"));
2773 OSLibPostMessageDirect(OS2HwndFrame, WIN32APP_POSTPONEDESTROY, 0, 0);
2774 return TRUE;
2775 }
2776
2777 BOOL fOldChildDestructionInProgress = FALSE;
2778 if(GetParent()) {
2779 Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(GetParent());
2780 if(window) {
2781 fOldChildDestructionInProgress = window->IsChildDestructionInProgress();
2782 window->SetChildDestructionInProgress(TRUE);
2783 RELEASE_WNDOBJ(window);
2784 }
2785 }
2786//hack end
2787
2788 BOOL ret = OSLibWinDestroyWindow(OS2HwndFrame);
2789
2790//hack alert; PM crashes if child calls DestroyWindow for parent/owner in WM_DESTROY
2791// handler; must postpone it
2792 if(GetParent()) {
2793 Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(GetParent());
2794 if(window) {
2795 window->SetChildDestructionInProgress(fOldChildDestructionInProgress);
2796 RELEASE_WNDOBJ(window);
2797 }
2798 }
2799//hack end
2800 return ret;
2801}
2802//******************************************************************************
2803//******************************************************************************
2804Win32BaseWindow *Win32BaseWindow::getParent()
2805{
2806 Win32BaseWindow *wndparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
2807 //experiment
2808#if 0
2809 return ((ULONG)wndparent == (ULONG)windowDesktop) ? NULL : wndparent;
2810#else
2811 return wndparent;
2812#endif
2813}
2814//******************************************************************************
2815//Note: does not set last error if no parent (verified in NT4, SP6)
2816//******************************************************************************
2817HWND Win32BaseWindow::GetParent()
2818{
2819 Win32BaseWindow *wndparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
2820
2821 if(getStyle() & WS_CHILD)
2822 {
2823 if(wndparent) {
2824 return wndparent->getWindowHandle();
2825 }
2826 dprintf(("WARNING: GetParent: WS_CHILD but no parent!!"));
2827 DebugInt3();
2828 return 0;
2829 }
2830 else
2831 if(getStyle() & WS_POPUP)
2832 return (getOwner()) ? getOwner()->getWindowHandle() : 0;
2833 else return 0;
2834}
2835//******************************************************************************
2836//******************************************************************************
2837HWND Win32BaseWindow::SetParent(HWND hwndNewParent)
2838{
2839 HWND oldhwnd;
2840 Win32BaseWindow *newparent;
2841 Win32BaseWindow *oldparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
2842 BOOL fShow = FALSE;
2843
2844 if(oldparent) {
2845 oldhwnd = oldparent->getWindowHandle();
2846 oldparent->removeChild(this);
2847 }
2848 else oldhwnd = 0;
2849
2850 /* Windows hides the window first, then shows it again
2851 * including the WM_SHOWWINDOW messages and all */
2852 if(IsWindowCreated() && (getStyle() & WS_VISIBLE)) {
2853 ShowWindow(SW_HIDE);
2854 fShow = TRUE;
2855 }
2856 if(oldparent) {
2857 //release parent here (increased refcount during creation)
2858 RELEASE_WNDOBJ(oldparent);
2859 }
2860 newparent = GetWindowFromHandle(hwndNewParent);
2861 if(newparent && !newparent->isDesktopWindow())
2862 {
2863 setParent(newparent);
2864 getParent()->addChild(this);
2865 fParentChange = TRUE;
2866 // in case we haven't finished creating the window whose parent we're
2867 // setting here, the OS/2 HWND might not exist yet and we call the PM
2868 // API with a NULLHANDLE - no problem
2869 // when we create the OS/2 window lateron, we will create it with the
2870 // right parent anyway.
2871 OSLibWinSetParent(getOS2FrameWindowHandle(), getParent()->getOS2WindowHandle());
2872 if(!(getStyle() & WS_CHILD))
2873 {
2874 if(getWindowId())
2875 {
2876 DestroyMenu( (HMENU) getWindowId() );
2877 setWindowId(0);
2878 }
2879 }
2880 //SvL: Even though the win32 coordinates might not change, the PM
2881 // coordinates can. We must make sure the control stays at the
2882 // same position (y) relative to the (new) parent.
2883 // TODO: shouldn't we check the state of the window and not do it in INIT state?
2884 SetWindowPos(HWND_TOPMOST, rectWindow.left, rectWindow.top, 0, 0,
2885 SWP_NOACTIVATE|SWP_NOSIZE);
2886 fParentChange = FALSE;
2887 }
2888 else {
2889 if(newparent) RELEASE_WNDOBJ(newparent);
2890
2891 setParent(windowDesktop);
2892 windowDesktop->addRef();
2893 windowDesktop->addChild(this);
2894 OSLibWinSetParent(getOS2FrameWindowHandle(), OSLIB_HWND_DESKTOP);
2895
2896 setWindowId(0);
2897 }
2898 /* SetParent additionally needs to make hwndChild the topmost window
2899 in the x-order and send the expected WM_WINDOWPOSCHANGING and
2900 WM_WINDOWPOSCHANGED notification messages.
2901 */
2902 if(state >= STATE_PRE_WMNCCREATE) {
2903 SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0,
2904 SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|(fShow? SWP_SHOWWINDOW : 0));
2905
2906 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
2907 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
2908 }
2909 return oldhwnd;
2910}
2911//******************************************************************************
2912//******************************************************************************
2913BOOL Win32BaseWindow::IsChild(HWND hwndParent)
2914{
2915 // PH: Optimizer won't unroll calls to getParent() even
2916 // in release build.
2917 Win32BaseWindow *_parent = getParent();
2918
2919 if(_parent)
2920 {
2921 if(_parent->getWindowHandle() == hwndParent)
2922 return TRUE;
2923
2924 return _parent->IsChild(hwndParent);
2925 }
2926 else
2927 return 0;
2928}
2929//******************************************************************************
2930//******************************************************************************
2931HWND Win32BaseWindow::GetTopWindow()
2932{
2933 HWND hwndTop;
2934 Win32BaseWindow *topwindow;
2935
2936 hwndTop = OSLibWinQueryWindow(getOS2WindowHandle(), QWOS_TOP);
2937 if(!isDesktopWindow())
2938 {
2939 topwindow = GetWindowFromOS2FrameHandle(hwndTop);
2940 //Note: GetTopWindow can't return a window that hasn't processed
2941 // WM_NCCREATE yet (verified in NT4, SP6)
2942 if(topwindow) {
2943 if(topwindow->state >= STATE_POST_WMNCCREATE) {
2944 hwndTop = topwindow->getWindowHandle();
2945 }
2946 else hwndTop = topwindow->GetWindow(GW_HWNDNEXT);
2947 RELEASE_WNDOBJ(topwindow);
2948 return hwndTop;
2949 }
2950 if(topwindow) RELEASE_WNDOBJ(topwindow);
2951 return 0;
2952 }
2953 while(hwndTop) {
2954 topwindow = GetWindowFromOS2FrameHandle(hwndTop);
2955 //Note: GetTopWindow can't return a window that hasn't processed
2956 // WM_NCCREATE yet (verified in NT4, SP6)
2957 if(topwindow) {
2958 if(topwindow->state >= STATE_POST_WMNCCREATE) {
2959 hwndTop = topwindow->getWindowHandle();
2960 }
2961 else hwndTop = topwindow->GetWindow(GW_HWNDNEXT);
2962 RELEASE_WNDOBJ(topwindow);
2963 return hwndTop;
2964 }
2965 if(topwindow) RELEASE_WNDOBJ(topwindow);
2966 hwndTop = OSLibWinQueryWindow(hwndTop, QWOS_NEXT);
2967 }
2968
2969 return 0;
2970}
2971//******************************************************************************
2972// Get the top-level parent for a child window.
2973//******************************************************************************
2974HWND Win32BaseWindow::GetTopParent()
2975{
2976 Win32BaseWindow *window = this;
2977 HWND hwndTopParent = 0;
2978
2979 lock();
2980 while(window && (window->getStyle() & WS_CHILD))
2981 {
2982 window = window->getParent();
2983 }
2984 if(window) {
2985 hwndTopParent = window->getWindowHandle();
2986 }
2987 unlock();
2988 return hwndTopParent;
2989}
2990//******************************************************************************
2991//TODO: Should not enumerate children that are created during the enumeration!
2992//TODO: Do this more efficiently
2993//******************************************************************************
2994BOOL Win32BaseWindow::EnumChildWindows(WNDENUMPROC lpfn, LPARAM lParam)
2995{
2996 BOOL rc = TRUE;
2997 HWND hwnd;
2998 Win32BaseWindow *prevchild = 0, *child = 0;
2999
3000 dprintf(("EnumChildWindows of %x parameter %x %x (%x)", getWindowHandle(), lpfn, lParam, getFirstChild()));
3001 lock();
3002 for (child = (Win32BaseWindow *)getFirstChild(); child != NULL; child = (Win32BaseWindow *)child->getNextChild())
3003 {
3004 dprintf(("EnumChildWindows: enumerating child %x (owner %x; parent %x)", child->getWindowHandle(), (child->getOwner()) ? child->getOwner()->getWindowHandle() : 0, getWindowHandle()));
3005 hwnd = child->getWindowHandle();
3006 if(child->IsWindowDestroyed() || child->getOwner()) {
3007 continue; //shouldn't have an owner (Wine)
3008 }
3009 child->addRef();
3010 unlock();
3011 if(lpfn(hwnd, lParam) == FALSE)
3012 {
3013 child->release();
3014 return FALSE;
3015 }
3016 child->release();
3017 lock();
3018 //check if the window still exists
3019 if(!::IsWindow(hwnd))
3020 {
3021 child = prevchild;
3022 if(child == NULL) break;
3023 continue;
3024 }
3025 if(child->getFirstChild() != NULL)
3026 {
3027 dprintf(("EnumChildWindows: Enumerate children of %x", child->getWindowHandle()));
3028 child->addRef();
3029 unlock();
3030 if(child->EnumChildWindows(lpfn, lParam) == FALSE)
3031 {
3032 child->release();
3033 return FALSE;
3034 }
3035 child->release();
3036 lock();
3037 }
3038 prevchild = child;
3039 }
3040 unlock();
3041 return rc;
3042}
3043//******************************************************************************
3044//Enumerate first-level children only and check thread id
3045//******************************************************************************
3046BOOL Win32BaseWindow::EnumThreadWindows(DWORD dwThreadId, WNDENUMPROC lpfn, LPARAM lParam)
3047{
3048 Win32BaseWindow *child = 0;
3049 ULONG tid, pid;
3050 BOOL rc;
3051 HWND hwnd;
3052
3053 dprintf(("EnumThreadWindows %x %x %x", dwThreadId, lpfn, lParam));
3054
3055 for (child = (Win32BaseWindow *)getFirstChild(); child; child = (Win32BaseWindow *)child->getNextChild())
3056 {
3057 OSLibWinQueryWindowProcess(child->getOS2WindowHandle(), &pid, &tid);
3058
3059 if(dwThreadId == tid) {
3060 dprintf2(("EnumThreadWindows: Found Window %x", child->getWindowHandle()));
3061 if((rc = lpfn(child->getWindowHandle(), lParam)) == FALSE) {
3062 break;
3063 }
3064 }
3065 }
3066 return TRUE;
3067}
3068//******************************************************************************
3069//Enumerate first-level children only
3070//******************************************************************************
3071BOOL Win32BaseWindow::EnumWindows(WNDENUMPROC lpfn, LPARAM lParam)
3072{
3073 Win32BaseWindow *window;
3074 BOOL rc;
3075 HWND hwnd = WNDHANDLE_MAGIC_HIGHWORD;
3076 DWORD dwStyle;
3077
3078 dprintf(("EnumWindows %x %x", lpfn, lParam));
3079
3080 for(int i=0;i<MAX_WINDOW_HANDLES;i++)
3081 {
3082 window = Win32BaseWindow::GetWindowFromHandle(hwnd);
3083 if(window) {
3084 if(window->getWindowHandle() != hwnd) {
3085 dprintf(("CORRUPT WINDOW %x %x", window, hwnd));
3086 }
3087 RELEASE_WNDOBJ(window);
3088 dwStyle = ::GetWindowLongA(hwnd, GWL_STYLE);
3089 if ((dwStyle & WS_POPUP) || ((dwStyle & WS_CAPTION) == WS_CAPTION))
3090 {
3091 dprintf2(("EnumWindows: Found Window %x", hwnd));
3092 if((rc = lpfn(hwnd, lParam)) == FALSE) {
3093 break;
3094 }
3095 }
3096 }
3097 hwnd++;
3098 }
3099 return TRUE;
3100}
3101//******************************************************************************
3102//******************************************************************************
3103HWND Win32BaseWindow::FindWindowById(int id)
3104{
3105 HWND hwnd;
3106
3107 lock();
3108 for (Win32BaseWindow *child = (Win32BaseWindow *)getFirstChild(); child; child = (Win32BaseWindow *)child->getNextChild())
3109 {
3110 if (child->getWindowId() == id)
3111 {
3112 hwnd = child->getWindowHandle();
3113 unlock();
3114 return hwnd;
3115 }
3116 }
3117 unlock();
3118 return 0;
3119}
3120//******************************************************************************
3121//TODO:
3122//We assume (for now) that if hwndParent or hwndChildAfter are real window handles, that
3123//the current process owns them.
3124//******************************************************************************
3125HWND Win32BaseWindow::FindWindowEx(HWND hwndParent, HWND hwndChildAfter, ATOM atom, LPSTR lpszWindow)
3126{
3127 Win32BaseWindow *parent = GetWindowFromHandle(hwndParent);
3128 Win32BaseWindow *child = GetWindowFromHandle(hwndChildAfter);
3129 Win32BaseWindow *firstchild = child;
3130
3131 dprintf(("FindWindowEx %x %x %x %s", hwndParent, hwndChildAfter, atom, lpszWindow));
3132 if((hwndParent != 0 && !parent) ||
3133 (hwndChildAfter != 0 && !child) ||
3134 (hwndParent == 0 && hwndChildAfter != 0))
3135 {
3136 if(parent) RELEASE_WNDOBJ(parent);
3137 if(firstchild) RELEASE_WNDOBJ(firstchild);
3138 dprintf(("Win32BaseWindow::FindWindowEx: parent or child not found %x %x", hwndParent, hwndChildAfter));
3139 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
3140 return 0;
3141 }
3142 SetLastError(0);
3143 if(hwndParent != 0)
3144 {//if the current process owns the window, just do a quick search
3145 lock(&critsect);
3146 child = (Win32BaseWindow *)parent->getFirstChild();
3147 if(hwndChildAfter != 0)
3148 {
3149 while(child)
3150 {
3151 if(child->getWindowHandle() == hwndChildAfter)
3152 {
3153 child = (Win32BaseWindow *)child->getNextChild();
3154 break;
3155 }
3156 child = (Win32BaseWindow *)child->getNextChild();
3157 }
3158 }
3159 while(child)
3160 {
3161 //According to Wine, the class doesn't need to be specified
3162 if((!atom || child->getWindowClass()->getAtom() == atom) &&
3163 (!lpszWindow || child->hasWindowName(lpszWindow)))
3164 {
3165 dprintf(("FindWindowEx: Found window %x", child->getWindowHandle()));
3166 HWND hwndChild = child->getWindowHandle();
3167 unlock(&critsect);
3168 if(parent) RELEASE_WNDOBJ(parent);
3169 if(firstchild) RELEASE_WNDOBJ(firstchild);
3170 dprintf(("FindWindowEx: Found window %x", child->getWindowHandle()));
3171 return hwndChild;
3172 }
3173 child = (Win32BaseWindow *)child->getNextChild();
3174 }
3175 unlock(&critsect);
3176 if(parent) RELEASE_WNDOBJ(parent);
3177 if(firstchild) RELEASE_WNDOBJ(firstchild);
3178 }
3179 else {
3180 Win32BaseWindow *wnd;
3181 HWND henum, hwnd;
3182
3183 henum = OSLibWinBeginEnumWindows(OSLIB_HWND_DESKTOP);
3184 hwnd = OSLibWinGetNextWindow(henum);
3185
3186 while(hwnd)
3187 {
3188 wnd = GetWindowFromOS2FrameHandle(hwnd);
3189 if(wnd == NULL) {
3190 hwnd = OSLibWinQueryClientWindow(hwnd);
3191 if(hwnd) wnd = GetWindowFromOS2Handle(hwnd);
3192 }
3193
3194 if(wnd) {
3195 //According to Wine, the class doesn't need to be specified
3196 if((!atom || wnd->getWindowClass()->getAtom() == atom) &&
3197 (!lpszWindow || wnd->hasWindowName(lpszWindow)))
3198 {
3199 OSLibWinEndEnumWindows(henum);
3200 dprintf(("FindWindowEx: Found window %x", wnd->getWindowHandle()));
3201 HWND hwndret = wnd->getWindowHandle();
3202 RELEASE_WNDOBJ(wnd);
3203 return hwndret;
3204 }
3205 RELEASE_WNDOBJ(wnd);
3206 }
3207 hwnd = OSLibWinGetNextWindow(henum);
3208 }
3209 OSLibWinEndEnumWindows(henum);
3210 if(parent) RELEASE_WNDOBJ(parent);
3211 if(firstchild) RELEASE_WNDOBJ(firstchild);
3212 }
3213 SetLastError(ERROR_CANNOT_FIND_WND_CLASS); //TODO: not always correct
3214 return 0;
3215}
3216//******************************************************************************
3217//******************************************************************************
3218HWND Win32BaseWindow::GetWindow(UINT uCmd)
3219{
3220 HWND hwndRelated = 0;
3221 Win32BaseWindow *window;
3222
3223 switch(uCmd)
3224 {
3225 case GW_HWNDFIRST:
3226 window = (Win32BaseWindow *)getParent();
3227 if(window)
3228 {
3229 hwndRelated = OSLibWinQueryWindow(window->getOS2WindowHandle(), QWOS_TOP);
3230 window = GetWindowFromOS2FrameHandle(hwndRelated);
3231 if(window) {
3232 hwndRelated = window->getWindowHandle();
3233 RELEASE_WNDOBJ(window);
3234 }
3235 else hwndRelated = 0;
3236 }
3237 else {
3238 dprintf(("WARNING: GW_HWNDFIRST not correctly implemented for toplevel/most windows!"));
3239 hwndRelated = 0; //TODO: not correct; should get first child in z-order of desktop
3240 }
3241 break;
3242
3243 case GW_HWNDLAST:
3244 window = (Win32BaseWindow *)getParent();
3245 if(window) {
3246 hwndRelated = OSLibWinQueryWindow(window->getOS2WindowHandle(), QWOS_BOTTOM);
3247 dprintf(("os2 handle %x", hwndRelated));
3248 window = GetWindowFromOS2FrameHandle(hwndRelated);
3249 if(window) {
3250 hwndRelated = window->getWindowHandle();
3251 RELEASE_WNDOBJ(window);
3252 }
3253 else hwndRelated = 0;
3254 }
3255 else {
3256 dprintf(("WARNING: GW_HWNDLAST not correctly implemented for toplevel/most windows!"));
3257 hwndRelated = 0; //TODO: not correct; should get first child in z-order of desktop
3258 }
3259 break;
3260
3261 case GW_HWNDNEXT:
3262 if(getParent()) {
3263 hwndRelated = OSLibWinQueryWindow(getOS2FrameWindowHandle(), QWOS_NEXT);
3264 window = GetWindowFromOS2FrameHandle(hwndRelated);
3265 if(window) {
3266 hwndRelated = window->getWindowHandle();
3267 RELEASE_WNDOBJ(window);
3268 }
3269 else hwndRelated = 0;
3270 }
3271 else {
3272 dprintf(("WARNING: GW_HWNDNEXT not correctly implemented for toplevel/most windows!"));
3273 hwndRelated = 0; //TODO: not correct; should get first child in z-order of desktop
3274 }
3275 break;
3276
3277 case GW_HWNDPREV:
3278 if(getParent()) {
3279 hwndRelated = OSLibWinQueryWindow(getOS2FrameWindowHandle(), QWOS_PREV);
3280 window = GetWindowFromOS2FrameHandle(hwndRelated);
3281 if(window) {
3282 hwndRelated = window->getWindowHandle();
3283 RELEASE_WNDOBJ(window);
3284 }
3285 else hwndRelated = 0;
3286 }
3287 else {
3288 dprintf(("WARNING: GW_HWNDPREV not correctly implemented for toplevel/most windows!"));
3289 hwndRelated = 0; //TODO: not correct; should get first child in z-order of desktop
3290 }
3291 break;
3292
3293 case GW_OWNER:
3294 {
3295 Win32BaseWindow *owner = getOwner();
3296 if(owner) {
3297 hwndRelated = owner->getWindowHandle();
3298 }
3299 break;
3300 }
3301
3302 case GW_CHILD:
3303 hwndRelated = OSLibWinQueryWindow(getOS2WindowHandle(), QWOS_TOP);
3304 window = GetWindowFromOS2FrameHandle(hwndRelated);
3305
3306 //Before a window has processed WM_NCCREATE:
3307 //- GetWindow(parent, GW_CHILD) can't return that window handle
3308 //(verified in NT4, SP6)
3309 if(window) {
3310 if(window->state >= STATE_POST_WMNCCREATE) {
3311 hwndRelated = window->getWindowHandle();
3312 RELEASE_WNDOBJ(window);
3313 }
3314 else {
3315 hwndRelated = window->GetWindow(GW_HWNDNEXT);
3316 RELEASE_WNDOBJ(window);
3317 }
3318 }
3319 else hwndRelated = 0;
3320
3321 break;
3322
3323 //for internal use only
3324 case GW_HWNDNEXTCHILD:
3325 lock();
3326 window = (Win32BaseWindow *)getNextChild();
3327 if(window) {
3328 hwndRelated = window->getWindowHandle();
3329 }
3330 else hwndRelated = 0;
3331 unlock();
3332 break;
3333
3334 case GW_HWNDPREVCHILD:
3335 DebugInt3();
3336 break;
3337
3338 case GW_HWNDFIRSTCHILD:
3339 lock();
3340 window = (Win32BaseWindow *)getFirstChild();
3341 if(window) {
3342 hwndRelated = window->getWindowHandle();
3343 }
3344 else hwndRelated = 0;
3345 unlock();
3346 break;
3347
3348 case GW_HWNDLASTCHILD:
3349 lock();
3350 window = (Win32BaseWindow *)getFirstChild();
3351 if(window) {
3352 while (window->getNextChild())
3353 {
3354 window = (Win32BaseWindow *)window->getNextChild();
3355 }
3356 hwndRelated = window->getWindowHandle();
3357 }
3358 else hwndRelated = 0;
3359 unlock();
3360 break;
3361 }
3362end:
3363 dprintf(("GetWindow %x %d returned %x", getWindowHandle(), uCmd, hwndRelated));
3364 return hwndRelated;
3365}
3366//******************************************************************************
3367//******************************************************************************
3368HWND Win32BaseWindow::SetActiveWindow()
3369{
3370 HWND hwndActive;
3371
3372 dprintf(("SetActiveWindow %x", getWindowHandle()));
3373 if(getStyle() & WS_CHILD) {
3374// if(getStyle() & (WS_DISABLED | WS_CHILD)) {
3375 dprintf(("WARNING: Window is a child or disabled"));
3376 return 0;
3377 }
3378
3379 if(GetActiveWindow() == getWindowHandle()) {
3380 dprintf(("Window already active"));
3381 return getWindowHandle();
3382 }
3383 if (HOOK_IsHooked( WH_CBT ))
3384 {
3385 CBTACTIVATESTRUCT cbta;
3386 LRESULT ret;
3387
3388 cbta.fMouse = FALSE;
3389 cbta.hWndActive = GetActiveWindow();
3390 ret = HOOK_CallHooksA(WH_CBT, HCBT_ACTIVATE, getWindowHandle(), (LPARAM)&cbta);
3391 if(ret)
3392 {
3393 dprintf(("SetActiveWindow %x, CBT hook cancelled operation", getWindowHandle()));
3394 return cbta.hWndActive;
3395 }
3396 }
3397 SetWindowPos(HWND_TOP, 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
3398
3399// if(OSLibWinSetActiveWindow(OS2Hwnd) == FALSE) {
3400// dprintf(("OSLibWinSetActiveWindow %x returned FALSE!", OS2Hwnd));
3401// }
3402 hwndActive = GetActiveWindow();
3403 return (hwndActive) ? hwndActive : windowDesktop->getWindowHandle(); //pretend the desktop was active
3404}
3405//******************************************************************************
3406//Used to change active status of an mdi window
3407//******************************************************************************
3408BOOL Win32BaseWindow::DeactivateChildWindow()
3409{
3410 /* child windows get a WM_CHILDACTIVATE message */
3411 if((getStyle() & (WS_CHILD | WS_POPUP)) == WS_CHILD )
3412 {
3413 ULONG flags = OSLibWinGetWindowULong(getOS2WindowHandle(), OFFSET_WIN32FLAGS);
3414 OSLibWinSetWindowULong(getOS2WindowHandle(), OFFSET_WIN32FLAGS, (flags & ~WINDOWFLAG_ACTIVE));
3415 return TRUE;
3416 }
3417 DebugInt3(); //should not be called for non-child window
3418 return FALSE;
3419}
3420//******************************************************************************
3421//WM_ENABLE is sent to hwnd, but not to it's children (as it should be)
3422//******************************************************************************
3423BOOL Win32BaseWindow::EnableWindow(BOOL fEnable)
3424{
3425 BOOL rc;
3426
3427 dprintf(("Win32BaseWindow::EnableWindow %x %d", getWindowHandle(), fEnable));
3428 //return true if previous state was disabled, else false (sdk docs)
3429 rc = (getStyle() & WS_DISABLED) != 0;
3430 if(rc && !fEnable) {
3431 SendMessageA(getWindowHandle(), WM_CANCELMODE, 0, 0);
3432 }
3433 OSLibWinEnableWindow(OS2HwndFrame, fEnable);
3434 if(fEnable == FALSE) {
3435 //SvL: No need to clear focus as PM already does this
3436 if(getWindowHandle() == GetCapture()) {
3437 ReleaseCapture(); /* A disabled window can't capture the mouse */
3438 dprintf(("Released capture for window %x that is being disabled", getWindowHandle()));
3439 }
3440 }
3441 return rc;
3442}
3443//******************************************************************************
3444//******************************************************************************
3445BOOL Win32BaseWindow::CloseWindow()
3446{
3447 if (::GetWindowLongW( getWindowHandle() , GWL_STYLE ) & WS_CHILD) return FALSE;
3448 ShowWindow( SW_MINIMIZE );
3449 return TRUE;
3450}
3451//******************************************************************************
3452//TODO: Not be 100% correct; should return active window of current thread
3453// or NULL when there is none -> WinQueryActiveWindow just returns
3454// the current active window
3455//******************************************************************************
3456HWND Win32BaseWindow::GetActiveWindow()
3457{
3458 HWND hwndActive;
3459
3460 hwndActive = OSLibWinQueryActiveWindow();
3461 return OS2ToWin32Handle(hwndActive);
3462}
3463//******************************************************************************
3464//******************************************************************************
3465BOOL Win32BaseWindow::hasWindowName(LPSTR wndname, BOOL fUnicode)
3466{
3467 INT len = GetWindowTextLength(fUnicode);
3468 BOOL res;
3469
3470 if (wndname == NULL)
3471 return (len == 0);
3472
3473 len++;
3474 if (fUnicode)
3475 {
3476 WCHAR *text = (WCHAR*)malloc(len*sizeof(WCHAR));
3477
3478 GetWindowTextW(text,len);
3479 res = (lstrcmpW(text,(LPWSTR)wndname) == 0);
3480 free(text);
3481 }
3482 else
3483 {
3484 CHAR *text = (CHAR*)malloc(len*sizeof(CHAR));
3485
3486 GetWindowTextA(text,len);
3487 res = (strcmp(text,wndname) == 0);
3488 free(text);
3489 }
3490
3491 return res;
3492}
3493//******************************************************************************
3494//******************************************************************************
3495CHAR *Win32BaseWindow::getWindowNamePtrA()
3496{
3497 INT len = GetWindowTextLength(FALSE);
3498 CHAR *text;
3499
3500 if (len == 0) return NULL;
3501 len++;
3502 text = (CHAR*)malloc(len*sizeof(CHAR));
3503 GetWindowTextA(text,len);
3504
3505 return text;
3506}
3507//******************************************************************************
3508//******************************************************************************
3509WCHAR *Win32BaseWindow::getWindowNamePtrW()
3510{
3511 INT len = GetWindowTextLength(TRUE);
3512 WCHAR *text;
3513
3514 if (len == 0) return NULL;
3515 len++;
3516 text = (WCHAR*)malloc(len*sizeof(WCHAR));
3517 GetWindowTextW(text,len);
3518
3519 return text;
3520}
3521//******************************************************************************
3522//******************************************************************************
3523VOID Win32BaseWindow::freeWindowNamePtr(PVOID namePtr)
3524{
3525 if (namePtr) free(namePtr);
3526}
3527//******************************************************************************
3528//When using this API for a window that was created by a different process, NT
3529//does NOT send WM_GETTEXTLENGTH.
3530//******************************************************************************
3531int Win32BaseWindow::GetWindowTextLength(BOOL fUnicode)
3532{
3533 //if the destination window is created by this process, send message
3534 if(dwProcessId == currentProcessId)
3535 {
3536 if(fUnicode) {
3537 return SendMessageW(getWindowHandle(), WM_GETTEXTLENGTH,0,0);
3538 }
3539 else return SendMessageA(getWindowHandle(), WM_GETTEXTLENGTH,0,0);
3540 }
3541 //else get data directory from window structure
3542 //TODO: must lock window structure.... (TODO)
3543 return windowNameLength;
3544}
3545//******************************************************************************
3546//When using this API for a window that was created by a different process, NT
3547//does NOT send WM_GETTEXT.
3548//******************************************************************************
3549int Win32BaseWindow::GetWindowTextA(LPSTR lpsz, int cch)
3550{
3551 //if the destination window is created by this process, send message
3552 if(dwProcessId == currentProcessId) {
3553 return SendMessageA(getWindowHandle(),WM_GETTEXT,(WPARAM)cch,(LPARAM)lpsz);
3554 }
3555
3556 //else get data directory from window structure
3557 if (!lpsz || !cch) return 0;
3558 if (!windowNameA) lpsz[0] = 0;
3559 else memcpy(lpsz, windowNameA, min(windowNameLength + 1, cch) );
3560 return min(windowNameLength, cch);
3561}
3562//******************************************************************************
3563//When using this API for a window that was created by a different process, NT
3564//does NOT send WM_GETTEXT.
3565//******************************************************************************
3566int Win32BaseWindow::GetWindowTextW(LPWSTR lpsz, int cch)
3567{
3568 //if the destination window is created by this process, send message
3569 if(dwProcessId == currentProcessId) {
3570 return ::SendMessageW(getWindowHandle(), WM_GETTEXT,(WPARAM)cch,(LPARAM)lpsz);
3571 }
3572 //else get data directory from window structure
3573 if (!lpsz || !cch)
3574 return 0;
3575 if (!windowNameW)
3576 lpsz[0] = 0;
3577 else
3578 memcpy(lpsz, windowNameW, min( sizeof(WCHAR) * (windowNameLength+1), cch));
3579
3580 return min(windowNameLength, cch);
3581}
3582//******************************************************************************
3583//TODO: How does this work when the target window belongs to a different process???
3584//******************************************************************************
3585BOOL Win32BaseWindow::SetWindowTextA(LPSTR lpsz)
3586{
3587 return SendMessageA(getWindowHandle(),WM_SETTEXT,0,(LPARAM)lpsz);
3588}
3589//******************************************************************************
3590//******************************************************************************
3591BOOL Win32BaseWindow::SetWindowTextW(LPWSTR lpsz)
3592{
3593 return SendMessageW(getWindowHandle(), WM_SETTEXT,0,(LPARAM)lpsz);
3594}
3595//******************************************************************************
3596//******************************************************************************
3597LONG Win32BaseWindow::SetWindowLong(int index, ULONG value, BOOL fUnicode)
3598{
3599 LONG oldval;
3600
3601 switch(index) {
3602 case GWL_EXSTYLE:
3603 {
3604 STYLESTRUCT ss;
3605
3606 if(dwExStyle == value) {
3607 oldval = value;
3608 break;
3609 }
3610 ss.styleOld = dwExStyle;
3611 ss.styleNew = value;
3612 dprintf(("SetWindowLong GWL_EXSTYLE %x old %x new style %x", getWindowHandle(), dwExStyle, value));
3613 SendMessageA(getWindowHandle(),WM_STYLECHANGING,GWL_EXSTYLE,(LPARAM)&ss);
3614 setExStyle(ss.styleNew);
3615 SendMessageA(getWindowHandle(),WM_STYLECHANGED,GWL_EXSTYLE,(LPARAM)&ss);
3616 OSLibSetWindowStyle(getOS2FrameWindowHandle(), getOS2WindowHandle(), getStyle(), getExStyle());
3617 oldval = ss.styleOld;
3618 break;
3619 }
3620 case GWL_STYLE:
3621 {
3622 STYLESTRUCT ss;
3623
3624 //SvL: TODO: Can you change minimize or maximize status here too?
3625
3626 if(dwStyle == value) {
3627 oldval = value;
3628 break;
3629 }
3630 dprintf(("SetWindowLong GWL_STYLE %x old %x new style %x (%x)", getWindowHandle(), dwStyle, value));
3631
3632 //Changing WS_CHILD style is allowed
3633 ss.styleOld = getStyle();
3634 ss.styleNew = value;
3635 SendMessageA(getWindowHandle(),WM_STYLECHANGING,GWL_STYLE,(LPARAM)&ss);
3636 setStyle(ss.styleNew);
3637 SendMessageA(getWindowHandle(),WM_STYLECHANGED,GWL_STYLE,(LPARAM)&ss);
3638 OSLibSetWindowStyle(getOS2FrameWindowHandle(), getOS2WindowHandle(), getStyle(), getExStyle());
3639
3640 //TODO: Might not be correct to use ShowWindow here
3641 if((ss.styleOld & WS_VISIBLE) != (ss.styleNew & WS_VISIBLE)) {
3642 if(ss.styleNew & WS_VISIBLE)
3643 ShowWindow(SW_SHOWNOACTIVATE);
3644 else ShowWindow(SW_HIDE);
3645 }
3646#ifdef DEBUG
3647 PrintWindowStyle(ss.styleNew, 0);
3648#endif
3649 oldval = ss.styleOld;
3650 break;
3651 }
3652 case GWL_WNDPROC:
3653 {
3654 //Note: Type of SetWindowLong determines new window proc type
3655 // UNLESS the new window proc has already been registered
3656 // (use the old type in that case)
3657 // (VERIFIED in NT 4, SP6)
3658 WINDOWPROCTYPE type = WINPROC_GetProcType((HWINDOWPROC)value);
3659 if(type == WIN_PROC_INVALID) {
3660 type = (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A;
3661 }
3662 oldval = (LONG)WINPROC_GetProc(win32wndproc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
3663 dprintf(("SetWindowLong%c GWL_WNDPROC %x old %x new wndproc %x", (fUnicode) ? 'W' : 'A', getWindowHandle(), oldval, value));
3664 WINPROC_SetProc((HWINDOWPROC *)&win32wndproc, (WNDPROC)value, type, WIN_PROC_WINDOW);
3665 break;
3666 }
3667 case GWL_HINSTANCE:
3668 oldval = hInstance;
3669 hInstance = value;
3670 break;
3671
3672 case GWL_HWNDPARENT:
3673 oldval = SetParent((HWND)value);
3674 break;
3675
3676 case GWL_ID:
3677 dprintf(("GWL_ID old %x, new %x", getWindowId(), value));
3678 oldval = getWindowId();
3679 setWindowId(value);
3680 break;
3681
3682 case GWL_USERDATA:
3683 oldval = userData;
3684 userData = value;
3685 break;
3686
3687 default:
3688 if(index >= 0 && index + sizeof(ULONG) <= nrUserWindowBytes)
3689 {
3690 oldval = *(ULONG *)(userWindowBytes + index);
3691 *(ULONG *)(userWindowBytes + index) = value;
3692 break;
3693 }
3694 dprintf(("WARNING: SetWindowLong%c %x %d %x returned %x INVALID index!", (fUnicode) ? 'W' : 'A', getWindowHandle(), index, value));
3695 SetLastError(ERROR_INVALID_INDEX); //verified in NT4, SP6
3696 return 0;
3697 }
3698 //Note: NT4, SP6 does not set the last error to 0
3699 SetLastError(ERROR_SUCCESS);
3700 dprintf2(("SetWindowLong%c %x %d %x returned %x", (fUnicode) ? 'W' : 'A', getWindowHandle(), index, value, oldval));
3701 return oldval;
3702}
3703//******************************************************************************
3704//******************************************************************************
3705ULONG Win32BaseWindow::GetWindowLong(int index, BOOL fUnicode)
3706{
3707 ULONG value;
3708
3709 switch(index) {
3710 case GWL_EXSTYLE:
3711 value = dwExStyle;
3712 break;
3713 case GWL_STYLE:
3714 value = dwStyle;
3715 break;
3716 case GWL_WNDPROC:
3717 value = (LONG)WINPROC_GetProc(win32wndproc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
3718 break;
3719 case GWL_HINSTANCE:
3720 value = hInstance;
3721 break;
3722 case GWL_HWNDPARENT:
3723 value = GetParent();
3724 break;
3725 case GWL_ID:
3726 value = getWindowId();
3727 break;
3728 case GWL_USERDATA:
3729 value = userData;
3730 break;
3731 default:
3732 if(index >= 0 && index + sizeof(ULONG) <= nrUserWindowBytes)
3733 {
3734 value = *(ULONG *)(userWindowBytes + index);
3735 break;
3736 }
3737 dprintf(("WARNING: GetWindowLong%c %x %d %x returned %x INVALID index!", (fUnicode) ? 'W' : 'A', getWindowHandle(), index, value));
3738 SetLastError(ERROR_INVALID_INDEX); //verified in NT4, SP6
3739 return 0;
3740 }
3741 dprintf2(("GetWindowLong%c %x %d %x", (fUnicode) ? 'W' : 'A', getWindowHandle(), index, value));
3742 //Note: NT4, SP6 does not set the last error to 0
3743 SetLastError(ERROR_SUCCESS);
3744 return value;
3745}
3746//******************************************************************************
3747//******************************************************************************
3748WORD Win32BaseWindow::SetWindowWord(int index, WORD value)
3749{
3750 WORD oldval;
3751
3752 if(index >= 0 && index + sizeof(WORD) <= nrUserWindowBytes)
3753 {
3754 oldval = *(WORD *)(userWindowBytes + index);
3755 *(WORD *)(userWindowBytes + index) = value;
3756 //Note: NT4, SP6 does not set the last error to 0
3757 dprintf2(("SetWindowWord %x %d %x returned %x", getWindowHandle(), index, value, oldval));
3758 SetLastError(ERROR_SUCCESS);
3759 return oldval;
3760 }
3761 switch(index)
3762 {
3763 case GWW_HINSTANCE:
3764 oldval = hInstance;
3765 hInstance = value;
3766 break;
3767
3768 case GWW_HWNDPARENT:
3769 oldval = SetParent((HWND)(WNDHANDLE_MAGIC_HIGHWORD | value));
3770 break;
3771
3772 case GWW_ID:
3773 oldval = getWindowId();
3774 setWindowId(value);
3775 break;
3776
3777 default:
3778 dprintf(("WARNING: SetWindowWord %x %d %x returned %x INVALID index!", getWindowHandle(), index, value));
3779 SetLastError(ERROR_INVALID_INDEX); //verified in NT4, SP6
3780 return 0;
3781 }
3782 //Note: NT4, SP6 does not set the last error to 0
3783 SetLastError(ERROR_SUCCESS);
3784 dprintf2(("SetWindowWord %x %d %x returned %x", getWindowHandle(), index, value, oldval));
3785 return oldval;
3786}
3787//******************************************************************************
3788//******************************************************************************
3789WORD Win32BaseWindow::GetWindowWord(int index)
3790{
3791 if(index >= 0 && index + sizeof(WORD) <= nrUserWindowBytes)
3792 {
3793 //Note: NT4, SP6 does not set the last error to 0
3794 SetLastError(ERROR_SUCCESS);
3795 dprintf2(("GetWindowWord %x %d %x", getWindowHandle(), index, *(WORD *)(userWindowBytes + index)));
3796 return *(WORD *)(userWindowBytes + index);
3797 }
3798 switch(index)
3799 {
3800 case GWW_ID:
3801 if(HIWORD(getWindowId()))
3802 dprintf(("WARNING: GWW_ID: discards high bits of 0x%08x!\n", getWindowId()));
3803 return (WORD)getWindowId();
3804
3805 case GWW_HWNDPARENT:
3806 dprintf(("WARNING: GWW_HWNDPARENT: discards high bits of 0x%08x!\n", GetParent()));
3807 return (WORD) GetParent();
3808
3809 case GWW_HINSTANCE:
3810 if (HIWORD(hInstance))
3811 dprintf(("WARNING: GWW_HINSTANCE: discards high bits of 0x%08x!\n", hInstance));
3812 return (WORD)hInstance;
3813 }
3814
3815 dprintf(("WARNING: GetWindowWord %x %d returned %x INVALID index!", getWindowHandle(), index));
3816 SetLastError(ERROR_INVALID_INDEX); //verified in NT4, SP6
3817 return 0;
3818}
3819//******************************************************************************
3820//Locates window in linked list and increases reference count (if found)
3821//Window object must be unreferenced after usage
3822//******************************************************************************
3823Win32BaseWindow *Win32BaseWindow::GetWindowFromHandle(HWND hwnd)
3824{
3825 Win32BaseWindow *window;
3826
3827////TODO: temporary workaround for crashes in Opera (pmwinx; releasesemaphore)
3828//// while browsing
3829//// Not thread safe now!
3830//// lock(&critsect);
3831 if(HwGetWindowHandleData(hwnd, (DWORD *)&window) == TRUE) {
3832 if(window) {
3833//// dprintf(("addRef %x; refcount %d", hwnd, window->getRefCount()+1));
3834 window->addRef();
3835 }
3836//// unlock(&critsect);
3837 return window;
3838 }
3839//// unlock(&critsect);
3840// dprintf2(("Win32BaseWindow::GetWindowFromHandle: not a win32 window %x", hwnd));
3841 return NULL;
3842}
3843//******************************************************************************
3844//Locates window in linked list and increases reference count (if found)
3845//Window object must be unreferenced after usage
3846//******************************************************************************
3847Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2Handle(HWND hwndOS2)
3848{
3849 DWORD magic;
3850 HWND hwnd;
3851
3852 if(hwndOS2 == OSLIB_HWND_DESKTOP)
3853 {
3854 windowDesktop->addRef();
3855 return windowDesktop;
3856 }
3857
3858 hwnd = (HWND)OSLibWinGetWindowULong(hwndOS2, OFFSET_WIN32WNDPTR);
3859 magic = OSLibWinGetWindowULong(hwndOS2, OFFSET_WIN32PM_MAGIC);
3860
3861 if(hwnd && CheckMagicDword(magic)) {
3862 return GetWindowFromHandle(hwnd);
3863 }
3864// dprintf2(("Win32BaseWindow::GetWindowFromOS2Handle: not an Odin os2 window %x", hwndOS2));
3865 return 0;
3866}
3867//******************************************************************************
3868//Locates window in linked list and increases reference count (if found)
3869//Window object must be unreferenced after usage
3870//******************************************************************************
3871Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2FrameHandle(HWND hwnd)
3872{
3873 return GetWindowFromOS2Handle(OSLibWinWindowFromID(hwnd,OSLIB_FID_CLIENT));
3874}
3875//******************************************************************************
3876//******************************************************************************
3877HWND WIN32API Win32ToOS2Handle(HWND hwnd)
3878{
3879 HWND hwndOS2;
3880
3881 Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(hwnd);
3882
3883 if(window) {
3884 hwndOS2 = window->getOS2WindowHandle();
3885 RELEASE_WNDOBJ(window);
3886 return hwndOS2;
3887 }
3888// dprintf2(("Win32BaseWindow::Win32ToOS2Handle: not a win32 window %x", hwnd));
3889 return hwnd;
3890}
3891//******************************************************************************
3892//******************************************************************************
3893HWND WIN32API Win32ToOS2FrameHandle(HWND hwnd)
3894{
3895 HWND hwndOS2;
3896
3897 Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(hwnd);
3898
3899 if(window) {
3900 hwndOS2 = window->getOS2FrameWindowHandle();
3901 RELEASE_WNDOBJ(window);
3902 return hwndOS2;
3903 }
3904// dprintf2(("Win32BaseWindow::Win32ToOS2Handle: not a win32 window %x", hwnd));
3905 return hwnd;
3906}
3907//******************************************************************************
3908//******************************************************************************
3909HWND WIN32API OS2ToWin32Handle(HWND hwnd)
3910{
3911 Win32BaseWindow *window = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
3912 HWND hwndWin32;
3913
3914 if(window) {
3915 hwndWin32 = window->getWindowHandle();
3916 RELEASE_WNDOBJ(window);
3917 return hwndWin32;
3918 }
3919 window = Win32BaseWindow::GetWindowFromOS2FrameHandle(hwnd);
3920 if(window) {
3921 hwndWin32 = window->getWindowHandle();
3922 RELEASE_WNDOBJ(window);
3923 return hwndWin32;
3924 }
3925
3926// dprintf2(("Win32BaseWindow::OS2ToWin32Handle: not a win32 window %x", hwnd));
3927 return 0;
3928// else return hwnd; //OS/2 window handle
3929}
3930#ifdef DEBUG
3931LONG Win32BaseWindow::addRef()
3932{
3933// dprintf2(("addRef %x %d", getWindowHandle(), getRefCount()+1));
3934 return GenericObject::addRef();
3935}
3936//******************************************************************************
3937//******************************************************************************
3938LONG Win32BaseWindow::release(char *function, int line)
3939{
3940// dprintf2(("release %s %d %x %d", function, line, getWindowHandle(), getRefCount()-1));
3941 return GenericObject::release();
3942}
3943#endif
3944//******************************************************************************
3945//******************************************************************************
3946GenericObject *Win32BaseWindow::windows = NULL;
3947CRITICAL_SECTION Win32BaseWindow::critsect = {0};
3948
3949//******************************************************************************
3950//******************************************************************************
3951#ifdef DEBUG
3952void PrintWindowStyle(DWORD dwStyle, DWORD dwExStyle)
3953{
3954 char style[256] = "";
3955 char exstyle[256] = "";
3956
3957 /* Window styles */
3958 if(dwStyle & WS_CHILD)
3959 strcat(style, "WS_CHILD ");
3960 if(dwStyle & WS_POPUP)
3961 strcat(style, "WS_POPUP ");
3962 if(dwStyle & WS_VISIBLE)
3963 strcat(style, "WS_VISIBLE ");
3964 if(dwStyle & WS_DISABLED)
3965 strcat(style, "WS_DISABLED ");
3966 if(dwStyle & WS_CLIPSIBLINGS)
3967 strcat(style, "WS_CLIPSIBLINGS ");
3968 if(dwStyle & WS_CLIPCHILDREN)
3969 strcat(style, "WS_CLIPCHILDREN ");
3970 if(dwStyle & WS_MAXIMIZE)
3971 strcat(style, "WS_MAXIMIZE ");
3972 if(dwStyle & WS_MINIMIZE)
3973 strcat(style, "WS_MINIMIZE ");
3974 if(dwStyle & WS_GROUP)
3975 strcat(style, "WS_GROUP ");
3976 if(dwStyle & WS_TABSTOP)
3977 strcat(style, "WS_TABSTOP ");
3978
3979 if((dwStyle & WS_CAPTION) == WS_CAPTION)
3980 strcat(style, "WS_CAPTION ");
3981 if(dwStyle & WS_DLGFRAME)
3982 strcat(style, "WS_DLGFRAME ");
3983 if(dwStyle & WS_BORDER)
3984 strcat(style, "WS_BORDER ");
3985
3986 if(dwStyle & WS_VSCROLL)
3987 strcat(style, "WS_VSCROLL ");
3988 if(dwStyle & WS_HSCROLL)
3989 strcat(style, "WS_HSCROLL ");
3990 if(dwStyle & WS_SYSMENU)
3991 strcat(style, "WS_SYSMENU ");
3992 if(dwStyle & WS_THICKFRAME)
3993 strcat(style, "WS_THICKFRAME ");
3994 if(dwStyle & WS_MINIMIZEBOX)
3995 strcat(style, "WS_MINIMIZEBOX ");
3996 if(dwStyle & WS_MAXIMIZEBOX)
3997 strcat(style, "WS_MAXIMIZEBOX ");
3998
3999 if(dwExStyle & WS_EX_DLGMODALFRAME)
4000 strcat(exstyle, "WS_EX_DLGMODALFRAME ");
4001 if(dwExStyle & WS_EX_ACCEPTFILES)
4002 strcat(exstyle, "WS_EX_ACCEPTFILES ");
4003 if(dwExStyle & WS_EX_NOPARENTNOTIFY)
4004 strcat(exstyle, "WS_EX_NOPARENTNOTIFY ");
4005 if(dwExStyle & WS_EX_TOPMOST)
4006 strcat(exstyle, "WS_EX_TOPMOST ");
4007 if(dwExStyle & WS_EX_TRANSPARENT)
4008 strcat(exstyle, "WS_EX_TRANSPARENT ");
4009
4010 if(dwExStyle & WS_EX_MDICHILD)
4011 strcat(exstyle, "WS_EX_MDICHILD ");
4012 if(dwExStyle & WS_EX_TOOLWINDOW)
4013 strcat(exstyle, "WS_EX_TOOLWINDOW ");
4014 if(dwExStyle & WS_EX_WINDOWEDGE)
4015 strcat(exstyle, "WS_EX_WINDOWEDGE ");
4016 if(dwExStyle & WS_EX_CLIENTEDGE)
4017 strcat(exstyle, "WS_EX_CLIENTEDGE ");
4018 if(dwExStyle & WS_EX_CONTEXTHELP)
4019 strcat(exstyle, "WS_EX_CONTEXTHELP ");
4020 if(dwExStyle & WS_EX_RIGHT)
4021 strcat(exstyle, "WS_EX_RIGHT ");
4022 if(dwExStyle & WS_EX_LEFT)
4023 strcat(exstyle, "WS_EX_LEFT ");
4024 if(dwExStyle & WS_EX_RTLREADING)
4025 strcat(exstyle, "WS_EX_RTLREADING ");
4026 if(dwExStyle & WS_EX_LTRREADING)
4027 strcat(exstyle, "WS_EX_LTRREADING ");
4028 if(dwExStyle & WS_EX_LEFTSCROLLBAR)
4029 strcat(exstyle, "WS_EX_LEFTSCROLLBAR ");
4030 if(dwExStyle & WS_EX_RIGHTSCROLLBAR)
4031 strcat(exstyle, "WS_EX_RIGHTSCROLLBAR ");
4032 if(dwExStyle & WS_EX_CONTROLPARENT)
4033 strcat(exstyle, "WS_EX_CONTROLPARENT ");
4034 if(dwExStyle & WS_EX_STATICEDGE)
4035 strcat(exstyle, "WS_EX_STATICEDGE ");
4036 if(dwExStyle & WS_EX_APPWINDOW)
4037 strcat(exstyle, "WS_EX_APPWINDOW ");
4038
4039 dprintf(("Window style: %x %s", dwStyle, style));
4040 dprintf(("Window exStyle: %x %s", dwExStyle, exstyle));
4041}
4042#endif
4043//******************************************************************************
4044//******************************************************************************
Note: See TracBrowser for help on using the repository browser.