[AT-SPI] Rename AccessibleImpl to <Control>Accessible
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
1 /*
2  * Copyright (c) 2014 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 "radio-button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 #if defined(DEBUG_ENABLED)
26 extern Debug::Filter* gLogButtonFilter;
27 #endif
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 namespace
36 {
37 BaseHandle Create()
38 {
39   return Toolkit::RadioButton::New();
40 }
41
42 TypeRegistration typeRegistration(typeid(Toolkit::RadioButton), typeid(Toolkit::Button), Create);
43
44 } // namespace
45
46 Dali::Toolkit::RadioButton RadioButton::New()
47 {
48   // Create the implementation, temporarily owned on stack
49   IntrusivePtr<RadioButton> internalRadioButton = new RadioButton();
50
51   // Pass ownership to CustomActor
52   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
53
54   // Second-phase init of the implementation
55   // This can only be done after the CustomActor connection has been made...
56   internalRadioButton->Initialize();
57
58   return radioButton;
59 }
60
61 RadioButton::RadioButton()
62 {
63   SetTogglableButton(true);
64 }
65
66 RadioButton::~RadioButton()
67 {
68 }
69
70 void RadioButton::OnInitialize()
71 {
72   Button::OnInitialize();
73
74   DevelControl::SetAccessibilityConstructor(Self(), [](Dali::Actor actor) {
75     return std::make_unique<RadioButtonAccessible>(actor, Dali::Accessibility::Role::RADIO_BUTTON);
76   });
77 }
78
79 bool RadioButton::OnToggleReleased()
80 {
81   // Radio button overrides toggle release (button up) as doesn't allow un-selection to be performed on it directly.
82   return false;
83 }
84
85 void RadioButton::OnStateChange(State newState)
86 {
87   // Radio button can be part of a group, if a button in the group is selected then all others should be unselected
88   DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "RadioButton::OnStateChange state(%d)\n", newState);
89
90   if(SELECTED_STATE == newState)
91   {
92     Actor parent = Self().GetParent();
93     if(parent)
94     {
95       for(unsigned int i = 0; i < parent.GetChildCount(); ++i)
96       {
97         Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
98         if(radioButtonChild && radioButtonChild != Self())
99         {
100           radioButtonChild.SetProperty(Toolkit::Button::Property::SELECTED, false);
101         }
102       }
103     }
104   }
105
106   // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
107   if(Dali::Accessibility::IsUp() && (Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor() == Self())
108      && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
109   {
110     Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(Dali::Accessibility::State::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0);
111   }
112 }
113
114 Dali::Accessibility::States RadioButton::RadioButtonAccessible::CalculateStates()
115 {
116   auto state = Button::ButtonAccessible::CalculateStates();
117   auto self = Toolkit::Button::DownCast(Self());
118
119   if(self.GetProperty<bool>(Toolkit::Button::Property::SELECTED))
120   {
121     state[Dali::Accessibility::State::CHECKED] = true;
122   }
123
124   state[Dali::Accessibility::State::SELECTABLE] = true;
125   return state;
126 }
127
128 } // namespace Internal
129
130 } // namespace Toolkit
131
132 } // namespace Dali