345ce066b0981216ec02cdbf133c0685a1bd363b
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.cpp
1 /*
2  * Copyright (c) 2020 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 "push-button-impl.h"
20
21 // EXTERNAL INCLUDES
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>
26
27 // INTERNAL INCLUDES
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>
32
33 #if defined(DEBUG_ENABLED)
34   extern Debug::Filter* gLogButtonFilter;
35 #endif
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 namespace
47 {
48
49 BaseHandle Create()
50 {
51   return Toolkit::PushButton::New();
52 }
53
54 // Properties
55
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create )
57
58 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding",  STRING, LABEL_PADDING )
59 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding",  STRING, ICON_PADDING )
60
61 DALI_TYPE_REGISTRATION_END()
62
63 } // unnamed namespace
64
65 namespace
66 {
67
68 } // unnamed namespace
69
70 Dali::Toolkit::PushButton PushButton::New()
71 {
72   // Create the implementation, temporarily owned on stack
73   IntrusivePtr< PushButton > internalPushButton = new PushButton();
74
75   // Pass ownership to CustomActor
76   Dali::Toolkit::PushButton pushButton( *internalPushButton );
77
78   // Second-phase init of the implementation
79   // This can only be done after the CustomActor connection has been made...
80   internalPushButton->Initialize();
81
82   return pushButton;
83 }
84
85 PushButton::PushButton()
86 : Button(),
87   mIconAlignment( RIGHT )
88 {
89   DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
90     return std::unique_ptr< Dali::Accessibility::Accessible >(
91         new AccessibleImpl( actor, Dali::Accessibility::Role::PUSH_BUTTON ) );
92   } );
93 }
94
95 PushButton::~PushButton()
96 {
97 }
98
99 void PushButton::OnInitialize()
100 {
101   Button::OnInitialize();
102
103   // Push button requires the Leave event.
104   Actor self = Self();
105   self.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
106 }
107
108 void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment )
109 {
110   mIconAlignment = iconAlignment;
111   Button::Align labelAlignment;
112   switch ( iconAlignment )
113   {
114   case RIGHT:
115   {
116     labelAlignment = Button::BEGIN;
117     break;
118   }
119   case TOP:
120   {
121     labelAlignment = Button::BOTTOM;
122     break;
123   }
124   case BOTTOM:
125   {
126     labelAlignment = Button::TOP;
127     break;
128   }
129   case LEFT:
130   default:
131     labelAlignment = Button::END;
132     break;
133   }
134
135   Button::SetLabelAlignment( labelAlignment );
136 }
137
138 const PushButton::IconAlignment PushButton::GetIconAlignment() const
139 {
140   return mIconAlignment;
141 }
142
143 void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
144 {
145   Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
146
147   if ( pushButton )
148   {
149     PushButton& pushButtonImpl( GetImplementation( pushButton ) );
150
151     // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
152
153     switch ( propertyIndex )
154     {
155       case Toolkit::PushButton::Property::LABEL_PADDING:
156       {
157         Vector4 padding ( value.Get< Vector4 >() );
158         pushButtonImpl.Button::SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
159         break;
160       }
161       case Toolkit::PushButton::Property::ICON_PADDING:
162       {
163         Vector4 padding ( value.Get< Vector4 >() );
164         pushButtonImpl.Button::SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
165         break;
166       }
167     }
168   }
169 }
170
171 Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
172 {
173   Property::Value value;
174
175   Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
176
177   if ( pushButton )
178   {
179     PushButton& pushButtonImpl( GetImplementation( pushButton ) );
180
181     switch ( propertyIndex )
182     {
183       case Toolkit::PushButton::Property::LABEL_PADDING:
184       {
185         Padding padding = pushButtonImpl.Button::GetLabelPadding();
186         value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
187         break;
188       }
189       case Toolkit::PushButton::Property::ICON_PADDING:
190       {
191         Padding padding = pushButtonImpl.Button::GetForegroundPadding();
192         value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
193         break;
194       }
195     }
196   }
197
198   return value;
199 }
200
201 Dali::Accessibility::States PushButton::AccessibleImpl::CalculateStates()
202 {
203   auto tmp = Button::AccessibleImpl::CalculateStates();
204   auto slf = Toolkit::Button::DownCast( self );
205   tmp[Dali::Accessibility::State::PRESSED] = slf.GetProperty<bool>( Toolkit::Button::Property::SELECTED );
206   return tmp;
207 }
208
209 void PushButton::OnStateChange( State newState )
210 {
211   // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
212   if (Dali::Accessibility::IsUp() && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
213   {
214     Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
215       Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0
216     );
217   }
218 }
219
220 } // namespace Internal
221
222 } // namespace Toolkit
223
224 } // namespace Dali