1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 //_________________________________________________________________________________________________________________
33 //_________________________________________________________________________________________________________________
35 #ifndef __FRAMEWORK_FRAMECONTAINER_HXX_
36 #include <classes/framecontainer.hxx>
38 #include <threadhelp/writeguard.hxx>
39 #include <threadhelp/readguard.hxx>
41 #ifndef __FRAMEWORK_COMMANDS_HXX_
45 //_________________________________________________________________________________________________________________
47 //_________________________________________________________________________________________________________________
49 #ifndef _COM_SUN_STAR_FRAME_FRAMESEARCH_FLAG_HPP_
50 #include <com/sun/star/frame/FrameSearchFlag.hpp>
53 //_________________________________________________________________________________________________________________
54 // includes of other projects
55 //_________________________________________________________________________________________________________________
56 #include <vcl/svapp.hxx>
58 //_________________________________________________________________________________________________________________
60 //_________________________________________________________________________________________________________________
64 //_________________________________________________________________________________________________________________
66 //_________________________________________________________________________________________________________________
68 //_________________________________________________________________________________________________________________
69 // non exported definitions
70 //_________________________________________________________________________________________________________________
72 //_________________________________________________________________________________________________________________
74 //_________________________________________________________________________________________________________________
76 /**-***************************************************************************************************************
77 @short initialize an empty container
78 @descr The container will be empty then - special features (e.g. the async quit mechanism) are disabled.
80 @threadsafe not neccessary - its not a singleton
81 @modified 01.07.2002 14:42,as96863
82 *****************************************************************************************************************/
83 FrameContainer::FrameContainer()
84 // initialize base classes first.
85 // Order is neccessary for right initilization of his and OUR member ... m_aLock
86 : ThreadHelpBase ( &Application::GetSolarMutex() )
88 , m_bAsyncQuit ( sal_False ) // default must be "disabled"!
89 , m_aAsyncCall ( LINK( this, FrameContainer, implts_asyncQuit ) )
94 /**-***************************************************************************************************************
95 @short deinitialize may a filled container
96 @descr Special features (if the currently are running) will be dsiabled and we free all used other ressources.
98 @threadsafe not neccessary - its not a singleton
99 @modified 01.07.2002 14:43,as96863
100 *****************************************************************************************************************/
101 FrameContainer::~FrameContainer()
103 // Don't forget to free memory!
104 m_aContainer
.clear();
105 m_xActiveFrame
.clear();
108 /**-***************************************************************************************************************
109 @short append a new frame to the container
110 @descr We accept the incoming frame only, if it is a valid reference and dosnt exist already.
113 frame, which should be added to this container
114 Must be a valid reference.
117 @modified 01.07.2002 14:44,as96863
118 *****************************************************************************************************************/
119 void FrameContainer::append( const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
121 if (xFrame
.is() && ! exist(xFrame
))
124 WriteGuard
aWriteLock( m_aLock
);
125 m_aContainer
.push_back( xFrame
);
131 /**-***************************************************************************************************************
132 @short remove a frame from the container
133 @descr In case we remove the last frame and our internal special feature (the async quit mechanism)
134 was enabled by the desktop instance, we start it.
137 frame, which should be deleted from this container
138 Must be a valid reference.
141 @modified 01.07.2002 14:52,as96863
142 *****************************************************************************************************************/
143 void FrameContainer::remove( const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
146 // write lock neccessary for follwing erase()!
147 WriteGuard
aWriteLock( m_aLock
);
149 TFrameIterator aSearchedItem
= ::std::find( m_aContainer
.begin(), m_aContainer
.end(), xFrame
);
150 if (aSearchedItem
!=m_aContainer
.end())
152 m_aContainer
.erase( aSearchedItem
);
154 // If removed frame was the current active frame - reset state variable.
155 if (m_xActiveFrame
==xFrame
)
156 m_xActiveFrame
= css::uno::Reference
< css::frame::XFrame
>();
158 // We don't need the write lock any longer ...
159 // downgrade to read access.
160 aWriteLock
.downgrade();
162 // If last frame was removed and special quit mode is enabled by the desktop
163 // we must terminate the application by using this asynchronous callback!
164 if (m_aContainer.size()<1 && m_bAsyncQuit)
165 m_aAsyncCall.Post(0);
173 /**-***************************************************************************************************************
174 @short check if the given frame currently exist inside the container
178 reference to the queried frame
180 @return <TRUE/> if frame is oart of this container
184 @modified 01.07.2002 14:55,as96863
185 *****************************************************************************************************************/
186 sal_Bool
FrameContainer::exist( const css::uno::Reference
< css::frame::XFrame
>& xFrame
) const
189 ReadGuard
aReadLock( m_aLock
);
190 return( ::std::find( m_aContainer
.begin(), m_aContainer
.end(), xFrame
) != m_aContainer
.end() );
194 /**-***************************************************************************************************************
195 @short delete all existing items of the container
199 @modified 01.07.2002 15:00,as96863
200 *****************************************************************************************************************/
201 void FrameContainer::clear()
204 WriteGuard
aWriteLock( m_aLock
);
206 // Clear the container ...
207 m_aContainer
.clear();
208 // ... and don't forget to reset the active frame.
209 // Its an reference to a valid container-item.
210 // But no container item => no active frame!
211 m_xActiveFrame
= css::uno::Reference
< css::frame::XFrame
>();
213 // If special quit mode is used - we must terminate the desktop.
214 // He is the owner of this container and can't work without any visible tasks/frames!
216 m_aAsyncCall.Post(0);
223 /**-***************************************************************************************************************
224 @short returns count of all current existing frames
227 @deprecated This value can't be guaranteed for multithreading environments.
228 So it will be marked as deprecated and should be replaced by "getAllElements()".
230 @return the count of existing container items
233 @modified 01.07.2002 15:00,as96863
234 *****************************************************************************************************************/
235 sal_uInt32
FrameContainer::getCount() const
238 ReadGuard
aReadLock( m_aLock
);
239 return( (sal_uInt32
)m_aContainer
.size() );
243 /**-***************************************************************************************************************
244 @short returns one item of this container
247 @deprecated This value can't be guaranteed for multithreading environments.
248 So it will be marked as deprecated and should be replaced by "getAllElements()".
251 a valud between 0 and (getCount()-1) to adress one container item
253 @return a reference to a frame inside the container, which match with given index
256 @modified 01.07.2002 15:03,as96863
257 *****************************************************************************************************************/
258 css::uno::Reference
< css::frame::XFrame
> FrameContainer::operator[]( sal_uInt32 nIndex
) const
261 css::uno::Reference
< css::frame::XFrame
> xFrame
;
264 // Get element form container WITH automatic test of ranges!
265 // If index not valid, a out_of_range exception is thrown.
267 ReadGuard
aReadLock( m_aLock
);
268 xFrame
= m_aContainer
.at( nIndex
);
272 catch( std::out_of_range
& )
274 // The index is not valid for current container-content - we must handle this case!
275 // We can return the default value ...
276 LOG_EXCEPTION( "FrameContainer::operator[]", "Exception catched ...", DECLARE_ASCII("::std::out_of_range") )
281 /**-***************************************************************************************************************
282 @short returns a snapshot of all currently existing frames inside this container
283 @descr Should be used to replace the deprecated functions getCount()/operator[]!
285 @return a list of all frame refrences inside this container
288 @modified 01.07.2002 15:09,as96863
289 *****************************************************************************************************************/
290 css::uno::Sequence
< css::uno::Reference
< css::frame::XFrame
> > FrameContainer::getAllElements() const
293 ReadGuard
aReadLock( m_aLock
);
295 sal_Int32 nPosition
= 0;
296 css::uno::Sequence
< css::uno::Reference
< css::frame::XFrame
> > lElements ( (sal_uInt32
)m_aContainer
.size() );
297 for (TConstFrameIterator pItem
=m_aContainer
.begin(); pItem
!=m_aContainer
.end(); ++pItem
)
298 lElements
[nPosition
++] = *pItem
;
306 /**-***************************************************************************************************************
307 @short set the given frame as the new active one inside this container
308 @descr We accept this frame only, if it's already a part of this container.
311 reference to the new active frame
312 Must be a valid reference and already part of this container.
315 @modified 01.07.2002 15:11,as96863
316 *****************************************************************************************************************/
317 void FrameContainer::setActive( const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
319 if ( !xFrame
.is() || exist(xFrame
) )
322 WriteGuard
aWriteLock( m_aLock
);
323 m_xActiveFrame
= xFrame
;
329 /**-***************************************************************************************************************
330 @short return sthe current active frame of this container
331 @descr Value can be null in case the frame was removed from the container and nobody
332 from outside decide which of all others should be the new one ...
334 @return a reference to the current active frame
338 @modified 01.07.2002 15:11,as96863
339 *****************************************************************************************************************/
340 css::uno::Reference
< css::frame::XFrame
> FrameContainer::getActive() const
343 ReadGuard
aReadLock( m_aLock
);
344 return m_xActiveFrame
;
348 /**-***************************************************************************************************************
349 @short implements a simple search based on current container items
350 @descr It can be used for findFrame() and implements a deep down search.
353 target name, which is searched
355 @return reference to the found frame or NULL if not.
358 @modified 01.07.2002 15:22,as96863
359 *****************************************************************************************************************/
360 css::uno::Reference
< css::frame::XFrame
> FrameContainer::searchOnAllChildrens( const ::rtl::OUString
& sName
) const
363 ReadGuard
aReadLock( m_aLock
);
365 // Step over all child frames. But if direct child isn't the right one search on his children first - before
366 // you go to next direct child of this container!
367 css::uno::Reference
< css::frame::XFrame
> xSearchedFrame
;
368 for( TConstFrameIterator pIterator
=m_aContainer
.begin(); pIterator
!=m_aContainer
.end(); ++pIterator
)
370 if ((*pIterator
)->getName()==sName
)
372 xSearchedFrame
= *pIterator
;
377 xSearchedFrame
= (*pIterator
)->findFrame( sName
, css::frame::FrameSearchFlag::CHILDREN
);
378 if (xSearchedFrame
.is())
384 return xSearchedFrame
;
387 /**-***************************************************************************************************************
388 @short implements a simple search based on current container items
389 @descr It can be used for findFrame() and search on members of this container only!
392 target name, which is searched
394 @return reference to the found frame or NULL if not.
397 @modified 01.07.2002 15:22,as96863
398 *****************************************************************************************************************/
399 css::uno::Reference
< css::frame::XFrame
> FrameContainer::searchOnDirectChildrens( const ::rtl::OUString
& sName
) const
402 ReadGuard
aReadLock( m_aLock
);
404 css::uno::Reference
< css::frame::XFrame
> xSearchedFrame
;
405 for( TConstFrameIterator pIterator
=m_aContainer
.begin(); pIterator
!=m_aContainer
.end(); ++pIterator
)
407 if ((*pIterator
)->getName()==sName
)
409 xSearchedFrame
= *pIterator
;
415 return xSearchedFrame
;
418 } // namespace framework