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 : namespace {
77 :
78 : class LangSelectionStatusbarController:
79 : public svt::StatusbarController, private boost::noncopyable
80 : {
81 : public:
82 : explicit LangSelectionStatusbarController( const css::uno::Reference< css::uno::XComponentContext >& xContext );
83 :
84 : // XServiceInfo
85 0 : virtual OUString SAL_CALL getImplementationName()
86 : throw (css::uno::RuntimeException)
87 : {
88 0 : return OUString("com.sun.star.comp.framework.LangSelectionStatusbarController");
89 : }
90 :
91 0 : virtual bool SAL_CALL supportsService(OUString const & ServiceName)
92 : throw (css::uno::RuntimeException)
93 : {
94 0 : return ServiceName == "com.sun.star.frame.StatusbarController";
95 : }
96 :
97 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
98 : throw (css::uno::RuntimeException)
99 : {
100 0 : css::uno::Sequence< OUString > aSeq(1);
101 0 : aSeq[0] = OUString("com.sun.star.frame.StatusbarController");
102 0 : return aSeq;
103 : }
104 :
105 : // XInitialization
106 : virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw (css::uno::Exception, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
107 :
108 : // XStatusListener
109 : virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
110 :
111 : // XStatusbarController
112 : virtual void SAL_CALL command( const css::awt::Point& aPos,
113 : ::sal_Int32 nCommand,
114 : sal_Bool bMouseEvent,
115 : const css::uno::Any& aData ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
116 : virtual void SAL_CALL click( const css::awt::Point& aPos ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
117 :
118 : private:
119 1708 : virtual ~LangSelectionStatusbarController() {}
120 :
121 : bool m_bShowMenu; // if the menu is to be displayed or not (depending on the selected object/text)
122 : sal_Int16 m_nScriptType; // the flags for the different script types available in the selection, LATIN = 0x0001, ASIAN = 0x0002, COMPLEX = 0x0004
123 : OUString m_aCurLang; // the language of the current selection, "*" if there are more than one languages
124 : OUString m_aKeyboardLang; // the keyboard language
125 : OUString m_aGuessedTextLang; // the 'guessed' language for the selection, "" if none could be guessed
126 : LanguageGuessingHelper m_aLangGuessHelper;
127 :
128 : void LangMenu( const css::awt::Point& aPos ) throw (css::uno::RuntimeException);
129 : };
130 :
131 854 : LangSelectionStatusbarController::LangSelectionStatusbarController( const uno::Reference< uno::XComponentContext >& xContext ) :
132 : svt::StatusbarController( xContext, uno::Reference< frame::XFrame >(), OUString(), 0 ),
133 : m_bShowMenu( true ),
134 : m_nScriptType( LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX ),
135 854 : m_aLangGuessHelper( xContext )
136 : {
137 854 : }
138 :
139 854 : void SAL_CALL LangSelectionStatusbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
140 : throw (css::uno::Exception, css::uno::RuntimeException, std::exception)
141 : {
142 854 : SolarMutexGuard aSolarMutexGuard;
143 :
144 854 : svt::StatusbarController::initialize( aArguments );
145 :
146 854 : if ( m_xStatusbarItem.is() )
147 : {
148 854 : m_xStatusbarItem->setText( FWK_RESSTR(STR_LANGSTATUS_MULTIPLE_LANGUAGES) );
149 854 : }
150 854 : }
151 :
152 0 : void LangSelectionStatusbarController::LangMenu(
153 : const css::awt::Point& aPos )
154 : throw (css::uno::RuntimeException)
155 : {
156 0 : if (!m_bShowMenu)
157 0 : return;
158 :
159 : //add context menu
160 0 : Reference< awt::XPopupMenu > xPopupMenu( awt::PopupMenu::create( m_xContext ) );
161 : //sub menu that contains all items except the last two items: Separator + Set Language for Paragraph
162 0 : Reference< awt::XPopupMenu > subPopupMenu( awt::PopupMenu::create( m_xContext ) );
163 :
164 : // get languages to be displayed in the menu
165 0 : std::set< OUString > aLangItems;
166 : FillLangItems( aLangItems, m_xFrame, m_aLangGuessHelper,
167 0 : m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedTextLang );
168 :
169 : // add first few entries to main menu
170 0 : sal_Int16 nItemId = static_cast< sal_Int16 >(MID_LANG_SEL_1);
171 0 : const OUString sAsterisk("*"); // multiple languages in current selection
172 0 : const OUString sEmpty; // 'no language found' from language guessing
173 0 : const OUString sNone( SvtLanguageTable::GetLanguageString( LANGUAGE_NONE ));
174 0 : std::map< sal_Int16, OUString > aLangMap;
175 0 : std::set< OUString >::const_iterator it;
176 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
177 : {
178 0 : const OUString & rStr( *it );
179 0 : if ( rStr != sNone &&
180 0 : rStr != sAsterisk &&
181 0 : rStr != sEmpty)
182 : {
183 : DBG_ASSERT( MID_LANG_SEL_1 <= nItemId && nItemId <= MID_LANG_SEL_9,
184 : "nItemId outside of expected range!" );
185 0 : xPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
186 0 : if ( rStr == m_aCurLang )
187 : {
188 : //make a sign for the current language
189 0 : xPopupMenu->checkItem( nItemId, sal_True );
190 : }
191 0 : aLangMap[ nItemId ] = rStr;
192 0 : ++nItemId;
193 : }
194 : }
195 :
196 0 : xPopupMenu->insertItem( MID_LANG_SEL_NONE, FWK_RESSTR(STR_LANGSTATUS_NONE), 0, MID_LANG_SEL_NONE );
197 0 : if ( sNone == m_aCurLang )
198 0 : xPopupMenu->checkItem( MID_LANG_SEL_NONE, sal_True );
199 0 : xPopupMenu->insertItem( MID_LANG_SEL_RESET, FWK_RESSTR(STR_RESET_TO_DEFAULT_LANGUAGE), 0, MID_LANG_SEL_RESET );
200 0 : xPopupMenu->insertItem( MID_LANG_SEL_MORE, FWK_RESSTR(STR_LANGSTATUS_MORE), 0, MID_LANG_SEL_MORE );
201 :
202 : // add entries to submenu ('set language for paragraph')
203 0 : nItemId = static_cast< sal_Int16 >(MID_LANG_PARA_1);
204 0 : for (it = aLangItems.begin(); it != aLangItems.end(); ++it)
205 : {
206 0 : const OUString & rStr( *it );
207 0 : if( rStr != sNone &&
208 0 : rStr != sAsterisk &&
209 0 : rStr != sEmpty)
210 : {
211 : DBG_ASSERT( MID_LANG_PARA_1 <= nItemId && nItemId <= MID_LANG_PARA_9,
212 : "nItemId outside of expected range!" );
213 0 : subPopupMenu->insertItem( nItemId, rStr, 0, nItemId );
214 0 : aLangMap[nItemId] = rStr;
215 0 : ++nItemId;
216 : }
217 : }
218 0 : subPopupMenu->insertItem( MID_LANG_PARA_NONE, FWK_RESSTR(STR_LANGSTATUS_NONE), 0, MID_LANG_PARA_NONE );
219 0 : subPopupMenu->insertItem( MID_LANG_PARA_RESET, FWK_RESSTR(STR_RESET_TO_DEFAULT_LANGUAGE), 0, MID_LANG_PARA_RESET );
220 0 : subPopupMenu->insertItem( MID_LANG_PARA_MORE, FWK_RESSTR(STR_LANGSTATUS_MORE), 0, MID_LANG_PARA_MORE );
221 :
222 : // add last two entries to main menu
223 0 : xPopupMenu->insertSeparator( MID_LANG_PARA_SEPARATOR );
224 0 : xPopupMenu->insertItem( MID_LANG_PARA_STRING, FWK_RESSTR(STR_SET_LANGUAGE_FOR_PARAGRAPH), 0, MID_LANG_PARA_STRING );
225 0 : xPopupMenu->setPopupMenu( MID_LANG_PARA_STRING, subPopupMenu );
226 :
227 : // now display the popup menu and execute every command ...
228 :
229 0 : Reference< awt::XWindowPeer > xParent( m_xParentWindow, UNO_QUERY );
230 0 : com::sun::star::awt::Rectangle aRect( aPos.X, aPos.Y, 0, 0 );
231 0 : sal_Int16 nId = xPopupMenu->execute( xParent, aRect, com::sun::star::awt::PopupMenuDirection::EXECUTE_UP+16 );
232 : //click "More..."
233 0 : if ( nId && m_xFrame.is() )
234 : {
235 0 : OUStringBuffer aBuff;
236 : //set selected language as current language for selection
237 0 : const OUString aSelectedLang = aLangMap[nId];
238 :
239 0 : if (MID_LANG_SEL_1 <= nId && nId <= MID_LANG_SEL_9)
240 : {
241 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_" );
242 0 : aBuff.append( aSelectedLang );
243 : }
244 0 : else if (nId == MID_LANG_SEL_NONE)
245 : {
246 : //set None as current language for selection
247 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_LANGUAGE_NONE" );
248 : }
249 0 : else if (nId == MID_LANG_SEL_RESET)
250 : {
251 : // reset language attributes for selection
252 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Current_RESET_LANGUAGES" );
253 : }
254 0 : else if (nId == MID_LANG_SEL_MORE)
255 : {
256 : //open the dialog "format/character" for current selection
257 0 : aBuff.append( ".uno:FontDialog?Page:string=font" );
258 : }
259 0 : else if (MID_LANG_PARA_1 <= nId && nId <= MID_LANG_PARA_9)
260 : {
261 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_" );
262 0 : aBuff.append( aSelectedLang );
263 : }
264 0 : else if (nId == MID_LANG_PARA_NONE)
265 : {
266 : //set None as language for current paragraph
267 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_LANGUAGE_NONE" );
268 : }
269 0 : else if (nId == MID_LANG_PARA_RESET)
270 : {
271 : // reset language attributes for paragraph
272 0 : aBuff.append( ".uno:LanguageStatus?Language:string=Paragraph_RESET_LANGUAGES" );
273 : }
274 0 : else if (nId == MID_LANG_PARA_MORE)
275 : {
276 : //open the dialog "format/character" for current paragraph
277 0 : aBuff.append( ".uno:FontDialogForParagraph" );
278 : }
279 :
280 0 : const Sequence< beans::PropertyValue > aDummyArgs;
281 0 : execute( aBuff.makeStringAndClear(), aDummyArgs );
282 0 : }
283 : }
284 :
285 0 : void SAL_CALL LangSelectionStatusbarController::command(
286 : const css::awt::Point& aPos,
287 : ::sal_Int32 nCommand,
288 : sal_Bool /*bMouseEvent*/,
289 : const css::uno::Any& /*aData*/ )
290 : throw (css::uno::RuntimeException, std::exception)
291 : {
292 0 : if ( nCommand & ::awt::Command::CONTEXTMENU )
293 : {
294 0 : LangMenu( aPos );
295 : }
296 0 : }
297 :
298 0 : void SAL_CALL LangSelectionStatusbarController::click(
299 : const css::awt::Point& aPos )
300 : throw (css::uno::RuntimeException, std::exception)
301 : {
302 0 : LangMenu( aPos );
303 0 : }
304 :
305 : // XStatusListener
306 1053 : void SAL_CALL LangSelectionStatusbarController::statusChanged( const FeatureStateEvent& Event )
307 : throw ( RuntimeException, std::exception )
308 : {
309 : // This function will be called when observed data changes,
310 : // for example the selection or keyboard language.
311 : // - It displays the language in use in the status bar
312 : // - and it stores the relevant data for creating the menu
313 : // at some later point in the member variables
314 : // m_nScriptType, m_aCurLang, m_aKeyboardLang, m_aGuessedText
315 :
316 1053 : SolarMutexGuard aSolarMutexGuard;
317 :
318 1053 : if ( m_bDisposed )
319 1053 : return;
320 :
321 1053 : m_bShowMenu = true;
322 1053 : m_nScriptType = LS_SCRIPT_LATIN | LS_SCRIPT_ASIAN | LS_SCRIPT_COMPLEX; //set the default value
323 :
324 1053 : if ( m_xStatusbarItem.is() )
325 : {
326 1053 : OUString aStrValue;
327 2106 : Sequence< OUString > aSeq;
328 :
329 1053 : if ( Event.State >>= aStrValue )
330 0 : m_xStatusbarItem->setText( aStrValue );
331 1053 : else if ( Event.State >>= aSeq )
332 : {
333 199 : if ( aSeq.getLength() == 4 )
334 : {
335 199 : OUString aStatusText = aSeq[0];
336 199 : if (aStatusText == "*")
337 : {
338 0 : aStatusText = FWK_RESSTR(STR_LANGSTATUS_MULTIPLE_LANGUAGES);
339 : }
340 199 : m_xStatusbarItem->setText( aStatusText );
341 :
342 : // Retrieve all other values from the sequence and
343 : // store it members!
344 199 : m_aCurLang = aSeq[0];
345 199 : m_nScriptType = static_cast< sal_Int16 >( aSeq[1].toInt32() );
346 199 : m_aKeyboardLang = aSeq[2];
347 199 : m_aGuessedTextLang = aSeq[3];
348 : }
349 : }
350 854 : else if ( !Event.State.hasValue() )
351 : {
352 854 : m_xStatusbarItem->setText( OUString() );
353 854 : m_bShowMenu = false; // no language -> no menu
354 1053 : }
355 1053 : }
356 : }
357 :
358 : }
359 :
360 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
361 854 : com_sun_star_comp_framework_LangSelectionStatusbarController_get_implementation(
362 : css::uno::XComponentContext *context,
363 : css::uno::Sequence<css::uno::Any> const &)
364 : {
365 854 : return cppu::acquire(new LangSelectionStatusbarController(context));
366 951 : }
367 :
368 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|