xref: /trunk/main/svtools/source/uno/toolboxcontroller.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/toolboxcontroller.hxx>
27 #include <com/sun/star/beans/PropertyValue.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/frame/XDispatchProvider.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/frame/XLayoutManager.hpp>
32 #include <vos/mutex.hxx>
33 #include <vcl/svapp.hxx>
34 #include <svtools/imgdef.hxx>
35 #include <svtools/miscopt.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <vcl/toolbox.hxx>
38 //shizhobo
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 const int TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIABLE  = 1;
41 const int TOOLBARCONTROLLER_PROPCOUNT               = 1;
42 const rtl::OUString TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIABLE( RTL_CONSTASCII_USTRINGPARAM( "SupportsVisiable" ));
43 //end
44 
45 using ::rtl::OUString;
46 
47 using namespace ::cppu;
48 using namespace ::com::sun::star::awt;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::util;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::frame;
54 using namespace ::com::sun::star::frame;
55 
56 namespace svt
57 {
58 
ToolboxController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & xFrame,const::rtl::OUString & aCommandURL)59 ToolboxController::ToolboxController(
60 
61     const Reference< XMultiServiceFactory >& rServiceManager,
62     const Reference< XFrame >& xFrame,
63     const ::rtl::OUString& aCommandURL ) :
64     OPropertyContainer(GetBroadcastHelper())
65     ,   OWeakObject()
66     ,   m_bInitialized( sal_False )
67     ,   m_bDisposed( sal_False )
68     ,   m_nToolBoxId( SAL_MAX_UINT16 )
69     ,   m_xFrame(xFrame)
70     ,   m_xServiceManager( rServiceManager )
71     ,   m_aCommandURL( aCommandURL )
72     ,   m_aListenerContainer( m_aMutex )
73 {
74     //registger Propertyh by shizhoubo
75     registerProperty(TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIABLE, TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIABLE, com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY,
76         &m_bSupportVisiable, getCppuType(&m_bSupportVisiable));
77 
78     try
79     {
80         m_xUrlTransformer.set( m_xServiceManager->createInstance(
81                                                             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
82                                                         UNO_QUERY );
83     }
84     catch(const Exception&)
85     {
86     }
87 }
88 
ToolboxController()89 ToolboxController::ToolboxController() :
90     OPropertyContainer(GetBroadcastHelper())
91     ,   OWeakObject()
92     ,   m_bInitialized( sal_False )
93     ,   m_bDisposed( sal_False )
94     ,   m_nToolBoxId( SAL_MAX_UINT16 )
95     ,   m_aListenerContainer( m_aMutex )
96 {
97     //registger Propertyh by shizhoubo
98     registerProperty(TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIABLE, TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIABLE, com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY,
99         &m_bSupportVisiable, getCppuType(&m_bSupportVisiable));
100 }
101 
~ToolboxController()102 ToolboxController::~ToolboxController()
103 {
104 }
105 
getFrameInterface() const106 Reference< XFrame > ToolboxController::getFrameInterface() const
107 {
108     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
109     return m_xFrame;
110 }
111 
getServiceManager() const112 Reference< XMultiServiceFactory > ToolboxController::getServiceManager() const
113 {
114     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
115     return m_xServiceManager;
116 }
117 
getLayoutManager() const118 Reference< XLayoutManager > ToolboxController::getLayoutManager() const
119 {
120     Reference< XLayoutManager > xLayoutManager;
121     Reference< XPropertySet > xPropSet;
122     {
123         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
124         xPropSet = Reference< XPropertySet >( m_xFrame, UNO_QUERY );
125     }
126 
127     if ( xPropSet.is() )
128     {
129         try
130         {
131             xLayoutManager.set(xPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "LayoutManager" ))),UNO_QUERY);
132         }
133         catch ( Exception& )
134         {
135         }
136     }
137 
138     return xLayoutManager;
139 }
140 
141 // XInterface
queryInterface(const Type & rType)142 Any SAL_CALL ToolboxController::queryInterface( const Type& rType )
143 {
144     Any a = ::cppu::queryInterface(
145                 rType ,
146                 static_cast< XToolbarController* >( this ),
147                 static_cast< XStatusListener* >( this ),
148                 static_cast< XEventListener* >( this ),
149                 static_cast< XInitialization* >( this ),
150                 static_cast< XComponent* >( this ),
151                 static_cast< XUpdatable* >( this ));
152     if ( !a.hasValue())
153     {
154         a = ::cppu::queryInterface(rType
155             ,static_cast<XPropertySet*>(this)
156             ,static_cast<XMultiPropertySet*>(this)
157             ,static_cast<XFastPropertySet*>(this));
158         if (!a.hasValue())
159             return OWeakObject::queryInterface( rType );
160     }
161     return a;
162 }
163 
acquire()164 void SAL_CALL ToolboxController::acquire() throw ()
165 {
166     OWeakObject::acquire();
167 }
168 
release()169 void SAL_CALL ToolboxController::release() throw ()
170 {
171     OWeakObject::release();
172 }
173 
initialize(const Sequence<Any> & aArguments)174 void SAL_CALL ToolboxController::initialize( const Sequence< Any >& aArguments )
175 {
176     bool bInitialized( true );
177 
178     {
179         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
180 
181         if ( m_bDisposed )
182             throw DisposedException();
183 
184         bInitialized = m_bInitialized;
185     }
186 
187     if ( !bInitialized )
188     {
189         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
190         m_bInitialized = sal_True;
191         //shizhoubo add
192         m_bSupportVisiable = sal_False;
193         PropertyValue aPropValue;
194         for ( int i = 0; i < aArguments.getLength(); i++ )
195         {
196             if ( aArguments[i] >>= aPropValue )
197             {
198                 if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("Frame") ))
199                     m_xFrame.set(aPropValue.Value,UNO_QUERY);
200                 else if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("CommandURL") ))
201                     aPropValue.Value >>= m_aCommandURL;
202                 else if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("ServiceManager") ))
203                     m_xServiceManager.set(aPropValue.Value,UNO_QUERY);
204                 else if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("ParentWindow") ))
205                     m_xParentWindow.set(aPropValue.Value,UNO_QUERY);
206                 else if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("ModuleIdentifier" ) ) )
207                     aPropValue.Value >>= m_sModuleName;
208                 else if ( aPropValue.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("Identifier" ) ) )
209                     aPropValue.Value >>= m_nToolBoxId;
210             }
211         }
212 
213         try
214         {
215             if ( !m_xUrlTransformer.is() && m_xServiceManager.is() )
216                 m_xUrlTransformer.set( m_xServiceManager->createInstance(
217                                                                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
218                                                             UNO_QUERY );
219         }
220         catch(const Exception&)
221         {
222         }
223 
224         if ( m_aCommandURL.getLength() )
225             m_aListenerMap.insert( URLToDispatchMap::value_type( m_aCommandURL, Reference< XDispatch >() ));
226     }
227 }
228 
update()229 void SAL_CALL ToolboxController::update()
230 {
231     {
232         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
233         if ( m_bDisposed )
234             throw DisposedException();
235     }
236 
237     // Bind all registered listeners to their dispatch objects
238     bindListener();
239 }
240 
241 // XComponent
dispose()242 void SAL_CALL ToolboxController::dispose()
243 {
244     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
245 
246     {
247         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
248         if ( m_bDisposed )
249             throw DisposedException();
250     }
251 
252     com::sun::star::lang::EventObject aEvent( xThis );
253     m_aListenerContainer.disposeAndClear( aEvent );
254 
255     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
256     Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
257     URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
258     while ( pIter != m_aListenerMap.end() )
259     {
260         try
261         {
262             Reference< XDispatch > xDispatch( pIter->second );
263 
264             com::sun::star::util::URL aTargetURL;
265             aTargetURL.Complete = pIter->first;
266             if ( m_xUrlTransformer.is() )
267                 m_xUrlTransformer->parseStrict( aTargetURL );
268 
269             if ( xDispatch.is() && xStatusListener.is() )
270                 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
271         }
272         catch ( Exception& )
273         {
274         }
275 
276         ++pIter;
277     }
278 
279     m_bDisposed = sal_True;
280 }
281 
addEventListener(const Reference<XEventListener> & xListener)282 void SAL_CALL ToolboxController::addEventListener( const Reference< XEventListener >& xListener )
283 {
284     m_aListenerContainer.addInterface( ::getCppuType( ( const Reference< XEventListener >* ) NULL ), xListener );
285 }
286 
removeEventListener(const Reference<XEventListener> & aListener)287 void SAL_CALL ToolboxController::removeEventListener( const Reference< XEventListener >& aListener )
288 {
289     m_aListenerContainer.removeInterface( ::getCppuType( ( const Reference< XEventListener >* ) NULL ), aListener );
290 }
291 
292 // XEventListener
disposing(const EventObject & Source)293 void SAL_CALL ToolboxController::disposing( const EventObject& Source )
294 {
295     Reference< XInterface > xSource( Source.Source );
296 
297     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
298 
299     if ( m_bDisposed )
300         return;
301 
302     URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
303     while ( pIter != m_aListenerMap.end() )
304     {
305         // Compare references and release dispatch references if they are equal.
306         Reference< XInterface > xIfac( pIter->second, UNO_QUERY );
307         if ( xSource == xIfac )
308             pIter->second.clear();
309         ++pIter;
310     }
311 
312     Reference< XInterface > xIfac( m_xFrame, UNO_QUERY );
313     if ( xIfac == xSource )
314         m_xFrame.clear();
315 }
316 
317 // XStatusListener
statusChanged(const FeatureStateEvent &)318 void SAL_CALL ToolboxController::statusChanged( const FeatureStateEvent& )
319 {
320     // must be implemented by sub class
321 }
322 
323 // XToolbarController
execute(sal_Int16 KeyModifier)324 void SAL_CALL ToolboxController::execute( sal_Int16 KeyModifier )
325 {
326     Reference< XDispatch >       xDispatch;
327     ::rtl::OUString                     aCommandURL;
328 
329     {
330         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
331 
332         if ( m_bDisposed )
333             throw DisposedException();
334 
335         if ( m_bInitialized &&
336              m_xFrame.is() &&
337              m_xServiceManager.is() &&
338              m_aCommandURL.getLength() )
339         {
340 
341             aCommandURL = m_aCommandURL;
342             URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
343             if ( pIter != m_aListenerMap.end() )
344                 xDispatch = pIter->second;
345         }
346     }
347 
348     if ( xDispatch.is() )
349     {
350         try
351         {
352             com::sun::star::util::URL aTargetURL;
353             Sequence<PropertyValue>   aArgs( 1 );
354 
355             // Provide key modifier information to dispatch function
356             aArgs[0].Name   = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
357             aArgs[0].Value  = makeAny( KeyModifier );
358 
359             aTargetURL.Complete = aCommandURL;
360             if ( m_xUrlTransformer.is() )
361                 m_xUrlTransformer->parseStrict( aTargetURL );
362             xDispatch->dispatch( aTargetURL, aArgs );
363         }
364         catch ( DisposedException& )
365         {
366         }
367     }
368 }
369 
click()370 void SAL_CALL ToolboxController::click()
371 {
372 }
373 
doubleClick()374 void SAL_CALL ToolboxController::doubleClick()
375 {
376 }
377 
createPopupWindow()378 Reference< XWindow > SAL_CALL ToolboxController::createPopupWindow()
379 {
380     return Reference< XWindow >();
381 }
382 
createItemWindow(const Reference<XWindow> &)383 Reference< XWindow > SAL_CALL ToolboxController::createItemWindow( const Reference< XWindow >& )
384 {
385     return Reference< XWindow >();
386 }
387 
addStatusListener(const rtl::OUString & aCommandURL)388 void ToolboxController::addStatusListener( const rtl::OUString& aCommandURL )
389 {
390     Reference< XDispatch >       xDispatch;
391     Reference< XStatusListener > xStatusListener;
392     com::sun::star::util::URL    aTargetURL;
393 
394     {
395         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
396         URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
397 
398         // Already in the list of status listener. Do nothing.
399         if ( pIter != m_aListenerMap.end() )
400             return;
401 
402         // Check if we are already initialized. Implementation starts adding itself as status listener when
403         // initialize is called.
404         if ( !m_bInitialized )
405         {
406             // Put into the hash_map of status listener. Will be activated when initialized is called
407             m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, Reference< XDispatch >() ));
408             return;
409         }
410         else
411         {
412             // Add status listener directly as initialize has already been called.
413             Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
414             if ( m_xServiceManager.is() && xDispatchProvider.is() )
415             {
416                 aTargetURL.Complete = aCommandURL;
417                 if ( m_xUrlTransformer.is() )
418                     m_xUrlTransformer->parseStrict( aTargetURL );
419                 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
420 
421                 xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
422                 URLToDispatchMap::iterator aIter = m_aListenerMap.find( aCommandURL );
423                 if ( aIter != m_aListenerMap.end() )
424                 {
425                     Reference< XDispatch > xOldDispatch( aIter->second );
426                     aIter->second = xDispatch;
427 
428                     try
429                     {
430                         if ( xOldDispatch.is() )
431                             xOldDispatch->removeStatusListener( xStatusListener, aTargetURL );
432                     }
433                     catch ( Exception& )
434                     {
435                     }
436                 }
437                 else
438                     m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, xDispatch ));
439             }
440         }
441     }
442 
443     // Call without locked mutex as we are called back from dispatch implementation
444     try
445     {
446         if ( xDispatch.is() )
447             xDispatch->addStatusListener( xStatusListener, aTargetURL );
448     }
449     catch ( Exception& )
450     {
451     }
452 }
453 
removeStatusListener(const rtl::OUString & aCommandURL)454 void ToolboxController::removeStatusListener( const rtl::OUString& aCommandURL )
455 {
456     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
457 
458     URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
459     if ( pIter != m_aListenerMap.end() )
460     {
461         Reference< XDispatch > xDispatch( pIter->second );
462         Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
463         m_aListenerMap.erase( pIter );
464 
465         try
466         {
467             com::sun::star::util::URL aTargetURL;
468             aTargetURL.Complete = aCommandURL;
469             if ( m_xUrlTransformer.is() )
470                 m_xUrlTransformer->parseStrict( aTargetURL );
471 
472             if ( xDispatch.is() && xStatusListener.is() )
473                 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
474         }
475         catch ( Exception& )
476         {
477         }
478     }
479 }
480 
bindListener()481 void ToolboxController::bindListener()
482 {
483     std::vector< Listener > aDispatchVector;
484     Reference< XStatusListener > xStatusListener;
485 
486     {
487         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
488 
489         if ( !m_bInitialized )
490             return;
491 
492         // Collect all registered command URL's and store them temporary
493         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
494         if ( m_xServiceManager.is() && xDispatchProvider.is() )
495         {
496             xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
497             URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
498             while ( pIter != m_aListenerMap.end() )
499             {
500                 com::sun::star::util::URL aTargetURL;
501                 aTargetURL.Complete = pIter->first;
502                 if ( m_xUrlTransformer.is() )
503                     m_xUrlTransformer->parseStrict( aTargetURL );
504 
505                 Reference< XDispatch > xDispatch( pIter->second );
506                 if ( xDispatch.is() )
507                 {
508                     // We already have a dispatch object => we have to requery.
509                     // Release old dispatch object and remove it as listener
510                     try
511                     {
512                         xDispatch->removeStatusListener( xStatusListener, aTargetURL );
513                     }
514                     catch ( Exception& )
515                     {
516                     }
517                 }
518 
519                 pIter->second.clear();
520                 xDispatch.clear();
521 
522                 // Query for dispatch object. Old dispatch will be released with this, too.
523                 try
524                 {
525                     xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
526                 }
527                 catch ( Exception& )
528                 {
529                 }
530                 pIter->second = xDispatch;
531 
532                 Listener aListener( aTargetURL, xDispatch );
533                 aDispatchVector.push_back( aListener );
534                 ++pIter;
535             }
536         }
537     }
538 
539     // Call without locked mutex as we are called back from dispatch implementation
540     if ( xStatusListener.is() )
541     {
542         try
543         {
544             for ( sal_uInt32 i = 0; i < aDispatchVector.size(); i++ )
545             {
546                 Listener& rListener = aDispatchVector[i];
547                 if ( rListener.xDispatch.is() )
548                     rListener.xDispatch->addStatusListener( xStatusListener, rListener.aURL );
549                 else if ( rListener.aURL.Complete == m_aCommandURL )
550                 {
551                     try
552                     {
553                         // Send status changed for the main URL, if we cannot get a valid dispatch object.
554                         // UI disables the button. Catch exception as we release our mutex, it is possible
555                         // that someone else already disposed this instance!
556                         FeatureStateEvent aFeatureStateEvent;
557                         aFeatureStateEvent.IsEnabled = sal_False;
558                         aFeatureStateEvent.FeatureURL = rListener.aURL;
559                         aFeatureStateEvent.State = Any();
560                         xStatusListener->statusChanged( aFeatureStateEvent );
561                     }
562                     catch ( Exception& )
563                     {
564                     }
565                 }
566             }
567         }
568         catch ( Exception& )
569         {
570         }
571     }
572 }
573 
unbindListener()574 void ToolboxController::unbindListener()
575 {
576     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
577 
578     if ( !m_bInitialized )
579         return;
580 
581     // Collect all registered command URL's and store them temporary
582     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
583     if ( m_xServiceManager.is() && xDispatchProvider.is() )
584     {
585         Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
586         URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
587         while ( pIter != m_aListenerMap.end() )
588         {
589             com::sun::star::util::URL aTargetURL;
590             aTargetURL.Complete = pIter->first;
591             if ( m_xUrlTransformer.is() )
592                 m_xUrlTransformer->parseStrict( aTargetURL );
593 
594             Reference< XDispatch > xDispatch( pIter->second );
595             if ( xDispatch.is() )
596             {
597                 // We already have a dispatch object => we have to requery.
598                 // Release old dispatch object and remove it as listener
599                 try
600                 {
601                     xDispatch->removeStatusListener( xStatusListener, aTargetURL );
602                 }
603                 catch ( Exception& )
604                 {
605                 }
606             }
607             pIter->second.clear();
608             ++pIter;
609         }
610     }
611 }
612 
isBound() const613 sal_Bool ToolboxController::isBound() const
614 {
615     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
616 
617     if ( !m_bInitialized )
618         return sal_False;
619 
620     URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( m_aCommandURL );
621     if ( pIter != m_aListenerMap.end() )
622         return ( pIter->second.is() );
623 
624     return sal_False;
625 }
626 
hasBigImages() const627 sal_Bool ToolboxController::hasBigImages() const
628 {
629     return SvtMiscOptions().AreCurrentSymbolsLarge();
630 }
631 
isHighContrast() const632 sal_Bool ToolboxController::isHighContrast() const
633 {
634     sal_Bool bHighContrast( sal_False );
635 
636     Reference< XWindow > xWindow = m_xParentWindow;
637     if ( xWindow.is() )
638     {
639         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
640         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
641         if ( pWindow )
642             bHighContrast = ( ((ToolBox *)pWindow)->GetSettings().GetStyleSettings().GetHighContrastMode() );
643     }
644 
645     return bHighContrast;
646 }
647 
updateStatus()648 void ToolboxController::updateStatus()
649 {
650     bindListener();
651 }
652 
updateStatus(const rtl::OUString aCommandURL)653 void ToolboxController::updateStatus( const rtl::OUString aCommandURL )
654 {
655     Reference< XDispatch > xDispatch;
656     Reference< XStatusListener > xStatusListener;
657     com::sun::star::util::URL aTargetURL;
658 
659     {
660         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
661 
662         if ( !m_bInitialized )
663             return;
664 
665         // Try to find a dispatch object for the requested command URL
666         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
667         xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
668         if ( m_xServiceManager.is() && xDispatchProvider.is() )
669         {
670             aTargetURL.Complete = aCommandURL;
671             if ( m_xUrlTransformer.is() )
672                 m_xUrlTransformer->parseStrict( aTargetURL );
673             xDispatch = xDispatchProvider->queryDispatch( aTargetURL, rtl::OUString(), 0 );
674         }
675     }
676 
677     if ( xDispatch.is() && xStatusListener.is() )
678     {
679         // Catch exception as we release our mutex, it is possible that someone else
680         // has already disposed this instance!
681         // Add/remove status listener to get a update status information from the
682         // requested command.
683         try
684         {
685             xDispatch->addStatusListener( xStatusListener, aTargetURL );
686             xDispatch->removeStatusListener( xStatusListener, aTargetURL );
687         }
688         catch ( Exception& )
689         {
690         }
691     }
692 }
693 
getURLTransformer() const694 Reference< XURLTransformer > ToolboxController::getURLTransformer() const
695 {
696     return m_xUrlTransformer;
697 }
698 
getParent() const699 Reference< ::com::sun::star::awt::XWindow > ToolboxController::getParent() const
700 {
701     return m_xParentWindow;
702 }
703 
dispatchCommand(const OUString & sCommandURL,const Sequence<PropertyValue> & rArgs,const OUString & sTarget)704 void ToolboxController::dispatchCommand( const OUString& sCommandURL, const Sequence< PropertyValue >& rArgs, const OUString &sTarget )
705 {
706     try
707     {
708         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY_THROW );
709         URL aURL;
710         aURL.Complete = sCommandURL;
711         getURLTransformer()->parseStrict( aURL );
712 
713         Reference< XDispatch > xDispatch( xDispatchProvider->queryDispatch( aURL, sTarget, 0 ), UNO_QUERY_THROW );
714 
715         DispatchInfo *pDispatchInfo = new DispatchInfo( xDispatch, aURL, rArgs );
716         if ( !Application::PostUserEvent( STATIC_LINK(0, ToolboxController, ExecuteHdl_Impl),
717                                           pDispatchInfo ) )
718             delete pDispatchInfo;
719 
720     }
721     catch( Exception& )
722     {
723     }
724 }
725 
726 //
727 //-------------------------------------------------------------------------
728 // XPropertySet by shizhoubo
getPropertySetInfo()729 com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo >  SAL_CALL ToolboxController::getPropertySetInfo()
730 {
731     Reference<XPropertySetInfo>  xInfo( createPropertySetInfo( getInfoHelper() ) );
732     return xInfo;
733 }
734 //-------------------------------------------------------------------------
getInfoHelper()735 ::cppu::IPropertyArrayHelper& ToolboxController::getInfoHelper()
736 {
737         return *const_cast<ToolboxController*>(this)->getArrayHelper();
738 }
739 //OPropertyArrayUsageHelper by shizhoubo
740 //------------------------------------------------------------------------------
createArrayHelper() const741 ::cppu::IPropertyArrayHelper* ToolboxController::createArrayHelper( ) const
742 {
743         com::sun::star::uno::Sequence< Property > aProps;
744         describeProperties(aProps);
745         return new ::cppu::OPropertyArrayHelper(aProps);
746 }
747 //shizhoubo for supportsvisiable
setSupportVisiableProperty(sal_Bool bValue)748 void ToolboxController::setSupportVisiableProperty(sal_Bool bValue)
749 {
750     m_bSupportVisiable = bValue;
751 }
752 //OPropertySetHelper by shizhoubo
convertFastPropertyValue(com::sun::star::uno::Any & aConvertedValue,com::sun::star::uno::Any & aOldValue,sal_Int32 nHandle,const com::sun::star::uno::Any & aValue)753 sal_Bool SAL_CALL ToolboxController::convertFastPropertyValue( com::sun::star::uno::Any&    aConvertedValue ,
754                                              com::sun::star::uno::Any&        aOldValue       ,
755                                              sal_Int32                        nHandle         ,
756                                              const com::sun::star::uno::Any&  aValue          )
757 {
758     switch (nHandle)
759     {
760         case TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIABLE:
761         {
762             sal_Bool aNewValue(sal_False);
763             aValue >>= aNewValue;
764             if (aNewValue != m_bSupportVisiable)
765             {
766                 aConvertedValue <<= aNewValue;
767                 aOldValue <<= m_bSupportVisiable;
768                 return sal_True;
769             }
770             return sal_False;
771         }
772     }
773     return OPropertyContainer::convertFastPropertyValue(aConvertedValue, aOldValue, nHandle, aValue);
774 }
775 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const com::sun::star::uno::Any & aValue)776 void SAL_CALL ToolboxController::setFastPropertyValue_NoBroadcast(
777     sal_Int32                       nHandle,
778     const com::sun::star::uno::Any& aValue )
779 {
780     OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, aValue);
781     if (TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIABLE == nHandle)
782     {
783         sal_Bool rValue(sal_False);
784         if (( aValue >>= rValue ) && m_bInitialized)
785             this->setSupportVisiableProperty( rValue );
786     }
787 }
788 
789 //--------------------------------------------------------------------
790 
IMPL_STATIC_LINK_NOINSTANCE(ToolboxController,ExecuteHdl_Impl,DispatchInfo *,pDispatchInfo)791 IMPL_STATIC_LINK_NOINSTANCE( ToolboxController, ExecuteHdl_Impl, DispatchInfo*, pDispatchInfo )
792 {
793     pDispatchInfo->mxDispatch->dispatch( pDispatchInfo->maURL, pDispatchInfo->maArgs );
794     delete pDispatchInfo;
795     return 0;
796 }
797 
enable(bool bEnable)798 void ToolboxController::enable( bool bEnable )
799 {
800     ToolBox* pToolBox = 0;
801     sal_uInt16 nItemId = 0;
802     if( getToolboxId( nItemId, &pToolBox ) )
803     {
804         pToolBox->EnableItem( nItemId, bEnable ? sal_True : sal_False );
805     }
806 }
807 
getToolboxId(sal_uInt16 & rItemId,ToolBox ** ppToolBox)808 bool ToolboxController::getToolboxId( sal_uInt16& rItemId, ToolBox** ppToolBox )
809 {
810     if( (m_nToolBoxId != SAL_MAX_UINT16) && (ppToolBox == 0) )
811         return m_nToolBoxId;
812 
813     ToolBox* pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
814 
815     if( (m_nToolBoxId == SAL_MAX_UINT16) && pToolBox )
816     {
817         const sal_uInt16 nCount = pToolBox->GetItemCount();
818         for ( sal_uInt16 nPos = 0; nPos < nCount; ++nPos )
819         {
820             const sal_uInt16 nItemId = pToolBox->GetItemId( nPos );
821             if ( pToolBox->GetItemCommand( nItemId ) == String( m_aCommandURL ) )
822             {
823                 m_nToolBoxId = nItemId;
824                 break;
825             }
826         }
827     }
828 
829     if( ppToolBox )
830         *ppToolBox = pToolBox;
831 
832     rItemId = m_nToolBoxId;
833 
834     return (rItemId != SAL_MAX_UINT16) && (( ppToolBox == 0) || (*ppToolBox != 0) );
835 }
836 //end
837 
838 } // svt
839