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 12 : DEFINE_XSERVICEINFO_MULTISERVICE_2 ( LanguageSelectionMenuController ,
66 : OWeakObject ,
67 : SERVICENAME_POPUPMENUCONTROLLER ,
68 : IMPLEMENTATIONNAME_LANGUAGESELECTIONMENUCONTROLLER
69 : )
70 :
71 1 : DEFINE_INIT_SERVICE ( LanguageSelectionMenuController, {} )
72 :
73 1 : LanguageSelectionMenuController::LanguageSelectionMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext )
74 : : svt::PopupMenuControllerBase(xContext)
75 : , m_bShowMenu(true)
76 2 : , m_nScriptType(SvtScriptType::LATIN | SvtScriptType::ASIAN | SvtScriptType::COMPLEX)
77 3 : , m_aLangGuessHelper(xContext)
78 : {
79 1 : }
80 :
81 2 : LanguageSelectionMenuController::~LanguageSelectionMenuController()
82 : {
83 2 : }
84 :
85 : // XEventListener
86 0 : void SAL_CALL LanguageSelectionMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
87 : {
88 0 : Reference< css::awt::XMenuListener > xHolder(static_cast<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 >(static_cast<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 = SvtScriptType::LATIN | SvtScriptType::ASIAN | SvtScriptType::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< SvtScriptType >(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 = static_cast<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 = static_cast<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?Page:string=font";
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 : // get languages to be displayed in the menu
225 0 : std::set< OUString > aLangItems;
226 : FillLangItems( aLangItems, m_xFrame, m_aLangGuessHelper,
227 0 : m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
228 :
229 : // now add menu entries
230 : // the different menus purpose will be handled by the different string
231 : // for aCmd_Dialog and aCmd_Language
232 :
233 0 : sal_Int16 nItemId = 1; // in this control the item id is not important for executing the command
234 0 : const OUString sAsterisk("*"); // multiple languages in current selection
235 0 : const OUString sEmpty; // 'no language found' from language guessing
236 0 : const OUString sNone( SvtLanguageTable::GetLanguageString( LANGUAGE_NONE ));
237 0 : std::set< OUString >::const_iterator it;
238 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
239 : {
240 0 : const OUString & rStr( *it );
241 0 : if (rStr != sNone &&
242 0 : rStr != sAsterisk &&
243 0 : rStr != sEmpty)
244 : {
245 0 : pPopupMenu->InsertItem( nItemId, rStr );
246 0 : aCmd = aCmd_Language;
247 0 : aCmd += rStr;
248 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
249 0 : if (rStr == m_aCurLang && eMode == MODE_SetLanguageSelectionMenu )
250 : {
251 : //make a sign for the current language
252 0 : pPopupMenu->CheckItem( nItemId, true );
253 : }
254 0 : ++nItemId;
255 : }
256 : }
257 :
258 : // entry for LANGUAGE_NONE
259 0 : ++nItemId;
260 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_LANGSTATUS_NONE).toString() );
261 0 : aCmd = aCmd_Language + "LANGUAGE_NONE";
262 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
263 :
264 : // entry for 'Reset to default language'
265 0 : ++nItemId;
266 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_RESET_TO_DEFAULT_LANGUAGE).toString() );
267 0 : aCmd = aCmd_Language + "RESET_LANGUAGES";
268 0 : pPopupMenu->SetItemCommand( nItemId, aCmd );
269 :
270 : // entry for opening the Format/Character dialog
271 0 : ++nItemId;
272 0 : pPopupMenu->InsertItem( nItemId, FwlResId(STR_LANGSTATUS_MORE).toString());
273 0 : pPopupMenu->SetItemCommand( nItemId, aCmd_Dialog );
274 : }
275 :
276 0 : void SAL_CALL LanguageSelectionMenuController::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
277 : {
278 0 : svt::PopupMenuControllerBase::updatePopupMenu();
279 :
280 : // Force status update to get information about the current languages
281 0 : osl::ClearableMutexGuard aLock( m_aMutex );
282 0 : Reference< XDispatch > xDispatch( m_xLanguageDispatch );
283 0 : com::sun::star::util::URL aTargetURL;
284 0 : aTargetURL.Complete = m_aLangStatusCommandURL;
285 0 : m_xURLTransformer->parseStrict( aTargetURL );
286 0 : aLock.clear();
287 :
288 0 : if ( xDispatch.is() )
289 : {
290 0 : xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
291 0 : xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
292 : }
293 :
294 : // TODO: Fill menu with the information retrieved by the status update
295 :
296 0 : if ( m_aCommandURL == ".uno:SetLanguageSelectionMenu" )
297 : {
298 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageSelectionMenu );
299 : }
300 0 : else if ( m_aCommandURL == ".uno:SetLanguageParagraphMenu" )
301 : {
302 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageParagraphMenu );
303 : }
304 0 : else if ( m_aCommandURL == ".uno:SetLanguageAllTextMenu" )
305 : {
306 0 : fillPopupMenu(m_xPopupMenu, MODE_SetLanguageAllTextMenu );
307 0 : }
308 0 : }
309 :
310 : // XInitialization
311 0 : void SAL_CALL LanguageSelectionMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException, std::exception )
312 : {
313 0 : osl::MutexGuard aLock( m_aMutex );
314 :
315 0 : bool bInitalized( m_bInitialized );
316 0 : if ( !bInitalized )
317 : {
318 0 : svt::PopupMenuControllerBase::initialize(aArguments);
319 :
320 0 : if ( m_bInitialized )
321 : {
322 0 : m_aLangStatusCommandURL = ".uno:LanguageStatus";
323 0 : m_aMenuCommandURL_Lang = m_aLangStatusCommandURL;
324 0 : m_aMenuCommandURL_Font = ".uno:FontDialog";
325 0 : m_aMenuCommandURL_CharDlgForParagraph = ".uno:FontDialogForParagraph";
326 : }
327 0 : }
328 0 : }
329 :
330 : }
331 :
332 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|