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";
47 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected-disabled.png";
48 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected-disabled.png";
50 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
53 Dali::Toolkit::RadioButton RadioButton::New()
55 // Create the implementation, temporarily owned on stack
56 IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
58 // Pass ownership to CustomActor
59 Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
61 // Second-phase init of the implementation
62 // This can only be done after the CustomActor connection has been made...
63 internalRadioButton->Initialize();
68 RadioButton::RadioButton()
70 SetTogglableButton(true);
73 RadioButton::~RadioButton()
77 void RadioButton::OnInitialize()
79 Button::OnInitialize();
83 // Wrap size of radio button around all its children
84 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
86 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
87 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
88 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
89 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
94 void RadioButton::OnButtonUp()
96 if( ButtonDown == GetState() )
98 // Don't allow selection on an already selected radio button
101 SetSelected( !IsSelected() );
106 void RadioButton::OnLabelSet( bool noPadding )
108 Actor& label = GetLabelActor();
112 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
113 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
115 // Radio button width is FIT_TO_CHILDREN, so the label must have a sensible policy to fill out the space
116 if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::FILL_TO_PARENT )
118 label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
121 if( IsSelected() && GetSelectedImage() )
123 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
125 else if( GetUnselectedImage() )
127 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
131 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
136 void RadioButton::OnSelected()
138 Actor& label = GetLabelActor();
140 PaintState paintState = GetPaintState();
143 case UnselectedState:
145 Actor parent = Self().GetParent();
148 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
150 Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
151 if( radioButtonChild && radioButtonChild != Self() )
153 radioButtonChild.SetSelected( false );
158 Actor& selectedImage = GetSelectedImage();
159 if( label && selectedImage )
161 label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
167 Actor& buttonImage = GetUnselectedImage();
168 if( label && buttonImage )
170 label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
181 } // namespace Internal
183 } // namespace Toolkit