bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / tbxctrls / tbunocontroller.cxx
blobdc7e1dd1c361541da1b6d0b3a12ed63fa3b22975
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/.
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 .
21 #include "tbunocontroller.hxx"
23 #include <com/sun/star/frame/status/FontHeight.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <vcl/svapp.hxx>
28 #include <vcl/window.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <svtools/ctrltool.hxx>
31 #include <svtools/ctrlbox.hxx>
32 #include <osl/mutex.hxx>
33 #include <comphelper/processfactory.hxx>
35 #include <memory>
37 #define LOGICAL_EDIT_HEIGHT 12
39 using namespace ::com::sun::star;
41 namespace svx
44 class FontHeightToolBoxControl;
45 class SvxFontSizeBox_Impl : public FontSizeBox
47 public:
48 SvxFontSizeBox_Impl( Window* pParent,
49 const uno::Reference< frame::XDispatchProvider >& rDispatchProvider,
50 const uno::Reference< frame::XFrame >& _xFrame,
51 FontHeightToolBoxControl& rCtrl );
53 void statusChanged_Impl( long nHeight, bool bErase = false );
54 void UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont );
56 virtual long Notify( NotifyEvent& rNEvt );
58 protected:
59 virtual void Select();
60 virtual void DataChanged( const DataChangedEvent& rDCEvt );
62 private:
63 FontHeightToolBoxControl* m_pCtrl;
64 String m_aCurText;
65 Size m_aLogicalSize;
66 bool m_bRelease;
67 uno::Reference< frame::XDispatchProvider > m_xDispatchProvider;
68 uno::Reference< frame::XFrame > m_xFrame;
69 uno::Reference< awt::XWindow > m_xOldFocusWindow;
71 void ReleaseFocus_Impl();
74 //========================================================================
75 // class SvxFontSizeBox_Impl --------------------------------------------------
76 //========================================================================
78 SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(
79 Window* _pParent,
80 const uno::Reference< frame::XDispatchProvider >& _rDispatchProvider,
81 const uno::Reference< frame::XFrame >& _xFrame,
82 FontHeightToolBoxControl& _rCtrl ) :
84 FontSizeBox( _pParent, WinBits( WB_DROPDOWN ) ),
86 m_pCtrl ( &_rCtrl ),
87 m_aLogicalSize ( 30,100 ),
88 m_bRelease ( true ),
89 m_xDispatchProvider ( _rDispatchProvider ),
90 m_xFrame ( _xFrame )
92 SetSizePixel( LogicToPixel( m_aLogicalSize, MAP_APPFONT ));
93 SetValue( 0 );
94 SetText( String() );
97 // -----------------------------------------------------------------------
99 void SvxFontSizeBox_Impl::ReleaseFocus_Impl()
101 if ( !m_bRelease )
103 m_bRelease = true;
104 return;
107 if ( m_xFrame.is() && m_xFrame->getContainerWindow().is() )
108 m_xFrame->getContainerWindow()->setFocus();
111 // -----------------------------------------------------------------------
113 void SvxFontSizeBox_Impl::Select()
115 FontSizeBox::Select();
117 if ( !IsTravelSelect() )
119 sal_Int64 nSelVal = GetValue();
120 float fSelVal = float( nSelVal ) / 10;
122 uno::Sequence< beans::PropertyValue > aArgs( 1 );
123 aArgs[0].Name = OUString( "FontHeight.Height" );
124 aArgs[0].Value = uno::makeAny( fSelVal );
126 /* #i33380# DR 2004-09-03 Moved the following line above the Dispatch() call.
127 This instance may be deleted in the meantime (i.e. when a dialog is opened
128 while in Dispatch()), accessing members will crash in this case. */
129 ReleaseFocus_Impl();
131 m_pCtrl->dispatchCommand( aArgs );
135 // -----------------------------------------------------------------------
137 void SvxFontSizeBox_Impl::statusChanged_Impl( long nPoint, bool bErase )
139 if ( !bErase )
141 // Metric Umrechnen
142 long nVal = nPoint;
144 // ge"andert => neuen Wert setzen
145 if ( GetValue() != nVal )
146 SetValue( nVal );
148 else
150 // Wert in der Anzeige l"oschen
151 SetValue( -1L );
152 SetText( String() );
154 SaveValue();
157 // -----------------------------------------------------------------------
159 void SvxFontSizeBox_Impl::UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont )
161 // Sizes-Liste auff"ullen
162 sal_Int64 nOldVal = GetValue(); // alten Wert merken
163 const FontList* _pFontList = NULL;
164 ::std::auto_ptr<FontList> aHold( new FontList( this ));
165 _pFontList = aHold.get();
167 if ( !rCurrentFont.Name.isEmpty() )
169 FontInfo _aFontInfo;
170 _aFontInfo.SetName( rCurrentFont.Name );
171 _aFontInfo.SetStyleName( rCurrentFont.StyleName );
172 _aFontInfo.SetHeight( rCurrentFont.Height );
173 Fill( &_aFontInfo, _pFontList );
175 else
177 Fill( NULL, _pFontList );
179 SetValue( nOldVal ); // alten Wert wiederherstellen
180 m_aCurText = GetText(); // zum R"ucksetzen bei ESC merken
183 // -----------------------------------------------------------------------
185 long SvxFontSizeBox_Impl::Notify( NotifyEvent& rNEvt )
187 long nHandled = 0;
189 if ( rNEvt.GetType() == EVENT_KEYINPUT )
191 sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
193 switch ( nCode )
195 case KEY_RETURN:
196 case KEY_TAB:
198 if ( KEY_TAB == nCode )
199 m_bRelease = false;
200 else
201 nHandled = 1;
202 Select();
203 break;
206 case KEY_ESCAPE:
207 SetText( m_aCurText );
208 ReleaseFocus_Impl();
209 nHandled = 1;
210 break;
213 else if( EVENT_LOSEFOCUS == rNEvt.GetType() )
215 Window* pFocusWin = Application::GetFocusWindow();
216 if(!HasFocus() && GetSubEdit() != pFocusWin)
217 SetText(GetSavedValue());
220 return nHandled ? nHandled : FontSizeBox::Notify( rNEvt );
223 // ---------------------------------------------------------------------------
225 void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent& rDCEvt )
227 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
228 (rDCEvt.GetFlags() & SETTINGS_STYLE) )
230 SetSizePixel( LogicToPixel( m_aLogicalSize, MAP_APPFONT ));
231 Size aDropSize( m_aLogicalSize.Width(), LOGICAL_EDIT_HEIGHT );
232 SetDropDownSizePixel( LogicToPixel( aDropSize, MAP_APPFONT ));
235 FontSizeBox::DataChanged( rDCEvt );
238 //========================================================================
239 // class FontHeightToolBoxControl
240 //========================================================================
242 FontHeightToolBoxControl::FontHeightToolBoxControl( const uno::Reference< uno::XComponentContext >& rxContext )
243 : svt::ToolboxController( rxContext,
244 uno::Reference< frame::XFrame >(),
245 OUString( ".uno:FontHeight" ) ),
246 m_pBox( NULL )
248 addStatusListener( OUString( ".uno:CharFontName" ));
251 FontHeightToolBoxControl::~FontHeightToolBoxControl()
255 // XInterface
256 ::com::sun::star::uno::Any SAL_CALL FontHeightToolBoxControl::queryInterface( const ::com::sun::star::uno::Type& aType )
257 throw (::com::sun::star::uno::RuntimeException)
259 uno::Any a = ToolboxController::queryInterface( aType );
260 if ( a.hasValue() )
261 return a;
263 return ::cppu::queryInterface( aType, static_cast< lang::XServiceInfo* >( this ));
266 void SAL_CALL FontHeightToolBoxControl::acquire() throw ()
268 ToolboxController::acquire();
271 void SAL_CALL FontHeightToolBoxControl::release() throw ()
273 ToolboxController::release();
276 // XServiceInfo
277 sal_Bool SAL_CALL FontHeightToolBoxControl::supportsService( const OUString& ServiceName )
278 throw(uno::RuntimeException)
280 const uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
281 const OUString * pArray = aSNL.getConstArray();
283 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
284 if( pArray[i] == ServiceName )
285 return true;
287 return false;
290 OUString SAL_CALL FontHeightToolBoxControl::getImplementationName()
291 throw( uno::RuntimeException )
293 return getImplementationName_Static();
296 uno::Sequence< OUString > SAL_CALL FontHeightToolBoxControl::getSupportedServiceNames( )
297 throw( uno::RuntimeException )
299 return getSupportedServiceNames_Static();
302 uno::Sequence< OUString > FontHeightToolBoxControl::getSupportedServiceNames_Static()
303 throw()
305 uno::Sequence< OUString > aSNS( 1 );
306 aSNS.getArray()[0] = OUString( "com.sun.star.frame.ToolbarController" );
307 return aSNS;
310 // XComponent
311 void SAL_CALL FontHeightToolBoxControl::dispose()
312 throw (uno::RuntimeException)
314 svt::ToolboxController::dispose();
316 SolarMutexGuard aSolarMutexGuard;
317 delete m_pBox;
318 m_pBox = 0;
321 // XStatusListener
322 void SAL_CALL FontHeightToolBoxControl::statusChanged(
323 const frame::FeatureStateEvent& rEvent )
324 throw ( uno::RuntimeException )
326 if ( m_pBox )
328 SolarMutexGuard aSolarMutexGuard;
329 if ( rEvent.FeatureURL.Path == "FontHeight" )
331 if ( rEvent.IsEnabled )
333 m_pBox->Enable();
334 frame::status::FontHeight aFontHeight;
335 if ( rEvent.State >>= aFontHeight )
336 m_pBox->statusChanged_Impl( long( 10. * aFontHeight.Height ), false );
337 else
338 m_pBox->statusChanged_Impl( long( -1 ), true );
340 else
341 m_pBox->Disable();
343 else if ( rEvent.FeatureURL.Path == "CharFontName" )
345 if ( rEvent.State >>= m_aCurrentFont )
346 m_pBox->UpdateFont( m_aCurrentFont );
351 // XToolbarController
352 void SAL_CALL FontHeightToolBoxControl::execute( sal_Int16 /*KeyModifier*/ )
353 throw (::com::sun::star::uno::RuntimeException)
357 void SAL_CALL FontHeightToolBoxControl::click()
358 throw (::com::sun::star::uno::RuntimeException)
362 void SAL_CALL FontHeightToolBoxControl::doubleClick()
363 throw (::com::sun::star::uno::RuntimeException)
367 uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createPopupWindow()
368 throw (::com::sun::star::uno::RuntimeException)
370 return uno::Reference< awt::XWindow >();
373 uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createItemWindow(
374 const uno::Reference< awt::XWindow >& Parent )
375 throw (::com::sun::star::uno::RuntimeException)
377 uno::Reference< awt::XWindow > xItemWindow;
378 uno::Reference< awt::XWindow > xParent( Parent );
380 Window* pParent = VCLUnoHelper::GetWindow( xParent );
381 if ( pParent )
383 SolarMutexGuard aSolarMutexGuard;
384 m_pBox = new SvxFontSizeBox_Impl(
385 pParent,
386 uno::Reference< frame::XDispatchProvider >( m_xFrame, uno::UNO_QUERY ),
387 m_xFrame,
388 *this );
389 xItemWindow = VCLUnoHelper::GetInterface( m_pBox );
392 return xItemWindow;
395 void FontHeightToolBoxControl::dispatchCommand(
396 const uno::Sequence< beans::PropertyValue >& rArgs )
398 uno::Reference< frame::XDispatchProvider > xDispatchProvider( m_xFrame, uno::UNO_QUERY );
399 if ( xDispatchProvider.is() )
401 util::URL aURL;
402 uno::Reference< frame::XDispatch > xDispatch;
403 uno::Reference< util::XURLTransformer > xURLTransformer = getURLTransformer();
405 aURL.Complete = OUString( ".uno:FontHeight" );
406 xURLTransformer->parseStrict( aURL );
407 xDispatch = xDispatchProvider->queryDispatch( aURL, OUString(), 0 );
408 if ( xDispatch.is() )
409 xDispatch->dispatch( aURL, rArgs );
413 uno::Reference< uno::XInterface > SAL_CALL FontHeightToolBoxControl_createInstance(
414 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
416 return *new FontHeightToolBoxControl( comphelper::getComponentContext(rSMgr) );
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */