(Button) Allow button size to return natural size
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18
19 #include "check-box-button-impl.h"
20
21 // EXTERNAL INCLUDES
22
23 #include <algorithm>
24
25 // INTERNAL INCLUDES
26
27 #include "check-box-button-default-painter-impl.h"
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 BaseHandle Create()
42 {
43   return Toolkit::CheckBoxButton::New();
44 }
45
46 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
47
48 TypeAction a1(mType, Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK, &CheckBoxButton::DoAction);
49
50 }
51
52 namespace
53 {
54   // Helper function used to cast a ButtonPainterPtr to CheckBoxButtonDefaultPainterPtr
55   CheckBoxButtonDefaultPainterPtr GetCheckBoxButtonPainter( ButtonPainterPtr painter )
56   {
57     return static_cast<CheckBoxButtonDefaultPainter*>( painter.Get() );
58   }
59 } // namespace
60
61 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
62 {
63   // Create the implementation, temporarily owned on stack
64   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
65
66   // Pass ownership to CustomActor
67   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
68
69   // Second-phase init of the implementation
70   // This can only be done after the CustomActor connection has been made...
71   internalCheckBoxButton->Initialize();
72
73   return checkBoxButton;
74 }
75
76 void CheckBoxButton::SetChecked( bool checked )
77 {
78   if( !mDimmed && ( checked != mChecked ) )
79   {
80     // Stores the state.
81     mChecked = checked;
82
83     Toolkit::CheckBoxButton handle( GetOwner() );
84
85     // Notifies the painter the checkbox has been checked.
86     GetCheckBoxButtonPainter( mPainter )->Checked( handle );
87
88     // Emit signal.
89     mClickedSignalV2.Emit( handle );
90   }
91 }
92
93 bool CheckBoxButton::IsChecked() const
94 {
95   return mChecked;
96 }
97
98 void CheckBoxButton::SetBackgroundImage( Image image )
99 {
100   SetBackgroundImage( ImageActor::New( image ) );
101 }
102
103 void CheckBoxButton::SetBackgroundImage( Actor image )
104 {
105   Toolkit::CheckBoxButton handle( GetOwner() );
106   GetCheckBoxButtonPainter( mPainter )->SetBackgroundImage( handle, image );
107 }
108
109 Actor& CheckBoxButton::GetBackgroundImage()
110 {
111   return mBackgroundImage;
112 }
113
114 Actor CheckBoxButton::GetBackgroundImage() const
115 {
116   return mBackgroundImage;
117 }
118
119 void CheckBoxButton::SetCheckedImage( Image image )
120 {
121   SetCheckedImage( ImageActor::New( image ) );
122 }
123
124 void CheckBoxButton::SetCheckedImage( Actor image )
125 {
126   Toolkit::CheckBoxButton handle( GetOwner() );
127   GetCheckBoxButtonPainter( mPainter )->SetCheckedImage( handle, image );
128 }
129
130 Actor& CheckBoxButton::GetCheckedImage()
131 {
132   return mCheckedImage;
133 }
134
135 Actor CheckBoxButton::GetCheckedImage() const
136 {
137   return mCheckedImage;
138 }
139
140 void CheckBoxButton::SetDimmedBackgroundImage( Image image )
141 {
142   SetDimmedBackgroundImage( ImageActor::New( image ) );
143 }
144
145 void CheckBoxButton::SetDimmedBackgroundImage( Actor image )
146 {
147   Toolkit::CheckBoxButton handle( GetOwner() );
148   GetCheckBoxButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image );
149 }
150
151 Actor& CheckBoxButton::GetDimmedBackgroundImage()
152 {
153   return mDimmedBackgroundImage;
154 }
155
156 Actor CheckBoxButton::GetDimmedBackgroundImage() const
157 {
158   return mDimmedBackgroundImage;
159 }
160
161 void CheckBoxButton::SetDimmedCheckedImage( Image image )
162 {
163   SetDimmedCheckedImage( ImageActor::New( image ) );
164 }
165
166 void CheckBoxButton::SetDimmedCheckedImage( Actor image )
167 {
168   Toolkit::CheckBoxButton handle( GetOwner() );
169   GetCheckBoxButtonPainter( mPainter )->SetDimmedCheckedImage( handle, image );
170 }
171
172 Actor& CheckBoxButton::GetDimmedCheckedImage()
173 {
174   return mDimmedCheckedImage;
175 }
176
177 Actor CheckBoxButton::GetDimmedCheckedImage() const
178 {
179   return mDimmedCheckedImage;
180 }
181
182 Actor& CheckBoxButton::GetFadeOutBackgroundImage()
183 {
184   return mFadeOutBackgroundImage;
185 }
186
187 Actor& CheckBoxButton::GetFadeOutCheckedImage()
188 {
189   return mFadeOutCheckedImage;
190 }
191
192 void CheckBoxButton::OnButtonInitialize()
193 {
194   mUseFadeAnimationProperty = Self().RegisterProperty( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME, false );
195   mUseCheckAnimationProperty = Self().RegisterProperty( Toolkit::CheckBoxButton::USE_CHECK_ANIMATION_PROPERTY_NAME, true );
196 }
197
198 void CheckBoxButton::OnButtonUp()
199 {
200   if( ButtonDown == mState )
201   {
202     // Stores the state, notifies the painter and emits a signal.
203     SetChecked( !mChecked );
204   }
205 }
206
207 void CheckBoxButton::OnAnimationTimeSet( float animationTime )
208 {
209   GetCheckBoxButtonPainter( mPainter )->SetAnimationTime( animationTime );
210 }
211
212 float CheckBoxButton::OnAnimationTimeRequested() const
213 {
214   return GetCheckBoxButtonPainter( mPainter )->GetAnimationTime();
215 }
216
217 void CheckBoxButton::OnActivated()
218 {
219   // When the button is activated, it performs the click action
220   std::vector<Property::Value> attributes;
221   DoClickAction(attributes);
222 }
223
224 void CheckBoxButton::DoClickAction(const PropertyValueContainer& attributes)
225 {
226   // Prevents the button signals from doing a recursive loop by sending an action
227   // and re-emitting the signals.
228   if(!mClickActionPerforming)
229   {
230     mClickActionPerforming = true;
231     SetChecked( !mChecked );
232     mClickActionPerforming = false;
233   }
234 }
235
236 bool CheckBoxButton::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
237 {
238   bool ret = false;
239
240   Dali::BaseHandle handle(object);
241
242   Toolkit::CheckBoxButton button = Toolkit::CheckBoxButton::DownCast(handle);
243
244   if(Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK == actionName)
245   {
246     GetImplementation(button).DoClickAction(attributes);
247     ret = true;
248   }
249
250   return ret;
251 }
252
253 CheckBoxButton::CheckBoxButton()
254 : Button(),
255   mChecked( false ),
256   mClickActionPerforming(false)
257 {
258   // Creates specific painter.
259   mPainter = new CheckBoxButtonDefaultPainter();
260 }
261
262 CheckBoxButton::~CheckBoxButton()
263 {
264   mPainter = NULL;
265 }
266
267 } // namespace Internal
268
269 } // namespace Toolkit
270
271 } // namespace Dali