1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <com/sun/star/inspection/XPropertyControl.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <cppuhelper/compbase.hxx>
25 #include <cppuhelper/basemutex.hxx>
26 #include <tools/link.hxx>
27 #include <vcl/weld.hxx>
28 #include <vcl/weldutils.hxx>
29 
30 class NotifyEvent;
31 class ColorListBox;
32 class SvtCalendarBox;
33 
34 namespace pcr
35 {
36 
37     //= CommonBehaviourControlHelper
38 
39     /** A helper class for implementing the <type scope="css::inspection">XPropertyControl</type>
40         or one of its derived interfaces.
41 
42         This class is used as a base class the CommonBehaviourControl template.
43     */
44     class CommonBehaviourControlHelper
45     {
46     private:
47         sal_Int16                       m_nControlType;
48         css::uno::Reference< css::inspection::XPropertyControlContext >
49                                         m_xContext;
50         css::inspection::XPropertyControl&
51                                         m_rAntiImpl;
52         bool                            m_bModified;
53 
54     public:
55         /** creates the instance
56             @param  nControlType
57                 the type of the control - one of the <type scope="css::inspection">PropertyControlType</type>
58                 constants
59             @param pAntiImpl
60                 Reference to the instance as whose "impl-class" we act i.e. the CommonBehaviourControl<> template,
61                 which is why we hold it without acquiring it/
62         */
63         CommonBehaviourControlHelper(
64             sal_Int16 nControlType,
65             css::inspection::XPropertyControl& rAntiImpl);
66 
67         virtual ~CommonBehaviourControlHelper();
68 
setModified()69         virtual void setModified() { m_bModified = true; }
70 
71         virtual void editChanged();
72 
73         // XPropertyControl
74         /// @throws css::uno::RuntimeException
getControlType() const75         ::sal_Int16 getControlType() const { return m_nControlType; }
76         /// @throws css::uno::RuntimeException
getControlContext() const77         const css::uno::Reference< css::inspection::XPropertyControlContext >& getControlContext() const { return m_xContext; }
78         /// @throws css::uno::RuntimeException
79         void setControlContext( const css::uno::Reference< css::inspection::XPropertyControlContext >& controlcontext );
80         /// @throws css::uno::RuntimeException
isModified() const81         bool isModified(  ) const { return m_bModified; }
82         /// @throws css::uno::RuntimeException
83         void notifyModifiedValue(  );
84 
85         virtual weld::Widget* getWidget() = 0;
86 
87         /// may be used by derived classes, they forward the event to the PropCtrListener
88         DECL_LINK( ModifiedHdl, weld::ComboBox&, void );
89         DECL_LINK( ColorModifiedHdl, ColorListBox&, void );
90         DECL_LINK( EditModifiedHdl, weld::Entry&, void );
91         DECL_LINK( MetricModifiedHdl, weld::MetricSpinButton&, void );
92         DECL_LINK( FormattedModifiedHdl, weld::FormattedSpinButton&, void );
93         DECL_LINK( TimeModifiedHdl, weld::FormattedSpinButton&, void );
94         DECL_LINK( DateModifiedHdl, SvtCalendarBox&, void );
95         DECL_LINK( GetFocusHdl, weld::Widget&, void );
96         DECL_LINK( LoseFocusHdl, weld::Widget&, void );
97     };
98 
99 
100     //= CommonBehaviourControl
101 
102     /** implements a base class for <type scope="css::inspection">XPropertyControl</type>
103         implementations
104 
105         @param TControlInterface
106             an interface class which is derived from (or identical to) <type scope="css::inspection">XPropertyControl</type>
107         @param TControlWindow
108             a class which is derived from weld::Widget
109     */
110     template < class TControlInterface, class TControlWindow >
111     class CommonBehaviourControl    :public ::cppu::BaseMutex
112                                     ,public ::cppu::WeakComponentImplHelper< TControlInterface >
113                                     ,public CommonBehaviourControlHelper
114     {
115     protected:
116         typedef ::cppu::WeakComponentImplHelper< TControlInterface >    ComponentBaseClass;
117 
118         inline CommonBehaviourControl(sal_Int16 nControlType,
119                                       std::unique_ptr<weld::Builder> xBuilder,
120                                       std::unique_ptr<TControlWindow> xWidget,
121                                       bool bReadOnly);
122 
~CommonBehaviourControl()123         virtual ~CommonBehaviourControl() override
124         {
125             clear_widgetry();
126         }
127 
128         // XPropertyControl - delegated to ->m_aImplControl
getControlType()129         virtual ::sal_Int16 SAL_CALL getControlType() override
130             { return CommonBehaviourControlHelper::getControlType(); }
getControlContext()131         virtual css::uno::Reference< css::inspection::XPropertyControlContext > SAL_CALL getControlContext() override
132             { return CommonBehaviourControlHelper::getControlContext(); }
setControlContext(const css::uno::Reference<css::inspection::XPropertyControlContext> & controlcontext)133         virtual void SAL_CALL setControlContext( const css::uno::Reference< css::inspection::XPropertyControlContext >& controlcontext ) override
134             { CommonBehaviourControlHelper::setControlContext( controlcontext ); }
getControlWindow()135         virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getControlWindow() override
136             { return new weld::TransportAsXWindow(getWidget()); }
isModified()137         virtual sal_Bool SAL_CALL isModified(  ) override
138             { return CommonBehaviourControlHelper::isModified(); }
notifyModifiedValue()139         virtual void SAL_CALL notifyModifiedValue(  ) override
140             { CommonBehaviourControlHelper::notifyModifiedValue(); }
141 
clear_widgetry()142         void clear_widgetry()
143         {
144             if (!m_xControlWindow)
145                 return;
146             weld::Widget* pWidget = getWidget();
147             std::unique_ptr<weld::Container> xParent(pWidget->weld_parent());
148             xParent->move(pWidget, nullptr);
149             m_xControlWindow.reset();
150             m_xBuilder.reset();
151         }
152 
153         // XComponent
disposing()154         virtual void SAL_CALL disposing() override
155         {
156             clear_widgetry();
157         }
158 
getTypedControlWindow()159         TControlWindow*       getTypedControlWindow()
160             { return m_xControlWindow.get(); }
getTypedControlWindow() const161         const TControlWindow* getTypedControlWindow() const
162             { return m_xControlWindow.get(); }
163 
SetModifyHandler()164         virtual void SetModifyHandler()
165         {
166             m_xControlWindow->connect_focus_in( LINK( this, CommonBehaviourControlHelper, GetFocusHdl ) );
167             m_xControlWindow->connect_focus_out( LINK( this, CommonBehaviourControlHelper, LoseFocusHdl ) );
168         }
169 
170         /** checks whether the instance is already disposed
171             @throws DisposedException
172                 if the instance is already disposed
173         */
174         inline void impl_checkDisposed_throw();
175     protected:
176         std::unique_ptr<weld::Builder> m_xBuilder;
177     private:
178         std::unique_ptr<TControlWindow> m_xControlWindow;
179     };
180 
181     //= CommonBehaviourControl - implementation
182     template< class TControlInterface, class TControlWindow >
CommonBehaviourControl(sal_Int16 nControlType,std::unique_ptr<weld::Builder> xBuilder,std::unique_ptr<TControlWindow> xWidget,bool bReadOnly)183     inline CommonBehaviourControl< TControlInterface, TControlWindow >::CommonBehaviourControl(sal_Int16 nControlType,
184                                                                                                std::unique_ptr<weld::Builder> xBuilder,
185                                                                                                std::unique_ptr<TControlWindow> xWidget,
186                                                                                                bool bReadOnly)
187         : ComponentBaseClass( m_aMutex )
188         , CommonBehaviourControlHelper( nControlType, *this )
189         , m_xBuilder(std::move(xBuilder))
190         , m_xControlWindow(std::move(xWidget))
191     {
192         if (bReadOnly)
193         {
194             // disable widget by default, entries will override to enable the widget but set it non-editable
195             m_xControlWindow->set_sensitive(false);
196         }
197     }
198 
199     template< class TControlInterface, class TControlWindow >
impl_checkDisposed_throw()200     inline void CommonBehaviourControl< TControlInterface, TControlWindow >::impl_checkDisposed_throw()
201     {
202         if ( ComponentBaseClass::rBHelper.bDisposed )
203             throw css::lang::DisposedException( OUString(), *this );
204     }
205 
206 } // namespace pcr
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
209