2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUi_LayoutLayoutContainer.cpp
19 * @brief This is the implementation file for LayoutContainer class.
21 * This file contains the implementation of LayoutContainer class.
25 #include "FUi_Control.h"
26 #include "FUi_LayoutLayoutContainer.h"
27 #include "FUi_LayoutLayoutList.h"
28 #include "FUi_LayoutLayout.h"
29 #include "FUi_LayoutAbsoluteLayout.h"
31 namespace Tizen { namespace Ui { namespace _Layout
34 LayoutContainer::LayoutContainer(void)
35 : __pCurrentLayout(null)
36 , __defaultLayoutFlag(false)
38 LayoutSize zeroSize = {0, 0};
39 __intendedWindowSize = zeroSize;
41 __pLayoutList = new (std::nothrow) LayoutList();
42 SysTryReturnVoidResult(NID_UI, __pLayoutList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
45 LayoutContainer::~LayoutContainer(void)
47 Layout* pDefaultLayout = GetDefaultLayout();
48 delete pDefaultLayout;
50 LayoutListNode* pLayoutNode = __pLayoutList->GetFirstNode();
51 while (pLayoutNode != null)
53 Layout* pLayout = pLayoutNode->GetLayout();
56 if (pLayout->SetContainer(null) != E_SUCCESS)
61 pLayoutNode = __pLayoutList->GetNextNode(*pLayoutNode);
67 LayoutContainer::SetCurrentLayout(Layout& layout)
69 if (!__pLayoutList->CheckNodeExists(layout))
71 SysTryReturn(NID_UI, false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] This layout does not exist.");
74 __pCurrentLayout = &layout;
75 __pCurrentLayout->SetUpdateState(true);
81 LayoutContainer::AddLayout(Layout& layout)
85 if (!__pLayoutList->CheckNodeExists(layout))
87 SysTryReturn(NID_UI, !layout.HasLayoutContainer(), E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] This layout already belong to other container.");
88 SysTryReturn(NID_UI, __pLayoutList->AddNode(layout), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
89 r = layout.SetContainer(this);
96 LayoutContainer::GetLayout() const
98 return __pCurrentLayout;
102 LayoutContainer::AddItem(LayoutItem& addItem)
104 LayoutListNode* pCurNode = __pLayoutList->GetFirstNode();
105 Layout* pLayout = null;
106 result r = E_SUCCESS;
110 pLayout = pCurNode->GetLayout();
113 r = pLayout->AddItem(addItem);
116 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
118 pCurNode = __pLayoutList->GetNextNode(*pCurNode);
123 LayoutListNode* pRollBackNode = __pLayoutList->GetFirstNode();
124 while (pRollBackNode != null && pRollBackNode != pCurNode)
126 pLayout = pRollBackNode->GetLayout();
129 pLayout->RemoveItem(addItem);
132 pRollBackNode = __pLayoutList->GetNextNode(*pCurNode);
138 LayoutContainer::RemoveItem(LayoutItem& removeItem)
140 LayoutListNode* pCurNode = __pLayoutList->GetFirstNode();
141 Layout* pLayout = null;
142 result r = E_SUCCESS;
146 pLayout = pCurNode->GetLayout();
149 r |= pLayout->RemoveItem(removeItem);
151 pCurNode = __pLayoutList->GetNextNode(*pCurNode);
154 return (r == E_SUCCESS) ? E_SUCCESS : GetLastResult();
158 LayoutContainer::OnDestroyItem(LayoutItem& item)
160 LayoutListNode* pCurNode = __pLayoutList->GetFirstNode();
161 Layout* pLayout = null;
165 pLayout = pCurNode->GetLayout();
168 pLayout->RemoveItem(item);
170 pCurNode = __pLayoutList->GetNextNode(*pCurNode);
175 LayoutContainer::OnSetDefaultLayout(Layout& deletedLayout)
177 LayoutListNode* pDefaultNode = __pLayoutList->GetFirstNode();
178 if (pDefaultNode == null)
180 return E_INVALID_STATE;
183 Layout* pLayout = pDefaultNode->GetLayout();
186 return E_INVALID_STATE;
189 if (__pCurrentLayout == &deletedLayout)
191 SetCurrentLayout(*pLayout);
199 LayoutContainer::SetIntendedWindowSize(const LayoutSize intendedWindowSize)
201 __intendedWindowSize = intendedWindowSize;
205 LayoutContainer::GetIntendedWindowSize() const
207 return __intendedWindowSize;
211 LayoutContainer::Measure(int width, int height)
213 LayoutContainer* pContainer = GetParentContainer();
214 if (pContainer == null)
216 return E_INVALID_STATE;
219 Layout* pLayout = pContainer->GetLayout();
222 return E_INVALID_STATE;
225 LayoutMatchMode widthMode;
226 LayoutMatchMode heightMode;
227 result re = E_SYSTEM;
228 re = pLayout->GetItemWidthMatchMode(*this, widthMode);
233 re = pLayout->GetItemHeightMatchMode(*this, heightMode);
239 re = LayoutItem::Measure(width, height);
245 int measuredWidth = 0;
246 int measuredHeight = 0;
247 GetMeasuredSize(measuredWidth, measuredHeight);
249 bool wrapContainer = true;
250 LayoutRect itemBounds;
251 pLayout->GetItemBaseRect(*this, itemBounds);
253 if (widthMode == WRAP_CONTENT || heightMode == WRAP_CONTENT)
255 if (measuredWidth != itemBounds.w || measuredHeight != itemBounds.h)
257 wrapContainer = false;
261 if (widthMode == WRAP_CONTENT && wrapContainer)
263 measuredWidth = width;
265 if (heightMode == WRAP_CONTENT && wrapContainer)
267 measuredHeight = height;
270 LayoutRect rect = {0, 0, measuredWidth, measuredHeight};
272 if (__pCurrentLayout != null)
274 LayoutRect calculatedRect;
275 GetItemWindowRect(calculatedRect);
276 calculatedRect.w = measuredWidth;
277 calculatedRect.h = measuredHeight;
278 SetItemWindowRect(calculatedRect);
280 ConvertWindowToClientBounds(calculatedRect, calculatedRect);
282 LayoutSize containerSize = {calculatedRect.w, calculatedRect.h};
284 SetIntendedWindowSize(containerSize);
285 __pCurrentLayout->OnLayout(measuredWidth, measuredHeight, true);
286 rect = __pCurrentLayout->GetLayoutRect();
289 if (widthMode == WRAP_CONTENT && wrapContainer)
291 measuredWidth = rect.w;
293 if (heightMode == WRAP_CONTENT && wrapContainer)
295 measuredHeight = rect.h;
298 RunItemMeasure(measuredWidth, measuredHeight);
306 if (measuredWidth < minSize.w)
308 measuredWidth = minSize.w;
310 else if (measuredWidth > maxSize.w)
312 measuredWidth = maxSize.w;
315 if (measuredHeight < minSize.h)
317 measuredHeight = minSize.h;
319 else if (measuredHeight > maxSize.h)
321 measuredHeight = maxSize.h;
324 SetMeasuredSize(measuredWidth, measuredHeight);
330 LayoutContainer::OnDestroyLayout(Layout& layout)
332 LayoutListNode* pLayoutNode = __pLayoutList->GetFirstNode();
333 while (pLayoutNode != null)
335 if (pLayoutNode->GetLayout() == &layout)
337 __pLayoutList->RemoveNode(*pLayoutNode);
340 pLayoutNode = __pLayoutList->GetNextNode(*pLayoutNode);
343 if (&layout == __pCurrentLayout)
345 pLayoutNode = __pLayoutList->GetFirstNode();
347 if (pLayoutNode != null)
349 SetCurrentLayout(*pLayoutNode->GetLayout());
355 LayoutContainer::SetDefaultLayout(Layout& layout)
357 if (__defaultLayoutFlag)
359 return E_INVALID_STATE;
362 if (__pLayoutList->GetFirstNode() != null)
364 return E_INVALID_STATE;
367 __defaultLayoutFlag = true;
369 return SetCurrentLayout(layout);
373 LayoutContainer::GetDefaultLayout() const
375 LayoutListNode* pLayoutNode = __pLayoutList->GetFirstNode();
376 if (pLayoutNode == null)
382 return pLayoutNode->GetLayout();
387 LayoutContainer::LayoutExists(Layout& layout)
389 return __pLayoutList->CheckNodeExists(layout);
393 LayoutContainer::OnDestroyContainerProxy()
395 LayoutListNode* pCurNode = __pLayoutList->GetFirstNode();
396 Layout* pLayout = null;
400 pLayout = pCurNode->GetLayout();
403 pLayout->SetContainer(this);
405 pCurNode = __pLayoutList->GetNextNode(*pCurNode);
410 LayoutContainer::GetLayoutList(void)
412 return __pLayoutList;
416 }}} // Tizen::Ui::_Layout