merge the formfield patch from ooo-build
[ooovba.git] / framework / source / uielement / langselectionstatusbarcontroller.cxx
bloba99c60a10ee0332406f4745b043fb713153224ba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: langselectionstatusbarcontroller.cxx,v $
10 * $Revision: 1.6.40.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <uielement/langselectionstatusbarcontroller.hxx>
34 #include <classes/fwkresid.hxx>
35 #include <services.h>
36 #include <classes/resource.hrc>
37 #include <vos/mutex.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/window.hxx>
40 #include <vcl/status.hxx>
41 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
42 #include <toolkit/unohlp.hxx>
43 #endif
44 #include <toolkit/helper/convert.hxx>
46 #include <com/sun/star/frame/XPopupMenuController.hpp>
47 #include <toolkit/helper/vclunohelper.hxx>
48 #include <com/sun/star/awt/PopupMenuDirection.hpp>
49 #include <svtools/langtab.hxx>
50 #include "sal/types.h"
51 #include <vcl/svapp.hxx>
52 #include <com/sun/star/awt/MenuItemStyle.hpp>
53 #include <com/sun/star/document/XDocumentLanguages.hpp>
54 #include <i18npool/mslangid.hxx>
55 #include <com/sun/star/i18n/ScriptType.hpp>
56 #include <com/sun/star/frame/XModule.hpp>
57 #include <com/sun/star/frame/XModel.hpp>
59 #include <map>
60 #include <set>
61 #include <classes/fwkresid.hxx>
62 #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
63 #include <classes/resource.hrc>
64 #endif
65 #include <com/sun/star/frame/XFrame.hpp>
66 #include <com/sun/star/frame/XDispatch.hpp>
67 #include <com/sun/star/frame/XDispatchProvider.hpp>
68 #include <com/sun/star/util/XURLTransformer.hpp>
69 #include <comphelper/processfactory.hxx>
71 #include <toolkit/unohlp.hxx>
72 #include <tools/gen.hxx>
73 #include <com/sun/star/awt/Command.hpp>
74 #include <svtools/languageoptions.hxx>
75 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
76 #include <dispatch/uieventloghelper.hxx>
78 #include "helper/mischelper.hxx"
80 using namespace ::cppu;
81 using namespace ::com::sun::star;
82 using namespace ::com::sun::star::uno;
83 using namespace ::com::sun::star::lang;
84 using namespace ::com::sun::star::frame;
85 using namespace ::com::sun::star::i18n;
86 using namespace ::com::sun::star::document;
88 namespace framework
91 DEFINE_XSERVICEINFO_MULTISERVICE ( LangSelectionStatusbarController ,
92 OWeakObject ,
93 SERVICENAME_STATUSBARCONTROLLER ,
94 IMPLEMENTATIONNAME_LANGSELECTIONSTATUSBARCONTROLLER
97 DEFINE_INIT_SERVICE ( LangSelectionStatusbarController, {} )
99 LangSelectionStatusbarController::LangSelectionStatusbarController( const uno::Reference< lang::XMultiServiceFactory >& xServiceManager ) :
100 svt::StatusbarController( xServiceManager, uno::Reference< frame::XFrame >(), rtl::OUString(), 0 ),
101 m_bShowMenu( sal_True ),
102 m_nScriptType( 7 )
103 ,m_aLangGuessHelper(xServiceManager)
107 // XInterface
108 Any SAL_CALL LangSelectionStatusbarController::queryInterface( const Type& rType )
109 throw ( RuntimeException )
111 return svt::StatusbarController::queryInterface( rType );
114 void SAL_CALL LangSelectionStatusbarController::acquire() throw ()
116 svt::StatusbarController::acquire();
119 void SAL_CALL LangSelectionStatusbarController::release() throw ()
121 svt::StatusbarController::release();
124 void SAL_CALL LangSelectionStatusbarController::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
125 throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
127 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::initialize" );
128 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
130 svt::StatusbarController::initialize( aArguments );
132 if ( m_xParentWindow.is() && m_nID > 0 )
134 Window* pWindow = VCLUnoHelper::GetWindow( m_xParentWindow );
135 if ( pWindow && ( pWindow->GetType() == WINDOW_STATUSBAR ))
137 StatusBar* pStatusBar = (StatusBar *)pWindow;
138 pStatusBar->SetItemText( m_nID, FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) );
143 // XComponent
144 void SAL_CALL LangSelectionStatusbarController::dispose()
145 throw (::com::sun::star::uno::RuntimeException)
147 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::dispose" );
148 svt::StatusbarController::dispose();
151 // XEventListener
152 void SAL_CALL LangSelectionStatusbarController::disposing( const com::sun::star::lang::EventObject& Source )
153 throw ( RuntimeException )
155 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::disposing" );
156 svt::StatusbarController::disposing( Source );
159 // XStatusbarController
160 ::sal_Bool SAL_CALL LangSelectionStatusbarController::mouseButtonDown(
161 const ::com::sun::star::awt::MouseEvent& )
162 throw (::com::sun::star::uno::RuntimeException)
164 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::mouseButtonDown" );
165 return sal_False;
168 ::sal_Bool SAL_CALL LangSelectionStatusbarController::mouseMove(
169 const ::com::sun::star::awt::MouseEvent& )
170 throw (::com::sun::star::uno::RuntimeException)
172 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::mouseMove" );
173 return sal_False;
176 ::sal_Bool SAL_CALL LangSelectionStatusbarController::mouseButtonUp(
177 const ::com::sun::star::awt::MouseEvent& )
178 throw (::com::sun::star::uno::RuntimeException)
180 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::mouseButtonUp" );
181 return sal_False;
184 void LangSelectionStatusbarController::LangMenu()throw (::com::sun::star::uno::RuntimeException)
186 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::LangMenu" );
187 if (!m_bShowMenu)
188 return;
190 //add context menu
191 const static ::rtl::OUString s_sPopupMenu(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.PopupMenu"));
192 Reference< awt::XPopupMenu > xPopupMenu( m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
193 //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
194 Reference< awt::XPopupMenu > subPopupMenu(m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
196 std::set< ::rtl::OUString > LangItems;
198 SvtLanguageTable aLanguageTable;
199 USHORT nItemId=1;
201 //1--add current language
202 if( m_aCurLang != ::rtl::OUString( ) &&
203 LANGUAGE_DONTKNOW != aLanguageTable.GetType( m_aCurLang ))
204 LangItems.insert( m_aCurLang );
206 //2--System
207 SvtLanguageTable aLangTable;
208 const AllSettings& rAllSettings = Application::GetSettings();
209 LanguageType rSystemLanguage = rAllSettings.GetLanguage();
210 if( rSystemLanguage != LANGUAGE_DONTKNOW )
212 if ( IsScriptTypeMatchingToLanguage( m_nScriptType, rSystemLanguage ))
213 LangItems.insert( ::rtl::OUString( aLangTable.GetString( rSystemLanguage )) );
216 //3--UI
217 LanguageType rUILanguage = rAllSettings.GetUILanguage();
218 if( rUILanguage != LANGUAGE_DONTKNOW )
220 if ( IsScriptTypeMatchingToLanguage( m_nScriptType, rUILanguage ))
221 LangItems.insert( ::rtl::OUString( aLangTable.GetString( rUILanguage )) );
224 //4--guessed language
225 uno::Reference< linguistic2::XLanguageGuessing > xLangGuesser( m_aLangGuessHelper.GetGuesser() );
226 if ( xLangGuesser.is() && m_aGuessedText.getLength() > 0)
228 ::com::sun::star::lang::Locale aLocale(xLangGuesser->guessPrimaryLanguage( m_aGuessedText, 0, m_aGuessedText.getLength()) );
229 LanguageType nLang = MsLangId::convertLocaleToLanguageWithFallback( aLocale );
230 if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_NONE && nLang != LANGUAGE_SYSTEM
231 && IsScriptTypeMatchingToLanguage( m_nScriptType, nLang ))
232 LangItems.insert( aLangTable.GetString( nLang ));
235 //5--keyboard language
236 if( m_aKeyboardLang != ::rtl::OUString::createFromAscii( "" ))
238 if ( IsScriptTypeMatchingToLanguage( m_nScriptType, aLanguageTable.GetType( m_aKeyboardLang )))
239 LangItems.insert( m_aKeyboardLang );
242 //6--all languages used in current document
243 Reference< com::sun::star::frame::XModel > xModel;
244 if ( m_xFrame.is() )
246 Reference< com::sun::star::frame::XController > xController( m_xFrame->getController(), UNO_QUERY );
247 if ( xController.is() )
248 xModel = xController->getModel();
250 Reference< document::XDocumentLanguages > xDocumentLanguages( xModel, UNO_QUERY );
251 /*the description of m_nScriptType
252 LATIN : 1
253 ASIAN : 2
254 COMPLEX:4
255 LATIN + ASIAN : 3
256 LATIN + COMPLEX : 5
257 ASIAN + COMPLEX : 6
258 LATIN + ASIAN + COMPLEX : 7
261 sal_Int16 nCount=7;
262 if ( xDocumentLanguages.is() )
264 Sequence< Locale > rLocales( xDocumentLanguages->getDocumentLanguages( m_nScriptType, nCount ));
265 if ( rLocales.getLength() > 0 )
267 for ( USHORT i = 0; i<rLocales.getLength();++i )
269 if ( LangItems.size() == 7 )
270 break;
271 const Locale& rLocale=rLocales[i];
272 if( IsScriptTypeMatchingToLanguage( m_nScriptType, aLangTable.GetType( rLocale.Language )))
273 LangItems.insert( ::rtl::OUString( rLocale.Language ) );
277 std::map< sal_Int16, ::rtl::OUString > LangTable;
279 for( std::set< ::rtl::OUString >::const_iterator it = LangItems.begin(); it != LangItems.end(); ++it )
281 if ( *it != ::rtl::OUString( aLangTable.GetString( LANGUAGE_NONE ) )&&
282 *it != ::rtl::OUString::createFromAscii( "*" ) &&
283 *it != ::rtl::OUString::createFromAscii( "" ))
285 //nItemId = xPopupMenu->getItemCount()+1;
286 nItemId++;
287 xPopupMenu->insertItem( nItemId, *it, css::awt::MenuItemStyle::RADIOCHECK, nItemId );
288 LangTable[nItemId]=*it;
289 if( *it == m_aCurLang )
291 //make a sign for the current language
292 xPopupMenu->checkItem( nItemId, TRUE );
297 //7--none
298 nItemId++;
299 xPopupMenu->insertItem( nItemId, String( FwkResId( STR_LANGSTATUS_NONE )), css::awt::MenuItemStyle::RADIOCHECK, nItemId );
300 //More...
301 nItemId++;
302 xPopupMenu->insertItem( nItemId, String( FwkResId( STR_LANGSTATUS_MORE )), css::awt::MenuItemStyle::RADIOCHECK, nItemId );
304 for( ::std::set< ::rtl::OUString >::const_iterator it = LangItems.begin(); it != LangItems.end(); ++it )
306 if( *it != ::rtl::OUString( aLangTable.GetString( LANGUAGE_NONE ) )&&
307 *it != ::rtl::OUString::createFromAscii( "*" ) &&
308 *it != ::rtl::OUString::createFromAscii( "" ))
310 nItemId++;
311 subPopupMenu->insertItem( nItemId, *it, css::awt::MenuItemStyle::RADIOCHECK, nItemId );
312 LangTable[nItemId]=*it;
315 //7--none
316 nItemId++;
317 subPopupMenu->insertItem( nItemId, String( FwkResId( STR_LANGSTATUS_NONE )), css::awt::MenuItemStyle::RADIOCHECK, nItemId );
318 //More
319 nItemId++;
320 subPopupMenu->insertItem( nItemId, String( FwkResId( STR_LANGSTATUS_MORE )), css::awt::MenuItemStyle::RADIOCHECK, nItemId );
322 nItemId++;
323 xPopupMenu->insertSeparator(nItemId);
325 nItemId++;
326 xPopupMenu->insertItem( nItemId, String( FwkResId( STR_SET_LANGUAGE_FOR_PARAGRAPH )), css::awt::MenuItemStyle::RADIOCHECK, nItemId );
327 xPopupMenu->setPopupMenu( nItemId, subPopupMenu );
329 //display the popup menu and execute every command
331 Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
333 com::sun::star::awt::Rectangle mRectangle;
334 Window* pWindow = VCLUnoHelper::GetWindow( m_xParentWindow );
335 const Point mMousePos = pWindow->GetPointerPosPixel();
336 mRectangle.X = mMousePos.X();
337 mRectangle.Y = mMousePos.Y();
338 sal_Int16 nId = xPopupMenu->execute( xParent, mRectangle, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
339 //click "More..."
340 if ( m_xFrame.is() )
342 uno::Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
343 util::URL aURL;
345 if ( nId < nItemId-3-subPopupMenu->getItemCount() )
347 //1..7
348 //set selected language as current language for selection
349 String SelectedLang = LangTable[nId];
350 aURL.Complete+=String::CreateFromAscii(".uno:LanguageStatus?Language:string=Current_");
351 aURL.Complete+=SelectedLang;
353 else if ( nId == nItemId-3-subPopupMenu->getItemCount() )
356 //set None as current language for selection
357 aURL.Complete+=String::CreateFromAscii(".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE");
359 else if ( nId == nItemId-2-subPopupMenu->getItemCount() )
361 //9 (more)...
362 //open the dialog "format/character" for current selection
363 aURL.Complete+=String::CreateFromAscii(".uno:FontDialog?Language:string=*");
365 else if ( nId < nItemId-3 && nId>nItemId-2-subPopupMenu->getItemCount() )
367 //1..7 para
368 //set selected language for current paragraph
369 String SelectedLang = LangTable[nId];
370 aURL.Complete+=String::CreateFromAscii(".uno:LanguageStatus?Language:string=Paragraph_");
371 aURL.Complete+=SelectedLang;
373 else if ( nId==nItemId-3 )
375 //8 para
376 //set None as language for current paragraph
377 aURL.Complete+=String::CreateFromAscii(".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE");
379 else if ( nId==nItemId-2 )
381 //9 (more) para...
382 //open the dialog "format/character" for current paragraph
383 aURL.Complete+=String::CreateFromAscii(".uno:FontDialogForParagraph");
386 uno::Reference< util::XURLTransformer > xURLTransformer( m_xServiceManager->createInstance( rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), uno::UNO_QUERY );
387 xURLTransformer->parseStrict( aURL );
388 uno::Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL,::rtl::OUString(),0);
389 if( xDispatch.is() )
391 uno::Sequence< beans::PropertyValue > aPV;
392 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
393 UiEventLogHelper(::rtl::OUString::createFromAscii("ButtonToolbarController")).log(m_xServiceManager, m_xFrame, aURL, aPV);
394 xDispatch->dispatch( aURL, aPV);
399 void SAL_CALL LangSelectionStatusbarController::command(
400 const ::com::sun::star::awt::Point& /*aPos*/,
401 ::sal_Int32 nCommand,
402 ::sal_Bool /*bMouseEvent*/,
403 const ::com::sun::star::uno::Any& /*aData*/ )
404 throw (::com::sun::star::uno::RuntimeException)
406 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::command" );
407 if ( nCommand & ::awt::Command::CONTEXTMENU )
409 LangMenu();
413 void SAL_CALL LangSelectionStatusbarController::paint(
414 const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics >& xGraphics,
415 const ::com::sun::star::awt::Rectangle& rOutputRectangle,
416 ::sal_Int32 nItemId,
417 ::sal_Int32 nStyle )
418 throw (::com::sun::star::uno::RuntimeException)
420 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::paint" );
421 svt::StatusbarController::paint( xGraphics, rOutputRectangle, nItemId, nStyle );
424 void SAL_CALL LangSelectionStatusbarController::click()
425 throw (::com::sun::star::uno::RuntimeException)
427 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::click" );
428 LangMenu();
431 void SAL_CALL LangSelectionStatusbarController::doubleClick()
432 throw (::com::sun::star::uno::RuntimeException)
434 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::doubleClick" );
435 svt::StatusbarController::doubleClick();
438 // XStatusListener
439 void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
440 throw ( RuntimeException )
442 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::statusChanged" );
443 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
445 if ( m_bDisposed )
446 return;
448 m_bShowMenu = sal_True;
450 m_nScriptType=7;//set the default value
451 Window* pWindow = VCLUnoHelper::GetWindow( m_xParentWindow );
452 if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR && m_nID != 0 )
454 rtl::OUString aStrValue;
455 Sequence< ::rtl::OUString > aSeq;
457 StatusBar* pStatusBar = (StatusBar *)pWindow;
458 if ( Event.State >>= aStrValue )
460 pStatusBar->SetItemText( m_nID, aStrValue );
461 m_aCurrentLanguage = aStrValue;
463 else if ( Event.State >>= aSeq )
465 if ( aSeq.getLength() == 4 )
467 const String aMultipleLangText( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) );
468 ::rtl::OUString aStatusText = aSeq[0];
469 if ( 0 == aStatusText.compareToAscii( "*" ))
470 aStatusText = aMultipleLangText;
471 pStatusBar->SetItemText( m_nID, aStatusText );
473 // Retrieve all other values from the sequence and
474 // store it members!
475 m_aCurLang = aSeq[0];
476 m_nScriptType = static_cast< sal_Int16 >( aSeq[1].toInt32() );
477 m_aKeyboardLang = aSeq[2];
478 m_aGuessedText = aSeq[3];
481 else if ( !Event.State.hasValue() )
483 pStatusBar->SetItemText( m_nID, String() );
484 m_bShowMenu = sal_False; // no language -> no menu