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 
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 
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 
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 	sal_Int16 nId = xPopupMenu->execute( xParent, aPos, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
207     //click "More..."
208     if ( nId && m_xFrame.is() )
209     {
210         OUStringBuffer aBuff;
211         //set selected language as current language for selection
212         const OUString aSelectedLang = aLangMap[nId];
213 
214 		if (MID_LANG_SEL_1 <= nId && nId <= MID_LANG_SEL_9)
215 		{
216             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_") ));
217 			aBuff.append( aSelectedLang );
218 		}
219 		else if (nId == MID_LANG_SEL_NONE)
220 		{
221             //set None as current language for selection
222             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE") ));
223 		}
224 		else if (nId == MID_LANG_SEL_RESET)
225 		{
226             // reset language attributes for selection
227             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_RESET_LANGUAGES") ));
228         }
229 		else if (nId == MID_LANG_SEL_MORE)
230 		{
231 			//open the dialog "format/character" for current selection
232             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialog?Language:string=*") ));
233 		}
234         else if (MID_LANG_PARA_1 <= nId && nId <= MID_LANG_PARA_9)
235 		{
236             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_") ));
237             aBuff.append( aSelectedLang );
238 		}
239         else if (nId == MID_LANG_PARA_NONE)
240         {
241             //set None as language for current paragraph
242             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE") ));
243         }
244         else if (nId == MID_LANG_PARA_RESET)
245         {
246             // reset language attributes for paragraph
247             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_RESET_LANGUAGES") ));
248         }
249         else if (nId == MID_LANG_PARA_MORE)
250 		{
251 			//open the dialog "format/character" for current paragraph
252             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialogForParagraph") ));
253 		}
254 
255         const Sequence< beans::PropertyValue > aDummyArgs;
256         execute( aBuff.makeStringAndClear(), aDummyArgs );
257     }
258 }
259 
260 void SAL_CALL LangSelectionStatusbarController::command(
261     const ::com::sun::star::awt::Point& aPos,
262     ::sal_Int32 nCommand,
263     ::sal_Bool /*bMouseEvent*/,
264     const ::com::sun::star::uno::Any& /*aData*/ )
265 throw (::com::sun::star::uno::RuntimeException)
266 {
267     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::command" );
268 	if ( nCommand & ::awt::Command::CONTEXTMENU )
269 	{
270         LangMenu( aPos );
271 	}
272 }
273 
274 void SAL_CALL LangSelectionStatusbarController::click(
275     const ::com::sun::star::awt::Point& aPos )
276 throw (::com::sun::star::uno::RuntimeException)
277 {
278     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::click" );
279     LangMenu( aPos );
280 }
281 
282 // XStatusListener
283 void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
284 throw ( RuntimeException )
285 {
286     // This function will be called when observed data changes,
287     // for example the selection or keyboard language.
288     // - It displays the language in use in the status bar
289     // - and it stores the relevant data for creating the menu
290     //   at some later point in the member variables
291     //      m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedText
292 
293     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::statusChanged" );
294     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
295 
296     if ( m_bDisposed )
297         return;
298 
299 	m_bShowMenu = sal_True;
300 	m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX;  //set the default value
301 
302     if ( m_xStatusbarItem.is() )
303     {
304         OUString aStrValue;
305         Sequence< OUString > aSeq;
306 
307         if ( Event.State >>= aStrValue )
308             m_xStatusbarItem->setText( aStrValue );
309         else if ( Event.State >>= aSeq )
310         {
311             if ( aSeq.getLength() == 4 )
312             {
313 				OUString aStatusText = aSeq[0];
314 				if ( 0 == aStatusText.compareToAscii( RTL_CONSTASCII_STRINGPARAM("*") ))
315                 {
316                     aStatusText = String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) );
317                 }
318                 m_xStatusbarItem->setText( aStatusText );
319 
320                 // Retrieve all other values from the sequence and
321                 // store it members!
322 				m_aCurLang      = aSeq[0];
323                 m_nScriptType   = static_cast< sal_Int16 >( aSeq[1].toInt32() );
324 	            m_aKeyboardLang = aSeq[2];
325                 m_aGuessedTextLang  = aSeq[3];
326             }
327         }
328         else if ( !Event.State.hasValue() )
329 		{
330             m_xStatusbarItem->setText( OUString() );
331 			m_bShowMenu = sal_False;	// no language -> no menu
332 		}
333     }
334 }
335 
336 }
337 
338