Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_PanelPresenter.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 /**
19  * @file                FUiCtrl_PanelPresenter.cpp
20  * @brief       This is the implementation file for the %_PanelPresenter class.
21  *
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FBaseErrorDefine.h>
26 #include <FGrpCanvas.h>
27 #include <FGrp_BitmapImpl.h>
28 #include "FUiAnim_VisualElement.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUiCtrl_PanelPresenter.h"
31 #include "FUiCtrl_Panel.h"
32
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui::Animations;
35
36 namespace Tizen { namespace Ui { namespace Controls
37 {
38
39 _PanelPresenter::_PanelPresenter(void)
40         : __pPanel(null)
41 {
42         // Nothing
43 }
44
45 _PanelPresenter::~_PanelPresenter(void)
46 {
47         // Nothing
48 }
49
50 result
51 _PanelPresenter::Initialize(_Panel& panel)
52 {
53         __pPanel = &panel;
54
55         return E_SUCCESS;
56 }
57
58 result
59 _PanelPresenter::Draw(void)
60 {
61         if (__pPanel->GetBackgroundBitmap() == null
62                 && (__pPanel->GetGroupStyle() == GROUP_STYLE_NONE || (__pPanel->GetGroupStyleBitmap() == null && __pPanel->GetGroupStyleBackgroundBitmap() == null)))
63         {
64                 return E_SUCCESS;
65         }
66
67         Canvas* pCanvas = __pPanel->GetVisualElement()->GetCanvasN();
68         result r = GetLastResult();
69         SysTryReturn(NID_UI_CTRL, pCanvas != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
70         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
71
72         if (__pPanel->GetGroupStyle() == GROUP_STYLE_NONE)
73         {
74                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
75         }
76         else
77         {
78                 Color panelGroupBackgroundColor;
79                 GET_COLOR_CONFIG(Panel::BG_NORMAL, panelGroupBackgroundColor);
80                 pCanvas->SetBackgroundColor(panelGroupBackgroundColor);
81         }
82         r = GetLastResult();
83         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
84
85         r = pCanvas->Clear();
86         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
87
88         DrawGroupStyleBackgroundBitmap(pCanvas);
89         r = GetLastResult();
90         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
91
92         DrawBackgrounBitmap(pCanvas);
93         r = GetLastResult();
94         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
95
96         DrawGroupStyleBitmap(pCanvas);
97         r = GetLastResult();
98         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
99
100         // fall throw
101 CATCH:
102         delete pCanvas;
103
104         return r;
105 }
106
107 void
108 _PanelPresenter::DrawBackgrounBitmap(Canvas* pCanvas)
109 {
110         Bitmap* pBackgroundBitmap = __pPanel->GetBackgroundBitmap();
111
112         if (pBackgroundBitmap != null)
113         {
114                 Rectangle bounds = __pPanel->GetBounds();
115                 bounds.x = 0;
116                 bounds.y = 0;
117
118                 if (!__pPanel->IsBackgroundBitmapStretch())
119                 {
120                         int bitmapWidth = pBackgroundBitmap->GetWidth();
121                         int bitmapHeight = pBackgroundBitmap->GetHeight();
122
123                         switch (__pPanel->GetBackgroundBitmapHorizontalAlign())
124                         {
125                                 case PANEL_BACKGROUND_BITMAP_HORIZONTAL_ALIGN_RIGHT:
126                                         bounds.x = bounds.width - bitmapWidth;
127                                         break;
128                                 case PANEL_BACKGROUND_BITMAP_HORIZONTAL_ALIGN_CENTER:
129                                         bounds.x = (bounds.width / 2) - (bitmapWidth / 2);
130                                         break;
131                                 case PANEL_BACKGROUND_BITMAP_HORIZONTAL_ALIGN_LEFT:
132                                         // fall through
133                                 default:
134                                         break;
135                         }
136
137                         switch (__pPanel->GetBackgroundBitmapVerticalAlign())
138                         {
139                                 case PANEL_BACKGROUND_BITMAP_VERTICAL_ALIGN_BOTTOM:
140                                         bounds.y = bounds.height - bitmapHeight;
141                                         break;
142                                 case PANEL_BACKGROUND_BITMAP_VERTICAL_ALIGN_MIDDLE:
143                                         bounds.y = (bounds.height / 2) - (bitmapHeight / 2);
144                                         break;
145                                 case PANEL_BACKGROUND_BITMAP_VERTICAL_ALIGN_TOP:
146                                         // fall through
147                                 default:
148                                         break;
149                         }
150
151                         bounds.width = bitmapWidth;
152                         bounds.height = bitmapHeight;
153                 }
154
155                 if (pBackgroundBitmap->IsNinePatchedBitmap())
156                 {
157                         result r = pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
158                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
159                 }
160                 else
161                 {
162                         result r = pCanvas->DrawBitmap(bounds, *pBackgroundBitmap);
163                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
164                 }
165         }
166
167 }
168
169 void
170 _PanelPresenter::DrawGroupStyleBitmap(Canvas* pCanvas)
171 {
172         result r = E_SUCCESS;
173
174         Bitmap* pColorReplacedBitmap = null;
175         Bitmap* pGroupStyleBitmap = __pPanel->GetGroupStyleBitmap();
176
177         if (pGroupStyleBitmap != null)
178         {
179                 Rectangle bounds = __pPanel->GetBounds();
180                 bounds.x = 0;
181                 bounds.y = 0;
182
183                 pColorReplacedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pGroupStyleBitmap, Color::GetColor(COLOR_ID_MAGENTA), __pPanel->GetBackgroundColor());
184                 if (pColorReplacedBitmap != null)
185                 {
186                         if (pGroupStyleBitmap->IsNinePatchedBitmap())
187                         {
188                                 r = pCanvas->DrawNinePatchedBitmap(bounds, *pColorReplacedBitmap);
189                                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
190                         }
191                         else
192                         {
193                                 r = pCanvas->DrawBitmap(bounds, *pColorReplacedBitmap);
194                                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
195                         }
196                 }
197         }
198
199         // fall through
200 CATCH:
201         delete pColorReplacedBitmap;
202
203         SetLastResult(r);
204 }
205
206 void
207 _PanelPresenter::DrawGroupStyleBackgroundBitmap(Tizen::Graphics::Canvas* pCanvas)
208 {
209         result r = E_SUCCESS;
210
211         Bitmap* pColorReplacedBitmap = null;
212         Bitmap* pGroupStyleBackgroundBitmap = __pPanel->GetGroupStyleBackgroundBitmap();
213
214         if (pGroupStyleBackgroundBitmap != null)
215         {
216                 Rectangle bounds = __pPanel->GetBounds();
217                 bounds.x = 0;
218                 bounds.y = 0;
219
220                 pColorReplacedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pGroupStyleBackgroundBitmap, Color::GetColor(COLOR_ID_MAGENTA), __pPanel->GetBackgroundColor());
221                 r = GetLastResult();
222                 SysTryCatch(NID_UI_CTRL, pColorReplacedBitmap != null && r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
223
224                 if (pGroupStyleBackgroundBitmap->IsNinePatchedBitmap())
225                 {
226                         r = pCanvas->DrawNinePatchedBitmap(bounds, *pColorReplacedBitmap);
227                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
228                 }
229                 else
230                 {
231                         r = pCanvas->DrawBitmap(bounds, *pColorReplacedBitmap);
232                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
233                 }
234         }
235
236         // fall through
237 CATCH:
238         delete pColorReplacedBitmap;
239
240         SetLastResult(r);
241 }
242
243 }}} // Tizen::Ui::Controls
244