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/FrameSearchFlag.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <vcl/svapp.hxx>
25 #include <sal/log.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 // An instance of this class can only work with valid initialization.
46 // We share the mutex with our owner class, need a valid factory to instantiate new services and
47 // use the access to our owner for some operations.
48 SAL_WARN_IF( !xOwner
.is() || !pFrameContainer
, "fwk", "OFrames::OFrames(): Invalid parameter detected!" );
51 // (protected!) destructor
55 // Reset instance, free memory...
60 void SAL_CALL
OFrames::append( const css::uno::Reference
< XFrame
>& xFrame
)
64 // Safe impossible cases
65 // Method is not defined for ALL incoming parameters!
66 SAL_WARN_IF( !xFrame
.is(), "fwk", "OFrames::append(): Invalid parameter detected!" );
68 // Do the follow only, if owner instance valid!
69 // Lock owner for follow operations - make a "hard reference"!
70 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
73 // Append frame to the end of the container ...
74 m_pFrameContainer
->append( xFrame
);
75 // Set owner of this instance as parent of the new frame in container!
76 xFrame
->setCreator( xOwner
);
78 // Else; Do nothing! Our owner is dead.
79 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
83 void SAL_CALL
OFrames::remove( const css::uno::Reference
< XFrame
>& xFrame
)
87 // Safe impossible cases
88 // Method is not defined for ALL incoming parameters!
89 SAL_WARN_IF( !xFrame
.is(), "fwk", "OFrames::remove(): Invalid parameter detected!" );
91 // Do the follow only, if owner instance valid!
92 // Lock owner for follow operations - make a "hard reference"!
93 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
96 // Search frame and remove it from container ...
97 m_pFrameContainer
->remove( xFrame
);
98 // Don't reset owner-property of removed frame!
99 // This must do the caller of this method himself.
100 // See documentation of interface XFrames for further information.
102 // Else; Do nothing! Our owner is dead.
103 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
107 Sequence
< css::uno::Reference
< XFrame
> > SAL_CALL
OFrames::queryFrames( sal_Int32 nSearchFlags
)
111 // Safe impossible cases
112 // Method is not defined for ALL incoming parameters!
113 SAL_WARN_IF( !impldbg_checkParameter_queryFrames( nSearchFlags
), "fwk", "OFrames::queryFrames(): Invalid parameter detected!" );
115 // Set default return value. (empty sequence)
116 Sequence
< css::uno::Reference
< XFrame
> > seqFrames
;
118 // Do the follow only, if owner instance valid.
119 // Lock owner for follow operations - make a "hard reference"!
120 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
123 // Work only, if search was not started here ...!
124 if( !m_bRecursiveSearchProtection
)
126 // This class is a helper for services, which must implement XFrames.
127 // His parent and children are MY parent and children to.
128 // All searchflags are supported by this implementation!
129 // If some flags should not be supported - don't call me with this flags!!!
131 // Search with AUTO-flag is not supported yet!
132 // We think about right implementation.
133 SAL_WARN_IF( (nSearchFlags
& FrameSearchFlag::AUTO
), "fwk", "OFrames::queryFrames(): Search with AUTO-flag is not supported yet!" );
135 // Search for ALL and GLOBAL is superfluous!
136 // We support all necessary flags, from which these two flags are derived.
137 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
138 // GLOBAL = ALL + TASKS
140 // Add parent to list ... if any exist!
141 if( nSearchFlags
& FrameSearchFlag::PARENT
)
143 css::uno::Reference
< XFrame
> xParent
= xOwner
->getCreator();
146 Sequence
< css::uno::Reference
< XFrame
> > seqParent( 1 );
147 seqParent
[0] = xParent
;
148 impl_appendSequence( seqFrames
, seqParent
);
152 // Add owner to list if SELF is searched.
153 if( nSearchFlags
& FrameSearchFlag::SELF
)
155 Sequence
< css::uno::Reference
< XFrame
> > seqSelf( 1 );
157 impl_appendSequence( seqFrames
, seqSelf
);
160 // Add SIBLINGS to list.
161 if( nSearchFlags
& FrameSearchFlag::SIBLINGS
)
163 // Else; start a new search.
164 // Protect this instance against recursive calls from parents.
165 m_bRecursiveSearchProtection
= true;
166 // Ask parent of my owner for frames and append results to return list.
167 css::uno::Reference
< XFramesSupplier
> xParent
= xOwner
->getCreator();
168 // If a parent exist ...
171 // ... ask him for right frames.
172 impl_appendSequence( seqFrames
, xParent
->getFrames()->queryFrames( nSearchFlags
) );
174 // We have all searched information.
175 // Reset protection-mode.
176 m_bRecursiveSearchProtection
= false;
179 // If searched for children, step over all elements in container and collect the information.
180 if ( nSearchFlags
& FrameSearchFlag::CHILDREN
)
182 // Don't search for parents, siblings and self at children!
183 // These things are supported by this instance himself.
184 sal_Int32
const nChildSearchFlags
= FrameSearchFlag::SELF
| FrameSearchFlag::CHILDREN
;
185 // Step over all items of container and ask children for frames.
186 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
187 for ( sal_uInt32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
189 // We don't must control this conversion.
190 // We have done this at append()!
191 css::uno::Reference
< XFramesSupplier
> xItem( (*m_pFrameContainer
)[nIndex
], UNO_QUERY
);
192 impl_appendSequence( seqFrames
, xItem
->getFrames()->queryFrames( nChildSearchFlags
) );
197 // Else; Do nothing! Our owner is dead.
198 SAL_WARN_IF( !xOwner
.is(), "fwk", "OFrames::queryFrames(): Our owner is dead - you can't query for frames ...!" );
200 // Return result of this operation.
205 sal_Int32 SAL_CALL
OFrames::getCount()
209 // Set default return value.
210 sal_Int32 nCount
= 0;
212 // Do the follow only, if owner instance valid.
213 // Lock owner for follow operations - make a "hard reference"!
214 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
217 // Set CURRENT size of container for return.
218 nCount
= m_pFrameContainer
->getCount();
227 Any SAL_CALL
OFrames::getByIndex( sal_Int32 nIndex
)
231 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
232 if ( nIndex
< 0 || ( sal::static_int_cast
< sal_uInt32
>( nIndex
) >= nCount
))
233 throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
234 static_cast<OWeakObject
*>(this) );
236 // Set default return value.
239 // Do the follow only, if owner instance valid.
240 // Lock owner for follow operations - make a "hard reference"!
241 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
244 // Get element form container.
245 // (If index not valid, FrameContainer return NULL!)
246 aReturnValue
<<= (*m_pFrameContainer
)[nIndex
];
249 // Return result of this operation.
254 Type SAL_CALL
OFrames::getElementType()
256 // This "container" support XFrame-interfaces only!
257 return cppu::UnoType
<XFrame
>::get();
261 sal_Bool SAL_CALL
OFrames::hasElements()
265 // Set default return value.
266 bool bHasElements
= false;
267 // Do the follow only, if owner instance valid.
268 // Lock owner for follow operations - make a "hard reference"!
269 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
272 // If some elements exist ...
273 if ( m_pFrameContainer
->getCount() > 0 )
275 // ... change this state value!
279 // Return result of this operation.
285 void OFrames::impl_resetObject()
288 // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
289 // It exist two ways to call this method. From destructor and from disposing().
290 // I can't say, which one is the first. Normally the disposing-call - but other way...
292 // This instance can't work if the weakreference to owner is invalid!
293 // Destroy this to reset this object.
295 // Reset pointer to shared container to!
296 m_pFrameContainer
= nullptr;
299 void OFrames::impl_appendSequence( Sequence
< css::uno::Reference
< XFrame
> >& seqDestination
,
300 const Sequence
< css::uno::Reference
< XFrame
> >& seqSource
)
302 // Get some information about the sequences.
303 sal_Int32 nSourceCount
= seqSource
.getLength();
304 sal_Int32 nDestinationCount
= seqDestination
.getLength();
305 const css::uno::Reference
< XFrame
>* pSourceAccess
= seqSource
.getConstArray();
306 css::uno::Reference
< XFrame
>* pDestinationAccess
= seqDestination
.getArray();
308 // Get memory for result list.
309 Sequence
< css::uno::Reference
< XFrame
> > seqResult ( nSourceCount
+ nDestinationCount
);
310 css::uno::Reference
< XFrame
>* pResultAccess
= seqResult
.getArray();
311 sal_Int32 nResultPosition
= 0;
313 // Copy all items from first sequence.
314 for ( sal_Int32 nSourcePosition
=0; nSourcePosition
<nSourceCount
; ++nSourcePosition
)
316 pResultAccess
[nResultPosition
] = pSourceAccess
[nSourcePosition
];
320 // Don't manipulate nResultPosition between these two loops!
321 // It's the current position in the result list.
323 // Copy all items from second sequence.
324 for ( sal_Int32 nDestinationPosition
=0; nDestinationPosition
<nDestinationCount
; ++nDestinationPosition
)
326 pResultAccess
[nResultPosition
] = pDestinationAccess
[nDestinationPosition
];
330 // Return result of this operation.
331 seqDestination
.realloc( 0 );
332 seqDestination
= seqResult
;
337 /*-----------------------------------------------------------------------------------------------------------------
338 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
339 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
343 If you miss a test for one of this parameters, contact the author or add it himself !(?)
344 But ... look for right testing! See using of this methods!
345 -----------------------------------------------------------------------------------------------------------------*/
347 // A search for frames must initiate with right flags.
348 // Some one are superfluous and not supported yet. But here we control only the range of incoming parameter!
349 bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags
)
351 // Set default return value.
355 ( nSearchFlags
!= FrameSearchFlag::AUTO
) &&
356 ( !( nSearchFlags
& FrameSearchFlag::PARENT
) ) &&
357 ( !( nSearchFlags
& FrameSearchFlag::SELF
) ) &&
358 ( !( nSearchFlags
& FrameSearchFlag::CHILDREN
) ) &&
359 ( !( nSearchFlags
& FrameSearchFlag::CREATE
) ) &&
360 ( !( nSearchFlags
& FrameSearchFlag::SIBLINGS
) ) &&
361 ( !( nSearchFlags
& FrameSearchFlag::TASKS
) ) &&
362 ( !( nSearchFlags
& FrameSearchFlag::ALL
) ) &&
363 ( !( nSearchFlags
& FrameSearchFlag::GLOBAL
) )
368 // Return result of check.
372 } // namespace framework
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */