Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "framecontrol.hxx"
21 :
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/typeprovider.hxx>
31 : #include <osl/diagnose.h>
32 :
33 : //______________________________________________________________________________________________________________
34 : // namespaces
35 : //______________________________________________________________________________________________________________
36 :
37 : using namespace ::rtl ;
38 : using namespace ::osl ;
39 : using namespace ::cppu ;
40 : using namespace ::com::sun::star::uno ;
41 : using namespace ::com::sun::star::lang ;
42 : using namespace ::com::sun::star::beans ;
43 : using namespace ::com::sun::star::awt ;
44 : using namespace ::com::sun::star::frame ;
45 : using namespace ::com::sun::star::util ;
46 :
47 : namespace unocontrols{
48 :
49 : //______________________________________________________________________________________________________________
50 : // construct/destruct
51 : //______________________________________________________________________________________________________________
52 :
53 0 : FrameControl::FrameControl( const Reference< XComponentContext >& rxContext)
54 : : BaseControl ( rxContext )
55 : , OBroadcastHelper ( m_aMutex )
56 : , OPropertySetHelper ( *(static_cast< OBroadcastHelper * >(this)) )
57 : , m_aInterfaceContainer ( m_aMutex )
58 0 : , m_aConnectionPointContainer ( m_aMutex )
59 : {
60 0 : }
61 :
62 0 : FrameControl::~FrameControl()
63 : {
64 0 : }
65 :
66 : //____________________________________________________________________________________________________________
67 : // XInterface
68 : //____________________________________________________________________________________________________________
69 :
70 0 : Any SAL_CALL FrameControl::queryInterface( const Type& rType ) throw( RuntimeException )
71 : {
72 : // Attention:
73 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
74 0 : Any aReturn ;
75 0 : Reference< XInterface > xDel = BaseControl::impl_getDelegator();
76 0 : if ( xDel.is() )
77 : {
78 : // If an delegator exist, forward question to his queryInterface.
79 : // Delegator will ask his own queryAggregation!
80 0 : aReturn = xDel->queryInterface( rType );
81 : }
82 : else
83 : {
84 : // If an delegator unknown, forward question to own queryAggregation.
85 0 : aReturn = queryAggregation( rType );
86 : }
87 :
88 0 : return aReturn ;
89 : }
90 :
91 : //____________________________________________________________________________________________________________
92 : // XInterface
93 : //____________________________________________________________________________________________________________
94 :
95 0 : void SAL_CALL FrameControl::acquire() throw()
96 : {
97 : // Attention:
98 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
99 :
100 : // Forward to baseclass
101 0 : BaseControl::acquire();
102 0 : }
103 :
104 : //____________________________________________________________________________________________________________
105 : // XInterface
106 : //____________________________________________________________________________________________________________
107 :
108 0 : void SAL_CALL FrameControl::release() throw()
109 : {
110 : // Attention:
111 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
112 :
113 : // Forward to baseclass
114 0 : BaseControl::release();
115 0 : }
116 :
117 : //____________________________________________________________________________________________________________
118 : // XTypeProvider
119 : //____________________________________________________________________________________________________________
120 :
121 0 : Sequence< Type > SAL_CALL FrameControl::getTypes() throw( RuntimeException )
122 : {
123 : // Optimize this method !
124 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
125 : // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
126 : static OTypeCollection* pTypeCollection = NULL ;
127 :
128 0 : if ( pTypeCollection == NULL )
129 : {
130 : // Ready for multithreading; get global mutex for first call of this method only! see before
131 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
132 :
133 : // Control these pointer again ... it can be, that another instance will be faster then these!
134 0 : if ( pTypeCollection == NULL )
135 : {
136 : // Create a static typecollection ...
137 0 : static OTypeCollection aTypeCollection ( ::getCppuType(( const Reference< XControlModel >*)NULL ) ,
138 0 : ::getCppuType(( const Reference< XControlContainer >*)NULL ) ,
139 0 : ::getCppuType(( const Reference< XConnectionPointContainer >*)NULL ) ,
140 : BaseControl::getTypes()
141 0 : );
142 : // ... and set his address to static pointer!
143 0 : pTypeCollection = &aTypeCollection ;
144 0 : }
145 : }
146 :
147 0 : return pTypeCollection->getTypes();
148 : }
149 :
150 : //____________________________________________________________________________________________________________
151 : // XAggregation
152 : //____________________________________________________________________________________________________________
153 :
154 0 : Any SAL_CALL FrameControl::queryAggregation( const Type& aType ) throw( RuntimeException )
155 : {
156 : // Ask for my own supported interfaces ...
157 : // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
158 : Any aReturn ( ::cppu::queryInterface( aType ,
159 : static_cast< XControlModel* > ( this ) ,
160 : static_cast< XConnectionPointContainer* > ( this )
161 : )
162 0 : );
163 :
164 : // If searched interface not supported by this class ...
165 0 : if ( aReturn.hasValue() == sal_False )
166 : {
167 : // ... ask baseclasses.
168 0 : aReturn = OPropertySetHelper::queryInterface( aType );
169 0 : if ( aReturn.hasValue() == sal_False )
170 : {
171 0 : aReturn = BaseControl::queryAggregation( aType );
172 : }
173 : }
174 :
175 0 : return aReturn ;
176 : }
177 :
178 : //____________________________________________________________________________________________________________
179 : // XControl
180 : //____________________________________________________________________________________________________________
181 :
182 0 : void SAL_CALL FrameControl::createPeer( const Reference< XToolkit >& xToolkit ,
183 : const Reference< XWindowPeer >& xParentPeer ) throw( RuntimeException )
184 : {
185 0 : BaseControl::createPeer( xToolkit, xParentPeer );
186 0 : if ( impl_getPeerWindow().is() )
187 : {
188 0 : if( !m_sComponentURL.isEmpty() )
189 : {
190 0 : impl_createFrame( getPeer(), m_sComponentURL, m_seqLoaderArguments );
191 : }
192 : }
193 0 : }
194 :
195 : //____________________________________________________________________________________________________________
196 : // XControl
197 : //____________________________________________________________________________________________________________
198 :
199 0 : sal_Bool SAL_CALL FrameControl::setModel( const Reference< XControlModel >& /*xModel*/ ) throw( RuntimeException )
200 : {
201 : // We have no model.
202 0 : return sal_False ;
203 : }
204 :
205 : //____________________________________________________________________________________________________________
206 : // XControl
207 : //____________________________________________________________________________________________________________
208 :
209 0 : Reference< XControlModel > SAL_CALL FrameControl::getModel() throw( RuntimeException )
210 : {
211 : // We have no model.
212 0 : return Reference< XControlModel >();
213 : }
214 :
215 : //____________________________________________________________________________________________________________
216 : // XControl
217 : //____________________________________________________________________________________________________________
218 :
219 0 : void SAL_CALL FrameControl::dispose() throw( RuntimeException )
220 : {
221 0 : impl_deleteFrame();
222 0 : BaseControl::dispose();
223 0 : }
224 :
225 : //____________________________________________________________________________________________________________
226 : // XView
227 : //____________________________________________________________________________________________________________
228 :
229 0 : sal_Bool SAL_CALL FrameControl::setGraphics( const Reference< XGraphics >& /*xDevice*/ ) throw( RuntimeException )
230 : {
231 : // it is not possible to print this control
232 0 : return sal_False ;
233 : }
234 :
235 : //____________________________________________________________________________________________________________
236 : // XView
237 : //____________________________________________________________________________________________________________
238 :
239 0 : Reference< XGraphics > SAL_CALL FrameControl::getGraphics() throw( RuntimeException )
240 : {
241 : // when its not posible to set graphics ! then its possible to return null
242 0 : return Reference< XGraphics >();
243 : }
244 :
245 : //____________________________________________________________________________________________________________
246 : // XConnectionPointContainer
247 : //____________________________________________________________________________________________________________
248 :
249 0 : Sequence< Type > SAL_CALL FrameControl::getConnectionPointTypes() throw( RuntimeException )
250 : {
251 : // Forwarded to helper class
252 0 : return m_aConnectionPointContainer.getConnectionPointTypes();
253 : }
254 :
255 : //____________________________________________________________________________________________________________
256 : // XConnectionPointContainer
257 : //____________________________________________________________________________________________________________
258 :
259 0 : Reference< XConnectionPoint > SAL_CALL FrameControl::queryConnectionPoint( const Type& aType ) throw( RuntimeException )
260 : {
261 : // Forwarded to helper class
262 0 : return m_aConnectionPointContainer.queryConnectionPoint( aType );
263 : }
264 :
265 : //____________________________________________________________________________________________________________
266 : // XConnectionPointContainer
267 : //____________________________________________________________________________________________________________
268 :
269 0 : void SAL_CALL FrameControl::advise( const Type& aType ,
270 : const Reference< XInterface >& xListener ) throw( RuntimeException )
271 : {
272 : // Forwarded to helper class
273 0 : m_aConnectionPointContainer.advise( aType, xListener );
274 0 : }
275 :
276 : //____________________________________________________________________________________________________________
277 : // XConnectionPointContainer
278 : //____________________________________________________________________________________________________________
279 :
280 0 : void SAL_CALL FrameControl::unadvise( const Type& aType ,
281 : const Reference< XInterface >& xListener ) throw( RuntimeException )
282 : {
283 : // Forwarded to helper class
284 0 : m_aConnectionPointContainer.unadvise( aType, xListener );
285 0 : }
286 :
287 : //____________________________________________________________________________________________________________
288 : // impl but public method to register service
289 : //____________________________________________________________________________________________________________
290 :
291 0 : const Sequence< OUString > FrameControl::impl_getStaticSupportedServiceNames()
292 : {
293 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
294 0 : Sequence< OUString > seqServiceNames( 1 );
295 0 : seqServiceNames.getArray() [0] = SERVICENAME_FRAMECONTROL;
296 0 : return seqServiceNames ;
297 : }
298 :
299 : //____________________________________________________________________________________________________________
300 : // impl but public method to register service
301 : //____________________________________________________________________________________________________________
302 :
303 0 : const OUString FrameControl::impl_getStaticImplementationName()
304 : {
305 0 : return OUString(IMPLEMENTATIONNAME_FRAMECONTROL);
306 : }
307 :
308 : //____________________________________________________________________________________________________________
309 : // OPropertySetHelper
310 : //____________________________________________________________________________________________________________
311 :
312 0 : sal_Bool FrameControl::convertFastPropertyValue( Any& rConvertedValue ,
313 : Any& rOldValue ,
314 : sal_Int32 nHandle ,
315 : const Any& rValue ) throw( IllegalArgumentException )
316 : {
317 0 : sal_Bool bReturn = sal_False ;
318 0 : switch (nHandle)
319 : {
320 0 : case PROPERTYHANDLE_COMPONENTURL : rConvertedValue = rValue ;
321 0 : rOldValue <<= m_sComponentURL ;
322 0 : bReturn = sal_True ;
323 0 : break ;
324 :
325 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rConvertedValue = rValue ;
326 0 : rOldValue <<= m_seqLoaderArguments ;
327 0 : bReturn = sal_True ;
328 0 : break ;
329 : }
330 :
331 0 : if ( bReturn == sal_False )
332 : {
333 0 : throw IllegalArgumentException();
334 : }
335 :
336 0 : return bReturn ;
337 : }
338 :
339 : //____________________________________________________________________________________________________________
340 : // OPropertySetHelper
341 : //____________________________________________________________________________________________________________
342 :
343 0 : void FrameControl::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
344 : const Any& rValue )
345 : throw ( ::com::sun::star::uno::Exception )
346 : {
347 : // this method only set the value
348 0 : MutexGuard aGuard (m_aMutex) ;
349 0 : switch (nHandle)
350 : {
351 0 : case PROPERTYHANDLE_COMPONENTURL : rValue >>= m_sComponentURL ;
352 0 : if (getPeer().is())
353 : {
354 0 : impl_createFrame ( getPeer(), m_sComponentURL, m_seqLoaderArguments ) ;
355 : }
356 0 : break ;
357 :
358 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rValue >>= m_seqLoaderArguments ;
359 0 : break ;
360 :
361 : default : OSL_ENSURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
362 0 : }
363 0 : }
364 :
365 : //____________________________________________________________________________________________________________
366 : // OPropertySetHelper
367 : //____________________________________________________________________________________________________________
368 :
369 0 : void FrameControl::getFastPropertyValue( Any& rRet ,
370 : sal_Int32 nHandle ) const
371 : {
372 0 : MutexGuard aGuard ( Mutex::getGlobalMutex() ) ;
373 :
374 0 : switch (nHandle)
375 : {
376 0 : case PROPERTYHANDLE_COMPONENTURL : rRet <<= m_sComponentURL ;
377 0 : break ;
378 :
379 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rRet <<= m_seqLoaderArguments ;
380 0 : break ;
381 :
382 0 : case PROPERTYHANDLE_FRAME : rRet <<= m_xFrame ;
383 0 : break ;
384 :
385 : default : OSL_ENSURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
386 0 : }
387 0 : }
388 :
389 : //____________________________________________________________________________________________________________
390 : // OPropertySetHelper
391 : //____________________________________________________________________________________________________________
392 :
393 0 : IPropertyArrayHelper& FrameControl::getInfoHelper()
394 : {
395 : // Create a table that map names to index values.
396 : static OPropertyArrayHelper* pInfo ;
397 :
398 0 : if (!pInfo)
399 : {
400 : // global method must be guarded
401 0 : MutexGuard aGuard ( Mutex::getGlobalMutex() ) ;
402 :
403 0 : if (!pInfo)
404 : {
405 0 : pInfo = new OPropertyArrayHelper( impl_getStaticPropertyDescriptor(), sal_True );
406 0 : }
407 : }
408 :
409 0 : return *pInfo ;
410 : }
411 :
412 : //____________________________________________________________________________________________________________
413 : // OPropertySetHelper
414 : //____________________________________________________________________________________________________________
415 :
416 0 : Reference< XPropertySetInfo > SAL_CALL FrameControl::getPropertySetInfo() throw( RuntimeException )
417 : {
418 : // Optimize this method !
419 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
420 : // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
421 : static Reference< XPropertySetInfo >* pInfo = (Reference< XPropertySetInfo >*)0 ;
422 0 : if ( pInfo == (Reference< XPropertySetInfo >*)0 )
423 : {
424 : // Ready for multithreading
425 0 : MutexGuard aGuard ( Mutex::getGlobalMutex () ) ;
426 : // Control this pointer again, another instance can be faster then these!
427 0 : if ( pInfo == (Reference< XPropertySetInfo >*)0 )
428 : {
429 : // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
430 : // (Use method "getInfoHelper()".)
431 0 : static Reference< XPropertySetInfo > xInfo ( createPropertySetInfo ( getInfoHelper () ) ) ;
432 0 : pInfo = &xInfo ;
433 0 : }
434 : }
435 0 : return ( *pInfo ) ;
436 : }
437 :
438 : //____________________________________________________________________________________________________________
439 : // BaseControl
440 : //____________________________________________________________________________________________________________
441 :
442 0 : WindowDescriptor* FrameControl::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
443 : {
444 0 : WindowDescriptor* pDescriptor = new WindowDescriptor ;
445 :
446 0 : pDescriptor->Type = WindowClass_CONTAINER ;
447 0 : pDescriptor->ParentIndex = -1 ;
448 0 : pDescriptor->Parent = xParentPeer ;
449 0 : pDescriptor->Bounds = getPosSize () ;
450 0 : pDescriptor->WindowAttributes = 0 ;
451 :
452 0 : return pDescriptor ;
453 : }
454 :
455 : //____________________________________________________________________________________________________________
456 : // private method
457 : //____________________________________________________________________________________________________________
458 :
459 0 : void FrameControl::impl_createFrame( const Reference< XWindowPeer >& xPeer ,
460 : const OUString& rURL ,
461 : const Sequence< PropertyValue >& rArguments )
462 : {
463 0 : Reference< XFrame2 > xOldFrame ;
464 0 : Reference< XFrame2 > xNewFrame ;
465 :
466 : {
467 0 : MutexGuard aGuard ( m_aMutex ) ;
468 0 : xOldFrame = m_xFrame ;
469 : }
470 :
471 :
472 :
473 0 : xNewFrame = Frame::create( impl_getComponentContext() );
474 :
475 0 : Reference< XWindow > xWP ( xPeer, UNO_QUERY ) ;
476 0 : xNewFrame->initialize ( xWP ) ;
477 :
478 : // option
479 : //xFrame->setName( "WhatYouWant" );
480 :
481 0 : Reference< XURLTransformer > xTrans = URLTransformer::create( impl_getComponentContext() );
482 : // load file
483 0 : URL aURL ;
484 0 : aURL.Complete = rURL ;
485 0 : xTrans->parseStrict( aURL ) ;
486 :
487 0 : Reference< XDispatch > xDisp = xNewFrame->queryDispatch ( aURL, OUString (), FrameSearchFlag::SELF ) ;
488 0 : if (xDisp.is())
489 : {
490 0 : xDisp->dispatch ( aURL, rArguments ) ;
491 : }
492 :
493 : // set the frame
494 : {
495 0 : MutexGuard aGuard ( m_aMutex ) ;
496 0 : m_xFrame = xNewFrame ;
497 : }
498 :
499 : // notify the listeners
500 0 : sal_Int32 nFrameId = PROPERTYHANDLE_FRAME ;
501 0 : Any aNewFrame ( &xNewFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
502 0 : Any aOldFrame ( &xOldFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
503 :
504 0 : fire ( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False ) ;
505 :
506 0 : if (xOldFrame.is())
507 : {
508 0 : xOldFrame->dispose () ;
509 0 : }
510 0 : }
511 :
512 : //____________________________________________________________________________________________________________
513 : // private method
514 : //____________________________________________________________________________________________________________
515 :
516 0 : void FrameControl::impl_deleteFrame()
517 : {
518 0 : Reference< XFrame2 > xOldFrame;
519 0 : Reference< XFrame2 > xNullFrame;
520 :
521 : {
522 : // do not dispose the frame in this guarded section (deadlock?)
523 0 : MutexGuard aGuard( m_aMutex );
524 0 : xOldFrame = m_xFrame;
525 0 : m_xFrame = Reference< XFrame2 > ();
526 : }
527 :
528 : // notify the listeners
529 0 : sal_Int32 nFrameId = PROPERTYHANDLE_FRAME;
530 0 : Any aNewFrame( &xNullFrame, ::getCppuType((const Reference< XFrame2 >*)0) );
531 0 : Any aOldFrame( &xOldFrame, ::getCppuType((const Reference< XFrame2 >*)0) );
532 0 : fire( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False );
533 :
534 : // dispose the frame
535 0 : if( xOldFrame.is() )
536 0 : xOldFrame->dispose();
537 0 : }
538 :
539 : //____________________________________________________________________________________________________________
540 : // private method
541 : //____________________________________________________________________________________________________________
542 :
543 0 : const Sequence< Property > FrameControl::impl_getStaticPropertyDescriptor()
544 : {
545 : // All Properties of this implementation. The array must be sorted!
546 : static const Property pPropertys[PROPERTY_COUNT] =
547 : {
548 0 : Property( PROPERTYNAME_COMPONENTURL, PROPERTYHANDLE_COMPONENTURL, ::getCppuType((const OUString*)0), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
549 0 : Property( PROPERTYNAME_FRAME, PROPERTYHANDLE_FRAME, ::getCppuType((const Reference< XFrame >*)0), PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT ),
550 0 : Property( PROPERTYNAME_LOADERARGUMENTS, PROPERTYHANDLE_LOADERARGUMENTS, ::getCppuType((const Sequence< PropertyValue >*)0), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED )
551 0 : };
552 :
553 0 : static const Sequence< Property > seqPropertys( pPropertys, PROPERTY_COUNT );
554 :
555 0 : return seqPropertys ;
556 : }
557 :
558 : } // namespace unocontrols
559 :
560 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|