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/.
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 #include <com/sun/star/awt/FontDescriptor.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>
26 #include <rtl/ref.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/window.hxx>
29 #include <vcl/settings.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <svtools/ctrltool.hxx>
32 #include <svtools/ctrlbox.hxx>
33 #include <svtools/toolboxcontroller.hxx>
34 #include <osl/mutex.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <cppuhelper/queryinterface.hxx>
37 #include <cppuhelper/supportsservice.hxx>
39 #include <boost/scoped_ptr.hpp>
41 using namespace ::com::sun::star
;
45 class SvxFontSizeBox_Impl
;
46 class FontHeightToolBoxControl
: public svt::ToolboxController
,
47 public lang::XServiceInfo
50 FontHeightToolBoxControl(
51 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>& rServiceManager
);
52 virtual ~FontHeightToolBoxControl();
55 virtual ::com::sun::star::uno::Any SAL_CALL
queryInterface( const ::com::sun::star::uno::Type
& aType
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
56 virtual void SAL_CALL
acquire() throw () SAL_OVERRIDE
;
57 virtual void SAL_CALL
release() throw () SAL_OVERRIDE
;
60 virtual OUString SAL_CALL
getImplementationName() throw( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
61 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
62 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
65 virtual void SAL_CALL
dispose() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
68 virtual void SAL_CALL
statusChanged( const ::com::sun::star::frame::FeatureStateEvent
& Event
) throw ( ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
71 virtual void SAL_CALL
execute( sal_Int16 KeyModifier
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
72 virtual void SAL_CALL
click() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
73 virtual void SAL_CALL
doubleClick() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
74 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
> SAL_CALL
createPopupWindow() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
75 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
> SAL_CALL
createItemWindow( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
>& Parent
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
77 void dispatchCommand( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& rArgs
);
78 using svt::ToolboxController::dispatchCommand
;
81 VclPtr
<SvxFontSizeBox_Impl
> m_pBox
;
82 ::com::sun::star::awt::FontDescriptor m_aCurrentFont
;
85 class SvxFontSizeBox_Impl
: public FontSizeBox
88 SvxFontSizeBox_Impl( vcl::Window
* pParent
,
89 const uno::Reference
< frame::XDispatchProvider
>& rDispatchProvider
,
90 const uno::Reference
< frame::XFrame
>& _xFrame
,
91 FontHeightToolBoxControl
& rCtrl
);
93 void statusChanged_Impl( long nHeight
, bool bErase
= false );
94 void UpdateFont( const ::com::sun::star::awt::FontDescriptor
& rCurrentFont
);
95 void SetOptimalSize();
97 virtual bool Notify( NotifyEvent
& rNEvt
) SAL_OVERRIDE
;
100 virtual void Select() SAL_OVERRIDE
;
101 virtual void DataChanged( const DataChangedEvent
& rDCEvt
) SAL_OVERRIDE
;
104 FontHeightToolBoxControl
* m_pCtrl
;
108 uno::Reference
< frame::XDispatchProvider
> m_xDispatchProvider
;
109 uno::Reference
< frame::XFrame
> m_xFrame
;
110 uno::Reference
< awt::XWindow
> m_xOldFocusWindow
;
112 void ReleaseFocus_Impl();
115 SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(
116 vcl::Window
* _pParent
,
117 const uno::Reference
< frame::XDispatchProvider
>& _rDispatchProvider
,
118 const uno::Reference
< frame::XFrame
>& _xFrame
,
119 FontHeightToolBoxControl
& _rCtrl
) :
121 FontSizeBox( _pParent
, WinBits( WB_DROPDOWN
) ),
124 m_aLogicalSize ( 0,100 ),
126 m_xDispatchProvider ( _rDispatchProvider
),
133 void SvxFontSizeBox_Impl::ReleaseFocus_Impl()
141 if ( m_xFrame
.is() && m_xFrame
->getContainerWindow().is() )
142 m_xFrame
->getContainerWindow()->setFocus();
147 void SvxFontSizeBox_Impl::Select()
149 FontSizeBox::Select();
151 if ( !IsTravelSelect() )
153 sal_Int64 nSelVal
= GetValue();
154 float fSelVal
= float( nSelVal
) / 10;
156 uno::Sequence
< beans::PropertyValue
> aArgs( 1 );
157 aArgs
[0].Name
= "FontHeight.Height";
158 aArgs
[0].Value
= uno::makeAny( fSelVal
);
160 /* #i33380# DR 2004-09-03 Moved the following line above the Dispatch() call.
161 This instance may be deleted in the meantime (i.e. when a dialog is opened
162 while in Dispatch()), accessing members will crash in this case. */
165 m_pCtrl
->dispatchCommand( aArgs
);
171 void SvxFontSizeBox_Impl::statusChanged_Impl( long nPoint
, bool bErase
)
178 // ge"andert => neuen Wert setzen
179 if ( GetValue() != nVal
)
184 // Wert in der Anzeige l"oschen
193 void SvxFontSizeBox_Impl::UpdateFont( const ::com::sun::star::awt::FontDescriptor
& rCurrentFont
)
195 // Sizes-Liste auff"ullen
196 sal_Int64 nOldVal
= GetValue(); // alten Wert merken
197 const FontList
* _pFontList
= NULL
;
198 boost::scoped_ptr
<FontList
> aHold( new FontList( this ));
199 _pFontList
= aHold
.get();
201 if ( !rCurrentFont
.Name
.isEmpty() )
203 vcl::FontInfo _aFontInfo
;
204 _aFontInfo
.SetName( rCurrentFont
.Name
);
205 _aFontInfo
.SetStyleName( rCurrentFont
.StyleName
);
206 _aFontInfo
.SetHeight( rCurrentFont
.Height
);
207 Fill( &_aFontInfo
, _pFontList
);
211 Fill( NULL
, _pFontList
);
213 SetValue( nOldVal
); // alten Wert wiederherstellen
214 m_aCurText
= GetText(); // zum R"ucksetzen bei ESC merken
219 bool SvxFontSizeBox_Impl::Notify( NotifyEvent
& rNEvt
)
221 bool nHandled
= false;
223 if ( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
225 sal_uInt16 nCode
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
232 if ( KEY_TAB
== nCode
)
241 SetText( m_aCurText
);
247 else if( MouseNotifyEvent::LOSEFOCUS
== rNEvt
.GetType() )
249 vcl::Window
* pFocusWin
= Application::GetFocusWindow();
250 if(!HasFocus() && GetSubEdit() != pFocusWin
)
251 SetText(GetSavedValue());
254 return nHandled
|| FontSizeBox::Notify( rNEvt
);
257 void SvxFontSizeBox_Impl::SetOptimalSize()
259 Size
aPrefSize(LogicToPixel(m_aLogicalSize
, MAP_APPFONT
));
260 aPrefSize
.Width() = get_preferred_size().Width();
261 SetSizePixel(aPrefSize
);
264 void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent
& rDCEvt
)
266 if ( (rDCEvt
.GetType() == DataChangedEventType::SETTINGS
) &&
267 (rDCEvt
.GetFlags() & AllSettingsFlags::STYLE
) )
272 FontSizeBox::DataChanged( rDCEvt
);
275 FontHeightToolBoxControl::FontHeightToolBoxControl( const uno::Reference
< uno::XComponentContext
>& rxContext
)
276 : svt::ToolboxController( rxContext
,
277 uno::Reference
< frame::XFrame
>(),
278 OUString( ".uno:FontHeight" ) ),
281 addStatusListener( OUString( ".uno:CharFontName" ));
284 FontHeightToolBoxControl::~FontHeightToolBoxControl()
289 ::com::sun::star::uno::Any SAL_CALL
FontHeightToolBoxControl::queryInterface( const ::com::sun::star::uno::Type
& aType
)
290 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
292 uno::Any a
= ToolboxController::queryInterface( aType
);
296 return ::cppu::queryInterface( aType
, static_cast< lang::XServiceInfo
* >( this ));
299 void SAL_CALL
FontHeightToolBoxControl::acquire() throw ()
301 ToolboxController::acquire();
304 void SAL_CALL
FontHeightToolBoxControl::release() throw ()
306 ToolboxController::release();
310 sal_Bool SAL_CALL
FontHeightToolBoxControl::supportsService( const OUString
& ServiceName
)
311 throw(uno::RuntimeException
, std::exception
)
313 return cppu::supportsService(this, ServiceName
);
316 OUString SAL_CALL
FontHeightToolBoxControl::getImplementationName()
317 throw( uno::RuntimeException
, std::exception
)
319 return OUString("com.sun.star.svx.FontHeightToolBoxController");
322 uno::Sequence
< OUString
> SAL_CALL
FontHeightToolBoxControl::getSupportedServiceNames( )
323 throw( uno::RuntimeException
, std::exception
)
325 uno::Sequence
< OUString
> aSNS( 1 );
326 aSNS
.getArray()[0] = "com.sun.star.frame.ToolbarController";
331 void SAL_CALL
FontHeightToolBoxControl::dispose()
332 throw (uno::RuntimeException
, std::exception
)
334 svt::ToolboxController::dispose();
336 SolarMutexGuard aSolarMutexGuard
;
337 m_pBox
.disposeAndClear();
341 void SAL_CALL
FontHeightToolBoxControl::statusChanged(
342 const frame::FeatureStateEvent
& rEvent
)
343 throw ( uno::RuntimeException
, std::exception
)
347 SolarMutexGuard aSolarMutexGuard
;
348 if ( rEvent
.FeatureURL
.Path
== "FontHeight" )
350 if ( rEvent
.IsEnabled
)
353 frame::status::FontHeight aFontHeight
;
354 if ( rEvent
.State
>>= aFontHeight
)
355 m_pBox
->statusChanged_Impl( long( 10. * aFontHeight
.Height
), false );
357 m_pBox
->statusChanged_Impl( long( -1 ), true );
362 else if ( rEvent
.FeatureURL
.Path
== "CharFontName" )
364 if ( rEvent
.State
>>= m_aCurrentFont
)
365 m_pBox
->UpdateFont( m_aCurrentFont
);
370 // XToolbarController
371 void SAL_CALL
FontHeightToolBoxControl::execute( sal_Int16
/*KeyModifier*/ )
372 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
376 void SAL_CALL
FontHeightToolBoxControl::click()
377 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
381 void SAL_CALL
FontHeightToolBoxControl::doubleClick()
382 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
386 uno::Reference
< awt::XWindow
> SAL_CALL
FontHeightToolBoxControl::createPopupWindow()
387 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
389 return uno::Reference
< awt::XWindow
>();
392 uno::Reference
< awt::XWindow
> SAL_CALL
FontHeightToolBoxControl::createItemWindow(
393 const uno::Reference
< awt::XWindow
>& Parent
)
394 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
396 uno::Reference
< awt::XWindow
> xItemWindow
;
397 uno::Reference
< awt::XWindow
> xParent( Parent
);
399 vcl::Window
* pParent
= VCLUnoHelper::GetWindow( xParent
);
402 SolarMutexGuard aSolarMutexGuard
;
403 m_pBox
= VclPtr
<SvxFontSizeBox_Impl
>::Create(
406 uno::Reference
< frame::XDispatchProvider
>( m_xFrame
, uno::UNO_QUERY
),
409 //Get the box to fill itself with all its sizes
410 m_pBox
->UpdateFont(m_aCurrentFont
);
411 //Make it size itself to its optimal size re above sizes
412 m_pBox
->SetOptimalSize();
413 xItemWindow
= VCLUnoHelper::GetInterface( m_pBox
);
419 void FontHeightToolBoxControl::dispatchCommand(
420 const uno::Sequence
< beans::PropertyValue
>& rArgs
)
422 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider( m_xFrame
, uno::UNO_QUERY
);
423 if ( xDispatchProvider
.is() )
426 uno::Reference
< frame::XDispatch
> xDispatch
;
427 uno::Reference
< util::XURLTransformer
> xURLTransformer
= getURLTransformer();
429 aURL
.Complete
= ".uno:FontHeight";
430 xURLTransformer
->parseStrict( aURL
);
431 xDispatch
= xDispatchProvider
->queryDispatch( aURL
, OUString(), 0 );
432 if ( xDispatch
.is() )
433 xDispatch
->dispatch( aURL
, rArgs
);
439 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
440 com_sun_star_svx_FontHeightToolBoxController_get_implementation(
441 css::uno::XComponentContext
*rxContext
,
442 css::uno::Sequence
<css::uno::Any
> const &)
444 return cppu::acquire(new FontHeightToolBoxControl(rxContext
));
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */