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/XDevice.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/awt/MenuItemStyle.hpp>
27 #include <com/sun/star/frame/XDispatchProvider.hpp>
29 #include <vcl/menu.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/i18nhelp.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <vcl/mnemonic.hxx>
34 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/document/XDocumentLanguages.hpp>
37 #include <com/sun/star/frame/XPopupMenuController.hpp>
38 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
40 #include <i18nlangtag/mslangid.hxx>
41 #include <svl/languageoptions.hxx>
42 #include <svtools/langtab.hxx>
43 #include <classes/fwlresid.hxx>
45 #include <classes/resource.hrc>
47 #include "helper/mischelper.hxx"
48 #include <osl/mutex.hxx>
55 using namespace ::com::sun::star
;
56 using namespace com::sun::star::uno
;
57 using namespace com::sun::star::lang
;
58 using namespace com::sun::star::frame
;
59 using namespace com::sun::star::beans
;
60 using namespace com::sun::star::util
;
65 DEFINE_XSERVICEINFO_MULTISERVICE_2 ( LanguageSelectionMenuController
,
67 SERVICENAME_POPUPMENUCONTROLLER
,
68 IMPLEMENTATIONNAME_LANGUAGESELECTIONMENUCONTROLLER
71 DEFINE_INIT_SERVICE ( LanguageSelectionMenuController
, {} )
73 LanguageSelectionMenuController::LanguageSelectionMenuController( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
)
74 : svt::PopupMenuControllerBase(xContext
)
76 , m_nScriptType(SvtScriptType::LATIN
| SvtScriptType::ASIAN
| SvtScriptType::COMPLEX
)
77 , m_aLangGuessHelper(xContext
)
81 LanguageSelectionMenuController::~LanguageSelectionMenuController()
86 void SAL_CALL
LanguageSelectionMenuController::disposing( const EventObject
& ) throw ( RuntimeException
, std::exception
)
88 Reference
< css::awt::XMenuListener
> xHolder(( OWeakObject
*)this, UNO_QUERY
);
90 osl::MutexGuard
aLock( m_aMutex
);
93 m_xLanguageDispatch
.clear();
95 if ( m_xPopupMenu
.is() )
96 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(( OWeakObject
*)this, UNO_QUERY
));
101 void SAL_CALL
LanguageSelectionMenuController::statusChanged( const FeatureStateEvent
& Event
) throw ( RuntimeException
, std::exception
)
103 SolarMutexGuard aSolarMutexGuard
;
105 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
109 m_nScriptType
= SvtScriptType::LATIN
| SvtScriptType::ASIAN
| SvtScriptType::COMPLEX
; //set the default value
111 Sequence
< OUString
> aSeq
;
113 if ( Event
.State
>>= aSeq
)
115 if ( aSeq
.getLength() == 4 )
117 // Retrieve all other values from the sequence and
119 m_aCurLang
= aSeq
[0];
120 m_nScriptType
= static_cast< SvtScriptType
>(aSeq
[1].toInt32());
121 m_aKeyboardLang
= aSeq
[2];
122 m_aGuessedTextLang
= aSeq
[3];
125 else if ( !Event
.State
.hasValue() )
127 m_bShowMenu
= false; // no language -> no sub-menu entries -> disable menu
132 void LanguageSelectionMenuController::impl_select(const Reference
< XDispatch
>& _xDispatch
,const ::com::sun::star::util::URL
& aTargetURL
)
134 Reference
< XDispatch
> xDispatch
= _xDispatch
;
136 if ( aTargetURL
.Complete
== m_aMenuCommandURL_Font
)
137 { //open format/character dialog for current selection
138 xDispatch
= m_xMenuDispatch_Font
;
140 else if ( aTargetURL
.Complete
== m_aMenuCommandURL_Lang
)
141 { //open language tab-page in tools/options dialog
142 xDispatch
= m_xMenuDispatch_Lang
;
144 else if ( aTargetURL
.Complete
== m_aMenuCommandURL_CharDlgForParagraph
)
145 { //open format/character dialog for current selection
146 xDispatch
= m_xMenuDispatch_CharDlgForParagraph
;
149 if ( !xDispatch
.is() )
151 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
152 if ( xDispatchProvider
.is() )
153 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
156 if ( xDispatch
.is() )
158 Sequence
<PropertyValue
> aArgs
;
159 xDispatch
->dispatch( aTargetURL
, aArgs
);
163 // XPopupMenuController
164 void LanguageSelectionMenuController::impl_setPopupMenu()
166 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
168 com::sun::star::util::URL aTargetURL
;
170 // Register for language updates
171 aTargetURL
.Complete
= m_aLangStatusCommandURL
;
172 m_xURLTransformer
->parseStrict( aTargetURL
);
173 m_xLanguageDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
175 // Register for setting languages and opening language dialog
176 aTargetURL
.Complete
= m_aMenuCommandURL_Lang
;
177 m_xURLTransformer
->parseStrict( aTargetURL
);
178 m_xMenuDispatch_Lang
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
180 // Register for opening character dialog
181 aTargetURL
.Complete
= m_aMenuCommandURL_Font
;
182 m_xURLTransformer
->parseStrict( aTargetURL
);
183 m_xMenuDispatch_Font
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
185 // Register for opening character dialog with preselected paragraph
186 aTargetURL
.Complete
= m_aMenuCommandURL_CharDlgForParagraph
;
187 m_xURLTransformer
->parseStrict( aTargetURL
);
188 m_xMenuDispatch_CharDlgForParagraph
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
191 void LanguageSelectionMenuController::fillPopupMenu( Reference
< css::awt::XPopupMenu
>& rPopupMenu
, const Mode eMode
)
193 VCLXPopupMenu
* pVCLPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( rPopupMenu
));
194 PopupMenu
* pPopupMenu
= 0;
196 SolarMutexGuard aSolarMutexGuard
;
198 resetPopupMenu( rPopupMenu
);
203 pPopupMenu
= static_cast<PopupMenu
*>(pVCLPopupMenu
->GetMenu());
206 OUString aCmd_Dialog
;
207 OUString aCmd_Language
;
208 if( eMode
== MODE_SetLanguageSelectionMenu
)
210 aCmd_Dialog
+= ".uno:FontDialog?Page:string=font";
211 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Current_";
213 else if ( eMode
== MODE_SetLanguageParagraphMenu
)
215 aCmd_Dialog
+= ".uno:FontDialogForParagraph";
216 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Paragraph_";
218 else if ( eMode
== MODE_SetLanguageAllTextMenu
)
220 aCmd_Dialog
+= ".uno:LanguageStatus?Language:string=*";
221 aCmd_Language
+= ".uno:LanguageStatus?Language:string=Default_";
224 // get languages to be displayed in the menu
225 std::set
< OUString
> aLangItems
;
226 FillLangItems( aLangItems
, m_xFrame
, m_aLangGuessHelper
,
227 m_nScriptType
, m_aCurLang
, m_aKeyboardLang
, m_aGuessedTextLang
);
229 // now add menu entries
230 // the different menus purpose will be handled by the different string
231 // for aCmd_Dialog and aCmd_Language
233 sal_Int16 nItemId
= 1; // in this control the item id is not important for executing the command
234 const OUString
sAsterisk("*"); // multiple languages in current selection
235 const OUString sEmpty
; // 'no language found' from language guessing
236 const OUString
sNone( SvtLanguageTable::GetLanguageString( LANGUAGE_NONE
));
237 std::set
< OUString
>::const_iterator it
;
238 for (it
= aLangItems
.begin(); it
!= aLangItems
.end(); ++it
)
240 const OUString
& rStr( *it
);
245 pPopupMenu
->InsertItem( nItemId
, rStr
);
246 aCmd
= aCmd_Language
;
248 pPopupMenu
->SetItemCommand( nItemId
, aCmd
);
249 if (rStr
== m_aCurLang
&& eMode
== MODE_SetLanguageSelectionMenu
)
251 //make a sign for the current language
252 pPopupMenu
->CheckItem( nItemId
, true );
258 // entry for LANGUAGE_NONE
260 pPopupMenu
->InsertItem( nItemId
, FwlResId(STR_LANGSTATUS_NONE
).toString() );
261 aCmd
= aCmd_Language
+ "LANGUAGE_NONE";
262 pPopupMenu
->SetItemCommand( nItemId
, aCmd
);
264 // entry for 'Reset to default language'
266 pPopupMenu
->InsertItem( nItemId
, FwlResId(STR_RESET_TO_DEFAULT_LANGUAGE
).toString() );
267 aCmd
= aCmd_Language
+ "RESET_LANGUAGES";
268 pPopupMenu
->SetItemCommand( nItemId
, aCmd
);
270 // entry for opening the Format/Character dialog
272 pPopupMenu
->InsertItem( nItemId
, FwlResId(STR_LANGSTATUS_MORE
).toString());
273 pPopupMenu
->SetItemCommand( nItemId
, aCmd_Dialog
);
276 void SAL_CALL
LanguageSelectionMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
278 svt::PopupMenuControllerBase::updatePopupMenu();
280 // Force status update to get information about the current languages
281 osl::ClearableMutexGuard
aLock( m_aMutex
);
282 Reference
< XDispatch
> xDispatch( m_xLanguageDispatch
);
283 com::sun::star::util::URL aTargetURL
;
284 aTargetURL
.Complete
= m_aLangStatusCommandURL
;
285 m_xURLTransformer
->parseStrict( aTargetURL
);
288 if ( xDispatch
.is() )
290 xDispatch
->addStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
291 xDispatch
->removeStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
294 // TODO: Fill menu with the information retrieved by the status update
296 if ( m_aCommandURL
== ".uno:SetLanguageSelectionMenu" )
298 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageSelectionMenu
);
300 else if ( m_aCommandURL
== ".uno:SetLanguageParagraphMenu" )
302 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageParagraphMenu
);
304 else if ( m_aCommandURL
== ".uno:SetLanguageAllTextMenu" )
306 fillPopupMenu(m_xPopupMenu
, MODE_SetLanguageAllTextMenu
);
311 void SAL_CALL
LanguageSelectionMenuController::initialize( const Sequence
< Any
>& aArguments
) throw ( Exception
, RuntimeException
, std::exception
)
313 osl::MutexGuard
aLock( m_aMutex
);
315 bool bInitalized( m_bInitialized
);
318 svt::PopupMenuControllerBase::initialize(aArguments
);
320 if ( m_bInitialized
)
322 m_aLangStatusCommandURL
= ".uno:LanguageStatus";
323 m_aMenuCommandURL_Lang
= m_aLangStatusCommandURL
;
324 m_aMenuCommandURL_Font
= ".uno:FontDialog";
325 m_aMenuCommandURL_CharDlgForParagraph
= ".uno:FontDialogForParagraph";
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */