2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
19 #include "push-button-impl.h"
27 #include "push-button-default-painter-impl.h"
29 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
47 return Toolkit::PushButton::New();
50 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
52 SignalConnectorType signalConnector1( typeRegistration, Toolkit::PushButton::SIGNAL_TOGGLED , &PushButton::DoConnectSignal );
53 SignalConnectorType signalConnector2( typeRegistration, Toolkit::PushButton::SIGNAL_PRESSED , &PushButton::DoConnectSignal );
54 SignalConnectorType signalConnector3( typeRegistration, Toolkit::PushButton::SIGNAL_RELEASED, &PushButton::DoConnectSignal );
56 TypeAction action1( typeRegistration, Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK, &PushButton::DoAction );
63 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
64 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
66 // Helper function used to cast a ButtonPainter to PushButtonDefaultPainter
67 PushButtonDefaultPainterPtr GetPushButtonPainter( Dali::Toolkit::Internal::ButtonPainterPtr painter )
69 return static_cast<PushButtonDefaultPainter*>( painter.Get() );
73 Dali::Toolkit::PushButton PushButton::New()
75 // Create the implementation, temporarily owned on stack
76 IntrusivePtr< PushButton > internalPushButton = new PushButton();
78 // Pass ownership to CustomActor
79 Dali::Toolkit::PushButton pushButton( *internalPushButton );
81 // Second-phase init of the implementation
82 // This can only be done after the CustomActor connection has been made...
83 internalPushButton->Initialize();
88 void PushButton::SetAutoRepeating( bool autoRepeating )
90 mAutoRepeating = autoRepeating;
92 // An autorepeating button can't be a toggle button.
95 mToggleButton = false;
98 // Emit a signal is not wanted, only change the appearance.
99 Toolkit::PushButton handle( GetOwner() );
100 GetPushButtonPainter( mPainter )->Toggled( handle );
105 // Notifies the painter.
106 GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating );
109 bool PushButton::IsAutoRepeating() const
111 return mAutoRepeating;
114 void PushButton::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
116 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
117 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
120 float PushButton::GetInitialAutoRepeatingDelay() const
122 return mInitialAutoRepeatingDelay;
125 void PushButton::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
127 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
128 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
131 float PushButton::GetNextAutoRepeatingDelay() const
133 return mNextAutoRepeatingDelay;
136 void PushButton::SetToggleButton( bool toggle )
138 mToggleButton = toggle;
140 // A toggle button can't be an autorepeating button.
143 mAutoRepeating = false;
145 // Notifies the painter.
146 GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating );
150 bool PushButton::IsToggleButton() const
152 return mToggleButton;
155 void PushButton::SetToggled( bool toggle )
157 if( !mDimmed && mToggleButton && ( toggle != mToggled ) )
161 Toolkit::PushButton handle( GetOwner() );
163 // Notifies the painter the button has been toggled.
164 GetPushButtonPainter( mPainter )->Toggled( handle );
167 mToggledSignalV2.Emit( handle, mToggled );
171 bool PushButton::IsToggled() const
173 return mToggleButton && mToggled;
176 void PushButton::SetButtonImage( Image image )
178 SetButtonImage( ImageActor::New( image ) );
181 void PushButton::SetButtonImage( Actor image )
183 Toolkit::PushButton handle( GetOwner() );
184 GetPushButtonPainter( mPainter )->SetButtonImage( handle, image );
187 Actor& PushButton::GetButtonImage()
192 Actor PushButton::GetButtonImage() const
197 void PushButton::SetBackgroundImage( Image image )
199 SetBackgroundImage( ImageActor::New( image ) );
202 void PushButton::SetBackgroundImage( Actor image )
204 Toolkit::PushButton handle( GetOwner() );
205 GetPushButtonPainter( mPainter )->SetBackgroundImage( handle, image );
208 Actor& PushButton::GetBackgroundImage()
210 return mBackgroundImage;
213 Actor PushButton::GetBackgroundImage() const
215 return mBackgroundImage;
218 void PushButton::SetPressedImage( Image image )
220 SetPressedImage( ImageActor::New( image ) );
223 void PushButton::SetPressedImage( Actor image )
225 Toolkit::PushButton handle( GetOwner() );
226 GetPushButtonPainter( mPainter )->SetPressedImage( handle, image );
229 Actor& PushButton::GetPressedImage()
231 return mPressedImage;
234 Actor PushButton::GetPressedImage() const
236 return mPressedImage;
239 void PushButton::SetDimmedBackgroundImage( Image image )
241 SetDimmedBackgroundImage( ImageActor::New( image ) );
244 void PushButton::SetDimmedBackgroundImage( Actor image )
246 Toolkit::PushButton handle( GetOwner() );
247 GetPushButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image );
250 Actor& PushButton::GetDimmedBackgroundImage()
252 return mDimmedBackgroundImage;
255 Actor PushButton::GetDimmedBackgroundImage() const
257 return mDimmedBackgroundImage;
260 void PushButton::SetDimmedImage( Image image )
262 SetDimmedImage( ImageActor::New( image ) );
265 void PushButton::SetDimmedImage( Actor image )
267 Toolkit::PushButton handle( GetOwner() );
268 GetPushButtonPainter( mPainter )->SetDimmedImage( handle, image );
271 Actor& PushButton::GetDimmedImage()
276 Actor PushButton::GetDimmedImage() const
281 void PushButton::SetLabelText( const std::string& text )
283 Toolkit::TextView textView ( Toolkit::TextView::New( text ) );
284 textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button
285 SetLabelText( textView );
288 void PushButton::SetLabelText( Actor text )
290 Toolkit::PushButton handle( GetOwner() );
291 GetPushButtonPainter( mPainter )->SetLabelText( handle, text );
294 Actor& PushButton::GetLabel()
299 Actor PushButton::GetLabelText() const
304 Actor& PushButton::GetFadeOutBackgroundImage()
306 return mFadeOutBackgroundImage;
309 Actor& PushButton::GetFadeOutButtonImage()
311 return mFadeOutButtonImage;
314 Toolkit::PushButton::ToggledSignalV2& PushButton::ToggledSignal()
316 return mToggledSignalV2;
319 Toolkit::PushButton::PressedSignalV2& PushButton::PressedSignal()
321 return mPressedSignalV2;
324 Toolkit::PushButton::ReleasedSignalV2& PushButton::ReleasedSignal()
326 return mReleasedSignalV2;
329 bool PushButton::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
331 Dali::BaseHandle handle( object );
333 bool connected( true );
334 Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle);
336 if( Toolkit::PushButton::SIGNAL_TOGGLED == signalName )
338 button.ToggledSignal().Connect( tracker, functor );
340 else if( Toolkit::PushButton::SIGNAL_PRESSED == signalName )
342 button.PressedSignal().Connect( tracker, functor );
344 else if( Toolkit::PushButton::SIGNAL_RELEASED == signalName )
346 button.ReleasedSignal().Connect( tracker, functor );
350 // signalName does not match any signal
357 void PushButton::OnButtonInitialize()
359 // Push button requires the Leave event.
361 root.SetLeaveRequired( true );
364 void PushButton::OnButtonDown()
368 Toolkit::PushButton handle( GetOwner() );
370 // Notifies the painter the button has been pressed.
371 GetPushButtonPainter( mPainter )->Pressed( handle );
375 SetUpTimer( mInitialAutoRepeatingDelay );
379 mPressedSignalV2.Emit( handle );
383 void PushButton::OnButtonUp()
385 if( ButtonDown == mState )
389 mToggled = !mToggled;
391 Toolkit::PushButton handle( GetOwner() );
393 // Notifies the painter the button has been toggled.
394 GetPushButtonPainter( mPainter )->Toggled( handle );
397 mToggledSignalV2.Emit( handle, mToggled );
401 Toolkit::PushButton handle( GetOwner() );
403 // Notifies the painter the button has been clicked.
404 GetPushButtonPainter( mPainter )->Released( handle );
405 GetPushButtonPainter( mPainter )->Clicked( handle );
409 mAutoRepeatingTimer.Reset();
413 mReleasedSignalV2.Emit( handle );
414 mClickedSignalV2.Emit( handle );
419 void PushButton::OnTouchPointLeave()
421 if( ButtonDown == mState )
425 Toolkit::PushButton handle( GetOwner() );
427 // Notifies the painter the button has been released.
428 GetPushButtonPainter( mPainter )->Released( handle );
432 mAutoRepeatingTimer.Reset();
436 mReleasedSignalV2.Emit( handle );
441 void PushButton::OnTouchPointInterrupted()
446 void PushButton::OnAnimationTimeSet( float animationTime )
448 GetPushButtonPainter( mPainter )->SetAnimationTime( animationTime );
451 float PushButton::OnAnimationTimeRequested() const
453 return GetPushButtonPainter( mPainter )->GetAnimationTime();
456 PushButton::PushButton()
458 mAutoRepeating( false ),
459 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
460 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
461 mToggleButton( false ),
462 mAutoRepeatingTimer(),
464 mClickActionPerforming(false)
466 // Creates specific painter.
467 mPainter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
470 PushButton::~PushButton()
472 if( mAutoRepeatingTimer )
474 mAutoRepeatingTimer.Reset();
480 void PushButton::SetUpTimer( float delay )
482 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
483 mAutoRepeatingTimer.TickSignal().Connect( this, &PushButton::AutoRepeatingSlot );
484 mAutoRepeatingTimer.Start();
487 bool PushButton::AutoRepeatingSlot()
489 bool consumed = false;
492 // Restart the autorepeat timer.
493 SetUpTimer( mNextAutoRepeatingDelay );
495 Toolkit::PushButton handle( GetOwner() );
497 // Notifies the painter the button has been pressed.
498 GetPushButtonPainter( mPainter )->Pressed( handle );
501 consumed = mReleasedSignalV2.Emit( handle );
502 consumed |= mClickedSignalV2.Emit( handle );
503 consumed |= mPressedSignalV2.Emit( handle );
509 void PushButton::OnActivated()
511 // When the button is activated, it performs the click action
512 std::vector<Property::Value> attributes;
513 DoClickAction(attributes);
516 void PushButton::DoClickAction(const PropertyValueContainer& attributes)
518 // Prevents the button signals from doing a recursive loop by sending an action
519 // and re-emitting the signals.
520 if(!mClickActionPerforming)
522 mClickActionPerforming = true;
526 mClickActionPerforming = false;
530 bool PushButton::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
534 Dali::BaseHandle handle(object);
536 Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle);
538 DALI_ASSERT_ALWAYS(button);
540 if(Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK == actionName)
542 GetImplementation(button).DoClickAction(attributes);
549 } // namespace Internal
551 } // namespace Toolkit