summaryrefslogtreecommitdiff
path: root/extensions/source/propctrlr/commoncontrol.hxx
blob: fc77505352f57729ca90bd114c1310f2728c2a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_COMMONCONTROL_HXX
#define INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_COMMONCONTROL_HXX

#include <com/sun/star/inspection/XPropertyControl.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/basemutex.hxx>
#include <tools/link.hxx>
#include <tools/wintypes.hxx>
#include <vcl/weld.hxx>
#include <vcl/weldutils.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <type_traits>

class NotifyEvent;
class ColorListBox;
class SvtCalendarBox;

namespace pcr
{

    //= CommonBehaviourControlHelper

    /** A helper class for implementing the <type scope="css::inspection">XPropertyControl</type>
        or one of its derived interfaces.

        This class is used as a base class the CommonBehaviourControl template.
    */
    class CommonBehaviourControlHelper
    {
    private:
        sal_Int16                       m_nControlType;
        css::uno::Reference< css::inspection::XPropertyControlContext >
                                        m_xContext;
        css::inspection::XPropertyControl&
                                        m_rAntiImpl;
        bool                            m_bModified;

    public:
        /** creates the instance
            @param  nControlType
                the type of the control - one of the <type scope="css::inspection">PropertyControlType</type>
                constants
            @param pAntiImpl
                Reference to the instance as whose "impl-class" we act i.e. the CommonBehaviourControl<> template,
                which is why we hold it without acquiring it/
        */
        CommonBehaviourControlHelper(
            sal_Int16 nControlType,
            css::inspection::XPropertyControl& rAntiImpl);

        virtual ~CommonBehaviourControlHelper();

        virtual void setModified() { m_bModified = true; }

        // XPropertyControl
        /// @throws css::uno::RuntimeException
        ::sal_Int16 getControlType() const { return m_nControlType; }
        /// @throws css::uno::RuntimeException
        const css::uno::Reference< css::inspection::XPropertyControlContext >& getControlContext() const { return m_xContext; }
        /// @throws css::uno::RuntimeException
        void setControlContext( const css::uno::Reference< css::inspection::XPropertyControlContext >& controlcontext );
        /// @throws css::uno::RuntimeException
        bool isModified(  ) const { return m_bModified; }
        /// @throws css::uno::RuntimeException
        void notifyModifiedValue(  );

        virtual weld::Widget* getWidget() = 0;

        /// may be used by derived classes, they forward the event to the PropCtrListener
        DECL_LINK( ModifiedHdl, weld::ComboBox&, void );
        DECL_LINK( ColorModifiedHdl, ColorListBox&, void );
        DECL_LINK( EditModifiedHdl, weld::Entry&, void );
        DECL_LINK( MetricModifiedHdl, weld::MetricSpinButton&, void );
        DECL_LINK( FormattedModifiedHdl, weld::FormattedSpinButton&, void );
        DECL_LINK( TimeModifiedHdl, weld::TimeSpinButton&, void );
        DECL_LINK( DateModifiedHdl, SvtCalendarBox&, void );
        DECL_LINK( GetFocusHdl, weld::Widget&, void );
        DECL_LINK( LoseFocusHdl, weld::Widget&, void );
    };


    //= CommonBehaviourControl

    /** implements a base class for <type scope="css::inspection">XPropertyControl</type>
        implementations

        @param TControlInterface
            an interface class which is derived from (or identical to) <type scope="css::inspection">XPropertyControl</type>
        @param TControlWindow
            a class which is derived from weld::Widget
    */
    template < class TControlInterface, class TControlWindow >
    class CommonBehaviourControl    :public ::cppu::BaseMutex
                                    ,public ::cppu::WeakComponentImplHelper< TControlInterface >
                                    ,public CommonBehaviourControlHelper
    {
    protected:
        typedef ::cppu::WeakComponentImplHelper< TControlInterface >    ComponentBaseClass;

        inline CommonBehaviourControl(sal_Int16 nControlType,
                                      std::unique_ptr<weld::Builder> xBuilder,
                                      std::unique_ptr<TControlWindow> xWidget,
                                      bool bReadOnly);

        virtual ~CommonBehaviourControl() override
        {
            clear_widgetry();
        }

        // XPropertyControl - delegated to ->m_aImplControl
        virtual ::sal_Int16 SAL_CALL getControlType() override
            { return CommonBehaviourControlHelper::getControlType(); }
        virtual css::uno::Reference< css::inspection::XPropertyControlContext > SAL_CALL getControlContext() override
            { return CommonBehaviourControlHelper::getControlContext(); }
        virtual void SAL_CALL setControlContext( const css::uno::Reference< css::inspection::XPropertyControlContext >& controlcontext ) override
            { CommonBehaviourControlHelper::setControlContext( controlcontext ); }
        virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getControlWindow() override
            { return new weld::TransportAsXWindow(getWidget()); }
        virtual sal_Bool SAL_CALL isModified(  ) override
            { return CommonBehaviourControlHelper::isModified(); }
        virtual void SAL_CALL notifyModifiedValue(  ) override
            { CommonBehaviourControlHelper::notifyModifiedValue(); }

        void clear_widgetry()
        {
            if (!m_xControlWindow)
                return;
            weld::Widget* pWidget = getWidget();
            std::unique_ptr<weld::Container> xParent(pWidget->weld_parent());
            xParent->move(pWidget, nullptr);
            m_xControlWindow.reset();
            m_xBuilder.reset();
        }

        // XComponent
        virtual void SAL_CALL disposing() override
        {
            clear_widgetry();
        }

        TControlWindow*       getTypedControlWindow()
            { return m_xControlWindow.get(); }
        const TControlWindow* getTypedControlWindow() const
            { return m_xControlWindow.get(); }

        virtual void SetModifyHandler()
        {
            m_xControlWindow->connect_focus_in( LINK( this, CommonBehaviourControlHelper, GetFocusHdl ) );
            m_xControlWindow->connect_focus_out( LINK( this, CommonBehaviourControlHelper, LoseFocusHdl ) );
        }

        /** checks whether the instance is already disposed
            @throws DisposedException
                if the instance is already disposed
        */
        inline void impl_checkDisposed_throw();
    protected:
        std::unique_ptr<weld::Builder> m_xBuilder;
    private:
        std::unique_ptr<TControlWindow> m_xControlWindow;
    };

    //= CommonBehaviourControl - implementation
    template< class TControlInterface, class TControlWindow >
    inline CommonBehaviourControl< TControlInterface, TControlWindow >::CommonBehaviourControl(sal_Int16 nControlType,
                                                                                               std::unique_ptr<weld::Builder> xBuilder,
                                                                                               std::unique_ptr<TControlWindow> xWidget,
                                                                                               bool bReadOnly)
        : ComponentBaseClass( m_aMutex )
        , CommonBehaviourControlHelper( nControlType, *this )
        , m_xBuilder(std::move(xBuilder))
        , m_xControlWindow(std::move(xWidget))
    {
        if (bReadOnly)
        {
            // disable widget by default, entries will override to enable the widget but set it non-editable
            m_xControlWindow->set_sensitive(false);
        }
    }

    template< class TControlInterface, class TControlWindow >
    inline void CommonBehaviourControl< TControlInterface, TControlWindow >::impl_checkDisposed_throw()
    {
        if ( ComponentBaseClass::rBHelper.bDisposed )
            throw css::lang::DisposedException( OUString(), *this );
    }

} // namespace pcr

#endif // INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_COMMONCONTROL_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */