Ignore:
Timestamp:
Jun 9, 2001, 4:50:26 PM (24 years ago)
Author:
sandervl
Message:

reference count (window + class objects) rewrite

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/user32/win32wbase.cpp

    r5810 r5935  
    1 /* $Id: win32wbase.cpp,v 1.259 2001-05-27 19:01:35 sandervl Exp $ */
     1/* $Id: win32wbase.cpp,v 1.260 2001-06-09 14:50:21 sandervl Exp $ */
    22/*
    33 * Win32 Window Base Class for OS/2
     
    1414 *
    1515 * 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.
    1623 *
    1724 * NOTE: Client rectangle always relative to frame window
     
    6673//******************************************************************************
    6774//******************************************************************************
    68 Win32BaseWindow::Win32BaseWindow(DWORD objType) : GenericObject(&windows, objType)
     75Win32BaseWindow::Win32BaseWindow()
     76                     : GenericObject(&windows, &critsect), ChildWindow(&critsect)
    6977{
    7078  Init();
     
    7280//******************************************************************************
    7381//******************************************************************************
    74 Win32BaseWindow::Win32BaseWindow(HWND hwndOS2, ULONG reserved) : GenericObject(&windows, OBJTYPE_WINDOW), ChildWindow()
     82Win32BaseWindow::Win32BaseWindow(HWND hwndOS2, ULONG reserved)
     83                     : GenericObject(&windows, &critsect), ChildWindow(&critsect)
    7584{
    7685  Init();
     
    8089//******************************************************************************
    8190Win32BaseWindow::Win32BaseWindow(CREATESTRUCTA *lpCreateStructA, ATOM classAtom, BOOL isUnicode)
    82                         : GenericObject(&windows, OBJTYPE_WINDOW), ChildWindow()
     91                     : GenericObject(&windows, &critsect), ChildWindow(&critsect)
    8392{
    8493  Init();
     
    115124  nrUserWindowBytes= 0;
    116125
    117   magic            = WIN32PM_MAGIC;
    118126  OS2Hwnd          = 0;
    119127  OS2HwndFrame     = 0;
     
    179187Win32BaseWindow::~Win32BaseWindow()
    180188{
     189    if(getRefCount() < 0) {
     190        DebugInt3();
     191    }
     192
    181193    if(hTaskList) {
    182194        OSLibWinRemoveFromTasklist(hTaskList);
     
    187199    OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32PM_MAGIC, 0);
    188200
    189     if(!fDestroyAll && getParent() && getParent()->getFirstChild() == this && getNextChild() == NULL)
    190     {
    191         //if we're the last child that's being destroyed and our
    192         //parent window was also destroyed, then we delete the parent object
    193         if(getParent()->IsWindowDestroyed())
    194         {
    195             dprintf(("Last Child (%x) destroyed, get rid of our parent window (%x)", getWindowHandle(), getParent()->getWindowHandle()));
    196             delete getParent();
    197             setParent(NULL);  //or else we'll crash in the dtor of the ChildWindow class
    198         }
    199     }
    200     else
    201201    if(fDestroyAll) {
    202202        dprintf(("Destroying window %x %s", getWindowHandle(), windowNameA));
     
    206206    /* Decrement class window counter */
    207207    if(windowClass) {
    208         windowClass->DecreaseWindowCount();
     208        RELEASE_CLASSOBJ(windowClass);
    209209    }
    210210
     
    236236    if(propertyList) {
    237237        removeWindowProps();
     238    }
     239    Win32BaseWindow *wndparent = (Win32BaseWindow *)ChildWindow::getParentOfChild();
     240    if(wndparent) {
     241        RELEASE_WNDOBJ(wndparent);
     242    }
     243    if(windowClass) {
     244        RELEASE_CLASSOBJ(windowClass);
    238245    }
    239246}
     
    287294            if (!window->IsWindow() )
    288295            {
     296                    RELEASE_WNDOBJ(window);
    289297                    dprintf(("Bad parent %04x\n", cs->hwndParent ));
    290298                    SetLastError(ERROR_INVALID_PARAMETER);
    291299                    return FALSE;
    292300            }
     301            RELEASE_WNDOBJ(window);
    293302            /* Windows does this for overlapped windows
    294303             * (I don't know about other styles.) */
     
    314323        return 0;
    315324    }
    316     /* Increment class window counter */
    317     windowClass->IncreaseWindowCount();
    318325
    319326#ifdef DEBUG
     
    417424        else
    418425        {
    419             owner = GetWindowFromHandle(cs->hwndParent)->GetTopParent();
     426            owner = GetWindowFromHandle(GetWindowFromHandle(cs->hwndParent)->GetTopParent());
    420427            if(owner == NULL)
    421428            {
     
    491498    fNoSizeMsg = TRUE;
    492499
    493     if(OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, (ULONG)this) == FALSE) {
     500    if(OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, getWindowHandle()) == FALSE) {
    494501        dprintf(("WM_CREATE: WinSetWindowULong %X failed!!", OS2Hwnd));
    495502        SetLastError(ERROR_OUTOFMEMORY); //TODO: Better error
     
    827834    SendInternalMessageA(WM_NCDESTROY, 0, 0);
    828835
    829     TIMER_KillTimerFromWindow(OS2Hwnd);
    830 
    831     if(getFirstChild() == NULL && fCreationFinished) {
     836    TIMER_KillTimerFromWindow(getWindowHandle());
     837
     838    if(getRefCount() == 0 && getFirstChild() == NULL && fCreationFinished) {
    832839        delete this;
    833840    }
    834841    else {
    835842        //make sure no message can ever arrive for this window again (PM or from other win32 windows)
     843        dprintf(("Mark window %x (%x) as deleted", getWindowHandle(), this));
     844        markDeleted();
    836845        OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32WNDPTR, 0);
    837846        OSLibWinSetWindowULong(OS2Hwnd, OFFSET_WIN32PM_MAGIC, 0);
     
    10281037
    10291038        /* Activate the window if needed */
    1030         hwndTop = (GetTopParent()) ? GetTopParent()->getWindowHandle() : 0;
     1039        hwndTop = GetTopParent();
    10311040
    10321041        HWND hwndActive = GetActiveWindow();
     
    16731682        if(wParam == VK_F4) /* try to close the window */
    16741683        {
    1675             Win32BaseWindow *window = GetTopParent();
     1684            Win32BaseWindow *window = GetWindowFromHandle(GetTopParent());
    16761685            if(window && !(window->getClass()->getStyle() & CS_NOCLOSE))
    16771686                PostMessageA(window->getWindowHandle(), WM_SYSCOMMAND, SC_CLOSE, 0);
     1687            if(window) RELEASE_WNDOBJ(window);
    16781688            return 0;
    16791689        }
     
    27492759        OSLibWinSetParent(getOS2FrameWindowHandle(), OSLIB_HWND_DESKTOP);
    27502760
    2751     //TODO: Send WM_STYLECHANGED msg?
     2761        //TODO: Send WM_STYLECHANGED msg?
    27522762        setStyle(getStyle() & ~WS_CHILD);
    27532763        setWindowId(0);
     
    27902800        topwindow = GetWindowFromOS2FrameHandle(hwndTop);
    27912801        if(topwindow) {
    2792             return topwindow->getWindowHandle();
     2802            hwndTop = topwindow->getWindowHandle();
     2803            RELEASE_WNDOBJ(topwindow);
     2804            return hwndTop;
    27932805        }
    27942806        return 0;
     
    27972809        topwindow = GetWindowFromOS2FrameHandle(hwndTop);
    27982810        if(topwindow) {
    2799             return topwindow->getWindowHandle();
     2811            hwndTop = topwindow->getWindowHandle();
     2812            RELEASE_WNDOBJ(topwindow);
     2813            return hwndTop;
    28002814        }
    28012815        hwndTop = OSLibWinQueryWindow(hwndTop, QWOS_NEXT);
     
    28072821// Get the top-level parent for a child window.
    28082822//******************************************************************************
    2809 Win32BaseWindow *Win32BaseWindow::GetTopParent()
     2823HWND Win32BaseWindow::GetTopParent()
    28102824{
    28112825 Win32BaseWindow *window = this;
    2812 
     2826 HWND             hwndTopParent = 0;
     2827
     2828    lock();
    28132829    while(window && (window->getStyle() & WS_CHILD))
    28142830    {
    28152831        window = window->getParent();
    28162832    }
    2817     return window;
     2833    if(window) {
     2834        hwndTopParent = window->getWindowHandle();
     2835    }
     2836    unlock();
     2837    return hwndTopParent;
    28182838}
    28192839//******************************************************************************
     
    29072927//******************************************************************************
    29082928//******************************************************************************
    2909 Win32BaseWindow *Win32BaseWindow::FindWindowById(int id)
    2910 {
     2929HWND Win32BaseWindow::FindWindowById(int id)
     2930{
     2931    lock();
    29112932    for (Win32BaseWindow *child = (Win32BaseWindow *)getFirstChild(); child; child = (Win32BaseWindow *)child->getNextChild())
    29122933    {
    29132934        if (child->getWindowId() == id)
    29142935        {
    2915             return child;
    2916         }
    2917     }
     2936            unlock();
     2937            return child->getWindowHandle();
     2938        }
     2939    }
     2940    unlock();
    29182941    return 0;
    29192942}
     
    29873010                    OSLibWinEndEnumWindows(henum);
    29883011                    dprintf(("FindWindowEx: Found window %x", wnd->getWindowHandle()));
    2989                     return wnd->getWindowHandle();
     3012                    HWND hwndret = wnd->getWindowHandle();
     3013                    RELEASE_WNDOBJ(wnd);
     3014                    return hwndret;
    29903015                }
     3016                RELEASE_WNDOBJ(wnd);
    29913017            }
    29923018            hwnd = OSLibWinGetNextWindow(henum);
     
    30143040            if(window) {
    30153041                 hwndRelated = window->getWindowHandle();
     3042                 RELEASE_WNDOBJ(window);
    30163043            }
    30173044            else hwndRelated = 0;
     
    30313058            if(window) {
    30323059                 hwndRelated = window->getWindowHandle();
     3060                 RELEASE_WNDOBJ(window);
    30333061            }
    30343062            else hwndRelated = 0;
     
    30463074            if(window) {
    30473075                 hwndRelated = window->getWindowHandle();
     3076                 RELEASE_WNDOBJ(window);
    30483077            }
    30493078            else hwndRelated = 0;
     
    30613090            if(window) {
    30623091                 hwndRelated = window->getWindowHandle();
     3092                 RELEASE_WNDOBJ(window);
    30633093            }
    30643094            else hwndRelated = 0;
     
    30813111        if(window) {
    30823112             hwndRelated = window->getWindowHandle();
     3113             RELEASE_WNDOBJ(window);
    30833114        }
    30843115        else hwndRelated = 0;
     
    31743205{
    31753206 HWND          hwndActive;
    3176  Win32BaseWindow  *win32wnd;
    3177  ULONG         magic;
    31783207
    31793208  hwndActive = OSLibWinQueryActiveWindow();
     
    33773406                }
    33783407                oldval = (LONG)WINPROC_GetProc(win32wndproc, (fUnicode) ? WIN_PROC_32W : WIN_PROC_32A);
     3408                dprintf(("SetWindowLong GWL_WNDPROC %x old %x new style %x", getWindowHandle(), oldval, value));
    33793409                WINPROC_SetProc((HWINDOWPROC *)&win32wndproc, (WNDPROC)value, type, WIN_PROC_WINDOW);
    33803410                break;
     
    35003530//******************************************************************************
    35013531//******************************************************************************
    3502 Win32BaseWindow *Win32BaseWindow::GetWindowFromHandle(HWND hwnd)
    3503 {
    3504  Win32BaseWindow *window;
    3505 
    3506     if(HwGetWindowHandleData(hwnd, (DWORD *)&window) == TRUE) {
    3507          return window;
    3508     }
    3509 //    dprintf2(("Win32BaseWindow::GetWindowFromHandle: not a win32 window %x", hwnd));
    3510     return NULL;
    3511 }
    3512 //******************************************************************************
    3513 //******************************************************************************
    3514 Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2Handle(HWND hwnd)
    3515 {
    3516  Win32BaseWindow *win32wnd;
    3517  DWORD        magic;
    3518 
    3519     if(hwnd == OSLIB_HWND_DESKTOP)
    3520     {
    3521         return windowDesktop;
    3522     }
    3523 
    3524     win32wnd = (Win32BaseWindow *)OSLibWinGetWindowULong(hwnd, OFFSET_WIN32WNDPTR);
    3525     magic    = OSLibWinGetWindowULong(hwnd, OFFSET_WIN32PM_MAGIC);
    3526 
    3527     if(win32wnd && CheckMagicDword(magic)) {
    3528         return win32wnd;
    3529     }
    3530 //  dprintf2(("Win32BaseWindow::GetWindowFromOS2Handle: not an Odin os2 window %x", hwnd));
    3531     return 0;
    3532 }
    3533 //******************************************************************************
    3534 //******************************************************************************
    3535 Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2FrameHandle(HWND hwnd)
    3536 {
    3537     return GetWindowFromOS2Handle(OSLibWinWindowFromID(hwnd,OSLIB_FID_CLIENT));
    3538 }
    3539 //******************************************************************************
    3540 //******************************************************************************
    35413532HWND Win32BaseWindow::getNextDlgGroupItem(HWND hwndCtrl, BOOL fPrevious)
    35423533{
     
    36223613    return retvalue;
    36233614}
     3615//******************************************************************************
     3616//Locates window in linked list and increases reference count (if found)
     3617//Window object must be unreferenced after usage
     3618//******************************************************************************
     3619Win32BaseWindow *Win32BaseWindow::GetWindowFromHandle(HWND hwnd)
     3620{
     3621 Win32BaseWindow *window;
     3622
     3623    lock(&critsect);
     3624    if(HwGetWindowHandleData(hwnd, (DWORD *)&window) == TRUE) {
     3625         if(window) {
     3626////             dprintf(("addRef %x; refcount %d", hwnd, window->getRefCount()+1));
     3627             window->addRef();
     3628         }
     3629         unlock(&critsect);
     3630         return window;
     3631    }
     3632    unlock(&critsect);
     3633//    dprintf2(("Win32BaseWindow::GetWindowFromHandle: not a win32 window %x", hwnd));
     3634    return NULL;
     3635}
     3636//******************************************************************************
     3637//Locates window in linked list and increases reference count (if found)
     3638//Window object must be unreferenced after usage
     3639//******************************************************************************
     3640Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2Handle(HWND hwndOS2)
     3641{
     3642 DWORD            magic;
     3643 HWND             hwnd;
     3644
     3645    if(hwndOS2 == OSLIB_HWND_DESKTOP)
     3646    {
     3647        windowDesktop->addRef();
     3648        return windowDesktop;
     3649    }
     3650
     3651    hwnd  = (HWND)OSLibWinGetWindowULong(hwndOS2, OFFSET_WIN32WNDPTR);
     3652    magic = OSLibWinGetWindowULong(hwndOS2, OFFSET_WIN32PM_MAGIC);
     3653
     3654    if(hwnd && CheckMagicDword(magic)) {
     3655        return GetWindowFromHandle(hwnd);
     3656    }
     3657//  dprintf2(("Win32BaseWindow::GetWindowFromOS2Handle: not an Odin os2 window %x", hwndOS2));
     3658    return 0;
     3659}
     3660//******************************************************************************
     3661//Locates window in linked list and increases reference count (if found)
     3662//Window object must be unreferenced after usage
     3663//******************************************************************************
     3664Win32BaseWindow *Win32BaseWindow::GetWindowFromOS2FrameHandle(HWND hwnd)
     3665{
     3666    return GetWindowFromOS2Handle(OSLibWinWindowFromID(hwnd,OSLIB_FID_CLIENT));
     3667}
     3668//******************************************************************************
     3669//******************************************************************************
     3670HWND WIN32API Win32ToOS2Handle(HWND hwnd)
     3671{
     3672    HWND hwndOS2;
     3673
     3674    Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(hwnd);
     3675
     3676    if(window) {
     3677            hwndOS2 = window->getOS2WindowHandle();
     3678            RELEASE_WNDOBJ(window);
     3679            return hwndOS2;
     3680    }
     3681//    dprintf2(("Win32BaseWindow::Win32ToOS2Handle: not a win32 window %x", hwnd));
     3682    return hwnd;
     3683}
     3684//******************************************************************************
     3685//******************************************************************************
     3686HWND WIN32API OS2ToWin32Handle(HWND hwnd)
     3687{
     3688    Win32BaseWindow *window = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
     3689    HWND hwndWin32;
     3690
     3691    if(window) {
     3692            hwndWin32 = window->getWindowHandle();
     3693            RELEASE_WNDOBJ(window);
     3694            return hwndWin32;
     3695    }
     3696    window = Win32BaseWindow::GetWindowFromOS2FrameHandle(hwnd);
     3697    if(window) {
     3698            hwndWin32 = window->getWindowHandle();
     3699            RELEASE_WNDOBJ(window);
     3700            return hwndWin32;
     3701    }
     3702
     3703//    dprintf2(("Win32BaseWindow::OS2ToWin32Handle: not a win32 window %x", hwnd));
     3704    return 0;
     3705//    else    return hwnd;    //OS/2 window handle
     3706}
     3707//******************************************************************************
     3708//******************************************************************************
     3709GenericObject   *Win32BaseWindow::windows  = NULL;
     3710CRITICAL_SECTION Win32BaseWindow::critsect = {0};
     3711
    36243712//******************************************************************************
    36253713//******************************************************************************
     
    37183806//******************************************************************************
    37193807//******************************************************************************
    3720 HWND WIN32API Win32ToOS2Handle(HWND hwnd)
    3721 {
    3722     Win32BaseWindow *window = Win32BaseWindow::GetWindowFromHandle(hwnd);
    3723 
    3724     if(window) {
    3725             return window->getOS2WindowHandle();
    3726     }
    3727 //    dprintf2(("Win32BaseWindow::Win32ToOS2Handle: not a win32 window %x", hwnd));
    3728     return hwnd;
    3729 }
    3730 //******************************************************************************
    3731 //******************************************************************************
    3732 HWND WIN32API OS2ToWin32Handle(HWND hwnd)
    3733 {
    3734     Win32BaseWindow *window = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
    3735 
    3736     if(window) {
    3737             return window->getWindowHandle();
    3738     }
    3739     window = Win32BaseWindow::GetWindowFromOS2FrameHandle(hwnd);
    3740     if(window) {
    3741             return window->getWindowHandle();
    3742     }
    3743 
    3744 //    dprintf2(("Win32BaseWindow::OS2ToWin32Handle: not a win32 window %x", hwnd));
    3745     return 0;
    3746 //    else    return hwnd;    //OS/2 window handle
    3747 }
    3748 //******************************************************************************
    3749 //******************************************************************************
    3750 
    3751 GenericObject *Win32BaseWindow::windows  = NULL;
Note: See TracChangeset for help on using the changeset viewer.