1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "tbunocontroller.hxx"
28 
29 #include <com/sun/star/frame/status/FontHeight.hpp>
30 #include <com/sun/star/frame/XDispatchProvider.hpp>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 
33 #include <vcl/svapp.hxx>
34 #include <vcl/window.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <svtools/ctrltool.hxx>
37 #include <svtools/ctrlbox.hxx>
38 #include <vos/mutex.hxx>
39 
40 #include <memory>
41 
42 #define LOGICAL_EDIT_HEIGHT         12
43 
44 using namespace ::com::sun::star;
45 
46 namespace svx
47 {
48 
49 class FontHeightToolBoxControl;
50 class SvxFontSizeBox_Impl : public FontSizeBox
51 {
52 public:
53                         SvxFontSizeBox_Impl( Window* pParent,
54                                              const uno::Reference< frame::XDispatchProvider >& rDispatchProvider,
55                                              const uno::Reference< frame::XFrame >& _xFrame,
56 										     FontHeightToolBoxControl& rCtrl );
57 
58     void                statusChanged_Impl( long nHeight, bool bErase = false );
59     void                UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont );
60 
61     virtual long		Notify( NotifyEvent& rNEvt );
62 
63 protected:
64 	virtual void        Select();
65     virtual void        DataChanged( const DataChangedEvent& rDCEvt );
66 
67 private:
68 	FontHeightToolBoxControl*	               m_pCtrl;
69 	String							           m_aCurText;
70     Size                                       m_aLogicalSize;
71 	sal_Bool							           m_bRelease;
72     uno::Reference< frame::XDispatchProvider > m_xDispatchProvider;
73     uno::Reference< frame::XFrame >            m_xFrame;
74     uno::Reference< awt::XWindow >             m_xOldFocusWindow;
75 
76 	void				ReleaseFocus_Impl();
77 };
78 
79 //========================================================================
80 // class SvxFontSizeBox_Impl --------------------------------------------------
81 //========================================================================
82 
SvxFontSizeBox_Impl(Window * _pParent,const uno::Reference<frame::XDispatchProvider> & _rDispatchProvider,const uno::Reference<frame::XFrame> & _xFrame,FontHeightToolBoxControl & _rCtrl)83 SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(
84     Window*                                           _pParent,
85     const uno::Reference< frame::XDispatchProvider >& _rDispatchProvider,
86     const uno::Reference< frame::XFrame >&            _xFrame,
87     FontHeightToolBoxControl&                         _rCtrl ) :
88 
89 	FontSizeBox( _pParent, WinBits( WB_DROPDOWN ) ),
90 
91 	m_pCtrl		        ( &_rCtrl ),
92     m_aLogicalSize      ( 30,100 ),
93 	m_bRelease	        ( sal_True ),
94     m_xDispatchProvider ( _rDispatchProvider ),
95     m_xFrame            ( _xFrame )
96 {
97     SetSizePixel( LogicToPixel( m_aLogicalSize, MAP_APPFONT ));
98 	SetValue( 0 );
99 	SetText( String() );
100 }
101 
102 // -----------------------------------------------------------------------
103 
ReleaseFocus_Impl()104 void SvxFontSizeBox_Impl::ReleaseFocus_Impl()
105 {
106 	if ( !m_bRelease )
107 	{
108 		m_bRelease = sal_True;
109 		return;
110     }
111 
112     if ( m_xFrame.is() && m_xFrame->getContainerWindow().is() )
113         m_xFrame->getContainerWindow()->setFocus();
114 }
115 
116 // -----------------------------------------------------------------------
117 
Select()118 void SvxFontSizeBox_Impl::Select()
119 {
120 	FontSizeBox::Select();
121 
122 	if ( !IsTravelSelect() )
123 	{
124 		sal_Int64 nSelVal = GetValue();
125 		float fSelVal     = float( nSelVal ) / 10;
126 
127         uno::Sequence< beans::PropertyValue > aArgs( 1 );
128         aArgs[0].Name  = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontHeight.Height" ));
129         aArgs[0].Value = uno::makeAny( fSelVal );
130 
131         /*  #i33380# DR 2004-09-03 Moved the following line above the Dispatch() call.
132             This instance may be deleted in the meantime (i.e. when a dialog is opened
133             while in Dispatch()), accessing members will crash in this case. */
134         ReleaseFocus_Impl();
135 
136         m_pCtrl->dispatchCommand( aArgs );
137 	}
138 }
139 
140 // -----------------------------------------------------------------------
141 
statusChanged_Impl(long nPoint,bool bErase)142 void SvxFontSizeBox_Impl::statusChanged_Impl( long nPoint, bool bErase )
143 {
144 	if ( !bErase )
145 	{
146 		// Metric Umrechnen
147         long nVal = nPoint;
148 
149 		// ge"andert => neuen Wert setzen
150 		if ( GetValue() != nVal )
151 			SetValue( nVal );
152 	}
153 	else
154 	{
155 		// Wert in der Anzeige l"oschen
156 		SetValue( -1L );
157 		SetText( String() );
158 	}
159     SaveValue();
160 }
161 
162 // -----------------------------------------------------------------------
163 
UpdateFont(const::com::sun::star::awt::FontDescriptor & rCurrentFont)164 void SvxFontSizeBox_Impl::UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont )
165 {
166 	// Sizes-Liste auff"ullen
167 	sal_Int64 nOldVal = GetValue(); // alten Wert merken
168     const FontList* _pFontList = NULL;
169     ::std::auto_ptr<FontList> aHold( new FontList( this ));
170     _pFontList = aHold.get();
171 
172     if ( rCurrentFont.Name.getLength() > 0 )
173 	{
174         FontInfo _aFontInfo;
175         _aFontInfo.SetName( rCurrentFont.Name );
176         _aFontInfo.SetStyleName( rCurrentFont.StyleName );
177         _aFontInfo.SetHeight( rCurrentFont.Height );
178         Fill( &_aFontInfo, _pFontList );
179 	}
180 	else
181 	{
182 		Fill( NULL, _pFontList );
183 	}
184 	SetValue( nOldVal ); // alten Wert wiederherstellen
185 	m_aCurText = GetText(); // zum R"ucksetzen bei ESC merken
186 }
187 
188 // -----------------------------------------------------------------------
189 
Notify(NotifyEvent & rNEvt)190 long SvxFontSizeBox_Impl::Notify( NotifyEvent& rNEvt )
191 {
192 	long nHandled = 0;
193 
194 	if ( rNEvt.GetType() == EVENT_KEYINPUT )
195 	{
196 		sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
197 
198 		switch ( nCode )
199 		{
200 			case KEY_RETURN:
201 			case KEY_TAB:
202 			{
203 				if ( KEY_TAB == nCode )
204 					m_bRelease = sal_False;
205 				else
206 					nHandled = 1;
207 				Select();
208 				break;
209 			}
210 
211 			case KEY_ESCAPE:
212 				SetText( m_aCurText );
213 				ReleaseFocus_Impl();
214 				nHandled = 1;
215 				break;
216 		}
217 	}
218     else if( EVENT_LOSEFOCUS == rNEvt.GetType() )
219 	{
220 		Window* pFocusWin = Application::GetFocusWindow();
221 		if(!HasFocus() && GetSubEdit() != pFocusWin)
222 			SetText(GetSavedValue());
223 	}
224 
225     return nHandled ? nHandled : FontSizeBox::Notify( rNEvt );
226 }
227 
228 // ---------------------------------------------------------------------------
229 
DataChanged(const DataChangedEvent & rDCEvt)230 void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent& rDCEvt )
231 {
232 	if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
233          (rDCEvt.GetFlags() & SETTINGS_STYLE) )
234     {
235         SetSizePixel( LogicToPixel( m_aLogicalSize, MAP_APPFONT ));
236         Size aDropSize( m_aLogicalSize.Width(), LOGICAL_EDIT_HEIGHT );
237         SetDropDownSizePixel( LogicToPixel( aDropSize, MAP_APPFONT ));
238     }
239 
240     FontSizeBox::DataChanged( rDCEvt );
241 }
242 
243 //========================================================================
244 // class FontHeightToolBoxControl
245 //========================================================================
246 
FontHeightToolBoxControl(const uno::Reference<lang::XMultiServiceFactory> & rServiceManager)247 FontHeightToolBoxControl::FontHeightToolBoxControl(
248     const uno::Reference< lang::XMultiServiceFactory >& rServiceManager ) :
249     svt::ToolboxController( rServiceManager,
250                             uno::Reference< frame::XFrame >(),
251                             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontHeight" )) ),
252     m_pBox( NULL )
253 {
254     addStatusListener( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CharFontName" )));
255 }
256 
~FontHeightToolBoxControl()257 FontHeightToolBoxControl::~FontHeightToolBoxControl()
258 {
259 }
260 
261 // XInterface
queryInterface(const::com::sun::star::uno::Type & aType)262 ::com::sun::star::uno::Any SAL_CALL FontHeightToolBoxControl::queryInterface( const ::com::sun::star::uno::Type& aType )
263 throw (::com::sun::star::uno::RuntimeException)
264 {
265     uno::Any a = ToolboxController::queryInterface( aType );
266     if ( a.hasValue() )
267         return a;
268 
269     return ::cppu::queryInterface( aType, static_cast< lang::XServiceInfo* >( this ));
270 }
271 
acquire()272 void SAL_CALL FontHeightToolBoxControl::acquire() throw ()
273 {
274     ToolboxController::acquire();
275 }
276 
release()277 void SAL_CALL FontHeightToolBoxControl::release() throw ()
278 {
279     ToolboxController::release();
280 }
281 
282 // XServiceInfo
supportsService(const::rtl::OUString & ServiceName)283 sal_Bool SAL_CALL FontHeightToolBoxControl::supportsService( const ::rtl::OUString& ServiceName )
284 throw(uno::RuntimeException)
285 {
286     const uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
287     const ::rtl::OUString * pArray = aSNL.getConstArray();
288 
289     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
290         if( pArray[i] == ServiceName )
291             return true;
292 
293     return false;
294 }
295 
getImplementationName()296 ::rtl::OUString SAL_CALL FontHeightToolBoxControl::getImplementationName()
297 throw( uno::RuntimeException )
298 {
299 	return getImplementationName_Static();
300 }
301 
getSupportedServiceNames()302 uno::Sequence< ::rtl::OUString > SAL_CALL FontHeightToolBoxControl::getSupportedServiceNames(  )
303 throw( uno::RuntimeException )
304 {
305     return getSupportedServiceNames_Static();
306 }
307 
getSupportedServiceNames_Static()308 uno::Sequence< ::rtl::OUString > FontHeightToolBoxControl::getSupportedServiceNames_Static()
309 throw()
310 {
311     uno::Sequence< ::rtl::OUString > aSNS( 1 );
312     aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ));
313     return aSNS;
314 }
315 
316 // XComponent
dispose()317 void SAL_CALL FontHeightToolBoxControl::dispose()
318 throw (uno::RuntimeException)
319 {
320     svt::ToolboxController::dispose();
321 
322     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
323     delete m_pBox;
324     m_pBox = 0;
325 }
326 
327 // XStatusListener
statusChanged(const frame::FeatureStateEvent & rEvent)328 void SAL_CALL FontHeightToolBoxControl::statusChanged(
329     const frame::FeatureStateEvent& rEvent )
330 throw ( uno::RuntimeException )
331 {
332     if ( m_pBox )
333     {
334         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
335         if ( rEvent.FeatureURL.Path.equalsAscii( "FontHeight" ))
336         {
337 	        if ( rEvent.IsEnabled )
338 	        {
339                 m_pBox->Enable();
340                 frame::status::FontHeight aFontHeight;
341                 if ( rEvent.State >>= aFontHeight )
342                     m_pBox->statusChanged_Impl( long( 10. * aFontHeight.Height ), false );
343                 else
344                     m_pBox->statusChanged_Impl( long( -1 ), true );
345             }
346             else
347                 m_pBox->Disable();
348         }
349         else if ( rEvent.FeatureURL.Path.equalsAscii( "CharFontName" ))
350         {
351             if ( rEvent.State >>= m_aCurrentFont )
352                 m_pBox->UpdateFont( m_aCurrentFont );
353         }
354     }
355 }
356 
357 // XToolbarController
execute(sal_Int16)358 void SAL_CALL FontHeightToolBoxControl::execute( sal_Int16 /*KeyModifier*/ )
359 throw (::com::sun::star::uno::RuntimeException)
360 {
361 }
362 
click()363 void SAL_CALL FontHeightToolBoxControl::click()
364 throw (::com::sun::star::uno::RuntimeException)
365 {
366 }
367 
doubleClick()368 void SAL_CALL FontHeightToolBoxControl::doubleClick()
369 throw (::com::sun::star::uno::RuntimeException)
370 {
371 }
372 
createPopupWindow()373 uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createPopupWindow()
374 throw (::com::sun::star::uno::RuntimeException)
375 {
376     return uno::Reference< awt::XWindow >();
377 }
378 
createItemWindow(const uno::Reference<awt::XWindow> & Parent)379 uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createItemWindow(
380     const uno::Reference< awt::XWindow >& Parent )
381     throw (::com::sun::star::uno::RuntimeException)
382 {
383     uno::Reference< awt::XWindow > xItemWindow;
384     uno::Reference< awt::XWindow > xParent( Parent );
385 
386     Window* pParent = VCLUnoHelper::GetWindow( xParent );
387     if ( pParent )
388     {
389         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
390         m_pBox = new SvxFontSizeBox_Impl(
391                         pParent,
392                         uno::Reference< frame::XDispatchProvider >( m_xFrame, uno::UNO_QUERY ),
393                         m_xFrame,
394                         *this );
395         xItemWindow = VCLUnoHelper::GetInterface( m_pBox );
396     }
397 
398     return xItemWindow;
399 }
400 
dispatchCommand(const uno::Sequence<beans::PropertyValue> & rArgs)401 void FontHeightToolBoxControl::dispatchCommand(
402     const uno::Sequence< beans::PropertyValue >& rArgs )
403 {
404     uno::Reference< frame::XDispatchProvider > xDispatchProvider( m_xFrame, uno::UNO_QUERY );
405     if ( xDispatchProvider.is() )
406     {
407         util::URL                               aURL;
408         uno::Reference< frame::XDispatch >      xDispatch;
409         uno::Reference< util::XURLTransformer > xURLTransformer = getURLTransformer();
410 
411         aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontHeight" ));
412         xURLTransformer->parseStrict( aURL );
413         xDispatch = xDispatchProvider->queryDispatch( aURL, ::rtl::OUString(), 0 );
414         if ( xDispatch.is() )
415             xDispatch->dispatch( aURL, rArgs );
416     }
417 }
418 
FontHeightToolBoxControl_createInstance(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)419 uno::Reference< uno::XInterface > SAL_CALL FontHeightToolBoxControl_createInstance(
420     const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
421 {
422 	return *new FontHeightToolBoxControl( rSMgr );
423 }
424 
425 }
426