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/images/resource-image.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()
62 mUnselectedImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
63 mSelectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
65 mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
67 // SetTogglableButton(true);
68 mTogglableButton = true; // TODO: Use SetTogglableButton() after refactoring painter
71 RadioButton::~RadioButton()
75 void RadioButton::SetLabel( Actor label )
81 mRadioIcon.Remove( mLabel );
86 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
87 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
88 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
89 mRadioIcon.Add( label );
98 void RadioButton::SetSelected( bool selected )
100 if( IsSelected() != selected )
104 Actor parent = Self().GetParent();
107 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
109 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
113 rbChild.SetSelected(false);
119 mRadioIcon.SetImage(mSelectedImage);
124 mRadioIcon.SetImage(mUnselectedImage);
127 // Raise state changed signal
128 Toolkit::RadioButton handle( GetOwner() );
129 StateChangedSignal().Emit( handle );
135 void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
137 Vector3 newSize( mRadioIcon.GetNaturalSize() );
139 Actor& label = GetLabel();
143 // Offset the label from the radio button image
144 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
146 // Find the size of the control using size negotiation
147 Vector3 actorNaturalSize( label.GetNaturalSize() );
148 Control::Relayout( label, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
150 Vector3 actorSize( label.GetSize() );
151 newSize.width += actorSize.width;
152 newSize.height = std::max( newSize.height, actorSize.height );
155 Self().SetSize( newSize );
158 void RadioButton::OnInitialize()
160 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
161 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
162 Self().Add( mRadioIcon );
167 void RadioButton::OnButtonUp()
169 if( ButtonDown == GetState() )
171 // Don't allow selection on an already selected radio button
174 SetSelected(!IsSelected());