Typo fixed in Control implementation doc.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-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 // CLASS HEADER
19 #include "check-box-button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 // INTERNAL INCLUDES
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( !mDisabled && ( 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     // Raise state changed signal
89     mStateChangedSignal.Emit( handle, mChecked );
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::SetDisabledBackgroundImage( Image image )
141 {
142   SetDisabledBackgroundImage( ImageActor::New( image ) );
143 }
144
145 void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
146 {
147   Toolkit::CheckBoxButton handle( GetOwner() );
148   GetCheckBoxButtonPainter( mPainter )->SetDisabledBackgroundImage( handle, image );
149 }
150
151 Actor& CheckBoxButton::GetDisabledBackgroundImage()
152 {
153   return mDisabledBackgroundImage;
154 }
155
156 Actor CheckBoxButton::GetDisabledBackgroundImage() const
157 {
158   return mDisabledBackgroundImage;
159 }
160
161 void CheckBoxButton::SetDisabledCheckedImage( Image image )
162 {
163   SetDisabledCheckedImage( ImageActor::New( image ) );
164 }
165
166 void CheckBoxButton::SetDisabledCheckedImage( Actor image )
167 {
168   Toolkit::CheckBoxButton handle( GetOwner() );
169   GetCheckBoxButtonPainter( mPainter )->SetDisabledCheckedImage( handle, image );
170 }
171
172 Actor& CheckBoxButton::GetDisabledCheckedImage()
173 {
174   return mDisabledCheckedImage;
175 }
176
177 Actor CheckBoxButton::GetDisabledCheckedImage() const
178 {
179   return mDisabledCheckedImage;
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 }
195
196 void CheckBoxButton::OnButtonUp()
197 {
198   if( ButtonDown == mState )
199   {
200     // Stores the state, notifies the painter and emits a signal.
201     SetChecked( !mChecked );
202   }
203 }
204
205 void CheckBoxButton::OnAnimationTimeSet( float animationTime )
206 {
207   GetCheckBoxButtonPainter( mPainter )->SetAnimationTime( animationTime );
208 }
209
210 float CheckBoxButton::OnAnimationTimeRequested() const
211 {
212   return GetCheckBoxButtonPainter( mPainter )->GetAnimationTime();
213 }
214
215 void CheckBoxButton::OnActivated()
216 {
217   // When the button is activated, it performs the click action
218   PropertyValueContainer attributes;
219   DoClickAction(attributes);
220 }
221
222 void CheckBoxButton::DoClickAction(const PropertyValueContainer& attributes)
223 {
224   // Prevents the button signals from doing a recursive loop by sending an action
225   // and re-emitting the signals.
226   if(!mClickActionPerforming)
227   {
228     mClickActionPerforming = true;
229     SetChecked( !mChecked );
230     mClickActionPerforming = false;
231   }
232 }
233
234 bool CheckBoxButton::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
235 {
236   bool ret = false;
237
238   Dali::BaseHandle handle(object);
239
240   Toolkit::CheckBoxButton button = Toolkit::CheckBoxButton::DownCast(handle);
241
242   if(Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK == actionName)
243   {
244     GetImplementation(button).DoClickAction(attributes);
245     ret = true;
246   }
247
248   return ret;
249 }
250
251 CheckBoxButton::CheckBoxButton()
252 : Button(),
253   mChecked( false ),
254   mClickActionPerforming(false)
255 {
256   // Creates specific painter.
257   mPainter = new CheckBoxButtonDefaultPainter();
258 }
259
260 CheckBoxButton::~CheckBoxButton()
261 {
262   mPainter = NULL;
263 }
264
265 } // namespace Internal
266
267 } // namespace Toolkit
268
269 } // namespace Dali