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( FIT_TO_CHILDREN, 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 );
114 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
118 label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
123 bool RadioButton::OnSelected()
125 Actor& buttonImage = GetButtonImage();
126 Actor& selectedImage = GetSelectedImage();
127 Actor& label = GetLabel();
129 PaintState paintState = GetPaintState();
133 case UnselectedState:
135 Actor parent = Self().GetParent();
138 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
140 Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
141 if( radioButtonChild && radioButtonChild != Self() )
143 radioButtonChild.SetSelected( false );
148 RemoveChild( buttonImage );
152 label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
158 RemoveChild( selectedImage );
162 label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
172 // there is no animation
176 } // namespace Internal
178 } // namespace Toolkit