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::util
;
45 static bool lcl_I18nCompareString(const OUString
& rStr1
, const OUString
& rStr2
)
47 const vcl::I18nHelper
& rI18nHelper
= Application::GetSettings().GetUILocaleI18nHelper();
48 return rI18nHelper
.CompareString( rStr1
, rStr2
) < 0;
54 // XInterface, XTypeProvider, XServiceInfo
56 OUString SAL_CALL
FontMenuController::getImplementationName()
58 return u
"com.sun.star.comp.framework.FontMenuController"_ustr
;
61 sal_Bool SAL_CALL
FontMenuController::supportsService( const OUString
& sServiceName
)
63 return cppu::supportsService(this, sServiceName
);
66 css::uno::Sequence
< OUString
> SAL_CALL
FontMenuController::getSupportedServiceNames()
68 return { SERVICENAME_POPUPMENUCONTROLLER
};
71 FontMenuController::FontMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
72 svt::PopupMenuControllerBase( xContext
)
76 FontMenuController::~FontMenuController()
81 void FontMenuController::fillPopupMenu( const Sequence
< OUString
>& rFontNameSeq
, Reference
< css::awt::XPopupMenu
> const & rPopupMenu
)
83 SolarMutexGuard aSolarMutexGuard
;
85 resetPopupMenu( rPopupMenu
);
87 std::vector
<OUString
> aVector
;
88 aVector
.reserve(rFontNameSeq
.getLength());
89 for ( OUString
const & s
: rFontNameSeq
)
91 aVector
.push_back(MnemonicGenerator::EraseAllMnemonicChars(s
));
93 sort(aVector
.begin(), aVector
.end(), lcl_I18nCompareString
);
95 static constexpr OUStringLiteral
aFontNameCommandPrefix( u
".uno:CharFontName?CharFontName.FamilyName:string=" );
96 const sal_Int16 nCount
= static_cast<sal_Int16
>(aVector
.size());
97 for ( sal_Int16 i
= 0; i
< nCount
; i
++ )
99 const OUString
& rName
= aVector
[i
];
100 m_xPopupMenu
->insertItem( i
+1, rName
, css::awt::MenuItemStyle::RADIOCHECK
| css::awt::MenuItemStyle::AUTOCHECK
, i
);
101 if ( rName
== m_aFontFamilyName
)
102 m_xPopupMenu
->checkItem( i
+1, true );
103 OUString aFontNameCommand
= aFontNameCommandPrefix
+ INetURLObject::encode( rName
, INetURLObject::PART_HTTP_QUERY
, INetURLObject::EncodeMechanism::All
);
104 m_xPopupMenu
->setCommand(i
+ 1, aFontNameCommand
); // Store font name into item command.
109 void SAL_CALL
FontMenuController::disposing( const EventObject
& )
111 Reference
< css::awt::XMenuListener
> xHolder(this);
113 std::unique_lock
aLock( m_aMutex
);
116 m_xFontListDispatch
.clear();
118 if ( m_xPopupMenu
.is() )
119 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
120 m_xPopupMenu
.clear();
124 void SAL_CALL
FontMenuController::statusChanged( const FeatureStateEvent
& Event
)
126 css::awt::FontDescriptor aFontDescriptor
;
127 Sequence
< OUString
> aFontNameSeq
;
129 if ( Event
.State
>>= aFontDescriptor
)
131 std::unique_lock
aLock( m_aMutex
);
132 m_aFontFamilyName
= aFontDescriptor
.Name
;
134 else if ( Event
.State
>>= aFontNameSeq
)
136 std::unique_lock
aLock( m_aMutex
);
137 if ( m_xPopupMenu
.is() )
138 fillPopupMenu( aFontNameSeq
, m_xPopupMenu
);
143 void SAL_CALL
FontMenuController::itemActivated( const css::awt::MenuEvent
& )
145 std::unique_lock
aLock( m_aMutex
);
147 if ( !m_xPopupMenu
.is() )
150 // find new font name and set check mark!
151 sal_uInt16 nChecked
= 0;
152 sal_uInt16 nItemCount
= m_xPopupMenu
->getItemCount();
153 for( sal_uInt16 i
= 0; i
< nItemCount
; i
++ )
155 sal_uInt16 nItemId
= m_xPopupMenu
->getItemId( i
);
157 if ( m_xPopupMenu
->isItemChecked( nItemId
) )
160 OUString aText
= m_xPopupMenu
->getItemText( nItemId
);
162 // TODO: must be replaced by implementation of VCL, when available
163 sal_Int32 nIndex
= aText
.indexOf( '~' );
165 aText
= aText
.replaceAt( nIndex
, 1, u
"" );
166 // TODO: must be replaced by implementation of VCL, when available
168 if ( aText
== m_aFontFamilyName
)
170 m_xPopupMenu
->checkItem( nItemId
, true );
176 m_xPopupMenu
->checkItem( nChecked
, false );
179 // XPopupMenuController
180 void FontMenuController::impl_setPopupMenu(std::unique_lock
<std::mutex
>& /*rGuard*/)
182 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
184 css::util::URL aTargetURL
;
185 // Register for font list updates to get the current font list from the controller
186 aTargetURL
.Complete
= ".uno:FontNameList";
187 m_xURLTransformer
->parseStrict( aTargetURL
);
188 m_xFontListDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
191 void SAL_CALL
FontMenuController::updatePopupMenu()
193 svt::PopupMenuControllerBase::updatePopupMenu();
195 std::unique_lock
aLock( m_aMutex
);
196 Reference
< XDispatch
> xDispatch( m_xFontListDispatch
);
197 css::util::URL aTargetURL
;
198 aTargetURL
.Complete
= ".uno:FontNameList";
199 m_xURLTransformer
->parseStrict( aTargetURL
);
202 if ( xDispatch
.is() )
204 xDispatch
->addStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
205 xDispatch
->removeStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
211 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
212 framework_FontMenuController_get_implementation(
213 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
215 return cppu::acquire(new framework::FontMenuController(context
));
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */