Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <classes/fwkresid.hxx>
21 : #include <services.h>
22 : #include <classes/resource.hrc>
23 : #include <osl/mutex.hxx>
24 : #include <vcl/svapp.hxx>
25 : #include <vcl/window.hxx>
26 : #include <vcl/status.hxx>
27 : #include <toolkit/helper/convert.hxx>
28 :
29 : #include <boost/noncopyable.hpp>
30 : #include <cppuhelper/supportsservice.hxx>
31 : #include <toolkit/helper/vclunohelper.hxx>
32 : #include <com/sun/star/awt/PopupMenu.hpp>
33 : #include <com/sun/star/awt/PopupMenuDirection.hpp>
34 : #include <svtools/langtab.hxx>
35 : #include <svtools/statusbarcontroller.hxx>
36 : #include "sal/types.h"
37 : #include <com/sun/star/awt/MenuItemStyle.hpp>
38 : #include <com/sun/star/document/XDocumentLanguages.hpp>
39 : #include <i18nlangtag/mslangid.hxx>
40 : #include <com/sun/star/i18n/ScriptType.hpp>
41 : #include <com/sun/star/frame/XModule.hpp>
42 : #include <com/sun/star/frame/XModel.hpp>
43 :
44 : #include <com/sun/star/frame/XFrame.hpp>
45 : #include <com/sun/star/frame/XDispatch.hpp>
46 : #include <com/sun/star/frame/XDispatchProvider.hpp>
47 : #include <comphelper/processfactory.hxx>
48 :
49 : #include <tools/gen.hxx>
50 : #include <com/sun/star/awt/Command.hpp>
51 : #include <svl/languageoptions.hxx>
52 :
53 : #include "helper/mischelper.hxx"
54 :
55 : #include <rtl/ustrbuf.hxx>
56 : #include <rtl/ref.hxx>
57 :
58 : #include <macros/generic.hxx>
59 : #include <macros/xinterface.hxx>
60 : #include <macros/xtypeprovider.hxx>
61 : #include <macros/xserviceinfo.hxx>
62 : #include <stdtypes.h>
63 :
64 : #include <map>
65 : #include <set>
66 :
67 : using namespace ::cppu;
68 : using namespace ::com::sun::star;
69 : using namespace css::uno;
70 : using namespace css::lang;
71 : using namespace css::frame;
72 : using namespace css::i18n;
73 : using namespace css::document;
74 : using namespace framework;
75 :
76 : class SvtLanguageTable;
77 :
78 : namespace {
79 :
80 : class LangSelectionStatusbarController:
81 : public svt::StatusbarController, private boost::noncopyable
82 : {
83 : public:
84 : explicit LangSelectionStatusbarController( const css::uno::Reference< css::uno::XComponentContext >& xContext );
85 :
86 : // XServiceInfo
87 0 : virtual OUString SAL_CALL getImplementationName()
88 : throw (css::uno::RuntimeException)
89 : {
90 0 : return OUString("com.sun.star.comp.framework.LangSelectionStatusbarController");
91 : }
92 :
93 0 : virtual bool SAL_CALL supportsService(OUString const & ServiceName)
94 : throw (css::uno::RuntimeException)
95 : {
96 0 : return ServiceName == "com.sun.star.frame.StatusbarController";
97 : }
98 :
99 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
100 : throw (css::uno::RuntimeException)
101 : {
102 0 : css::uno::Sequence< OUString > aSeq(1);
103 0 : aSeq[0] = OUString("com.sun.star.frame.StatusbarController");
104 0 : return aSeq;
105 : }
106 :
107 : // XInitialization
108 : virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw (css::uno::Exception, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
109 :
110 : // XStatusListener
111 : virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
112 :
113 : // XStatusbarController
114 : virtual void SAL_CALL command( const css::awt::Point& aPos,
115 : ::sal_Int32 nCommand,
116 : sal_Bool bMouseEvent,
117 : const css::uno::Any& aData ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
118 : virtual void SAL_CALL click( const css::awt::Point& aPos ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
119 :
120 : private:
121 0 : virtual ~LangSelectionStatusbarController() {}
122 :
123 : bool m_bShowMenu; // if the menu is to be displayed or not (depending on the selected object/text)
124 : sal_Int16 m_nScriptType; // the flags for the different script types available in the selection, LATIN = 0x0001, ASIAN = 0x0002, COMPLEX = 0x0004
125 : OUString m_aCurLang; // the language of the current selection, "*" if there are more than one languages
126 : OUString m_aKeyboardLang; // the keyboard language
127 : OUString m_aGuessedTextLang; // the 'guessed' language for the selection, "" if none could be guessed
128 : LanguageGuessingHelper m_aLangGuessHelper;
129 :
130 : void LangMenu( const css::awt::Point& aPos ) throw (css::uno::RuntimeException);
131 : };
132 :
133 0 : LangSelectionStatusbarController::LangSelectionStatusbarController( const uno::Reference< uno::XComponentContext >& xContext ) :
134 : svt::StatusbarController( xContext, uno::Reference< frame::XFrame >(), OUString(), 0 ),
135 : m_bShowMenu( true ),
136 : m_nScriptType( LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX ),
137 0 : m_aLangGuessHelper( xContext )
138 : {
139 0 : }
140 :
141 0 : void SAL_CALL LangSelectionStatusbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
142 : throw (css::uno::Exception, css::uno::RuntimeException, std::exception)
143 : {
144 0 : SolarMutexGuard aSolarMutexGuard;
145 :
146 0 : svt::StatusbarController::initialize( aArguments );
147 :
148 0 : if ( m_xStatusbarItem.is() )
149 : {
150 0 : m_xStatusbarItem->setText( FWK_RESSTR(STR_LANGSTATUS_MULTIPLE_LANGUAGES) );
151 0 : }
152 0 : }
153 :
154 0 : void LangSelectionStatusbarController::LangMenu(
155 : const css::awt::Point& aPos )
156 : throw (css::uno::RuntimeException)
157 : {
158 0 : if (!m_bShowMenu)
159 0 : return;
160 :
161 : //add context menu
162 0 : Reference< awt::XPopupMenu > xPopupMenu( awt::PopupMenu::create( m_xContext ) );
163 : //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
164 0 : Reference< awt::XPopupMenu > subPopupMenu( awt::PopupMenu::create( m_xContext ) );
165 :
166 0 : SvtLanguageTable aLanguageTable;
167 :
168 : // get languages to be displayed in the menu
169 0 : std::set< OUString > aLangItems;
170 : FillLangItems( aLangItems, aLanguageTable, m_xFrame, m_aLangGuessHelper,
171 0 : m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
172 :
173 : // add first few entries to main menu
174 0 : sal_Int16 nItemId = static_cast< sal_Int16 >(MID_LANG_SEL_1);
175 0 : const OUString sAsterisk("*"); // multiple languages in current selection
176 0 : const OUString sEmpty; // 'no language found' from language guessing
177 0 : std::map< sal_Int16, OUString > aLangMap;
178 0 : std::set< OUString >::const_iterator it;
179 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
180 : {
181 0 : const OUString & rStr( *it );
182 0 : if ( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) ) &&
183 0 : rStr != sAsterisk &&
184 0 : rStr != sEmpty)
185 : {
186 : DBG_ASSERT( MID_LANG_SEL_1 <= nItemId && nItemId <= MID_LANG_SEL_9,
187 : "nItemId outside of expected range!" );
188 0 : xPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
189 0 : if ( rStr == m_aCurLang )
190 : {
191 : //make a sign for the current language
192 0 : xPopupMenu->checkItem( nItemId, sal_True );
193 : }
194 0 : aLangMap[ nItemId ] = rStr;
195 0 : ++nItemId;
196 : }
197 : }
198 :
199 0 : xPopupMenu->insertItem( MID_LANG_SEL_NONE, FWK_RESSTR(STR_LANGSTATUS_NONE), 0, MID_LANG_SEL_NONE );
200 0 : if ( aLanguageTable.GetString( LANGUAGE_NONE ) == m_aCurLang )
201 0 : xPopupMenu->checkItem( MID_LANG_SEL_NONE, sal_True );
202 0 : xPopupMenu->insertItem( MID_LANG_SEL_RESET, FWK_RESSTR(STR_RESET_TO_DEFAULT_LANGUAGE), 0, MID_LANG_SEL_RESET );
203 0 : xPopupMenu->insertItem( MID_LANG_SEL_MORE, FWK_RESSTR(STR_LANGSTATUS_MORE), 0, MID_LANG_SEL_MORE );
204 :
205 : // add entries to submenu ('set language for paragraph')
206 0 : nItemId = static_cast< sal_Int16 >(MID_LANG_PARA_1);
207 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
208 : {
209 0 : const OUString & rStr( *it );
210 0 : if( rStr != OUString( aLanguageTable.GetString( LANGUAGE_NONE ) )&&
211 0 : rStr != sAsterisk &&
212 0 : rStr != sEmpty)
213 : {
214 : DBG_ASSERT( MID_LANG_PARA_1 <= nItemId && nItemId <= MID_LANG_PARA_9,
215 : "nItemId outside of expected range!" );
216 0 : subPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
217 0 : aLangMap[nItemId] = rStr;
218 0 : ++nItemId;
219 : }
220 : }
221 0 : subPopupMenu->insertItem( MID_LANG_PARA_NONE, FWK_RESSTR(STR_LANGSTATUS_NONE), 0, MID_LANG_PARA_NONE );
222 0 : subPopupMenu->insertItem( MID_LANG_PARA_RESET, FWK_RESSTR(STR_RESET_TO_DEFAULT_LANGUAGE), 0, MID_LANG_PARA_RESET );
223 0 : subPopupMenu->insertItem( MID_LANG_PARA_MORE, FWK_RESSTR(STR_LANGSTATUS_MORE), 0, MID_LANG_PARA_MORE );
224 :
225 : // add last two entries to main menu
226 0 : xPopupMenu->insertSeparator( MID_LANG_PARA_SEPARATOR );
227 0 : xPopupMenu->insertItem( MID_LANG_PARA_STRING, FWK_RESSTR(STR_SET_LANGUAGE_FOR_PARAGRAPH), 0, MID_LANG_PARA_STRING );
228 0 : xPopupMenu->setPopupMenu( MID_LANG_PARA_STRING, subPopupMenu );
229 :
230 : // now display the popup menu and execute every command ...
231 :
232 0 : Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
233 0 : com::sun::star::awt::Rectangle aRect( aPos.X, aPos.Y, 0, 0 );
234 0 : sal_Int16 nId = xPopupMenu->execute( xParent, aRect, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
235 : //click "More..."
236 0 : if ( nId && m_xFrame.is() )
237 : {
238 0 : OUStringBuffer aBuff;
239 : //set selected language as current language for selection
240 0 : const OUString aSelectedLang = aLangMap[nId];
241 :
242 0 : if (MID_LANG_SEL_1 <= nId && nId <= MID_LANG_SEL_9)
243 : {
244 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_" );
245 0 : aBuff.append( aSelectedLang );
246 : }
247 0 : else if (nId == MID_LANG_SEL_NONE)
248 : {
249 : //set None as current language for selection
250 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE" );
251 : }
252 0 : else if (nId == MID_LANG_SEL_RESET)
253 : {
254 : // reset language attributes for selection
255 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_RESET_LANGUAGES" );
256 : }
257 0 : else if (nId == MID_LANG_SEL_MORE)
258 : {
259 : //open the dialog "format/character" for current selection
260 0 : aBuff.append( ".uno:FontDialog?Language:string=*" );
261 : }
262 0 : else if (MID_LANG_PARA_1 <= nId && nId <= MID_LANG_PARA_9)
263 : {
264 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_" );
265 0 : aBuff.append( aSelectedLang );
266 : }
267 0 : else if (nId == MID_LANG_PARA_NONE)
268 : {
269 : //set None as language for current paragraph
270 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE" );
271 : }
272 0 : else if (nId == MID_LANG_PARA_RESET)
273 : {
274 : // reset language attributes for paragraph
275 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_RESET_LANGUAGES" );
276 : }
277 0 : else if (nId == MID_LANG_PARA_MORE)
278 : {
279 : //open the dialog "format/character" for current paragraph
280 0 : aBuff.append( ".uno:FontDialogForParagraph" );
281 : }
282 :
283 0 : const Sequence< beans::PropertyValue > aDummyArgs;
284 0 : execute( aBuff.makeStringAndClear(), aDummyArgs );
285 0 : }
286 : }
287 :
288 0 : void SAL_CALL LangSelectionStatusbarController::command(
289 : const css::awt::Point& aPos,
290 : ::sal_Int32 nCommand,
291 : sal_Bool /*bMouseEvent*/,
292 : const css::uno::Any& /*aData*/ )
293 : throw (css::uno::RuntimeException, std::exception)
294 : {
295 0 : if ( nCommand & ::awt::Command::CONTEXTMENU )
296 : {
297 0 : LangMenu( aPos );
298 : }
299 0 : }
300 :
301 0 : void SAL_CALL LangSelectionStatusbarController::click(
302 : const css::awt::Point& aPos )
303 : throw (css::uno::RuntimeException, std::exception)
304 : {
305 0 : LangMenu( aPos );
306 0 : }
307 :
308 : // XStatusListener
309 0 : void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
310 : throw ( RuntimeException, std::exception )
311 : {
312 : // This function will be called when observed data changes,
313 : // for example the selection or keyboard language.
314 : // - It displays the language in use in the status bar
315 : // - and it stores the relevant data for creating the menu
316 : // at some later point in the member variables
317 : // m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedText
318 :
319 0 : SolarMutexGuard aSolarMutexGuard;
320 :
321 0 : if ( m_bDisposed )
322 0 : return;
323 :
324 0 : m_bShowMenu = true;
325 0 : m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX; //set the default value
326 :
327 0 : if ( m_xStatusbarItem.is() )
328 : {
329 0 : OUString aStrValue;
330 0 : Sequence< OUString > aSeq;
331 :
332 0 : if ( Event.State >>= aStrValue )
333 0 : m_xStatusbarItem->setText( aStrValue );
334 0 : else if ( Event.State >>= aSeq )
335 : {
336 0 : if ( aSeq.getLength() == 4 )
337 : {
338 0 : OUString aStatusText = aSeq[0];
339 0 : if (aStatusText == "*")
340 : {
341 0 : aStatusText = FWK_RESSTR(STR_LANGSTATUS_MULTIPLE_LANGUAGES);
342 : }
343 0 : m_xStatusbarItem->setText( aStatusText );
344 :
345 : // Retrieve all other values from the sequence and
346 : // store it members!
347 0 : m_aCurLang = aSeq[0];
348 0 : m_nScriptType = static_cast< sal_Int16 >( aSeq[1].toInt32() );
349 0 : m_aKeyboardLang = aSeq[2];
350 0 : m_aGuessedTextLang = aSeq[3];
351 : }
352 : }
353 0 : else if ( !Event.State.hasValue() )
354 : {
355 0 : m_xStatusbarItem->setText( OUString() );
356 0 : m_bShowMenu = false; // no language -> no menu
357 0 : }
358 0 : }
359 : }
360 :
361 : }
362 :
363 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
364 0 : com_sun_star_comp_framework_LangSelectionStatusbarController_get_implementation(
365 : css::uno::XComponentContext *context,
366 : css::uno::Sequence<css::uno::Any> const &)
367 : {
368 0 : return cppu::acquire(new LangSelectionStatusbarController(context));
369 : }
370 :
371 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|