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 <framework/framecontainer.hxx>
22 #include <com/sun/star/frame/FrameSearchFlag.hpp>
24 #include <vcl/svapp.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <sal/log.hxx>
30 /**-***************************************************************************************************************
31 @short initialize an empty container
32 @descr The container will be empty then - special features (e.g. the async quit mechanism) are disabled.
34 @threadsafe not necessary - it's not a singleton
35 *****************************************************************************************************************/
36 FrameContainer::FrameContainer()
38 , m_bAsyncQuit ( sal_False ) // default must be "disabled"!
39 , m_aAsyncCall ( LINK( this, FrameContainer, implts_asyncQuit ) )
44 /**-***************************************************************************************************************
45 @short deinitialize may a filled container
46 @descr Special features (if the currently are running) will be disabled and we free all used other resources.
48 @threadsafe not necessary - it's not a singleton
49 *****************************************************************************************************************/
50 FrameContainer::~FrameContainer()
52 // Don't forget to free memory!
54 m_xActiveFrame
.clear();
57 /**-***************************************************************************************************************
58 @short append a new frame to the container
59 @descr We accept the incoming frame only, if it is a valid reference and doesn't exist already.
62 frame, which should be added to this container
63 Must be a valid reference.
66 *****************************************************************************************************************/
67 void FrameContainer::append(const css::uno::Reference
<css::frame::XFrame
>& xFrame
)
69 if (xFrame
.is() && !exist(xFrame
))
72 m_aContainer
.push_back(xFrame
);
76 /**-***************************************************************************************************************
77 @short remove a frame from the container
78 @descr In case we remove the last frame and our internal special feature (the async quit mechanism)
79 was enabled by the desktop instance, we start it.
82 frame, which should be deleted from this container
83 Must be a valid reference.
86 *****************************************************************************************************************/
87 void FrameContainer::remove(const css::uno::Reference
<css::frame::XFrame
>& xFrame
)
91 TFrameContainer::iterator aSearchedItem
92 = ::std::find(m_aContainer
.begin(), m_aContainer
.end(), xFrame
);
93 if (aSearchedItem
!= m_aContainer
.end())
95 m_aContainer
.erase(aSearchedItem
);
97 // If removed frame was the current active frame - reset state variable.
98 if (m_xActiveFrame
== xFrame
)
99 m_xActiveFrame
.clear();
103 /**-***************************************************************************************************************
104 @short check if the given frame currently exist inside the container
106 reference to the queried frame
108 @return <TRUE/> if frame is part of this container
112 *****************************************************************************************************************/
113 bool FrameContainer::exist(const css::uno::Reference
<css::frame::XFrame
>& xFrame
) const
116 return (::std::find(m_aContainer
.begin(), m_aContainer
.end(), xFrame
) != m_aContainer
.end());
119 /**-***************************************************************************************************************
120 @short delete all existing items of the container
122 *****************************************************************************************************************/
123 void FrameContainer::clear()
126 // Clear the container ...
127 m_aContainer
.clear();
128 // ... and don't forget to reset the active frame.
129 // It's a reference to a valid container-item.
130 // But no container item => no active frame!
131 m_xActiveFrame
.clear();
134 /**-***************************************************************************************************************
135 @short returns count of all current existing frames
136 @deprecated This value can't be guaranteed for multithreading environments.
137 So it will be marked as deprecated and should be replaced by "getAllElements()".
139 @return the count of existing container items
142 *****************************************************************************************************************/
143 sal_uInt32
FrameContainer::getCount() const
146 return static_cast<sal_uInt32
>(m_aContainer
.size());
149 /**-***************************************************************************************************************
150 @short returns one item of this container
151 @deprecated This value can't be guaranteed for multithreading environments.
152 So it will be marked as deprecated and should be replaced by "getAllElements()".
155 a value between 0 and (getCount()-1) to address one container item
157 @return a reference to a frame inside the container, which match with given index
160 *****************************************************************************************************************/
161 css::uno::Reference
<css::frame::XFrame
> FrameContainer::operator[](sal_uInt32 nIndex
) const
163 css::uno::Reference
<css::frame::XFrame
> xFrame
;
166 // Get element form container WITH automatic test of ranges!
167 // If index not valid, an out_of_range exception is thrown.
169 xFrame
= m_aContainer
.at(nIndex
);
171 catch (const std::out_of_range
&)
173 // The index is not valid for current container-content - we must handle this case!
174 // We can return the default value ...
175 SAL_INFO("fwk", "FrameContainer::operator[]: Exception caught: std::out_of_range");
180 /**-***************************************************************************************************************
181 @short returns a snapshot of all currently existing frames inside this container
182 @descr Should be used to replace the deprecated functions getCount()/operator[]!
184 @return a list of all frame references inside this container
187 *****************************************************************************************************************/
188 css::uno::Sequence
<css::uno::Reference
<css::frame::XFrame
>> FrameContainer::getAllElements() const
191 return comphelper::containerToSequence(m_aContainer
);
194 /**-***************************************************************************************************************
195 @short set the given frame as the new active one inside this container
196 @descr We accept this frame only, if it's already a part of this container.
199 reference to the new active frame
200 Must be a valid reference and already part of this container.
203 *****************************************************************************************************************/
204 void FrameContainer::setActive(const css::uno::Reference
<css::frame::XFrame
>& xFrame
)
206 if (!xFrame
.is() || exist(xFrame
))
209 m_xActiveFrame
= xFrame
;
213 /**-***************************************************************************************************************
214 @short return the current active frame of this container
215 @descr Value can be null in case the frame was removed from the container and nobody
216 from outside decide which of all others should be the new one...
218 @return a reference to the current active frame
222 *****************************************************************************************************************/
223 css::uno::Reference
<css::frame::XFrame
> FrameContainer::getActive() const
226 return m_xActiveFrame
;
229 /**-***************************************************************************************************************
230 @short implements a simple search based on current container items
231 @descr It can be used for findFrame() and implements a deep down search.
234 target name, which is searched
236 @return reference to the found frame or NULL if not.
239 *****************************************************************************************************************/
240 css::uno::Reference
<css::frame::XFrame
>
241 FrameContainer::searchOnAllChildrens(const OUString
& sName
) const
244 // Step over all child frames. But if direct child isn't the right one search on his children first - before
245 // you go to next direct child of this container!
246 css::uno::Reference
<css::frame::XFrame
> xSearchedFrame
;
247 for (auto const& container
: m_aContainer
)
249 if (container
->getName() == sName
)
251 xSearchedFrame
= container
;
256 xSearchedFrame
= container
->findFrame(sName
, css::frame::FrameSearchFlag::CHILDREN
);
257 if (xSearchedFrame
.is())
261 return xSearchedFrame
;
264 /**-***************************************************************************************************************
265 @short implements a simple search based on current container items
266 @descr It can be used for findFrame() and search on members of this container only!
269 target name, which is searched
271 @return reference to the found frame or NULL if not.
274 *****************************************************************************************************************/
275 css::uno::Reference
<css::frame::XFrame
>
276 FrameContainer::searchOnDirectChildrens(std::u16string_view sName
) const
279 css::uno::Reference
<css::frame::XFrame
> xSearchedFrame
;
280 for (auto const& container
: m_aContainer
)
282 if (container
->getName() == sName
)
284 xSearchedFrame
= container
;
288 return xSearchedFrame
;
291 } // namespace framework
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */