merge the formfield patch from ooo-build
[ooovba.git] / framework / source / uielement / fontsizemenucontroller.cxx
blobe3ff3f2033e6ce858d4aac5ef04da5cdce3880e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fontsizemenucontroller.cxx,v $
10 * $Revision: 1.10.40.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <uielement/fontsizemenucontroller.hxx>
35 //_________________________________________________________________________________________________________________
36 // my own includes
37 //_________________________________________________________________________________________________________________
38 #include <threadhelp/resetableguard.hxx>
39 #include "services.h"
41 //_________________________________________________________________________________________________________________
42 // interface includes
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/awt/XDevice.hpp>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/awt/MenuItemStyle.hpp>
47 #include <com/sun/star/frame/XDispatchProvider.hpp>
48 #include <com/sun/star/view/XPrintable.hpp>
50 //_________________________________________________________________________________________________________________
51 // includes of other projects
52 //_________________________________________________________________________________________________________________
54 #ifndef _VCL_MENU_HXX_
55 #include <vcl/menu.hxx>
56 #endif
57 #include <vcl/mapunit.hxx>
58 #ifndef _VCL_SVAPP_HXX_
59 #include <vcl/svapp.hxx>
60 #endif
61 #include <vcl/i18nhelp.hxx>
62 #ifndef _VCL_OUTPUTDEVICE_HXX_
63 #include <vcl/outdev.hxx>
64 #endif
65 #include <vcl/print.hxx>
66 #ifndef _SVTOOLS_CTRLTOOL_HXX_
67 #include <svtools/ctrltool.hxx>
68 #endif
69 #include <dispatch/uieventloghelper.hxx>
71 //_________________________________________________________________________________________________________________
72 // Defines
73 //_________________________________________________________________________________________________________________
74 //
76 using namespace com::sun::star::uno;
77 using namespace com::sun::star::lang;
78 using namespace com::sun::star::frame;
79 using namespace com::sun::star::beans;
80 using namespace com::sun::star::util;
81 using namespace com::sun::star::view;
82 using namespace com::sun::star::beans;
84 namespace framework
87 DEFINE_XSERVICEINFO_MULTISERVICE ( FontSizeMenuController ,
88 OWeakObject ,
89 SERVICENAME_POPUPMENUCONTROLLER ,
90 IMPLEMENTATIONNAME_FONTSIZEMENUCONTROLLER
93 DEFINE_INIT_SERVICE ( FontSizeMenuController, {} )
95 FontSizeMenuController::FontSizeMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
96 PopupMenuControllerBase( xServiceManager ),
97 m_pHeightArray( 0 ),
98 m_bRebuildMenu( sal_True )
102 FontSizeMenuController::~FontSizeMenuController()
104 delete []m_pHeightArray;
107 // private function
108 rtl::OUString FontSizeMenuController::retrievePrinterName( com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame )
110 rtl::OUString aPrinterName;
112 if ( rFrame.is() )
114 Reference< XController > xController = m_xFrame->getController();
115 if ( xController.is() )
117 Reference< XPrintable > xPrintable( xController->getModel(), UNO_QUERY );
118 if ( xPrintable.is() )
120 Sequence< PropertyValue > aPrinterSeq = xPrintable->getPrinter();
121 for ( int i = 0; i < aPrinterSeq.getLength(); i++ )
123 if ( aPrinterSeq[i].Name.equalsAscii( "Name" ))
125 aPrinterSeq[i].Value >>= aPrinterName;
126 break;
133 return aPrinterName;
136 // private function
137 void FontSizeMenuController::setCurHeight( long nHeight, Reference< css::awt::XPopupMenu >& rPopupMenu )
139 // check menu item
140 rtl::OUString aHeight = Application::GetSettings().GetUILocaleI18nHelper().GetNum( nHeight, 1, TRUE, FALSE );
141 USHORT nChecked = 0;
142 USHORT nItemCount = rPopupMenu->getItemCount();
143 for( USHORT i = 0; i < nItemCount; i++ )
145 USHORT nItemId = rPopupMenu->getItemId( i );
147 if ( m_pHeightArray[i] == nHeight )
149 rPopupMenu->checkItem( nItemId, TRUE );
150 return;
153 if ( rPopupMenu->isItemChecked( nItemId ) )
154 nChecked = nItemId;
157 if ( nChecked )
158 rPopupMenu->checkItem( nChecked, FALSE );
161 // private function
162 void FontSizeMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
164 const rtl::OUString aFontNameCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontHeight?FontHeight=" ));
165 VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
166 PopupMenu* pVCLPopupMenu = 0;
168 resetPopupMenu( rPopupMenu );
169 if ( pPopupMenu )
170 pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
172 if ( pVCLPopupMenu )
174 FontList* pFontList = 0;
175 Printer* pInfoPrinter = 0;
176 rtl::OUString aPrinterName;
178 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
180 // try to retrieve printer name of document
181 aPrinterName = retrievePrinterName( m_xFrame );
182 if ( aPrinterName.getLength() > 0 )
184 pInfoPrinter = new Printer( aPrinterName );
185 if ( pInfoPrinter && pInfoPrinter->GetDevFontCount() > 0 )
186 pFontList = new FontList( pInfoPrinter );
189 if ( pFontList == 0 )
190 pFontList = new FontList( Application::GetDefaultDevice() );
192 FontInfo aFntInfo = pFontList->Get( m_aFontDescriptor.Name, m_aFontDescriptor.StyleName );
194 // setup font size array
195 if ( m_pHeightArray )
196 delete m_pHeightArray;
198 const long* pTempAry;
199 const long* pAry = pFontList->GetSizeAry( aFntInfo );
200 USHORT nSizeCount = 0;
201 while ( pAry[nSizeCount] )
202 nSizeCount++;
204 USHORT nPos = 0;
205 const rtl::OUString aFontHeightCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontHeight?FontHeight.Height:float=" ));
207 // first insert font size names (for simplified/traditional chinese)
208 float fPoint;
209 rtl::OUString aHeightString;
210 FontSizeNames aFontSizeNames( Application::GetSettings().GetUILanguage() );
211 m_pHeightArray = new long[nSizeCount+aFontSizeNames.Count()];
212 rtl::OUString aCommand;
214 if ( !aFontSizeNames.IsEmpty() )
216 if ( pAry == pFontList->GetStdSizeAry() )
218 // for scalable fonts all font size names
219 ULONG nCount = aFontSizeNames.Count();
220 for( ULONG i = 0; i < nCount; i++ )
222 String aSizeName = aFontSizeNames.GetIndexName( i );
223 long nSize = aFontSizeNames.GetIndexSize( i );
224 m_pHeightArray[nPos] = nSize;
225 nPos++; // Id is nPos+1
226 pVCLPopupMenu->InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
227 fPoint = float( m_pHeightArray[nPos-1] ) / 10;
229 // Create dispatchable .uno command and set it
230 aCommand = aFontHeightCommand + rtl::OUString::valueOf( fPoint );
231 pVCLPopupMenu->SetItemCommand( nPos, aCommand );
234 else
236 // for fixed size fonts only selectable font size names
237 pTempAry = pAry;
238 while ( *pTempAry )
240 String aSizeName = aFontSizeNames.Size2Name( *pTempAry );
241 if ( aSizeName.Len() )
243 m_pHeightArray[nPos] = *pTempAry;
244 nPos++; // Id is nPos+1
245 pVCLPopupMenu->InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
246 fPoint = float( m_pHeightArray[nPos-1] ) / 10;
248 // Create dispatchable .uno command and set it
249 aCommand = aFontHeightCommand + rtl::OUString::valueOf( fPoint );
250 pVCLPopupMenu->SetItemCommand( nPos, aCommand );
252 pTempAry++;
257 // then insert numerical font size values
258 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
259 pTempAry = pAry;
260 while ( *pTempAry )
262 m_pHeightArray[nPos] = *pTempAry;
263 nPos++; // Id is nPos+1
264 pVCLPopupMenu->InsertItem( nPos, rI18nHelper.GetNum( *pTempAry, 1, TRUE, FALSE ), MIB_RADIOCHECK | MIB_AUTOCHECK );
265 fPoint = float( m_pHeightArray[nPos-1] ) / 10;
267 // Create dispatchable .uno command and set it
268 aCommand = aFontHeightCommand + rtl::OUString::valueOf( fPoint );
269 pVCLPopupMenu->SetItemCommand( nPos, aCommand );
271 pTempAry++;
274 setCurHeight( long( m_aFontHeight.Height * 10), rPopupMenu );
276 delete pFontList;
277 delete pInfoPrinter;
281 // XEventListener
282 void SAL_CALL FontSizeMenuController::disposing( const EventObject& ) throw ( RuntimeException )
284 Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
286 ResetableGuard aLock( m_aLock );
287 m_xFrame.clear();
288 m_xDispatch.clear();
289 m_xCurrentFontDispatch.clear();
290 if ( m_xPopupMenu.is() )
291 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
292 m_xPopupMenu.clear();
295 // XStatusListener
296 void SAL_CALL FontSizeMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
298 com::sun::star::awt::FontDescriptor aFontDescriptor;
299 ::com::sun::star::frame::status::FontHeight aFontHeight;
301 if ( Event.State >>= aFontDescriptor )
303 ResetableGuard aLock( m_aLock );
304 m_aFontDescriptor = aFontDescriptor;
306 if ( m_xPopupMenu.is() )
307 fillPopupMenu( m_xPopupMenu );
310 else if ( Event.State >>= aFontHeight )
312 ResetableGuard aLock( m_aLock );
313 m_aFontHeight = aFontHeight;
315 if ( m_xPopupMenu.is() )
317 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
318 setCurHeight( long( m_aFontHeight.Height * 10), m_xPopupMenu );
323 // XMenuListener
324 void FontSizeMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
326 Sequence<PropertyValue> aArgs;
327 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
328 UiEventLogHelper(::rtl::OUString::createFromAscii("FontSizeMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgs);
329 _xDispatch->dispatch( aTargetURL, aArgs );
332 // XPopupMenuController
333 void FontSizeMenuController::impl_setPopupMenu()
335 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
336 com::sun::star::util::URL aTargetURL;
337 // Register for font name updates which gives us info about the current font!
338 aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CharFontName" ));
339 m_xURLTransformer->parseStrict( aTargetURL );
340 m_xCurrentFontDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
343 void SAL_CALL FontSizeMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException )
345 ResetableGuard aLock( m_aLock );
347 if ( m_bDisposed )
348 throw DisposedException();
350 Reference< XDispatch > xDispatch( m_xCurrentFontDispatch );
351 com::sun::star::util::URL aTargetURL;
352 aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CharFontName" ));
353 m_xURLTransformer->parseStrict( aTargetURL );
354 aLock.unlock();
356 if ( xDispatch.is() )
358 xDispatch->addStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );
359 xDispatch->removeStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );
362 PopupMenuControllerBase::updatePopupMenu();