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