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