1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "framecontrol.hxx"
22 #include <com/sun/star/beans/PropertyAttribute.hpp>
23 #include <com/sun/star/frame/Frame.hpp>
24 #include <com/sun/star/frame/FrameSearchFlag.hpp>
25 #include <com/sun/star/frame/XDispatchProvider.hpp>
26 #include <com/sun/star/frame/XDispatch.hpp>
27 #include <com/sun/star/util/URLTransformer.hpp>
28 #include <com/sun/star/util/XURLTransformer.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <osl/diagnose.h>
36 using namespace ::osl
;
37 using namespace ::cppu
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::awt
;
42 using namespace ::com::sun::star::frame
;
43 using namespace ::com::sun::star::util
;
45 namespace unocontrols
{
49 FrameControl::FrameControl( const Reference
< XComponentContext
>& rxContext
)
50 : BaseControl ( rxContext
)
51 , OBroadcastHelper ( m_aMutex
)
52 , OPropertySetHelper ( *(static_cast< OBroadcastHelper
* >(this)) )
53 , m_aInterfaceContainer ( m_aMutex
)
54 , m_aConnectionPointContainer ( m_aMutex
)
58 FrameControl::~FrameControl()
64 Any SAL_CALL
FrameControl::queryInterface( const Type
& rType
) throw( RuntimeException
, std::exception
)
67 // Don't use mutex or guard in this method!!! Is a method of XInterface.
69 Reference
< XInterface
> xDel
= BaseControl::impl_getDelegator();
72 // If an delegator exist, forward question to his queryInterface.
73 // Delegator will ask his own queryAggregation!
74 aReturn
= xDel
->queryInterface( rType
);
78 // If an delegator unknown, forward question to own queryAggregation.
79 aReturn
= queryAggregation( rType
);
87 void SAL_CALL
FrameControl::acquire() throw()
90 // Don't use mutex or guard in this method!!! Is a method of XInterface.
92 // Forward to baseclass
93 BaseControl::acquire();
98 void SAL_CALL
FrameControl::release() throw()
101 // Don't use mutex or guard in this method!!! Is a method of XInterface.
103 // Forward to baseclass
104 BaseControl::release();
109 Sequence
< Type
> SAL_CALL
FrameControl::getTypes() throw( RuntimeException
, std::exception
)
111 // Optimize this method !
112 // We initialize a static variable only one time. And we don't must use a mutex at every call!
113 // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
114 static OTypeCollection
* pTypeCollection
= NULL
;
116 if ( pTypeCollection
== NULL
)
118 // Ready for multithreading; get global mutex for first call of this method only! see before
119 MutexGuard
aGuard( Mutex::getGlobalMutex() );
121 // Control these pointer again ... it can be, that another instance will be faster then these!
122 if ( pTypeCollection
== NULL
)
124 // Create a static typecollection ...
125 static OTypeCollection
aTypeCollection ( cppu::UnoType
<XControlModel
>::get(),
126 cppu::UnoType
<XControlContainer
>::get(),
127 cppu::UnoType
<XConnectionPointContainer
>::get(),
128 BaseControl::getTypes()
130 // ... and set his address to static pointer!
131 pTypeCollection
= &aTypeCollection
;
135 return pTypeCollection
->getTypes();
140 Any SAL_CALL
FrameControl::queryAggregation( const Type
& aType
) throw( RuntimeException
, std::exception
)
142 // Ask for my own supported interfaces ...
143 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
144 Any
aReturn ( ::cppu::queryInterface( aType
,
145 static_cast< XControlModel
* > ( this ) ,
146 static_cast< XConnectionPointContainer
* > ( this )
150 // If searched interface not supported by this class ...
151 if ( !aReturn
.hasValue() )
153 // ... ask baseclasses.
154 aReturn
= OPropertySetHelper::queryInterface( aType
);
155 if ( !aReturn
.hasValue() )
157 aReturn
= BaseControl::queryAggregation( aType
);
164 OUString
FrameControl::getImplementationName()
165 throw (css::uno::RuntimeException
, std::exception
)
167 return impl_getStaticImplementationName();
170 css::uno::Sequence
<OUString
> FrameControl::getSupportedServiceNames()
171 throw (css::uno::RuntimeException
, std::exception
)
173 return impl_getStaticSupportedServiceNames();
178 void SAL_CALL
FrameControl::createPeer( const Reference
< XToolkit
>& xToolkit
,
179 const Reference
< XWindowPeer
>& xParentPeer
) throw( RuntimeException
, std::exception
)
181 BaseControl::createPeer( xToolkit
, xParentPeer
);
182 if ( impl_getPeerWindow().is() )
184 if( !m_sComponentURL
.isEmpty() )
186 impl_createFrame( getPeer(), m_sComponentURL
, m_seqLoaderArguments
);
193 sal_Bool SAL_CALL
FrameControl::setModel( const Reference
< XControlModel
>& /*xModel*/ ) throw( RuntimeException
, std::exception
)
201 Reference
< XControlModel
> SAL_CALL
FrameControl::getModel() throw( RuntimeException
, std::exception
)
204 return Reference
< XControlModel
>();
209 void SAL_CALL
FrameControl::dispose() throw( RuntimeException
, std::exception
)
212 BaseControl::dispose();
217 sal_Bool SAL_CALL
FrameControl::setGraphics( const Reference
< XGraphics
>& /*xDevice*/ ) throw( RuntimeException
, std::exception
)
219 // it is not possible to print this control
225 Reference
< XGraphics
> SAL_CALL
FrameControl::getGraphics() throw( RuntimeException
, std::exception
)
227 // when it's not possible to set graphics ! then it's possible to return null
228 return Reference
< XGraphics
>();
231 // XConnectionPointContainer
233 Sequence
< Type
> SAL_CALL
FrameControl::getConnectionPointTypes() throw( RuntimeException
, std::exception
)
235 // Forwarded to helper class
236 return m_aConnectionPointContainer
.getConnectionPointTypes();
239 // XConnectionPointContainer
241 Reference
< XConnectionPoint
> SAL_CALL
FrameControl::queryConnectionPoint( const Type
& aType
) throw( RuntimeException
, std::exception
)
243 // Forwarded to helper class
244 return m_aConnectionPointContainer
.queryConnectionPoint( aType
);
247 // XConnectionPointContainer
249 void SAL_CALL
FrameControl::advise( const Type
& aType
,
250 const Reference
< XInterface
>& xListener
) throw( RuntimeException
, std::exception
)
252 // Forwarded to helper class
253 m_aConnectionPointContainer
.advise( aType
, xListener
);
256 // XConnectionPointContainer
258 void SAL_CALL
FrameControl::unadvise( const Type
& aType
,
259 const Reference
< XInterface
>& xListener
) throw( RuntimeException
, std::exception
)
261 // Forwarded to helper class
262 m_aConnectionPointContainer
.unadvise( aType
, xListener
);
265 // impl but public method to register service
267 const Sequence
< OUString
> FrameControl::impl_getStaticSupportedServiceNames()
269 MutexGuard
aGuard( Mutex::getGlobalMutex() );
270 Sequence
< OUString
> seqServiceNames( 1 );
271 seqServiceNames
.getArray() [0] = SERVICENAME_FRAMECONTROL
;
272 return seqServiceNames
;
275 // impl but public method to register service
277 const OUString
FrameControl::impl_getStaticImplementationName()
279 return OUString(IMPLEMENTATIONNAME_FRAMECONTROL
);
282 // OPropertySetHelper
284 sal_Bool
FrameControl::convertFastPropertyValue( Any
& rConvertedValue
,
287 const Any
& rValue
) throw( IllegalArgumentException
)
289 bool bReturn
= false;
292 case PROPERTYHANDLE_COMPONENTURL
: rConvertedValue
= rValue
;
293 rOldValue
<<= m_sComponentURL
;
297 case PROPERTYHANDLE_LOADERARGUMENTS
: rConvertedValue
= rValue
;
298 rOldValue
<<= m_seqLoaderArguments
;
305 throw IllegalArgumentException();
311 // OPropertySetHelper
313 void FrameControl::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
,
315 throw ( ::com::sun::star::uno::Exception
, std::exception
)
317 // this method only set the value
318 MutexGuard
aGuard (m_aMutex
);
321 case PROPERTYHANDLE_COMPONENTURL
: rValue
>>= m_sComponentURL
;
324 impl_createFrame ( getPeer(), m_sComponentURL
, m_seqLoaderArguments
);
328 case PROPERTYHANDLE_LOADERARGUMENTS
: rValue
>>= m_seqLoaderArguments
;
331 default : OSL_ENSURE ( nHandle
== -1, "This is an invalid property handle." );
335 // OPropertySetHelper
337 void FrameControl::getFastPropertyValue( Any
& rRet
,
338 sal_Int32 nHandle
) const
340 MutexGuard
aGuard ( Mutex::getGlobalMutex() );
344 case PROPERTYHANDLE_COMPONENTURL
: rRet
<<= m_sComponentURL
;
347 case PROPERTYHANDLE_LOADERARGUMENTS
: rRet
<<= m_seqLoaderArguments
;
350 case PROPERTYHANDLE_FRAME
: rRet
<<= m_xFrame
;
353 default : OSL_ENSURE ( nHandle
== -1, "This is an invalid property handle." );
357 // OPropertySetHelper
359 IPropertyArrayHelper
& FrameControl::getInfoHelper()
361 // Create a table that map names to index values.
362 static OPropertyArrayHelper
* pInfo
;
366 // global method must be guarded
367 MutexGuard
aGuard ( Mutex::getGlobalMutex() );
371 pInfo
= new OPropertyArrayHelper( impl_getStaticPropertyDescriptor(), true );
378 // OPropertySetHelper
380 Reference
< XPropertySetInfo
> SAL_CALL
FrameControl::getPropertySetInfo() throw( RuntimeException
, std::exception
)
382 // Optimize this method !
383 // We initialize a static variable only one time. And we don't must use a mutex at every call!
384 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
385 static Reference
< XPropertySetInfo
>* pInfo
= (Reference
< XPropertySetInfo
>*)0;
386 if ( pInfo
== (Reference
< XPropertySetInfo
>*)0 )
388 // Ready for multithreading
389 MutexGuard
aGuard ( Mutex::getGlobalMutex () );
390 // Control this pointer again, another instance can be faster then these!
391 if ( pInfo
== (Reference
< XPropertySetInfo
>*)0 )
393 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
394 // (Use method "getInfoHelper()".)
395 static Reference
< XPropertySetInfo
> xInfo ( createPropertySetInfo ( getInfoHelper () ) );
404 WindowDescriptor
* FrameControl::impl_getWindowDescriptor( const Reference
< XWindowPeer
>& xParentPeer
)
406 WindowDescriptor
* pDescriptor
= new WindowDescriptor
;
408 pDescriptor
->Type
= WindowClass_CONTAINER
;
409 pDescriptor
->ParentIndex
= -1;
410 pDescriptor
->Parent
= xParentPeer
;
411 pDescriptor
->Bounds
= getPosSize ();
412 pDescriptor
->WindowAttributes
= 0;
419 void FrameControl::impl_createFrame( const Reference
< XWindowPeer
>& xPeer
,
420 const OUString
& rURL
,
421 const Sequence
< PropertyValue
>& rArguments
)
423 Reference
< XFrame2
> xOldFrame
;
424 Reference
< XFrame2
> xNewFrame
;
427 MutexGuard
aGuard ( m_aMutex
);
428 xOldFrame
= m_xFrame
;
431 xNewFrame
= Frame::create( impl_getComponentContext() );
433 Reference
< XWindow
> xWP ( xPeer
, UNO_QUERY
);
434 xNewFrame
->initialize ( xWP
);
437 //xFrame->setName( "WhatYouWant" );
439 Reference
< XURLTransformer
> xTrans
= URLTransformer::create( impl_getComponentContext() );
442 aURL
.Complete
= rURL
;
443 xTrans
->parseStrict( aURL
);
445 Reference
< XDispatch
> xDisp
= xNewFrame
->queryDispatch ( aURL
, OUString (), FrameSearchFlag::SELF
);
448 xDisp
->dispatch ( aURL
, rArguments
);
453 MutexGuard
aGuard ( m_aMutex
);
454 m_xFrame
= xNewFrame
;
457 // notify the listeners
458 sal_Int32 nFrameId
= PROPERTYHANDLE_FRAME
;
459 Any
aNewFrame ( &xNewFrame
, cppu::UnoType
<XFrame
>::get());
460 Any
aOldFrame ( &xOldFrame
, cppu::UnoType
<XFrame
>::get());
462 fire ( &nFrameId
, &aNewFrame
, &aOldFrame
, 1, false );
466 xOldFrame
->dispose ();
472 void FrameControl::impl_deleteFrame()
474 Reference
< XFrame2
> xOldFrame
;
475 Reference
< XFrame2
> xNullFrame
;
478 // do not dispose the frame in this guarded section (deadlock?)
479 MutexGuard
aGuard( m_aMutex
);
480 xOldFrame
= m_xFrame
;
481 m_xFrame
= Reference
< XFrame2
> ();
484 // notify the listeners
485 sal_Int32 nFrameId
= PROPERTYHANDLE_FRAME
;
486 Any
aNewFrame( &xNullFrame
, cppu::UnoType
<XFrame2
>::get());
487 Any
aOldFrame( &xOldFrame
, cppu::UnoType
<XFrame2
>::get());
488 fire( &nFrameId
, &aNewFrame
, &aOldFrame
, 1, false );
492 xOldFrame
->dispose();
497 const Sequence
< Property
> FrameControl::impl_getStaticPropertyDescriptor()
499 // All Properties of this implementation. The array must be sorted!
500 static const Property pPropertys
[PROPERTY_COUNT
] =
502 Property( PROPERTYNAME_COMPONENTURL
, PROPERTYHANDLE_COMPONENTURL
, cppu::UnoType
<OUString
>::get(), PropertyAttribute::BOUND
| PropertyAttribute::CONSTRAINED
),
503 Property( PROPERTYNAME_FRAME
, PROPERTYHANDLE_FRAME
, cppu::UnoType
<XFrame
>::get(), PropertyAttribute::BOUND
| PropertyAttribute::TRANSIENT
),
504 Property( PROPERTYNAME_LOADERARGUMENTS
, PROPERTYHANDLE_LOADERARGUMENTS
, cppu::UnoType
<Sequence
<PropertyValue
>>::get(), PropertyAttribute::BOUND
| PropertyAttribute::CONSTRAINED
)
507 static const Sequence
< Property
> seqPropertys( pPropertys
, PROPERTY_COUNT
);
512 } // namespace unocontrols
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */