LibreOffice Module svx (master) 1
tbunocontroller.cxx
Go to the documentation of this file.
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#include <com/sun/star/frame/XFrame.hpp>
21#include <com/sun/star/frame/status/FontHeight.hpp>
22#include <com/sun/star/frame/XDispatchProvider.hpp>
23#include <com/sun/star/beans/PropertyValue.hpp>
24#include <com/sun/star/lang/XServiceInfo.hpp>
25#include <com/sun/star/util/XURLTransformer.hpp>
26
28#include <rtl/math.hxx>
29#include <utility>
30#include <vcl/event.hxx>
31#include <vcl/svapp.hxx>
32#include <vcl/weldutils.hxx>
33#include <vcl/window.hxx>
34#include <vcl/settings.hxx>
36#include <svtools/ctrltool.hxx>
37#include <svtools/ctrlbox.hxx>
39#include <tools/json_writer.hxx>
40#include <vcl/toolbox.hxx>
43
44#include <memory>
45
47
48using namespace ::com::sun::star;
49
50namespace {
51
52class SvxFontSizeBox_Base;
53class SvxFontSizeBox_Impl;
54
55typedef cppu::ImplInheritanceHelper< ::svt::ToolboxController, lang::XServiceInfo> FontHeightToolBoxControl_Base;
56class FontHeightToolBoxControl : public FontHeightToolBoxControl_Base
57{
58 public:
59 explicit FontHeightToolBoxControl(
60 const css::uno::Reference< css::uno::XComponentContext >& rServiceManager );
61
62 // XServiceInfo
63 virtual OUString SAL_CALL getImplementationName() override;
64 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
65 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
66
67 // XComponent
68 virtual void SAL_CALL dispose() override;
69
70 // XStatusListener
71 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;
72
73 // XToolbarController
74 virtual void SAL_CALL execute( sal_Int16 KeyModifier ) override;
75 virtual void SAL_CALL click() override;
76 virtual void SAL_CALL doubleClick() override;
77 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override;
78 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createItemWindow( const css::uno::Reference< css::awt::XWindow >& Parent ) override;
79
80 void dispatchCommand( const css::uno::Sequence< css::beans::PropertyValue >& rArgs );
82
83 private:
85 std::unique_ptr<SvxFontSizeBox_Base> m_xWeldBox;
86 SvxFontSizeBox_Base* m_pBox;
87};
88
89class SvxFontSizeBox_Base
90{
91public:
92 SvxFontSizeBox_Base(std::unique_ptr<weld::ComboBox> xWidget,
93 uno::Reference< frame::XFrame > _xFrame,
94 FontHeightToolBoxControl& rCtrl);
95
96 virtual ~SvxFontSizeBox_Base()
97 {
98 }
99
100 virtual void set_sensitive(bool bSensitive)
101 {
102 m_xWidget->set_sensitive(bSensitive);
103 }
104
105 void statusChanged_Impl(tools::Long nHeight, bool bErase);
106 void UpdateFont();
107
108protected:
109 FontHeightToolBoxControl& m_rCtrl;
110 OUString m_aCurText;
111 bool m_bRelease;
112 uno::Reference<frame::XFrame> m_xFrame;
113 std::unique_ptr<FontSizeBox> m_xWidget;
114
115 void ReleaseFocus_Impl();
116 void Select();
117
118 virtual bool DoKeyInput(const KeyEvent& rKEvt);
119
120 DECL_LINK(SelectHdl, weld::ComboBox&, void);
121 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
122 DECL_LINK(ActivateHdl, weld::ComboBox&, bool);
123 DECL_LINK(FocusOutHdl, weld::Widget&, void);
124 DECL_LINK(DumpAsPropertyTreeHdl, tools::JsonWriter&, void);
125};
126
127class SvxFontSizeBox_Impl final : public InterimItemWindow
128 , public SvxFontSizeBox_Base
129{
130public:
131 SvxFontSizeBox_Impl(vcl::Window* pParent,
132 const uno::Reference< frame::XFrame >& _xFrame,
133 FontHeightToolBoxControl& rCtrl);
134
135 virtual void dispose() override
136 {
137 m_xWidget.reset();
139 }
140
141 virtual void GetFocus() override
142 {
143 if (m_xWidget)
144 m_xWidget->grab_focus();
146 }
147
148 virtual ~SvxFontSizeBox_Impl() override
149 {
150 disposeOnce();
151 }
152
153 void SetOptimalSize();
154
155 virtual void DataChanged(const DataChangedEvent& rDCEvt) override;
156
157 virtual void set_sensitive(bool bSensitive) override
158 {
159 m_xWidget->set_sensitive(bSensitive);
160 if (bSensitive)
162 else
164 }
165
166private:
167 virtual bool DoKeyInput(const KeyEvent& rKEvt) override;
168};
169
170SvxFontSizeBox_Base::SvxFontSizeBox_Base(std::unique_ptr<weld::ComboBox> xWidget,
171 uno::Reference<frame::XFrame> xFrame,
172 FontHeightToolBoxControl& rCtrl)
173 : m_rCtrl(rCtrl)
174 , m_bRelease(true)
175 , m_xFrame(std::move(xFrame))
176 , m_xWidget(new FontSizeBox(std::move(xWidget)))
177{
178 m_xWidget->set_value(0);
179 m_xWidget->set_active_or_entry_text("");
180 m_xWidget->disable_entry_completion();
181
182 m_xWidget->connect_changed(LINK(this, SvxFontSizeBox_Base, SelectHdl));
183 m_xWidget->connect_key_press(LINK(this, SvxFontSizeBox_Base, KeyInputHdl));
184 m_xWidget->connect_entry_activate(LINK(this, SvxFontSizeBox_Base, ActivateHdl));
185 m_xWidget->connect_focus_out(LINK(this, SvxFontSizeBox_Base, FocusOutHdl));
186 m_xWidget->connect_get_property_tree(LINK(this, SvxFontSizeBox_Base, DumpAsPropertyTreeHdl));
187}
188
189void SvxFontSizeBox_Base::ReleaseFocus_Impl()
190{
191 if ( !m_bRelease )
192 {
193 m_bRelease = true;
194 return;
195 }
196
197 if ( m_xFrame.is() && m_xFrame->getContainerWindow().is() )
198 m_xFrame->getContainerWindow()->setFocus();
199}
200
201IMPL_LINK(SvxFontSizeBox_Base, SelectHdl, weld::ComboBox&, rCombo, void)
202{
203 if (rCombo.changed_by_direct_pick()) // only when picked from the list
204 Select();
205}
206
207IMPL_LINK_NOARG(SvxFontSizeBox_Base, ActivateHdl, weld::ComboBox&, bool)
208{
209 Select();
210 return true;
211}
212
213void SvxFontSizeBox_Base::Select()
214{
215 sal_Int64 nSelVal = m_xWidget->get_value();
216 float fSelVal = float( nSelVal ) / 10;
217
218 uno::Sequence< beans::PropertyValue > aArgs{ comphelper::makePropertyValue("FontHeight.Height",
219 fSelVal) };
220
221 /* #i33380# DR 2004-09-03 Moved the following line above the Dispatch() call.
222 This instance may be deleted in the meantime (i.e. when a dialog is opened
223 while in Dispatch()), accessing members will crash in this case. */
224 ReleaseFocus_Impl();
225
226 m_rCtrl.dispatchCommand( aArgs );
227}
228
229void SvxFontSizeBox_Base::statusChanged_Impl( tools::Long nPoint, bool bErase )
230{
231 if ( !bErase )
232 {
233 // convert the metric
234 tools::Long nVal = nPoint;
235
236 // changed => set new value
237 if (m_xWidget->get_value() != nVal)
238 m_xWidget->set_value(nVal);
239 }
240 else
241 {
242 // delete value in the display
243 m_xWidget->set_value(-1L);
244 m_xWidget->set_active_or_entry_text("");
245 }
246 m_aCurText = m_xWidget->get_active_text();
247}
248
249void SvxFontSizeBox_Base::UpdateFont()
250{
251 // filling up the sizes list
252 auto nOldVal = m_xWidget->get_value(); // memorize old value
254
255 m_xWidget->Fill(&aFontList);
256
257 m_xWidget->set_value(nOldVal); // restore old value
258 m_aCurText = m_xWidget->get_active_text(); // memorize to reset at ESC
259}
260
261IMPL_LINK(SvxFontSizeBox_Base, KeyInputHdl, const KeyEvent&, rKEvt, bool)
262{
263 return DoKeyInput(rKEvt);
264}
265
266bool SvxFontSizeBox_Base::DoKeyInput(const KeyEvent& rKEvt)
267{
268 bool bHandled = false;
269
270 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
271
272 switch (nCode)
273 {
274 case KEY_TAB:
275 m_bRelease = false;
276 Select();
277 break;
278
279 case KEY_ESCAPE:
280 m_xWidget->set_active_or_entry_text(m_aCurText);
281 if (!m_rCtrl.IsInSidebar())
282 {
283 ReleaseFocus_Impl();
284 bHandled = true;
285 }
286 break;
287 }
288
289 return bHandled;
290}
291
292bool SvxFontSizeBox_Impl::DoKeyInput(const KeyEvent& rKEvt)
293{
294 return SvxFontSizeBox_Base::DoKeyInput(rKEvt) || ChildKeyInput(rKEvt);
295}
296
297IMPL_LINK_NOARG(SvxFontSizeBox_Base, FocusOutHdl, weld::Widget&, void)
298{
299 if (!m_xWidget->has_focus()) // a combobox can be comprised of different subwidget so double-check if none of those has focus
300 m_xWidget->set_active_or_entry_text(m_aCurText);
301}
302
303void SvxFontSizeBox_Impl::SetOptimalSize()
304{
305 SetSizePixel(get_preferred_size());
306}
307
308SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(vcl::Window* pParent,
309 const uno::Reference<frame::XFrame>& rFrame,
310 FontHeightToolBoxControl& rCtrl)
311 : InterimItemWindow(pParent, "svx/ui/fontsizebox.ui", "FontSizeBox")
312 , SvxFontSizeBox_Base(m_xBuilder->weld_combo_box("fontsizecombobox"), rFrame, rCtrl)
313{
314}
315
316void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent& rDCEvt )
317{
318 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
319 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
320 {
321 SetOptimalSize();
322 }
323}
324
325IMPL_LINK(SvxFontSizeBox_Base, DumpAsPropertyTreeHdl, tools::JsonWriter&, rJsonWriter, void)
326{
327 {
328 auto entriesNode = rJsonWriter.startNode("entries");
329 for (int i = 0, nCount = m_xWidget->get_count(); i < nCount; ++i)
330 {
331 auto entryNode = rJsonWriter.startNode("");
332 rJsonWriter.put("", m_xWidget->get_text(i));
333 }
334 }
335
336 int nActive = m_xWidget->get_active();
337 rJsonWriter.put("selectedCount", static_cast<sal_Int32>(nActive == -1 ? 0 : 1));
338 {
339 auto selectedNode = rJsonWriter.startNode("selectedEntries");
340 if (nActive != -1)
341 {
342 auto node = rJsonWriter.startNode("");
343 rJsonWriter.put("", static_cast<sal_Int32>(nActive));
344 }
345 }
346
347 rJsonWriter.put("command", ".uno:FontHeight");
348}
349
350FontHeightToolBoxControl::FontHeightToolBoxControl( const uno::Reference< uno::XComponentContext >& rxContext )
351 : FontHeightToolBoxControl_Base( rxContext,
352 uno::Reference< frame::XFrame >(),
353 ".uno:FontHeight" ),
354 m_pBox( nullptr )
355{
356 addStatusListener( ".uno:CharFontName");
357}
358
359// XServiceInfo
360sal_Bool SAL_CALL FontHeightToolBoxControl::supportsService( const OUString& ServiceName )
361{
362 return cppu::supportsService(this, ServiceName);
363}
364
365OUString SAL_CALL FontHeightToolBoxControl::getImplementationName()
366{
367 return "com.sun.star.svx.FontHeightToolBoxController";
368}
369
370uno::Sequence< OUString > SAL_CALL FontHeightToolBoxControl::getSupportedServiceNames( )
371{
372 return { "com.sun.star.frame.ToolbarController" };
373}
374
375// XComponent
376void SAL_CALL FontHeightToolBoxControl::dispose()
377{
379
380 SolarMutexGuard aSolarMutexGuard;
381 m_xVclBox.disposeAndClear();
382 m_xWeldBox.reset();
383 m_pBox = nullptr;
384}
385
386// XStatusListener
387void SAL_CALL FontHeightToolBoxControl::statusChanged(
388 const frame::FeatureStateEvent& rEvent )
389{
390 if ( !m_pBox )
391 return;
392
393 SolarMutexGuard aSolarMutexGuard;
394 if (rEvent.FeatureURL.Path == "FontHeight")
395 {
396 if ( rEvent.IsEnabled )
397 {
398 m_pBox->set_sensitive(true);
399 frame::status::FontHeight aFontHeight;
400 if ( rEvent.State >>= aFontHeight )
401 {
402 // tdf#83090 - correctly round the height of the font
403 aFontHeight.Height = rtl::math::round(10. * aFontHeight.Height);
404 m_pBox->statusChanged_Impl(tools::Long(aFontHeight.Height), false);
405 }
406 else
407 m_pBox->statusChanged_Impl( tools::Long( -1 ), true );
408 }
409 else
410 {
411 m_pBox->set_sensitive(false);
412 m_pBox->statusChanged_Impl( tools::Long( -1 ), true );
413 }
414
415 if (m_pToolbar)
416 m_pToolbar->set_item_sensitive(m_aCommandURL, rEvent.IsEnabled);
417 else
418 {
419 ToolBox* pToolBox = nullptr;
421 if (getToolboxId(nId, &pToolBox))
422 pToolBox->EnableItem(nId, rEvent.IsEnabled);
423 }
424 }
425 else if ( rEvent.FeatureURL.Path == "CharFontName" )
426 {
427 m_pBox->UpdateFont();
428 }
429}
430
431// XToolbarController
432void SAL_CALL FontHeightToolBoxControl::execute( sal_Int16 /*KeyModifier*/ )
433{
434}
435
436void SAL_CALL FontHeightToolBoxControl::click()
437{
438}
439
440void SAL_CALL FontHeightToolBoxControl::doubleClick()
441{
442}
443
444uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createPopupWindow()
445{
446 return uno::Reference< awt::XWindow >();
447}
448
449uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createItemWindow(
450 const uno::Reference< awt::XWindow >& xParent )
451{
452 uno::Reference< awt::XWindow > xItemWindow;
453
454 if (m_pBuilder)
455 {
456 SolarMutexGuard aSolarMutexGuard;
457
458 std::unique_ptr<weld::ComboBox> xWidget(m_pBuilder->weld_combo_box("fontsizecombobox"));
459
460 xItemWindow = css::uno::Reference<css::awt::XWindow>(new weld::TransportAsXWindow(xWidget.get()));
461
462 m_xWeldBox.reset(new SvxFontSizeBox_Base(std::move(xWidget), m_xFrame, *this));
463 m_pBox = m_xWeldBox.get();
464 //Get the box to fill itself with all its sizes
465 m_pBox->UpdateFont();
466 }
467 else
468 {
469 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow( xParent );
470 if ( pParent )
471 {
472 SolarMutexGuard aSolarMutexGuard;
473 m_xVclBox = VclPtr<SvxFontSizeBox_Impl>::Create( pParent, m_xFrame, *this );
474 m_pBox = m_xVclBox.get();
475 //Get the box to fill itself with all its sizes
476 m_pBox->UpdateFont();
477 //Make it size itself to its optimal size re above sizes
478 m_xVclBox->SetOptimalSize();
479 xItemWindow = VCLUnoHelper::GetInterface(m_xVclBox);
480 }
481 }
482
483 return xItemWindow;
484}
485
486void FontHeightToolBoxControl::dispatchCommand(
487 const uno::Sequence< beans::PropertyValue >& rArgs )
488{
489 uno::Reference< frame::XDispatchProvider > xDispatchProvider( m_xFrame, uno::UNO_QUERY );
490 if ( xDispatchProvider.is() )
491 {
492 util::URL aURL;
493 uno::Reference< frame::XDispatch > xDispatch;
494 uno::Reference< util::XURLTransformer > xURLTransformer = getURLTransformer();
495
496 aURL.Complete = ".uno:FontHeight";
497 xURLTransformer->parseStrict( aURL );
498 xDispatch = xDispatchProvider->queryDispatch( aURL, OUString(), 0 );
499 if ( xDispatch.is() )
500 xDispatch->dispatch( aURL, rArgs );
501 }
502}
503
504}
505
506extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
508 css::uno::XComponentContext *rxContext,
509 css::uno::Sequence<css::uno::Any> const &)
510{
511 return cppu::acquire(new FontHeightToolBoxControl(rxContext));
512}
513
514/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Image > m_xWidget
IMPL_LINK(MaskData, PipetteHdl, const OUString &, rId, void)
Definition: _bmpmask.cxx:192
IMPL_LINK_NOARG(MaskData, ExecHdl, weld::Button &, void)
Definition: _bmpmask.cxx:300
css::uno::Reference< css::lang::XComponent > m_xFrame
static OutputDevice * GetDefaultDevice()
DataChangedEventType GetType() const
AllSettingsFlags GetFlags() const
virtual void dispose() override
virtual void GetFocus() override
const vcl::KeyCode & GetKeyCode() const
void EnableItem(ToolBoxItemId nItemId, bool bEnable=true)
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
static VclPtr< reference_type > Create(Arg &&... arg)
virtual void SAL_CALL dispose() override
void dispatchCommand(const OUString &sCommandURL, const css::uno::Sequence< css::beans::PropertyValue > &rArgs, const OUString &rTarget=OUString())
sal_uInt16 GetCode() const
void Disable(bool bChild=true)
void Enable(bool bEnable=true, bool bChild=true)
virtual void DataChanged(const DataChangedEvent &rDCEvt)
virtual std::unique_ptr< ComboBox > weld_combo_box(const OUString &id)=0
Reference< XDispatch > xDispatch
URL aURL
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
constexpr sal_uInt16 KEY_ESCAPE
constexpr sal_uInt16 KEY_TAB
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference
void dispose()
long Long
sal_Int16 nId
Reference< XFrame > xFrame
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_svx_FontHeightToolBoxController_get_implementation(css::uno::XComponentContext *rxContext, css::uno::Sequence< css::uno::Any > const &)
weld::Builder * m_pBuilder
unsigned char sal_Bool