2 * Copyright (c) 2019 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/public-api/images/resource-image.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
30 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
34 #if defined(DEBUG_ENABLED)
35 extern Debug::Filter* gLogButtonFilter;
52 return Toolkit::PushButton::New();
57 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create )
59 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "unselectedIcon", STRING, UNSELECTED_ICON )
60 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "selectedIcon", STRING, SELECTED_ICON )
61 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconAlignment", STRING, ICON_ALIGNMENT )
62 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING )
63 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING )
65 DALI_TYPE_REGISTRATION_END()
68 * Table to define Text-to-enum conversions for IconAlignment.
70 const Dali::Scripting::StringEnum IconAlignmentTable[] = {
71 { "LEFT", Toolkit::Internal::PushButton::LEFT },
72 { "RIGHT", Toolkit::Internal::PushButton::RIGHT },
73 { "TOP", Toolkit::Internal::PushButton::TOP },
74 { "BOTTOM", Toolkit::Internal::PushButton::BOTTOM },
75 }; const unsigned int IconAlignmentTableCount = sizeof( IconAlignmentTable ) / sizeof( IconAlignmentTable[0] );
77 } // unnamed namespace
82 } // unnamed namespace
84 Dali::Toolkit::PushButton PushButton::New()
86 // Create the implementation, temporarily owned on stack
87 IntrusivePtr< PushButton > internalPushButton = new PushButton();
89 // Pass ownership to CustomActor
90 Dali::Toolkit::PushButton pushButton( *internalPushButton );
92 // Second-phase init of the implementation
93 // This can only be done after the CustomActor connection has been made...
94 internalPushButton->Initialize();
99 PushButton::PushButton()
101 mIconAlignment( RIGHT )
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 PushButton::~PushButton()
113 void PushButton::OnInitialize()
115 Button::OnInitialize();
117 // Push button requires the Leave event.
119 self.SetLeaveRequired( true );
122 void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment )
124 mIconAlignment = iconAlignment;
125 Button::Align labelAlignment;
126 switch ( iconAlignment )
130 labelAlignment = Button::BEGIN;
135 labelAlignment = Button::BOTTOM;
140 labelAlignment = Button::TOP;
145 labelAlignment = Button::END;
149 Button::SetLabelAlignment( labelAlignment );
152 const PushButton::IconAlignment PushButton::GetIconAlignment() const
154 return mIconAlignment;
157 void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
159 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
163 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
165 // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
167 switch ( propertyIndex )
169 case Toolkit::PushButton::Property::UNSELECTED_ICON:
171 pushButtonImpl.CreateVisualsForComponent( Toolkit::Button::Property::UNSELECTED_VISUAL, value, DepthIndex::CONTENT );
174 case Toolkit::PushButton::Property::SELECTED_ICON:
176 pushButtonImpl.CreateVisualsForComponent( Toolkit::Button::Property::SELECTED_VISUAL, value, DepthIndex::CONTENT );
179 case Toolkit::PushButton::Property::ICON_ALIGNMENT:
181 IconAlignment iconAlignment;
182 if( Scripting::GetEnumeration< IconAlignment >( value.Get< std::string >().c_str(), IconAlignmentTable, IconAlignmentTableCount, iconAlignment ) )
184 pushButtonImpl.SetIconAlignment( iconAlignment );
188 case Toolkit::PushButton::Property::LABEL_PADDING:
190 Vector4 padding ( value.Get< Vector4 >() );
191 pushButtonImpl.Button::SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
194 case Toolkit::PushButton::Property::ICON_PADDING:
196 Vector4 padding ( value.Get< Vector4 >() );
197 pushButtonImpl.Button::SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
204 Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
206 Property::Value value;
208 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
212 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
214 switch ( propertyIndex )
216 case Toolkit::PushButton::Property::UNSELECTED_ICON:
218 //value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
221 case Toolkit::PushButton::Property::SELECTED_ICON:
223 //value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
226 case Toolkit::PushButton::Property::ICON_ALIGNMENT:
228 value = Scripting::GetLinearEnumerationName< IconAlignment >( pushButtonImpl.GetIconAlignment(), IconAlignmentTable, IconAlignmentTableCount );
231 case Toolkit::PushButton::Property::LABEL_PADDING:
233 Padding padding = pushButtonImpl.Button::GetLabelPadding();
234 value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
237 case Toolkit::PushButton::Property::ICON_PADDING:
239 Padding padding = pushButtonImpl.Button::GetForegroundPadding();
240 value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
249 Dali::Accessibility::States PushButton::AccessibleImpl::CalculateStates()
251 auto tmp = Button::AccessibleImpl::CalculateStates();
252 auto slf = Toolkit::Button::DownCast( self );
253 tmp[Dali::Accessibility::State::PRESSED] = slf.GetProperty<bool>( Toolkit::Button::Property::SELECTED );
257 void PushButton::OnStateChange( State newState )
259 // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
260 if (Dali::Accessibility::IsUp() && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
262 Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
263 Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0
268 } // namespace Internal
270 } // namespace Toolkit