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 <sal/macros.h>
21 : #include <uielement/controlmenucontroller.hxx>
22 :
23 : #include <threadhelp/resetableguard.hxx>
24 : #include "services.h"
25 :
26 : #include <com/sun/star/awt/XDevice.hpp>
27 : #include <com/sun/star/beans/PropertyValue.hpp>
28 : #include <com/sun/star/awt/MenuItemStyle.hpp>
29 : #include <com/sun/star/frame/XDispatchProvider.hpp>
30 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
31 : #include <com/sun/star/container/XNameContainer.hpp>
32 : #include <com/sun/star/beans/XPropertySet.hpp>
33 :
34 : #include <vcl/menu.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <vcl/i18nhelp.hxx>
37 : #include <rtl/ustrbuf.hxx>
38 : #include <rtl/strbuf.hxx>
39 : #include <svl/solar.hrc>
40 : #include <tools/rcid.h>
41 : #include <vcl/image.hxx>
42 : #include <svtools/menuoptions.hxx>
43 : #include <osl/mutex.hxx>
44 :
45 : // Copied from svx
46 : // Function-Id's
47 : #define RID_FMSHELL_CONVERSIONMENU (RID_FORMS_START + 4)
48 : #define RID_SVXIMGLIST_FMEXPL (RID_FORMS_START + 0)
49 :
50 : // Forms - Ids, used to address images from image list
51 : #define SID_FMSLOTS_START (SID_SVX_START + 592)
52 : #define SID_MORE_FMSLOTS_START (SID_SVX_START + 702)
53 :
54 : #define SID_FM_CONVERTTO_EDIT (SID_MORE_FMSLOTS_START + 32)
55 : #define SID_FM_CONVERTTO_BUTTON (SID_MORE_FMSLOTS_START + 33)
56 : #define SID_FM_CONVERTTO_FIXEDTEXT (SID_MORE_FMSLOTS_START + 34)
57 : #define SID_FM_CONVERTTO_LISTBOX (SID_MORE_FMSLOTS_START + 35)
58 : #define SID_FM_CONVERTTO_CHECKBOX (SID_MORE_FMSLOTS_START + 36)
59 : #define SID_FM_CONVERTTO_RADIOBUTTON (SID_MORE_FMSLOTS_START + 37)
60 : #define SID_FM_CONVERTTO_GROUPBOX (SID_MORE_FMSLOTS_START + 38)
61 : #define SID_FM_CONVERTTO_COMBOBOX (SID_MORE_FMSLOTS_START + 39)
62 : #define SID_FM_CONVERTTO_GRID (SID_MORE_FMSLOTS_START + 40)
63 : #define SID_FM_CONVERTTO_IMAGEBUTTON (SID_MORE_FMSLOTS_START + 41)
64 : #define SID_FM_CONVERTTO_FILECONTROL (SID_MORE_FMSLOTS_START + 42)
65 : #define SID_FM_CONVERTTO_DATE (SID_MORE_FMSLOTS_START + 43)
66 : #define SID_FM_CONVERTTO_TIME (SID_MORE_FMSLOTS_START + 44)
67 : #define SID_FM_CONVERTTO_NUMERIC (SID_MORE_FMSLOTS_START + 45)
68 : #define SID_FM_CONVERTTO_CURRENCY (SID_MORE_FMSLOTS_START + 46)
69 : #define SID_FM_CONVERTTO_PATTERN (SID_MORE_FMSLOTS_START + 47)
70 : #define SID_FM_CONVERTTO_IMAGECONTROL (SID_MORE_FMSLOTS_START + 48)
71 : #define SID_FM_CONVERTTO_FORMATTED (SID_MORE_FMSLOTS_START + 49)
72 : #define SID_FM_CONVERTTO_SCROLLBAR (SID_MORE_FMSLOTS_START + 68)
73 : #define SID_FM_CONVERTTO_SPINBUTTON (SID_MORE_FMSLOTS_START + 69)
74 :
75 : #define SID_FM_DATEFIELD (SID_MORE_FMSLOTS_START + 2)
76 : #define SID_FM_TIMEFIELD (SID_MORE_FMSLOTS_START + 3)
77 : #define SID_FM_NUMERICFIELD (SID_MORE_FMSLOTS_START + 4)
78 : #define SID_FM_CURRENCYFIELD (SID_MORE_FMSLOTS_START + 5)
79 : #define SID_FM_PATTERNFIELD (SID_MORE_FMSLOTS_START + 6)
80 : #define SID_FM_IMAGECONTROL (SID_MORE_FMSLOTS_START + 8)
81 : #define SID_FM_FORMATTEDFIELD (SID_MORE_FMSLOTS_START + 26)
82 : #define SID_FM_SCROLLBAR (SID_MORE_FMSLOTS_START + 66)
83 : #define SID_FM_SPINBUTTON (SID_MORE_FMSLOTS_START + 67)
84 : #define SID_FM_CONFIG (SID_FMSLOTS_START + 1)
85 : #define SID_FM_PUSHBUTTON (SID_FMSLOTS_START + 2)
86 : #define SID_FM_RADIOBUTTON (SID_FMSLOTS_START + 3)
87 : #define SID_FM_CHECKBOX (SID_FMSLOTS_START + 4)
88 : #define SID_FM_FIXEDTEXT (SID_FMSLOTS_START + 5)
89 : #define SID_FM_GROUPBOX (SID_FMSLOTS_START + 6)
90 : #define SID_FM_EDIT (SID_FMSLOTS_START + 7)
91 : #define SID_FM_LISTBOX (SID_FMSLOTS_START + 8)
92 : #define SID_FM_COMBOBOX (SID_FMSLOTS_START + 9)
93 : #define SID_FM_URLBUTTON (SID_FMSLOTS_START + 10)
94 : #define SID_FM_DBGRID (SID_FMSLOTS_START + 11)
95 : #define SID_FM_IMAGEBUTTON (SID_FMSLOTS_START + 12)
96 : #define SID_FM_FILECONTROL (SID_FMSLOTS_START + 13)
97 :
98 : static sal_Int16 nConvertSlots[] =
99 : {
100 : SID_FM_CONVERTTO_EDIT,
101 : SID_FM_CONVERTTO_BUTTON,
102 : SID_FM_CONVERTTO_FIXEDTEXT,
103 : SID_FM_CONVERTTO_LISTBOX,
104 : SID_FM_CONVERTTO_CHECKBOX,
105 : SID_FM_CONVERTTO_RADIOBUTTON,
106 : SID_FM_CONVERTTO_GROUPBOX,
107 : SID_FM_CONVERTTO_COMBOBOX,
108 : // SID_FM_CONVERTTO_GRID,
109 : SID_FM_CONVERTTO_IMAGEBUTTON,
110 : SID_FM_CONVERTTO_FILECONTROL,
111 : SID_FM_CONVERTTO_DATE,
112 : SID_FM_CONVERTTO_TIME,
113 : SID_FM_CONVERTTO_NUMERIC,
114 : SID_FM_CONVERTTO_CURRENCY,
115 : SID_FM_CONVERTTO_PATTERN,
116 : SID_FM_CONVERTTO_IMAGECONTROL,
117 : SID_FM_CONVERTTO_FORMATTED,
118 : SID_FM_CONVERTTO_SCROLLBAR,
119 : SID_FM_CONVERTTO_SPINBUTTON
120 : };
121 :
122 : static sal_Int16 nCreateSlots[] =
123 : {
124 : SID_FM_EDIT,
125 : SID_FM_PUSHBUTTON,
126 : SID_FM_FIXEDTEXT,
127 : SID_FM_LISTBOX,
128 : SID_FM_CHECKBOX,
129 : SID_FM_RADIOBUTTON,
130 : SID_FM_GROUPBOX,
131 : SID_FM_COMBOBOX,
132 : // SID_FM_DBGRID,
133 : SID_FM_IMAGEBUTTON,
134 : SID_FM_FILECONTROL,
135 : SID_FM_DATEFIELD,
136 : SID_FM_TIMEFIELD,
137 : SID_FM_NUMERICFIELD,
138 : SID_FM_CURRENCYFIELD,
139 : SID_FM_PATTERNFIELD,
140 : SID_FM_IMAGECONTROL,
141 : SID_FM_FORMATTEDFIELD,
142 : SID_FM_SCROLLBAR,
143 : SID_FM_SPINBUTTON
144 : };
145 :
146 : const char* aCommands[] =
147 : {
148 : ".uno:ConvertToEdit",
149 : ".uno:ConvertToButton",
150 : ".uno:ConvertToFixed",
151 : ".uno:ConvertToList",
152 : ".uno:ConvertToCheckBox",
153 : ".uno:ConvertToRadio",
154 : ".uno:ConvertToGroup",
155 : ".uno:ConvertToCombo",
156 : // ".uno:ConvertToGrid",
157 : ".uno:ConvertToImageBtn",
158 : ".uno:ConvertToFileControl",
159 : ".uno:ConvertToDate",
160 : ".uno:ConvertToTime",
161 : ".uno:ConvertToNumeric",
162 : ".uno:ConvertToCurrency",
163 : ".uno:ConvertToPattern",
164 : ".uno:ConvertToImageControl",
165 : ".uno:ConvertToFormatted",
166 : ".uno:ConvertToScrollBar",
167 : ".uno:ConvertToSpinButton"
168 : };
169 :
170 : //_________________________________________________________________________________________________________________
171 : // Defines
172 : //_________________________________________________________________________________________________________________
173 :
174 : using namespace com::sun::star::uno;
175 : using namespace com::sun::star::lang;
176 : using namespace com::sun::star::frame;
177 : using namespace com::sun::star::beans;
178 : using namespace com::sun::star::util;
179 : using namespace com::sun::star::style;
180 : using namespace com::sun::star::container;
181 :
182 : namespace framework
183 : {
184 :
185 157 : DEFINE_XSERVICEINFO_MULTISERVICE ( ControlMenuController ,
186 : OWeakObject ,
187 : SERVICENAME_POPUPMENUCONTROLLER ,
188 : IMPLEMENTATIONNAME_CONTROLMENUCONTROLLER
189 : )
190 :
191 0 : DEFINE_INIT_SERVICE ( ControlMenuController, {} )
192 :
193 0 : ControlMenuController::ControlMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
194 : svt::PopupMenuControllerBase( xServiceManager ),
195 0 : m_pResPopupMenu( 0 )
196 : {
197 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
198 0 : m_bShowMenuImages = rSettings.GetUseImagesInMenus();
199 :
200 0 : }
201 :
202 0 : ControlMenuController::~ControlMenuController()
203 : {
204 0 : }
205 :
206 : // private function
207 0 : void ControlMenuController::updateImagesPopupMenu( PopupMenu* pPopupMenu )
208 : {
209 0 : ResMgr* pResMgr = ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag().getLocale());
210 0 : ResId aResId( RID_SVXIMGLIST_FMEXPL, *pResMgr );
211 0 : aResId.SetRT( RSC_IMAGELIST );
212 :
213 0 : if ( pResMgr->IsAvailable( aResId ))
214 : {
215 0 : ImageList aImageList( aResId );
216 0 : for ( sal_uInt32 i=0; i < sizeof(nConvertSlots)/sizeof(nConvertSlots[0]); ++i )
217 : {
218 : // das entsprechende Image dran
219 0 : if ( m_bShowMenuImages )
220 0 : pPopupMenu->SetItemImage( nConvertSlots[i], aImageList.GetImage(nCreateSlots[i]));
221 : else
222 0 : pPopupMenu->SetItemImage( nConvertSlots[i], Image() );
223 0 : }
224 : }
225 :
226 0 : delete pResMgr;
227 0 : }
228 :
229 : // private function
230 0 : void ControlMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
231 : {
232 0 : VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
233 0 : PopupMenu* pVCLPopupMenu = 0;
234 :
235 0 : SolarMutexGuard aSolarMutexGuard;
236 :
237 0 : resetPopupMenu( rPopupMenu );
238 0 : if ( pPopupMenu )
239 0 : pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
240 :
241 0 : if ( pVCLPopupMenu && m_pResPopupMenu )
242 0 : *pVCLPopupMenu = *m_pResPopupMenu;
243 0 : }
244 :
245 : // XEventListener
246 0 : void SAL_CALL ControlMenuController::disposing( const EventObject& ) throw ( RuntimeException )
247 : {
248 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
249 :
250 0 : osl::ResettableMutexGuard aLock( m_aMutex );
251 0 : m_xFrame.clear();
252 0 : m_xDispatch.clear();
253 0 : m_xServiceManager.clear();
254 :
255 0 : if ( m_xPopupMenu.is() )
256 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
257 0 : m_xPopupMenu.clear();
258 0 : delete m_pResPopupMenu;
259 0 : }
260 :
261 : // XStatusListener
262 0 : void SAL_CALL ControlMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
263 : {
264 0 : osl::ResettableMutexGuard aLock( m_aMutex );
265 :
266 0 : sal_uInt16 nMenuId = 0;
267 0 : for (sal_uInt32 i=0; i < sizeof(aCommands) / sizeof (aCommands[0]); ++i)
268 : {
269 0 : if ( Event.FeatureURL.Complete.equalsAscii( aCommands[i] ))
270 : {
271 0 : nMenuId = nConvertSlots[i];
272 0 : break;
273 : }
274 : }
275 :
276 0 : if ( nMenuId )
277 : {
278 0 : VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( m_xPopupMenu );
279 :
280 0 : SolarMutexGuard aSolarMutexGuard;
281 :
282 0 : PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
283 :
284 0 : if ( !Event.IsEnabled && pVCLPopupMenu->GetItemPos( nMenuId ) != MENU_ITEM_NOTFOUND )
285 0 : pVCLPopupMenu->RemoveItem( pVCLPopupMenu->GetItemPos( nMenuId ));
286 0 : else if ( Event.IsEnabled && pVCLPopupMenu->GetItemPos( nMenuId ) == MENU_ITEM_NOTFOUND )
287 : {
288 0 : sal_Int16 nSourcePos = m_pResPopupMenu->GetItemPos(nMenuId);
289 0 : sal_Int16 nPrevInSource = nSourcePos;
290 0 : sal_uInt16 nPrevInConversion = MENU_ITEM_NOTFOUND;
291 0 : while (nPrevInSource>0)
292 : {
293 0 : sal_Int16 nPrevId = m_pResPopupMenu->GetItemId(--nPrevInSource);
294 :
295 : // do we have the source's predecessor in our conversion menu, too ?
296 0 : nPrevInConversion = pVCLPopupMenu->GetItemPos( nPrevId );
297 0 : if ( nPrevInConversion != MENU_ITEM_NOTFOUND )
298 0 : break;
299 : }
300 :
301 0 : if ( MENU_ITEM_NOTFOUND == nPrevInConversion )
302 : // none of the items which precede the nSID-slot in the source menu are present in our conversion menu
303 0 : nPrevInConversion = sal::static_int_cast< sal_uInt16 >(-1); // put the item at the first position
304 :
305 0 : pVCLPopupMenu->InsertItem( nMenuId, m_pResPopupMenu->GetItemText( nMenuId ), m_pResPopupMenu->GetItemBits( nMenuId ), ++nPrevInConversion );
306 0 : pVCLPopupMenu->SetItemImage( nMenuId, m_pResPopupMenu->GetItemImage( nMenuId ));
307 0 : pVCLPopupMenu->SetHelpId( nMenuId, m_pResPopupMenu->GetHelpId( nMenuId ));
308 0 : }
309 0 : }
310 0 : }
311 :
312 : // XMenuListener
313 0 : void ControlMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aURL)
314 : {
315 0 : UrlToDispatchMap::iterator pIter = m_aURLToDispatchMap.find( aURL.Complete );
316 0 : if ( pIter != m_aURLToDispatchMap.end() )
317 : {
318 0 : Sequence<PropertyValue> aArgs;
319 0 : Reference< XDispatch > xDispatch = pIter->second;
320 0 : if ( xDispatch.is() )
321 0 : xDispatch->dispatch( aURL, aArgs );
322 : }
323 0 : }
324 :
325 0 : void SAL_CALL ControlMenuController::activate( const css::awt::MenuEvent& ) throw (RuntimeException)
326 : {
327 0 : osl::ResettableMutexGuard aLock( m_aMutex );
328 :
329 0 : if ( m_xPopupMenu.is() )
330 : {
331 0 : SolarMutexGuard aSolarMutexGuard;
332 :
333 : // Check if some modes have changed so we have to update our menu images
334 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
335 0 : sal_Bool bShowMenuImages = rSettings.GetUseImagesInMenus();
336 0 : sal_Bool bUpdateImages = (bShowMenuImages != m_bShowMenuImages);
337 :
338 0 : if ( bUpdateImages )
339 : {
340 0 : m_bShowMenuImages = bShowMenuImages;
341 :
342 0 : VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( m_xPopupMenu );
343 0 : if ( pPopupMenu )
344 : {
345 0 : PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
346 0 : if ( pVCLPopupMenu && bUpdateImages )
347 0 : updateImagesPopupMenu( pVCLPopupMenu );
348 : }
349 0 : }
350 0 : }
351 0 : }
352 :
353 : // XPopupMenuController
354 0 : void ControlMenuController::impl_setPopupMenu()
355 : {
356 0 : if ( m_pResPopupMenu == 0 )
357 : {
358 0 : ResMgr* pResMgr = ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag().getLocale());
359 0 : if ( pResMgr )
360 : {
361 0 : ResId aResId( RID_FMSHELL_CONVERSIONMENU, *pResMgr );
362 0 : aResId.SetRT( RSC_MENU );
363 0 : if ( pResMgr->IsAvailable( aResId ))
364 0 : m_pResPopupMenu = new PopupMenu( aResId );
365 :
366 0 : updateImagesPopupMenu( m_pResPopupMenu );
367 0 : delete pResMgr;
368 : }
369 : } // if ( m_pResPopupMenu == 0 )
370 0 : }
371 :
372 0 : void SAL_CALL ControlMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException)
373 : {
374 0 : osl::ResettableMutexGuard aLock( m_aMutex );
375 :
376 0 : throwIfDisposed();
377 :
378 0 : if ( m_xFrame.is() && m_xPopupMenu.is() )
379 : {
380 0 : URL aTargetURL;
381 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
382 0 : fillPopupMenu( m_xPopupMenu );
383 0 : m_aURLToDispatchMap.free();
384 :
385 0 : for (sal_uInt32 i=0; i<sizeof(aCommands)/sizeof(aCommands[0]); ++i)
386 : {
387 0 : aTargetURL.Complete = rtl::OUString::createFromAscii( aCommands[i] );
388 0 : m_xURLTransformer->parseStrict( aTargetURL );
389 :
390 0 : Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
391 0 : if ( xDispatch.is() )
392 : {
393 0 : xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
394 0 : xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
395 0 : m_aURLToDispatchMap.insert( UrlToDispatchMap::value_type( aTargetURL.Complete, xDispatch ));
396 : }
397 0 : }
398 0 : }
399 0 : }
400 :
401 : // XInitialization
402 0 : void SAL_CALL ControlMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
403 : {
404 0 : osl::ResettableMutexGuard aLock( m_aMutex );
405 0 : svt::PopupMenuControllerBase::initialize(aArguments);
406 0 : m_aBaseURL = ::rtl::OUString();
407 0 : }
408 :
409 : }
410 :
411 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|