Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / querydesign / limitboxcontroller.cxx
blob59d563388fd5a27fc52427609c74ffcc5c89350e
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/.
8 */
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;
30 namespace
33 /// Default values
34 sal_Int64 const aDefLimitAry[] =
37 10,
38 20,
44 namespace dbaui
47 /**
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
53 public:
54 LimitBox(vcl::Window* pParent, LimitBoxController* pCtrl)
55 : InterimItemWindow(pParent, u"dbaccess/ui/limitbox.ui"_ustr, u"LimitBox"_ustr)
56 , m_pControl( pCtrl )
57 , m_xWidget(m_xBuilder->weld_combo_box(u"limit"_ustr))
59 InitControlBase(m_xWidget.get());
61 LoadDefaultLimits();
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
73 m_xWidget.reset();
74 InterimItemWindow::dispose();
77 virtual ~LimitBox() override
79 disposeOnce();
82 void set_sensitive(bool bSensitive)
84 m_xWidget->set_sensitive(bSensitive);
87 void set_value(int nLimit)
89 if (nLimit < 0)
90 m_xWidget->set_active(0);
91 else
92 m_xWidget->set_entry_text(OUString::number(nLimit));
93 m_xWidget->save_value();
96 private:
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);
105 void Apply()
107 if (!m_xWidget->get_value_changed_from_saved())
108 return;
109 sal_Int64 nLimit;
110 OUString sActiveText = m_xWidget->get_active_text();
111 if (sActiveText == DBA_RES(STR_QUERY_LIMIT_ALL))
112 nLimit = -1;
113 else
115 nLimit = m_xWidget->get_active_text().toInt64();
116 if (nLimit < 0)
117 nLimit = -1;
119 set_value(nLimit);
120 m_pControl->dispatchCommand({ comphelper::makePropertyValue(u"DBLimit.Value"_ustr, nLimit) });
123 ///Initialize entries
124 void LoadDefaultLimits()
126 m_xWidget->freeze();
127 m_xWidget->append_text(DBA_RES(STR_QUERY_LIMIT_ALL));
128 for (auto nIndex : aDefLimitAry)
130 m_xWidget->append_text(OUString::number(nIndex));
132 m_xWidget->thaw();
136 IMPL_LINK(LimitBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
138 bool bHandled = false;
139 const sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
140 switch (nCode)
142 case KEY_ESCAPE:
143 m_xWidget->set_entry_text(m_xWidget->get_saved_value());
144 bHandled = true;
145 break;
146 case KEY_RETURN:
148 bHandled = ActivateHdl(*m_xWidget);
149 break;
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
158 return;
159 Apply();
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();
171 Apply();
172 return true;
175 LimitBoxController::LimitBoxController(
176 const uno::Reference< uno::XComponentContext >& rxContext ) :
177 LimitBoxController_Base( rxContext,
178 uno::Reference< frame::XFrame >(),
179 ".uno:DBLimit" ),
180 m_xLimitBox( nullptr )
184 LimitBoxController::~LimitBoxController()
188 /// XServiceInfo
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)
199 return true;
201 return false;
204 css::uno::Sequence< OUString > SAL_CALL LimitBoxController::getSupportedServiceNames()
206 return { u"com.sun.star.frame.ToolbarController"_ustr };
209 /// XComponent
210 void SAL_CALL LimitBoxController::dispose()
212 svt::ToolboxController::dispose();
214 SolarMutexGuard aSolarMutexGuard;
215 m_xLimitBox.disposeAndClear();
218 /// XStatusListener
219 void SAL_CALL LimitBoxController::statusChanged(
220 const frame::FeatureStateEvent& rEvent )
222 if ( !m_xLimitBox )
223 return;
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);
235 else
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 );
264 if ( pParent )
266 SolarMutexGuard aSolarMutexGuard;
267 m_xLimitBox = VclPtr<LimitBox>::Create(pParent, this);
268 xItemWindow = VCLUnoHelper::GetInterface(m_xLimitBox);
271 return xItemWindow;
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() )
280 util::URL aURL;
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 );
292 } // dbaui namespace
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: */