update dev300-m58
[ooovba.git] / framework / source / helper / oframes.cxx
blob43f968225c3f43c6fa471a7a900d3328dd2593e1
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: oframes.cxx,v $
10 * $Revision: 1.13 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
37 #include <helper/oframes.hxx>
39 #ifndef _FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
40 #include <threadhelp/resetableguard.hxx>
41 #endif
43 //_________________________________________________________________________________________________________________
44 // interface includes
45 //_________________________________________________________________________________________________________________
46 #include <com/sun/star/frame/XDesktop.hpp>
47 #include <com/sun/star/frame/FrameSearchFlag.hpp>
49 //_________________________________________________________________________________________________________________
50 // includes of other projects
51 //_________________________________________________________________________________________________________________
52 #include <vcl/svapp.hxx>
54 //_________________________________________________________________________________________________________________
55 // namespace
56 //_________________________________________________________________________________________________________________
58 namespace framework{
60 using namespace ::com::sun::star::container ;
61 using namespace ::com::sun::star::frame ;
62 using namespace ::com::sun::star::lang ;
63 using namespace ::com::sun::star::uno ;
64 using namespace ::cppu ;
65 using namespace ::osl ;
66 using namespace ::rtl ;
67 using namespace ::std ;
68 using namespace ::vos ;
70 //_________________________________________________________________________________________________________________
71 // non exported const
72 //_________________________________________________________________________________________________________________
74 //_________________________________________________________________________________________________________________
75 // non exported definitions
76 //_________________________________________________________________________________________________________________
78 //_________________________________________________________________________________________________________________
79 // declarations
80 //_________________________________________________________________________________________________________________
82 //*****************************************************************************************************************
83 // constructor
84 //*****************************************************************************************************************
85 OFrames::OFrames( const Reference< XMultiServiceFactory >& xFactory ,
86 const Reference< XFrame >& xOwner ,
87 FrameContainer* pFrameContainer )
88 // Init baseclasses first
89 : ThreadHelpBase ( &Application::GetSolarMutex() )
90 // Init member
91 , m_xFactory ( xFactory )
92 , m_xOwner ( xOwner )
93 , m_pFrameContainer ( pFrameContainer )
94 , m_bRecursiveSearchProtection( sal_False )
96 // Safe impossible cases
97 // Method is not defined for ALL incoming parameters!
98 LOG_ASSERT( impldbg_checkParameter_OFramesCtor( xFactory, xOwner, pFrameContainer ), "OFrames::OFrames()\nInvalid parameter detected!\n" )
101 //*****************************************************************************************************************
102 // (proteced!) destructor
103 //*****************************************************************************************************************
104 OFrames::~OFrames()
106 // Reset instance, free memory ....
107 impl_resetObject();
110 //*****************************************************************************************************************
111 // XFrames
112 //*****************************************************************************************************************
113 void SAL_CALL OFrames::append( const Reference< XFrame >& xFrame ) throw( RuntimeException )
115 // Ready for multithreading
116 ResetableGuard aGuard( m_aLock );
118 // Safe impossible cases
119 // Method is not defined for ALL incoming parameters!
120 LOG_ASSERT( impldbg_checkParameter_append( xFrame ), "OFrames::append()\nInvalid parameter detected!\n" )
122 // Do the follow only, if owner instance valid!
123 // Lock owner for follow operations - make a "hard reference"!
124 Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
125 if ( xOwner.is() == sal_True )
127 // Append frame to the end of the container ...
128 m_pFrameContainer->append( xFrame );
129 // Set owner of this instance as parent of the new frame in container!
130 xFrame->setCreator( xOwner );
132 // Else; Do nothing! Ouer owner is dead.
133 LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::append()\nOuer owner is dead - you can't append any frames ...!\n" )
136 //*****************************************************************************************************************
137 // XFrames
138 //*****************************************************************************************************************
139 void SAL_CALL OFrames::remove( const Reference< XFrame >& xFrame ) throw( RuntimeException )
141 // Ready for multithreading
142 ResetableGuard aGuard( m_aLock );
144 // Safe impossible cases
145 // Method is not defined for ALL incoming parameters!
146 LOG_ASSERT( impldbg_checkParameter_remove( xFrame ), "OFrames::remove()\nInvalid parameter detected!\n" )
148 // Do the follow only, if owner instance valid!
149 // Lock owner for follow operations - make a "hard reference"!
150 Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
151 if ( xOwner.is() == sal_True )
153 // Search frame and remove it from container ...
154 m_pFrameContainer->remove( xFrame );
155 // Don't reset owner-property of removed frame!
156 // This must do the caller of this method himself.
157 // See documentation of interface XFrames for further informations.
159 // Else; Do nothing! Ouer owner is dead.
160 LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::remove()\nOuer owner is dead - you can't remove any frames ...!\n" )
163 //*****************************************************************************************************************
164 // XFrames
165 //*****************************************************************************************************************
166 Sequence< Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags ) throw( RuntimeException )
168 // Ready for multithreading
169 ResetableGuard aGuard( m_aLock );
171 // Safe impossible cases
172 // Method is not defined for ALL incoming parameters!
173 LOG_ASSERT( impldbg_checkParameter_queryFrames( nSearchFlags ), "OFrames::queryFrames()\nInvalid parameter detected!\n" )
175 // Set default return value. (empty sequence)
176 Sequence< Reference< XFrame > > seqFrames;
178 // Do the follow only, if owner instance valid.
179 // Lock owner for follow operations - make a "hard reference"!
180 Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
181 if ( xOwner.is() == sal_True )
183 // Work only, if search was not started here ...!
184 if( m_bRecursiveSearchProtection == sal_False )
186 // This class is a helper for services, which must implement XFrames.
187 // His parent and childs are MY parent and childs to.
188 // All searchflags are supported by this implementation!
189 // If some flags should not be supported - don't call me with this flags!!!
191 //_____________________________________________________________________________________________________________
192 // Search with AUTO-flag is not supported yet!
193 // We think about right implementation.
194 LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch with AUTO-flag is not supported yet!\nWe think about right implementation.\n" )
195 // If searched for tasks ...
196 // Its not supported yet.
197 LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch for tasks not supported yet!\n" )
199 //_____________________________________________________________________________________________________________
200 // Search for ALL and GLOBAL is superflous!
201 // We support all necessary flags, from which these two flags are derived.
202 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
203 // GLOBAL = ALL + TASKS
205 //_____________________________________________________________________________________________________________
206 // Add parent to list ... if any exist!
207 if( nSearchFlags & FrameSearchFlag::PARENT )
209 Reference< XFrame > xParent( xOwner->getCreator(), UNO_QUERY );
210 if( xParent.is() == sal_True )
212 Sequence< Reference< XFrame > > seqParent( 1 );
213 seqParent[0] = xParent;
214 impl_appendSequence( seqFrames, seqParent );
218 //_____________________________________________________________________________________________________________
219 // Add owner to list if SELF is searched.
220 if( nSearchFlags & FrameSearchFlag::SELF )
222 Sequence< Reference< XFrame > > seqSelf( 1 );
223 seqSelf[0] = xOwner;
224 impl_appendSequence( seqFrames, seqSelf );
227 //_____________________________________________________________________________________________________________
228 // Add SIBLINGS to list.
229 if( nSearchFlags & FrameSearchFlag::SIBLINGS )
231 // Else; start a new search.
232 // Protect this instance against recursive calls from parents.
233 m_bRecursiveSearchProtection = sal_True;
234 // Ask parent of my owner for frames and append results to return list.
235 Reference< XFramesSupplier > xParent( xOwner->getCreator(), UNO_QUERY );
236 // If a parent exist ...
237 if ( xParent.is() == sal_True )
239 // ... ask him for right frames.
240 impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
242 // We have all searched informations.
243 // Reset protection-mode.
244 m_bRecursiveSearchProtection = sal_False;
247 //_____________________________________________________________________________________________________________
248 // If searched for children, step over all elements in container and collect the informations.
249 if ( nSearchFlags & FrameSearchFlag::CHILDREN )
251 // Don't search for parents, siblings and self at childrens!
252 // These things are supported by this instance himself.
253 sal_Int32 nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
254 // Step over all items of container and ask childrens for frames.
255 sal_uInt32 nCount = m_pFrameContainer->getCount();
256 for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
258 // We don't must control this conversion.
259 // We have done this at append()!
260 Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
261 impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
266 // Else; Do nothing! Ouer owner is dead.
267 LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::queryFrames()\nOuer owner is dead - you can't query for frames ...!\n" )
269 // Resturn result of this operation.
270 return seqFrames;
273 //*****************************************************************************************************************
274 // XIndexAccess
275 //*****************************************************************************************************************
276 sal_Int32 SAL_CALL OFrames::getCount() throw( RuntimeException )
278 // Ready for multithreading
279 ResetableGuard aGuard( m_aLock );
281 // Set default return value.
282 sal_Int32 nCount = 0;
284 // Do the follow only, if owner instance valid.
285 // Lock owner for follow operations - make a "hard reference"!
286 Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
287 if ( xOwner.is() == sal_True )
289 // Set CURRENT size of container for return.
290 nCount = m_pFrameContainer->getCount();
293 // Return result.
294 return nCount;
297 //*****************************************************************************************************************
298 // XIndexAccess
299 //*****************************************************************************************************************
300 Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex ) throw( IndexOutOfBoundsException ,
301 WrappedTargetException ,
302 RuntimeException )
304 // Ready for multithreading
305 ResetableGuard aGuard( m_aLock );
307 sal_uInt32 nCount = m_pFrameContainer->getCount();
308 if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
309 throw IndexOutOfBoundsException( OUString::createFromAscii( "OFrames::getByIndex - Index out of bounds" ),
310 (OWeakObject *)this );
312 // Set default return value.
313 Any aReturnValue;
315 // Do the follow only, if owner instance valid.
316 // Lock owner for follow operations - make a "hard reference"!
317 Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
318 if ( xOwner.is() == sal_True )
320 // Get element form container.
321 // (If index not valid, FrameContainer return NULL!)
322 aReturnValue <<= (*m_pFrameContainer)[nIndex];
325 // Return result of this operation.
326 return aReturnValue;
329 //*****************************************************************************************************************
330 // XElementAccess
331 //*****************************************************************************************************************
332 Type SAL_CALL OFrames::getElementType() throw( RuntimeException )
334 // This "container" support XFrame-interfaces only!
335 return ::getCppuType( (const Reference< XFrame >*)NULL );
338 //*****************************************************************************************************************
339 // XElementAccess
340 //*****************************************************************************************************************
341 sal_Bool SAL_CALL OFrames::hasElements() throw( RuntimeException )
343 // Ready for multithreading
344 ResetableGuard aGuard( m_aLock );
346 // Set default return value.
347 sal_Bool bHasElements = sal_False;
348 // Do the follow only, if owner instance valid.
349 // Lock owner for follow operations - make a "hard reference"!
350 Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
351 if ( xOwner.is() == sal_True )
353 // If some elements exist ...
354 if ( m_pFrameContainer->getCount() > 0 )
356 // ... change this state value!
357 bHasElements = sal_True;
360 // Return result of this operation.
361 return bHasElements;
364 //*****************************************************************************************************************
365 // proteced method
366 //*****************************************************************************************************************
367 void OFrames::impl_resetObject()
369 // Attention:
370 // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
371 // It exist two ways to call this method. From destructor and from disposing().
372 // I can't say, which one is the first. Normaly the disposing-call - but other way ....
374 // This instance can't work if the weakreference to owner is invalid!
375 // Destroy this to reset this object.
376 m_xOwner = WeakReference< XFrame >();
377 // Reset pointer to shared container to!
378 m_pFrameContainer = NULL;
381 //*****************************************************************************************************************
382 // private method
383 //*****************************************************************************************************************
384 void OFrames::impl_appendSequence( Sequence< Reference< XFrame > >& seqDestination ,
385 const Sequence< Reference< XFrame > >& seqSource )
387 // Get some informations about the sequences.
388 sal_Int32 nSourceCount = seqSource.getLength();
389 sal_Int32 nDestinationCount = seqDestination.getLength();
390 const Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
391 Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
393 // Get memory for result list.
394 Sequence< Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
395 Reference< XFrame >* pResultAccess = seqResult.getArray();
396 sal_Int32 nResultPosition = 0;
398 // Copy all items from first sequence.
399 for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
401 pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
402 ++nResultPosition;
405 // Don't manipulate nResultPosition between these two loops!
406 // Its the current position in the result list.
408 // Copy all items from second sequence.
409 for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
411 pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
412 ++nResultPosition;
415 // Return result of this operation.
416 seqDestination.realloc( 0 );
417 seqDestination = seqResult;
420 //_________________________________________________________________________________________________________________
421 // debug methods
422 //_________________________________________________________________________________________________________________
424 /*-----------------------------------------------------------------------------------------------------------------
425 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
426 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
428 ATTENTION
430 If you miss a test for one of this parameters, contact the autor or add it himself !(?)
431 But ... look for right testing! See using of this methods!
432 -----------------------------------------------------------------------------------------------------------------*/
434 #ifdef ENABLE_ASSERTIONS
436 //*****************************************************************************************************************
437 // An instance of this class can only work with valid initialization.
438 // We share the mutex with ouer owner class, need a valid factory to instanciate new services and
439 // use the access to ouer owner for some operations.
440 sal_Bool OFrames::impldbg_checkParameter_OFramesCtor( const Reference< XMultiServiceFactory >& xFactory ,
441 const Reference< XFrame >& xOwner ,
442 FrameContainer* pFrameContainer )
444 // Set default return value.
445 sal_Bool bOK = sal_True;
446 // Check parameter.
447 if (
448 ( &xFactory == NULL ) ||
449 ( &xOwner == NULL ) ||
450 ( xFactory.is() == sal_False ) ||
451 ( xOwner.is() == sal_False ) ||
452 ( pFrameContainer == NULL )
455 bOK = sal_False ;
457 // Return result of check.
458 return bOK ;
461 //*****************************************************************************************************************
462 // Its only allowed to add valid references to container.
463 // AND - alle frames must support XFrames-interface!
464 sal_Bool OFrames::impldbg_checkParameter_append( const Reference< XFrame >& xFrame )
466 // Set default return value.
467 sal_Bool bOK = sal_True;
468 // Check parameter.
469 if (
470 ( &xFrame == NULL ) ||
471 ( xFrame.is() == sal_False )
474 bOK = sal_False ;
476 // Return result of check.
477 return bOK ;
480 //*****************************************************************************************************************
481 // Its only allowed to add valid references to container...
482 // ... => You can only delete valid references!
483 sal_Bool OFrames::impldbg_checkParameter_remove( const Reference< XFrame >& xFrame )
485 // Set default return value.
486 sal_Bool bOK = sal_True;
487 // Check parameter.
488 if (
489 ( &xFrame == NULL ) ||
490 ( xFrame.is() == sal_False )
493 bOK = sal_False ;
495 // Return result of check.
496 return bOK ;
499 //*****************************************************************************************************************
500 // A search for frames must initiate with right flags.
501 // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
502 sal_Bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
504 // Set default return value.
505 sal_Bool bOK = sal_True;
506 // Check parameter.
507 if (
508 ( nSearchFlags != FrameSearchFlag::AUTO ) &&
509 ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
510 ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
511 ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
512 ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
513 ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
514 ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
515 ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
516 ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
519 bOK = sal_False ;
521 // Return result of check.
522 return bOK ;
525 #endif // #ifdef ENABLE_ASSERTIONS
527 } // namespace framework