1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include "limitboxcontroller.hxx"
12 #include <com/sun/star/frame/XDispatchProvider.hpp>
13 #include <com/sun/star/frame/XFrame.hpp>
14 #include <com/sun/star/beans/PropertyValue.hpp>
15 #include <com/sun/star/util/XURLTransformer.hpp>
17 #include <comphelper/propertyvalue.hxx>
18 #include <vcl/InterimItemWindow.hxx>
19 #include <vcl/event.hxx>
20 #include <vcl/svapp.hxx>
21 #include <vcl/window.hxx>
22 #include <toolkit/helper/vclunohelper.hxx>
23 #include <cppuhelper/queryinterface.hxx>
25 #include <core_resource.hxx>
26 #include <strings.hrc>
28 using namespace ::com::sun::star
;
34 sal_Int64
const aDefLimitAry
[] =
48 * Input box to add limit to an SQL query (maximum number of result's rows)
49 * This box is reachable on the Query Design Toolbar
51 class LimitBox final
: public InterimItemWindow
54 LimitBox(vcl::Window
* pParent
, LimitBoxController
* pCtrl
)
55 : InterimItemWindow(pParent
, u
"dbaccess/ui/limitbox.ui"_ustr
, u
"LimitBox"_ustr
)
57 , m_xWidget(m_xBuilder
->weld_combo_box(u
"limit"_ustr
))
59 InitControlBase(m_xWidget
.get());
63 m_xWidget
->connect_key_press(LINK(this, LimitBox
, KeyInputHdl
));
64 m_xWidget
->connect_entry_activate(LINK(this, LimitBox
, ActivateHdl
));
65 m_xWidget
->connect_changed(LINK(this, LimitBox
, ChangeHdl
));
66 m_xWidget
->connect_focus_out(LINK(this, LimitBox
, FocusOutHdl
));
67 m_xWidget
->set_entry_width_chars(6);
68 SetSizePixel(m_xContainer
->get_preferred_size());
71 virtual void dispose() override
74 InterimItemWindow::dispose();
77 virtual ~LimitBox() override
82 void set_sensitive(bool bSensitive
)
84 m_xWidget
->set_sensitive(bSensitive
);
87 void set_value(int nLimit
)
90 m_xWidget
->set_active(0);
92 m_xWidget
->set_entry_text(OUString::number(nLimit
));
93 m_xWidget
->save_value();
97 LimitBoxController
* m_pControl
;
98 std::unique_ptr
<weld::ComboBox
> m_xWidget
;
100 DECL_LINK(KeyInputHdl
, const KeyEvent
&, bool);
101 DECL_LINK(ActivateHdl
, weld::ComboBox
&, bool);
102 DECL_LINK(ChangeHdl
, weld::ComboBox
&, void);
103 DECL_LINK(FocusOutHdl
, weld::Widget
&, void);
107 if (!m_xWidget
->get_value_changed_from_saved())
110 OUString sActiveText
= m_xWidget
->get_active_text();
111 if (sActiveText
== DBA_RES(STR_QUERY_LIMIT_ALL
))
115 nLimit
= m_xWidget
->get_active_text().toInt64();
120 m_pControl
->dispatchCommand({ comphelper::makePropertyValue(u
"DBLimit.Value"_ustr
, nLimit
) });
123 ///Initialize entries
124 void LoadDefaultLimits()
127 m_xWidget
->append_text(DBA_RES(STR_QUERY_LIMIT_ALL
));
128 for (auto nIndex
: aDefLimitAry
)
130 m_xWidget
->append_text(OUString::number(nIndex
));
136 IMPL_LINK(LimitBox
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
138 bool bHandled
= false;
139 const sal_uInt16 nCode
= rKEvt
.GetKeyCode().GetCode();
143 m_xWidget
->set_entry_text(m_xWidget
->get_saved_value());
148 bHandled
= ActivateHdl(*m_xWidget
);
152 return bHandled
|| ChildKeyInput(rKEvt
);
155 IMPL_LINK_NOARG(LimitBox
, FocusOutHdl
, weld::Widget
&, void)
157 if (!m_xWidget
|| m_xWidget
->has_focus()) // comboboxes can be comprised of multiple widgets, ensure all have lost focus
162 IMPL_LINK(LimitBox
, ChangeHdl
, weld::ComboBox
&, rComboBox
, void)
164 if (rComboBox
.changed_by_direct_pick())
165 ActivateHdl(rComboBox
);
168 IMPL_LINK_NOARG(LimitBox
, ActivateHdl
, weld::ComboBox
&, bool)
170 GrabFocusToDocument();
175 LimitBoxController::LimitBoxController(
176 const uno::Reference
< uno::XComponentContext
>& rxContext
) :
177 LimitBoxController_Base( rxContext
,
178 uno::Reference
< frame::XFrame
>(),
180 m_xLimitBox( nullptr )
184 LimitBoxController::~LimitBoxController()
189 OUString SAL_CALL
LimitBoxController::getImplementationName()
191 return u
"org.libreoffice.comp.dbu.LimitBoxController"_ustr
;
194 sal_Bool SAL_CALL
LimitBoxController::supportsService(const OUString
& _rServiceName
)
196 const css::uno::Sequence
< OUString
> aSupported(getSupportedServiceNames());
197 for (const OUString
& s
: aSupported
)
198 if (s
== _rServiceName
)
204 css::uno::Sequence
< OUString
> SAL_CALL
LimitBoxController::getSupportedServiceNames()
206 return { u
"com.sun.star.frame.ToolbarController"_ustr
};
210 void SAL_CALL
LimitBoxController::dispose()
212 svt::ToolboxController::dispose();
214 SolarMutexGuard aSolarMutexGuard
;
215 m_xLimitBox
.disposeAndClear();
219 void SAL_CALL
LimitBoxController::statusChanged(
220 const frame::FeatureStateEvent
& rEvent
)
225 SolarMutexGuard aSolarMutexGuard
;
226 if ( rEvent
.FeatureURL
.Path
== "DBLimit" )
228 if ( rEvent
.IsEnabled
)
230 m_xLimitBox
->set_sensitive(true);
231 sal_Int64 nLimit
= 0;
232 if (rEvent
.State
>>= nLimit
)
233 m_xLimitBox
->set_value(nLimit
);
236 m_xLimitBox
->set_sensitive(false);
240 /// XToolbarController
241 void SAL_CALL
LimitBoxController::execute( sal_Int16
/*KeyModifier*/ )
245 void SAL_CALL
LimitBoxController::click()
249 void SAL_CALL
LimitBoxController::doubleClick()
253 uno::Reference
< awt::XWindow
> SAL_CALL
LimitBoxController::createPopupWindow()
255 return uno::Reference
< awt::XWindow
>();
258 uno::Reference
< awt::XWindow
> SAL_CALL
LimitBoxController::createItemWindow(
259 const uno::Reference
< awt::XWindow
>& xParent
)
261 uno::Reference
< awt::XWindow
> xItemWindow
;
263 VclPtr
<vcl::Window
> pParent
= VCLUnoHelper::GetWindow( xParent
);
266 SolarMutexGuard aSolarMutexGuard
;
267 m_xLimitBox
= VclPtr
<LimitBox
>::Create(pParent
, this);
268 xItemWindow
= VCLUnoHelper::GetInterface(m_xLimitBox
);
274 void LimitBoxController::dispatchCommand(
275 const uno::Sequence
< beans::PropertyValue
>& rArgs
)
277 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider( m_xFrame
, uno::UNO_QUERY
);
278 if ( xDispatchProvider
.is() )
281 uno::Reference
< frame::XDispatch
> xDispatch
;
282 uno::Reference
< util::XURLTransformer
> xURLTransformer
= getURLTransformer();
284 aURL
.Complete
= ".uno:DBLimit";
285 xURLTransformer
->parseStrict( aURL
);
286 xDispatch
= xDispatchProvider
->queryDispatch( aURL
, OUString(), 0 );
287 if ( xDispatch
.is() )
288 xDispatch
->dispatch( aURL
, rArgs
);
294 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
295 org_libreoffice_comp_dbu_LimitBoxController_get_implementation(
296 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
298 return cppu::acquire(new ::dbaui::LimitBoxController(context
));
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */