xref: /AOO41X/main/svtools/source/uno/generictoolboxcontroller.cxx (revision 5900e8ec128faec89519683efce668ccd8cc6084)
1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir #include <svtools/generictoolboxcontroller.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_________________________________________________________________________________________________________________
29cdf0e10cSrcweir //	my own includes
30cdf0e10cSrcweir //_________________________________________________________________________________________________________________
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //_________________________________________________________________________________________________________________
33cdf0e10cSrcweir //	interface includes
34cdf0e10cSrcweir //_________________________________________________________________________________________________________________
35cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
36cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
37cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemStatus.hpp>
40cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemState.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43cdf0e10cSrcweir //	other includes
44cdf0e10cSrcweir //_________________________________________________________________________________________________________________
45cdf0e10cSrcweir #include <vos/mutex.hxx>
46cdf0e10cSrcweir #include <vcl/svapp.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::com::sun::star::awt;
49cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50cdf0e10cSrcweir using namespace ::com::sun::star::beans;
51cdf0e10cSrcweir using namespace ::com::sun::star::lang;
52cdf0e10cSrcweir using namespace ::com::sun::star::frame;
53cdf0e10cSrcweir using namespace ::com::sun::star::frame::status;
54cdf0e10cSrcweir using namespace ::com::sun::star::util;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace svt
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir struct ExecuteInfo
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >     xDispatch;
62cdf0e10cSrcweir     ::com::sun::star::util::URL                                                aTargetURL;
63cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >  aArgs;
64cdf0e10cSrcweir };
65cdf0e10cSrcweir 
GenericToolboxController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & rFrame,ToolBox * pToolbox,sal_uInt16 nID,const::rtl::OUString & aCommand)66cdf0e10cSrcweir GenericToolboxController::GenericToolboxController( const Reference< XMultiServiceFactory >& rServiceManager,
67cdf0e10cSrcweir                                                     const Reference< XFrame >&               rFrame,
68cdf0e10cSrcweir                                                     ToolBox*                                 pToolbox,
69cdf0e10cSrcweir                                                     sal_uInt16                                   nID,
70cdf0e10cSrcweir                                                     const ::rtl::OUString&                          aCommand ) :
71cdf0e10cSrcweir     svt::ToolboxController( rServiceManager, rFrame, aCommand )
72cdf0e10cSrcweir     ,   m_pToolbox( pToolbox )
73cdf0e10cSrcweir     ,   m_nID( nID )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     // Initialization is done through ctor
76cdf0e10cSrcweir     m_bInitialized = sal_True;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     // insert main command to our listener map
79cdf0e10cSrcweir     if ( m_aCommandURL.getLength() )
80cdf0e10cSrcweir         m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
~GenericToolboxController()83cdf0e10cSrcweir GenericToolboxController::~GenericToolboxController()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
dispose()87cdf0e10cSrcweir void SAL_CALL GenericToolboxController::dispose()
88cdf0e10cSrcweir throw ( RuntimeException )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     svt::ToolboxController::dispose();
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     m_pToolbox = 0;
95cdf0e10cSrcweir     m_nID = 0;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
execute(sal_Int16)98cdf0e10cSrcweir void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
99cdf0e10cSrcweir throw ( RuntimeException )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir     Reference< XDispatch >       xDispatch;
102cdf0e10cSrcweir     Reference< XURLTransformer > xURLTransformer;
103cdf0e10cSrcweir     ::rtl::OUString                     aCommandURL;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         if ( m_bDisposed )
109cdf0e10cSrcweir             throw DisposedException();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         if ( m_bInitialized &&
112cdf0e10cSrcweir              m_xFrame.is() &&
113cdf0e10cSrcweir              m_xServiceManager.is() &&
114cdf0e10cSrcweir              m_aCommandURL.getLength() )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             xURLTransformer = Reference< XURLTransformer >( m_xServiceManager->createInstance(
117cdf0e10cSrcweir                                                                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
118cdf0e10cSrcweir                                                             UNO_QUERY );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             aCommandURL = m_aCommandURL;
121cdf0e10cSrcweir             URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
122cdf0e10cSrcweir             if ( pIter != m_aListenerMap.end() )
123cdf0e10cSrcweir                 xDispatch = pIter->second;
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if ( xDispatch.is() && xURLTransformer.is() )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         com::sun::star::util::URL aTargetURL;
130cdf0e10cSrcweir         Sequence<PropertyValue>   aArgs;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         aTargetURL.Complete = aCommandURL;
133cdf0e10cSrcweir         xURLTransformer->parseStrict( aTargetURL );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         // Execute dispatch asynchronously
136cdf0e10cSrcweir         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
137cdf0e10cSrcweir         pExecuteInfo->xDispatch     = xDispatch;
138cdf0e10cSrcweir         pExecuteInfo->aTargetURL    = aTargetURL;
139cdf0e10cSrcweir         pExecuteInfo->aArgs         = aArgs;
140cdf0e10cSrcweir         Application::PostUserEvent( STATIC_LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
statusChanged(const FeatureStateEvent & Event)144cdf0e10cSrcweir void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
145cdf0e10cSrcweir throw ( RuntimeException )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     if ( m_bDisposed )
150cdf0e10cSrcweir         return;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     if ( m_pToolbox )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         sal_uInt16 nItemBits = m_pToolbox->GetItemBits( m_nID );
157cdf0e10cSrcweir         nItemBits &= ~TIB_CHECKABLE;
158cdf0e10cSrcweir 	    TriState eTri = STATE_NOCHECK;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         sal_Bool        bValue = sal_Bool();
161cdf0e10cSrcweir         rtl::OUString   aStrValue;
162cdf0e10cSrcweir         ItemStatus      aItemState;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         if ( Event.State >>= bValue )
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             // Boolean, treat it as checked/unchecked
167cdf0e10cSrcweir             m_pToolbox->SetItemBits( m_nID, nItemBits );
168cdf0e10cSrcweir             m_pToolbox->CheckItem( m_nID, bValue );
169cdf0e10cSrcweir             if ( bValue )
170cdf0e10cSrcweir                 eTri = STATE_CHECK;
171cdf0e10cSrcweir 		    nItemBits |= TIB_CHECKABLE;
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir         else if ( Event.State >>= aStrValue )
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             m_pToolbox->SetItemText( m_nID, aStrValue );
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir         else if ( Event.State >>= aItemState )
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir 			eTri = STATE_DONTKNOW;
180cdf0e10cSrcweir             nItemBits |= TIB_CHECKABLE;
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         m_pToolbox->SetItemState( m_nID, eTri );
184cdf0e10cSrcweir 	    m_pToolbox->SetItemBits( m_nID, nItemBits );
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
IMPL_STATIC_LINK_NOINSTANCE(GenericToolboxController,ExecuteHdl_Impl,ExecuteInfo *,pExecuteInfo)188cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir    try
191cdf0e10cSrcweir    {
192cdf0e10cSrcweir        // Asynchronous execution as this can lead to our own destruction!
193cdf0e10cSrcweir        // Framework can recycle our current frame and the layout manager disposes all user interface
194cdf0e10cSrcweir        // elements if a component gets detached from its frame!
195cdf0e10cSrcweir        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
196cdf0e10cSrcweir    }
197cdf0e10cSrcweir    catch ( Exception& )
198cdf0e10cSrcweir    {
199cdf0e10cSrcweir    }
200cdf0e10cSrcweir    delete pExecuteInfo;
201cdf0e10cSrcweir    return 0;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir } // namespace
205