Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / ui / layout / FUi_LayoutLayoutItem.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17 /**
18  * @file                FUi_LayoutLayoutItem.cpp
19  * @brief       This is the implementation file for LayoutItem class.
20  *
21  * This file contains the implementation of LayoutItem class.
22  */
23
24 #include "FUi_Control.h"
25 #include "FUi_LayoutLayoutItem.h"
26 #include "FUi_LayoutLayout.h"
27
28 namespace Tizen { namespace Ui { namespace _Layout
29 {
30
31 LayoutItem::LayoutItem(void)
32         : __pOuterControl(null)
33         , __pParentContainer(null)
34 {
35         __measuredSize.w = 0;
36         __measuredSize.h = 0;
37 }
38
39 LayoutItem::~LayoutItem(void)
40 {
41         if (__pParentContainer != null)
42         {
43                 __pParentContainer->OnDestroyItem(*this);
44         }
45 }
46
47 void
48 LayoutItem::SetItemHandler(ILayoutItemHandler* pOuterControl)
49 {
50         __pOuterControl = pOuterControl;
51 }
52
53 result
54 LayoutItem::SetItemWindowRect(const LayoutRect layoutRect)
55 {
56         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
57
58         Tizen::Graphics::Rectangle rect(layoutRect.x, layoutRect.y, layoutRect.w, layoutRect.h);
59         return __pOuterControl->SetItemBounds(rect);
60 }
61
62 void
63 LayoutItem::GetItemWindowRect(LayoutRect& LayoutRect) const
64 {
65         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
66
67         Tizen::Graphics::Rectangle rect = __pOuterControl->GetItemBounds();
68
69         LayoutRect.x = rect.x;
70         LayoutRect.y = rect.y;
71         LayoutRect.w = rect.width;
72         LayoutRect.h = rect.height;
73 }
74
75 void
76 LayoutItem::ConvertWindowToClientBounds(const LayoutRect windowRect, LayoutRect& clientRect)
77 {
78         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
79
80         Tizen::Graphics::Dimension size(windowRect.w, windowRect.h);
81         Tizen::Graphics::Rectangle rect = __pOuterControl->GetItemClientBoundsFromSize(size);
82
83         clientRect.x = rect.x;
84         clientRect.y = rect.y;
85         clientRect.w = rect.width;
86         clientRect.h = rect.height;
87 }
88
89 result
90 LayoutItem::Measure(int width, int height)
91 {
92         LayoutContainer* pContainer = __pParentContainer;
93         if (pContainer == null)
94         {
95                 return E_INVALID_STATE;
96         }
97
98         Layout* pLayout = pContainer->GetLayout();
99         if (pLayout == null)
100         {
101                 return E_INVALID_STATE;
102         }
103
104         LayoutMatchMode widthMode;
105         LayoutMatchMode heightMode;
106         result re = E_SYSTEM;
107         re = pLayout->GetItemWidthMatchMode(*this, widthMode);
108         if (re != E_SUCCESS)
109         {
110                 return re;
111         }
112         re = pLayout->GetItemHeightMatchMode(*this, heightMode);
113         if (re != E_SUCCESS)
114         {
115                 return re;
116         }
117
118         LayoutRect layoutRect = pLayout->GetLayoutRect();
119
120         LayoutMatchMode containerWidthMode = NONE_MODE;
121         LayoutMatchMode containerHeightMode = NONE_MODE;
122
123         LayoutContainer* pContainerParent = pContainer->GetParentContainer();
124         if (pContainerParent != null)
125         {
126                 re = pContainerParent->GetLayout()->GetItemWidthMatchMode(*pContainer, containerWidthMode);
127                 if (re != E_SUCCESS)
128                 {
129                         return re;
130                 }
131                 re = pContainerParent->GetLayout()->GetItemHeightMatchMode(*pContainer, containerHeightMode);
132                 if (re != E_SUCCESS)
133                 {
134                         return re;
135                 }
136         }
137
138         LayoutSize containerSize = pContainer->GetIntendedWindowSize();
139
140         int measuredWidth = 0;
141         int measuredHeight = 0;
142
143         int wrapContentWidth = 0;
144         int wrapContentHeight = 0;
145
146         Tizen::Graphics::Dimension size = __pOuterControl->GetItemContentSize();
147
148         wrapContentWidth = size.width;
149         wrapContentHeight = size.height;
150
151         if (widthMode == NONE_MODE)
152         {
153                 measuredWidth = width;
154         }
155         else if (widthMode == WRAP_CONTENT)
156         {
157                 measuredWidth = wrapContentWidth;
158         }
159         else if (widthMode == MATCH_PARENT)
160         {
161                 if (containerWidthMode != WRAP_CONTENT)
162                 {
163                         measuredWidth = containerSize.w;
164                 }
165                 else
166                 {
167                         measuredWidth = layoutRect.w;
168                 }
169         }
170
171         if (heightMode == NONE_MODE)
172         {
173                 measuredHeight = height;
174         }
175         else if (heightMode == WRAP_CONTENT)
176         {
177                 measuredHeight = wrapContentHeight;
178         }
179         else if (heightMode == MATCH_PARENT)
180         {
181                 if (containerHeightMode != WRAP_CONTENT)
182                 {
183                         measuredHeight = containerSize.h;
184                 }
185                 else
186                 {
187                         measuredHeight = layoutRect.h;
188                 }
189         }
190
191         RunItemMeasure(measuredWidth, measuredHeight);
192
193         LayoutSize minSize;
194         LayoutSize maxSize;
195
196         Tizen::Graphics::Dimension controlMinSize = __pOuterControl->GetItemMinimumSize();
197         Tizen::Graphics::Dimension controlMaxSize = __pOuterControl->GetItemMaximumSize();
198
199         minSize.w = controlMinSize.width;
200         minSize.h = controlMinSize.height;
201
202         maxSize.w = controlMaxSize.width;
203         maxSize.h = controlMaxSize.height;
204
205         if (measuredWidth < minSize.w)
206         {
207                 measuredWidth = minSize.w;
208         }
209         else if (measuredWidth > maxSize.w)
210         {
211                 measuredWidth = maxSize.w;
212         }
213
214         if (measuredHeight < minSize.h)
215         {
216                 measuredHeight = minSize.h;
217         }
218         else if (measuredHeight > maxSize.h)
219         {
220                 measuredHeight = maxSize.h;
221         }
222
223         SetMeasuredSize(measuredWidth, measuredHeight);
224
225         return E_SUCCESS;
226 }
227
228 void
229 LayoutItem::RunItemMeasure(int& width, int& height)
230 {
231         __pOuterControl->OnItemMeasure(width, height);
232 }
233
234 void
235 LayoutItem::Visible(bool visible)
236 {
237         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
238
239         __pOuterControl->SetItemVisibleState(visible);
240 }
241
242 void
243 LayoutItem::SetParentContainer(LayoutContainer* pParentContainer)
244 {
245         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
246         __pParentContainer = pParentContainer;
247 }
248
249 LayoutContainer*
250 LayoutItem::GetParentContainer(void) const
251 {
252         return __pParentContainer;
253 }
254
255 void
256 LayoutItem::SetMeasuredSize(int width, int height)
257 {
258         __measuredSize.w = width;
259         __measuredSize.h = height;
260 }
261
262 void
263 LayoutItem::GetMeasuredSize(int& width, int& height) const
264 {
265         width = __measuredSize.w;
266         height = __measuredSize.h;
267 }
268
269 void
270 LayoutItem::GetMinSize(LayoutSize& minSize) const
271 {
272         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
273         Tizen::Graphics::Dimension controlMinSize = __pOuterControl->GetItemMinimumSize();
274
275         minSize.w = controlMinSize.width;
276         minSize.h = controlMinSize.height;
277 }
278
279 void
280 LayoutItem::GetMaxSize(LayoutSize& maxSize) const
281 {
282         SysAssertf(__pOuterControl != null, "Did not set a ILayoutItemHandler.");
283         Tizen::Graphics::Dimension controlMaxSize = __pOuterControl->GetItemMaximumSize();
284
285         maxSize.w = controlMaxSize.width;
286         maxSize.h = controlMaxSize.height;
287 }
288
289 Layout*
290 LayoutItem::GetIncludedLayout(void) const
291 {
292         if (__pParentContainer != null)
293         {
294                 return __pParentContainer->GetLayout();
295         }
296         return null;
297 }
298
299 result
300 LayoutItem::OnChangeBaseRect(void)
301 {
302         Layout* pLayout = GetIncludedLayout();
303         if (pLayout == null)
304         {
305                 return E_INVALID_STATE;
306         }
307         LayoutRect rect;
308         GetItemWindowRect(rect);
309
310         return pLayout->SetItemBaseRect(*this, rect);
311 }
312
313 }}} // Tizen::Ui::_Layout