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;
33 const Property::Index RadioButton::PROPERTY_ACTIVE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11;
34 const Property::Index RadioButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 12;
43 return Toolkit::RadioButton::New();
46 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
48 PropertyRegistration property1(typeRegistration, "active", Toolkit::RadioButton::PROPERTY_ACTIVE, Property::BOOLEAN, &RadioButton::SetProperty, &RadioButton::GetProperty);
49 PropertyRegistration property2(typeRegistration, "label-actor", Toolkit::RadioButton::PROPERTY_LABEL_ACTOR, Property::MAP, &RadioButton::SetProperty, &RadioButton::GetProperty);
51 const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
52 const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
54 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
57 Dali::Toolkit::RadioButton RadioButton::New()
59 // Create the implementation, temporarily owned on stack
60 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
62 // Pass ownership to CustomActor
63 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
65 // Second-phase init of the implementation
66 // This can only be done after the CustomActor connection has been made...
67 internalRadioButton->Initialize();
72 RadioButton::RadioButton()
75 mInactiveImage = Dali::Image::New( INACTIVE_BUTTON_IMAGE_DIR );
76 mActiveImage = Dali::Image::New( ACTIVE_BUTTON_IMAGE_DIR );
78 mRadioIcon = Dali::ImageActor::New( mInactiveImage );
81 RadioButton::~RadioButton()
85 void RadioButton::SetLabel(const std::string& label)
92 void RadioButton::SetLabel(Actor label)
98 mRadioIcon.Remove( mLabel );
103 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
104 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
105 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
106 mRadioIcon.Add( label );
115 Actor RadioButton::GetLabel() const
120 void RadioButton::SetActive(bool active)
122 if( mActive != active )
126 Actor parent = Self().GetParent();
129 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
131 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
135 rbChild.SetActive(false);
141 mRadioIcon.SetImage(mActiveImage);
146 mRadioIcon.SetImage(mInactiveImage);
149 // Raise toggled signal
150 Toolkit::RadioButton handle( GetOwner() );
151 mToggledSignalV2.Emit( handle, mActive );
157 bool RadioButton::IsActive()const
162 void RadioButton::ToggleState()
167 void RadioButton::OnRelaidOut( Vector2 /*size*/, ActorSizeContainer& container )
169 Vector3 newSize( mRadioIcon.GetNaturalSize() );
173 // Offset the label from the radio button image
174 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
176 // Find the size of the control using size negotiation
177 Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
178 Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
180 Vector3 actorSize( mLabel.GetSize() );
181 newSize.width += actorSize.width;
182 newSize.height = std::max( newSize.height, actorSize.height );
185 Self().SetSize( newSize );
188 void RadioButton::OnInitialize()
190 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
191 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
192 Self().Add( mRadioIcon );
197 void RadioButton::OnButtonUp()
199 // Don't allow selection on an already active radio button
206 void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
208 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
212 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
214 switch ( propertyIndex )
216 case Toolkit::RadioButton::PROPERTY_ACTIVE:
218 radioButtonImpl.SetActive( value.Get< bool >( ) );
221 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
223 radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
230 Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
232 Property::Value value;
234 Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
238 RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
240 switch ( propertyIndex )
242 case Toolkit::RadioButton::PROPERTY_ACTIVE:
244 value = radioButtonImpl.mActive;
247 case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
250 Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );