Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_PanelImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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 /**
19  * @file                FUiCtrl_PanelImpl.cpp
20  * @brief       This is the implementation file for the %_PanelImpl class.
21  *
22  */
23
24 #include <FBaseErrors.h>
25 #include <FUiLayout.h>
26 #include <FBaseSysLog.h>
27 #include "FUi_LayoutImpl.h"
28 #include "FUi_LayoutLayoutMaker.h"
29 #include "FUi_DataBindingContextImpl.h"
30 #include "FUi_UiBuilder.h"
31 #include "FUiCtrl_PanelImpl.h"
32 #include "FUiCtrl_FormImpl.h"
33
34 using namespace Tizen::Graphics;
35
36 namespace Tizen { namespace Ui { namespace Controls
37 {
38
39 _PanelImpl::_PanelImpl(Control* pPublic, _Control* pCore, Layout* pPublicPortraitLayout, Layout* pPublicLandscapeLayout)
40         : _ContainerImpl(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout)
41 {
42         // nothing
43 }
44
45 _PanelImpl::~_PanelImpl(void)
46 {
47         // nothing
48 }
49
50 _PanelImpl*
51 _PanelImpl::CreatePanelImplN(Panel* pControl, const FloatRectangle& rect, GroupStyle groupStyle, Layout* pPublicPortraitLayout, Layout* pPublicLandscapeLayout)
52 {
53         ClearLastResult();
54
55         _PanelImpl* pImpl = null;
56         _Panel* pCore = null;
57
58         if (pPublicPortraitLayout != null)
59         {
60                 _LayoutImpl* pPortraitLayoutImpl = _LayoutImpl::GetInstance(*pPublicPortraitLayout);
61                 SysTryReturn(NID_UI_CTRL, pPortraitLayoutImpl != null, null, E_INVALID_ARG, "[%s] Portrait layout is invalid object.", GetErrorMessage(E_INVALID_ARG));
62         }
63
64         if (pPublicLandscapeLayout != null)
65         {
66                 _LayoutImpl* pLandscapeLayoutImpl = _LayoutImpl::GetInstance(*pPublicLandscapeLayout);
67                 SysTryReturn(NID_UI_CTRL, pLandscapeLayoutImpl != null, null, E_INVALID_ARG, "[%s] Landscape layout is invalid object", GetErrorMessage(E_INVALID_ARG));
68         }
69
70         pCore = _Panel::CreatePanelN(rect, groupStyle);
71         result r = GetLastResult();
72         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
73
74         pImpl = new (std::nothrow) _PanelImpl(pControl, pCore, pPublicPortraitLayout, pPublicLandscapeLayout);
75         r = CheckConstruction(pCore, pImpl);
76         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
77
78         return pImpl;
79 }
80
81 const _PanelImpl*
82 _PanelImpl::GetInstance(const Panel& panel)
83 {
84         return static_cast<const _PanelImpl*>(_ControlImpl::GetInstance(panel));
85 }
86
87 _PanelImpl*
88 _PanelImpl::GetInstance(Panel& panel)
89 {
90         return static_cast<_PanelImpl*>(_ControlImpl::GetInstance(panel));
91 }
92
93 const char*
94 _PanelImpl::GetPublicClassName(void) const
95 {
96         return "Tizen::Ui::Controls::Panel";
97 }
98
99 const Panel&
100 _PanelImpl::GetPublic(void) const
101 {
102         return static_cast <const Panel&>(_ControlImpl::GetPublic());
103 }
104
105 Panel&
106 _PanelImpl::GetPublic(void)
107 {
108         return static_cast <Panel&>(_ControlImpl::GetPublic());
109 }
110
111 const _Panel&
112 _PanelImpl::GetCore(void) const
113 {
114         return static_cast <const _Panel&>(_ControlImpl::GetCore());
115 }
116
117 _Panel&
118 _PanelImpl::GetCore(void)
119 {
120         return static_cast <_Panel&>(_ControlImpl::GetCore());
121 }
122
123 void
124 _PanelImpl::SetBackgroundColor(const Tizen::Graphics::Color& color)
125 {
126         if (!IsOpaque())
127         {
128                 GetCore().SetBackgroundColor(color);
129         }
130         else
131         {
132                 byte r, g, b, a;
133                 color.GetColorComponents(r, g, b, a);
134                 GetCore().SetBackgroundColor(Color(r*a/ 255, g*a/255, b*a/ 255, 255));
135         }
136 }
137
138 bool
139 _PanelImpl::IsCompositeEnabled(void) const
140 {
141         ClearLastResult();
142         return true;
143 }
144
145 result
146 _PanelImpl::SetCompositeEnabled(bool enabled)
147 {
148         ClearLastResult();
149
150         if (enabled)
151         {
152                 return E_SUCCESS;
153         }
154
155         SysLogException(NID_UI, E_SYSTEM, "[%s] This method is not supported.", GetErrorMessage(E_SYSTEM));
156         return E_SYSTEM;
157 }
158
159 result
160 _PanelImpl::OnAttachedToMainTree(void)
161 {
162         SetFocusable(false);
163
164         FloatRectangle builderBounds;
165         _ControlOrientation controlOrientation = _CONTROL_ORIENTATION_PORTRAIT;
166         bool exist = GetBuilderBoundsF(controlOrientation, builderBounds);
167
168         if (exist)
169         {
170                 _ContainerImpl* pContainerImpl = this;
171                 _FormImpl* pParentImpl = null;
172
173                 while(pContainerImpl)
174                 {
175                         pParentImpl = dynamic_cast<_FormImpl*>(pContainerImpl->GetParent());
176
177                         if (pParentImpl != null)
178                         {
179                                 break;
180                         }
181                         else
182                         {
183                                 pContainerImpl = pContainerImpl->GetParent();
184                         }
185                 }
186
187                 if (pParentImpl)
188                 {
189                         OrientationStatus orientation = pParentImpl->GetOrientationStatus();
190                         if (orientation == ORIENTATION_STATUS_LANDSCAPE
191                                 || orientation == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
192                         {
193                                 controlOrientation = _CONTROL_ORIENTATION_LANDSCAPE;
194                                 GetBuilderBoundsF(controlOrientation, builderBounds);
195                         }
196                         result r = SetBounds(builderBounds);
197                         if (r != E_SUCCESS)
198                         {
199                                 SysLogException(NID_UI_CTRL, r, "[%s] SetBounds failed", GetErrorMessage(r));
200                         }
201
202                         SetChildBuilderBounds(this, controlOrientation);
203                 }
204         }
205
206         _ContainerImpl::OnAttachedToMainTree();
207
208         return E_SUCCESS;
209 }
210
211 void
212 _PanelImpl::SetChildBuilderBounds(_ContainerImpl* pContainerImpl, _ControlOrientation controlOrientation)
213 {
214         FloatRectangle builderBounds;
215         _ContainerImpl* pTempContainerImpl;
216         for(int i = 0; i < pContainerImpl->GetChildCount(); i++)
217         {
218                 _ControlImpl* pControlImpl =  pContainerImpl->GetChild(i);
219                 bool exist = pControlImpl->GetBuilderBoundsF(controlOrientation, builderBounds);
220                 if (!exist)
221                 {
222                         continue;
223                 }
224
225                 pControlImpl->SetBounds(builderBounds);
226                 pTempContainerImpl = dynamic_cast<_ContainerImpl*>(pControlImpl);
227                 if (pTempContainerImpl != null)
228                 {
229                         SetChildBuilderBounds(pTempContainerImpl, controlOrientation);
230                 }
231
232         }
233 }
234
235
236 DataBindingContext*
237 _PanelImpl::GetDataBindingContextN(void) const
238 {
239         return _DataBindingContextImpl::GetDataBindingContextN(*this);
240 }
241
242 class _PanelMaker
243         : public _UiBuilderControlMaker
244 {
245 public:
246         _PanelMaker(_UiBuilder* uibuilder)
247                 : _UiBuilderControlMaker(uibuilder){};
248         virtual ~_PanelMaker(void){};
249         static _UiBuilderControlMaker*
250         GetInstance(_UiBuilder* uibuilder)
251         {
252                 _PanelMaker* pPanelMaker = new (std::nothrow) _PanelMaker(uibuilder);
253                 SysTryReturn(NID_UI_CTRL, pPanelMaker != null, null, E_OUT_OF_MEMORY, "[%s] The memory is insufficient.", GetErrorMessage(E_OUT_OF_MEMORY));
254
255                 return pPanelMaker;
256         };
257 protected:
258         virtual Control*
259         Make(_UiBuilderControl* pControl)
260         {
261                 result r = E_SYSTEM;
262                 _UiBuilderControlLayout* pControlProperty = null;
263                 Panel* pPanel = null;
264                 FloatRectangle rect;
265                 Tizen::Base::String elementString;
266                 int opacity = 0;
267                 Color color;
268                 GroupStyle panelGroupStyle = GROUP_STYLE_NONE;
269
270                 GetProperty(pControl, &pControlProperty);
271                 if (pControlProperty == null)
272                 {
273                         return null;
274                 }
275
276                 if(pControl->GetParentWin().IsEmpty())
277                 {
278                         pPanel = static_cast<Panel*> (GetContainer());
279                 }
280                 else
281                 {
282                         pPanel = new (std::nothrow) Panel();
283                 }
284                 SysTryReturn(NID_UI_CTRL, pPanel != null, null, E_OUT_OF_MEMORY, "[%s] UiBuilder failed to create Panel object.", GetErrorMessage(E_OUT_OF_MEMORY));
285                 rect = pControlProperty->GetRectF();
286
287                 if (pControl->GetElement(L"groupStyle", elementString) || pControl->GetElement(L"GroupStyle", elementString))
288                 {
289                         if (elementString.Equals(L"GROUP_STYLE_NONE", false))
290                         {
291                                 panelGroupStyle = GROUP_STYLE_NONE;
292                         }
293                         if (elementString.Equals(L"GROUP_STYLE_SINGLE", false))
294                         {
295                                 panelGroupStyle = GROUP_STYLE_SINGLE;
296                         }
297                         if (elementString.Equals(L"GROUP_STYLE_TOP", false))
298                         {
299                                 panelGroupStyle = GROUP_STYLE_TOP;
300                         }
301                         if (elementString.Equals(L"GROUP_STYLE_MIDDLE", false))
302                         {
303                                 panelGroupStyle = GROUP_STYLE_MIDDLE;
304                         }
305                         if (elementString.Equals(L"GROUP_STYLE_BOTTOM", false))
306                         {
307                                 panelGroupStyle = GROUP_STYLE_BOTTOM;
308                         }
309                 }
310
311                 // Construct
312                 _UiBuilderLayoutType layoutType = UIBUILDER_LAYOUT_NONE;
313                 __pLayoutMaker->GetLayoutType(pControlProperty, layoutType);
314                 if (layoutType == UIBUILDER_LAYOUT_NONE)
315                 {
316                         r = pPanel->Construct(rect, panelGroupStyle);
317                 }
318                 else
319                 {
320                         Layout* pPortraitLayout = null;
321                         Layout* pLandscapeLayout = null;
322                         result tempResult = E_SUCCESS;
323                         tempResult = __pLayoutMaker->GetLayoutN(pControl, pPortraitLayout, pLandscapeLayout);
324                         if (E_SUCCESS == tempResult)
325                         {
326                                 r = pPanel->Construct(*pPortraitLayout, *pLandscapeLayout, rect, panelGroupStyle);
327                         }
328                         else
329                         {
330                                 r = tempResult;
331                         }
332                         _UiBuilderLayoutType layoutType = UIBUILDER_LAYOUT_NONE;
333
334                         if (__pLayoutMaker->GetLayoutType(pControlProperty, layoutType) == false)
335                         {
336                                 delete pPanel;
337                                 return null;
338                         }
339
340                         if ( layoutType == UIBUILDER_LAYOUT_GRID)
341                         {
342                                 for (int i = 0; i < UIBUILDER_ATTRIBUTE_NUM; i++)
343                                 {
344                                         GridLayout* pGridLayout = null;
345                                         pControlProperty = pControl->GetAttribute(i);
346
347                                         if ( i == UIBUILDER_ATTRIBUTE_PORTRAIT)
348                                         {
349                                                 pGridLayout = dynamic_cast<GridLayout*>(pPortraitLayout);
350                                         }
351                                         else
352                                         {
353                                                 pGridLayout = dynamic_cast<GridLayout*>(pLandscapeLayout);
354                                         }
355                                         __pLayoutMaker->SetGridLayoutContainerProperty(pGridLayout, pControlProperty);
356                                 }
357                         }
358                         delete pPortraitLayout;
359                         if (pPortraitLayout != pLandscapeLayout)
360                         {
361                                 delete pLandscapeLayout;
362                         }
363                         //CONSTRUCT_WITH_LAYOUT_ARG2(pPanel, rect, gStyle);
364                 }
365                 if (r != E_SUCCESS)
366                 {
367                         delete pPanel;
368                         return null;
369                 }
370
371                 if (pControl->GetElement(L"backgroundOpacity", elementString) || pControl->GetElement(L"BGColorOpacity", elementString) || pControl->GetElement(L"backgroundColorOpacity", elementString))
372                 {
373                         Base::Integer::Parse(elementString, opacity);
374                 }
375                 if (pControl->GetElement(L"backgroundColor", elementString) || pControl->GetElement(L"BGColor", elementString))
376                 {
377                         ConvertStringToColor32(elementString, opacity, color);
378                         pPanel->SetBackgroundColor(color);
379                 }
380                 else
381                 {
382                         color = pPanel->GetBackgroundColor();
383                         color.SetAlpha(ConvertOpacity100to255(opacity));
384                         pPanel->SetBackgroundColor(color);
385                 }
386
387                 // set composite mode
388                 if (pControl->GetElement(L"compositeEnabled", elementString))
389                 {
390                         if (elementString.Equals(L"false", false))
391                         {
392                                 pPanel->SetCompositeEnabled(false);
393                         }
394                 }
395
396                 return pPanel;
397         }
398
399
400 private:
401 }; // _PanelMaker
402
403 _PanelRegister::_PanelRegister(void)
404 {
405         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
406         pUiBuilderControlTableManager->RegisterControl(L"Panel", _PanelMaker::GetInstance);
407 }
408 _PanelRegister::~_PanelRegister(void)
409 {
410         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
411         pUiBuilderControlTableManager->UnregisterControl(L"Panel");
412 }
413 static _PanelRegister PanelRegisterToUiBuilder;
414 }}} //Tizen::Ui::Controls
415