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::SetImage( Actor image )
77 mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 0 ) );
78 mLayoutContainer.AddChild( image, Toolkit::TableView::CellPosition( 0, 0 ) );
83 void RadioButton::SetButtonImage( Actor image )
85 Actor& buttonImage = GetButtonImage();
89 void RadioButton::SetSelectedImage( Actor image )
91 Actor& selectedImage = GetSelectedImage();
92 selectedImage = image;
95 void RadioButton::OnButtonInitialize()
99 // Wrap size of radio button around all its children
100 self.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
102 // Create the layout container empty at first
103 mLayoutContainer = Toolkit::TableView::New( 0, 0 );
104 mLayoutContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
105 mLayoutContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
106 mLayoutContainer.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
107 self.Add( mLayoutContainer );
109 Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
110 Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
112 SetButtonImage( ImageActor::New( buttonImage ) );
113 SetSelectedImage( ImageActor::New( selectedImage ) );
115 SetImage( GetButtonImage() );
120 void RadioButton::OnButtonUp()
122 if( ButtonDown == GetState() )
124 // Don't allow selection on an already selected radio button
127 SetSelected( !IsSelected() );
132 void RadioButton::OnLabelSet()
134 Actor& label = GetLabel();
138 // Add padding to the left of the label to create distance from the image
139 label.SetPadding( Padding( DISTANCE_BETWEEN_IMAGE_AND_LABEL, 0.0f, 0.0f, 0.0f ) );
141 mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 1 ) );
142 mLayoutContainer.AddChild( label, Toolkit::TableView::CellPosition( 0, 1 ) );
146 void RadioButton::OnSelected( bool selected )
150 Actor parent = Self().GetParent();
153 for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
155 Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
156 if( radioButtonChild )
158 radioButtonChild.SetSelected( false );
163 SetImage( GetSelectedImage() );
167 SetImage( GetButtonImage() );
171 } // namespace Internal
173 } // namespace Toolkit