1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include <uielement/langselectionstatusbarcontroller.hxx>
28 #include <classes/fwkresid.hxx>
29 #include <services.h>
30 #include <classes/resource.hrc>
31 #include <vos/mutex.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/window.hxx>
34 #include <vcl/status.hxx>
35 #include <toolkit/helper/convert.hxx>
36 
37 #include <com/sun/star/frame/XPopupMenuController.hpp>
38 #include <toolkit/helper/vclunohelper.hxx>
39 #include <com/sun/star/awt/PopupMenuDirection.hpp>
40 #include <svtools/langtab.hxx>
41 #include "sal/types.h"
42 #include <vcl/svapp.hxx>
43 #include <com/sun/star/awt/MenuItemStyle.hpp>
44 #include <com/sun/star/document/XDocumentLanguages.hpp>
45 #include <i18npool/mslangid.hxx>
46 #include <com/sun/star/i18n/ScriptType.hpp>
47 #include <com/sun/star/frame/XModule.hpp>
48 #include <com/sun/star/frame/XModel.hpp>
49 
50 #include <classes/fwkresid.hxx>
51 #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
52 #include <classes/resource.hrc>
53 #endif
54 #include <com/sun/star/frame/XFrame.hpp>
55 #include <com/sun/star/frame/XDispatch.hpp>
56 #include <com/sun/star/frame/XDispatchProvider.hpp>
57 #include <com/sun/star/util/XURLTransformer.hpp>
58 #include <comphelper/processfactory.hxx>
59 
60 #include <toolkit/helper/vclunohelper.hxx>
61 #include <tools/gen.hxx>
62 #include <com/sun/star/awt/Command.hpp>
63 #include <svl/languageoptions.hxx>
64 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
65 #include <dispatch/uieventloghelper.hxx>
66 
67 #include "helper/mischelper.hxx"
68 
69 #include <rtl/ustrbuf.hxx>
70 
71 #include <map>
72 #include <set>
73 
74 using namespace ::cppu;
75 using namespace ::com::sun::star;
76 using namespace ::com::sun::star::uno;
77 using namespace ::com::sun::star::lang;
78 using namespace ::com::sun::star::frame;
79 using namespace ::com::sun::star::i18n;
80 using namespace ::com::sun::star::document;
81 
82 using ::rtl::OUString;
83 using ::rtl::OUStringBuffer;
84 
85 
86 namespace framework
87 {
88 
DEFINE_XSERVICEINFO_MULTISERVICE(LangSelectionStatusbarController,OWeakObject,SERVICENAME_STATUSBARCONTROLLER,IMPLEMENTATIONNAME_LANGSELECTIONSTATUSBARCONTROLLER)89 DEFINE_XSERVICEINFO_MULTISERVICE        (   LangSelectionStatusbarController     	    ,
90                                             OWeakObject                             ,
91                                             SERVICENAME_STATUSBARCONTROLLER		    ,
92 											IMPLEMENTATIONNAME_LANGSELECTIONSTATUSBARCONTROLLER
93 										)
94 
95 DEFINE_INIT_SERVICE                     (   LangSelectionStatusbarController, {} )
96 
97 LangSelectionStatusbarController::LangSelectionStatusbarController( const uno::Reference< lang::XMultiServiceFactory >& xServiceManager ) :
98     svt::StatusbarController( xServiceManager, uno::Reference< frame::XFrame >(), OUString(), 0 ),
99 	m_bShowMenu( sal_True ),
100     m_nScriptType( LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX ),
101     m_aLangGuessHelper( xServiceManager )
102 {
103 }
104 
initialize(const::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> & aArguments)105 void SAL_CALL LangSelectionStatusbarController::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
106 throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
107 {
108     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::initialize" );
109     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
110 
111     svt::StatusbarController::initialize( aArguments );
112 
113     if ( m_xStatusbarItem.is() )
114     {
115         m_xStatusbarItem->setText( String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) ) );
116     }
117 }
118 
LangMenu(const::com::sun::star::awt::Point & aPos)119 void LangSelectionStatusbarController::LangMenu(
120     const ::com::sun::star::awt::Point& aPos )
121 throw (::com::sun::star::uno::RuntimeException)
122 {
123     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::LangMenu" );
124 	if (!m_bShowMenu)
125 		return;
126 
127 	//add context menu
128     const static OUString s_sPopupMenu(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.PopupMenu"));
129 	Reference< awt::XPopupMenu > xPopupMenu( m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
130     //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
131 	Reference< awt::XPopupMenu > subPopupMenu(m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
132 
133     SvtLanguageTable    aLanguageTable;
134 
135     // get languages to be displayed in the menu
136     std::set< OUString > aLangItems;
137     FillLangItems( aLangItems, aLanguageTable, m_xFrame, m_aLangGuessHelper,
138             m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
139 
140     //
141     // add first few entries to main menu
142     //
143 	sal_Int16 nItemId = static_cast< sal_Int16 >(MID_LANG_SEL_1);
144     const OUString sAsterix(RTL_CONSTASCII_USTRINGPARAM("*"));  // multiple languages in current selection
145     const OUString sEmpty;  // 'no language found' from language guessing
146     std::map< sal_Int16, OUString > aLangMap;
147     std::set< OUString >::const_iterator it;
148     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
149     {
150         const OUString & rStr( *it );
151         if ( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) ) &&
152              rStr != sAsterix &&
153              rStr != sEmpty)
154 		{
155             DBG_ASSERT( MID_LANG_SEL_1 <= nItemId && nItemId <= MID_LANG_SEL_9,
156                     "nItemId outside of expected range!" );
157 	        xPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
158 		    if ( rStr == m_aCurLang )
159 	        {
160 				//make a sign for the current language
161                 xPopupMenu->checkItem( nItemId, sal_True );
162 		    }
163             aLangMap[ nItemId ] = rStr;
164 			++nItemId;
165 		}
166     }
167     xPopupMenu->insertItem( MID_LANG_SEL_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_SEL_NONE );
168     xPopupMenu->insertItem( MID_LANG_SEL_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_SEL_RESET );
169     xPopupMenu->insertItem( MID_LANG_SEL_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_SEL_MORE );
170 
171     //
172     // add entries to submenu ('set language for paragraph')
173     //
174 	nItemId = static_cast< sal_Int16 >(MID_LANG_PARA_1);
175     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
176     {
177         const OUString & rStr( *it );
178         if( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) )&&
179             rStr != sAsterix &&
180             rStr != sEmpty)
181 		{
182             DBG_ASSERT( MID_LANG_PARA_1 <= nItemId && nItemId <= MID_LANG_PARA_9,
183                     "nItemId outside of expected range!" );
184             subPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
185 			aLangMap[nItemId] = rStr;
186             ++nItemId;
187 		}
188     }
189     subPopupMenu->insertItem( MID_LANG_PARA_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_PARA_NONE );
190     subPopupMenu->insertItem( MID_LANG_PARA_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_PARA_RESET );
191     subPopupMenu->insertItem( MID_LANG_PARA_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_PARA_MORE );
192 
193     //
194     // add last two entries to main menu
195     //
196 	xPopupMenu->insertSeparator( MID_LANG_PARA_SEPERATOR );
197     xPopupMenu->insertItem( MID_LANG_PARA_STRING, String( FwkResId( STR_SET_LANGUAGE_FOR_PARAGRAPH )), 0, MID_LANG_PARA_STRING );
198 	xPopupMenu->setPopupMenu( MID_LANG_PARA_STRING, subPopupMenu );
199 
200 
201     //
202     // now display the popup menu and execute every command ...
203     //
204 
205 	Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
206     com::sun::star::awt::Rectangle aRect( aPos.X, aPos.Y, 0, 0 );
207 	sal_Int16 nId = xPopupMenu->execute( xParent, aRect, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
208     //click "More..."
209     if ( nId && m_xFrame.is() )
210     {
211         OUStringBuffer aBuff;
212         //set selected language as current language for selection
213         const OUString aSelectedLang = aLangMap[nId];
214 
215 		if (MID_LANG_SEL_1 <= nId && nId <= MID_LANG_SEL_9)
216 		{
217             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_") ));
218 			aBuff.append( aSelectedLang );
219 		}
220 		else if (nId == MID_LANG_SEL_NONE)
221 		{
222             //set None as current language for selection
223             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE") ));
224 		}
225 		else if (nId == MID_LANG_SEL_RESET)
226 		{
227             // reset language attributes for selection
228             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_RESET_LANGUAGES") ));
229         }
230 		else if (nId == MID_LANG_SEL_MORE)
231 		{
232 			//open the dialog "format/character" for current selection
233             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialog?Language:string=*") ));
234 		}
235         else if (MID_LANG_PARA_1 <= nId && nId <= MID_LANG_PARA_9)
236 		{
237             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_") ));
238             aBuff.append( aSelectedLang );
239 		}
240         else if (nId == MID_LANG_PARA_NONE)
241         {
242             //set None as language for current paragraph
243             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE") ));
244         }
245         else if (nId == MID_LANG_PARA_RESET)
246         {
247             // reset language attributes for paragraph
248             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_RESET_LANGUAGES") ));
249         }
250         else if (nId == MID_LANG_PARA_MORE)
251 		{
252 			//open the dialog "format/character" for current paragraph
253             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialogForParagraph") ));
254 		}
255 
256         const Sequence< beans::PropertyValue > aDummyArgs;
257         execute( aBuff.makeStringAndClear(), aDummyArgs );
258     }
259 }
260 
command(const::com::sun::star::awt::Point & aPos,::sal_Int32 nCommand,::sal_Bool,const::com::sun::star::uno::Any &)261 void SAL_CALL LangSelectionStatusbarController::command(
262     const ::com::sun::star::awt::Point& aPos,
263     ::sal_Int32 nCommand,
264     ::sal_Bool /*bMouseEvent*/,
265     const ::com::sun::star::uno::Any& /*aData*/ )
266 throw (::com::sun::star::uno::RuntimeException)
267 {
268     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::command" );
269 	if ( nCommand & ::awt::Command::CONTEXTMENU )
270 	{
271         LangMenu( aPos );
272 	}
273 }
274 
click(const::com::sun::star::awt::Point & aPos)275 void SAL_CALL LangSelectionStatusbarController::click(
276     const ::com::sun::star::awt::Point& aPos )
277 throw (::com::sun::star::uno::RuntimeException)
278 {
279     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::click" );
280     LangMenu( aPos );
281 }
282 
283 // XStatusListener
statusChanged(const FeatureStateEvent & Event)284 void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
285 throw ( RuntimeException )
286 {
287     // This function will be called when observed data changes,
288     // for example the selection or keyboard language.
289     // - It displays the language in use in the status bar
290     // - and it stores the relevant data for creating the menu
291     //   at some later point in the member variables
292     //      m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedText
293 
294     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::statusChanged" );
295     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
296 
297     if ( m_bDisposed )
298         return;
299 
300 	m_bShowMenu = sal_True;
301 	m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX;  //set the default value
302 
303     if ( m_xStatusbarItem.is() )
304     {
305         OUString aStrValue;
306         Sequence< OUString > aSeq;
307 
308         if ( Event.State >>= aStrValue )
309             m_xStatusbarItem->setText( aStrValue );
310         else if ( Event.State >>= aSeq )
311         {
312             if ( aSeq.getLength() == 4 )
313             {
314 				OUString aStatusText = aSeq[0];
315 				if ( 0 == aStatusText.compareToAscii( RTL_CONSTASCII_STRINGPARAM("*") ))
316                 {
317                     aStatusText = String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) );
318                 }
319                 m_xStatusbarItem->setText( aStatusText );
320 
321                 // Retrieve all other values from the sequence and
322                 // store it members!
323 				m_aCurLang      = aSeq[0];
324                 m_nScriptType   = static_cast< sal_Int16 >( aSeq[1].toInt32() );
325 	            m_aKeyboardLang = aSeq[2];
326                 m_aGuessedTextLang  = aSeq[3];
327             }
328         }
329         else if ( !Event.State.hasValue() )
330 		{
331             m_xStatusbarItem->setText( OUString() );
332 			m_bShowMenu = sal_False;	// no language -> no menu
333 		}
334     }
335 }
336 
337 }
338 
339