use insert function instead of for loop
[LibreOffice.git] / extensions / source / propctrlr / commoncontrol.hxx
blob746f2f56f6003adb2b40bc8b8fe8269994c070a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
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>
30 class NotifyEvent;
31 class ColorListBox;
32 class SvtCalendarBox;
34 namespace pcr
37 //= CommonBehaviourControlHelper
39 /** A helper class for implementing the <type scope="css::inspection">XPropertyControl</type>
40 or one of its derived interfaces.
42 This class is used as a base class the CommonBehaviourControl template.
44 class CommonBehaviourControlHelper
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;
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/
63 CommonBehaviourControlHelper(
64 sal_Int16 nControlType,
65 css::inspection::XPropertyControl& rAntiImpl);
67 virtual ~CommonBehaviourControlHelper();
69 virtual void setModified() { m_bModified = true; }
71 virtual void editChanged();
73 // XPropertyControl
74 /// @throws css::uno::RuntimeException
75 ::sal_Int16 getControlType() const { return m_nControlType; }
76 /// @throws css::uno::RuntimeException
77 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
81 bool isModified( ) const { return m_bModified; }
82 /// @throws css::uno::RuntimeException
83 void notifyModifiedValue( );
85 virtual weld::Widget* getWidget() = 0;
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 );
100 //= CommonBehaviourControl
102 /** implements a base class for <type scope="css::inspection">XPropertyControl</type>
103 implementations
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
110 template < class TControlInterface, class TControlWindow >
111 class CommonBehaviourControl :public ::cppu::BaseMutex
112 ,public ::cppu::WeakComponentImplHelper< TControlInterface >
113 ,public CommonBehaviourControlHelper
115 protected:
116 typedef ::cppu::WeakComponentImplHelper< TControlInterface > ComponentBaseClass;
118 inline CommonBehaviourControl(sal_Int16 nControlType,
119 std::unique_ptr<weld::Builder> xBuilder,
120 std::unique_ptr<TControlWindow> xWidget,
121 bool bReadOnly);
123 virtual ~CommonBehaviourControl() override
125 clear_widgetry();
128 // XPropertyControl - delegated to ->m_aImplControl
129 virtual ::sal_Int16 SAL_CALL getControlType() override
130 { return CommonBehaviourControlHelper::getControlType(); }
131 virtual css::uno::Reference< css::inspection::XPropertyControlContext > SAL_CALL getControlContext() override
132 { return CommonBehaviourControlHelper::getControlContext(); }
133 virtual void SAL_CALL setControlContext( const css::uno::Reference< css::inspection::XPropertyControlContext >& controlcontext ) override
134 { CommonBehaviourControlHelper::setControlContext( controlcontext ); }
135 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL getControlWindow() override
136 { return new weld::TransportAsXWindow(getWidget()); }
137 virtual sal_Bool SAL_CALL isModified( ) override
138 { return CommonBehaviourControlHelper::isModified(); }
139 virtual void SAL_CALL notifyModifiedValue( ) override
140 { CommonBehaviourControlHelper::notifyModifiedValue(); }
142 void clear_widgetry()
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();
153 // XComponent
154 virtual void SAL_CALL disposing() override
156 clear_widgetry();
159 TControlWindow* getTypedControlWindow()
160 { return m_xControlWindow.get(); }
161 const TControlWindow* getTypedControlWindow() const
162 { return m_xControlWindow.get(); }
164 virtual void SetModifyHandler()
166 m_xControlWindow->connect_focus_in( LINK( this, CommonBehaviourControlHelper, GetFocusHdl ) );
167 m_xControlWindow->connect_focus_out( LINK( this, CommonBehaviourControlHelper, LoseFocusHdl ) );
170 /** checks whether the instance is already disposed
171 @throws DisposedException
172 if the instance is already disposed
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;
181 //= CommonBehaviourControl - implementation
182 template< class TControlInterface, class TControlWindow >
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))
192 if (bReadOnly)
194 // disable widget by default, entries will override to enable the widget but set it non-editable
195 m_xControlWindow->set_sensitive(false);
199 template< class TControlInterface, class TControlWindow >
200 inline void CommonBehaviourControl< TControlInterface, TControlWindow >::impl_checkDisposed_throw()
202 if ( ComponentBaseClass::rBHelper.bDisposed )
203 throw css::lang::DisposedException( OUString(), *this );
206 } // namespace pcr
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */