Line data Source code
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 :
20 : #include <uielement/langselectionmenucontroller.hxx>
21 :
22 : #include "services.h"
23 :
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>
28 :
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>
35 :
36 : #include <com/sun/star/document/XDocumentLanguages.hpp>
37 : #include <com/sun/star/frame/XPopupMenuController.hpp>
38 : #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
39 :
40 : #include <i18nlangtag/mslangid.hxx>
41 : #include <svl/languageoptions.hxx>
42 : #include <svtools/langtab.hxx>
43 : #include <classes/fwlresid.hxx>
44 :
45 : #include <classes/resource.hrc>
46 :
47 : #include "helper/mischelper.hxx"
48 : #include <osl/mutex.hxx>
49 :
50 : #include <map>
51 : #include <set>
52 :
53 : // Defines
54 :
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;
61 :
62 : namespace framework
63 : {
64 :
65 0 : DEFINE_XSERVICEINFO_MULTISERVICE_2 ( LanguageSelectionMenuController ,
66 : OWeakObject ,
67 : SERVICENAME_POPUPMENUCONTROLLER ,
68 : IMPLEMENTATIONNAME_LANGUAGESELECTIONMENUCONTROLLER
69 : )
70 :
71 0 : DEFINE_INIT_SERVICE ( LanguageSelectionMenuController, {} )
72 :
73 0 : LanguageSelectionMenuController::LanguageSelectionMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext )
74 : : svt::PopupMenuControllerBase(xContext)
75 : , m_bShowMenu(true)
76 : , m_nScriptType(LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX)
77 0 : , m_aLangGuessHelper(xContext)
78 : {
79 0 : }
80 :
81 0 : LanguageSelectionMenuController::~LanguageSelectionMenuController()
82 : {
83 0 : }
84 :
85 : // XEventListener
86 0 : void SAL_CALL LanguageSelectionMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
87 : {
88 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
89 :
90 0 : osl::MutexGuard aLock( m_aMutex );
91 0 : m_xFrame.clear();
92 0 : m_xDispatch.clear();
93 0 : m_xLanguageDispatch.clear();
94 :
95 0 : if ( m_xPopupMenu.is() )
96 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
97 0 : m_xPopupMenu.clear();
98 0 : }
99 :
100 : // XStatusListener
101 0 : void SAL_CALL LanguageSelectionMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
102 : {
103 0 : SolarMutexGuard aSolarMutexGuard;
104 :
105 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
106 0 : return;
107 :
108 0 : m_bShowMenu = true;
109 0 : m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX; //set the default value
110 :
111 0 : Sequence< OUString > aSeq;
112 :
113 0 : if ( Event.State >>= aSeq )
114 : {
115 0 : if ( aSeq.getLength() == 4 )
116 : {
117 : // Retrieve all other values from the sequence and
118 : // store it members!
119 0 : m_aCurLang = aSeq[0];
120 0 : m_nScriptType = static_cast< sal_Int16 >(aSeq[1].toInt32());
121 0 : m_aKeyboardLang = aSeq[2];
122 0 : m_aGuessedTextLang = aSeq[3];
123 : }
124 : }
125 0 : else if ( !Event.State.hasValue() )
126 : {
127 0 : m_bShowMenu = false; // no language -> no sub-menu entries -> disable menu
128 0 : }
129 : }
130 :
131 : // XMenuListener
132 0 : void LanguageSelectionMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
133 : {
134 0 : Reference< XDispatch > xDispatch = _xDispatch;
135 :
136 0 : if ( aTargetURL.Complete == m_aMenuCommandURL_Font )
137 : { //open format/character dialog for current selection
138 0 : xDispatch = m_xMenuDispatch_Font;
139 : }
140 0 : else if ( aTargetURL.Complete == m_aMenuCommandURL_Lang )
141 : { //open language tab-page in tools/options dialog
142 0 : xDispatch = m_xMenuDispatch_Lang;
143 : }
144 0 : else if ( aTargetURL.Complete == m_aMenuCommandURL_CharDlgForParagraph )
145 : { //open format/character dialog for current selection
146 0 : xDispatch = m_xMenuDispatch_CharDlgForParagraph;
147 : }
148 :
149 0 : if ( !xDispatch.is() )
150 : {
151 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
152 0 : if ( xDispatchProvider.is() )
153 0 : xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
154 : }
155 :
156 0 : if ( xDispatch.is() )
157 : {
158 0 : Sequence<PropertyValue> aArgs;
159 0 : xDispatch->dispatch( aTargetURL, aArgs );
160 0 : }
161 0 : }
162 :
163 : // XPopupMenuController
164 0 : void LanguageSelectionMenuController::impl_setPopupMenu()
165 : {
166 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
167 :
168 0 : com::sun::star::util::URL aTargetURL;
169 :
170 : // Register for language updates
171 0 : aTargetURL.Complete = m_aLangStatusCommandURL;
172 0 : m_xURLTransformer->parseStrict( aTargetURL );
173 0 : m_xLanguageDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
174 :
175 : // Register for setting languages and opening language dialog
176 0 : aTargetURL.Complete = m_aMenuCommandURL_Lang;
177 0 : m_xURLTransformer->parseStrict( aTargetURL );
178 0 : m_xMenuDispatch_Lang = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
179 :
180 : // Register for opening character dialog
181 0 : aTargetURL.Complete = m_aMenuCommandURL_Font;
182 0 : m_xURLTransformer->parseStrict( aTargetURL );
183 0 : m_xMenuDispatch_Font = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
184 :
185 : // Register for opening character dialog with preselected paragraph
186 0 : aTargetURL.Complete = m_aMenuCommandURL_CharDlgForParagraph;
187 0 : m_xURLTransformer->parseStrict( aTargetURL );
188 0 : m_xMenuDispatch_CharDlgForParagraph = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
189 0 : }
190 :
191 0 : void LanguageSelectionMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu , const Mode eMode )
192 : {
193 0 : VCLXPopupMenu* pVCLPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
194 0 : PopupMenu* pPopupMenu = 0;
195 :
196 0 : SolarMutexGuard aSolarMutexGuard;
197 :
198 0 : resetPopupMenu( rPopupMenu );
199 0 : if (!m_bShowMenu)
200 0 : return;
201 :
202 0 : if ( pVCLPopupMenu )
203 0 : pPopupMenu = (PopupMenu *)pVCLPopupMenu->GetMenu();
204 :
205 0 : OUString aCmd;
206 0 : OUString aCmd_Dialog;
207 0 : OUString aCmd_Language;
208 0 : if( eMode == MODE_SetLanguageSelectionMenu )
209 : {
210 0 : aCmd_Dialog += ".uno:FontDialog?Language:string=*";
211 0 : aCmd_Language += ".uno:LanguageStatus?Language:string=Current_";
212 : }
213 0 : else if ( eMode == MODE_SetLanguageParagraphMenu )
214 : {
215 0 : aCmd_Dialog += ".uno:FontDialogForParagraph";
216 0 : aCmd_Language += ".uno:LanguageStatus?Language:string=Paragraph_";
217 : }
218 0 : else if ( eMode == MODE_SetLanguageAllTextMenu )
219 : {
220 0 : aCmd_Dialog += ".uno:LanguageStatus?Language:string=*";
221 0 : aCmd_Language += ".uno:LanguageStatus?Language:string=Default_";
222 : }
223 :
224 0 : SvtLanguageTable aLanguageTable;
225 :
226 : // get languages to be displayed in the menu
227 0 : std::set< OUString > aLangItems;
228 : FillLangItems( aLangItems, aLanguageTable, m_xFrame, m_aLangGuessHelper,
229 0 : m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
230 :
231 : // now add menu entries
232 : // the different menus purpose will be handled by the different string
233 : // for aCmd_Dialog and aCmd_Language
234 :
235 0 : sal_Int16 nItemId = 1; // in this control the item id is not important for executing the command
236 0 : const OUString sAsterisk("*"); // multiple languages in current selection
237 0 : const OUString sEmpty; // 'no language found' from language guessing
238 0 : std::set< OUString >::const_iterator it;
239 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
240 : {
241 0 : const OUString & rStr( *it );
242 0 : if (rStr != aLanguageTable.GetString( LANGUAGE_NONE ) &&
243 0 : rStr != sAsterisk &&
244 0 : rStr != sEmpty)
245 : {
246 0 : pPopupMenu->InsertItem( nItemId, rStr );
247 0 : aCmd = aCmd_Language;
248 0 : aCmd += rStr;
249 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
250 0 : if (rStr == m_aCurLang && eMode == MODE_SetLanguageSelectionMenu )
251 : {
252 : //make a sign for the current language
253 0 : pPopupMenu->CheckItem( nItemId, true );
254 : }
255 0 : ++nItemId;
256 : }
257 : }
258 :
259 : // entry for LANGUAGE_NONE
260 0 : ++nItemId;
261 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_LANGSTATUS_NONE).toString() );
262 0 : aCmd = aCmd_Language + "LANGUAGE_NONE";
263 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
264 :
265 : // entry for 'Reset to default language'
266 0 : ++nItemId;
267 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_RESET_TO_DEFAULT_LANGUAGE).toString() );
268 0 : aCmd = aCmd_Language + "RESET_LANGUAGES";
269 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
270 :
271 : // entry for opening the Format/Character dialog
272 0 : ++nItemId;
273 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_LANGSTATUS_MORE).toString());
274 0 : pPopupMenu->SetItemCommand( nItemId, aCmd_Dialog );
275 : }
276 :
277 0 : void SAL_CALL LanguageSelectionMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
278 : {
279 0 : svt::PopupMenuControllerBase::updatePopupMenu();
280 :
281 : // Force status update to get information about the current languages
282 0 : osl::ClearableMutexGuard aLock( m_aMutex );
283 0 : Reference< XDispatch > xDispatch( m_xLanguageDispatch );
284 0 : com::sun::star::util::URL aTargetURL;
285 0 : aTargetURL.Complete = m_aLangStatusCommandURL;
286 0 : m_xURLTransformer->parseStrict( aTargetURL );
287 0 : aLock.clear();
288 :
289 0 : if ( xDispatch.is() )
290 : {
291 0 : xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
292 0 : xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
293 : }
294 :
295 : // TODO: Fill menu with the information retrieved by the status update
296 :
297 0 : if ( m_aCommandURL == ".uno:SetLanguageSelectionMenu" )
298 : {
299 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageSelectionMenu );
300 : }
301 0 : else if ( m_aCommandURL == ".uno:SetLanguageParagraphMenu" )
302 : {
303 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageParagraphMenu );
304 : }
305 0 : else if ( m_aCommandURL == ".uno:SetLanguageAllTextMenu" )
306 : {
307 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageAllTextMenu );
308 0 : }
309 0 : }
310 :
311 : // XInitialization
312 0 : void SAL_CALL LanguageSelectionMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException, std::exception )
313 : {
314 0 : osl::MutexGuard aLock( m_aMutex );
315 :
316 0 : bool bInitalized( m_bInitialized );
317 0 : if ( !bInitalized )
318 : {
319 0 : svt::PopupMenuControllerBase::initialize(aArguments);
320 :
321 0 : if ( m_bInitialized )
322 : {
323 0 : m_aLangStatusCommandURL = ".uno:LanguageStatus";
324 0 : m_aMenuCommandURL_Lang = m_aLangStatusCommandURL;
325 0 : m_aMenuCommandURL_Font = ".uno:FontDialog";
326 0 : m_aMenuCommandURL_CharDlgForParagraph = ".uno:FontDialogForParagraph";
327 : }
328 0 : }
329 0 : }
330 :
331 : }
332 :
333 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|