Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <uielement/fontmenucontroller.hxx>
21 :
22 : #include <threadhelp/resetableguard.hxx>
23 : #include "services.h"
24 :
25 : #include <com/sun/star/awt/XDevice.hpp>
26 : #include <com/sun/star/beans/PropertyValue.hpp>
27 : #include <com/sun/star/awt/MenuItemStyle.hpp>
28 : #include <com/sun/star/frame/XDispatchProvider.hpp>
29 :
30 : #include <vcl/menu.hxx>
31 : #include <vcl/svapp.hxx>
32 : #include <vcl/i18nhelp.hxx>
33 : #include <tools/urlobj.hxx>
34 : #include <rtl/ustrbuf.hxx>
35 : #include <vcl/mnemonic.hxx>
36 : #include <osl/mutex.hxx>
37 :
38 : //_________________________________________________________________________________________________________________
39 : // Defines
40 : //_________________________________________________________________________________________________________________
41 :
42 : using namespace com::sun::star::uno;
43 : using namespace com::sun::star::lang;
44 : using namespace com::sun::star::frame;
45 : using namespace com::sun::star::beans;
46 : using namespace com::sun::star::util;
47 :
48 : using namespace std;
49 :
50 0 : static bool lcl_I18nCompareString(const rtl::OUString& rStr1, const rtl::OUString& rStr2)
51 : {
52 0 : const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
53 0 : return rI18nHelper.CompareString( rStr1, rStr2 ) < 0 ? true : false;
54 : }
55 :
56 : namespace framework
57 : {
58 :
59 0 : DEFINE_XSERVICEINFO_MULTISERVICE ( FontMenuController ,
60 : OWeakObject ,
61 : SERVICENAME_POPUPMENUCONTROLLER ,
62 : IMPLEMENTATIONNAME_FONTMENUCONTROLLER
63 : )
64 :
65 0 : DEFINE_INIT_SERVICE ( FontMenuController, {} )
66 :
67 0 : FontMenuController::FontMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
68 0 : svt::PopupMenuControllerBase( xServiceManager )
69 : {
70 0 : }
71 :
72 0 : FontMenuController::~FontMenuController()
73 : {
74 0 : }
75 :
76 : // private function
77 0 : void FontMenuController::fillPopupMenu( const Sequence< ::rtl::OUString >& rFontNameSeq, Reference< css::awt::XPopupMenu >& rPopupMenu )
78 : {
79 0 : const rtl::OUString* pFontNameArray = rFontNameSeq.getConstArray();
80 0 : VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
81 0 : PopupMenu* pVCLPopupMenu = 0;
82 :
83 0 : SolarMutexGuard aSolarMutexGuard;
84 :
85 0 : resetPopupMenu( rPopupMenu );
86 0 : if ( pPopupMenu )
87 0 : pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
88 :
89 0 : if ( pVCLPopupMenu )
90 : {
91 0 : vector<rtl::OUString> aVector;
92 0 : aVector.reserve(rFontNameSeq.getLength());
93 0 : for ( sal_uInt16 i = 0; i < rFontNameSeq.getLength(); i++ )
94 : {
95 0 : aVector.push_back(MnemonicGenerator::EraseAllMnemonicChars(pFontNameArray[i]));
96 : }
97 0 : sort(aVector.begin(), aVector.end(), lcl_I18nCompareString );
98 :
99 0 : const rtl::OUString aFontNameCommandPrefix( ".uno:CharFontName?CharFontName.FamilyName:string=" );
100 0 : const sal_Int16 nCount = (sal_Int16)aVector.size();
101 0 : for ( sal_Int16 i = 0; i < nCount; i++ )
102 : {
103 0 : const rtl::OUString& rName = aVector[i];
104 0 : m_xPopupMenu->insertItem( i+1, rName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, i );
105 0 : if ( rName == m_aFontFamilyName )
106 0 : m_xPopupMenu->checkItem( i+1, sal_True );
107 : // use VCL popup menu pointer to set vital information that are not part of the awt implementation
108 0 : rtl::OUStringBuffer aCommandBuffer( aFontNameCommandPrefix );
109 0 : aCommandBuffer.append( INetURLObject::encode( rName, INetURLObject::PART_HTTP_QUERY, '%', INetURLObject::ENCODE_ALL ));
110 0 : rtl::OUString aFontNameCommand = aCommandBuffer.makeStringAndClear();
111 0 : pVCLPopupMenu->SetItemCommand( i+1, aFontNameCommand ); // Store font name into item command.
112 0 : }
113 :
114 0 : }
115 0 : }
116 :
117 : // XEventListener
118 0 : void SAL_CALL FontMenuController::disposing( const EventObject& ) throw ( RuntimeException )
119 : {
120 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
121 :
122 0 : osl::MutexGuard aLock( m_aMutex );
123 0 : m_xFrame.clear();
124 0 : m_xDispatch.clear();
125 0 : m_xFontListDispatch.clear();
126 0 : m_xServiceManager.clear();
127 :
128 0 : if ( m_xPopupMenu.is() )
129 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
130 0 : m_xPopupMenu.clear();
131 0 : }
132 :
133 : // XStatusListener
134 0 : void SAL_CALL FontMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
135 : {
136 0 : com::sun::star::awt::FontDescriptor aFontDescriptor;
137 0 : Sequence< rtl::OUString > aFontNameSeq;
138 :
139 0 : if ( Event.State >>= aFontDescriptor )
140 : {
141 0 : osl::MutexGuard aLock( m_aMutex );
142 0 : m_aFontFamilyName = aFontDescriptor.Name;
143 : }
144 0 : else if ( Event.State >>= aFontNameSeq )
145 : {
146 0 : osl::MutexGuard aLock( m_aMutex );
147 0 : if ( m_xPopupMenu.is() )
148 0 : fillPopupMenu( aFontNameSeq, m_xPopupMenu );
149 0 : }
150 0 : }
151 :
152 : // XMenuListener
153 0 : void FontMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
154 : {
155 0 : Sequence<PropertyValue> aArgs;
156 : OSL_ENSURE(_xDispatch.is(),"FontMenuController::impl_select: No dispatch");
157 0 : if ( _xDispatch.is() )
158 0 : _xDispatch->dispatch( aTargetURL, aArgs );
159 0 : }
160 :
161 0 : void SAL_CALL FontMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
162 : {
163 0 : osl::MutexGuard aLock( m_aMutex );
164 :
165 0 : if ( m_xPopupMenu.is() )
166 : {
167 : // find new font name and set check mark!
168 0 : sal_uInt16 nChecked = 0;
169 0 : sal_uInt16 nItemCount = m_xPopupMenu->getItemCount();
170 0 : rtl::OUString aEmpty;
171 0 : for( sal_uInt16 i = 0; i < nItemCount; i++ )
172 : {
173 0 : sal_uInt16 nItemId = m_xPopupMenu->getItemId( i );
174 :
175 0 : if ( m_xPopupMenu->isItemChecked( nItemId ) )
176 0 : nChecked = nItemId;
177 :
178 0 : rtl::OUString aText = m_xPopupMenu->getItemText( nItemId );
179 :
180 : // TODO: must be replaced by implementation of VCL, when available
181 0 : sal_Int32 nIndex = aText.indexOf( (sal_Unicode)'~' );
182 0 : if ( nIndex >= 0 )
183 0 : aText = aText.replaceAt( nIndex, 1, aEmpty );
184 : // TODO: must be replaced by implementation of VCL, when available
185 :
186 0 : if ( aText == m_aFontFamilyName )
187 : {
188 0 : m_xPopupMenu->checkItem( nItemId, sal_True );
189 0 : return;
190 : }
191 0 : }
192 :
193 0 : if ( nChecked )
194 0 : m_xPopupMenu->checkItem( nChecked, sal_False );
195 0 : }
196 : }
197 :
198 : // XPopupMenuController
199 0 : void FontMenuController::impl_setPopupMenu()
200 : {
201 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
202 :
203 0 : com::sun::star::util::URL aTargetURL;
204 : // Register for font list updates to get the current font list from the controller
205 0 : aTargetURL.Complete = rtl::OUString( ".uno:FontNameList" );
206 0 : m_xURLTransformer->parseStrict( aTargetURL );
207 0 : m_xFontListDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
208 0 : }
209 :
210 0 : void SAL_CALL FontMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException )
211 : {
212 0 : svt::PopupMenuControllerBase::updatePopupMenu();
213 :
214 0 : osl::ClearableMutexGuard aLock( m_aMutex );
215 0 : Reference< XDispatch > xDispatch( m_xFontListDispatch );
216 0 : com::sun::star::util::URL aTargetURL;
217 0 : aTargetURL.Complete = rtl::OUString( ".uno:FontNameList" );
218 0 : m_xURLTransformer->parseStrict( aTargetURL );
219 0 : aLock.clear();
220 :
221 0 : if ( xDispatch.is() )
222 : {
223 0 : xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
224 0 : xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
225 0 : }
226 0 : }
227 :
228 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|