Update ooo320-m1
[ooovba.git] / framework / source / uielement / fontmenucontroller.cxx
blob320e7ffbe17d05158cb57858c5f737245f48f794
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: fontmenucontroller.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/fontmenucontroller.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>
50 //_________________________________________________________________________________________________________________
51 // includes of other projects
52 //_________________________________________________________________________________________________________________
54 #ifndef _VCL_MENU_HXX_
55 #include <vcl/menu.hxx>
56 #endif
57 #include <vcl/svapp.hxx>
58 #include <vcl/i18nhelp.hxx>
59 #include <tools/urlobj.hxx>
60 #include <rtl/ustrbuf.hxx>
61 #ifndef _VCL_MNEMONIC_HXX_
62 #include <vcl/mnemonic.hxx>
63 #endif
64 #include <dispatch/uieventloghelper.hxx>
66 //_________________________________________________________________________________________________________________
67 // Defines
68 //_________________________________________________________________________________________________________________
69 //
71 using namespace com::sun::star::uno;
72 using namespace com::sun::star::lang;
73 using namespace com::sun::star::frame;
74 using namespace com::sun::star::beans;
75 using namespace com::sun::star::util;
77 using namespace std;
79 bool lcl_I18nCompareString(const rtl::OUString& rStr1, const rtl::OUString& rStr2)
81 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
82 return rI18nHelper.CompareString( rStr1, rStr2 ) < 0 ? true : false;
85 namespace framework
88 DEFINE_XSERVICEINFO_MULTISERVICE ( FontMenuController ,
89 OWeakObject ,
90 SERVICENAME_POPUPMENUCONTROLLER ,
91 IMPLEMENTATIONNAME_FONTMENUCONTROLLER
94 DEFINE_INIT_SERVICE ( FontMenuController, {} )
96 FontMenuController::FontMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
97 PopupMenuControllerBase( xServiceManager )
101 FontMenuController::~FontMenuController()
105 // private function
106 void FontMenuController::fillPopupMenu( const Sequence< ::rtl::OUString >& rFontNameSeq, Reference< css::awt::XPopupMenu >& rPopupMenu )
108 const rtl::OUString* pFontNameArray = rFontNameSeq.getConstArray();
109 VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
110 PopupMenu* pVCLPopupMenu = 0;
112 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
114 resetPopupMenu( rPopupMenu );
115 if ( pPopupMenu )
116 pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
118 if ( pVCLPopupMenu )
120 vector<rtl::OUString> aVector;
121 aVector.reserve(rFontNameSeq.getLength());
122 for ( USHORT i = 0; i < rFontNameSeq.getLength(); i++ )
124 aVector.push_back(MnemonicGenerator::EraseAllMnemonicChars(pFontNameArray[i]));
126 sort(aVector.begin(), aVector.end(), lcl_I18nCompareString );
128 const rtl::OUString aFontNameCommandPrefix( RTL_CONSTASCII_USTRINGPARAM( ".uno:CharFontName?CharFontName.FamilyName:string=" ));
129 const sal_Int16 nCount = (sal_Int16)aVector.size();
130 for ( sal_Int16 i = 0; i < nCount; i++ )
132 const rtl::OUString& rName = aVector[i];
133 m_xPopupMenu->insertItem( i+1, rName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, i );
134 if ( rName == m_aFontFamilyName )
135 m_xPopupMenu->checkItem( i+1, sal_True );
136 // use VCL popup menu pointer to set vital information that are not part of the awt implementation
137 rtl::OUStringBuffer aCommandBuffer( aFontNameCommandPrefix );
138 aCommandBuffer.append( INetURLObject::encode( rName, INetURLObject::PART_HTTP_QUERY, '%', INetURLObject::ENCODE_ALL ));
139 rtl::OUString aFontNameCommand = aCommandBuffer.makeStringAndClear();
140 pVCLPopupMenu->SetItemCommand( i+1, aFontNameCommand ); // Store font name into item command.
146 // XEventListener
147 void SAL_CALL FontMenuController::disposing( const EventObject& ) throw ( RuntimeException )
149 Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
151 ResetableGuard aLock( m_aLock );
152 m_xFrame.clear();
153 m_xDispatch.clear();
154 m_xFontListDispatch.clear();
155 m_xServiceManager.clear();
157 if ( m_xPopupMenu.is() )
158 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
159 m_xPopupMenu.clear();
162 // XStatusListener
163 void SAL_CALL FontMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
165 com::sun::star::awt::FontDescriptor aFontDescriptor;
166 Sequence< rtl::OUString > aFontNameSeq;
168 if ( Event.State >>= aFontDescriptor )
170 ResetableGuard aLock( m_aLock );
171 m_aFontFamilyName = aFontDescriptor.Name;
173 else if ( Event.State >>= aFontNameSeq )
175 ResetableGuard aLock( m_aLock );
176 if ( m_xPopupMenu.is() )
177 fillPopupMenu( aFontNameSeq, m_xPopupMenu );
181 // XMenuListener
182 void FontMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
184 Sequence<PropertyValue> aArgs;
185 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
186 UiEventLogHelper(::rtl::OUString::createFromAscii("FontMenuController")).log(
187 m_xServiceManager,
188 m_xFrame,
189 aTargetURL,
190 Sequence<PropertyValue>());
191 _xDispatch->dispatch( aTargetURL, aArgs );
194 void SAL_CALL FontMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
196 ResetableGuard aLock( m_aLock );
198 if ( m_xPopupMenu.is() )
200 // find new font name and set check mark!
201 USHORT nChecked = 0;
202 USHORT nItemCount = m_xPopupMenu->getItemCount();
203 rtl::OUString aEmpty;
204 for( USHORT i = 0; i < nItemCount; i++ )
206 USHORT nItemId = m_xPopupMenu->getItemId( i );
208 if ( m_xPopupMenu->isItemChecked( nItemId ) )
209 nChecked = nItemId;
211 rtl::OUString aText = m_xPopupMenu->getItemText( nItemId );
213 // TODO: must be replaced by implementation of VCL, when available
214 sal_Int32 nIndex = aText.indexOf( (sal_Unicode)'~' );
215 if ( nIndex >= 0 )
216 aText = aText.replaceAt( nIndex, 1, aEmpty );
217 // TODO: must be replaced by implementation of VCL, when available
219 if ( aText == m_aFontFamilyName )
221 m_xPopupMenu->checkItem( nItemId, sal_True );
222 return;
226 if ( nChecked )
227 m_xPopupMenu->checkItem( nChecked, sal_False );
231 // XPopupMenuController
232 void FontMenuController::impl_setPopupMenu()
234 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
236 com::sun::star::util::URL aTargetURL;
237 // Register for font list updates to get the current font list from the controller
238 aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontNameList" ));
239 m_xURLTransformer->parseStrict( aTargetURL );
240 m_xFontListDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
243 void SAL_CALL FontMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException )
245 PopupMenuControllerBase::updatePopupMenu();
247 ResetableGuard aLock( m_aLock );
248 Reference< XDispatch > xDispatch( m_xFontListDispatch );
249 com::sun::star::util::URL aTargetURL;
250 aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontNameList" ));
251 m_xURLTransformer->parseStrict( aTargetURL );
252 aLock.unlock();
254 if ( xDispatch.is() )
256 xDispatch->addStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );
257 xDispatch->removeStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL );