2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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"
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
30 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #if defined(DEBUG_ENABLED)
34 extern Debug::Filter* gLogButtonFilter;
51 return Toolkit::PushButton::New();
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create )
58 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING )
59 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING )
61 DALI_TYPE_REGISTRATION_END()
63 } // unnamed namespace
68 } // unnamed namespace
70 Dali::Toolkit::PushButton PushButton::New()
72 // Create the implementation, temporarily owned on stack
73 IntrusivePtr< PushButton > internalPushButton = new PushButton();
75 // Pass ownership to CustomActor
76 Dali::Toolkit::PushButton pushButton( *internalPushButton );
78 // Second-phase init of the implementation
79 // This can only be done after the CustomActor connection has been made...
80 internalPushButton->Initialize();
85 PushButton::PushButton()
87 mIconAlignment( RIGHT )
91 PushButton::~PushButton()
95 void PushButton::OnInitialize()
97 Button::OnInitialize();
99 // Push button requires the Leave event.
101 self.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
103 DevelControl::SetAccessibilityConstructor( self, []( Dali::Actor actor ) {
104 return std::unique_ptr< Dali::Accessibility::Accessible >(
105 new AccessibleImpl( actor, Dali::Accessibility::Role::PUSH_BUTTON ) );
109 void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment )
111 mIconAlignment = iconAlignment;
112 Button::Align labelAlignment;
113 switch ( iconAlignment )
117 labelAlignment = Button::BEGIN;
122 labelAlignment = Button::BOTTOM;
127 labelAlignment = Button::TOP;
132 labelAlignment = Button::END;
136 Button::SetLabelAlignment( labelAlignment );
139 const PushButton::IconAlignment PushButton::GetIconAlignment() const
141 return mIconAlignment;
144 void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
146 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
150 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
152 // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
154 switch ( propertyIndex )
156 case Toolkit::PushButton::Property::LABEL_PADDING:
158 Vector4 padding ( value.Get< Vector4 >() );
159 pushButtonImpl.Button::SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
162 case Toolkit::PushButton::Property::ICON_PADDING:
164 Vector4 padding ( value.Get< Vector4 >() );
165 pushButtonImpl.Button::SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
172 Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
174 Property::Value value;
176 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
180 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
182 switch ( propertyIndex )
184 case Toolkit::PushButton::Property::LABEL_PADDING:
186 Padding padding = pushButtonImpl.Button::GetLabelPadding();
187 value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
190 case Toolkit::PushButton::Property::ICON_PADDING:
192 Padding padding = pushButtonImpl.Button::GetForegroundPadding();
193 value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
202 Dali::Accessibility::States PushButton::AccessibleImpl::CalculateStates()
204 auto tmp = Button::AccessibleImpl::CalculateStates();
205 auto slf = Toolkit::Button::DownCast( self );
206 tmp[Dali::Accessibility::State::PRESSED] = slf.GetProperty<bool>( Toolkit::Button::Property::SELECTED );
210 void PushButton::OnStateChange( State newState )
212 // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
213 if (Dali::Accessibility::IsUp() && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
215 Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
216 Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0
219 if (Self().GetProperty<bool>(Toolkit::Button::Property::TOGGLABLE))
221 Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
222 Dali::Accessibility::State::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0
228 } // namespace Internal
230 } // namespace Toolkit