Revert to tizen branch.
[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::OnButtonInitialize()
76 {
77   Actor self = Self();
78
79   // Wrap size of radio button around all its children
80   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
81
82   Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
83   Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
84
85   SetButtonImage( ImageActor::New( buttonImage ) );
86   SetSelectedImage( ImageActor::New( selectedImage ) );
87
88   RelayoutRequest();
89 }
90
91 void RadioButton::OnButtonUp()
92 {
93   if( ButtonDown == GetState() )
94   {
95     // Don't allow selection on an already selected radio button
96     if( !IsSelected() )
97     {
98       SetSelected( !IsSelected() );
99     }
100   }
101 }
102
103 void RadioButton::OnLabelSet()
104 {
105   Actor& label = GetLabel();
106
107   if( label )
108   {
109     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
110     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
111
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 )
114     {
115       label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
116     }
117
118     if( IsSelected() )
119     {
120       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
121     }
122     else
123     {
124       label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
125     }
126   }
127 }
128
129 bool RadioButton::OnSelected()
130 {
131   Actor& buttonImage = GetButtonImage();
132   Actor& selectedImage = GetSelectedImage();
133   Actor& label = GetLabel();
134
135   PaintState paintState = GetPaintState();
136
137   switch( paintState )
138   {
139     case UnselectedState:
140     {
141       Actor parent = Self().GetParent();
142       if( parent )
143       {
144         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
145         {
146           Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
147           if( radioButtonChild && radioButtonChild != Self() )
148           {
149             radioButtonChild.SetSelected( false );
150           }
151         }
152       }
153
154       RemoveChild( buttonImage );
155
156       if( label )
157       {
158         label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
159       }
160       break;
161     }
162     case SelectedState:
163     {
164       RemoveChild( selectedImage );
165
166       if( label )
167       {
168         label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
169       }
170       break;
171     }
172     default:
173     {
174       break;
175     }
176   }
177
178   // there is no animation
179   return false;
180 }
181
182 } // namespace Internal
183
184 } // namespace Toolkit
185
186 } // namespace Dali