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>
26 using namespace Dali::Toolkit::Internal;
33 return Toolkit::RadioButton::New();
36 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
38 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
39 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
41 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
44 Dali::Toolkit::RadioButton RadioButton::New()
46 // Create the implementation, temporarily owned on stack
47 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
49 // Pass ownership to CustomActor
50 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
52 // Second-phase init of the implementation
53 // This can only be done after the CustomActor connection has been made...
54 internalRadioButton->Initialize();
59 RadioButton::RadioButton()
61 mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
62 mSelectedImage = Dali::Image::New( SELECTED_BUTTON_IMAGE_DIR );
64 mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
66 // SetTogglableButton(true);
67 mTogglableButton = true; // TODO: Use SetTogglableButton() after refactoring painter
70 RadioButton::~RadioButton()
74 void RadioButton::SetLabel( Actor label )
80 mRadioIcon.Remove( mLabel );
85 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
86 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
87 label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
88 mRadioIcon.Add( label );
97 void RadioButton::SetSelected( bool selected )
99 if( IsSelected() != selected )
103 Actor parent = Self().GetParent();
106 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
108 Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
112 rbChild.SetSelected(false);
118 mRadioIcon.SetImage(mSelectedImage);
123 mRadioIcon.SetImage(mUnselectedImage);
126 // Raise state changed signal
127 Toolkit::RadioButton handle( GetOwner() );
128 StateChangedSignal().Emit( handle );
134 void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
136 Vector3 newSize( mRadioIcon.GetNaturalSize() );
138 Actor& label = GetLabel();
142 // Offset the label from the radio button image
143 newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
145 // Find the size of the control using size negotiation
146 Vector3 actorNaturalSize( label.GetNaturalSize() );
147 Control::Relayout( label, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
149 Vector3 actorSize( label.GetSize() );
150 newSize.width += actorSize.width;
151 newSize.height = std::max( newSize.height, actorSize.height );
154 Self().SetSize( newSize );
157 void RadioButton::OnInitialize()
159 mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
160 mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
161 Self().Add( mRadioIcon );
166 void RadioButton::OnButtonUp()
168 if( ButtonDown == GetState() )
170 // Don't allow selection on an already selected radio button
173 SetSelected(!IsSelected());