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>
40 return Toolkit::RadioButton::New();
43 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
45 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
46 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
48 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
51 Dali::Toolkit::RadioButton RadioButton::New()
53 // Create the implementation, temporarily owned on stack
54 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
56 // Pass ownership to CustomActor
57 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
59 // Second-phase init of the implementation
60 // This can only be done after the CustomActor connection has been made...
61 internalRadioButton->Initialize();
66 RadioButton::RadioButton()
68 SetTogglableButton(true);
71 RadioButton::~RadioButton()
75 void RadioButton::OnButtonInitialize()
79 // Wrap size of radio button around all its children
80 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
82 Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
83 Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
85 SetButtonImage( ImageActor::New( buttonImage ) );
86 SetSelectedImage( ImageActor::New( selectedImage ) );
91 void RadioButton::OnButtonUp()
93 if( ButtonDown == GetState() )
95 // Don't allow selection on an already selected radio button
98 SetSelected( !IsSelected() );
103 void RadioButton::OnLabelSet()
105 Actor& label = GetLabel();
109 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
110 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
112 // Radio button width is FIT_TO_CHILDREN, so the label must have a sensible policy to fill out the space
113 if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::FILL_TO_PARENT )
115 label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
120 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
124 label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
129 bool RadioButton::OnSelected()
131 Actor& buttonImage = GetButtonImage();
132 Actor& selectedImage = GetSelectedImage();
133 Actor& label = GetLabel();
135 PaintState paintState = GetPaintState();
139 case UnselectedState:
141 Actor parent = Self().GetParent();
144 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
146 Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
147 if( radioButtonChild && radioButtonChild != Self() )
149 radioButtonChild.SetSelected( false );
154 RemoveChild( buttonImage );
158 label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
164 RemoveChild( selectedImage );
168 label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
178 // there is no animation
182 } // namespace Internal
184 } // namespace Toolkit