Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_SliderImpl.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  * @file                FUiCtrl_SliderImpl.cpp
19  * @brief       This is the implementation file for the _SliderImpl class.
20  */
21
22 #include <FApp_AppInfo.h>
23 #include <FBaseInternalTypes.h>
24 #include "FUi_ResourceManager.h"
25 #include "FUi_ResourceSizeInfo.h"
26 #include "FUi_UiBuilder.h"
27 #include "FUiCtrl_SliderImpl.h"
28 #include "FUiCtrl_PublicAdjustmentEvent.h"
29 #include "FUiCtrl_PublicSliderEvent.h"
30
31 using namespace Tizen::Ui;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace Ui { namespace Controls
36 {
37
38 _SliderImpl*
39 _SliderImpl::GetInstance(Slider& slider)
40 {
41         return static_cast<_SliderImpl*> (slider._pControlImpl);
42 }
43
44 const _SliderImpl*
45 _SliderImpl::GetInstance(const Slider& slider)
46 {
47         return static_cast<const _SliderImpl*> (slider._pControlImpl);
48 }
49
50 _SliderImpl::_SliderImpl(Slider* pPublic, _Slider* pCore)
51         : _ControlImpl(pPublic, pCore)
52         , __pPublicAdjustmentEvent(null)
53         , __pPublicSliderEvent(null)
54 {
55 }
56
57 _SliderImpl::~_SliderImpl(void)
58 {
59         if (__pPublicAdjustmentEvent != null)
60         {
61                 delete __pPublicAdjustmentEvent;
62                 __pPublicAdjustmentEvent = null;
63         }
64
65         if (__pPublicSliderEvent != null)
66         {
67                 delete __pPublicSliderEvent;
68                 __pPublicSliderEvent = null;
69         }
70 }
71
72 _SliderImpl*
73 _SliderImpl::CreateSliderImplN(Slider* pControl, const Rectangle& rect, int sliderStyle)
74 {
75         ClearLastResult();
76
77         result r = E_SUCCESS;
78
79         r = FUNC_SIZE_INFO(Slider)(sliderStyle).CheckInitialSizeValid(Dimension(rect.width, rect.height), _CONTROL_ORIENTATION_PORTRAIT);
80         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
81
82         _Slider* pCore = _Slider::CreateSliderN();
83         r = GetLastResult();
84         SysTryReturn(NID_UI_CTRL, pCore != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
85
86         _SliderImpl* pImpl = new (std::nothrow) _SliderImpl(pControl, pCore);
87         r = CheckConstruction(pCore, pImpl);
88         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         r = pImpl->InitializeBoundsProperties(FUNC_SIZE_INFO(Slider)(sliderStyle), rect, pCore->GetOrientation());
91         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
92
93         pImpl->__pPublicAdjustmentEvent = _PublicAdjustmentEvent::CreateInstanceN(*pControl);
94         SysTryCatch(NID_UI_CTRL, pImpl->__pPublicAdjustmentEvent != null, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to construct an AdjustmentEvent instance");
95
96         pImpl->__pPublicSliderEvent = _PublicSliderEvent::CreateInstanceN(*pControl);
97         SysTryCatch(NID_UI_CTRL, pImpl->__pPublicSliderEvent != null, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to construct a SliderEvent instance");
98
99         r = pCore->AddAdjustmentEventListener(*pImpl);
100         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
101
102         r = pCore->AddSliderEventListener(*pImpl);
103         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
104
105         return pImpl;
106
107 CATCH:
108         delete pImpl;
109         pImpl = null;
110
111         return null;
112 }
113
114 result
115 _SliderImpl::Initialize(int minValue, int maxValue, BackgroundStyle bgStyle, int sliderStyle, GroupStyle groupStyle)
116 {
117         ClearLastResult();
118
119         result r = E_SUCCESS;
120
121         int style = _SLIDER_STYLE_NONE;
122
123         if (sliderStyle & SLIDER_STYLE_BUBBLE)
124         {
125                 style |= _SLIDER_STYLE_OVERLAY;
126         }
127
128         if (bgStyle == BACKGROUND_STYLE_NONE)
129         {
130                 SysTryReturn(NID_UI_CTRL, groupStyle == GROUP_STYLE_NONE, E_INVALID_OPERATION, E_INVALID_OPERATION,
131                                    "[E_INVALID_OPERATION] The current Slider style does not support given group style.");
132
133                 style |= _SLIDER_STYLE_NO_BG;
134         }
135         if (sliderStyle & SLIDER_STYLE_TITLE)
136         {
137                 style |= _SLIDER_STYLE_TITLE_TEXT;
138         }
139
140         r = SetRange(minValue, maxValue);
141         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
142
143         GetCore().SetStyle(style);
144         GetCore().SetGroupStyle(groupStyle);
145
146         return r;
147 }
148
149 result
150 _SliderImpl::SetRange(int minValue, int maxValue)
151 {
152         ClearLastResult();
153
154         return GetCore().SetRange(minValue, maxValue);
155 }
156
157 result
158 _SliderImpl::GetRange(int& minValue, int& maxValue) const
159 {
160         ClearLastResult();
161
162         return GetCore().GetRange(minValue, maxValue);
163 }
164
165 result
166 _SliderImpl::SetValue(int value)
167 {
168         ClearLastResult();
169
170         Variant varValue(value);
171
172         return GetCore().SetPropertyValue(varValue);
173 }
174
175 int
176 _SliderImpl::GetValue(void) const
177 {
178         ClearLastResult();
179
180         return GetCore().GetPropertyValue().ToInt();
181 }
182
183 result
184 _SliderImpl::SetIcon(IconPosition position, const Bitmap& icon)
185 {
186         ClearLastResult();
187
188         return GetCore().SetIcon(position, icon);
189 }
190
191 result
192 _SliderImpl::SetTitleText(const String& title)
193 {
194         ClearLastResult();
195
196         Variant varTitleText(title);
197
198         return GetCore().SetPropertyTitleText(varTitleText);
199 }
200
201 String
202 _SliderImpl::GetTitleText(void) const
203 {
204         ClearLastResult();
205
206         return GetCore().GetPropertyTitleText().ToString();
207 }
208
209 result
210 _SliderImpl::SetTitleTextColor(const Color& color)
211 {
212         ClearLastResult();
213
214         Variant varTitleTextColor(color);
215
216         return GetCore().SetPropertyTitleTextColor(varTitleTextColor);
217 }
218
219 Color
220 _SliderImpl::GetTitleTextColor(void) const
221 {
222         ClearLastResult();
223
224         return GetCore().GetPropertyTitleTextColor().ToColor();
225 }
226
227 result
228 _SliderImpl::AddAdjustmentEventListener(IAdjustmentEventListener& listener)
229 {
230         ClearLastResult();
231
232         result r = __pPublicAdjustmentEvent->AddListener(listener);
233         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_ALREADY_EXIST, E_SYSTEM, "A system error has occurred. The IAdjustmentEventListener instance already exists in the event listener list.");
234
235         return r;
236 }
237
238 result
239 _SliderImpl::RemoveAdjustmentEventListener(IAdjustmentEventListener& listener)
240 {
241         ClearLastResult();
242
243         result r = __pPublicAdjustmentEvent->RemoveListener(listener);
244         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_NOT_FOUND, E_SYSTEM, "A system error has occurred. The IAdjustmentEventListener instance does not exist");
245
246         return r;
247 }
248
249 result
250 _SliderImpl::AddSliderEventListener(const ISliderEventListener& listener)
251 {
252         ClearLastResult();
253
254         result r = __pPublicSliderEvent->AddListener(listener);
255         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_ALREADY_EXIST, E_SYSTEM, "A system error has occurred. The ISliderEventListener instance already exists in the event listener list.");
256
257         return r;
258 }
259
260 result
261 _SliderImpl::RemoveSliderEventListener(const ISliderEventListener& listener)
262 {
263         ClearLastResult();
264
265         result r = __pPublicSliderEvent->RemoveListener(listener);
266         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_NOT_FOUND, E_SYSTEM, "A system error has occurred. The ISliderEventListener does not exist in the event listener list.");
267
268         return r;
269 }
270
271 result
272 _SliderImpl::SetBarColor(const Color& color)
273 {
274         ClearLastResult();
275
276         Variant varBarColor(color);
277
278         return GetCore().SetPropertyBarColor(varBarColor);
279 }
280
281 Color
282 _SliderImpl::GetBarColor(void) const
283 {
284         ClearLastResult();
285
286         return GetCore().GetPropertyBarColor().ToColor();
287 }
288
289 result
290 _SliderImpl::SetColor(const Color& color)
291 {
292         ClearLastResult();
293
294         Variant varColor(color);
295
296         return GetCore().SetPropertyColor(varColor);
297 }
298
299 Color
300 _SliderImpl::GetColor(void) const
301 {
302         ClearLastResult();
303
304         return GetCore().GetPropertyColor().ToColor();
305 }
306
307 result
308 _SliderImpl::SetThumbBitmap(SliderThumbStatus status, const Bitmap& bitmap)
309 {
310         ClearLastResult();
311
312         return GetCore().SetThumbBitmap(status, bitmap);
313 }
314
315 void
316 _SliderImpl::SetThumbTextColor(SliderThumbStatus status, const Color& color)
317 {
318         ClearLastResult();
319
320         GetCore().SetThumbTextColor(status, color);
321
322         return;
323 }
324
325 const char*
326 _SliderImpl::GetPublicClassName(void) const
327 {
328         return "Tizen::Ui::Controls::Slider";
329 }
330
331 const Slider&
332 _SliderImpl::GetPublic(void) const
333 {
334         return static_cast <const Slider&>(_ControlImpl::GetPublic());
335 }
336
337 Slider&
338 _SliderImpl::GetPublic(void)
339 {
340         return static_cast <Slider&>(_ControlImpl::GetPublic());
341 }
342
343 const _Slider&
344 _SliderImpl::GetCore(void) const
345 {
346         return static_cast <const _Slider&>(_ControlImpl::GetCore());
347 }
348
349 _Slider&
350 _SliderImpl::GetCore(void)
351 {
352         return static_cast <_Slider&>(_ControlImpl::GetCore());
353 }
354
355 void
356 _SliderImpl::OnAdjustmentValueChanged(const _Control& source, int adjustment)
357 {
358         ClearLastResult();
359
360         Runtime::IEventArg* pEventArg = _PublicAdjustmentEvent::CreateAdjustmentEventArgN(adjustment);
361         result r = GetLastResult();
362         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, r, "[%s] Propagating.", GetErrorMessage(r));
363
364         __pPublicAdjustmentEvent->Fire(*pEventArg);
365         return;
366 }
367
368 void
369 _SliderImpl::OnSliderBarMoved(_Slider& source, int value)
370 {
371         ClearLastResult();
372
373         Runtime::IEventArg* pEventArg = _PublicSliderEvent::CreateSliderEventArgN(value);
374         result r = GetLastResult();
375         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, r, "[%s] Propagating.", GetErrorMessage(r));
376
377         __pPublicSliderEvent->Fire(*pEventArg);
378         return;
379 }
380
381 class _SliderMaker
382         : public _UiBuilderControlMaker
383 {
384 public:
385         _SliderMaker(_UiBuilder* pUiBuilder)
386                 : _UiBuilderControlMaker(pUiBuilder){}
387         virtual ~_SliderMaker(void){}
388
389         static _UiBuilderControlMaker*
390         GetInstance(_UiBuilder* pUiBuilder)
391         {
392                 _SliderMaker* pSliderMaker = new (std::nothrow) _SliderMaker(pUiBuilder);
393                 return pSliderMaker;
394         }
395
396 protected:
397         virtual Control*
398         Make(_UiBuilderControl* pControl)
399         {
400                 result r = E_SUCCESS;
401                 _UiBuilderControlLayout* pControlProperty = null;
402                 Slider* pSlider = null;
403                 Tizen::Base::String elementString;
404                 Tizen::Base::String elementString1;
405                 Rectangle rect;
406                 int tempMin = 0;
407                 int tempMax = 0;
408                 int temp = 0;
409                 int sliderStyle = SLIDER_STYLE_NONE;
410                 bool isShowTitle = false;
411                 bool isGroupStyle = false;
412                 BackgroundStyle backgroundStyleEnum = BACKGROUND_STYLE_DEFAULT;
413                 GroupStyle groupStyleEmum = GROUP_STYLE_NONE;
414
415                 Color color;
416                 int opacity = 0;
417
418
419                 GetProperty(pControl, &pControlProperty);
420                 if (pControlProperty == null)
421                 {
422                         return null;
423                 }
424
425                 pSlider = new (std::nothrow) Slider;
426                 if (pSlider == null)
427                 {
428                         return null;
429                 }
430                 rect = pControlProperty->GetRect();
431
432                 if (pControl->GetElement(L"backgroundStyle", elementString) || pControl->GetElement(L"BackgroundStyle", elementString))
433                 {
434                         if (elementString.Equals(L"BACKGROUND_STYLE_NONE", false))
435                         {
436                                 backgroundStyleEnum = BACKGROUND_STYLE_NONE;
437                         }
438                 }
439
440                 if (pControl->GetElement(L"showTitleText", elementString) || pControl->GetElement(L"bShowtitleText", elementString))
441                 {
442                         if (elementString.Equals(L"true", false))
443                         {
444                                 isShowTitle = true;
445                         }
446                 }
447
448                 if (pControl->GetElement(L"groupStyle", elementString) || pControl->GetElement(L"GroupStyle", elementString))
449                 {
450                         isGroupStyle = true;
451
452                         if (elementString.Equals(L"GROUP_STYLE_NONE", false))
453                         {
454                                 groupStyleEmum = GROUP_STYLE_NONE;
455                         }
456                         if (elementString.Equals(L"GROUP_STYLE_SINGLE", false))
457                         {
458                                 groupStyleEmum = GROUP_STYLE_SINGLE;
459                         }
460                         if (elementString.Equals(L"GROUP_STYLE_TOP", false))
461                         {
462                                 groupStyleEmum = GROUP_STYLE_TOP;
463                         }
464                         if (elementString.Equals(L"GROUP_STYLE_MIDDLE", false))
465                         {
466                                 groupStyleEmum = GROUP_STYLE_MIDDLE;
467                         }
468                         if (elementString.Equals(L"GROUP_STYLE_BOTTOM", false))
469                         {
470                                 groupStyleEmum = GROUP_STYLE_BOTTOM;
471                         }
472                 }
473                 if (isGroupStyle == true)
474                 {
475                         if (pControl->GetElement(L"min", elementString) && pControl->GetElement(L"max", elementString1))
476                         {
477                                 Base::Integer::Parse(elementString, tempMin);
478                                 Base::Integer::Parse(elementString1, tempMax);
479
480                                 if (tempMin > tempMax)
481                                 {
482                                         temp = tempMin;
483                                         tempMin = tempMax;
484                                         tempMax = tempMin;
485                                 }
486
487                                 r = pSlider->Construct(rect, backgroundStyleEnum, isShowTitle, tempMin, tempMax, groupStyleEmum);
488                         }
489                         else
490                         {
491                                 r = pSlider->Construct(rect, backgroundStyleEnum, isShowTitle, 0, 10, groupStyleEmum);
492                         }
493                 }
494
495                 else
496                 {
497                         if (pControl->GetElement(L"sliderStyleBubble", elementString))
498                         {
499                                 if (elementString.Equals(L"true", false))
500                                 {
501                                         sliderStyle |= SLIDER_STYLE_BUBBLE;
502                                 }
503                         }
504
505                         if (pControl->GetElement(L"sliderStyleTitle", elementString))
506                         {
507                                 if (elementString.Equals(L"true", false))
508                                 {
509                                         isShowTitle = true;
510                                         sliderStyle |= SLIDER_STYLE_TITLE;
511                                 }
512                         }
513
514                         if (pControl->GetElement(L"min", elementString) && pControl->GetElement(L"max", elementString1))
515                         {
516                                 Base::Integer::Parse(elementString, tempMin);
517                                 Base::Integer::Parse(elementString1, tempMax);
518
519                                 if (tempMin > tempMax)
520                                 {
521                                         temp = tempMin;
522                                         tempMin = tempMax;
523                                         tempMax = tempMin;
524                                 }
525
526                                 r = pSlider->Construct(rect, sliderStyle, tempMin, tempMax);
527                         }
528                         else
529                         {
530                                 r = pSlider->Construct(rect, sliderStyle, 0, 10);
531                         }
532                 }
533
534                 if (r != E_SUCCESS)
535                 {
536                         delete pSlider;
537                         pSlider = null;
538
539                         return null;
540                 }
541
542                 if (pControl->GetElement(L"value", elementString))
543                 {
544                         Base::Integer::Parse(elementString, temp);
545                         if (temp > tempMax || temp < tempMin)
546                         {
547                                 temp = (tempMin + tempMax) >> 1;
548                         }
549                         pSlider->SetValue(temp);
550                 }
551                 else
552                 {
553                         pSlider->SetValue((tempMin + tempMax) >> 1);
554                 }
555
556                 if (isShowTitle)
557                 {
558                         if (pControl->GetElement(L"titleText", elementString))
559                         {
560                                 pSlider->SetTitleText(elementString);
561                         }
562                         if (pControl->GetElement(L"colorOfTitleText", elementString))
563                         {
564                                 ConvertStringToColor(elementString, color);
565                                 pSlider->SetTitleTextColor(color);
566                         }
567                 }
568
569                 if (pControl->GetElement(L"normalThumbBitmap", elementString))
570                 {
571                         Bitmap* pBitmap = null;
572                         pBitmap = LoadBitmapN(elementString);
573                         if (pBitmap != null)
574                         {
575                                 pSlider->SetThumbBitmap(SLIDER_THUMB_STATUS_NORMAL, *pBitmap);
576                                 delete pBitmap;
577                                 pBitmap = null;
578                         }
579                 }
580
581                 if (pControl->GetElement(L"pressedThumbBitmap", elementString))
582                 {
583                         Bitmap* pBitmap = null;
584                         pBitmap = LoadBitmapN(elementString);
585                         if (pBitmap != null)
586                         {
587                                 pSlider->SetThumbBitmap(SLIDER_THUMB_STATUS_PRESSED, *pBitmap);
588                                 delete pBitmap;
589                                 pBitmap = null;
590                         }
591                 }
592
593                 if (pControl->GetElement(L"disabledThumbBitmap", elementString))
594                 {
595                         Bitmap* pBitmap = null;
596                         pBitmap = LoadBitmapN(elementString);
597                         if (pBitmap != null)
598                         {
599                                 pSlider->SetThumbBitmap(SLIDER_THUMB_STATUS_DISABLED, *pBitmap);
600                                 delete pBitmap;
601                                 pBitmap = null;
602                         }
603                 }
604
605                 if (pControl->GetElement(L"highlightedThumbBitmap", elementString))
606                 {
607                         Bitmap* pBitmap = null;
608                         pBitmap = LoadBitmapN(elementString);
609                         if (pBitmap != null)
610                         {
611                                 pSlider->SetThumbBitmap(SLIDER_THUMB_STATUS_HIGHLIGHTED, *pBitmap);
612                                 delete pBitmap;
613                                 pBitmap = null;
614                         }
615                 }
616
617                 if (pControl->GetElement(L"leftIconBitmapPath", elementString))
618                 {
619                         Bitmap* pBitmap = null;
620                         pBitmap = LoadBitmapN(elementString);
621                         if (pBitmap != null)
622                         {
623                                 pSlider->SetIcon(ICON_POSITION_LEFT, *pBitmap);
624                                 delete pBitmap;
625                                 pBitmap = null;
626                         }
627                 }
628
629                 if (pControl->GetElement(L"rightIconBitmapPath", elementString))
630                 {
631                         Bitmap* pBitmap = null;
632                         pBitmap = LoadBitmapN(elementString);
633                         if (pBitmap != null)
634                         {
635                                 pSlider->SetIcon(ICON_POSITION_RIGHT, *pBitmap);
636                                 delete pBitmap;
637                                 pBitmap = null;
638                         }
639                 }
640
641                 if (pControl->GetElement(L"normalThumbTextColor", elementString))
642                 {
643                         ConvertStringToColor(elementString, color);
644                         pSlider->SetThumbTextColor(SLIDER_THUMB_STATUS_NORMAL, color);
645                 }
646
647                 if (pControl->GetElement(L"pressedThumbTextColor", elementString))
648                 {
649                         ConvertStringToColor(elementString, color);
650                         pSlider->SetThumbTextColor(SLIDER_THUMB_STATUS_PRESSED, color);
651                 }
652
653                 if (pControl->GetElement(L"disabledThumbTextColor", elementString))
654                 {
655                         ConvertStringToColor(elementString, color);
656                         pSlider->SetThumbTextColor(SLIDER_THUMB_STATUS_DISABLED, color);
657                 }
658
659                 if (pControl->GetElement(L"highlightedThumbTextColor", elementString))
660                 {
661                         ConvertStringToColor(elementString, color);
662                         pSlider->SetThumbTextColor(SLIDER_THUMB_STATUS_HIGHLIGHTED, color);
663                 }
664
665                 if (pControl->GetElement(L"barColor", elementString))
666                 {
667                         ConvertStringToColor(elementString, color);
668                         pSlider->SetBarColor(color);
669                 }
670                 if (pControl->GetElement(L"colorOpacity", elementString))
671                 {
672                         Base::Integer::Parse(elementString, opacity);
673                 }
674                 if (pControl->GetElement(L"color", elementString))
675                 {
676                         ConvertStringToColor32(elementString, opacity, color);
677                         pSlider->SetColor(color);
678                 }
679
680                 return pSlider;
681         }
682
683 }; // _SliderMaker
684
685 _SliderRegister::_SliderRegister(void)
686 {
687         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
688         pUiBuilderControlTableManager->RegisterControl(L"Slider", _SliderMaker::GetInstance);
689 }
690 _SliderRegister::~_SliderRegister(void)
691 {
692         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
693         pUiBuilderControlTableManager->UnregisterControl(L"Slider");
694 }
695 static _SliderRegister SliderRegisterToUiBuilder;
696 }}} // Tizen::Ui::Controls