1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_framework.hxx" 30*cdf0e10cSrcweir #include <uielement/fontmenucontroller.hxx> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 33*cdf0e10cSrcweir // my own includes 34*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 35*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx> 36*cdf0e10cSrcweir #include "services.h" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 39*cdf0e10cSrcweir // interface includes 40*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 41*cdf0e10cSrcweir #include <com/sun/star/awt/XDevice.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 48*cdf0e10cSrcweir // includes of other projects 49*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir #ifndef _VCL_MENU_HXX_ 52*cdf0e10cSrcweir #include <vcl/menu.hxx> 53*cdf0e10cSrcweir #endif 54*cdf0e10cSrcweir #include <vcl/svapp.hxx> 55*cdf0e10cSrcweir #include <vcl/i18nhelp.hxx> 56*cdf0e10cSrcweir #include <tools/urlobj.hxx> 57*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 58*cdf0e10cSrcweir #ifndef _VCL_MNEMONIC_HXX_ 59*cdf0e10cSrcweir #include <vcl/mnemonic.hxx> 60*cdf0e10cSrcweir #endif 61*cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx> 62*cdf0e10cSrcweir #include <vos/mutex.hxx> 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 65*cdf0e10cSrcweir // Defines 66*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 67*cdf0e10cSrcweir // 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir using namespace com::sun::star::uno; 70*cdf0e10cSrcweir using namespace com::sun::star::lang; 71*cdf0e10cSrcweir using namespace com::sun::star::frame; 72*cdf0e10cSrcweir using namespace com::sun::star::beans; 73*cdf0e10cSrcweir using namespace com::sun::star::util; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir using namespace std; 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir bool lcl_I18nCompareString(const rtl::OUString& rStr1, const rtl::OUString& rStr2) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper(); 80*cdf0e10cSrcweir return rI18nHelper.CompareString( rStr1, rStr2 ) < 0 ? true : false; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir namespace framework 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE ( FontMenuController , 87*cdf0e10cSrcweir OWeakObject , 88*cdf0e10cSrcweir SERVICENAME_POPUPMENUCONTROLLER , 89*cdf0e10cSrcweir IMPLEMENTATIONNAME_FONTMENUCONTROLLER 90*cdf0e10cSrcweir ) 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir DEFINE_INIT_SERVICE ( FontMenuController, {} ) 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir FontMenuController::FontMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) : 95*cdf0e10cSrcweir svt::PopupMenuControllerBase( xServiceManager ) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir FontMenuController::~FontMenuController() 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // private function 104*cdf0e10cSrcweir void FontMenuController::fillPopupMenu( const Sequence< ::rtl::OUString >& rFontNameSeq, Reference< css::awt::XPopupMenu >& rPopupMenu ) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir const rtl::OUString* pFontNameArray = rFontNameSeq.getConstArray(); 107*cdf0e10cSrcweir VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu ); 108*cdf0e10cSrcweir PopupMenu* pVCLPopupMenu = 0; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() ); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir resetPopupMenu( rPopupMenu ); 113*cdf0e10cSrcweir if ( pPopupMenu ) 114*cdf0e10cSrcweir pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu(); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir if ( pVCLPopupMenu ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir vector<rtl::OUString> aVector; 119*cdf0e10cSrcweir aVector.reserve(rFontNameSeq.getLength()); 120*cdf0e10cSrcweir for ( sal_uInt16 i = 0; i < rFontNameSeq.getLength(); i++ ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir aVector.push_back(MnemonicGenerator::EraseAllMnemonicChars(pFontNameArray[i])); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir sort(aVector.begin(), aVector.end(), lcl_I18nCompareString ); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir const rtl::OUString aFontNameCommandPrefix( RTL_CONSTASCII_USTRINGPARAM( ".uno:CharFontName?CharFontName.FamilyName:string=" )); 127*cdf0e10cSrcweir const sal_Int16 nCount = (sal_Int16)aVector.size(); 128*cdf0e10cSrcweir for ( sal_Int16 i = 0; i < nCount; i++ ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir const rtl::OUString& rName = aVector[i]; 131*cdf0e10cSrcweir m_xPopupMenu->insertItem( i+1, rName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, i ); 132*cdf0e10cSrcweir if ( rName == m_aFontFamilyName ) 133*cdf0e10cSrcweir m_xPopupMenu->checkItem( i+1, sal_True ); 134*cdf0e10cSrcweir // use VCL popup menu pointer to set vital information that are not part of the awt implementation 135*cdf0e10cSrcweir rtl::OUStringBuffer aCommandBuffer( aFontNameCommandPrefix ); 136*cdf0e10cSrcweir aCommandBuffer.append( INetURLObject::encode( rName, INetURLObject::PART_HTTP_QUERY, '%', INetURLObject::ENCODE_ALL )); 137*cdf0e10cSrcweir rtl::OUString aFontNameCommand = aCommandBuffer.makeStringAndClear(); 138*cdf0e10cSrcweir pVCLPopupMenu->SetItemCommand( i+1, aFontNameCommand ); // Store font name into item command. 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // XEventListener 145*cdf0e10cSrcweir void SAL_CALL FontMenuController::disposing( const EventObject& ) throw ( RuntimeException ) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY ); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 150*cdf0e10cSrcweir m_xFrame.clear(); 151*cdf0e10cSrcweir m_xDispatch.clear(); 152*cdf0e10cSrcweir m_xFontListDispatch.clear(); 153*cdf0e10cSrcweir m_xServiceManager.clear(); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir if ( m_xPopupMenu.is() ) 156*cdf0e10cSrcweir m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY )); 157*cdf0e10cSrcweir m_xPopupMenu.clear(); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir // XStatusListener 161*cdf0e10cSrcweir void SAL_CALL FontMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException ) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir com::sun::star::awt::FontDescriptor aFontDescriptor; 164*cdf0e10cSrcweir Sequence< rtl::OUString > aFontNameSeq; 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir if ( Event.State >>= aFontDescriptor ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 169*cdf0e10cSrcweir m_aFontFamilyName = aFontDescriptor.Name; 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir else if ( Event.State >>= aFontNameSeq ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 174*cdf0e10cSrcweir if ( m_xPopupMenu.is() ) 175*cdf0e10cSrcweir fillPopupMenu( aFontNameSeq, m_xPopupMenu ); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // XMenuListener 180*cdf0e10cSrcweir void FontMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir Sequence<PropertyValue> aArgs; 183*cdf0e10cSrcweir if(::comphelper::UiEventsLogger::isEnabled()) //#i88653# 184*cdf0e10cSrcweir UiEventLogHelper(::rtl::OUString::createFromAscii("FontMenuController")).log( 185*cdf0e10cSrcweir m_xServiceManager, 186*cdf0e10cSrcweir m_xFrame, 187*cdf0e10cSrcweir aTargetURL, 188*cdf0e10cSrcweir Sequence<PropertyValue>()); 189*cdf0e10cSrcweir OSL_ENSURE(_xDispatch.is(),"FontMenuController::impl_select: No dispatch"); 190*cdf0e10cSrcweir if ( _xDispatch.is() ) 191*cdf0e10cSrcweir _xDispatch->dispatch( aTargetURL, aArgs ); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir void SAL_CALL FontMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir if ( m_xPopupMenu.is() ) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir // find new font name and set check mark! 201*cdf0e10cSrcweir sal_uInt16 nChecked = 0; 202*cdf0e10cSrcweir sal_uInt16 nItemCount = m_xPopupMenu->getItemCount(); 203*cdf0e10cSrcweir rtl::OUString aEmpty; 204*cdf0e10cSrcweir for( sal_uInt16 i = 0; i < nItemCount; i++ ) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir sal_uInt16 nItemId = m_xPopupMenu->getItemId( i ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir if ( m_xPopupMenu->isItemChecked( nItemId ) ) 209*cdf0e10cSrcweir nChecked = nItemId; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir rtl::OUString aText = m_xPopupMenu->getItemText( nItemId ); 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir // TODO: must be replaced by implementation of VCL, when available 214*cdf0e10cSrcweir sal_Int32 nIndex = aText.indexOf( (sal_Unicode)'~' ); 215*cdf0e10cSrcweir if ( nIndex >= 0 ) 216*cdf0e10cSrcweir aText = aText.replaceAt( nIndex, 1, aEmpty ); 217*cdf0e10cSrcweir // TODO: must be replaced by implementation of VCL, when available 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir if ( aText == m_aFontFamilyName ) 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir m_xPopupMenu->checkItem( nItemId, sal_True ); 222*cdf0e10cSrcweir return; 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir if ( nChecked ) 227*cdf0e10cSrcweir m_xPopupMenu->checkItem( nChecked, sal_False ); 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir // XPopupMenuController 232*cdf0e10cSrcweir void FontMenuController::impl_setPopupMenu() 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY ); 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir com::sun::star::util::URL aTargetURL; 237*cdf0e10cSrcweir // Register for font list updates to get the current font list from the controller 238*cdf0e10cSrcweir aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontNameList" )); 239*cdf0e10cSrcweir m_xURLTransformer->parseStrict( aTargetURL ); 240*cdf0e10cSrcweir m_xFontListDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 ); 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir void SAL_CALL FontMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException ) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir svt::PopupMenuControllerBase::updatePopupMenu(); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir osl::ClearableMutexGuard aLock( m_aMutex ); 248*cdf0e10cSrcweir Reference< XDispatch > xDispatch( m_xFontListDispatch ); 249*cdf0e10cSrcweir com::sun::star::util::URL aTargetURL; 250*cdf0e10cSrcweir aTargetURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:FontNameList" )); 251*cdf0e10cSrcweir m_xURLTransformer->parseStrict( aTargetURL ); 252*cdf0e10cSrcweir aLock.clear(); 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir if ( xDispatch.is() ) 255*cdf0e10cSrcweir { 256*cdf0e10cSrcweir xDispatch->addStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL ); 257*cdf0e10cSrcweir xDispatch->removeStatusListener( SAL_STATIC_CAST( XStatusListener*, this ), aTargetURL ); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263