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