LibreOffice Module framework (master) 1
langselectionmenucontroller.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/util/XURLTransformer.hpp>
27
28#include <vcl/svapp.hxx>
29
31#include <svtools/langtab.hxx>
33#include <classes/fwkresid.hxx>
34
35#include <strings.hrc>
36
37#include <helper/mischelper.hxx>
38#include <osl/mutex.hxx>
40
41#include <map>
42#include <set>
43
44// Defines
45
46using namespace ::com::sun::star;
47using namespace com::sun::star::uno;
48using namespace com::sun::star::lang;
49using namespace com::sun::star::frame;
50using namespace com::sun::star::beans;
51using namespace com::sun::star::util;
52
53namespace framework
54{
55
56// XInterface, XTypeProvider, XServiceInfo
57
59{
60 return "com.sun.star.comp.framework.LanguageSelectionMenuController";
61}
62
63sal_Bool SAL_CALL LanguageSelectionMenuController::supportsService( const OUString& sServiceName )
64{
66}
67
68css::uno::Sequence< OUString > SAL_CALL LanguageSelectionMenuController::getSupportedServiceNames()
69{
71}
72
73
74LanguageSelectionMenuController::LanguageSelectionMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext )
75 : svt::PopupMenuControllerBase(xContext)
76 , m_bShowMenu(true)
78 , m_aLangGuessHelper(xContext)
79{
80}
81
83{
84}
85
86// XEventListener
87void SAL_CALL LanguageSelectionMenuController::disposing( const EventObject& )
88{
90
91 std::unique_lock aLock( m_aMutex );
92 m_xFrame.clear();
93 m_xDispatch.clear();
94 m_xLanguageDispatch.clear();
95
96 if ( m_xPopupMenu.is() )
97 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
98 m_xPopupMenu.clear();
99}
100
101// XStatusListener
102void SAL_CALL LanguageSelectionMenuController::statusChanged( const FeatureStateEvent& Event )
103{
104 SolarMutexGuard aSolarMutexGuard;
105
106 if (m_bDisposed)
107 return;
108
109 m_bShowMenu = true;
110 m_nScriptType = SvtScriptType::LATIN | SvtScriptType::ASIAN | SvtScriptType::COMPLEX; //set the default value
111
113
114 if ( Event.State >>= aSeq )
115 {
116 if ( aSeq.getLength() == 4 )
117 {
118 // Retrieve all other values from the sequence and
119 // store it members!
120 m_aCurLang = aSeq[0];
121 m_nScriptType = static_cast< SvtScriptType >(aSeq[1].toInt32());
124 }
125 }
126 else if ( !Event.State.hasValue() )
127 {
128 m_bShowMenu = false; // no language -> no sub-menu entries -> disable menu
129 }
130}
131
132// XPopupMenuController
134{
135 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
136
137 css::util::URL aTargetURL;
138
139 // Register for language updates
141 m_xURLTransformer->parseStrict( aTargetURL );
142 m_xLanguageDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
143
144 // Register for setting languages and opening language dialog
146 m_xURLTransformer->parseStrict( aTargetURL );
147 m_xMenuDispatch_Lang = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
148
149 // Register for opening character dialog
151 m_xURLTransformer->parseStrict( aTargetURL );
152 m_xMenuDispatch_Font = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
153
154 // Register for opening character dialog with preselected paragraph
156 m_xURLTransformer->parseStrict( aTargetURL );
157 m_xMenuDispatch_CharDlgForParagraph = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
158}
159
161{
162 SolarMutexGuard aSolarMutexGuard;
163
164 resetPopupMenu( rPopupMenu );
165 if (!m_bShowMenu)
166 return;
167
168 OUString aCmd_Dialog;
169 OUString aCmd_Language;
171 {
172 aCmd_Dialog += ".uno:FontDialog?Page:string=font";
173 aCmd_Language += ".uno:LanguageStatus?Language:string=Current_";
174 }
176 {
177 aCmd_Dialog += ".uno:FontDialogForParagraph";
178 aCmd_Language += ".uno:LanguageStatus?Language:string=Paragraph_";
179 }
181 {
182 aCmd_Dialog += ".uno:LanguageStatus?Language:string=*";
183 aCmd_Language += ".uno:LanguageStatus?Language:string=Default_";
184 }
185
186 // get languages to be displayed in the menu
187 std::set< OUString > aLangItems;
190
191 // now add menu entries
192 // the different menus purpose will be handled by the different string
193 // for aCmd_Dialog and aCmd_Language
194 sal_Int16 nItemId = 0; // in this control the item id is not important for executing the command
195 static constexpr OUStringLiteral sAsterisk(u"*"); // multiple languages in current selection
197 for (auto const& langItem : aLangItems)
198 {
199 if (langItem != sNone &&
200 langItem != sAsterisk &&
201 !langItem.isEmpty()) // 'no language found' from language guessing
202 {
203 ++nItemId;
204 rPopupMenu->insertItem(nItemId, langItem, css::awt::MenuItemStyle::CHECKABLE, nItemId - 1);
205 OUString aCmd = aCmd_Language + langItem;
206 rPopupMenu->setCommand(nItemId, aCmd);
207 bool bChecked = langItem == m_aCurLang && eMode == MODE_SetLanguageSelectionMenu;
208 //make a sign for the current language
209 rPopupMenu->checkItem(nItemId, bChecked);
210 }
211 }
212
213 // entry for LANGUAGE_NONE
214 ++nItemId;
215 rPopupMenu->insertItem(nItemId, FwkResId(STR_LANGSTATUS_NONE), 0, nItemId - 1);
216 OUString aCmd = aCmd_Language + "LANGUAGE_NONE";
217 rPopupMenu->setCommand(nItemId, aCmd);
218
219 // entry for 'Reset to default language'
220 ++nItemId;
221 rPopupMenu->insertItem(nItemId, FwkResId(STR_RESET_TO_DEFAULT_LANGUAGE), 0, nItemId - 1);
222 aCmd = aCmd_Language + "RESET_LANGUAGES";
223 rPopupMenu->setCommand(nItemId, aCmd);
224
225 // entry for opening the Format/Character dialog
226 ++nItemId;
227 rPopupMenu->insertItem(nItemId, FwkResId(STR_LANGSTATUS_MORE), 0, nItemId - 1);
228 rPopupMenu->setCommand(nItemId, aCmd_Dialog);
229}
230
232{
233 svt::PopupMenuControllerBase::updatePopupMenu();
234
235 // Force status update to get information about the current languages
236 std::unique_lock aLock( m_aMutex );
238 css::util::URL aTargetURL;
240 m_xURLTransformer->parseStrict( aTargetURL );
241 aLock.unlock();
242
243 if ( xDispatch.is() )
244 {
245 xDispatch->addStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
246 xDispatch->removeStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
247 }
248
249 // TODO: Fill menu with the information retrieved by the status update
250
251 if ( m_aCommandURL == ".uno:SetLanguageSelectionMenu" )
252 {
254 }
255 else if ( m_aCommandURL == ".uno:SetLanguageParagraphMenu" )
256 {
258 }
259 else if ( m_aCommandURL == ".uno:SetLanguageAllTextMenu" )
260 {
262 }
263}
264
265// XInitialization
266void LanguageSelectionMenuController::initializeImpl( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
267{
268 bool bInitialized( m_bInitialized );
269 if ( !bInitialized )
270 {
271 svt::PopupMenuControllerBase::initializeImpl(rGuard, aArguments);
272
273 if ( m_bInitialized )
274 {
275 m_aLangStatusCommandURL = ".uno:LanguageStatus";
277 m_aMenuCommandURL_Font = ".uno:FontDialog";
278 m_aMenuCommandURL_CharDlgForParagraph = ".uno:FontDialogForParagraph";
279 }
280 }
281}
282
283}
284
285extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
287 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
288{
289 return cppu::acquire(new framework::LanguageSelectionMenuController(context));
290}
291
292
293/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define COMPLEX
#define ASIAN
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
static OUString GetLanguageString(const LanguageType eType)
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
css::uno::Reference< css::frame::XDispatch > m_xMenuDispatch_CharDlgForParagraph
LanguageSelectionMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
void fillPopupMenu(css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu, const Mode rMode)
css::uno::Reference< css::frame::XDispatch > m_xLanguageDispatch
css::uno::Reference< css::frame::XDispatch > m_xMenuDispatch_Lang
css::uno::Reference< css::frame::XDispatch > m_xMenuDispatch_Font
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void initializeImpl(std::unique_lock< std::mutex > &rGuard, const css::uno::Sequence< css::uno::Any > &aArguments) override
Reference< XDispatch > xDispatch
float u
OUString FwkResId(TranslateId aId)
Definition: fwkresid.cxx:22
bool m_bDisposed
Sequence< PropertyValue > aArguments
Mode eMode
#define LANGUAGE_NONE
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_LanguageSelectionMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
SvtScriptType
Sequence< sal_Int8 > aSeq
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
void FillLangItems(std::set< OUString > &rLangItems, const uno::Reference< frame::XFrame > &rxFrame, const LanguageGuessingHelper &rLangGuessHelper, SvtScriptType nScriptType, const OUString &rCurLang, const OUString &rKeyboardLang, const OUString &rGuessedTextLang)
Definition: mischelper.cxx:57
unsigned char sal_Bool
std::mutex m_aMutex
OUString aTargetURL
constexpr OUStringLiteral sNone