Merge "Typo fixed in Control implementation doc." into tizen
[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 using namespace Dali;
27 using namespace Dali::Toolkit::Internal;
28
29 namespace
30 {
31
32 BaseHandle Create()
33 {
34   return Toolkit::RadioButton::New();
35 }
36
37 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
38
39 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
40 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
41
42 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
43 }
44
45 Dali::Toolkit::RadioButton RadioButton::New()
46 {
47   // Create the implementation, temporarily owned on stack
48   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
49
50   // Pass ownership to CustomActor
51   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
52
53   // Second-phase init of the implementation
54   // This can only be done after the CustomActor connection has been made...
55   internalRadioButton->Initialize();
56
57   return radioButton;
58 }
59
60 RadioButton::RadioButton()
61 {
62   mUnselectedImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
63   mSelectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
64
65   mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
66
67 //  SetTogglableButton(true);
68   mTogglableButton = true;    // TODO: Use SetTogglableButton() after refactoring painter
69 }
70
71 RadioButton::~RadioButton()
72 {
73 }
74
75 void RadioButton::SetLabel( Actor label )
76 {
77   if( mLabel != label )
78   {
79     if( mLabel )
80     {
81       mRadioIcon.Remove( mLabel );
82     }
83
84     if( label )
85     {
86       label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
87       label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
88       label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
89       mRadioIcon.Add( label );
90     }
91
92     mLabel = label;
93
94     RelayoutRequest();
95   }
96 }
97
98 void RadioButton::SetSelected( bool selected )
99 {
100   if( IsSelected() != selected )
101   {
102     if( selected )
103     {
104       Actor parent = Self().GetParent();
105       if( parent )
106       {
107         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
108         {
109           Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
110
111           if( rbChild )
112           {
113             rbChild.SetSelected(false);
114           }
115         }
116       }
117
118       mSelected = true;
119       mRadioIcon.SetImage(mSelectedImage);
120     }
121     else
122     {
123       mSelected = false;
124       mRadioIcon.SetImage(mUnselectedImage);
125     }
126
127     // Raise state changed signal
128     Toolkit::RadioButton handle( GetOwner() );
129     StateChangedSignal().Emit( handle );
130
131     RelayoutRequest();
132   }
133 }
134
135 void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
136 {
137   Vector3 newSize( mRadioIcon.GetNaturalSize() );
138
139   Actor& label = GetLabel();
140
141   if( label )
142   {
143     // Offset the label from the radio button image
144     newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
145
146     // Find the size of the control using size negotiation
147     Vector3 actorNaturalSize( label.GetNaturalSize() );
148     Control::Relayout( label, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
149
150     Vector3 actorSize( label.GetSize() );
151     newSize.width += actorSize.width;
152     newSize.height = std::max( newSize.height, actorSize.height );
153   }
154
155   Self().SetSize( newSize );
156 }
157
158 void RadioButton::OnInitialize()
159 {
160   mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
161   mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
162   Self().Add( mRadioIcon );
163
164   RelayoutRequest();
165 }
166
167 void RadioButton::OnButtonUp()
168 {
169   if( ButtonDown == GetState() )
170   {
171     // Don't allow selection on an already selected radio button
172     if( !IsSelected() )
173     {
174       SetSelected(!IsSelected());
175     }
176   }
177 }