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 "services.h"
23 :
24 : #include <com/sun/star/awt/XDevice.hpp>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/awt/MenuItemStyle.hpp>
27 : #include <com/sun/star/frame/XDispatchProvider.hpp>
28 :
29 : #include <vcl/menu.hxx>
30 : #include <vcl/svapp.hxx>
31 : #include <vcl/settings.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 : // Defines
39 :
40 : using namespace com::sun::star::uno;
41 : using namespace com::sun::star::lang;
42 : using namespace com::sun::star::frame;
43 : using namespace com::sun::star::beans;
44 : using namespace com::sun::star::util;
45 :
46 : using namespace std;
47 :
48 0 : static bool lcl_I18nCompareString(const OUString& rStr1, const OUString& rStr2)
49 : {
50 0 : const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
51 0 : return rI18nHelper.CompareString( rStr1, rStr2 ) < 0;
52 : }
53 :
54 : namespace framework
55 : {
56 :
57 98 : DEFINE_XSERVICEINFO_MULTISERVICE_2 ( FontMenuController ,
58 : OWeakObject ,
59 : SERVICENAME_POPUPMENUCONTROLLER ,
60 : IMPLEMENTATIONNAME_FONTMENUCONTROLLER
61 : )
62 :
63 4 : DEFINE_INIT_SERVICE ( FontMenuController, {} )
64 :
65 4 : FontMenuController::FontMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
66 4 : svt::PopupMenuControllerBase( xContext )
67 : {
68 4 : }
69 :
70 8 : FontMenuController::~FontMenuController()
71 : {
72 8 : }
73 :
74 : // private function
75 0 : void FontMenuController::fillPopupMenu( const Sequence< OUString >& rFontNameSeq, Reference< css::awt::XPopupMenu >& rPopupMenu )
76 : {
77 0 : const OUString* pFontNameArray = rFontNameSeq.getConstArray();
78 0 : VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
79 0 : PopupMenu* pVCLPopupMenu = 0;
80 :
81 0 : SolarMutexGuard aSolarMutexGuard;
82 :
83 0 : resetPopupMenu( rPopupMenu );
84 0 : if ( pPopupMenu )
85 0 : pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
86 :
87 0 : if ( pVCLPopupMenu )
88 : {
89 0 : vector<OUString> aVector;
90 0 : aVector.reserve(rFontNameSeq.getLength());
91 0 : for ( sal_uInt16 i = 0; i < rFontNameSeq.getLength(); i++ )
92 : {
93 0 : aVector.push_back(MnemonicGenerator::EraseAllMnemonicChars(pFontNameArray[i]));
94 : }
95 0 : sort(aVector.begin(), aVector.end(), lcl_I18nCompareString );
96 :
97 0 : const OUString aFontNameCommandPrefix( ".uno:CharFontName?CharFontName.FamilyName:string=" );
98 0 : const sal_Int16 nCount = (sal_Int16)aVector.size();
99 0 : for ( sal_Int16 i = 0; i < nCount; i++ )
100 : {
101 0 : const OUString& rName = aVector[i];
102 0 : m_xPopupMenu->insertItem( i+1, rName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, i );
103 0 : if ( rName == m_aFontFamilyName )
104 0 : m_xPopupMenu->checkItem( i+1, sal_True );
105 : // use VCL popup menu pointer to set vital information that are not part of the awt implementation
106 0 : OUStringBuffer aCommandBuffer( aFontNameCommandPrefix );
107 0 : aCommandBuffer.append( INetURLObject::encode( rName, INetURLObject::PART_HTTP_QUERY, '%', INetURLObject::ENCODE_ALL ));
108 0 : OUString aFontNameCommand = aCommandBuffer.makeStringAndClear();
109 0 : pVCLPopupMenu->SetItemCommand( i+1, aFontNameCommand ); // Store font name into item command.
110 0 : }
111 :
112 0 : }
113 0 : }
114 :
115 : // XEventListener
116 0 : void SAL_CALL FontMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
117 : {
118 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
119 :
120 0 : osl::MutexGuard aLock( m_aMutex );
121 0 : m_xFrame.clear();
122 0 : m_xDispatch.clear();
123 0 : m_xFontListDispatch.clear();
124 :
125 0 : if ( m_xPopupMenu.is() )
126 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
127 0 : m_xPopupMenu.clear();
128 0 : }
129 :
130 : // XStatusListener
131 2 : void SAL_CALL FontMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
132 : {
133 2 : com::sun::star::awt::FontDescriptor aFontDescriptor;
134 4 : Sequence< OUString > aFontNameSeq;
135 :
136 2 : if ( Event.State >>= aFontDescriptor )
137 : {
138 0 : osl::MutexGuard aLock( m_aMutex );
139 0 : m_aFontFamilyName = aFontDescriptor.Name;
140 : }
141 2 : else if ( Event.State >>= aFontNameSeq )
142 : {
143 0 : osl::MutexGuard aLock( m_aMutex );
144 0 : if ( m_xPopupMenu.is() )
145 0 : fillPopupMenu( aFontNameSeq, m_xPopupMenu );
146 2 : }
147 2 : }
148 :
149 : // XMenuListener
150 0 : void FontMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
151 : {
152 0 : Sequence<PropertyValue> aArgs;
153 : OSL_ENSURE(_xDispatch.is(),"FontMenuController::impl_select: No dispatch");
154 0 : if ( _xDispatch.is() )
155 0 : _xDispatch->dispatch( aTargetURL, aArgs );
156 0 : }
157 :
158 0 : void SAL_CALL FontMenuController::itemActivated( const css::awt::MenuEvent& ) throw (RuntimeException, std::exception)
159 : {
160 0 : osl::MutexGuard aLock( m_aMutex );
161 :
162 0 : if ( m_xPopupMenu.is() )
163 : {
164 : // find new font name and set check mark!
165 0 : sal_uInt16 nChecked = 0;
166 0 : sal_uInt16 nItemCount = m_xPopupMenu->getItemCount();
167 0 : OUString aEmpty;
168 0 : for( sal_uInt16 i = 0; i < nItemCount; i++ )
169 : {
170 0 : sal_uInt16 nItemId = m_xPopupMenu->getItemId( i );
171 :
172 0 : if ( m_xPopupMenu->isItemChecked( nItemId ) )
173 0 : nChecked = nItemId;
174 :
175 0 : OUString aText = m_xPopupMenu->getItemText( nItemId );
176 :
177 : // TODO: must be replaced by implementation of VCL, when available
178 0 : sal_Int32 nIndex = aText.indexOf( (sal_Unicode)'~' );
179 0 : if ( nIndex >= 0 )
180 0 : aText = aText.replaceAt( nIndex, 1, aEmpty );
181 : // TODO: must be replaced by implementation of VCL, when available
182 :
183 0 : if ( aText == m_aFontFamilyName )
184 : {
185 0 : m_xPopupMenu->checkItem( nItemId, sal_True );
186 0 : return;
187 : }
188 0 : }
189 :
190 0 : if ( nChecked )
191 0 : m_xPopupMenu->checkItem( nChecked, sal_False );
192 0 : }
193 : }
194 :
195 : // XPopupMenuController
196 0 : void FontMenuController::impl_setPopupMenu()
197 : {
198 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
199 :
200 0 : com::sun::star::util::URL aTargetURL;
201 : // Register for font list updates to get the current font list from the controller
202 0 : aTargetURL.Complete = ".uno:FontNameList";
203 0 : m_xURLTransformer->parseStrict( aTargetURL );
204 0 : m_xFontListDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
205 0 : }
206 :
207 2 : void SAL_CALL FontMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
208 : {
209 2 : svt::PopupMenuControllerBase::updatePopupMenu();
210 :
211 2 : osl::ClearableMutexGuard aLock( m_aMutex );
212 4 : Reference< XDispatch > xDispatch( m_xFontListDispatch );
213 4 : com::sun::star::util::URL aTargetURL;
214 2 : aTargetURL.Complete = ".uno:FontNameList";
215 2 : m_xURLTransformer->parseStrict( aTargetURL );
216 2 : aLock.clear();
217 :
218 2 : if ( xDispatch.is() )
219 : {
220 0 : xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
221 0 : xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
222 2 : }
223 2 : }
224 :
225 198 : }
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|