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 <helper/oframes.hxx>
22 #include <com/sun/star/frame/XDesktop.hpp>
23 #include <com/sun/star/frame/FrameSearchFlag.hpp>
25 #include <vcl/svapp.hxx>
29 using namespace ::com::sun::star::container
;
30 using namespace ::com::sun::star::frame
;
31 using namespace ::com::sun::star::lang
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::cppu
;
34 using namespace ::osl
;
35 using namespace ::std
;
39 OFrames::OFrames( const css::uno::Reference
< XFrame
>& xOwner
,
40 FrameContainer
* pFrameContainer
)
42 , m_pFrameContainer ( pFrameContainer
)
43 , m_bRecursiveSearchProtection( false )
45 // Safe impossible cases
46 // Method is not defined for ALL incoming parameters!
47 SAL_WARN_IF( !impldbg_checkParameter_OFramesCtor( xOwner
, pFrameContainer
), "fwk", "OFrames::OFrames(): Invalid parameter detected!" );
50 // (proteced!) destructor
54 // Reset instance, free memory ....
59 void SAL_CALL
OFrames::append( const css::uno::Reference
< XFrame
>& xFrame
) throw( RuntimeException
, std::exception
)
63 // Safe impossible cases
64 // Method is not defined for ALL incoming parameters!
65 SAL_WARN_IF( !impldbg_checkParameter_append( xFrame
), "fwk", "OFrames::append(): Invalid parameter detected!" );
67 // Do the follow only, if owner instance valid!
68 // Lock owner for follow operations - make a "hard reference"!
69 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
72 // Append frame to the end of the container ...
73 m_pFrameContainer
->append( xFrame
);
74 // Set owner of this instance as parent of the new frame in container!
75 xFrame
->setCreator( xOwner
);
77 // Else; Do nothing! Our owner is dead.
78 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
82 void SAL_CALL
OFrames::remove( const css::uno::Reference
< XFrame
>& xFrame
) throw( RuntimeException
, std::exception
)
86 // Safe impossible cases
87 // Method is not defined for ALL incoming parameters!
88 SAL_WARN_IF( !impldbg_checkParameter_remove( xFrame
), "fwk", "OFrames::remove(): Invalid parameter detected!" );
90 // Do the follow only, if owner instance valid!
91 // Lock owner for follow operations - make a "hard reference"!
92 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
95 // Search frame and remove it from container ...
96 m_pFrameContainer
->remove( xFrame
);
97 // Don't reset owner-property of removed frame!
98 // This must do the caller of this method himself.
99 // See documentation of interface XFrames for further information.
101 // Else; Do nothing! Our owner is dead.
102 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
106 Sequence
< css::uno::Reference
< XFrame
> > SAL_CALL
OFrames::queryFrames( sal_Int32 nSearchFlags
) throw( RuntimeException
, std::exception
)
110 // Safe impossible cases
111 // Method is not defined for ALL incoming parameters!
112 SAL_WARN_IF( !impldbg_checkParameter_queryFrames( nSearchFlags
), "fwk", "OFrames::queryFrames(): Invalid parameter detected!" );
114 // Set default return value. (empty sequence)
115 Sequence
< css::uno::Reference
< XFrame
> > seqFrames
;
117 // Do the follow only, if owner instance valid.
118 // Lock owner for follow operations - make a "hard reference"!
119 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
122 // Work only, if search was not started here ...!
123 if( !m_bRecursiveSearchProtection
)
125 // This class is a helper for services, which must implement XFrames.
126 // His parent and children are MY parent and children to.
127 // All searchflags are supported by this implementation!
128 // If some flags should not be supported - don't call me with this flags!!!
130 // Search with AUTO-flag is not supported yet!
131 // We think about right implementation.
132 SAL_WARN_IF( (nSearchFlags
& FrameSearchFlag::AUTO
), "fwk", "OFrames::queryFrames(): Search with AUTO-flag is not supported yet!" );
134 // Search for ALL and GLOBAL is superflous!
135 // We support all necessary flags, from which these two flags are derived.
136 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
137 // GLOBAL = ALL + TASKS
139 // Add parent to list ... if any exist!
140 if( nSearchFlags
& FrameSearchFlag::PARENT
)
142 css::uno::Reference
< XFrame
> xParent( xOwner
->getCreator(), UNO_QUERY
);
145 Sequence
< css::uno::Reference
< XFrame
> > seqParent( 1 );
146 seqParent
[0] = xParent
;
147 impl_appendSequence( seqFrames
, seqParent
);
151 // Add owner to list if SELF is searched.
152 if( nSearchFlags
& FrameSearchFlag::SELF
)
154 Sequence
< css::uno::Reference
< XFrame
> > seqSelf( 1 );
156 impl_appendSequence( seqFrames
, seqSelf
);
159 // Add SIBLINGS to list.
160 if( nSearchFlags
& FrameSearchFlag::SIBLINGS
)
162 // Else; start a new search.
163 // Protect this instance against recursive calls from parents.
164 m_bRecursiveSearchProtection
= true;
165 // Ask parent of my owner for frames and append results to return list.
166 css::uno::Reference
< XFramesSupplier
> xParent( xOwner
->getCreator(), UNO_QUERY
);
167 // If a parent exist ...
170 // ... ask him for right frames.
171 impl_appendSequence( seqFrames
, xParent
->getFrames()->queryFrames( nSearchFlags
) );
173 // We have all searched information.
174 // Reset protection-mode.
175 m_bRecursiveSearchProtection
= false;
178 // If searched for children, step over all elements in container and collect the information.
179 if ( nSearchFlags
& FrameSearchFlag::CHILDREN
)
181 // Don't search for parents, siblings and self at children!
182 // These things are supported by this instance himself.
183 sal_Int32 nChildSearchFlags
= FrameSearchFlag::SELF
| FrameSearchFlag::CHILDREN
;
184 // Step over all items of container and ask children for frames.
185 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
186 for ( sal_uInt32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
188 // We don't must control this conversion.
189 // We have done this at append()!
190 css::uno::Reference
< XFramesSupplier
> xItem( (*m_pFrameContainer
)[nIndex
], UNO_QUERY
);
191 impl_appendSequence( seqFrames
, xItem
->getFrames()->queryFrames( nChildSearchFlags
) );
196 // Else; Do nothing! Our owner is dead.
197 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::queryFrames(): Our owner is dead - you can't query for frames ...!" );
199 // Resturn result of this operation.
204 sal_Int32 SAL_CALL
OFrames::getCount() throw( RuntimeException
, std::exception
)
208 // Set default return value.
209 sal_Int32 nCount
= 0;
211 // Do the follow only, if owner instance valid.
212 // Lock owner for follow operations - make a "hard reference"!
213 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
216 // Set CURRENT size of container for return.
217 nCount
= m_pFrameContainer
->getCount();
226 Any SAL_CALL
OFrames::getByIndex( sal_Int32 nIndex
) throw( IndexOutOfBoundsException
,
227 WrappedTargetException
,
228 RuntimeException
, std::exception
)
232 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
233 if ( nIndex
< 0 || ( sal::static_int_cast
< sal_uInt32
>( nIndex
) >= nCount
))
234 throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
235 (OWeakObject
*)this );
237 // Set default return value.
240 // Do the follow only, if owner instance valid.
241 // Lock owner for follow operations - make a "hard reference"!
242 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
245 // Get element form container.
246 // (If index not valid, FrameContainer return NULL!)
247 aReturnValue
<<= (*m_pFrameContainer
)[nIndex
];
250 // Return result of this operation.
255 Type SAL_CALL
OFrames::getElementType() throw( RuntimeException
, std::exception
)
257 // This "container" support XFrame-interfaces only!
258 return cppu::UnoType
<XFrame
>::get();
262 sal_Bool SAL_CALL
OFrames::hasElements() throw( RuntimeException
, std::exception
)
266 // Set default return value.
267 bool bHasElements
= false;
268 // Do the follow only, if owner instance valid.
269 // Lock owner for follow operations - make a "hard reference"!
270 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
273 // If some elements exist ...
274 if ( m_pFrameContainer
->getCount() > 0 )
276 // ... change this state value!
280 // Return result of this operation.
286 void OFrames::impl_resetObject()
289 // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
290 // It exist two ways to call this method. From destructor and from disposing().
291 // I can't say, which one is the first. Normally the disposing-call - but other way ....
293 // This instance can't work if the weakreference to owner is invalid!
294 // Destroy this to reset this object.
295 m_xOwner
= WeakReference
< XFrame
>();
296 // Reset pointer to shared container to!
297 m_pFrameContainer
= NULL
;
300 void OFrames::impl_appendSequence( Sequence
< css::uno::Reference
< XFrame
> >& seqDestination
,
301 const Sequence
< css::uno::Reference
< XFrame
> >& seqSource
)
303 // Get some information about the sequences.
304 sal_Int32 nSourceCount
= seqSource
.getLength();
305 sal_Int32 nDestinationCount
= seqDestination
.getLength();
306 const css::uno::Reference
< XFrame
>* pSourceAccess
= seqSource
.getConstArray();
307 css::uno::Reference
< XFrame
>* pDestinationAccess
= seqDestination
.getArray();
309 // Get memory for result list.
310 Sequence
< css::uno::Reference
< XFrame
> > seqResult ( nSourceCount
+ nDestinationCount
);
311 css::uno::Reference
< XFrame
>* pResultAccess
= seqResult
.getArray();
312 sal_Int32 nResultPosition
= 0;
314 // Copy all items from first sequence.
315 for ( sal_Int32 nSourcePosition
=0; nSourcePosition
<nSourceCount
; ++nSourcePosition
)
317 pResultAccess
[nResultPosition
] = pSourceAccess
[nSourcePosition
];
321 // Don't manipulate nResultPosition between these two loops!
322 // Its the current position in the result list.
324 // Copy all items from second sequence.
325 for ( sal_Int32 nDestinationPosition
=0; nDestinationPosition
<nDestinationCount
; ++nDestinationPosition
)
327 pResultAccess
[nResultPosition
] = pDestinationAccess
[nDestinationPosition
];
331 // Return result of this operation.
332 seqDestination
.realloc( 0 );
333 seqDestination
= seqResult
;
338 /*-----------------------------------------------------------------------------------------------------------------
339 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
340 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
344 If you miss a test for one of this parameters, contact the author or add it himself !(?)
345 But ... look for right testing! See using of this methods!
346 -----------------------------------------------------------------------------------------------------------------*/
348 // An instance of this class can only work with valid initialization.
349 // We share the mutex with our owner class, need a valid factory to instanciate new services and
350 // use the access to our owner for some operations.
351 bool OFrames::impldbg_checkParameter_OFramesCtor( const css::uno::Reference
< XFrame
>& xOwner
,
352 FrameContainer
* pFrameContainer
)
354 return xOwner
.is() && pFrameContainer
!= 0;
357 // Its only allowed to add valid references to container.
358 // AND - alle frames must support XFrames-interface!
359 bool OFrames::impldbg_checkParameter_append( const css::uno::Reference
< XFrame
>& xFrame
)
364 // Its only allowed to add valid references to container...
365 // ... => You can only delete valid references!
366 bool OFrames::impldbg_checkParameter_remove( const css::uno::Reference
< XFrame
>& xFrame
)
371 // A search for frames must initiate with right flags.
372 // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
373 bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags
)
375 // Set default return value.
379 ( nSearchFlags
!= FrameSearchFlag::AUTO
) &&
380 ( !( nSearchFlags
& FrameSearchFlag::PARENT
) ) &&
381 ( !( nSearchFlags
& FrameSearchFlag::SELF
) ) &&
382 ( !( nSearchFlags
& FrameSearchFlag::CHILDREN
) ) &&
383 ( !( nSearchFlags
& FrameSearchFlag::CREATE
) ) &&
384 ( !( nSearchFlags
& FrameSearchFlag::SIBLINGS
) ) &&
385 ( !( nSearchFlags
& FrameSearchFlag::TASKS
) ) &&
386 ( !( nSearchFlags
& FrameSearchFlag::ALL
) ) &&
387 ( !( nSearchFlags
& FrameSearchFlag::GLOBAL
) )
392 // Return result of check.
396 } // namespace framework
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */