2 * Copyright (c) 2021 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/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>
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>
33 #if defined(DEBUG_ENABLED)
34 extern Debug::Filter* gLogButtonFilter;
47 return Toolkit::PushButton::New();
52 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::PushButton, Toolkit::Button, Create)
54 DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING)
55 DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING)
57 DALI_TYPE_REGISTRATION_END()
59 } // unnamed namespace
63 } // unnamed namespace
65 Dali::Toolkit::PushButton PushButton::New()
67 // Create the implementation, temporarily owned on stack
68 IntrusivePtr<PushButton> internalPushButton = new PushButton();
70 // Pass ownership to CustomActor
71 Dali::Toolkit::PushButton pushButton(*internalPushButton);
73 // Second-phase init of the implementation
74 // This can only be done after the CustomActor connection has been made...
75 internalPushButton->Initialize();
80 PushButton::PushButton()
86 PushButton::~PushButton()
90 void PushButton::OnInitialize()
92 Button::OnInitialize();
94 // Push button requires the Leave event.
96 self.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
98 self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::PUSH_BUTTON);
101 DevelControl::ControlAccessible* PushButton::CreateAccessibleObject()
103 return new PushButtonAccessible(Self());
106 void PushButton::SetIconAlignment(const PushButton::IconAlignment iconAlignment)
108 mIconAlignment = iconAlignment;
109 Button::Align labelAlignment;
110 switch(iconAlignment)
114 labelAlignment = Button::BEGIN;
119 labelAlignment = Button::BOTTOM;
124 labelAlignment = Button::TOP;
129 labelAlignment = Button::END;
133 Button::SetLabelAlignment(labelAlignment);
136 const PushButton::IconAlignment PushButton::GetIconAlignment() const
138 return mIconAlignment;
141 void PushButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
143 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object));
147 PushButton& pushButtonImpl(GetImplementation(pushButton));
149 // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
151 switch(propertyIndex)
153 case Toolkit::PushButton::Property::LABEL_PADDING:
155 Vector4 padding(value.Get<Vector4>());
156 pushButtonImpl.Button::SetLabelPadding(Padding(padding.x, padding.y, padding.z, padding.w));
159 case Toolkit::PushButton::Property::ICON_PADDING:
161 Vector4 padding(value.Get<Vector4>());
162 pushButtonImpl.Button::SetForegroundPadding(Padding(padding.x, padding.y, padding.z, padding.w));
169 Property::Value PushButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
171 Property::Value value;
173 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object));
177 PushButton& pushButtonImpl(GetImplementation(pushButton));
179 switch(propertyIndex)
181 case Toolkit::PushButton::Property::LABEL_PADDING:
183 Padding padding = pushButtonImpl.Button::GetLabelPadding();
184 value = Vector4(padding.x, padding.y, padding.top, padding.bottom);
187 case Toolkit::PushButton::Property::ICON_PADDING:
189 Padding padding = pushButtonImpl.Button::GetForegroundPadding();
190 value = Vector4(padding.x, padding.y, padding.top, padding.bottom);
199 Dali::Accessibility::States PushButton::PushButtonAccessible::CalculateStates()
201 auto state = Button::ButtonAccessible::CalculateStates();
202 auto self = Toolkit::Button::DownCast(Self());
203 state[Dali::Accessibility::State::PRESSED] = self.GetProperty<bool>(Toolkit::Button::Property::SELECTED);
207 void PushButton::OnStateChange(State newState)
209 // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
210 if((Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor() == Self()) && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
212 auto* accessible = GetAccessibleObject();
214 accessible->EmitStateChanged(Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0);
216 if(Self().GetProperty<bool>(Toolkit::Button::Property::TOGGLABLE))
218 accessible->EmitStateChanged(Dali::Accessibility::State::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0);
223 } // namespace Internal
225 } // namespace Toolkit