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