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