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/object/type-registry.h>
24 #include <dali/public-api/scripting/scripting.h>
27 using namespace Dali::Toolkit::Internal;
34 return Toolkit::RadioButton::New();
37 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
39 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
40 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
42 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
45 Dali::Toolkit::RadioButton RadioButton::New()
47 // Create the implementation, temporarily owned on stack
48 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
50 // Pass ownership to CustomActor
51 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
53 // Second-phase init of the implementation
54 // This can only be done after the CustomActor connection has been made...
55 internalRadioButton->Initialize();
60 RadioButton::RadioButton()
63 mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
64 mSelectedImage = Dali::Image::New( SELECTED_BUTTON_IMAGE_DIR );
66 mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
69 RadioButton::~RadioButton()
73 void RadioButton::SetLabel(const std::string& label)
80 void RadioButton::SetLabel(Actor label)
86 mRadioIcon.Remove( mLabel );
91 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
92 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
93 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
94 mRadioIcon.Add( label );
103 Actor RadioButton::GetLabel() const
108 void RadioButton::SetSelected(bool selected)
110 if( mSelected != selected )
114 Actor parent = Self().GetParent();
117 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
119 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
123 rbChild.SetSelected(false);
129 mRadioIcon.SetImage(mSelectedImage);
134 mRadioIcon.SetImage(mUnselectedImage);
137 // Raise state changed signal
138 Toolkit::RadioButton handle( GetOwner() );
139 mStateChangedSignal.Emit( handle, mSelected );
145 bool RadioButton::IsSelected()const
150 void RadioButton::ToggleState()
152 SetSelected(!mSelected);
155 void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
157 Vector3 newSize( mRadioIcon.GetNaturalSize() );
161 // Offset the label from the radio button image
162 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
164 // Find the size of the control using size negotiation
165 Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
166 Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
168 Vector3 actorSize( mLabel.GetSize() );
169 newSize.width += actorSize.width;
170 newSize.height = std::max( newSize.height, actorSize.height );
173 Self().SetSize( newSize );
176 void RadioButton::OnInitialize()
178 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
179 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
180 Self().Add( mRadioIcon );
185 void RadioButton::OnButtonUp()
187 // Don't allow selection on an already selected radio button
194 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
196 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
200 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
202 if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
204 radioButtonImpl.SetSelected( value.Get< bool >( ) );
206 else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
208 radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
213 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
215 Property::Value value;
217 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
221 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
223 if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
225 value = radioButtonImpl.mSelected;
227 else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
230 Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );