Line data Source code
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/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>
25 :
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/supportsservice.hxx>
37 :
38 : #include <boost/scoped_ptr.hpp>
39 :
40 : #define LOGICAL_EDIT_HEIGHT 12
41 :
42 : using namespace ::com::sun::star;
43 :
44 : namespace {
45 :
46 : class SvxFontSizeBox_Impl;
47 : class FontHeightToolBoxControl : public svt::ToolboxController,
48 : public lang::XServiceInfo
49 : {
50 : public:
51 : FontHeightToolBoxControl(
52 : const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& rServiceManager );
53 : virtual ~FontHeightToolBoxControl();
54 :
55 : // XInterface
56 : 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;
57 : virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
58 : virtual void SAL_CALL release() throw () SAL_OVERRIDE;
59 :
60 : // XServiceInfo
61 : virtual OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
62 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
63 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
64 :
65 : // XComponent
66 : virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 :
68 : // XStatusListener
69 : virtual void SAL_CALL statusChanged( const ::com::sun::star::frame::FeatureStateEvent& Event ) throw ( ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
70 :
71 : // XToolbarController
72 : virtual void SAL_CALL execute( sal_Int16 KeyModifier ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 : virtual void SAL_CALL click() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 : virtual void SAL_CALL doubleClick() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
75 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow > SAL_CALL createPopupWindow() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
76 : 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 :
78 : void dispatchCommand( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rArgs );
79 : using svt::ToolboxController::dispatchCommand;
80 :
81 : private:
82 : SvxFontSizeBox_Impl* m_pBox;
83 : ::com::sun::star::awt::FontDescriptor m_aCurrentFont;
84 : };
85 :
86 0 : class SvxFontSizeBox_Impl : public FontSizeBox
87 : {
88 : public:
89 : SvxFontSizeBox_Impl( Window* pParent,
90 : const uno::Reference< frame::XDispatchProvider >& rDispatchProvider,
91 : const uno::Reference< frame::XFrame >& _xFrame,
92 : FontHeightToolBoxControl& rCtrl );
93 :
94 : void statusChanged_Impl( long nHeight, bool bErase = false );
95 : void UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont );
96 : void SetOptimalSize();
97 :
98 : virtual bool Notify( NotifyEvent& rNEvt ) SAL_OVERRIDE;
99 :
100 : protected:
101 : virtual void Select() SAL_OVERRIDE;
102 : virtual void DataChanged( const DataChangedEvent& rDCEvt ) SAL_OVERRIDE;
103 :
104 : private:
105 : FontHeightToolBoxControl* m_pCtrl;
106 : OUString m_aCurText;
107 : Size m_aLogicalSize;
108 : bool m_bRelease;
109 : uno::Reference< frame::XDispatchProvider > m_xDispatchProvider;
110 : uno::Reference< frame::XFrame > m_xFrame;
111 : uno::Reference< awt::XWindow > m_xOldFocusWindow;
112 :
113 : void ReleaseFocus_Impl();
114 : };
115 :
116 :
117 : // class SvxFontSizeBox_Impl --------------------------------------------------
118 :
119 :
120 0 : SvxFontSizeBox_Impl::SvxFontSizeBox_Impl(
121 : Window* _pParent,
122 : const uno::Reference< frame::XDispatchProvider >& _rDispatchProvider,
123 : const uno::Reference< frame::XFrame >& _xFrame,
124 : FontHeightToolBoxControl& _rCtrl ) :
125 :
126 : FontSizeBox( _pParent, WinBits( WB_DROPDOWN ) ),
127 :
128 : m_pCtrl ( &_rCtrl ),
129 : m_aLogicalSize ( 0,100 ),
130 : m_bRelease ( true ),
131 : m_xDispatchProvider ( _rDispatchProvider ),
132 0 : m_xFrame ( _xFrame )
133 : {
134 0 : SetValue( 0 );
135 0 : SetText( "" );
136 0 : }
137 :
138 :
139 :
140 0 : void SvxFontSizeBox_Impl::ReleaseFocus_Impl()
141 : {
142 0 : if ( !m_bRelease )
143 : {
144 0 : m_bRelease = true;
145 0 : return;
146 : }
147 :
148 0 : if ( m_xFrame.is() && m_xFrame->getContainerWindow().is() )
149 0 : m_xFrame->getContainerWindow()->setFocus();
150 : }
151 :
152 :
153 :
154 0 : void SvxFontSizeBox_Impl::Select()
155 : {
156 0 : FontSizeBox::Select();
157 :
158 0 : if ( !IsTravelSelect() )
159 : {
160 0 : sal_Int64 nSelVal = GetValue();
161 0 : float fSelVal = float( nSelVal ) / 10;
162 :
163 0 : uno::Sequence< beans::PropertyValue > aArgs( 1 );
164 0 : aArgs[0].Name = "FontHeight.Height";
165 0 : aArgs[0].Value = uno::makeAny( fSelVal );
166 :
167 : /* #i33380# DR 2004-09-03 Moved the following line above the Dispatch() call.
168 : This instance may be deleted in the meantime (i.e. when a dialog is opened
169 : while in Dispatch()), accessing members will crash in this case. */
170 0 : ReleaseFocus_Impl();
171 :
172 0 : m_pCtrl->dispatchCommand( aArgs );
173 : }
174 0 : }
175 :
176 :
177 :
178 0 : void SvxFontSizeBox_Impl::statusChanged_Impl( long nPoint, bool bErase )
179 : {
180 0 : if ( !bErase )
181 : {
182 : // Metric Umrechnen
183 0 : long nVal = nPoint;
184 :
185 : // ge"andert => neuen Wert setzen
186 0 : if ( GetValue() != nVal )
187 0 : SetValue( nVal );
188 : }
189 : else
190 : {
191 : // Wert in der Anzeige l"oschen
192 0 : SetValue( -1L );
193 0 : SetText( "" );
194 : }
195 0 : SaveValue();
196 0 : }
197 :
198 :
199 :
200 0 : void SvxFontSizeBox_Impl::UpdateFont( const ::com::sun::star::awt::FontDescriptor& rCurrentFont )
201 : {
202 : // Sizes-Liste auff"ullen
203 0 : sal_Int64 nOldVal = GetValue(); // alten Wert merken
204 0 : const FontList* _pFontList = NULL;
205 0 : boost::scoped_ptr<FontList> aHold( new FontList( this ));
206 0 : _pFontList = aHold.get();
207 :
208 0 : if ( !rCurrentFont.Name.isEmpty() )
209 : {
210 0 : FontInfo _aFontInfo;
211 0 : _aFontInfo.SetName( rCurrentFont.Name );
212 0 : _aFontInfo.SetStyleName( rCurrentFont.StyleName );
213 0 : _aFontInfo.SetHeight( rCurrentFont.Height );
214 0 : Fill( &_aFontInfo, _pFontList );
215 : }
216 : else
217 : {
218 0 : Fill( NULL, _pFontList );
219 : }
220 0 : SetValue( nOldVal ); // alten Wert wiederherstellen
221 0 : m_aCurText = GetText(); // zum R"ucksetzen bei ESC merken
222 0 : }
223 :
224 :
225 :
226 0 : bool SvxFontSizeBox_Impl::Notify( NotifyEvent& rNEvt )
227 : {
228 0 : bool nHandled = false;
229 :
230 0 : if ( rNEvt.GetType() == EVENT_KEYINPUT )
231 : {
232 0 : sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
233 :
234 0 : switch ( nCode )
235 : {
236 : case KEY_RETURN:
237 : case KEY_TAB:
238 : {
239 0 : if ( KEY_TAB == nCode )
240 0 : m_bRelease = false;
241 : else
242 0 : nHandled = true;
243 0 : Select();
244 0 : break;
245 : }
246 :
247 : case KEY_ESCAPE:
248 0 : SetText( m_aCurText );
249 0 : ReleaseFocus_Impl();
250 0 : nHandled = true;
251 0 : break;
252 : }
253 : }
254 0 : else if( EVENT_LOSEFOCUS == rNEvt.GetType() )
255 : {
256 0 : Window* pFocusWin = Application::GetFocusWindow();
257 0 : if(!HasFocus() && GetSubEdit() != pFocusWin)
258 0 : SetText(GetSavedValue());
259 : }
260 :
261 0 : return nHandled || FontSizeBox::Notify( rNEvt );
262 : }
263 :
264 0 : void SvxFontSizeBox_Impl::SetOptimalSize()
265 : {
266 0 : Size aPrefSize(LogicToPixel(m_aLogicalSize, MAP_APPFONT));
267 0 : aPrefSize.Width() = get_preferred_size().Width();
268 0 : SetSizePixel(aPrefSize);
269 0 : Size aDropSize(LogicToPixel(Size(0, LOGICAL_EDIT_HEIGHT), MAP_APPFONT));
270 0 : aDropSize.Width() = aPrefSize.Width();
271 0 : SetDropDownSizePixel(aDropSize);
272 0 : }
273 :
274 :
275 :
276 0 : void SvxFontSizeBox_Impl::DataChanged( const DataChangedEvent& rDCEvt )
277 : {
278 0 : if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
279 0 : (rDCEvt.GetFlags() & SETTINGS_STYLE) )
280 : {
281 0 : SetOptimalSize();
282 : }
283 :
284 0 : FontSizeBox::DataChanged( rDCEvt );
285 0 : }
286 :
287 :
288 : // class FontHeightToolBoxControl
289 :
290 :
291 0 : FontHeightToolBoxControl::FontHeightToolBoxControl( const uno::Reference< uno::XComponentContext >& rxContext )
292 : : svt::ToolboxController( rxContext,
293 : uno::Reference< frame::XFrame >(),
294 : OUString( ".uno:FontHeight" ) ),
295 0 : m_pBox( NULL )
296 : {
297 0 : addStatusListener( OUString( ".uno:CharFontName" ));
298 0 : }
299 :
300 0 : FontHeightToolBoxControl::~FontHeightToolBoxControl()
301 : {
302 0 : }
303 :
304 : // XInterface
305 0 : ::com::sun::star::uno::Any SAL_CALL FontHeightToolBoxControl::queryInterface( const ::com::sun::star::uno::Type& aType )
306 : throw (::com::sun::star::uno::RuntimeException, std::exception)
307 : {
308 0 : uno::Any a = ToolboxController::queryInterface( aType );
309 0 : if ( a.hasValue() )
310 0 : return a;
311 :
312 0 : return ::cppu::queryInterface( aType, static_cast< lang::XServiceInfo* >( this ));
313 : }
314 :
315 0 : void SAL_CALL FontHeightToolBoxControl::acquire() throw ()
316 : {
317 0 : ToolboxController::acquire();
318 0 : }
319 :
320 0 : void SAL_CALL FontHeightToolBoxControl::release() throw ()
321 : {
322 0 : ToolboxController::release();
323 0 : }
324 :
325 : // XServiceInfo
326 0 : sal_Bool SAL_CALL FontHeightToolBoxControl::supportsService( const OUString& ServiceName )
327 : throw(uno::RuntimeException, std::exception)
328 : {
329 0 : return cppu::supportsService(this, ServiceName);
330 : }
331 :
332 0 : OUString SAL_CALL FontHeightToolBoxControl::getImplementationName()
333 : throw( uno::RuntimeException, std::exception )
334 : {
335 0 : return OUString("com.sun.star.svx.FontHeightToolBoxController");
336 : }
337 :
338 0 : uno::Sequence< OUString > SAL_CALL FontHeightToolBoxControl::getSupportedServiceNames( )
339 : throw( uno::RuntimeException, std::exception )
340 : {
341 0 : uno::Sequence< OUString > aSNS( 1 );
342 0 : aSNS.getArray()[0] = "com.sun.star.frame.ToolbarController";
343 0 : return aSNS;
344 : }
345 :
346 : // XComponent
347 0 : void SAL_CALL FontHeightToolBoxControl::dispose()
348 : throw (uno::RuntimeException, std::exception)
349 : {
350 0 : svt::ToolboxController::dispose();
351 :
352 0 : SolarMutexGuard aSolarMutexGuard;
353 0 : delete m_pBox;
354 0 : m_pBox = 0;
355 0 : }
356 :
357 : // XStatusListener
358 0 : void SAL_CALL FontHeightToolBoxControl::statusChanged(
359 : const frame::FeatureStateEvent& rEvent )
360 : throw ( uno::RuntimeException, std::exception )
361 : {
362 0 : if ( m_pBox )
363 : {
364 0 : SolarMutexGuard aSolarMutexGuard;
365 0 : if ( rEvent.FeatureURL.Path == "FontHeight" )
366 : {
367 0 : if ( rEvent.IsEnabled )
368 : {
369 0 : m_pBox->Enable();
370 0 : frame::status::FontHeight aFontHeight;
371 0 : if ( rEvent.State >>= aFontHeight )
372 0 : m_pBox->statusChanged_Impl( long( 10. * aFontHeight.Height ), false );
373 : else
374 0 : m_pBox->statusChanged_Impl( long( -1 ), true );
375 : }
376 : else
377 0 : m_pBox->Disable();
378 : }
379 0 : else if ( rEvent.FeatureURL.Path == "CharFontName" )
380 : {
381 0 : if ( rEvent.State >>= m_aCurrentFont )
382 0 : m_pBox->UpdateFont( m_aCurrentFont );
383 0 : }
384 : }
385 0 : }
386 :
387 : // XToolbarController
388 0 : void SAL_CALL FontHeightToolBoxControl::execute( sal_Int16 /*KeyModifier*/ )
389 : throw (::com::sun::star::uno::RuntimeException, std::exception)
390 : {
391 0 : }
392 :
393 0 : void SAL_CALL FontHeightToolBoxControl::click()
394 : throw (::com::sun::star::uno::RuntimeException, std::exception)
395 : {
396 0 : }
397 :
398 0 : void SAL_CALL FontHeightToolBoxControl::doubleClick()
399 : throw (::com::sun::star::uno::RuntimeException, std::exception)
400 : {
401 0 : }
402 :
403 0 : uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createPopupWindow()
404 : throw (::com::sun::star::uno::RuntimeException, std::exception)
405 : {
406 0 : return uno::Reference< awt::XWindow >();
407 : }
408 :
409 0 : uno::Reference< awt::XWindow > SAL_CALL FontHeightToolBoxControl::createItemWindow(
410 : const uno::Reference< awt::XWindow >& Parent )
411 : throw (::com::sun::star::uno::RuntimeException, std::exception)
412 : {
413 0 : uno::Reference< awt::XWindow > xItemWindow;
414 0 : uno::Reference< awt::XWindow > xParent( Parent );
415 :
416 0 : Window* pParent = VCLUnoHelper::GetWindow( xParent );
417 0 : if ( pParent )
418 : {
419 0 : SolarMutexGuard aSolarMutexGuard;
420 : m_pBox = new SvxFontSizeBox_Impl(
421 : pParent,
422 : uno::Reference< frame::XDispatchProvider >( m_xFrame, uno::UNO_QUERY ),
423 : m_xFrame,
424 0 : *this );
425 : //Get the box to fill itself with all its sizes
426 0 : m_pBox->UpdateFont(m_aCurrentFont);
427 : //Make it size itself to its optimal size re above sizes
428 0 : m_pBox->SetOptimalSize();
429 0 : xItemWindow = VCLUnoHelper::GetInterface( m_pBox );
430 : }
431 :
432 0 : return xItemWindow;
433 : }
434 :
435 0 : void FontHeightToolBoxControl::dispatchCommand(
436 : const uno::Sequence< beans::PropertyValue >& rArgs )
437 : {
438 0 : uno::Reference< frame::XDispatchProvider > xDispatchProvider( m_xFrame, uno::UNO_QUERY );
439 0 : if ( xDispatchProvider.is() )
440 : {
441 0 : util::URL aURL;
442 0 : uno::Reference< frame::XDispatch > xDispatch;
443 0 : uno::Reference< util::XURLTransformer > xURLTransformer = getURLTransformer();
444 :
445 0 : aURL.Complete = ".uno:FontHeight";
446 0 : xURLTransformer->parseStrict( aURL );
447 0 : xDispatch = xDispatchProvider->queryDispatch( aURL, OUString(), 0 );
448 0 : if ( xDispatch.is() )
449 0 : xDispatch->dispatch( aURL, rArgs );
450 0 : }
451 0 : }
452 :
453 : }
454 :
455 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
456 0 : com_sun_star_svx_FontHeightToolBoxController_get_implementation(
457 : css::uno::XComponentContext *rxContext,
458 : css::uno::Sequence<css::uno::Any> const &)
459 : {
460 0 : return cppu::acquire(new FontHeightToolBoxControl(rxContext));
461 : }
462 :
463 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|