1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <com/sun/star/lang/XInitialization.hpp>
27 #include <com/sun/star/frame/XDispatch.hpp>
28 #include <com/sun/star/frame/XDispatchProvider.hpp>
29 #include <com/sun/star/frame/XStatusListener.hpp>
30 #include <com/sun/star/frame/XPopupMenuController.hpp>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <vcl/menu.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/settings.hxx>
36 #include <vcl/i18nhelp.hxx>
37 #include <rtl/ref.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <svl/solar.hrc>
40 #include <tools/rcid.h>
41 #include <vcl/image.hxx>
42 #include <svtools/menuoptions.hxx>
43 #include <svtools/popupmenucontrollerbase.hxx>
44 #include <osl/mutex.hxx>
45 #include <boost/scoped_ptr.hpp>
49 #define RID_FMSHELL_CONVERSIONMENU (RID_FORMS_START + 4)
50 #define RID_SVXIMGLIST_FMEXPL (RID_FORMS_START + 0)
52 // Forms - Ids, used to address images from image list
53 #define SID_FMSLOTS_START (SID_SVX_START + 592)
54 #define SID_MORE_FMSLOTS_START (SID_SVX_START + 702)
56 #define SID_FM_CONVERTTO_EDIT (SID_MORE_FMSLOTS_START + 32)
57 #define SID_FM_CONVERTTO_BUTTON (SID_MORE_FMSLOTS_START + 33)
58 #define SID_FM_CONVERTTO_FIXEDTEXT (SID_MORE_FMSLOTS_START + 34)
59 #define SID_FM_CONVERTTO_LISTBOX (SID_MORE_FMSLOTS_START + 35)
60 #define SID_FM_CONVERTTO_CHECKBOX (SID_MORE_FMSLOTS_START + 36)
61 #define SID_FM_CONVERTTO_RADIOBUTTON (SID_MORE_FMSLOTS_START + 37)
62 #define SID_FM_CONVERTTO_GROUPBOX (SID_MORE_FMSLOTS_START + 38)
63 #define SID_FM_CONVERTTO_COMBOBOX (SID_MORE_FMSLOTS_START + 39)
64 #define SID_FM_CONVERTTO_IMAGEBUTTON (SID_MORE_FMSLOTS_START + 41)
65 #define SID_FM_CONVERTTO_FILECONTROL (SID_MORE_FMSLOTS_START + 42)
66 #define SID_FM_CONVERTTO_DATE (SID_MORE_FMSLOTS_START + 43)
67 #define SID_FM_CONVERTTO_TIME (SID_MORE_FMSLOTS_START + 44)
68 #define SID_FM_CONVERTTO_NUMERIC (SID_MORE_FMSLOTS_START + 45)
69 #define SID_FM_CONVERTTO_CURRENCY (SID_MORE_FMSLOTS_START + 46)
70 #define SID_FM_CONVERTTO_PATTERN (SID_MORE_FMSLOTS_START + 47)
71 #define SID_FM_CONVERTTO_IMAGECONTROL (SID_MORE_FMSLOTS_START + 48)
72 #define SID_FM_CONVERTTO_FORMATTED (SID_MORE_FMSLOTS_START + 49)
73 #define SID_FM_CONVERTTO_SCROLLBAR (SID_MORE_FMSLOTS_START + 68)
74 #define SID_FM_CONVERTTO_SPINBUTTON (SID_MORE_FMSLOTS_START + 69)
76 #define SID_FM_DATEFIELD (SID_MORE_FMSLOTS_START + 2)
77 #define SID_FM_TIMEFIELD (SID_MORE_FMSLOTS_START + 3)
78 #define SID_FM_NUMERICFIELD (SID_MORE_FMSLOTS_START + 4)
79 #define SID_FM_CURRENCYFIELD (SID_MORE_FMSLOTS_START + 5)
80 #define SID_FM_PATTERNFIELD (SID_MORE_FMSLOTS_START + 6)
81 #define SID_FM_IMAGECONTROL (SID_MORE_FMSLOTS_START + 8)
82 #define SID_FM_FORMATTEDFIELD (SID_MORE_FMSLOTS_START + 26)
83 #define SID_FM_SCROLLBAR (SID_MORE_FMSLOTS_START + 66)
84 #define SID_FM_SPINBUTTON (SID_MORE_FMSLOTS_START + 67)
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_IMAGEBUTTON (SID_FMSLOTS_START + 12)
94 #define SID_FM_FILECONTROL (SID_FMSLOTS_START + 13)
96 static const sal_Int16 nConvertSlots
[] =
98 SID_FM_CONVERTTO_EDIT
,
99 SID_FM_CONVERTTO_BUTTON
,
100 SID_FM_CONVERTTO_FIXEDTEXT
,
101 SID_FM_CONVERTTO_LISTBOX
,
102 SID_FM_CONVERTTO_CHECKBOX
,
103 SID_FM_CONVERTTO_RADIOBUTTON
,
104 SID_FM_CONVERTTO_GROUPBOX
,
105 SID_FM_CONVERTTO_COMBOBOX
,
106 // SID_FM_CONVERTTO_GRID,
107 SID_FM_CONVERTTO_IMAGEBUTTON
,
108 SID_FM_CONVERTTO_FILECONTROL
,
109 SID_FM_CONVERTTO_DATE
,
110 SID_FM_CONVERTTO_TIME
,
111 SID_FM_CONVERTTO_NUMERIC
,
112 SID_FM_CONVERTTO_CURRENCY
,
113 SID_FM_CONVERTTO_PATTERN
,
114 SID_FM_CONVERTTO_IMAGECONTROL
,
115 SID_FM_CONVERTTO_FORMATTED
,
116 SID_FM_CONVERTTO_SCROLLBAR
,
117 SID_FM_CONVERTTO_SPINBUTTON
120 static const sal_Int16 nCreateSlots
[] =
136 SID_FM_CURRENCYFIELD
,
139 SID_FM_FORMATTEDFIELD
,
144 static const char* aCommands
[] =
146 ".uno:ConvertToEdit",
147 ".uno:ConvertToButton",
148 ".uno:ConvertToFixed",
149 ".uno:ConvertToList",
150 ".uno:ConvertToCheckBox",
151 ".uno:ConvertToRadio",
152 ".uno:ConvertToGroup",
153 ".uno:ConvertToCombo",
154 // ".uno:ConvertToGrid",
155 ".uno:ConvertToImageBtn",
156 ".uno:ConvertToFileControl",
157 ".uno:ConvertToDate",
158 ".uno:ConvertToTime",
159 ".uno:ConvertToNumeric",
160 ".uno:ConvertToCurrency",
161 ".uno:ConvertToPattern",
162 ".uno:ConvertToImageControl",
163 ".uno:ConvertToFormatted",
164 ".uno:ConvertToScrollBar",
165 ".uno:ConvertToSpinButton"
169 using namespace com::sun::star::uno
;
170 using namespace com::sun::star::lang
;
171 using namespace com::sun::star::frame
;
172 using namespace com::sun::star::beans
;
176 class ControlMenuController
: public svt::PopupMenuControllerBase
178 using svt::PopupMenuControllerBase::disposing
;
181 ControlMenuController( const uno::Reference
< uno::XComponentContext
>& xContext
);
182 virtual ~ControlMenuController();
185 virtual OUString SAL_CALL
getImplementationName()
186 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
188 return OUString("com.sun.star.comp.framework.ControlMenuController");
191 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
192 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
194 return cppu::supportsService(this, ServiceName
);
197 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
198 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
200 css::uno::Sequence
< OUString
> aSeq(1);
201 aSeq
[0] = "com.sun.star.frame.PopupMenuController";
205 // XPopupMenuController
206 virtual void SAL_CALL
updatePopupMenu() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
209 virtual void SAL_CALL
initialize( const uno::Sequence
< uno::Any
>& aArguments
) throw (uno::Exception
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
212 virtual void SAL_CALL
statusChanged( const frame::FeatureStateEvent
& Event
) throw ( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
215 virtual void SAL_CALL
itemActivated( const awt::MenuEvent
& rEvent
) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
218 virtual void SAL_CALL
disposing( const lang::EventObject
& Source
) throw ( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
221 virtual void impl_setPopupMenu() SAL_OVERRIDE
;
222 virtual void impl_select(const uno::Reference
< frame::XDispatch
>& _xDispatch
,const util::URL
& aURL
) SAL_OVERRIDE
;
224 class UrlToDispatchMap
: public std::unordered_map
< OUString
,
225 uno::Reference
< frame::XDispatch
>,
227 std::equal_to
< OUString
> >
232 UrlToDispatchMap().swap( *this );// get rid of reserved capacity
236 void updateImagesPopupMenu( PopupMenu
* pPopupMenu
);
237 void fillPopupMenu( uno::Reference
< awt::XPopupMenu
>& rPopupMenu
);
239 bool m_bShowMenuImages
: 1;
240 PopupMenu
* m_pResPopupMenu
;
241 UrlToDispatchMap m_aURLToDispatchMap
;
244 ControlMenuController::ControlMenuController( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
) :
245 svt::PopupMenuControllerBase( xContext
),
248 const StyleSettings
& rSettings
= Application::GetSettings().GetStyleSettings();
249 m_bShowMenuImages
= rSettings
.GetUseImagesInMenus();
253 ControlMenuController::~ControlMenuController()
258 void ControlMenuController::updateImagesPopupMenu( PopupMenu
* pPopupMenu
)
260 boost::scoped_ptr
<ResMgr
> pResMgr(ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag()));
261 ResId
aResId( RID_SVXIMGLIST_FMEXPL
, *pResMgr
);
262 aResId
.SetRT( RSC_IMAGELIST
);
264 if ( pResMgr
->IsAvailable( aResId
))
266 ImageList
aImageList( aResId
);
267 for ( sal_uInt32 i
=0; i
< sizeof(nConvertSlots
)/sizeof(nConvertSlots
[0]); ++i
)
269 // das entsprechende Image dran
270 if ( m_bShowMenuImages
)
271 pPopupMenu
->SetItemImage( nConvertSlots
[i
], aImageList
.GetImage(nCreateSlots
[i
]));
273 pPopupMenu
->SetItemImage( nConvertSlots
[i
], Image() );
279 void ControlMenuController::fillPopupMenu( Reference
< css::awt::XPopupMenu
>& rPopupMenu
)
281 VCLXPopupMenu
* pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( rPopupMenu
));
282 PopupMenu
* pVCLPopupMenu
= 0;
284 SolarMutexGuard aSolarMutexGuard
;
286 resetPopupMenu( rPopupMenu
);
288 pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
290 if ( pVCLPopupMenu
&& m_pResPopupMenu
)
291 *pVCLPopupMenu
= *m_pResPopupMenu
;
295 void SAL_CALL
ControlMenuController::disposing( const EventObject
& ) throw ( RuntimeException
, std::exception
)
297 Reference
< css::awt::XMenuListener
> xHolder(( OWeakObject
*)this, UNO_QUERY
);
299 osl::ResettableMutexGuard
aLock( m_aMutex
);
303 if ( m_xPopupMenu
.is() )
304 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(( OWeakObject
*)this, UNO_QUERY
));
305 m_xPopupMenu
.clear();
306 delete m_pResPopupMenu
;
310 void SAL_CALL
ControlMenuController::statusChanged( const FeatureStateEvent
& Event
) throw ( RuntimeException
, std::exception
)
312 osl::ResettableMutexGuard
aLock( m_aMutex
);
314 sal_uInt16 nMenuId
= 0;
315 for (sal_uInt32 i
=0; i
< sizeof(aCommands
) / sizeof (aCommands
[0]); ++i
)
317 if ( Event
.FeatureURL
.Complete
.equalsAscii( aCommands
[i
] ))
319 nMenuId
= nConvertSlots
[i
];
324 VCLXPopupMenu
* pPopupMenu
= NULL
;
327 pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( m_xPopupMenu
));
331 SolarMutexGuard aSolarMutexGuard
;
333 PopupMenu
* pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
335 if ( !Event
.IsEnabled
&& pVCLPopupMenu
->GetItemPos( nMenuId
) != MENU_ITEM_NOTFOUND
)
336 pVCLPopupMenu
->RemoveItem( pVCLPopupMenu
->GetItemPos( nMenuId
));
337 else if ( Event
.IsEnabled
&& pVCLPopupMenu
->GetItemPos( nMenuId
) == MENU_ITEM_NOTFOUND
)
339 sal_Int16 nSourcePos
= m_pResPopupMenu
->GetItemPos(nMenuId
);
340 sal_Int16 nPrevInSource
= nSourcePos
;
341 sal_uInt16 nPrevInConversion
= MENU_ITEM_NOTFOUND
;
342 while (nPrevInSource
>0)
344 sal_Int16 nPrevId
= m_pResPopupMenu
->GetItemId(--nPrevInSource
);
346 // do we have the source's predecessor in our conversion menu, too ?
347 nPrevInConversion
= pVCLPopupMenu
->GetItemPos( nPrevId
);
348 if ( nPrevInConversion
!= MENU_ITEM_NOTFOUND
)
352 if ( MENU_ITEM_NOTFOUND
== nPrevInConversion
)
353 // none of the items which precede the nSID-slot in the source menu are present in our conversion menu
354 nPrevInConversion
= sal::static_int_cast
< sal_uInt16
>(-1); // put the item at the first position
356 pVCLPopupMenu
->InsertItem( nMenuId
, m_pResPopupMenu
->GetItemText( nMenuId
), m_pResPopupMenu
->GetItemBits( nMenuId
), OString(), ++nPrevInConversion
);
357 pVCLPopupMenu
->SetItemImage( nMenuId
, m_pResPopupMenu
->GetItemImage( nMenuId
));
358 pVCLPopupMenu
->SetHelpId( nMenuId
, m_pResPopupMenu
->GetHelpId( nMenuId
));
364 void ControlMenuController::impl_select(const Reference
< XDispatch
>& /*_xDispatch*/,const ::com::sun::star::util::URL
& aURL
)
366 UrlToDispatchMap::iterator pIter
= m_aURLToDispatchMap
.find( aURL
.Complete
);
367 if ( pIter
!= m_aURLToDispatchMap
.end() )
369 Sequence
<PropertyValue
> aArgs
;
370 Reference
< XDispatch
> xDispatch
= pIter
->second
;
371 if ( xDispatch
.is() )
372 xDispatch
->dispatch( aURL
, aArgs
);
376 void SAL_CALL
ControlMenuController::itemActivated( const css::awt::MenuEvent
& ) throw (RuntimeException
, std::exception
)
378 osl::ResettableMutexGuard
aLock( m_aMutex
);
380 if ( m_xPopupMenu
.is() )
382 SolarMutexGuard aSolarMutexGuard
;
384 // Check if some modes have changed so we have to update our menu images
385 const StyleSettings
& rSettings
= Application::GetSettings().GetStyleSettings();
386 bool bShowMenuImages
= rSettings
.GetUseImagesInMenus();
387 bool bUpdateImages
= (bShowMenuImages
!= m_bShowMenuImages
);
391 m_bShowMenuImages
= bShowMenuImages
;
393 VCLXPopupMenu
* pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXPopupMenu::GetImplementation( m_xPopupMenu
));
396 PopupMenu
* pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
397 if ( pVCLPopupMenu
&& bUpdateImages
)
398 updateImagesPopupMenu( pVCLPopupMenu
);
404 // XPopupMenuController
405 void ControlMenuController::impl_setPopupMenu()
407 if ( m_pResPopupMenu
== 0 )
409 boost::scoped_ptr
<ResMgr
> pResMgr(ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag()));
412 ResId
aResId( RID_FMSHELL_CONVERSIONMENU
, *pResMgr
);
413 aResId
.SetRT( RSC_MENU
);
414 if ( pResMgr
->IsAvailable( aResId
))
416 m_pResPopupMenu
= new PopupMenu( aResId
);
417 updateImagesPopupMenu( m_pResPopupMenu
);
420 } // if ( m_pResPopupMenu == 0 )
423 void SAL_CALL
ControlMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
425 osl::ResettableMutexGuard
aLock( m_aMutex
);
429 if ( m_xFrame
.is() && m_xPopupMenu
.is() )
431 css::util::URL aTargetURL
;
432 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
433 fillPopupMenu( m_xPopupMenu
);
434 m_aURLToDispatchMap
.free();
436 for (sal_uInt32 i
=0; i
<sizeof(aCommands
)/sizeof(aCommands
[0]); ++i
)
438 aTargetURL
.Complete
= OUString::createFromAscii( aCommands
[i
] );
439 m_xURLTransformer
->parseStrict( aTargetURL
);
441 Reference
< XDispatch
> xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
442 if ( xDispatch
.is() )
444 xDispatch
->addStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
445 xDispatch
->removeStatusListener( (static_cast< XStatusListener
* >(this)), aTargetURL
);
446 m_aURLToDispatchMap
.insert( UrlToDispatchMap::value_type( aTargetURL
.Complete
, xDispatch
));
453 void SAL_CALL
ControlMenuController::initialize( const Sequence
< Any
>& aArguments
) throw ( Exception
, RuntimeException
, std::exception
)
455 osl::ResettableMutexGuard
aLock( m_aMutex
);
456 svt::PopupMenuControllerBase::initialize(aArguments
);
462 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
463 com_sun_star_comp_framework_ControlMenuController_get_implementation(
464 css::uno::XComponentContext
*context
,
465 css::uno::Sequence
<css::uno::Any
> const &)
467 return cppu::acquire(new ControlMenuController(context
));
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */