[dali_1.0.10] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18
19 // CLASS HEADER
20
21 #include "radio-button-impl.h"
22
23 using namespace Dali;
24 using namespace Dali::Toolkit::Internal;
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30 const Property::Index RadioButton::PROPERTY_ACTIVE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11;
31 const Property::Index RadioButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 12;
32 }
33 }
34
35 namespace
36 {
37
38 BaseHandle Create()
39 {
40   return Toolkit::RadioButton::New();
41 }
42
43 TypeRegistration typeRegistration(typeid (Toolkit::RadioButton ), typeid (Toolkit::Button ), Create);
44
45 PropertyRegistration property1(typeRegistration, "active", Toolkit::RadioButton::PROPERTY_ACTIVE, Property::BOOLEAN, &RadioButton::SetProperty, &RadioButton::GetProperty);
46 PropertyRegistration property2(typeRegistration, "label-actor", Toolkit::RadioButton::PROPERTY_LABEL_ACTOR, Property::MAP, &RadioButton::SetProperty, &RadioButton::GetProperty);
47 }
48
49 namespace
50 {
51 const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
52 const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
53 const Vector3 IMAGE_WIDTH(16.f, 0.f, 0.f);
54 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.f, 0.f, 0.f);
55 }
56
57 Dali::Toolkit::RadioButton RadioButton::New()
58 {
59   // Create the implementation, temporarily owned on stack
60   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
61
62   // Pass ownership to CustomActor
63   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
64
65   // Second-phase init of the implementation
66   // This can only be done after the CustomActor connection has been made...
67   internalRadioButton->Initialize();
68
69   return radioButton;
70 }
71
72 RadioButton::RadioButton()
73   : Button(),
74   mActive(false)
75 {
76   mInactiveImage = Dali::Image::New(INACTIVE_BUTTON_IMAGE_DIR);
77   mActiveImage = Dali::Image::New(ACTIVE_BUTTON_IMAGE_DIR);
78
79   mImageActor = Dali::ImageActor::New(mInactiveImage);
80   mLabel = Actor::New();
81 }
82
83 RadioButton::~RadioButton()
84 {
85 }
86
87 void RadioButton::SetLabel(const std::string& label)
88 {
89   mLabel.Reset();
90   mLabel = Actor::New();
91
92   Toolkit::TextView textView = Toolkit::TextView::New(label);
93   textView.SetWidthExceedPolicy(Toolkit::TextView::ShrinkToFit); // Make sure our text always fits inside the button
94   textView.SetAnchorPoint(AnchorPoint::TOP_LEFT);
95
96   mLabel.Add(textView);
97 }
98
99 void RadioButton::SetLabel(Actor label)
100 {
101   if( mLabel != label )
102   {
103     Self().Remove(mLabel);
104     mLabel = label;
105     Self().Add(mLabel);
106   }
107 }
108
109 Actor RadioButton::GetLabel() const
110 {
111   return mLabel;
112 }
113
114 void RadioButton::SetActive(bool active)
115 {
116   if( mActive != active )
117   {
118     if( active )
119     {
120       Actor parent = Self().GetParent();
121
122       if( parent )
123       {
124         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
125         {
126           Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
127
128           if( rbChild )
129           {
130             rbChild.SetActive(false);
131           }
132         }
133       }
134       mActive = true;
135       mImageActor.SetImage(mActiveImage);
136     }
137     else
138     {
139       mActive = false;
140       mImageActor.SetImage(mInactiveImage);
141     }
142   }
143 }
144
145 bool RadioButton::IsActive()const
146 {
147   return mActive;
148 }
149
150 void RadioButton::ToggleState()
151 {
152   SetActive(!mActive);
153 }
154
155 void RadioButton::OnInitialize()
156 {
157   mImageActor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
158   Self().Add(mImageActor);
159
160   mLabel.SetAnchorPoint(AnchorPoint::TOP_LEFT);
161   mLabel.MoveBy(IMAGE_WIDTH + DISTANCE_BETWEEN_IMAGE_AND_LABEL);
162   Self().Add(mLabel);
163 }
164
165 void RadioButton::OnButtonUp()
166 {
167   ToggleState();
168 }
169
170 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
171 {
172   Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast(Dali::BaseHandle(object));
173
174   if( radioButton )
175   {
176     RadioButton & radioButtonImpl(GetImplementation(radioButton));
177
178     switch ( propertyIndex )
179     {
180       case Toolkit::RadioButton::PROPERTY_ACTIVE:
181       {
182         radioButtonImpl.SetActive(value.Get< bool >( ));
183         break;
184       }
185       case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
186       {
187         radioButtonImpl.SetLabel(Scripting::NewActor(value.Get< Property::Map >( )));
188         break;
189       }
190     }
191   }
192 }
193
194 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
195 {
196   Property::Value value;
197
198   Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast(Dali::BaseHandle(object));
199
200   if( radioButton )
201   {
202     RadioButton & radioButtonImpl(GetImplementation(radioButton));
203
204     switch ( propertyIndex )
205     {
206       case Toolkit::RadioButton::PROPERTY_ACTIVE:
207       {
208         value = radioButtonImpl.mActive;
209         break;
210       }
211       case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
212       {
213         Property::Map map;
214         Scripting::CreatePropertyMap(radioButtonImpl.mLabel, map);
215         value = map;
216         break;
217       }
218     }
219   }
220
221   return value;
222 }