110b918a5SAriel Constenla-Haile /**************************************************************
210b918a5SAriel Constenla-Haile  *
310b918a5SAriel Constenla-Haile  * Licensed to the Apache Software Foundation (ASF) under one
410b918a5SAriel Constenla-Haile  * or more contributor license agreements.  See the NOTICE file
510b918a5SAriel Constenla-Haile  * distributed with this work for additional information
610b918a5SAriel Constenla-Haile  * regarding copyright ownership.  The ASF licenses this file
710b918a5SAriel Constenla-Haile  * to you under the Apache License, Version 2.0 (the
810b918a5SAriel Constenla-Haile  * "License"); you may not use this file except in compliance
910b918a5SAriel Constenla-Haile  * with the License.  You may obtain a copy of the License at
1010b918a5SAriel Constenla-Haile  *
1110b918a5SAriel Constenla-Haile  *   http://www.apache.org/licenses/LICENSE-2.0
1210b918a5SAriel Constenla-Haile  *
1310b918a5SAriel Constenla-Haile  * Unless required by applicable law or agreed to in writing,
1410b918a5SAriel Constenla-Haile  * software distributed under the License is distributed on an
1510b918a5SAriel Constenla-Haile  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1610b918a5SAriel Constenla-Haile  * KIND, either express or implied.  See the License for the
1710b918a5SAriel Constenla-Haile  * specific language governing permissions and limitations
1810b918a5SAriel Constenla-Haile  * under the License.
1910b918a5SAriel Constenla-Haile  *
2010b918a5SAriel Constenla-Haile  *************************************************************/
2110b918a5SAriel Constenla-Haile 
22*7a164331Smseidel 
23*7a164331Smseidel 
2410b918a5SAriel Constenla-Haile // MARKER(update_precomp.py): autogen include statement, do not remove
2510b918a5SAriel Constenla-Haile #include "precompiled_framework.hxx"
2610b918a5SAriel Constenla-Haile 
2710b918a5SAriel Constenla-Haile #include <uielement/popuptoolbarcontroller.hxx>
287ca83afaSAriel Constenla-Haile #include <framework/menuconfiguration.hxx>
297ca83afaSAriel Constenla-Haile #include <toolkit/awt/vclxmenu.hxx>
3010b918a5SAriel Constenla-Haile #include <comphelper/processfactory.hxx>
3110b918a5SAriel Constenla-Haile #include <svtools/imagemgr.hxx>
3210b918a5SAriel Constenla-Haile #include <svtools/miscopt.hxx>
3310b918a5SAriel Constenla-Haile #include <toolkit/helper/vclunohelper.hxx>
3410b918a5SAriel Constenla-Haile #include <tools/urlobj.hxx>
3510b918a5SAriel Constenla-Haile #include <unotools/moduleoptions.hxx>
3610b918a5SAriel Constenla-Haile #include <vcl/svapp.hxx>
3710b918a5SAriel Constenla-Haile #include <vcl/toolbox.hxx>
3810b918a5SAriel Constenla-Haile #include <vos/mutex.hxx>
3910b918a5SAriel Constenla-Haile 
4010b918a5SAriel Constenla-Haile #include <com/sun/star/awt/PopupMenuDirection.hpp>
4110b918a5SAriel Constenla-Haile #include <com/sun/star/frame/PopupMenuControllerFactory.hpp>
427ca83afaSAriel Constenla-Haile #include <com/sun/star/frame/XDispatchProvider.hpp>
4310b918a5SAriel Constenla-Haile 
4410b918a5SAriel Constenla-Haile #define UNO_COMMAND_RECENT_FILE_LIST    ".uno:RecentFileList"
457ca83afaSAriel Constenla-Haile #define SFX_REFERER_USER                "private:user"
4610b918a5SAriel Constenla-Haile 
4710b918a5SAriel Constenla-Haile using rtl::OUString;
4810b918a5SAriel Constenla-Haile namespace css = ::com::sun::star;
4910b918a5SAriel Constenla-Haile 
5010b918a5SAriel Constenla-Haile namespace framework
5110b918a5SAriel Constenla-Haile {
5210b918a5SAriel Constenla-Haile 
PopupMenuToolbarController(const css::uno::Reference<css::uno::XComponentContext> & xContext,const OUString & rPopupCommand)5310b918a5SAriel Constenla-Haile PopupMenuToolbarController::PopupMenuToolbarController(
5410b918a5SAriel Constenla-Haile     const css::uno::Reference< css::uno::XComponentContext >& xContext,
5510b918a5SAriel Constenla-Haile     const OUString &rPopupCommand )
5610b918a5SAriel Constenla-Haile     : svt::ToolboxController()
5710b918a5SAriel Constenla-Haile     , m_xContext( xContext )
5810b918a5SAriel Constenla-Haile     , m_bHasController( sal_False )
5910b918a5SAriel Constenla-Haile     , m_aPopupCommand( rPopupCommand )
6010b918a5SAriel Constenla-Haile {
6110b918a5SAriel Constenla-Haile }
6210b918a5SAriel Constenla-Haile 
~PopupMenuToolbarController()6310b918a5SAriel Constenla-Haile PopupMenuToolbarController::~PopupMenuToolbarController()
6410b918a5SAriel Constenla-Haile {
6510b918a5SAriel Constenla-Haile }
6610b918a5SAriel Constenla-Haile 
dispose()6710b918a5SAriel Constenla-Haile void SAL_CALL PopupMenuToolbarController::dispose()
6810b918a5SAriel Constenla-Haile throw ( css::uno::RuntimeException )
6910b918a5SAriel Constenla-Haile {
7010b918a5SAriel Constenla-Haile     svt::ToolboxController::dispose();
7110b918a5SAriel Constenla-Haile 
7210b918a5SAriel Constenla-Haile     osl::MutexGuard aGuard( m_aMutex );
7310b918a5SAriel Constenla-Haile     if( m_xPopupMenuController.is() )
7410b918a5SAriel Constenla-Haile     {
7510b918a5SAriel Constenla-Haile         css::uno::Reference< css::lang::XComponent > xComponent(
7610b918a5SAriel Constenla-Haile             m_xPopupMenuController, css::uno::UNO_QUERY );
7710b918a5SAriel Constenla-Haile         if( xComponent.is() )
7810b918a5SAriel Constenla-Haile         {
7910b918a5SAriel Constenla-Haile             try
8010b918a5SAriel Constenla-Haile             {
8110b918a5SAriel Constenla-Haile                 xComponent->dispose();
8210b918a5SAriel Constenla-Haile             }
8310b918a5SAriel Constenla-Haile             catch (...)
8410b918a5SAriel Constenla-Haile             {}
8510b918a5SAriel Constenla-Haile         }
8610b918a5SAriel Constenla-Haile         m_xPopupMenuController.clear();
8710b918a5SAriel Constenla-Haile     }
8810b918a5SAriel Constenla-Haile 
8910b918a5SAriel Constenla-Haile     m_xContext.clear();
9010b918a5SAriel Constenla-Haile     m_xPopupMenuFactory.clear();
9110b918a5SAriel Constenla-Haile     m_xPopupMenu.clear();
9210b918a5SAriel Constenla-Haile }
9310b918a5SAriel Constenla-Haile 
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)9410b918a5SAriel Constenla-Haile void SAL_CALL PopupMenuToolbarController::initialize(
9510b918a5SAriel Constenla-Haile     const css::uno::Sequence< css::uno::Any >& aArguments )
9610b918a5SAriel Constenla-Haile throw ( css::uno::Exception, css::uno::RuntimeException )
9710b918a5SAriel Constenla-Haile {
9810b918a5SAriel Constenla-Haile     ToolboxController::initialize( aArguments );
9910b918a5SAriel Constenla-Haile 
10010b918a5SAriel Constenla-Haile     osl::MutexGuard aGuard( m_aMutex );
10110b918a5SAriel Constenla-Haile     if ( !m_aPopupCommand.getLength() )
10210b918a5SAriel Constenla-Haile         m_aPopupCommand = m_aCommandURL;
10310b918a5SAriel Constenla-Haile 
10410b918a5SAriel Constenla-Haile     try
10510b918a5SAriel Constenla-Haile     {
10610b918a5SAriel Constenla-Haile         m_xPopupMenuFactory.set(
10710b918a5SAriel Constenla-Haile             css::frame::PopupMenuControllerFactory::create( m_xContext ) );
10810b918a5SAriel Constenla-Haile         m_bHasController = m_xPopupMenuFactory->hasController(
10910b918a5SAriel Constenla-Haile             m_aPopupCommand, getModuleName() );
11010b918a5SAriel Constenla-Haile     }
11110b918a5SAriel Constenla-Haile     catch (const css::uno::Exception& e)
11210b918a5SAriel Constenla-Haile     {
11310b918a5SAriel Constenla-Haile         OSL_TRACE( "PopupMenuToolbarController - caught an exception! %s",
11410b918a5SAriel Constenla-Haile                    rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
11510b918a5SAriel Constenla-Haile         (void) e;
11610b918a5SAriel Constenla-Haile     }
11710b918a5SAriel Constenla-Haile 
11810b918a5SAriel Constenla-Haile     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
11910b918a5SAriel Constenla-Haile     ToolBox* pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
12010b918a5SAriel Constenla-Haile     if ( pToolBox )
12110b918a5SAriel Constenla-Haile     {
12210b918a5SAriel Constenla-Haile         ToolBoxItemBits nCurStyle( pToolBox->GetItemBits( m_nToolBoxId ) );
12310b918a5SAriel Constenla-Haile         ToolBoxItemBits nSetStyle( getDropDownStyle() );
12410b918a5SAriel Constenla-Haile         pToolBox->SetItemBits( m_nToolBoxId,
12510b918a5SAriel Constenla-Haile                                m_bHasController ?
12610b918a5SAriel Constenla-Haile                                     nCurStyle | nSetStyle :
12710b918a5SAriel Constenla-Haile                                     nCurStyle & ~nSetStyle );
12810b918a5SAriel Constenla-Haile     }
12910b918a5SAriel Constenla-Haile 
13010b918a5SAriel Constenla-Haile }
13110b918a5SAriel Constenla-Haile 
13210b918a5SAriel Constenla-Haile void SAL_CALL
statusChanged(const css::frame::FeatureStateEvent & rEvent)13310b918a5SAriel Constenla-Haile PopupMenuToolbarController::statusChanged(
13410b918a5SAriel Constenla-Haile     const css::frame::FeatureStateEvent& rEvent )
13510b918a5SAriel Constenla-Haile     throw ( css::uno::RuntimeException )
13610b918a5SAriel Constenla-Haile {
13710b918a5SAriel Constenla-Haile     // TODO move to base class
13810b918a5SAriel Constenla-Haile 
13910b918a5SAriel Constenla-Haile     svt::ToolboxController::statusChanged( rEvent );
14010b918a5SAriel Constenla-Haile     enable( rEvent.IsEnabled );
14110b918a5SAriel Constenla-Haile }
14210b918a5SAriel Constenla-Haile 
14310b918a5SAriel Constenla-Haile css::uno::Reference< css::awt::XWindow > SAL_CALL
createPopupWindow()14410b918a5SAriel Constenla-Haile PopupMenuToolbarController::createPopupWindow()
14510b918a5SAriel Constenla-Haile     throw ( css::uno::RuntimeException )
14610b918a5SAriel Constenla-Haile {
14710b918a5SAriel Constenla-Haile     css::uno::Reference< css::awt::XWindow > xRet;
14810b918a5SAriel Constenla-Haile 
14910b918a5SAriel Constenla-Haile     osl::MutexGuard aGuard( m_aMutex );
15010b918a5SAriel Constenla-Haile     if ( !m_bHasController )
15110b918a5SAriel Constenla-Haile         return xRet;
15210b918a5SAriel Constenla-Haile 
15310b918a5SAriel Constenla-Haile     createPopupMenuController();
15410b918a5SAriel Constenla-Haile 
15510b918a5SAriel Constenla-Haile     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
15610b918a5SAriel Constenla-Haile     ToolBox* pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
15710b918a5SAriel Constenla-Haile     if ( !pToolBox )
15810b918a5SAriel Constenla-Haile         return xRet;
15910b918a5SAriel Constenla-Haile 
16010b918a5SAriel Constenla-Haile     pToolBox->SetItemDown( m_nToolBoxId, sal_True );
1615bd54fd8SAriel Constenla-Haile     WindowAlign eAlign( pToolBox->GetAlign() );
16210b918a5SAriel Constenla-Haile     sal_uInt16 nId = m_xPopupMenu->execute(
16310b918a5SAriel Constenla-Haile         css::uno::Reference< css::awt::XWindowPeer >( getParent(), css::uno::UNO_QUERY ),
16410b918a5SAriel Constenla-Haile         VCLUnoHelper::ConvertToAWTRect( pToolBox->GetItemRect( m_nToolBoxId ) ),
1655bd54fd8SAriel Constenla-Haile         ( eAlign == WINDOWALIGN_TOP || eAlign == WINDOWALIGN_BOTTOM ) ?
1665bd54fd8SAriel Constenla-Haile             css::awt::PopupMenuDirection::EXECUTE_DOWN :
1675bd54fd8SAriel Constenla-Haile             css::awt::PopupMenuDirection::EXECUTE_RIGHT );
16810b918a5SAriel Constenla-Haile     pToolBox->SetItemDown( m_nToolBoxId, sal_False );
16910b918a5SAriel Constenla-Haile 
17010b918a5SAriel Constenla-Haile     if ( nId )
17110b918a5SAriel Constenla-Haile         functionExecuted( m_xPopupMenu->getCommand( nId ) );
17210b918a5SAriel Constenla-Haile 
17310b918a5SAriel Constenla-Haile     return xRet;
17410b918a5SAriel Constenla-Haile }
17510b918a5SAriel Constenla-Haile 
functionExecuted(const OUString &)17610b918a5SAriel Constenla-Haile void PopupMenuToolbarController::functionExecuted( const OUString &/*rCommand*/)
17710b918a5SAriel Constenla-Haile {
17810b918a5SAriel Constenla-Haile }
17910b918a5SAriel Constenla-Haile 
getDropDownStyle() const18010b918a5SAriel Constenla-Haile sal_uInt16 PopupMenuToolbarController::getDropDownStyle() const
18110b918a5SAriel Constenla-Haile {
18210b918a5SAriel Constenla-Haile     return TIB_DROPDOWN;
18310b918a5SAriel Constenla-Haile }
18410b918a5SAriel Constenla-Haile 
createPopupMenuController()18510b918a5SAriel Constenla-Haile void PopupMenuToolbarController::createPopupMenuController()
18610b918a5SAriel Constenla-Haile {
18710b918a5SAriel Constenla-Haile     if( !m_bHasController )
18810b918a5SAriel Constenla-Haile         return;
18910b918a5SAriel Constenla-Haile 
19010b918a5SAriel Constenla-Haile     if ( !m_xPopupMenuController.is() )
19110b918a5SAriel Constenla-Haile     {
19210b918a5SAriel Constenla-Haile         css::uno::Sequence< css::uno::Any > aArgs( 2 );
19310b918a5SAriel Constenla-Haile         css::beans::PropertyValue aProp;
19410b918a5SAriel Constenla-Haile 
19510b918a5SAriel Constenla-Haile         aProp.Name = DECLARE_ASCII( "Frame" );
19610b918a5SAriel Constenla-Haile         aProp.Value <<= m_xFrame;
19710b918a5SAriel Constenla-Haile         aArgs[0] <<= aProp;
19810b918a5SAriel Constenla-Haile 
19910b918a5SAriel Constenla-Haile         aProp.Name = DECLARE_ASCII( "ModuleIdentifier" );
20010b918a5SAriel Constenla-Haile         aProp.Value <<= getModuleName();
20110b918a5SAriel Constenla-Haile         aArgs[1] <<= aProp;
20210b918a5SAriel Constenla-Haile         try
20310b918a5SAriel Constenla-Haile         {
20410b918a5SAriel Constenla-Haile             m_xPopupMenu.set(
20510b918a5SAriel Constenla-Haile                 m_xContext->getServiceManager()->createInstanceWithContext(
20610b918a5SAriel Constenla-Haile                     DECLARE_ASCII( "com.sun.star.awt.PopupMenu" ), m_xContext ),
20710b918a5SAriel Constenla-Haile                         css::uno::UNO_QUERY_THROW );
20810b918a5SAriel Constenla-Haile             m_xPopupMenuController.set(
20910b918a5SAriel Constenla-Haile                 m_xPopupMenuFactory->createInstanceWithArgumentsAndContext(
21010b918a5SAriel Constenla-Haile                     m_aPopupCommand, aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
21110b918a5SAriel Constenla-Haile 
21210b918a5SAriel Constenla-Haile             m_xPopupMenuController->setPopupMenu( m_xPopupMenu );
21310b918a5SAriel Constenla-Haile         }
21410b918a5SAriel Constenla-Haile         catch ( const css::uno::Exception &e )
21510b918a5SAriel Constenla-Haile         {
21610b918a5SAriel Constenla-Haile             m_xPopupMenu.clear();
21710b918a5SAriel Constenla-Haile             OSL_TRACE( "PopupMenuToolbarController - caught an exception! %s",
21810b918a5SAriel Constenla-Haile                        rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
21910b918a5SAriel Constenla-Haile             (void) e;
22010b918a5SAriel Constenla-Haile         }
22110b918a5SAriel Constenla-Haile     }
22210b918a5SAriel Constenla-Haile }
22310b918a5SAriel Constenla-Haile 
22410b918a5SAriel Constenla-Haile DEFINE_XSERVICEINFO_MULTISERVICE_2( WizardsToolbarController,
22510b918a5SAriel Constenla-Haile                                     ::cppu::OWeakObject,
22610b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("com.sun.star.frame.ToolbarController"),
22710b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("org.apache.openoffice.comp.framework.WizardsToolbarController")
22810b918a5SAriel Constenla-Haile                                    )
22910b918a5SAriel Constenla-Haile 
23010b918a5SAriel Constenla-Haile DEFINE_INIT_SERVICE( WizardsToolbarController, {} )
23110b918a5SAriel Constenla-Haile 
WizardsToolbarController(const css::uno::Reference<css::uno::XComponentContext> & xContext)23210b918a5SAriel Constenla-Haile WizardsToolbarController::WizardsToolbarController(
23310b918a5SAriel Constenla-Haile     const css::uno::Reference< css::uno::XComponentContext >& xContext )
23410b918a5SAriel Constenla-Haile     : PopupMenuToolbarController( xContext )
23510b918a5SAriel Constenla-Haile {
23610b918a5SAriel Constenla-Haile }
23710b918a5SAriel Constenla-Haile 
getDropDownStyle() const23810b918a5SAriel Constenla-Haile sal_uInt16 WizardsToolbarController::getDropDownStyle() const
23910b918a5SAriel Constenla-Haile {
24010b918a5SAriel Constenla-Haile     return TIB_DROPDOWNONLY;
24110b918a5SAriel Constenla-Haile }
24210b918a5SAriel Constenla-Haile 
24310b918a5SAriel Constenla-Haile DEFINE_XSERVICEINFO_MULTISERVICE_2( OpenToolbarController,
24410b918a5SAriel Constenla-Haile                                     ::cppu::OWeakObject,
24510b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("com.sun.star.frame.ToolbarController"),
24610b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("org.apache.openoffice.comp.framework.OpenToolbarController")
24710b918a5SAriel Constenla-Haile                                    )
24810b918a5SAriel Constenla-Haile 
24910b918a5SAriel Constenla-Haile DEFINE_INIT_SERVICE( OpenToolbarController, {} )
25010b918a5SAriel Constenla-Haile 
OpenToolbarController(const css::uno::Reference<css::uno::XComponentContext> & xContext)25110b918a5SAriel Constenla-Haile OpenToolbarController::OpenToolbarController(
25210b918a5SAriel Constenla-Haile     const css::uno::Reference< css::uno::XComponentContext >& xContext )
25310b918a5SAriel Constenla-Haile     : PopupMenuToolbarController( xContext, DECLARE_ASCII( UNO_COMMAND_RECENT_FILE_LIST ) )
25410b918a5SAriel Constenla-Haile {
25510b918a5SAriel Constenla-Haile }
25610b918a5SAriel Constenla-Haile 
25710b918a5SAriel Constenla-Haile 
25810b918a5SAriel Constenla-Haile DEFINE_XSERVICEINFO_MULTISERVICE_2( NewToolbarController,
25910b918a5SAriel Constenla-Haile                                     ::cppu::OWeakObject,
26010b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("com.sun.star.frame.ToolbarController"),
26110b918a5SAriel Constenla-Haile                                     DECLARE_ASCII("org.apache.openoffice.comp.framework.NewToolbarController")
26210b918a5SAriel Constenla-Haile                                    )
26310b918a5SAriel Constenla-Haile 
26410b918a5SAriel Constenla-Haile DEFINE_INIT_SERVICE( NewToolbarController, {} )
26510b918a5SAriel Constenla-Haile 
NewToolbarController(const css::uno::Reference<css::uno::XComponentContext> & xContext)26610b918a5SAriel Constenla-Haile NewToolbarController::NewToolbarController(
26710b918a5SAriel Constenla-Haile     const css::uno::Reference< css::uno::XComponentContext >& xContext )
26810b918a5SAriel Constenla-Haile     : PopupMenuToolbarController( xContext )
26910b918a5SAriel Constenla-Haile {
27010b918a5SAriel Constenla-Haile }
27110b918a5SAriel Constenla-Haile 
27289d8a215SAriel Constenla-Haile void SAL_CALL
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)27389d8a215SAriel Constenla-Haile NewToolbarController::initialize(
27489d8a215SAriel Constenla-Haile     const css::uno::Sequence< css::uno::Any >& aArguments )
27589d8a215SAriel Constenla-Haile throw ( css::uno::Exception, css::uno::RuntimeException )
27689d8a215SAriel Constenla-Haile {
27789d8a215SAriel Constenla-Haile     PopupMenuToolbarController::initialize( aArguments );
27889d8a215SAriel Constenla-Haile 
27989d8a215SAriel Constenla-Haile     osl::MutexGuard aGuard( m_aMutex );
28089d8a215SAriel Constenla-Haile     createPopupMenuController();
28189d8a215SAriel Constenla-Haile }
28289d8a215SAriel Constenla-Haile 
28310b918a5SAriel Constenla-Haile void SAL_CALL
statusChanged(const css::frame::FeatureStateEvent & rEvent)28410b918a5SAriel Constenla-Haile NewToolbarController::statusChanged(
28510b918a5SAriel Constenla-Haile     const css::frame::FeatureStateEvent& rEvent )
28610b918a5SAriel Constenla-Haile     throw ( css::uno::RuntimeException )
28710b918a5SAriel Constenla-Haile {
28810b918a5SAriel Constenla-Haile     if ( rEvent.IsEnabled )
28910b918a5SAriel Constenla-Haile     {
29010b918a5SAriel Constenla-Haile         OUString aState;
29110b918a5SAriel Constenla-Haile         rEvent.State >>= aState;
29210b918a5SAriel Constenla-Haile         // set the image even if the state is not a string
29310b918a5SAriel Constenla-Haile         // this will set the image of the default module
29410b918a5SAriel Constenla-Haile         setItemImage( aState );
29510b918a5SAriel Constenla-Haile     }
29610b918a5SAriel Constenla-Haile 
29710b918a5SAriel Constenla-Haile     enable( rEvent.IsEnabled );
29810b918a5SAriel Constenla-Haile }
29910b918a5SAriel Constenla-Haile 
3007ca83afaSAriel Constenla-Haile void SAL_CALL
execute(sal_Int16)3017ca83afaSAriel Constenla-Haile NewToolbarController::execute( sal_Int16 /*KeyModifier*/ )
3027ca83afaSAriel Constenla-Haile     throw ( css::uno::RuntimeException )
3037ca83afaSAriel Constenla-Haile {
3047ca83afaSAriel Constenla-Haile     osl::MutexGuard aGuard( m_aMutex );
3057ca83afaSAriel Constenla-Haile     if ( !m_aLastURL.getLength() )
3067ca83afaSAriel Constenla-Haile         return;
3077ca83afaSAriel Constenla-Haile 
3087ca83afaSAriel Constenla-Haile     OUString aTarget( RTL_CONSTASCII_USTRINGPARAM( "_default" ) );
3097ca83afaSAriel Constenla-Haile     if ( m_xPopupMenu.is() )
3107ca83afaSAriel Constenla-Haile     {
3117ca83afaSAriel Constenla-Haile         // TODO investigate how to wrap Get/SetUserValue in css::awt::XMenu
3127ca83afaSAriel Constenla-Haile         MenuConfiguration::Attributes* pMenuAttributes( 0 );
313*7a164331Smseidel         VCLXPopupMenu* pTkPopupMenu =
3149b1ae0e5SAriel Constenla-Haile             ( VCLXPopupMenu * ) VCLXMenu::GetImplementation( m_xPopupMenu );
3157ca83afaSAriel Constenla-Haile 
3167ca83afaSAriel Constenla-Haile         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
3177ca83afaSAriel Constenla-Haile         PopupMenu* pVCLPopupMenu = dynamic_cast< PopupMenu * >( pTkPopupMenu->GetMenu() );
3187ca83afaSAriel Constenla-Haile         if ( pVCLPopupMenu )
3197ca83afaSAriel Constenla-Haile             pMenuAttributes = reinterpret_cast< MenuConfiguration::Attributes* >(
3207ca83afaSAriel Constenla-Haile                 pVCLPopupMenu->GetUserValue( pVCLPopupMenu->GetCurItemId() ) );
3217ca83afaSAriel Constenla-Haile 
3227ca83afaSAriel Constenla-Haile         if ( pMenuAttributes )
3237ca83afaSAriel Constenla-Haile             aTarget = pMenuAttributes->aTargetFrame;
3247ca83afaSAriel Constenla-Haile     }
3257ca83afaSAriel Constenla-Haile 
3267ca83afaSAriel Constenla-Haile     css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
327*7a164331Smseidel     aArgs[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ) );
3287ca83afaSAriel Constenla-Haile     aArgs[0].Value <<= OUString( RTL_CONSTASCII_USTRINGPARAM( SFX_REFERER_USER ) );
3297ca83afaSAriel Constenla-Haile 
3307ca83afaSAriel Constenla-Haile     dispatchCommand( m_aLastURL, aArgs, aTarget );
3317ca83afaSAriel Constenla-Haile }
3327ca83afaSAriel Constenla-Haile 
functionExecuted(const OUString & rCommand)33310b918a5SAriel Constenla-Haile void NewToolbarController::functionExecuted( const OUString &rCommand )
33410b918a5SAriel Constenla-Haile {
33510b918a5SAriel Constenla-Haile     setItemImage( rCommand );
33610b918a5SAriel Constenla-Haile }
33710b918a5SAriel Constenla-Haile 
33810b918a5SAriel Constenla-Haile /**
339*7a164331Smseidel     it returns the existing state of the given URL in the popupmenu of this toolbox control.
34010b918a5SAriel Constenla-Haile 
34110b918a5SAriel Constenla-Haile     If the given URL can be located as an action command of one menu item of the
3424e7d57d8Smseidel     popup menu of this control, we return sal_True. Otherwise we return sal_False.
34310b918a5SAriel Constenla-Haile     Further we return a fallback URL, in case we have to return sal_False. Because
344*7a164331Smseidel     the outside code must select a valid item of the popup menu every time...
3454e7d57d8Smseidel     and we define it here. By the way this method was written to handle
34610b918a5SAriel Constenla-Haile     error situations gracefully. E.g. it can be called during creation time
34710b918a5SAriel Constenla-Haile     but then we have no valid menu. For this case we know another fallback URL.
34810b918a5SAriel Constenla-Haile     Then we return the private:factory/ URL of the default factory.
34910b918a5SAriel Constenla-Haile 
35010b918a5SAriel Constenla-Haile     @param  rPopupMenu
351*7a164331Smseidel                 points to the popup menu, on which item we try to locate the given URL
35207a3d7f1SPedro Giffuni                 Can be NULL! Search will be suppressed then.
35310b918a5SAriel Constenla-Haile 
35410b918a5SAriel Constenla-Haile     @param  sURL
35510b918a5SAriel Constenla-Haile                 the URL for searching
35610b918a5SAriel Constenla-Haile 
35710b918a5SAriel Constenla-Haile     @param  sFallback
35810b918a5SAriel Constenla-Haile                 contains the fallback URL in case we return FALSE
35910b918a5SAriel Constenla-Haile                 Must point to valid memory!
36010b918a5SAriel Constenla-Haile 
36110b918a5SAriel Constenla-Haile     @param  aImage
36210b918a5SAriel Constenla-Haile                 contains the image of the menu for the URL.
36310b918a5SAriel Constenla-Haile 
36410b918a5SAriel Constenla-Haile     @return sal_True - if URL could be located as an item of the popup menu.
3654e7d57d8Smseidel             sal_False - otherwise.
36610b918a5SAriel Constenla-Haile */
Impl_ExistURLInMenu(const css::uno::Reference<css::awt::XPopupMenu> & rPopupMenu,OUString & sURL,OUString & sFallback,Image & aImage)36710b918a5SAriel Constenla-Haile static sal_Bool Impl_ExistURLInMenu(
36810b918a5SAriel Constenla-Haile     const css::uno::Reference< css::awt::XPopupMenu > &rPopupMenu,
36910b918a5SAriel Constenla-Haile     OUString &sURL,
37010b918a5SAriel Constenla-Haile     OUString &sFallback,
37110b918a5SAriel Constenla-Haile     Image &aImage )
37210b918a5SAriel Constenla-Haile {
37310b918a5SAriel Constenla-Haile     sal_Bool bValidFallback( sal_False );
37410b918a5SAriel Constenla-Haile     sal_uInt16 nCount( 0 );
375dc055bd2SAriel Constenla-Haile     if ( rPopupMenu.is() && ( nCount = rPopupMenu->getItemCount() ) != 0 && sURL.getLength() )
37610b918a5SAriel Constenla-Haile     {
37710b918a5SAriel Constenla-Haile         for ( sal_uInt16 n = 0; n < nCount; ++n )
37810b918a5SAriel Constenla-Haile         {
37910b918a5SAriel Constenla-Haile             sal_uInt16 nId = rPopupMenu->getItemId( n );
38010b918a5SAriel Constenla-Haile             OUString aCmd( rPopupMenu->getCommand( nId ) );
38110b918a5SAriel Constenla-Haile 
38210b918a5SAriel Constenla-Haile             if ( !bValidFallback && aCmd.getLength() )
38310b918a5SAriel Constenla-Haile             {
38410b918a5SAriel Constenla-Haile                 sFallback = aCmd;
38510b918a5SAriel Constenla-Haile                 bValidFallback = sal_True;
38610b918a5SAriel Constenla-Haile             }
38710b918a5SAriel Constenla-Haile 
38810b918a5SAriel Constenla-Haile             // match even if the menu command is more detailed
38910b918a5SAriel Constenla-Haile             // (maybe an additional query) #i28667#
39010b918a5SAriel Constenla-Haile             if ( aCmd.match( sURL ) )
39110b918a5SAriel Constenla-Haile             {
39210b918a5SAriel Constenla-Haile                 sURL = aCmd;
39310b918a5SAriel Constenla-Haile                 const css::uno::Reference< css::graphic::XGraphic > xGraphic(
39410b918a5SAriel Constenla-Haile                     rPopupMenu->getItemImage( nId ) );
39510b918a5SAriel Constenla-Haile                 if ( xGraphic.is() )
39610b918a5SAriel Constenla-Haile                     aImage = Image( xGraphic );
39710b918a5SAriel Constenla-Haile                 return sal_True;
39810b918a5SAriel Constenla-Haile             }
39910b918a5SAriel Constenla-Haile         }
40010b918a5SAriel Constenla-Haile     }
40110b918a5SAriel Constenla-Haile 
40210b918a5SAriel Constenla-Haile     if ( !bValidFallback )
40310b918a5SAriel Constenla-Haile     {
40410b918a5SAriel Constenla-Haile         rtl::OUStringBuffer aBuffer;
40510b918a5SAriel Constenla-Haile         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "private:factory/" ) );
40610b918a5SAriel Constenla-Haile         aBuffer.append( SvtModuleOptions().GetDefaultModuleName() );
40710b918a5SAriel Constenla-Haile         sFallback = aBuffer.makeStringAndClear();
40810b918a5SAriel Constenla-Haile     }
40910b918a5SAriel Constenla-Haile 
41010b918a5SAriel Constenla-Haile     return sal_False;
41110b918a5SAriel Constenla-Haile }
41210b918a5SAriel Constenla-Haile 
41310b918a5SAriel Constenla-Haile /** We accept URL's here only, which exist as items of our internal popup menu.
41410b918a5SAriel Constenla-Haile     All other ones will be ignored and a fallback is used.
41510b918a5SAriel Constenla-Haile  */
setItemImage(const OUString & rCommand)41610b918a5SAriel Constenla-Haile void NewToolbarController::setItemImage( const OUString &rCommand )
41710b918a5SAriel Constenla-Haile {
41810b918a5SAriel Constenla-Haile     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
41910b918a5SAriel Constenla-Haile     ToolBox* pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
42010b918a5SAriel Constenla-Haile     if ( !pToolBox )
42110b918a5SAriel Constenla-Haile         return;
42210b918a5SAriel Constenla-Haile 
42310b918a5SAriel Constenla-Haile     OUString aURL = rCommand;
42410b918a5SAriel Constenla-Haile     OUString sFallback;
42510b918a5SAriel Constenla-Haile     Image aMenuImage;
42610b918a5SAriel Constenla-Haile 
42710b918a5SAriel Constenla-Haile     sal_Bool bValid( Impl_ExistURLInMenu( m_xPopupMenu, aURL, sFallback, aMenuImage ) );
42889d8a215SAriel Constenla-Haile     if ( !bValid )
42910b918a5SAriel Constenla-Haile         aURL = sFallback;
43010b918a5SAriel Constenla-Haile 
43110b918a5SAriel Constenla-Haile     sal_Bool bBig = SvtMiscOptions().AreCurrentSymbolsLarge();
43210b918a5SAriel Constenla-Haile     sal_Bool bHC = pToolBox->GetSettings().GetStyleSettings().GetHighContrastMode();
43310b918a5SAriel Constenla-Haile 
43410b918a5SAriel Constenla-Haile     INetURLObject aURLObj( aURL );
43510b918a5SAriel Constenla-Haile     Image aImage = SvFileInformationManager::GetImageNoDefault( aURLObj, bBig, bHC );
43610b918a5SAriel Constenla-Haile     if ( !aImage )
43789d8a215SAriel Constenla-Haile         aImage = !!aMenuImage ?
43889d8a215SAriel Constenla-Haile             aMenuImage :
43989d8a215SAriel Constenla-Haile             SvFileInformationManager::GetImage( aURLObj, bBig, bHC );
44010b918a5SAriel Constenla-Haile 
44110b918a5SAriel Constenla-Haile     // if everything failed, just use the image associated with the toolbar item command
44210b918a5SAriel Constenla-Haile     if ( !aImage )
44310b918a5SAriel Constenla-Haile         return;
44410b918a5SAriel Constenla-Haile 
44510b918a5SAriel Constenla-Haile     Size aBigSize( pToolBox->GetDefaultImageSize() );
44610b918a5SAriel Constenla-Haile     if ( bBig && aImage.GetSizePixel() != aBigSize )
44710b918a5SAriel Constenla-Haile     {
44810b918a5SAriel Constenla-Haile         BitmapEx aScaleBmpEx( aImage.GetBitmapEx() );
44910b918a5SAriel Constenla-Haile         aScaleBmpEx.Scale( aBigSize, BMP_SCALE_INTERPOLATE );
45010b918a5SAriel Constenla-Haile         pToolBox->SetItemImage( m_nToolBoxId, Image( aScaleBmpEx ) );
45110b918a5SAriel Constenla-Haile     }
45210b918a5SAriel Constenla-Haile     else
45310b918a5SAriel Constenla-Haile         pToolBox->SetItemImage( m_nToolBoxId, aImage );
4547ca83afaSAriel Constenla-Haile 
4557ca83afaSAriel Constenla-Haile     m_aLastURL = aURL;
45610b918a5SAriel Constenla-Haile }
45710b918a5SAriel Constenla-Haile 
45810b918a5SAriel Constenla-Haile 
45910b918a5SAriel Constenla-Haile }
460