Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_Progress.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_Progress.cpp
20  * @brief               This is the implementation file for the _Progress class.
21  */
22
23 #include <FBaseErrorDefine.h>
24 #include <FBaseSysLog.h>
25 #include "FUiAnim_VisualElement.h"
26 #include "FUi_ResourceManager.h"
27 #include "FUi_AccessibilityContainer.h"
28 #include "FUi_AccessibilityElement.h"
29 #include "FUiCtrl_Progress.h"
30 #include "FUiCtrl_ProgressPresenter.h"
31
32 using namespace Tizen::Ui;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Base;
35
36 namespace Tizen { namespace Ui { namespace Controls
37 {
38
39 IMPLEMENT_PROPERTY(_Progress);
40
41 _Progress::_Progress(void)
42         : __pProgressPresenter(null)
43         , __pAccessibilityElement(null)
44 {
45         Tizen::Ui::Animations::_VisualElement* pBase = GetVisualElement();
46
47         if (pBase != null)
48         {
49                 pBase->SetSurfaceOpaque(false);
50         }
51 }
52
53 _Progress::~_Progress(void)
54 {
55         delete __pProgressPresenter;
56         __pProgressPresenter = null;
57         if (__pAccessibilityElement)
58         {
59                 __pAccessibilityElement->Activate(false);
60                 __pAccessibilityElement = null;
61         }
62 }
63
64 _Progress*
65 _Progress::CreateProgressN()
66 {
67         result r = E_SUCCESS;
68
69         _Progress* pProgress = new (std::nothrow) _Progress;
70         SysTryReturn(NID_UI_CTRL, pProgress, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
71
72         _AccessibilityContainer* pContainer = pProgress->GetAccessibilityContainer();
73
74         pProgress->__pProgressPresenter = _ProgressPresenter::CreateInstanceN(*pProgress);
75         r = GetLastResult();
76         SysTryCatch(NID_UI_CTRL, pProgress->__pProgressPresenter != null, , r,
77                                 "[%s] Propagating.", GetErrorMessage(r));
78
79         r = GET_COLOR_CONFIG(SLIDER::BAR_NORMAL, pProgress->__barColor);
80         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get progress colorconfig", GetErrorMessage(r));
81
82         r = GET_COLOR_CONFIG(SLIDER::BAR_BG_NORMAL, pProgress->__barBgColor);
83         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get progress colorconfig", GetErrorMessage(r));
84
85         pProgress->AcquireHandle();
86         pProgress->SetFocusable(false);
87
88         if (pContainer)
89         {
90                 pContainer->Activate(true);
91                 pProgress->InitializeAccessibilityElement();
92         }
93
94
95         return pProgress;
96
97 CATCH:
98         delete pProgress;
99         pProgress = null;
100
101         return null;
102 }
103
104 result
105 _Progress::InitializeAccessibilityElement(void)
106 {
107         result r = E_SUCCESS;
108
109         if (__pAccessibilityElement)
110         {
111                 return r;
112         }
113
114         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
115         if (pContainer)
116         {
117                 __pAccessibilityElement = new _AccessibilityElement(true);
118                 SysTryReturn(NID_UI_CTRL, __pAccessibilityElement, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
119                 __pAccessibilityElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBounds().width, GetBounds().height));
120                 __pAccessibilityElement->SetName(L"ProgressingImage");
121                 __pAccessibilityElement->SetLabel(L"Progressing");
122
123                 String string;
124                 GET_STRING_CONFIG(IDS_TPLATFORM_BODY_PD_PERCENT_T_TTS, string);
125                 string.Insert(GetPercentComplete(), 0);
126                 __pAccessibilityElement->SetValue(string);
127
128                 r = pContainer->AddElement(*__pAccessibilityElement);
129                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
130         }
131
132         return r;
133 }
134
135 result
136 _Progress::OnAttachedToMainTree(void)
137 {
138         if (__pAccessibilityElement)
139         {
140                 __pAccessibilityElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBounds().width, GetBounds().height));
141         }
142         return E_SUCCESS;
143 }
144
145 void
146 _Progress::OnBoundsChanged(void)
147 {
148         if (__pAccessibilityElement)
149         {
150                 __pAccessibilityElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBounds().width, GetBounds().height));
151         }
152
153         return;
154 }
155
156 result
157 _Progress::SetValue(int value)
158 {
159         return SetProperty("value", Variant(value));
160 }
161
162 result
163 _Progress::SetPropertyValue(const Variant& value)
164 {
165         result r = E_SUCCESS;
166
167         int tempValue = value.ToInt();
168         r = GetLastResult();
169         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
170
171         __pProgressPresenter->SetValue(tempValue);
172
173         r = InitializeAccessibilityElement();
174         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
175
176         if (__pAccessibilityElement)
177         {
178                 String string;
179                 GET_STRING_CONFIG(IDS_TPLATFORM_BODY_PD_PERCENT_T_TTS, string);
180                 string.Insert(GetPercentComplete(), 0);
181                 __pAccessibilityElement->SetValue(string);
182         }
183
184         return r;
185 }
186
187 int
188 _Progress::GetValue(void) const
189 {
190         Variant value = GetProperty("value");
191         result r = GetLastResult();
192         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, -1, r, "[%s] Propagating.", GetErrorMessage(r));
193
194         return value.ToInt();
195 }
196
197 Variant
198 _Progress::GetPropertyValue(void) const
199 {
200         return Variant(__pProgressPresenter->GetValue());
201 }
202
203 result
204 _Progress::SetRange(int minValue, int maxValue)
205 {
206         SysTryReturn(NID_UI_CTRL, minValue >= 0 && maxValue >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
207                                         "[E_OUT_OF_RANGE] Values can not be negative.");
208         SysTryReturn(NID_UI_CTRL, minValue < maxValue, E_INVALID_ARG, E_INVALID_ARG,
209                                 "[E_INVALID_ARG] Invalid argument(s) is used. The minimum value is greater than maximum value.");
210
211         result r = E_SUCCESS;
212
213         Variant oldMinValue = GetProperty("minValue");
214         Variant value = GetProperty("value");
215
216         r = SetProperty("minValue", Variant(minValue));
217         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to set minimum value.", GetErrorMessage(r));
218
219         r = SetProperty("maxValue", Variant(maxValue));
220         if (r != E_SUCCESS)
221         {
222                 SysLogException(NID_UI_CTRL, r, "[%s] Failed to set maximum value.", GetErrorMessage(r));
223
224                 result nestedResult = SetPropertyMinValue(oldMinValue);
225                 if (nestedResult != E_SUCCESS)
226                 {
227                         SysLog(NID_UI_CTRL, "Failed to recover original minimum value");
228                         return r;
229                 }
230
231                 nestedResult = SetPropertyValue(value);
232                 if (nestedResult != E_SUCCESS)
233                 {
234                         SysLog(NID_UI_CTRL, "Failed to recover original value");
235                 }
236         }
237
238         if (__pAccessibilityElement)
239         {
240                 String string;
241                 GET_STRING_CONFIG(IDS_TPLATFORM_BODY_PD_PERCENT_T_TTS, string);
242                 string.Insert(GetPercentComplete(), 0);
243                 __pAccessibilityElement->SetValue(string);
244         }
245
246         return r;
247 }
248
249 result
250 _Progress::SetPropertyMinValue(const Variant& minValue)
251 {
252         result r = E_SUCCESS;
253
254         int tempMinValue = minValue.ToInt();
255         r = GetLastResult();
256         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
257
258         __pProgressPresenter->SetMinValue(tempMinValue);
259
260         return r;
261 }
262
263 result
264 _Progress::SetPropertyMaxValue(const Variant& maxValue)
265 {
266         result r = E_SUCCESS;
267
268         int tempMaxValue = maxValue.ToInt();
269         r = GetLastResult();
270         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
271
272         __pProgressPresenter->SetMaxValue(tempMaxValue);
273
274         return r;
275 }
276
277 result
278 _Progress::GetRange(int& minValue, int& maxValue) const
279 {
280         result r = E_SUCCESS;
281
282         Variant minimum = GetProperty("minValue");
283         r = GetLastResult();
284         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
285
286         Variant maximum = GetProperty("maxValue");
287         r = GetLastResult();
288         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
289
290         minValue = minimum.ToInt();
291         maxValue = maximum.ToInt();
292
293         return r;
294 }
295
296 Variant
297 _Progress::GetPropertyMinValue(void) const
298 {
299         return Variant(__pProgressPresenter->GetMinValue());
300 }
301
302 Variant
303 _Progress::GetPropertyMaxValue(void) const
304 {
305         return Variant(__pProgressPresenter->GetMaxValue());
306 }
307
308 int
309 _Progress::GetPercentComplete(void) const
310 {
311         return __pProgressPresenter->GetPercentComplete();
312 }
313
314 result
315 _Progress::SetBarColor(const Color& color)
316 {
317         return SetProperty("barColor", Variant(color));
318 }
319
320 result
321 _Progress::SetPropertyBarColor(const Variant& barColor)
322 {
323         result r = E_SUCCESS;
324
325         Color tempColor = barColor.ToColor();
326         r = GetLastResult();
327         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
328
329         __barColor = tempColor;
330         __barColor.SetAlpha(0xFF);
331
332         return r;
333 }
334
335 Color
336 _Progress::GetBarColor(void) const
337 {
338         Variant barColor = GetProperty("barColor");
339         result r = GetLastResult();
340         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, Color(), r, "[%s] Propagating.", GetErrorMessage(r));
341
342         return barColor.ToColor();
343 }
344
345 Variant
346 _Progress::GetPropertyBarColor(void) const
347 {
348         return Variant(__barColor);
349 }
350
351 result
352 _Progress::SetBarBackgroundColor(const Color& color)
353 {
354         return SetProperty("barBackgroundColor", Variant(color));
355 }
356
357 result
358 _Progress::SetPropertyBarBackgroundColor(const Variant& barBgColor)
359 {
360         result r = E_SUCCESS;
361
362         Color tempColor = barBgColor.ToColor();
363         r = GetLastResult();
364         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
365
366         __barBgColor = tempColor;
367         __barBgColor.SetAlpha(0xFF);
368
369         return r;
370 }
371
372 Color
373 _Progress::GetBarBackgroundColor(void) const
374 {
375         Variant barBackgroundColor = GetProperty("barBackgroundColor");
376         result r = GetLastResult();
377         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, Color(), r, "[%s] Propagating.", GetErrorMessage(r));
378
379         return barBackgroundColor.ToColor();
380 }
381
382 Variant
383 _Progress::GetPropertyBarBackgroundColor(void) const
384 {
385         return Variant(__barBgColor);
386 }
387 void
388 _Progress::OnDraw(void)
389 {
390         __pProgressPresenter->Draw();
391         return;
392 }
393
394 }}} // Tizen::Ui::Controls