Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_GroupContainerPresenter.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_GroupContainerPresenter.cpp
20  * @brief               This is the implementation file for the _GroupContainerPresenter class.
21  *
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FBaseErrorDefine.h>
26 #include <FGrpCanvas.h>
27 #include <FGrp_BitmapImpl.h>
28 #include "FUiCtrl_GroupContainerPresenter.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUiAnim_VisualElement.h"
31 #include "FUi_CoordinateSystemUtils.h"
32 #include "FUiCtrl_Form.h"
33
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui::Animations;
36
37 namespace Tizen { namespace Ui { namespace Controls
38 {
39
40 _GroupContainerPresenter::_GroupContainerPresenter(void)
41 {
42         // Nothing
43 }
44
45 _GroupContainerPresenter::_GroupContainerPresenter(_GroupContainer& groupContainer)
46         : __pGroupContainer(&groupContainer)
47         , __pSystemChildColorReplacedBgBitmap(null)
48         , __pSystemChildBgEffectBitmap(null)
49         , __pGroupContainerColorReplacedBgBitmap(null)
50         , __parentBgColor(Color(0, 0, 0, 0))
51         , __bgColor(Color(0, 0, 0, 0))
52 {
53         // Nothing
54 }
55
56 _GroupContainerPresenter::~_GroupContainerPresenter(void)
57 {
58         __pGroupContainer = null;
59
60         delete __pSystemChildColorReplacedBgBitmap;
61         __pSystemChildColorReplacedBgBitmap = null;
62
63         delete __pSystemChildBgEffectBitmap;
64         __pSystemChildBgEffectBitmap = null;
65
66         delete __pGroupContainerColorReplacedBgBitmap;
67         __pGroupContainerColorReplacedBgBitmap = null;
68 }
69
70 result
71 _GroupContainerPresenter::LoadResourceBitmaps(void)
72 {
73         result r = E_SUCCESS;
74         Bitmap* pSystemChildBgBitmap = null;
75         Bitmap* pGroupContainerBgBitmap = null;
76         __parentBgColor = GetParentColor();
77
78         r = GET_BITMAP_CONFIG_N(GROUPCONTAINER::SYSTEM_CONTROL_BG, BITMAP_PIXEL_FORMAT_ARGB8888, pSystemChildBgBitmap);
79         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
80
81         if (__pSystemChildColorReplacedBgBitmap)
82         {
83                 delete __pSystemChildColorReplacedBgBitmap;
84                 __pSystemChildColorReplacedBgBitmap = null;
85         }
86
87     __pSystemChildColorReplacedBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSystemChildBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), __parentBgColor);
88         r = GetLastResult();
89         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Failed to get replacement color Bitmap.", GetErrorMessage(r));
90
91         if (__pSystemChildBgEffectBitmap == null)
92         {
93                 r = GET_BITMAP_CONFIG_N(GROUPCONTAINER::BORDER_BG_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pSystemChildBgEffectBitmap);
94                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
95         }
96
97         r = GET_BITMAP_CONFIG_N(GROUPCONTAINER::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pGroupContainerBgBitmap);
98         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
99
100         if (__pGroupContainerColorReplacedBgBitmap)
101         {
102                 delete __pGroupContainerColorReplacedBgBitmap;
103                 __pGroupContainerColorReplacedBgBitmap = null;
104         }
105
106         __bgColor = __pGroupContainer->GetBackgroundColor();
107
108         __pGroupContainerColorReplacedBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pGroupContainerBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), __bgColor);
109         r = GetLastResult();
110         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Failed to get replacement color Bitmap.", GetErrorMessage(r));
111
112         delete pSystemChildBgBitmap;
113         delete pGroupContainerBgBitmap;
114
115         return r;
116
117 CATCH:
118
119         delete pSystemChildBgBitmap;
120
121         delete pGroupContainerBgBitmap;
122
123         delete __pSystemChildColorReplacedBgBitmap;
124         __pSystemChildColorReplacedBgBitmap = null;
125
126         delete __pSystemChildBgEffectBitmap;
127         __pSystemChildBgEffectBitmap = null;
128
129         return r;
130 }
131
132 void
133 _GroupContainerPresenter::Draw(void)
134 {
135         result r = E_SUCCESS;
136         FloatRectangle gridBounds(0.0f, 0.0f, 0.0f, 0.0f);
137         FloatRectangle rect(0.0f, 0.0f, 0.0f, 0.0f);
138         int width = 0;
139         int lineAdjustPosition = 0;
140         int rowIndex = 0;
141         int columnIndex = 0;
142         int rows = __pGroupContainer->GetRowCount();
143         int columns = __pGroupContainer->GetColumnCount();
144         float lineWidth = __pGroupContainer->GetLineWidth();
145
146         if (__parentBgColor != GetParentColor() || __bgColor != __pGroupContainer->GetBackgroundColor())
147         {
148                 LoadResourceBitmaps();
149         }
150
151         Canvas* pCanvas = __pGroupContainer->GetVisualElement()->GetCanvasN();
152
153         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the canvas.");
154
155         Canvas* pControlCanvas = null;
156         _Control* pControl = __pGroupContainer->GetSystemChild();
157         SysTryCatch(NID_UI_CTRL, pControl != null, ,E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the child control.");
158
159         pControlCanvas = pControl->GetCanvasN();
160         SysTryCatch(NID_UI_CTRL, pControlCanvas != null, ,E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the canvas.");
161
162         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
163         pCanvas->Clear();
164
165         pControlCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
166         pControlCanvas->Clear();
167
168         pCanvas->SetLineStyle(LINE_STYLE_SOLID);
169         pCanvas->SetLineWidth(lineWidth);
170         r = GetLastResult();
171         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, ,r, "[%s] Propagating.", GetErrorMessage(r));
172
173         pCanvas->SetForegroundColor(__pGroupContainer->GetLineColor());
174
175         gridBounds = __pGroupContainer->GetGridBounds();
176
177         DrawBackgroundBitmapForGroupContainer(pCanvas);
178         DrawBackgroundBitmapForSystemChild(pControlCanvas);
179         DrawBackgroundEffectBitmapForSystemChild(pControlCanvas);
180
181         if (gridBounds.width < __pGroupContainer->GetBoundsF().width)
182         {
183                 pCanvas->DrawLine(FloatPoint(gridBounds.x + gridBounds.width, gridBounds.y), FloatPoint(gridBounds.x + gridBounds.width, gridBounds.y + gridBounds.height));
184         }
185
186         if (gridBounds.height < __pGroupContainer->GetBoundsF().height)
187         {
188                 pCanvas->DrawLine(FloatPoint(gridBounds.x, gridBounds.y + gridBounds.height), FloatPoint(gridBounds.x + gridBounds.width, gridBounds.y + gridBounds.height));
189         }
190
191         width = _CoordinateSystemUtils::ConvertToInteger(lineWidth);
192         lineAdjustPosition = ((width % 2) == 0) ? (width / 2) : ((width - 1) / 2);
193
194         for (rowIndex = 0; rowIndex < rows; rowIndex++)
195         {
196                 for (columnIndex = 0; columnIndex < columns; columnIndex++)
197                 {
198                         __pGroupContainer->ResizeControl(rowIndex, columnIndex);
199                         rect = __pGroupContainer->GetBoundsAt(rowIndex , columnIndex);
200
201                         //Draw Cell Left and Top Lines for all cells
202                         if ((__pGroupContainer->IsMerged(rowIndex, columnIndex) == false) || (__pGroupContainer->IsParent(rowIndex, columnIndex) == true))
203                         {
204                                 //Cells's Top Line
205                                 if (rowIndex != 0)
206                                 {
207                                         pCanvas->DrawLine(FloatPoint(rect.x - lineWidth, rect.y - lineWidth + lineAdjustPosition), FloatPoint(rect.x + rect.width, rect.y - lineWidth + lineAdjustPosition));
208                                 }
209                                 //Cell's Left Line
210                                 if (columnIndex != 0)
211                                 {
212                                         pCanvas->DrawLine(FloatPoint(rect.x - lineWidth + lineAdjustPosition, rect.y), FloatPoint(rect.x - lineWidth + lineAdjustPosition, rect.y + rect.height));
213                                 }
214                         }
215                 }
216         }
217
218         delete pCanvas;
219         delete pControlCanvas;
220
221         return;
222
223 CATCH:
224         delete pCanvas;
225         delete pControlCanvas;
226
227         return;
228 }
229
230 void
231 _GroupContainerPresenter::DrawBackgroundBitmapForSystemChild(Canvas* pCanvas)
232 {
233         result r = E_SUCCESS;
234
235         FloatRectangle bounds = __pGroupContainer->GetBoundsF();
236         bounds.x = 0.0f;
237         bounds.y = 0.0f;
238
239         if(__pSystemChildColorReplacedBgBitmap != null)
240         {
241                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pSystemChildColorReplacedBgBitmap))
242                 {
243                         r = pCanvas->DrawNinePatchedBitmap(bounds, *__pSystemChildColorReplacedBgBitmap);
244                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
245                 }
246                 else
247                 {
248                         r = pCanvas->DrawBitmap(bounds, *__pSystemChildColorReplacedBgBitmap);
249                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
250                 }
251         }
252
253         return;
254 }
255
256 void
257 _GroupContainerPresenter::DrawBackgroundBitmapForGroupContainer(Canvas* pCanvas)
258 {
259         result r = E_SUCCESS;
260
261         FloatRectangle bounds = __pGroupContainer->GetBoundsF();
262         bounds.x = 0.0f;
263         bounds.y = 0.0f;
264
265         if(__pGroupContainerColorReplacedBgBitmap != null)
266         {
267                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pGroupContainerColorReplacedBgBitmap))
268                 {
269                         r = pCanvas->DrawNinePatchedBitmap(bounds, *__pGroupContainerColorReplacedBgBitmap);
270                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
271                 }
272                 else
273                 {
274                         r = pCanvas->DrawBitmap(bounds, *__pGroupContainerColorReplacedBgBitmap);
275                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
276                 }
277         }
278
279         return;
280 }
281
282 void
283 _GroupContainerPresenter::DrawBackgroundEffectBitmapForSystemChild(Canvas* pCanvas)
284 {
285         result r = E_SUCCESS;
286
287         FloatRectangle bounds = __pGroupContainer->GetBoundsF();
288         bounds.x = 0.0f;
289         bounds.y = 0.0f;
290
291         if(__pSystemChildBgEffectBitmap != null)
292         {
293                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pSystemChildBgEffectBitmap))
294                 {
295                         r = pCanvas->DrawNinePatchedBitmap(bounds, *__pSystemChildBgEffectBitmap);
296                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
297                 }
298                 else
299                 {
300                         r = pCanvas->DrawBitmap(bounds, *__pSystemChildBgEffectBitmap);
301                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
302                 }
303         }
304
305         return;
306 }
307
308 Color
309 _GroupContainerPresenter::GetParentColor(void) const
310 {
311         _Control* pParentControl = __pGroupContainer->GetParent();
312         if (!pParentControl)
313         {
314                 return Color(0, 0, 0, 0);
315         }
316
317         Color parentBgColor = pParentControl->GetBackgroundColor();
318         _Form* pForm = dynamic_cast<_Form*>(pParentControl);
319
320         while (pForm == null && parentBgColor.GetAlpha() == 0x00)
321         {
322                 pParentControl = pParentControl->GetParent();
323                 if (!pParentControl)
324                 {
325                         break;
326                 }
327
328                 pForm = dynamic_cast<_Form*>(pParentControl);
329                 parentBgColor = pParentControl->GetBackgroundColor();
330         }
331
332         return parentBgColor;
333 }
334
335 }}} // Tizen::Ui::Controls
336