xref: /trunk/main/framework/source/uielement/langselectionstatusbarcontroller.cxx (revision bcc22a4c08e1268a4f06e54fb146f88ef49f2cdc)
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 <com/sun/star/awt/PopupMenuDirection.hpp>
39 #include <svtools/langtab.hxx>
40 #include "sal/types.h"
41 #include <vcl/svapp.hxx>
42 #include <com/sun/star/awt/MenuItemStyle.hpp>
43 #include <com/sun/star/document/XDocumentLanguages.hpp>
44 #include <i18npool/mslangid.hxx>
45 #include <com/sun/star/i18n/ScriptType.hpp>
46 #include <com/sun/star/frame/XModule.hpp>
47 #include <com/sun/star/frame/XModel.hpp>
48 
49 #include <classes/fwkresid.hxx>
50 #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
51 #include <classes/resource.hrc>
52 #endif
53 #include <com/sun/star/frame/XFrame.hpp>
54 #include <com/sun/star/frame/XDispatch.hpp>
55 #include <com/sun/star/frame/XDispatchProvider.hpp>
56 #include <com/sun/star/util/XURLTransformer.hpp>
57 #include <comphelper/processfactory.hxx>
58 
59 #include <toolkit/helper/vclunohelper.hxx>
60 #include <tools/gen.hxx>
61 #include <com/sun/star/awt/Command.hpp>
62 #include <svl/languageoptions.hxx>
63 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
64 #include <dispatch/uieventloghelper.hxx>
65 
66 #include "helper/mischelper.hxx"
67 
68 #include <rtl/ustrbuf.hxx>
69 
70 #include <map>
71 #include <set>
72 
73 using namespace ::cppu;
74 using namespace ::com::sun::star;
75 using namespace ::com::sun::star::uno;
76 using namespace ::com::sun::star::lang;
77 using namespace ::com::sun::star::frame;
78 using namespace ::com::sun::star::i18n;
79 using namespace ::com::sun::star::document;
80 
81 using ::rtl::OUString;
82 using ::rtl::OUStringBuffer;
83 
84 
85 namespace framework
86 {
87 
88 DEFINE_XSERVICEINFO_MULTISERVICE        (   LangSelectionStatusbarController            ,
89                                             OWeakObject                             ,
90                                             SERVICENAME_STATUSBARCONTROLLER         ,
91                                             IMPLEMENTATIONNAME_LANGSELECTIONSTATUSBARCONTROLLER
92                                         )
93 
94 DEFINE_INIT_SERVICE                     (   LangSelectionStatusbarController, {} )
95 
96 LangSelectionStatusbarController::LangSelectionStatusbarController( const uno::Reference< lang::XMultiServiceFactory >& xServiceManager ) :
97     svt::StatusbarController( xServiceManager, uno::Reference< frame::XFrame >(), OUString(), 0 ),
98     m_bShowMenu( sal_True ),
99     m_nScriptType( LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX ),
100     m_aLangGuessHelper( xServiceManager )
101 {
102 }
103 
104 void SAL_CALL LangSelectionStatusbarController::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
105 throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
106 {
107     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::initialize" );
108     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
109 
110     svt::StatusbarController::initialize( aArguments );
111 
112     if ( m_xStatusbarItem.is() )
113     {
114         m_xStatusbarItem->setText( String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) ) );
115     }
116 }
117 
118 void LangSelectionStatusbarController::LangMenu(
119     const ::com::sun::star::awt::Point& aPos )
120 throw (::com::sun::star::uno::RuntimeException)
121 {
122     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::LangMenu" );
123     if (!m_bShowMenu)
124         return;
125 
126     //add context menu
127     const static OUString s_sPopupMenu(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.PopupMenu"));
128     Reference< awt::XPopupMenu > xPopupMenu( m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
129     //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
130     Reference< awt::XPopupMenu > subPopupMenu(m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
131 
132     SvtLanguageTable    aLanguageTable;
133 
134     // get languages to be displayed in the menu
135     std::set< OUString > aLangItems;
136     FillLangItems( aLangItems, aLanguageTable, m_xFrame, m_aLangGuessHelper,
137             m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
138 
139     //
140     // add first few entries to main menu
141     //
142     sal_Int16 nItemId = static_cast< sal_Int16 >(MID_LANG_SEL_1);
143     const OUString sAsterix(RTL_CONSTASCII_USTRINGPARAM("*"));  // multiple languages in current selection
144     const OUString sEmpty;  // 'no language found' from language guessing
145     std::map< sal_Int16, OUString > aLangMap;
146     std::set< OUString >::const_iterator it;
147     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
148     {
149         const OUString & rStr( *it );
150         if ( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) ) &&
151              rStr != sAsterix &&
152              rStr != sEmpty)
153         {
154             DBG_ASSERT( MID_LANG_SEL_1 <= nItemId && nItemId <= MID_LANG_SEL_9,
155                     "nItemId outside of expected range!" );
156             xPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
157             if ( rStr == m_aCurLang )
158             {
159                 //make a sign for the current language
160                 xPopupMenu->checkItem( nItemId, sal_True );
161             }
162             aLangMap[ nItemId ] = rStr;
163             ++nItemId;
164         }
165     }
166     xPopupMenu->insertItem( MID_LANG_SEL_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_SEL_NONE );
167     xPopupMenu->insertItem( MID_LANG_SEL_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_SEL_RESET );
168     xPopupMenu->insertItem( MID_LANG_SEL_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_SEL_MORE );
169 
170     //
171     // add entries to submenu ('set language for paragraph')
172     //
173     nItemId = static_cast< sal_Int16 >(MID_LANG_PARA_1);
174     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
175     {
176         const OUString & rStr( *it );
177         if( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) )&&
178             rStr != sAsterix &&
179             rStr != sEmpty)
180         {
181             DBG_ASSERT( MID_LANG_PARA_1 <= nItemId && nItemId <= MID_LANG_PARA_9,
182                     "nItemId outside of expected range!" );
183             subPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
184             aLangMap[nItemId] = rStr;
185             ++nItemId;
186         }
187     }
188     subPopupMenu->insertItem( MID_LANG_PARA_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_PARA_NONE );
189     subPopupMenu->insertItem( MID_LANG_PARA_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_PARA_RESET );
190     subPopupMenu->insertItem( MID_LANG_PARA_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_PARA_MORE );
191 
192     //
193     // add last two entries to main menu
194     //
195     xPopupMenu->insertSeparator( MID_LANG_PARA_SEPERATOR );
196     xPopupMenu->insertItem( MID_LANG_PARA_STRING, String( FwkResId( STR_SET_LANGUAGE_FOR_PARAGRAPH )), 0, MID_LANG_PARA_STRING );
197     xPopupMenu->setPopupMenu( MID_LANG_PARA_STRING, subPopupMenu );
198 
199 
200     //
201     // now display the popup menu and execute every command ...
202     //
203 
204     Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
205     com::sun::star::awt::Rectangle aRect( aPos.X, aPos.Y, 0, 0 );
206     sal_Int16 nId = xPopupMenu->execute( xParent, aRect, 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