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/langselectionmenucontroller.hxx>
24 #include <com/sun/star/awt/MenuItemStyle.hpp>
25 #include <com/sun/star/frame/XDispatchProvider.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
28 #include <vcl/svapp.hxx>
30 #include <svl/languageoptions.hxx>
31 #include <svtools/langtab.hxx>
32 #include <toolkit/awt/vclxmenu.hxx>
33 #include <classes/fwkresid.hxx>
35 #include <strings.hrc>
37 #include <helper/mischelper.hxx>
38 #include <osl/mutex.hxx>
39 #include <cppuhelper/supportsservice.hxx>
46 using namespace ::com::sun::star
;
47 using namespace com::sun::star::uno
;
48 using namespace com::sun::star::lang
;
49 using namespace com::sun::star::frame
;
50 using namespace com::sun::star::beans
;
51 using namespace com::sun::star::util
;
56 // XInterface, XTypeProvider, XServiceInfo
58 OUString SAL_CALL
LanguageSelectionMenuController::getImplementationName()
60 return "com.sun.star.comp.framework.LanguageSelectionMenuController";
63 sal_Bool SAL_CALL
LanguageSelectionMenuController::supportsService( const OUString
& sServiceName
)
65 return cppu::supportsService(this, sServiceName
);
68 css::uno::Sequence
< OUString
> SAL_CALL
LanguageSelectionMenuController::getSupportedServiceNames()
70 return { SERVICENAME_POPUPMENUCONTROLLER
};
74 LanguageSelectionMenuController::LanguageSelectionMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
75 : svt::PopupMenuControllerBase(xContext
)
77 , m_nScriptType(SvtScriptType::LATIN
| SvtScriptType::ASIAN
| SvtScriptType::COMPLEX
)
78 , m_aLangGuessHelper(xContext
)
82 LanguageSelectionMenuController::~LanguageSelectionMenuController()
87 void SAL_CALL
LanguageSelectionMenuController::disposing( const EventObject
& )
89 Reference
< css::awt::XMenuListener
> xHolder(this);
91 std::unique_lock
aLock( m_aMutex
);
94 m_xLanguageDispatch
.clear();
96 if ( m_xPopupMenu
.is() )
97 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
102 void SAL_CALL
LanguageSelectionMenuController::statusChanged( const FeatureStateEvent
& Event
)
104 SolarMutexGuard aSolarMutexGuard
;
110 m_nScriptType
= SvtScriptType::LATIN
| SvtScriptType::ASIAN
| SvtScriptType::COMPLEX
; //set the default value
112 Sequence
< OUString
> aSeq
;
114 if ( Event
.State
>>= aSeq
)
116 if ( aSeq
.getLength() == 4 )
118 // Retrieve all other values from the sequence and
120 m_aCurLang
= aSeq
[0];
121 m_nScriptType
= static_cast< SvtScriptType
>(aSeq
[1].toInt32());
122 m_aKeyboardLang
= aSeq
[2];
123 m_aGuessedTextLang
= aSeq
[3];
126 else if ( !Event
.State
.hasValue() )
128 m_bShowMenu
= false; // no language -> no sub-menu entries -> disable menu
132 // XPopupMenuController
133 void LanguageSelectionMenuController::impl_setPopupMenu()
135 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
137 css::util::URL aTargetURL
;
139 // Register for language updates
140 aTargetURL
.Complete
= m_aLangStatusCommandURL
;
141 m_xURLTransformer
->parseStrict( aTargetURL
);
142 m_xLanguageDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
144 // Register for setting languages and opening language dialog
145 aTargetURL
.Complete
= m_aMenuCommandURL_Lang
;
146 m_xURLTransformer
->parseStrict( aTargetURL
);
147 m_xMenuDispatch_Lang
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
149 // Register for opening character dialog
150 aTargetURL
.Complete
= m_aMenuCommandURL_Font
;
151 m_xURLTransformer
->parseStrict( aTargetURL
);
152 m_xMenuDispatch_Font
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
154 // Register for opening character dialog with preselected paragraph
155 aTargetURL
.Complete
= m_aMenuCommandURL_CharDlgForParagraph
;
156 m_xURLTransformer
->parseStrict( aTargetURL
);
157 m_xMenuDispatch_CharDlgForParagraph
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
160 void LanguageSelectionMenuController::fillPopupMenu( Reference
< css::awt::XPopupMenu
> const & rPopupMenu
, const Mode eMode
)
162 SolarMutexGuard aSolarMutexGuard
;
164 resetPopupMenu( rPopupMenu
);
168 OUString aCmd_Dialog
;
169 OUString aCmd_Language
;
170 if( eMode
== MODE_SetLanguageSelectionMenu
)
172 aCmd_Dialog
+= ".uno:FontDialog?Page:string=font";
173 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Current_";
175 else if ( eMode
== MODE_SetLanguageParagraphMenu
)
177 aCmd_Dialog
+= ".uno:FontDialogForParagraph";
178 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Paragraph_";
180 else if ( eMode
== MODE_SetLanguageAllTextMenu
)
182 aCmd_Dialog
+= ".uno:LanguageStatus?Language:string=*";
183 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Default_";
186 // get languages to be displayed in the menu
187 std::set
< OUString
> aLangItems
;
188 FillLangItems( aLangItems
, m_xFrame
, m_aLangGuessHelper
,
189 m_nScriptType
, m_aCurLang
, m_aKeyboardLang
, m_aGuessedTextLang
);
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 const OUStringLiteral
sAsterisk(u
"*"); // multiple languages in current selection
196 const OUString
sNone( SvtLanguageTable::GetLanguageString( LANGUAGE_NONE
));
197 for (auto const& langItem
: aLangItems
)
199 if (langItem
!= sNone
&&
200 langItem
!= sAsterisk
&&
201 !langItem
.isEmpty()) // 'no language found' from language guessing
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
);
213 // entry for LANGUAGE_NONE
215 rPopupMenu
->insertItem(nItemId
, FwkResId(STR_LANGSTATUS_NONE
), 0, nItemId
- 1);
216 OUString aCmd
= aCmd_Language
+ "LANGUAGE_NONE";
217 rPopupMenu
->setCommand(nItemId
, aCmd
);
219 // entry for 'Reset to default language'
221 rPopupMenu
->insertItem(nItemId
, FwkResId(STR_RESET_TO_DEFAULT_LANGUAGE
), 0, nItemId
- 1);
222 aCmd
= aCmd_Language
+ "RESET_LANGUAGES";
223 rPopupMenu
->setCommand(nItemId
, aCmd
);
225 // entry for opening the Format/Character dialog
227 rPopupMenu
->insertItem(nItemId
, FwkResId(STR_LANGSTATUS_MORE
), 0, nItemId
- 1);
228 rPopupMenu
->setCommand(nItemId
, aCmd_Dialog
);
231 void SAL_CALL
LanguageSelectionMenuController::updatePopupMenu()
233 svt::PopupMenuControllerBase::updatePopupMenu();
235 // Force status update to get information about the current languages
236 std::unique_lock
aLock( m_aMutex
);
237 Reference
< XDispatch
> xDispatch( m_xLanguageDispatch
);
238 css::util::URL aTargetURL
;
239 aTargetURL
.Complete
= m_aLangStatusCommandURL
;
240 m_xURLTransformer
->parseStrict( aTargetURL
);
243 if ( xDispatch
.is() )
245 xDispatch
->addStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
246 xDispatch
->removeStatusListener( static_cast< XStatusListener
* >(this), aTargetURL
);
249 // TODO: Fill menu with the information retrieved by the status update
251 if ( m_aCommandURL
== ".uno:SetLanguageSelectionMenu" )
253 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageSelectionMenu
);
255 else if ( m_aCommandURL
== ".uno:SetLanguageParagraphMenu" )
257 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageParagraphMenu
);
259 else if ( m_aCommandURL
== ".uno:SetLanguageAllTextMenu" )
261 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageAllTextMenu
);
266 void LanguageSelectionMenuController::initializeImpl( std::unique_lock
<std::mutex
>& rGuard
, const Sequence
< Any
>& aArguments
)
268 bool bInitialized( m_bInitialized
);
271 svt::PopupMenuControllerBase::initializeImpl(rGuard
, aArguments
);
273 if ( m_bInitialized
)
275 m_aLangStatusCommandURL
= ".uno:LanguageStatus";
276 m_aMenuCommandURL_Lang
= m_aLangStatusCommandURL
;
277 m_aMenuCommandURL_Font
= ".uno:FontDialog";
278 m_aMenuCommandURL_CharDlgForParagraph
= ".uno:FontDialogForParagraph";
285 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
286 framework_LanguageSelectionMenuController_get_implementation(
287 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
289 return cppu::acquire(new framework::LanguageSelectionMenuController(context
));
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */