vclptr: move down impl. to OutputDevice

Ultimately we will want to ref-count & VclPtr OutputDevice instances
separately from Window - but for now merge. This helps fix the amazing
lifecycle foo in toolkit/

Change-Id: If40ee9f645c87aff08815926205e908205bdd67a
diff --git a/include/vcl/outdev.hxx b/include/vcl/outdev.hxx
index 4cf2454..e53fc65 100644
--- a/include/vcl/outdev.hxx
+++ b/include/vcl/outdev.hxx
@@ -263,6 +263,26 @@ class VCL_DLLPUBLIC OutputDevice
    friend class vcl::PDFWriterImpl;
    friend void ImplHandleResize( vcl::Window* pWindow, long nNewWidth, long nNewHeight );

    // All of this will need to be replicated in Window
    // or a shared base-class as/when we can break the
    // OutputDevice -> Window inheritance.
private:
    mutable int mnRefCnt;         // reference count

    template<typename T> friend class ::rtl::Reference;
    template<typename T> friend class ::VclPtr;

    inline void acquire() const
    {
        mnRefCnt++;
    }

    inline void release() const
    {
        if (!--mnRefCnt)
            delete const_cast<OutputDevice*>(this);
    }

private:
    OutputDevice(const OutputDevice&) SAL_DELETED_FUNCTION;
    OutputDevice& operator=(const OutputDevice&) SAL_DELETED_FUNCTION;
@@ -352,6 +372,7 @@ private:
    mutable bool                    mbTextSpecial : 1;
    mutable bool                    mbRefPoint : 1;
    mutable bool                    mbEnableRTL : 1;
    mutable bool                    mbDisposed : 1;

    /** @name Initialization and accessor functions
     */
@@ -359,10 +380,17 @@ private:

protected:
                                OutputDevice();

public:
    virtual                     ~OutputDevice();

protected:
    /// release all references to other objects.
    virtual void                        dispose();

public:
    /// call the dispose() method if we have not already been disposed.
    void                                disposeOnce();

public:

    /** Get the graphic context that the output device uses to draw on.
diff --git a/include/vcl/window.hxx b/include/vcl/window.hxx
index 8744c29..663d7dd 100644
--- a/include/vcl/window.hxx
+++ b/include/vcl/window.hxx
@@ -420,8 +420,6 @@ private:
    // OutputDevice
    ::OutputDevice* mpOutputDevice;

    mutable int mnRefCnt;         // reference count

#ifdef DBG_UTIL
    friend const char* ::ImplDbgCheckWindow( const void* pObj );
#endif
@@ -500,27 +498,10 @@ public:

    SAL_DLLPRIVATE static void          ImplCalcSymbolRect( Rectangle& rRect );

private:
    template<typename T> friend class ::rtl::Reference;
    template<typename T> friend class ::VclPtr;

    inline void acquire() const
    {
        mnRefCnt++;
    }

    inline void release() const
    {
        if (!--mnRefCnt)
            delete this;
    }

protected:

    /** This is intended to be used to clear any locally held references to other Window-subclass objects */
    virtual void                        dispose();
    /* call the dispose() method if we have not already been disposed */
    void                                disposeOnce();

    SAL_DLLPRIVATE void                 ImplInit( vcl::Window* pParent, WinBits nStyle, SystemParentData* pSystemParentData );

diff --git a/vcl/source/outdev/outdev.cxx b/vcl/source/outdev/outdev.cxx
index 372eb23..5d59ee1 100644
--- a/vcl/source/outdev/outdev.cxx
+++ b/vcl/source/outdev/outdev.cxx
@@ -183,7 +183,26 @@ OutputDevice::OutputDevice() :

OutputDevice::~OutputDevice()
{
    disposeOnce();
}

void OutputDevice::disposeOnce()
{
    if ( mbDisposed )
        return;
    mbDisposed = true;

    // catch badness where our OutputDevice sub-class was not
    // wrapped safely in a VclPtr cosily.
    assert( mnRefCnt > 0 );

    // hold a ref in case something unusual happens during dispose.
    VclPtr<OutputDevice> aRef(this);
    dispose();
}

void OutputDevice::dispose()
{
    if ( GetUnoGraphicsList() )
    {
        UnoWrapperBase* pWrapper = Application::GetUnoWrapper( false );
diff --git a/vcl/source/window/window.cxx b/vcl/source/window/window.cxx
index d2177e4..31f537c 100644
--- a/vcl/source/window/window.cxx
+++ b/vcl/source/window/window.cxx
@@ -137,23 +137,10 @@ bool Window::IsDisposed() const
    return !mpWindowImpl;
}

void Window::disposeOnce()
{
    if (!mpWindowImpl || mpWindowImpl->mbInDispose)
        return;
    mpWindowImpl->mbInDispose = true;

    // catch badness where our Window was not wrapped safely
    // in a VclPtr cosily.
    assert( mnRefCnt>0 );

    // hold a ref in case something silly happens during dispose.
    VclPtr<Window> aRef(this);
    dispose();
}

void Window::dispose()
{
    mpWindowImpl->mbInDispose = true;

    assert( mpWindowImpl && mpWindowImpl->mbInDispose ); // should only be called from disposeOnce()
    assert( !mpWindowImpl->mpParent ||
            !mpWindowImpl->mpParent->IsDisposed() ||
@@ -585,6 +572,8 @@ void Window::dispose()

    // should be the last statements
    delete mpWindowImpl; mpWindowImpl = NULL;

    OutputDevice::dispose();
}

Window::~Window()