41e4a6f26955473d33f98311a9bf086d7a9fdb82
[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 const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
49 const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
50
51 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
52 }
53
54 Dali::Toolkit::RadioButton RadioButton::New()
55 {
56   // Create the implementation, temporarily owned on stack
57   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
58
59   // Pass ownership to CustomActor
60   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
61
62   // Second-phase init of the implementation
63   // This can only be done after the CustomActor connection has been made...
64   internalRadioButton->Initialize();
65
66   return radioButton;
67 }
68
69 RadioButton::RadioButton()
70   : mActive(false)
71 {
72   mInactiveImage = Dali::Image::New( INACTIVE_BUTTON_IMAGE_DIR );
73   mActiveImage = Dali::Image::New( ACTIVE_BUTTON_IMAGE_DIR );
74
75   mRadioIcon = Dali::ImageActor::New( mInactiveImage );
76 }
77
78 RadioButton::~RadioButton()
79 {
80 }
81
82 void RadioButton::SetLabel(const std::string& label)
83 {
84   TextActor textActor = TextActor::DownCast( mLabel );
85   if( textActor )
86   {
87     textActor.SetText( label );
88   }
89   else
90   {
91     Toolkit::TextView newTextView = Toolkit::TextView::New( label );
92     SetLabel( newTextView );
93   }
94
95   RelayoutRequest();
96 }
97
98 void RadioButton::SetLabel(Actor label)
99 {
100   if( mLabel != label )
101   {
102     if( mLabel )
103     {
104       mRadioIcon.Remove( mLabel );
105     }
106
107     if( label )
108     {
109       label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
110       label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
111       label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
112       mRadioIcon.Add( label );
113     }
114
115     mLabel = label;
116
117     RelayoutRequest();
118   }
119 }
120
121 Actor RadioButton::GetLabel() const
122 {
123   return mLabel;
124 }
125
126 void RadioButton::SetActive(bool active)
127 {
128   if( mActive != active )
129   {
130     if( active )
131     {
132       Actor parent = Self().GetParent();
133       if( parent )
134       {
135         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
136         {
137           Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
138
139           if( rbChild )
140           {
141             rbChild.SetActive(false);
142           }
143         }
144       }
145
146       mActive = true;
147       mRadioIcon.SetImage(mActiveImage);
148     }
149     else
150     {
151       mActive = false;
152       mRadioIcon.SetImage(mInactiveImage);
153     }
154
155     // Raise toggled signal
156     Toolkit::RadioButton handle( GetOwner() );
157     mToggledSignalV2.Emit( handle, mActive );
158
159     RelayoutRequest();
160   }
161 }
162
163 bool RadioButton::IsActive()const
164 {
165   return mActive;
166 }
167
168 void RadioButton::ToggleState()
169 {
170   SetActive(!mActive);
171 }
172
173 void RadioButton::OnRelaidOut( Vector2 /*size*/, ActorSizeContainer& container )
174 {
175   Vector3 newSize( mRadioIcon.GetNaturalSize() );
176
177   if( mLabel )
178   {
179     // Offset the label from the radio button image
180     newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
181
182     // Find the size of the control using size negotiation
183     Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
184     Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
185
186     Vector3 actorSize( mLabel.GetSize() );
187     newSize.width += actorSize.width;
188     newSize.height = std::max( newSize.height, actorSize.height );
189   }
190
191   Self().SetSize( newSize );
192 }
193
194 void RadioButton::OnInitialize()
195 {
196   mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
197   mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
198   Self().Add( mRadioIcon );
199
200   RelayoutRequest();
201 }
202
203 void RadioButton::OnButtonUp()
204 {
205   // Don't allow selection on an already active radio button
206   if( !mActive )
207   {
208     ToggleState();
209   }
210 }
211
212 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
213 {
214   Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
215
216   if( radioButton )
217   {
218     RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
219
220     switch ( propertyIndex )
221     {
222       case Toolkit::RadioButton::PROPERTY_ACTIVE:
223       {
224         radioButtonImpl.SetActive( value.Get< bool >( ) );
225         break;
226       }
227       case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
228       {
229         radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
230         break;
231       }
232     }
233   }
234 }
235
236 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
237 {
238   Property::Value value;
239
240   Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
241
242   if( radioButton )
243   {
244     RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
245
246     switch ( propertyIndex )
247     {
248       case Toolkit::RadioButton::PROPERTY_ACTIVE:
249       {
250         value = radioButtonImpl.mActive;
251         break;
252       }
253       case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
254       {
255         Property::Map map;
256         Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );
257         value = map;
258         break;
259       }
260     }
261   }
262
263   return value;
264 }