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;
34 const Property::Index RadioButton::PROPERTY_ACTIVE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11;
35 const Property::Index RadioButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 12;
44 return Toolkit::RadioButton::New();
47 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
49 PropertyRegistration property1(typeRegistration, "active", Toolkit::RadioButton::PROPERTY_ACTIVE, Property::BOOLEAN, &RadioButton::SetProperty, &RadioButton::GetProperty);
50 PropertyRegistration property2(typeRegistration, "label-actor", Toolkit::RadioButton::PROPERTY_LABEL_ACTOR, Property::MAP, &RadioButton::SetProperty, &RadioButton::GetProperty);
52 const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
53 const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
55 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
58 Dali::Toolkit::RadioButton RadioButton::New()
60 // Create the implementation, temporarily owned on stack
61 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
63 // Pass ownership to CustomActor
64 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
66 // Second-phase init of the implementation
67 // This can only be done after the CustomActor connection has been made...
68 internalRadioButton->Initialize();
73 RadioButton::RadioButton()
76 mInactiveImage = Dali::Image::New( INACTIVE_BUTTON_IMAGE_DIR );
77 mActiveImage = Dali::Image::New( ACTIVE_BUTTON_IMAGE_DIR );
79 mRadioIcon = Dali::ImageActor::New( mInactiveImage );
82 RadioButton::~RadioButton()
86 void RadioButton::SetLabel(const std::string& label)
88 TextActor textActor = TextActor::DownCast( mLabel );
91 textActor.SetText( label );
95 Toolkit::TextView newTextView = Toolkit::TextView::New( label );
96 SetLabel( newTextView );
102 void RadioButton::SetLabel(Actor label)
104 if( mLabel != label )
108 mRadioIcon.Remove( mLabel );
113 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
114 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
115 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
116 mRadioIcon.Add( label );
125 Actor RadioButton::GetLabel() const
130 void RadioButton::SetActive(bool active)
132 if( mActive != active )
136 Actor parent = Self().GetParent();
139 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
141 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
145 rbChild.SetActive(false);
151 mRadioIcon.SetImage(mActiveImage);
156 mRadioIcon.SetImage(mInactiveImage);
159 // Raise toggled signal
160 Toolkit::RadioButton handle( GetOwner() );
161 mToggledSignal.Emit( handle, mActive );
167 bool RadioButton::IsActive()const
172 void RadioButton::ToggleState()
177 void RadioButton::OnRelaidOut( Vector2 /*size*/, ActorSizeContainer& container )
179 Vector3 newSize( mRadioIcon.GetNaturalSize() );
183 // Offset the label from the radio button image
184 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
186 // Find the size of the control using size negotiation
187 Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
188 Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
190 Vector3 actorSize( mLabel.GetSize() );
191 newSize.width += actorSize.width;
192 newSize.height = std::max( newSize.height, actorSize.height );
195 Self().SetSize( newSize );
198 void RadioButton::OnInitialize()
200 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
201 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
202 Self().Add( mRadioIcon );
207 void RadioButton::OnButtonUp()
209 // Don't allow selection on an already active radio button
216 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
218 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
222 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
224 switch ( propertyIndex )
226 case Toolkit::RadioButton::PROPERTY_ACTIVE:
228 radioButtonImpl.SetActive( value.Get< bool >( ) );
231 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
233 radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
240 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
242 Property::Value value;
244 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
248 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
250 switch ( propertyIndex )
252 case Toolkit::RadioButton::PROPERTY_ACTIVE:
254 value = radioButtonImpl.mActive;
257 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
260 Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );