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