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.
21 #include "radio-button-impl.h"
24 using namespace Dali::Toolkit::Internal;
30 const Property::Index RadioButton::PROPERTY_ACTIVE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11;
31 const Property::Index RadioButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 12;
40 return Toolkit::RadioButton::New();
43 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
45 PropertyRegistration property1(typeRegistration, "active", Toolkit::RadioButton::PROPERTY_ACTIVE, Property::BOOLEAN, &RadioButton::SetProperty, &RadioButton::GetProperty);
46 PropertyRegistration property2(typeRegistration, "label-actor", Toolkit::RadioButton::PROPERTY_LABEL_ACTOR, Property::MAP, &RadioButton::SetProperty, &RadioButton::GetProperty);
48 const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
49 const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
51 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
54 Dali::Toolkit::RadioButton RadioButton::New()
56 // Create the implementation, temporarily owned on stack
57 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
59 // Pass ownership to CustomActor
60 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
62 // Second-phase init of the implementation
63 // This can only be done after the CustomActor connection has been made...
64 internalRadioButton->Initialize();
69 RadioButton::RadioButton()
72 mInactiveImage = Dali::Image::New( INACTIVE_BUTTON_IMAGE_DIR );
73 mActiveImage = Dali::Image::New( ACTIVE_BUTTON_IMAGE_DIR );
75 mRadioIcon = Dali::ImageActor::New( mInactiveImage );
78 RadioButton::~RadioButton()
82 void RadioButton::SetLabel(const std::string& label)
84 TextActor textActor = TextActor::DownCast( mLabel );
87 textActor.SetText( label );
91 Toolkit::TextView newTextView = Toolkit::TextView::New( label );
92 SetLabel( newTextView );
98 void RadioButton::SetLabel(Actor label)
100 if( mLabel != label )
104 mRadioIcon.Remove( mLabel );
109 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
110 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
111 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
112 mRadioIcon.Add( label );
121 Actor RadioButton::GetLabel() const
126 void RadioButton::SetActive(bool active)
128 if( mActive != active )
132 Actor parent = Self().GetParent();
135 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
137 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
141 rbChild.SetActive(false);
147 mRadioIcon.SetImage(mActiveImage);
152 mRadioIcon.SetImage(mInactiveImage);
155 // Raise toggled signal
156 Toolkit::RadioButton handle( GetOwner() );
157 mToggledSignalV2.Emit( handle, mActive );
163 bool RadioButton::IsActive()const
168 void RadioButton::ToggleState()
173 void RadioButton::OnRelaidOut( Vector2 /*size*/, ActorSizeContainer& container )
175 Vector3 newSize( mRadioIcon.GetNaturalSize() );
179 // Offset the label from the radio button image
180 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
182 // Find the size of the control using size negotiation
183 Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
184 Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
186 Vector3 actorSize( mLabel.GetSize() );
187 newSize.width += actorSize.width;
188 newSize.height = std::max( newSize.height, actorSize.height );
191 Self().SetSize( newSize );
194 void RadioButton::OnInitialize()
196 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
197 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
198 Self().Add( mRadioIcon );
203 void RadioButton::OnButtonUp()
205 // Don't allow selection on an already active radio button
212 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
214 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
218 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
220 switch ( propertyIndex )
222 case Toolkit::RadioButton::PROPERTY_ACTIVE:
224 radioButtonImpl.SetActive( value.Get< bool >( ) );
227 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
229 radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
236 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
238 Property::Value value;
240 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
244 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
246 switch ( propertyIndex )
248 case Toolkit::RadioButton::PROPERTY_ACTIVE:
250 value = radioButtonImpl.mActive;
253 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
256 Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );