New size negotiation
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18
19 // CLASS HEADER
20 #include "radio-button-impl.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/images/resource-image.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 BaseHandle Create()
39 {
40   return Toolkit::RadioButton::New();
41 }
42
43 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
44
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
48 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL = 5.0f;
49 }
50
51 Dali::Toolkit::RadioButton RadioButton::New()
52 {
53   // Create the implementation, temporarily owned on stack
54   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
55
56   // Pass ownership to CustomActor
57   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
58
59   // Second-phase init of the implementation
60   // This can only be done after the CustomActor connection has been made...
61   internalRadioButton->Initialize();
62
63   return radioButton;
64 }
65
66 RadioButton::RadioButton()
67 {
68   SetTogglableButton(true);
69 }
70
71 RadioButton::~RadioButton()
72 {
73 }
74
75 void RadioButton::SetImage( Actor image )
76 {
77   mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 0 ) );
78   mLayoutContainer.AddChild( image, Toolkit::TableView::CellPosition( 0, 0 ) );
79
80   RelayoutRequest();
81 }
82
83 void RadioButton::SetButtonImage( Actor image )
84 {
85   Actor& buttonImage = GetButtonImage();
86   buttonImage = image;
87 }
88
89 void RadioButton::SetSelectedImage( Actor image )
90 {
91   Actor& selectedImage = GetSelectedImage();
92   selectedImage = image;
93 }
94
95 void RadioButton::OnButtonInitialize()
96 {
97   Actor self = Self();
98
99   // Wrap size of radio button around all its children
100   self.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
101
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 );
108
109   Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
110   Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
111
112   SetButtonImage( ImageActor::New( buttonImage ) );
113   SetSelectedImage( ImageActor::New( selectedImage ) );
114
115   SetImage( GetButtonImage() );
116
117   RelayoutRequest();
118 }
119
120 void RadioButton::OnButtonUp()
121 {
122   if( ButtonDown == GetState() )
123   {
124     // Don't allow selection on an already selected radio button
125     if( !IsSelected() )
126     {
127       SetSelected( !IsSelected() );
128     }
129   }
130 }
131
132 void RadioButton::OnLabelSet()
133 {
134   Actor& label = GetLabel();
135
136   if( label )
137   {
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 ) );
140
141     mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 1 ) );
142     mLayoutContainer.AddChild( label, Toolkit::TableView::CellPosition( 0, 1 ) );
143   }
144 }
145
146 void RadioButton::OnSelected( bool selected )
147 {
148   if( selected )
149   {
150     Actor parent = Self().GetParent();
151     if( parent )
152     {
153       for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
154       {
155         Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
156         if( radioButtonChild )
157         {
158           radioButtonChild.SetSelected( false );
159         }
160       }
161     }
162
163     SetImage( GetSelectedImage() );
164   }
165   else
166   {
167     SetImage( GetButtonImage() );
168   }
169 }
170
171 } // namespace Internal
172
173 } // namespace Toolkit
174
175 } // namespace Dali