vcl: more rtl::Reference cleanup

Change-Id: Idef6b4259d784120a06d2a6c51b77029566da59f
diff --git a/include/vcl/fixed.hxx b/include/vcl/fixed.hxx
index 30bbd95..e9688d5 100644
--- a/include/vcl/fixed.hxx
+++ b/include/vcl/fixed.hxx
@@ -62,6 +62,7 @@ public:
    explicit        FixedText( vcl::Window* pParent, WinBits nStyle = 0 );
    explicit        FixedText( vcl::Window* pParent, const ResId& rResId );
    virtual         ~FixedText();
    virtual void    dispose() SAL_OVERRIDE;

    virtual void    Paint( const Rectangle& rRect ) SAL_OVERRIDE;
    virtual void    Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags ) SAL_OVERRIDE;
diff --git a/include/vcl/layout.hxx b/include/vcl/layout.hxx
index e2893de..9f58876 100644
--- a/include/vcl/layout.hxx
+++ b/include/vcl/layout.hxx
@@ -742,6 +742,7 @@ public:
    void set_primary_text(const OUString &rPrimaryString);
    void set_secondary_text(const OUString &rSecondaryString);
    virtual ~MessageDialog();
    virtual void dispose() SAL_OVERRIDE;

    static void SetMessagesWidths(vcl::Window *pParent, VclMultiLineEdit *pPrimaryMessage,
        VclMultiLineEdit *pSecondaryMessage);
diff --git a/vcl/CppunitTest_vcl_lifecycle.mk b/vcl/CppunitTest_vcl_lifecycle.mk
new file mode 100644
index 0000000..54d4aff
--- /dev/null
+++ b/vcl/CppunitTest_vcl_lifecycle.mk
@@ -0,0 +1,52 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

$(eval $(call gb_CppunitTest_CppunitTest,vcl_lifecycle))

$(eval $(call gb_CppunitTest_set_include,vcl_lifecycle,\
    $$(INCLUDE) \
    -I$(SRCDIR)/vcl/inc \
))

$(eval $(call gb_CppunitTest_add_exception_objects,vcl_lifecycle, \
	vcl/qa/cppunit/lifecycle \
))

$(eval $(call gb_CppunitTest_use_externals,vcl_lifecycle,boost_headers))

$(eval $(call gb_CppunitTest_use_libraries,vcl_lifecycle, \
	comphelper \
	cppu \
	cppuhelper \
	sal \
	svt \
	test \
	tl \
	unotest \
	vcl \
	$(gb_UWINAPI) \
))

$(eval $(call gb_CppunitTest_use_api,vcl_lifecycle,\
	udkapi \
	offapi \
))

$(eval $(call gb_CppunitTest_use_ure,vcl_lifecycle))
$(eval $(call gb_CppunitTest_use_vcl,vcl_lifecycle))

$(eval $(call gb_CppunitTest_use_components,vcl_lifecycle,\
	configmgr/source/configmgr \
	i18npool/util/i18npool \
	ucb/source/core/ucb1 \
))

$(eval $(call gb_CppunitTest_use_configuration,vcl_lifecycle))

# vim: set noet sw=4 ts=4:
diff --git a/vcl/Module_vcl.mk b/vcl/Module_vcl.mk
index f5be45e..8cb05ba 100644
--- a/vcl/Module_vcl.mk
+++ b/vcl/Module_vcl.mk
@@ -103,6 +103,7 @@ $(eval $(call gb_Module_add_check_targets,vcl,\
	CppunitTest_vcl_fontcharmap \
	CppunitTest_vcl_complextext \
	CppunitTest_vcl_filters_test \
	CppunitTest_vcl_lifecycle \
	CppunitTest_vcl_outdev \
	CppunitTest_vcl_app_test \
	CppunitTest_vcl_wmf_test \
diff --git a/vcl/qa/cppunit/lifecycle.cxx b/vcl/qa/cppunit/lifecycle.cxx
new file mode 100644
index 0000000..d635dd6
--- /dev/null
+++ b/vcl/qa/cppunit/lifecycle.cxx
@@ -0,0 +1,64 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <unotest/filters-test.hxx>
#include <test/bootstrapfixture.hxx>

#include <vcl/wrkwin.hxx>
#include <vcl/button.hxx>

class LifecycleTest : public test::BootstrapFixture
{
    void testWidgets(vcl::Window *pParent);

public:
    LifecycleTest() : BootstrapFixture(true, false) {}

    void testIsolatedWidgets();
    void testParentedWidgets();

    CPPUNIT_TEST_SUITE(LifecycleTest);
    CPPUNIT_TEST(testIsolatedWidgets);
    CPPUNIT_TEST(testParentedWidgets);
    CPPUNIT_TEST_SUITE_END();
};

void LifecycleTest::testWidgets(vcl::Window *pParent)
{
    { PushButtonPtr   aPtr(new PushButton(pParent));   }
    { OKButtonPtr     aPtr(new OKButton(pParent));     }
    { CancelButtonPtr aPtr(new CancelButton(pParent)); }
    { HelpButtonPtr   aPtr(new HelpButton(pParent));   }

    // Some widgets really insist on adoption.
    if (pParent)
    {
        { CheckBoxPtr     aPtr(new CheckBox(pParent));  }
    }
//    { RadioButtonPtr  aPtr(new RadioButton(pParent));  }
}

void LifecycleTest::testIsolatedWidgets()
{
    testWidgets(NULL);
}

void LifecycleTest::testParentedWidgets()
{
    VclReference<WorkWindow> xWin(new WorkWindow((vcl::Window *)NULL,
                                                 WB_APP|WB_STDWORK));
    CPPUNIT_ASSERT(xWin.get() != NULL);
    testWidgets(xWin.get());
}

CPPUNIT_TEST_SUITE_REGISTRATION(LifecycleTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/control/fixed.cxx b/vcl/source/control/fixed.cxx
index 62c6135..5ee2a95 100644
--- a/vcl/source/control/fixed.cxx
+++ b/vcl/source/control/fixed.cxx
@@ -460,7 +460,13 @@ void FixedText::set_mnemonic_widget(vcl::Window *pWindow)

FixedText::~FixedText()
{
    dispose();
}

void FixedText::dispose()
{
    set_mnemonic_widget(NULL);
    Control::dispose();
}

SelectableFixedText::SelectableFixedText(vcl::Window* pParent, WinBits nStyle)
diff --git a/vcl/source/window/layout.cxx b/vcl/source/window/layout.cxx
index 74a9593..b0e27e7 100644
--- a/vcl/source/window/layout.cxx
+++ b/vcl/source/window/layout.cxx
@@ -1992,16 +1992,36 @@ MessageDialog::MessageDialog(vcl::Window* pParent, const OString& rID, const OUS
{
}

MessageDialog::~MessageDialog()
void MessageDialog::dispose()
{
    for (size_t i = 0; i < m_aOwnedButtons.size(); ++i)
        delete m_aOwnedButtons[i];
    m_aOwnedButtons.clear();

    delete m_pSecondaryMessage;
    m_pSecondaryMessage = NULL;

    delete m_pPrimaryMessage;
    m_pSecondaryMessage = NULL;

    delete m_pImage;
    m_pImage = NULL;

    delete m_pGrid;
    m_pGrid = NULL;

    delete m_pOwnedActionArea;
    m_pOwnedActionArea = NULL;

    delete m_pOwnedContentArea;
    m_pOwnedContentArea = NULL;

    Dialog::dispose();
}

MessageDialog::~MessageDialog()
{
    dispose();
}

void MessageDialog::response(short nResponseId)
diff --git a/vcl/source/window/window.cxx b/vcl/source/window/window.cxx
index 156cc66..122181d 100644
--- a/vcl/source/window/window.cxx
+++ b/vcl/source/window/window.cxx
@@ -551,6 +551,8 @@ void Window::dispose()
            vcl::Window* pSysWin = pSVData->maWinData.mpFirstFrame;
            while ( pSysWin->mpWindowImpl->mpFrameData->mpNextFrame != this )
                pSysWin = pSysWin->mpWindowImpl->mpFrameData->mpNextFrame;

            assert (mpWindowImpl->mpFrameData->mpNextFrame != pSysWin);
            pSysWin->mpWindowImpl->mpFrameData->mpNextFrame = mpWindowImpl->mpFrameData->mpNextFrame;
        }
        mpWindowImpl->mpFrame->SetCallback( NULL, NULL );
@@ -565,7 +567,7 @@ void Window::dispose()
Window::~Window()
{
    vcl::LazyDeletor<vcl::Window>::Undelete( this );

    dispose ();
    DBG_ASSERT( !mpWindowImpl->mbInDtor, "~Window - already in DTOR!" );
}

@@ -990,6 +992,7 @@ void Window::ImplInit( vcl::Window* pParent, WinBits nStyle, SystemParentData* p
        mpWindowImpl->mpOverlapWindow = this;

        // set frame data
        assert (pSVData->maWinData.mpFirstFrame != this);
        mpWindowImpl->mpFrameData->mpNextFrame        = pSVData->maWinData.mpFirstFrame;
        pSVData->maWinData.mpFirstFrame = this;
        mpWindowImpl->mpFrameData->mpFirstOverlap     = NULL;