merge the formfield patch from ooo-build
[ooovba.git] / UnoControls / source / controls / framecontrol.cxx
blob294eb59b5ea45b025214686918bb5ae6b3082ab1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: framecontrol.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 //______________________________________________________________________________________________________________
33 // my own include
34 //______________________________________________________________________________________________________________
36 #include "framecontrol.hxx"
38 //______________________________________________________________________________________________________________
39 // includes of other projects
40 //______________________________________________________________________________________________________________
41 #include <com/sun/star/frame/XDispatchProvider.hpp>
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <com/sun/star/frame/XDispatch.hpp>
44 #include <com/sun/star/frame/FrameSearchFlag.hpp>
45 #include <com/sun/star/frame/FrameSearchFlag.hpp>
46 #include <com/sun/star/beans/PropertyAttribute.hpp>
47 #include <cppuhelper/typeprovider.hxx>
48 #include <vos/diagnose.hxx>
50 //______________________________________________________________________________________________________________
51 // include of my own project
52 //______________________________________________________________________________________________________________
54 //______________________________________________________________________________________________________________
55 // namespaces
56 //______________________________________________________________________________________________________________
58 using namespace ::rtl ;
59 using namespace ::osl ;
60 using namespace ::cppu ;
61 using namespace ::com::sun::star::uno ;
62 using namespace ::com::sun::star::lang ;
63 using namespace ::com::sun::star::beans ;
64 using namespace ::com::sun::star::awt ;
65 using namespace ::com::sun::star::frame ;
66 using namespace ::com::sun::star::util ;
68 namespace unocontrols{
70 //______________________________________________________________________________________________________________
71 // construct/destruct
72 //______________________________________________________________________________________________________________
74 FrameControl::FrameControl( const Reference< XMultiServiceFactory >& xFactory )
75 : BaseControl ( xFactory )
76 , OBroadcastHelper ( m_aMutex )
77 , OPropertySetHelper ( *SAL_STATIC_CAST( OBroadcastHelper *, this ) )
78 , m_aInterfaceContainer ( m_aMutex )
79 , m_aConnectionPointContainer ( m_aMutex )
83 FrameControl::~FrameControl()
87 //____________________________________________________________________________________________________________
88 // XInterface
89 //____________________________________________________________________________________________________________
91 Any SAL_CALL FrameControl::queryInterface( const Type& rType ) throw( RuntimeException )
93 // Attention:
94 // Don't use mutex or guard in this method!!! Is a method of XInterface.
95 Any aReturn ;
96 Reference< XInterface > xDel = BaseControl::impl_getDelegator();
97 if ( xDel.is() )
99 // If an delegator exist, forward question to his queryInterface.
100 // Delegator will ask his own queryAggregation!
101 aReturn = xDel->queryInterface( rType );
103 else
105 // If an delegator unknown, forward question to own queryAggregation.
106 aReturn = queryAggregation( rType );
109 return aReturn ;
112 //____________________________________________________________________________________________________________
113 // XInterface
114 //____________________________________________________________________________________________________________
116 void SAL_CALL FrameControl::acquire() throw()
118 // Attention:
119 // Don't use mutex or guard in this method!!! Is a method of XInterface.
121 // Forward to baseclass
122 BaseControl::acquire();
125 //____________________________________________________________________________________________________________
126 // XInterface
127 //____________________________________________________________________________________________________________
129 void SAL_CALL FrameControl::release() throw()
131 // Attention:
132 // Don't use mutex or guard in this method!!! Is a method of XInterface.
134 // Forward to baseclass
135 BaseControl::release();
138 //____________________________________________________________________________________________________________
139 // XTypeProvider
140 //____________________________________________________________________________________________________________
142 Sequence< Type > SAL_CALL FrameControl::getTypes() throw( RuntimeException )
144 // Optimize this method !
145 // We initialize a static variable only one time. And we don't must use a mutex at every call!
146 // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
147 static OTypeCollection* pTypeCollection = NULL ;
149 if ( pTypeCollection == NULL )
151 // Ready for multithreading; get global mutex for first call of this method only! see before
152 MutexGuard aGuard( Mutex::getGlobalMutex() );
154 // Control these pointer again ... it can be, that another instance will be faster then these!
155 if ( pTypeCollection == NULL )
157 // Create a static typecollection ...
158 static OTypeCollection aTypeCollection ( ::getCppuType(( const Reference< XControlModel >*)NULL ) ,
159 ::getCppuType(( const Reference< XControlContainer >*)NULL ) ,
160 ::getCppuType(( const Reference< XConnectionPointContainer >*)NULL ) ,
161 BaseControl::getTypes()
163 // ... and set his address to static pointer!
164 pTypeCollection = &aTypeCollection ;
168 return pTypeCollection->getTypes();
171 //____________________________________________________________________________________________________________
172 // XAggregation
173 //____________________________________________________________________________________________________________
175 Any SAL_CALL FrameControl::queryAggregation( const Type& aType ) throw( RuntimeException )
177 // Ask for my own supported interfaces ...
178 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
179 Any aReturn ( ::cppu::queryInterface( aType ,
180 static_cast< XControlModel* > ( this ) ,
181 static_cast< XConnectionPointContainer* > ( this )
185 // If searched interface not supported by this class ...
186 if ( aReturn.hasValue() == sal_False )
188 // ... ask baseclasses.
189 aReturn = OPropertySetHelper::queryInterface( aType );
190 if ( aReturn.hasValue() == sal_False )
192 aReturn = BaseControl::queryAggregation( aType );
196 return aReturn ;
199 //____________________________________________________________________________________________________________
200 // XControl
201 //____________________________________________________________________________________________________________
203 void SAL_CALL FrameControl::createPeer( const Reference< XToolkit >& xToolkit ,
204 const Reference< XWindowPeer >& xParentPeer ) throw( RuntimeException )
206 BaseControl::createPeer( xToolkit, xParentPeer );
207 if ( impl_getPeerWindow().is() )
209 if( m_sComponentURL.getLength() > 0 )
211 impl_createFrame( getPeer(), m_sComponentURL, m_seqLoaderArguments );
216 //____________________________________________________________________________________________________________
217 // XControl
218 //____________________________________________________________________________________________________________
220 sal_Bool SAL_CALL FrameControl::setModel( const Reference< XControlModel >& /*xModel*/ ) throw( RuntimeException )
222 // We have no model.
223 return sal_False ;
226 //____________________________________________________________________________________________________________
227 // XControl
228 //____________________________________________________________________________________________________________
230 Reference< XControlModel > SAL_CALL FrameControl::getModel() throw( RuntimeException )
232 // We have no model.
233 return Reference< XControlModel >();
236 //____________________________________________________________________________________________________________
237 // XControl
238 //____________________________________________________________________________________________________________
240 void SAL_CALL FrameControl::dispose() throw( RuntimeException )
242 impl_deleteFrame();
243 BaseControl::dispose();
246 //____________________________________________________________________________________________________________
247 // XView
248 //____________________________________________________________________________________________________________
250 sal_Bool SAL_CALL FrameControl::setGraphics( const Reference< XGraphics >& /*xDevice*/ ) throw( RuntimeException )
252 // it is not possible to print this control
253 return sal_False ;
256 //____________________________________________________________________________________________________________
257 // XView
258 //____________________________________________________________________________________________________________
260 Reference< XGraphics > SAL_CALL FrameControl::getGraphics() throw( RuntimeException )
262 // when its not posible to set graphics ! then its possible to return null
263 return Reference< XGraphics >();
266 //____________________________________________________________________________________________________________
267 // XConnectionPointContainer
268 //____________________________________________________________________________________________________________
270 Sequence< Type > SAL_CALL FrameControl::getConnectionPointTypes() throw( RuntimeException )
272 // Forwarded to helper class
273 return m_aConnectionPointContainer.getConnectionPointTypes();
276 //____________________________________________________________________________________________________________
277 // XConnectionPointContainer
278 //____________________________________________________________________________________________________________
280 Reference< XConnectionPoint > SAL_CALL FrameControl::queryConnectionPoint( const Type& aType ) throw( RuntimeException )
282 // Forwarded to helper class
283 return m_aConnectionPointContainer.queryConnectionPoint( aType );
286 //____________________________________________________________________________________________________________
287 // XConnectionPointContainer
288 //____________________________________________________________________________________________________________
290 void SAL_CALL FrameControl::advise( const Type& aType ,
291 const Reference< XInterface >& xListener ) throw( RuntimeException )
293 // Forwarded to helper class
294 m_aConnectionPointContainer.advise( aType, xListener );
297 //____________________________________________________________________________________________________________
298 // XConnectionPointContainer
299 //____________________________________________________________________________________________________________
301 void SAL_CALL FrameControl::unadvise( const Type& aType ,
302 const Reference< XInterface >& xListener ) throw( RuntimeException )
304 // Forwarded to helper class
305 m_aConnectionPointContainer.unadvise( aType, xListener );
308 //____________________________________________________________________________________________________________
309 // impl but public method to register service
310 //____________________________________________________________________________________________________________
312 const Sequence< OUString > FrameControl::impl_getStaticSupportedServiceNames()
314 MutexGuard aGuard( Mutex::getGlobalMutex() );
315 Sequence< OUString > seqServiceNames( 1 );
316 seqServiceNames.getArray() [0] = OUString::createFromAscii( SERVICENAME_FRAMECONTROL );
317 return seqServiceNames ;
320 //____________________________________________________________________________________________________________
321 // impl but public method to register service
322 //____________________________________________________________________________________________________________
324 const OUString FrameControl::impl_getStaticImplementationName()
326 return OUString::createFromAscii( IMPLEMENTATIONNAME_FRAMECONTROL );
329 //____________________________________________________________________________________________________________
330 // OPropertySetHelper
331 //____________________________________________________________________________________________________________
333 sal_Bool FrameControl::convertFastPropertyValue( Any& rConvertedValue ,
334 Any& rOldValue ,
335 sal_Int32 nHandle ,
336 const Any& rValue ) throw( IllegalArgumentException )
338 sal_Bool bReturn = sal_False ;
339 switch (nHandle)
341 case PROPERTYHANDLE_COMPONENTURL : rConvertedValue = rValue ;
342 rOldValue <<= m_sComponentURL ;
343 bReturn = sal_True ;
344 break ;
346 case PROPERTYHANDLE_LOADERARGUMENTS : rConvertedValue = rValue ;
347 rOldValue <<= m_seqLoaderArguments ;
348 bReturn = sal_True ;
349 break ;
352 if ( bReturn == sal_False )
354 throw IllegalArgumentException();
357 return bReturn ;
360 //____________________________________________________________________________________________________________
361 // OPropertySetHelper
362 //____________________________________________________________________________________________________________
364 void FrameControl::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
365 const Any& rValue )
366 throw ( ::com::sun::star::uno::Exception )
368 // this method only set the value
369 MutexGuard aGuard (m_aMutex) ;
370 switch (nHandle)
372 case PROPERTYHANDLE_COMPONENTURL : rValue >>= m_sComponentURL ;
373 if (getPeer().is())
375 impl_createFrame ( getPeer(), m_sComponentURL, m_seqLoaderArguments ) ;
377 break ;
379 case PROPERTYHANDLE_LOADERARGUMENTS : rValue >>= m_seqLoaderArguments ;
380 break ;
382 default : VOS_ENSHURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
386 //____________________________________________________________________________________________________________
387 // OPropertySetHelper
388 //____________________________________________________________________________________________________________
390 void FrameControl::getFastPropertyValue( Any& rRet ,
391 sal_Int32 nHandle ) const
393 MutexGuard aGuard ( Mutex::getGlobalMutex() ) ;
395 switch (nHandle)
397 case PROPERTYHANDLE_COMPONENTURL : rRet <<= m_sComponentURL ;
398 break ;
400 case PROPERTYHANDLE_LOADERARGUMENTS : rRet <<= m_seqLoaderArguments ;
401 break ;
403 case PROPERTYHANDLE_FRAME : rRet <<= m_xFrame ;
404 break ;
406 default : VOS_ENSHURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
410 //____________________________________________________________________________________________________________
411 // OPropertySetHelper
412 //____________________________________________________________________________________________________________
414 IPropertyArrayHelper& FrameControl::getInfoHelper()
416 // Create a table that map names to index values.
417 static OPropertyArrayHelper* pInfo ;
419 if (!pInfo)
421 // global method must be guarded
422 MutexGuard aGuard ( Mutex::getGlobalMutex() ) ;
424 if (!pInfo)
426 pInfo = new OPropertyArrayHelper( impl_getStaticPropertyDescriptor(), sal_True );
430 return *pInfo ;
433 //--------------------------------------------------------------------------------------------------
434 // start OConnectionPointContainerHelper
435 //--------------------------------------------------------------------------------------------------
436 Uik* FrameControl::getConnectionPointUiks ( sal_Int32* pCount ) const
438 static Uik szUiks[] =
440 ((XEventListener*)NULL)->getSmartUik (),
441 ::getCppuType((const Reference< XPropertyChangeListener >*)0),
442 ::getCppuType((const Reference< XVetoableChangeListener >*)0),
443 ::getCppuType((const Reference< XPropertiesChangeListener >*)0)
446 *pCount = 4 ;
448 return szUiks ;
450 //--------------------------------------------------------------------------------------------------
451 // end OConnectionPointContainerHelper
452 //--------------------------------------------------------------------------------------------------
455 //____________________________________________________________________________________________________________
456 // OPropertySetHelper
457 //____________________________________________________________________________________________________________
459 Reference< XPropertySetInfo > SAL_CALL FrameControl::getPropertySetInfo() throw( RuntimeException )
461 // Optimize this method !
462 // We initialize a static variable only one time. And we don't must use a mutex at every call!
463 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
464 static Reference< XPropertySetInfo >* pInfo = (Reference< XPropertySetInfo >*)0 ;
465 if ( pInfo == (Reference< XPropertySetInfo >*)0 )
467 // Ready for multithreading
468 MutexGuard aGuard ( Mutex::getGlobalMutex () ) ;
469 // Control this pointer again, another instance can be faster then these!
470 if ( pInfo == (Reference< XPropertySetInfo >*)0 )
472 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
473 // (Use method "getInfoHelper()".)
474 static Reference< XPropertySetInfo > xInfo ( createPropertySetInfo ( getInfoHelper () ) ) ;
475 pInfo = &xInfo ;
478 return ( *pInfo ) ;
481 //____________________________________________________________________________________________________________
482 // BaseControl
483 //____________________________________________________________________________________________________________
485 WindowDescriptor* FrameControl::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
487 WindowDescriptor* pDescriptor = new WindowDescriptor ;
489 pDescriptor->Type = WindowClass_CONTAINER ;
490 pDescriptor->ParentIndex = -1 ;
491 pDescriptor->Parent = xParentPeer ;
492 pDescriptor->Bounds = getPosSize () ;
493 pDescriptor->WindowAttributes = 0 ;
495 return pDescriptor ;
498 //____________________________________________________________________________________________________________
499 // private method
500 //____________________________________________________________________________________________________________
502 void FrameControl::impl_createFrame( const Reference< XWindowPeer >& xPeer ,
503 const OUString& rURL ,
504 const Sequence< PropertyValue >& rArguments )
506 Reference< XFrame > xOldFrame ;
507 Reference< XFrame > xNewFrame ;
510 MutexGuard aGuard ( m_aMutex ) ;
511 xOldFrame = m_xFrame ;
514 xNewFrame = Reference< XFrame > ( impl_getMultiServiceFactory()->createInstance ( OUString::createFromAscii( "com.sun.star.frame.Frame" ) ), UNO_QUERY ) ;
515 Reference< XDispatchProvider > xDSP ( xNewFrame, UNO_QUERY ) ;
517 if (xDSP.is())
519 Reference< XWindow > xWP ( xPeer, UNO_QUERY ) ;
520 xNewFrame->initialize ( xWP ) ;
522 // option
523 //xFrame->setName( "WhatYouWant" );
525 Reference< XURLTransformer > xTrans ( impl_getMultiServiceFactory()->createInstance ( OUString::createFromAscii( "com.sun.star.util.URLTransformer" ) ), UNO_QUERY ) ;
526 if(xTrans.is())
528 // load file
529 URL aURL ;
531 aURL.Complete = rURL ;
532 xTrans->parseStrict( aURL ) ;
534 Reference< XDispatch > xDisp = xDSP->queryDispatch ( aURL, OUString (), FrameSearchFlag::SELF ) ;
535 if (xDisp.is())
537 xDisp->dispatch ( aURL, rArguments ) ;
542 // set the frame
544 MutexGuard aGuard ( m_aMutex ) ;
545 m_xFrame = xNewFrame ;
548 // notify the listeners
549 sal_Int32 nFrameId = PROPERTYHANDLE_FRAME ;
550 Any aNewFrame ( &xNewFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
551 Any aOldFrame ( &xOldFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
553 fire ( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False ) ;
555 if (xOldFrame.is())
557 xOldFrame->dispose () ;
561 //____________________________________________________________________________________________________________
562 // private method
563 //____________________________________________________________________________________________________________
565 void FrameControl::impl_deleteFrame()
567 Reference< XFrame > xOldFrame;
568 Reference< XFrame > xNullFrame;
571 // do not dispose the frame in this guarded section (deadlock?)
572 MutexGuard aGuard( m_aMutex );
573 xOldFrame = m_xFrame;
574 m_xFrame = Reference< XFrame > ();
577 // notify the listeners
578 sal_Int32 nFrameId = PROPERTYHANDLE_FRAME;
579 Any aNewFrame( &xNullFrame, ::getCppuType((const Reference< XFrame >*)0) );
580 Any aOldFrame( &xOldFrame, ::getCppuType((const Reference< XFrame >*)0) );
581 fire( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False );
583 // dispose the frame
584 if( xOldFrame.is() )
585 xOldFrame->dispose();
588 //____________________________________________________________________________________________________________
589 // private method
590 //____________________________________________________________________________________________________________
592 const Sequence< Property > FrameControl::impl_getStaticPropertyDescriptor()
594 // All Properties of this implementation. The array must be sorted!
595 static const Property pPropertys[PROPERTY_COUNT] =
597 Property( OUString::createFromAscii( PROPERTYNAME_COMPONENTURL ), PROPERTYHANDLE_COMPONENTURL , ::getCppuType((const OUString*)0) , PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
598 Property( OUString::createFromAscii( PROPERTYNAME_FRAME ), PROPERTYHANDLE_FRAME , ::getCppuType((const Reference< XFrame >*)0) , PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT ),
599 Property( OUString::createFromAscii( PROPERTYNAME_LOADERARGUMENTS ), PROPERTYHANDLE_LOADERARGUMENTS , ::getCppuType((const Sequence< PropertyValue >*)0), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED )
602 static const Sequence< Property > seqPropertys( pPropertys, PROPERTY_COUNT );
604 return seqPropertys ;
607 } // namespace unocontrols