1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uielement/fontmenucontroller.hxx>
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>
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>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <toolkit/awt/vclxmenu.hxx>
40 using namespace css::uno
;
41 using namespace css::lang
;
42 using namespace css::frame
;
43 using namespace css::beans
;
44 using namespace css::util
;
46 static bool lcl_I18nCompareString(const OUString
& rStr1
, const OUString
& rStr2
)
48 const vcl::I18nHelper
& rI18nHelper
= Application::GetSettings().GetUILocaleI18nHelper();
49 return rI18nHelper
.CompareString( rStr1
, rStr2
) < 0;
55 // XInterface, XTypeProvider, XServiceInfo
57 OUString SAL_CALL
FontMenuController::getImplementationName()
59 return "com.sun.star.comp.framework.FontMenuController";
62 sal_Bool SAL_CALL
FontMenuController::supportsService( const OUString
& sServiceName
)
64 return cppu::supportsService(this, sServiceName
);
67 css::uno::Sequence
< OUString
> SAL_CALL
FontMenuController::getSupportedServiceNames()
69 return { SERVICENAME_POPUPMENUCONTROLLER
};
72 FontMenuController::FontMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
73 svt::PopupMenuControllerBase( xContext
)
77 FontMenuController::~FontMenuController()
82 void FontMenuController::fillPopupMenu( const Sequence
< OUString
>& rFontNameSeq
, Reference
< css::awt::XPopupMenu
> const & rPopupMenu
)
84 SolarMutexGuard aSolarMutexGuard
;
86 resetPopupMenu( rPopupMenu
);
88 std::vector
<OUString
> aVector
;
89 aVector
.reserve(rFontNameSeq
.getLength());
90 for ( OUString
const & s
: rFontNameSeq
)
92 aVector
.push_back(MnemonicGenerator::EraseAllMnemonicChars(s
));
94 sort(aVector
.begin(), aVector
.end(), lcl_I18nCompareString
);
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
++ )
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.
110 void SAL_CALL
FontMenuController::disposing( const EventObject
& )
112 Reference
< css::awt::XMenuListener
> xHolder(this);
114 std::unique_lock
aLock( m_aMutex
);
117 m_xFontListDispatch
.clear();
119 if ( m_xPopupMenu
.is() )
120 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
121 m_xPopupMenu
.clear();
125 void SAL_CALL
FontMenuController::statusChanged( const FeatureStateEvent
& Event
)
127 css::awt::FontDescriptor aFontDescriptor
;
128 Sequence
< OUString
> aFontNameSeq
;
130 if ( Event
.State
>>= aFontDescriptor
)
132 std::unique_lock
aLock( m_aMutex
);
133 m_aFontFamilyName
= aFontDescriptor
.Name
;
135 else if ( Event
.State
>>= aFontNameSeq
)
137 std::unique_lock
aLock( m_aMutex
);
138 if ( m_xPopupMenu
.is() )
139 fillPopupMenu( aFontNameSeq
, m_xPopupMenu
);
144 void SAL_CALL
FontMenuController::itemActivated( const css::awt::MenuEvent
& )
146 std::unique_lock
aLock( m_aMutex
);
148 if ( !m_xPopupMenu
.is() )
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
++ )
156 sal_uInt16 nItemId
= m_xPopupMenu
->getItemId( i
);
158 if ( m_xPopupMenu
->isItemChecked( nItemId
) )
161 OUString aText
= m_xPopupMenu
->getItemText( nItemId
);
163 // TODO: must be replaced by implementation of VCL, when available
164 sal_Int32 nIndex
= aText
.indexOf( '~' );
166 aText
= aText
.replaceAt( nIndex
, 1, u
"" );
167 // TODO: must be replaced by implementation of VCL, when available
169 if ( aText
== m_aFontFamilyName
)
171 m_xPopupMenu
->checkItem( nItemId
, true );
177 m_xPopupMenu
->checkItem( nChecked
, false );
180 // XPopupMenuController
181 void FontMenuController::impl_setPopupMenu()
183 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
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 );
192 void SAL_CALL
FontMenuController::updatePopupMenu()
194 svt::PopupMenuControllerBase::updatePopupMenu();
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
);
203 if ( xDispatch
.is() )
205 xDispatch
->addStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
206 xDispatch
->removeStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
212 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
213 framework_FontMenuController_get_implementation(
214 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
216 return cppu::acquire(new framework::FontMenuController(context
));
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */