2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "radio-button-impl.h"
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>
28 using namespace Dali::Toolkit::Internal;
35 return Toolkit::RadioButton::New();
38 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
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";
43 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
46 Dali::Toolkit::RadioButton RadioButton::New()
48 // Create the implementation, temporarily owned on stack
49 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
51 // Pass ownership to CustomActor
52 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
54 // Second-phase init of the implementation
55 // This can only be done after the CustomActor connection has been made...
56 internalRadioButton->Initialize();
61 RadioButton::RadioButton()
64 mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
65 mSelectedImage = Dali::Image::New( SELECTED_BUTTON_IMAGE_DIR );
67 mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
70 RadioButton::~RadioButton()
74 void RadioButton::SetLabel(const std::string& label)
76 TextActor textActor = TextActor::DownCast( mLabel );
79 textActor.SetText( label );
83 Toolkit::TextView newTextView = Toolkit::TextView::New( label );
84 SetLabel( newTextView );
90 void RadioButton::SetLabel(Actor label)
96 mRadioIcon.Remove( mLabel );
101 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
102 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
103 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
104 mRadioIcon.Add( label );
113 Actor RadioButton::GetLabel() const
118 void RadioButton::SetSelected(bool selected)
120 if( mSelected != selected )
124 Actor parent = Self().GetParent();
127 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
129 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
133 rbChild.SetSelected(false);
139 mRadioIcon.SetImage(mSelectedImage);
144 mRadioIcon.SetImage(mUnselectedImage);
147 // Raise state changed signal
148 Toolkit::RadioButton handle( GetOwner() );
149 mStateChangedSignal.Emit( handle, mSelected );
155 bool RadioButton::IsSelected()const
160 void RadioButton::ToggleState()
162 SetSelected(!mSelected);
165 void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
167 Vector3 newSize( mRadioIcon.GetNaturalSize() );
171 // Offset the label from the radio button image
172 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
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 );
178 Vector3 actorSize( mLabel.GetSize() );
179 newSize.width += actorSize.width;
180 newSize.height = std::max( newSize.height, actorSize.height );
183 Self().SetSize( newSize );
186 void RadioButton::OnInitialize()
188 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
189 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
190 Self().Add( mRadioIcon );
195 void RadioButton::OnButtonUp()
197 // Don't allow selection on an already selected radio button
204 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
206 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
210 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
212 if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
214 radioButtonImpl.SetSelected( value.Get< bool >( ) );
216 else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
218 radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
223 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
225 Property::Value value;
227 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
231 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
233 if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
235 value = radioButtonImpl.mSelected;
237 else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
240 Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );