xref: /trunk/main/framework/source/uielement/langselectionstatusbarcontroller.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
DEFINE_XSERVICEINFO_MULTISERVICE(LangSelectionStatusbarController,OWeakObject,SERVICENAME_STATUSBARCONTROLLER,IMPLEMENTATIONNAME_LANGSELECTIONSTATUSBARCONTROLLER)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 
initialize(const::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> & aArguments)104 void SAL_CALL LangSelectionStatusbarController::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
105 {
106     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::initialize" );
107     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
108 
109     svt::StatusbarController::initialize( aArguments );
110 
111     if ( m_xStatusbarItem.is() )
112     {
113         m_xStatusbarItem->setText( String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) ) );
114     }
115 }
116 
LangMenu(const::com::sun::star::awt::Point & aPos)117 void LangSelectionStatusbarController::LangMenu(
118     const ::com::sun::star::awt::Point& aPos )
119 {
120     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::LangMenu" );
121     if (!m_bShowMenu)
122         return;
123 
124     //add context menu
125     const static OUString s_sPopupMenu(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.PopupMenu"));
126     Reference< awt::XPopupMenu > xPopupMenu( m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
127     //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
128     Reference< awt::XPopupMenu > subPopupMenu(m_xServiceManager->createInstance( s_sPopupMenu ), UNO_QUERY );
129 
130     SvtLanguageTable    aLanguageTable;
131 
132     // get languages to be displayed in the menu
133     std::set< OUString > aLangItems;
134     FillLangItems( aLangItems, aLanguageTable, m_xFrame, m_aLangGuessHelper,
135             m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
136 
137     //
138     // add first few entries to main menu
139     //
140     sal_Int16 nItemId = static_cast< sal_Int16 >(MID_LANG_SEL_1);
141     const OUString sAsterix(RTL_CONSTASCII_USTRINGPARAM("*"));  // multiple languages in current selection
142     const OUString sEmpty;  // 'no language found' from language guessing
143     std::map< sal_Int16, OUString > aLangMap;
144     std::set< OUString >::const_iterator it;
145     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
146     {
147         const OUString & rStr( *it );
148         if ( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) ) &&
149              rStr != sAsterix &&
150              rStr != sEmpty)
151         {
152             DBG_ASSERT( MID_LANG_SEL_1 <= nItemId && nItemId <= MID_LANG_SEL_9,
153                     "nItemId outside of expected range!" );
154             xPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
155             if ( rStr == m_aCurLang )
156             {
157                 //make a sign for the current language
158                 xPopupMenu->checkItem( nItemId, sal_True );
159             }
160             aLangMap[ nItemId ] = rStr;
161             ++nItemId;
162         }
163     }
164     xPopupMenu->insertItem( MID_LANG_SEL_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_SEL_NONE );
165     xPopupMenu->insertItem( MID_LANG_SEL_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_SEL_RESET );
166     xPopupMenu->insertItem( MID_LANG_SEL_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_SEL_MORE );
167 
168     //
169     // add entries to submenu ('set language for paragraph')
170     //
171     nItemId = static_cast< sal_Int16 >(MID_LANG_PARA_1);
172     for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
173     {
174         const OUString & rStr( *it );
175         if( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) )&&
176             rStr != sAsterix &&
177             rStr != sEmpty)
178         {
179             DBG_ASSERT( MID_LANG_PARA_1 <= nItemId && nItemId <= MID_LANG_PARA_9,
180                     "nItemId outside of expected range!" );
181             subPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
182             aLangMap[nItemId] = rStr;
183             ++nItemId;
184         }
185     }
186     subPopupMenu->insertItem( MID_LANG_PARA_NONE,  String( FwkResId( STR_LANGSTATUS_NONE )), 0, MID_LANG_PARA_NONE );
187     subPopupMenu->insertItem( MID_LANG_PARA_RESET, String( FwkResId( STR_RESET_TO_DEFAULT_LANGUAGE )), 0, MID_LANG_PARA_RESET );
188     subPopupMenu->insertItem( MID_LANG_PARA_MORE,  String( FwkResId( STR_LANGSTATUS_MORE )), 0, MID_LANG_PARA_MORE );
189 
190     //
191     // add last two entries to main menu
192     //
193     xPopupMenu->insertSeparator( MID_LANG_PARA_SEPERATOR );
194     xPopupMenu->insertItem( MID_LANG_PARA_STRING, String( FwkResId( STR_SET_LANGUAGE_FOR_PARAGRAPH )), 0, MID_LANG_PARA_STRING );
195     xPopupMenu->setPopupMenu( MID_LANG_PARA_STRING, subPopupMenu );
196 
197 
198     //
199     // now display the popup menu and execute every command ...
200     //
201 
202     Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
203     com::sun::star::awt::Rectangle aRect( aPos.X, aPos.Y, 0, 0 );
204     sal_Int16 nId = xPopupMenu->execute( xParent, aRect, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
205     //click "More..."
206     if ( nId && m_xFrame.is() )
207     {
208         OUStringBuffer aBuff;
209         //set selected language as current language for selection
210         const OUString aSelectedLang = aLangMap[nId];
211 
212         if (MID_LANG_SEL_1 <= nId && nId <= MID_LANG_SEL_9)
213         {
214             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_") ));
215             aBuff.append( aSelectedLang );
216         }
217         else if (nId == MID_LANG_SEL_NONE)
218         {
219             //set None as current language for selection
220             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE") ));
221         }
222         else if (nId == MID_LANG_SEL_RESET)
223         {
224             // reset language attributes for selection
225             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Current_RESET_LANGUAGES") ));
226         }
227         else if (nId == MID_LANG_SEL_MORE)
228         {
229             //open the dialog "format/character" for current selection
230             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialog?Language:string=*") ));
231         }
232         else if (MID_LANG_PARA_1 <= nId && nId <= MID_LANG_PARA_9)
233         {
234             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_") ));
235             aBuff.append( aSelectedLang );
236         }
237         else if (nId == MID_LANG_PARA_NONE)
238         {
239             //set None as language for current paragraph
240             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE") ));
241         }
242         else if (nId == MID_LANG_PARA_RESET)
243         {
244             // reset language attributes for paragraph
245             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:LanguageStatus?Language:string=Paragraph_RESET_LANGUAGES") ));
246         }
247         else if (nId == MID_LANG_PARA_MORE)
248         {
249             //open the dialog "format/character" for current paragraph
250             aBuff.appendAscii( RTL_CONSTASCII_STRINGPARAM( (".uno:FontDialogForParagraph") ));
251         }
252 
253         const Sequence< beans::PropertyValue > aDummyArgs;
254         execute( aBuff.makeStringAndClear(), aDummyArgs );
255     }
256 }
257 
command(const::com::sun::star::awt::Point & aPos,::sal_Int32 nCommand,::sal_Bool,const::com::sun::star::uno::Any &)258 void SAL_CALL LangSelectionStatusbarController::command(
259     const ::com::sun::star::awt::Point& aPos,
260     ::sal_Int32 nCommand,
261     ::sal_Bool /*bMouseEvent*/,
262     const ::com::sun::star::uno::Any& /*aData*/ )
263 {
264     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::command" );
265     if ( nCommand & ::awt::Command::CONTEXTMENU )
266     {
267         LangMenu( aPos );
268     }
269 }
270 
click(const::com::sun::star::awt::Point & aPos)271 void SAL_CALL LangSelectionStatusbarController::click(
272     const ::com::sun::star::awt::Point& aPos )
273 {
274     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::click" );
275     LangMenu( aPos );
276 }
277 
278 // XStatusListener
statusChanged(const FeatureStateEvent & Event)279 void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
280 {
281     // This function will be called when observed data changes,
282     // for example the selection or keyboard language.
283     // - It displays the language in use in the status bar
284     // - and it stores the relevant data for creating the menu
285     //   at some later point in the member variables
286     //      m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedText
287 
288     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "LangSelectionStatusbarController::statusChanged" );
289     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
290 
291     if ( m_bDisposed )
292         return;
293 
294     m_bShowMenu = sal_True;
295     m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX;  //set the default value
296 
297     if ( m_xStatusbarItem.is() )
298     {
299         OUString aStrValue;
300         Sequence< OUString > aSeq;
301 
302         if ( Event.State >>= aStrValue )
303             m_xStatusbarItem->setText( aStrValue );
304         else if ( Event.State >>= aSeq )
305         {
306             if ( aSeq.getLength() == 4 )
307             {
308                 OUString aStatusText = aSeq[0];
309                 if ( 0 == aStatusText.compareToAscii( RTL_CONSTASCII_STRINGPARAM("*") ))
310                 {
311                     aStatusText = String( FwkResId( STR_LANGSTATUS_MULTIPLE_LANGUAGES ) );
312                 }
313                 m_xStatusbarItem->setText( aStatusText );
314 
315                 // Retrieve all other values from the sequence and
316                 // store it members!
317                 m_aCurLang      = aSeq[0];
318                 m_nScriptType   = static_cast< sal_Int16 >( aSeq[1].toInt32() );
319                 m_aKeyboardLang = aSeq[2];
320                 m_aGuessedTextLang  = aSeq[3];
321             }
322         }
323         else if ( !Event.State.hasValue() )
324         {
325             m_xStatusbarItem->setText( OUString() );
326             m_bShowMenu = sal_False;    // no language -> no menu
327         }
328     }
329 }
330 
331 }
332