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