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 
27 #include <comphelper/propertyvalue.hxx>
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>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <sfx2/viewsh.hxx>
37 #include <svtools/ctrltool.hxx>
38 #include <svtools/ctrlbox.hxx>
39 #include <svtools/toolboxcontroller.hxx>
40 #include <tools/json_writer.hxx>
41 #include <vcl/toolbox.hxx>
42 #include <cppuhelper/supportsservice.hxx>
43 
44 #include <memory>
45 
46 #include <vcl/InterimItemWindow.hxx>
47 
48 using namespace ::com::sun::star;
49 
50 namespace {
51 
52 class SvxFontSizeBox_Base;
53 class SvxFontSizeBox_Impl;
54 
55 typedef cppu::ImplInheritanceHelper< ::svt::ToolboxController, lang::XServiceInfo> FontHeightToolBoxControl_Base;
56 class 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 );
81         using svt::ToolboxController::dispatchCommand;
82 
83     private:
84         VclPtr<SvxFontSizeBox_Impl> m_xVclBox;
85         std::unique_ptr<SvxFontSizeBox_Base> m_xWeldBox;
86         SvxFontSizeBox_Base* m_pBox;
87 };
88 
89 class SvxFontSizeBox_Base
90 {
91 public:
92     SvxFontSizeBox_Base(std::unique_ptr<weld::ComboBox> xWidget,
93                         uno::Reference< frame::XFrame > _xFrame,
94                         FontHeightToolBoxControl& rCtrl);
95 
~SvxFontSizeBox_Base()96     virtual ~SvxFontSizeBox_Base()
97     {
98     }
99 
set_sensitive(bool bSensitive)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 
108 protected:
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 
127 class SvxFontSizeBox_Impl final : public InterimItemWindow
128                                 , public SvxFontSizeBox_Base
129 {
130 public:
131     SvxFontSizeBox_Impl(vcl::Window* pParent,
132                         const uno::Reference< frame::XFrame >& _xFrame,
133                         FontHeightToolBoxControl& rCtrl);
134 
dispose()135     virtual void dispose() override
136     {
137         m_xWidget.reset();
138         InterimItemWindow::dispose();
139     }
140 
GetFocus()141     virtual void GetFocus() override
142     {
143         if (m_xWidget)
144             m_xWidget->grab_focus();
145         InterimItemWindow::GetFocus();
146     }
147 
~SvxFontSizeBox_Impl()148     virtual ~SvxFontSizeBox_Impl() override
149     {
150         disposeOnce();
151     }
152 
153     void SetOptimalSize();
154 
155     virtual void DataChanged(const DataChangedEvent& rDCEvt) override;
156 
set_sensitive(bool bSensitive)157     virtual void set_sensitive(bool bSensitive) override
158     {
159         m_xWidget->set_sensitive(bSensitive);
160         if (bSensitive)
161             InterimItemWindow::Enable();
162         else
163             InterimItemWindow::Disable();
164     }
165 
166 private:
167     virtual bool DoKeyInput(const KeyEvent& rKEvt) override;
168 };
169 
SvxFontSizeBox_Base(std::unique_ptr<weld::ComboBox> xWidget,uno::Reference<frame::XFrame> xFrame,FontHeightToolBoxControl & rCtrl)170 SvxFontSizeBox_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(u""_ustr);
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 
ReleaseFocus_Impl()189 void 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 
IMPL_LINK(SvxFontSizeBox_Base,SelectHdl,weld::ComboBox &,rCombo,void)201 IMPL_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 
IMPL_LINK_NOARG(SvxFontSizeBox_Base,ActivateHdl,weld::ComboBox &,bool)207 IMPL_LINK_NOARG(SvxFontSizeBox_Base, ActivateHdl, weld::ComboBox&, bool)
208 {
209     Select();
210     return true;
211 }
212 
Select()213 void 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(u"FontHeight.Height"_ustr,
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 
statusChanged_Impl(tools::Long nPoint,bool bErase)229 void 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(u""_ustr);
245     }
246     m_aCurText = m_xWidget->get_active_text();
247 }
248 
UpdateFont()249 void SvxFontSizeBox_Base::UpdateFont()
250 {
251     // filling up the sizes list
252     auto nOldVal = m_xWidget->get_value(); // memorize old value
253     FontList aFontList(Application::GetDefaultDevice());
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 
IMPL_LINK(SvxFontSizeBox_Base,KeyInputHdl,const KeyEvent &,rKEvt,bool)261 IMPL_LINK(SvxFontSizeBox_Base, KeyInputHdl, const KeyEvent&, rKEvt, bool)
262 {
263     return DoKeyInput(rKEvt);
264 }
265 
DoKeyInput(const KeyEvent & rKEvt)266 bool 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 
DoKeyInput(const KeyEvent & rKEvt)292 bool SvxFontSizeBox_Impl::DoKeyInput(const KeyEvent& rKEvt)
293 {
294     return SvxFontSizeBox_Base::DoKeyInput(rKEvt) || ChildKeyInput(rKEvt);
295 }
296 
IMPL_LINK_NOARG(SvxFontSizeBox_Base,FocusOutHdl,weld::Widget &,void)297 IMPL_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 
SetOptimalSize()303 void SvxFontSizeBox_Impl::SetOptimalSize()
304 {
305     SetSizePixel(get_preferred_size());
306 }
307 
SvxFontSizeBox_Impl(vcl::Window * pParent,const uno::Reference<frame::XFrame> & rFrame,FontHeightToolBoxControl & rCtrl)308 SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(vcl::Window* pParent,
309                                          const uno::Reference<frame::XFrame>& rFrame,
310                                          FontHeightToolBoxControl& rCtrl)
311     : InterimItemWindow(pParent, u"svx/ui/fontsizebox.ui"_ustr, u"FontSizeBox"_ustr, true, reinterpret_cast<sal_uInt64>(SfxViewShell::Current()))
312     , SvxFontSizeBox_Base(m_xBuilder->weld_combo_box(u"fontsizecombobox"_ustr), rFrame, rCtrl)
313 {
314 }
315 
DataChanged(const DataChangedEvent & rDCEvt)316 void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent& rDCEvt )
317 {
318     if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
319          (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
320     {
321         SetOptimalSize();
322     }
323 }
324 
IMPL_LINK(SvxFontSizeBox_Base,DumpAsPropertyTreeHdl,tools::JsonWriter &,rJsonWriter,void)325 IMPL_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 
FontHeightToolBoxControl(const uno::Reference<uno::XComponentContext> & rxContext)350 FontHeightToolBoxControl::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( u".uno:CharFontName"_ustr);
357 }
358 
359 // XServiceInfo
supportsService(const OUString & ServiceName)360 sal_Bool SAL_CALL FontHeightToolBoxControl::supportsService( const OUString& ServiceName )
361 {
362     return cppu::supportsService(this, ServiceName);
363 }
364 
getImplementationName()365 OUString SAL_CALL FontHeightToolBoxControl::getImplementationName()
366 {
367     return u"com.sun.star.svx.FontHeightToolBoxController"_ustr;
368 }
369 
getSupportedServiceNames()370 uno::Sequence< OUString > SAL_CALL FontHeightToolBoxControl::getSupportedServiceNames(  )
371 {
372     return { u"com.sun.star.frame.ToolbarController"_ustr };
373 }
374 
375 // XComponent
dispose()376 void SAL_CALL FontHeightToolBoxControl::dispose()
377 {
378     svt::ToolboxController::dispose();
379 
380     SolarMutexGuard aSolarMutexGuard;
381     m_xVclBox.disposeAndClear();
382     m_xWeldBox.reset();
383     m_pBox = nullptr;
384 }
385 
386 // XStatusListener
statusChanged(const frame::FeatureStateEvent & rEvent)387 void 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;
420             ToolBoxItemId nId;
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
execute(sal_Int16)432 void SAL_CALL FontHeightToolBoxControl::execute( sal_Int16 /*KeyModifier*/ )
433 {
434 }
435 
click()436 void SAL_CALL FontHeightToolBoxControl::click()
437 {
438 }
439 
doubleClick()440 void SAL_CALL FontHeightToolBoxControl::doubleClick()
441 {
442 }
443 
createPopupWindow()444 uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createPopupWindow()
445 {
446     return uno::Reference< awt::XWindow >();
447 }
448 
createItemWindow(const uno::Reference<awt::XWindow> & xParent)449 uno::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(u"fontsizecombobox"_ustr));
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 
dispatchCommand(const uno::Sequence<beans::PropertyValue> & rArgs)486 void 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 
506 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_svx_FontHeightToolBoxController_get_implementation(css::uno::XComponentContext * rxContext,css::uno::Sequence<css::uno::Any> const &)507 com_sun_star_svx_FontHeightToolBoxController_get_implementation(
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: */
515