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