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 "uielement/generictoolbarcontroller.hxx"
22 #include <com/sun/star/util/URLTransformer.hpp>
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/frame/status/ItemStatus.hpp>
28 #include <com/sun/star/frame/status/ItemState.hpp>
29 #include <com/sun/star/frame/status/Visibility.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <svtools/toolboxcontroller.hxx>
33 #include <osl/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/mnemonic.hxx>
36 #include <tools/urlobj.hxx>
37 #include <classes/resource.hrc>
38 #include <classes/fwkresid.hxx>
39 #include <framework/menuconfiguration.hxx>
40 #include <uielement/menubarmanager.hxx>
42 using namespace ::com::sun::star::awt
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::frame
;
47 using namespace ::com::sun::star::frame::status
;
48 using namespace ::com::sun::star::util
;
49 using namespace ::com::sun::star::container
;
54 static bool isEnumCommand( const OUString
& rCommand
)
56 INetURLObject
aURL( rCommand
);
58 if (( aURL
.GetProtocol() == INetProtocol::Uno
) &&
59 ( aURL
.GetURLPath().indexOf( '.' ) != -1))
65 static OUString
getEnumCommand( const OUString
& rCommand
)
67 INetURLObject
aURL( rCommand
);
69 OUString aEnumCommand
;
70 OUString aURLPath
= aURL
.GetURLPath();
71 sal_Int32 nIndex
= aURLPath
.indexOf( '.' );
72 if (( nIndex
> 0 ) && ( nIndex
< aURLPath
.getLength() ))
73 aEnumCommand
= aURLPath
.copy( nIndex
+1 );
78 static OUString
getMasterCommand( const OUString
& rCommand
)
80 OUString
aMasterCommand( rCommand
);
81 INetURLObject
aURL( rCommand
);
82 if ( aURL
.GetProtocol() == INetProtocol::Uno
)
84 sal_Int32 nIndex
= aURL
.GetURLPath().indexOf( '.' );
87 aURL
.SetURLPath( aURL
.GetURLPath().copy( 0, nIndex
) );
88 aMasterCommand
= aURL
.GetMainURL( INetURLObject::NO_DECODE
);
91 return aMasterCommand
;
96 ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XDispatch
> xDispatch
;
97 ::com::sun::star::util::URL aTargetURL
;
98 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> aArgs
;
101 GenericToolbarController::GenericToolbarController( const Reference
< XComponentContext
>& rxContext
,
102 const Reference
< XFrame
>& rFrame
,
105 const OUString
& aCommand
) :
106 svt::ToolboxController( rxContext
, rFrame
, aCommand
)
107 , m_pToolbar( pToolbar
)
109 , m_bEnumCommand( isEnumCommand( aCommand
))
110 , m_bMadeInvisible( false )
111 , m_aEnumCommand( getEnumCommand( aCommand
))
113 if ( m_bEnumCommand
)
114 addStatusListener( getMasterCommand( aCommand
) );
117 GenericToolbarController::~GenericToolbarController()
121 void SAL_CALL
GenericToolbarController::dispose()
122 throw ( RuntimeException
, std::exception
)
124 SolarMutexGuard aSolarMutexGuard
;
126 svt::ToolboxController::dispose();
132 void SAL_CALL
GenericToolbarController::execute( sal_Int16 KeyModifier
)
133 throw ( RuntimeException
, std::exception
)
135 Reference
< XDispatch
> xDispatch
;
136 Reference
< XURLTransformer
> xURLTransformer
;
137 OUString aCommandURL
;
140 SolarMutexGuard aSolarMutexGuard
;
143 throw DisposedException();
145 if ( m_bInitialized
&&
147 !m_aCommandURL
.isEmpty() )
149 xURLTransformer
= URLTransformer::create(m_xContext
);
151 aCommandURL
= m_aCommandURL
;
152 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( m_aCommandURL
);
153 if ( pIter
!= m_aListenerMap
.end() )
154 xDispatch
= pIter
->second
;
158 if ( xDispatch
.is() && xURLTransformer
.is() )
160 com::sun::star::util::URL aTargetURL
;
161 Sequence
<PropertyValue
> aArgs( 1 );
163 // Add key modifier to argument list
164 aArgs
[0].Name
= "KeyModifier";
165 aArgs
[0].Value
<<= KeyModifier
;
167 aTargetURL
.Complete
= aCommandURL
;
168 xURLTransformer
->parseStrict( aTargetURL
);
170 // Execute dispatch asynchronously
171 ExecuteInfo
* pExecuteInfo
= new ExecuteInfo
;
172 pExecuteInfo
->xDispatch
= xDispatch
;
173 pExecuteInfo
->aTargetURL
= aTargetURL
;
174 pExecuteInfo
->aArgs
= aArgs
;
175 Application::PostUserEvent( LINK(0, GenericToolbarController
, ExecuteHdl_Impl
), pExecuteInfo
);
179 void GenericToolbarController::statusChanged( const FeatureStateEvent
& Event
)
180 throw ( RuntimeException
, std::exception
)
182 SolarMutexGuard aSolarMutexGuard
;
189 m_pToolbar
->EnableItem( m_nID
, Event
.IsEnabled
);
191 ToolBoxItemBits nItemBits
= m_pToolbar
->GetItemBits( m_nID
);
192 nItemBits
&= ~ToolBoxItemBits::CHECKABLE
;
193 TriState eTri
= TRISTATE_FALSE
;
197 ItemStatus aItemState
;
198 Visibility aItemVisibility
;
200 if (( Event
.State
>>= bValue
) && !m_bEnumCommand
)
202 // Boolean, treat it as checked/unchecked
203 if ( m_bMadeInvisible
)
204 m_pToolbar
->ShowItem( m_nID
, true );
205 m_pToolbar
->CheckItem( m_nID
, bValue
);
207 eTri
= TRISTATE_TRUE
;
208 nItemBits
|= ToolBoxItemBits::CHECKABLE
;
210 else if ( Event
.State
>>= aStrValue
)
212 if ( m_bEnumCommand
)
214 if ( aStrValue
== m_aEnumCommand
)
219 m_pToolbar
->CheckItem( m_nID
, bValue
);
221 eTri
= TRISTATE_TRUE
;
222 nItemBits
|= ToolBoxItemBits::CHECKABLE
;
226 // Replacement for place holders
227 if ( aStrValue
.startsWith("($1)") )
229 OUString
aTmp(FwkResId(STR_UPDATEDOC
));
231 aTmp
+= aStrValue
.copy( 4 );
234 else if ( aStrValue
.startsWith("($2)") )
236 OUString
aTmp(FWK_RESSTR(STR_CLOSEDOC_ANDRETURN
));
237 aTmp
+= aStrValue
.copy( 4 );
240 else if ( aStrValue
.startsWith("($3)") )
242 OUString
aTmp(FWK_RESSTR(STR_SAVECOPYDOC
));
243 aTmp
+= aStrValue
.copy( 4 );
246 m_pToolbar
->SetItemText( m_nID
, aStrValue
);
247 m_pToolbar
->SetQuickHelpText( m_nID
, aStrValue
);
250 if ( m_bMadeInvisible
)
251 m_pToolbar
->ShowItem( m_nID
, true );
253 else if (( Event
.State
>>= aItemState
) && !m_bEnumCommand
)
255 eTri
= TRISTATE_INDET
;
256 nItemBits
|= ToolBoxItemBits::CHECKABLE
;
257 if ( m_bMadeInvisible
)
258 m_pToolbar
->ShowItem( m_nID
, true );
260 else if ( Event
.State
>>= aItemVisibility
)
262 m_pToolbar
->ShowItem( m_nID
, aItemVisibility
.bVisible
);
263 m_bMadeInvisible
= !aItemVisibility
.bVisible
;
265 else if ( m_bMadeInvisible
)
266 m_pToolbar
->ShowItem( m_nID
, true );
268 m_pToolbar
->SetItemState( m_nID
, eTri
);
269 m_pToolbar
->SetItemBits( m_nID
, nItemBits
);
273 IMPL_STATIC_LINK( GenericToolbarController
, ExecuteHdl_Impl
, ExecuteInfo
*, pExecuteInfo
)
275 SolarMutexReleaser aReleaser
;
278 // Asynchronous execution as this can lead to our own destruction!
279 // Framework can recycle our current frame and the layout manager disposes all user interface
280 // elements if a component gets detached from its frame!
281 pExecuteInfo
->xDispatch
->dispatch( pExecuteInfo
->aTargetURL
, pExecuteInfo
->aArgs
);
283 catch ( const Exception
& )
291 MenuToolbarController::MenuToolbarController( const Reference
< XComponentContext
>& rxContext
,
292 const Reference
< XFrame
>& rFrame
,
295 const OUString
& aCommand
,
296 const OUString
& aModuleIdentifier
,
297 const Reference
< XIndexAccess
>& xMenuDesc
)
298 : GenericToolbarController( rxContext
, rFrame
, pToolBar
, nID
, aCommand
),
299 m_xMenuDesc( xMenuDesc
),
301 m_aModuleIdentifier( aModuleIdentifier
)
305 MenuToolbarController::~MenuToolbarController()
309 if ( m_xMenuManager
.is() )
310 m_xMenuManager
->dispose();
312 catch( const Exception
& ) {}
321 class Toolbarmenu
: public ::PopupMenu
325 virtual ~Toolbarmenu();
328 Toolbarmenu::Toolbarmenu()
330 SAL_INFO("fwk.uielement", "contstructing Toolbarmenu " << this);
333 Toolbarmenu::~Toolbarmenu()
335 SAL_INFO("fwk.uielement", "destructing Toolbarmenu " << this);
338 void SAL_CALL
MenuToolbarController::click() throw (RuntimeException
, std::exception
)
343 Reference
< XWindow
> SAL_CALL
344 MenuToolbarController::createPopupWindow() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
348 Reference
< XDispatchProvider
> xDispatch
;
349 Reference
< XURLTransformer
> xURLTransformer
= URLTransformer::create( m_xContext
);
350 pMenu
= new Toolbarmenu();
351 m_xMenuManager
.set( new MenuBarManager( m_xContext
, m_xFrame
, xURLTransformer
, xDispatch
, m_aModuleIdentifier
, pMenu
, true, true ) );
352 if (m_xMenuManager
.is())
354 MenuBarManager
& rMgr
= dynamic_cast<MenuBarManager
&>(*m_xMenuManager
.get());
355 rMgr
.SetItemContainer(m_xMenuDesc
);
359 if ( !pMenu
|| !m_pToolbar
)
362 OSL_ENSURE ( pMenu
->GetItemCount(), "Empty PopupMenu!" );
364 ::Rectangle
aRect( m_pToolbar
->GetItemRect( m_nID
) );
365 pMenu
->Execute( m_pToolbar
, aRect
, PopupMenuFlags::ExecuteDown
);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */