Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_SliderOverlay.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_SliderOverlay.cpp
20  * @brief       This is the implementation file for the _SliderOverlay class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseErrorDefine.h>
25 #include <FGrp_BitmapImpl.h>
26 #include "FUiAnim_VisualElement.h"
27 #include "FUi_ResourceManager.h"
28 #include "FUi_EcoreEvasMgr.h"
29 #include "FUi_EcoreEvas.h"
30 #include "FUiCtrl_SliderOverlay.h"
31
32
33 using namespace Tizen::Graphics;
34
35 namespace Tizen { namespace Ui { namespace Controls
36 {
37
38 _SliderOverlay::_SliderOverlay(void)
39         : __pOverlayBgBitmap(null)
40         , __overlayBgColor(0)
41         , __overlayTextColor(0)
42         , __pFont(null)
43         , __pOwner(null)
44         , __overlayWidth(-1.0f)
45 {
46         Tizen::Ui::Animations::_VisualElement* pBase = GetVisualElement();
47
48         if (pBase != null)
49         {
50                 pBase->SetSurfaceOpaque(false);
51         }
52 }
53
54 _SliderOverlay::~_SliderOverlay(void)
55 {
56         delete __pOverlayBgBitmap;
57         __pOverlayBgBitmap = null;
58 }
59
60 result
61 _SliderOverlay::Construct(_Control& owner)
62 {
63         result r = E_SUCCESS;
64
65         __pOwner = &owner;
66
67         r = CreateRootVisualElement(_WINDOW_TYPE_VE);
68         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
69
70         SetActivationEnabled(false);
71
72         AcquireHandle();
73
74         r = GET_COLOR_CONFIG(SLIDER::OVERLAY_BG_NORMAL, __overlayBgColor);
75         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
76
77         r = GET_COLOR_CONFIG(SLIDER::OVERLAY_TEXT_NORMAL, __overlayTextColor);
78         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
79
80         r = LoadBitmaps();
81         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
82
83         return r;
84 }
85
86 result
87 _SliderOverlay::SetSliderRect(FloatRectangle& sliderRect, FloatRectangle& sliderTouchRect)
88 {
89         __sliderRect = sliderRect;
90         __sliderTouchRect = sliderTouchRect;
91
92         return E_SUCCESS;
93 }
94
95 void
96 _SliderOverlay::SetFont(Tizen::Graphics::Font* pFont)
97 {
98         __pFont = pFont;
99         __overlayWidth = -1.0f;
100         return;
101 }
102
103 result
104 _SliderOverlay::LoadBitmaps()
105 {
106         result r = E_SUCCESS;
107
108         Bitmap* pBitmap = null;
109         Bitmap* pReplacementBitmap = null;
110
111         r = GET_BITMAP_CONFIG_N(SLIDER::OVERLAY_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
112         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
113
114         pReplacementBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), __overlayBgColor);
115         r = GetLastResult();
116         SysTryCatch(NID_UI_CTRL, pReplacementBitmap != null, , r, "[%s] Propagating.", GetErrorMessage(r));
117
118         delete __pOverlayBgBitmap;
119         __pOverlayBgBitmap = pReplacementBitmap;
120
121         delete pBitmap;
122         pBitmap = null;
123
124         return r;
125
126 CATCH:
127         delete pBitmap;
128         pBitmap = null;
129
130         return r;
131 }
132
133 result
134 _SliderOverlay::DrawSliderOverlay(int positionX, int handleWidth, int value)
135 {
136         result r = E_SUCCESS;
137
138         float overlayWidth = 0.0f;
139         float overlayHeight = 0.0f;
140         float overlayBottomMargin = 0.0f;
141         float overlayLeftMargin = 0.0f;
142         int overlayTextSize = 0;
143         float overlayBodyHeight = 0.0f;
144         float overlayY = 0.0f;
145         Point textPoint(0, 0);
146         FloatDimension textDim(0.0f, 0.0f);
147
148         Bitmap* pBgBitmap = __pOverlayBgBitmap;
149
150         Tizen::Base::String text;
151         text.Append(value);
152         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
153
154         r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_MARGIN, orientation, overlayLeftMargin);
155         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
156
157         r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_HEIGHT, orientation, overlayHeight);
158         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
159
160         r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_BOTTOM_MARGIN, orientation, overlayBottomMargin);
161         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
162
163         r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_HEIGHT_WITHOUT_ARROW, orientation, overlayBodyHeight);
164         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
165
166         r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_TEXT_SIZE, orientation, overlayTextSize);
167         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
168
169         (_FontImpl::GetInstance(*__pFont))->SetSize(overlayTextSize);
170
171         if (__overlayWidth == -1.0f)
172         {
173                 int fixedValue = 999;
174                 Tizen::Base::String fixedText;
175                 fixedText.Append(fixedValue);
176                 __pFont->GetTextExtent(fixedText, fixedText.GetLength(), textDim);
177                 __overlayWidth = textDim.width + 2 * overlayLeftMargin;
178         }
179
180         __pFont->GetTextExtent(text, text.GetLength(), textDim);
181
182         if (text.GetLength() == SLIDER_OVERLAY_SINGLE_DIGIT)
183         {
184                 r = GET_SHAPE_CONFIG(SLIDER::OVERLAY_WIDTH, orientation, overlayWidth);
185                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
186         }
187         else
188         {
189                 overlayWidth = 2 * overlayLeftMargin + textDim.width;
190         }
191
192         if (__sliderTouchRect.y > 0)
193         {
194                 overlayY = __sliderTouchRect.y;
195         }
196
197         __sliderOverlayRect = FloatRectangle(__sliderRect.x + positionX + (handleWidth - overlayWidth) / 2.0f,
198                                                                         __sliderRect.y + overlayY - (overlayHeight + overlayBottomMargin),
199                                                                         overlayWidth, overlayHeight);
200
201         SetBounds(__sliderOverlayRect);
202
203         // Get canvas & Clear BG
204         Canvas* pCanvas = GetCanvasN();
205         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
206
207         pCanvas->SetBackgroundColor(Color(0x00000000));
208         pCanvas->Clear();
209
210         pCanvas->SetFont(*__pFont);
211
212         // Draw BG
213         FloatRectangle overlayRect = FloatRectangle(0.0f, 0.0f, __sliderOverlayRect.width, __sliderOverlayRect.height);
214
215         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBgBitmap))
216         {
217                 r = pCanvas->DrawNinePatchedBitmap(overlayRect, *pBgBitmap);
218         }
219         else
220         {
221                 r = pCanvas->DrawBitmap(overlayRect, *pBgBitmap);
222         }
223         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
224
225         // Draw Value Text
226         __pFont->GetTextExtent(text, text.GetLength(), textDim);
227         textPoint.x = (__sliderOverlayRect.width - textDim.width) / 2;
228         textPoint.y = textPoint.y + ((overlayBodyHeight - textDim.height) / 2);
229         pCanvas->SetForegroundColor(__overlayTextColor);
230         pCanvas->DrawText(textPoint, text);
231
232         delete pCanvas;
233         pCanvas = null;
234
235         return r;
236 CATCH:
237         delete pCanvas;
238         return E_SYSTEM;
239 }
240
241 bool
242 _SliderOverlay::IsActivatedOnOpen(void) const
243 {
244         return false;
245 }
246
247 result
248 _SliderOverlay::OnAttachedToMainTree(void)
249 {
250         SetOwner(__pOwner);
251         return _Window::OnAttachedToMainTree();
252 }
253
254 result
255 _SliderOverlay::OnDetachingFromMainTree(void)
256 {
257         return _Window::OnDetachingFromMainTree();
258 }
259
260 bool
261 _SliderOverlay::IsRotationSynchronized(void) const
262 {
263         return true;
264 }
265
266 }}} // Tizen::Ui::Controls