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