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