LibreOffice Module framework (master) 1
fontmenucontroller.cxx
Go to the documentation of this file.
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
21
22#include <services.h>
23
24#include <com/sun/star/awt/MenuItemStyle.hpp>
25#include <com/sun/star/frame/XDispatchProvider.hpp>
26#include <com/sun/star/frame/XFrame.hpp>
27#include <com/sun/star/util/XURLTransformer.hpp>
28
29#include <vcl/svapp.hxx>
30#include <vcl/settings.hxx>
31#include <vcl/i18nhelp.hxx>
32#include <tools/urlobj.hxx>
33#include <vcl/mnemonic.hxx>
34#include <osl/mutex.hxx>
37
38// Defines
39
40using namespace css::uno;
41using namespace css::lang;
42using namespace css::frame;
43using namespace css::beans;
44using namespace css::util;
45
46static bool lcl_I18nCompareString(const OUString& rStr1, const OUString& rStr2)
47{
49 return rI18nHelper.CompareString( rStr1, rStr2 ) < 0;
50}
51
52namespace framework
53{
54
55// XInterface, XTypeProvider, XServiceInfo
56
58{
59 return "com.sun.star.comp.framework.FontMenuController";
60}
61
62sal_Bool SAL_CALL FontMenuController::supportsService( const OUString& sServiceName )
63{
65}
66
67css::uno::Sequence< OUString > SAL_CALL FontMenuController::getSupportedServiceNames()
68{
70}
71
72FontMenuController::FontMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
73 svt::PopupMenuControllerBase( xContext )
74{
75}
76
78{
79}
80
81// private function
82void FontMenuController::fillPopupMenu( const Sequence< OUString >& rFontNameSeq, Reference< css::awt::XPopupMenu > const & rPopupMenu )
83{
84 SolarMutexGuard aSolarMutexGuard;
85
86 resetPopupMenu( rPopupMenu );
87
88 std::vector<OUString> aVector;
89 aVector.reserve(rFontNameSeq.getLength());
90 for ( OUString const & s : rFontNameSeq )
91 {
92 aVector.push_back(MnemonicGenerator::EraseAllMnemonicChars(s));
93 }
94 sort(aVector.begin(), aVector.end(), lcl_I18nCompareString );
95
96 static constexpr OUStringLiteral aFontNameCommandPrefix( u".uno:CharFontName?CharFontName.FamilyName:string=" );
97 const sal_Int16 nCount = static_cast<sal_Int16>(aVector.size());
98 for ( sal_Int16 i = 0; i < nCount; i++ )
99 {
100 const OUString& rName = aVector[i];
101 m_xPopupMenu->insertItem( i+1, rName, css::awt::MenuItemStyle::RADIOCHECK | css::awt::MenuItemStyle::AUTOCHECK, i );
102 if ( rName == m_aFontFamilyName )
103 m_xPopupMenu->checkItem( i+1, true );
104 OUString aFontNameCommand = aFontNameCommandPrefix + INetURLObject::encode( rName, INetURLObject::PART_HTTP_QUERY, INetURLObject::EncodeMechanism::All );
105 m_xPopupMenu->setCommand(i + 1, aFontNameCommand); // Store font name into item command.
106 }
107}
108
109// XEventListener
110void SAL_CALL FontMenuController::disposing( const EventObject& )
111{
112 Reference< css::awt::XMenuListener > xHolder(this);
113
114 std::unique_lock aLock( m_aMutex );
115 m_xFrame.clear();
116 m_xDispatch.clear();
117 m_xFontListDispatch.clear();
118
119 if ( m_xPopupMenu.is() )
120 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
121 m_xPopupMenu.clear();
122}
123
124// XStatusListener
125void SAL_CALL FontMenuController::statusChanged( const FeatureStateEvent& Event )
126{
127 css::awt::FontDescriptor aFontDescriptor;
128 Sequence< OUString > aFontNameSeq;
129
130 if ( Event.State >>= aFontDescriptor )
131 {
132 std::unique_lock aLock( m_aMutex );
133 m_aFontFamilyName = aFontDescriptor.Name;
134 }
135 else if ( Event.State >>= aFontNameSeq )
136 {
137 std::unique_lock aLock( m_aMutex );
138 if ( m_xPopupMenu.is() )
139 fillPopupMenu( aFontNameSeq, m_xPopupMenu );
140 }
141}
142
143// XMenuListener
144void SAL_CALL FontMenuController::itemActivated( const css::awt::MenuEvent& )
145{
146 std::unique_lock aLock( m_aMutex );
147
148 if ( !m_xPopupMenu.is() )
149 return;
150
151 // find new font name and set check mark!
152 sal_uInt16 nChecked = 0;
153 sal_uInt16 nItemCount = m_xPopupMenu->getItemCount();
154 for( sal_uInt16 i = 0; i < nItemCount; i++ )
155 {
156 sal_uInt16 nItemId = m_xPopupMenu->getItemId( i );
157
158 if ( m_xPopupMenu->isItemChecked( nItemId ) )
159 nChecked = nItemId;
160
161 OUString aText = m_xPopupMenu->getItemText( nItemId );
162
163 // TODO: must be replaced by implementation of VCL, when available
164 sal_Int32 nIndex = aText.indexOf( '~' );
165 if ( nIndex >= 0 )
166 aText = aText.replaceAt( nIndex, 1, u"" );
167 // TODO: must be replaced by implementation of VCL, when available
168
169 if ( aText == m_aFontFamilyName )
170 {
171 m_xPopupMenu->checkItem( nItemId, true );
172 return;
173 }
174 }
175
176 if ( nChecked )
177 m_xPopupMenu->checkItem( nChecked, false );
178}
179
180// XPopupMenuController
182{
183 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
184
185 css::util::URL aTargetURL;
186 // Register for font list updates to get the current font list from the controller
187 aTargetURL.Complete = ".uno:FontNameList";
188 m_xURLTransformer->parseStrict( aTargetURL );
189 m_xFontListDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
190}
191
193{
194 svt::PopupMenuControllerBase::updatePopupMenu();
195
196 std::unique_lock aLock( m_aMutex );
197 Reference< XDispatch > xDispatch( m_xFontListDispatch );
198 css::util::URL aTargetURL;
199 aTargetURL.Complete = ".uno:FontNameList";
200 m_xURLTransformer->parseStrict( aTargetURL );
201 aLock.unlock();
202
203 if ( xDispatch.is() )
204 {
205 xDispatch->addStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
206 xDispatch->removeStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
207 }
208}
209
210}
211
212extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
214 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
215{
216 return cppu::acquire(new framework::FontMenuController(context));
217}
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
const vcl::I18nHelper & GetUILocaleI18nHelper() const
static const AllSettings & GetSettings()
static OUString encode(std::u16string_view rText, Part ePart, EncodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
static OUString EraseAllMnemonicChars(const OUString &rStr)
virtual void SAL_CALL itemActivated(const css::awt::MenuEvent &rEvent) override
virtual void SAL_CALL updatePopupMenu() override
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::uno::Reference< css::frame::XDispatch > m_xFontListDispatch
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
FontMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
void fillPopupMenu(const css::uno::Sequence< OUString > &rFontNameSeq, css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
virtual ~FontMenuController() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
virtual void impl_setPopupMenu() override
sal_Int32 CompareString(const OUString &rStr1, const OUString &rStr2) const
int nCount
Reference< XDispatch > xDispatch
float u
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_FontMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static bool lcl_I18nCompareString(const OUString &rStr1, const OUString &rStr2)
sal_Int32 nIndex
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
int i
unsigned char sal_Bool
std::mutex m_aMutex
OUString aTargetURL