c30116df16e36968a9c3e7391b39dc99e93e9046
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / accessible.cpp
1 /*
2  * Copyright (c) 2019 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
20 //INTERNAL INCLUDES
21 #include <dali/internal/accessibility/bridge/accessibility-common.h>
22 #include <third-party/libunibreak/wordbreak.h>
23 #include <third-party/libunibreak/linebreak.h>
24
25 using namespace Dali::Accessibility;
26
27 std::vector< std::string > Accessible::GetInterfaces()
28 {
29   std::vector< std::string > tmp;
30   tmp.push_back( AtspiDbusInterfaceAccessible );
31   if( dynamic_cast< Collection* >( this ) )
32     tmp.push_back( AtspiDbusInterfaceCollection );
33   if( dynamic_cast< Text* >( this ) )
34     tmp.push_back( AtspiDbusInterfaceText );
35   if( dynamic_cast< Value* >( this ) )
36     tmp.push_back( AtspiDbusInterfaceValue );
37   if( dynamic_cast< Component* >( this ) )
38     tmp.push_back( AtspiDbusInterfaceComponent );
39   if( auto d = dynamic_cast< Action* >( this ) )
40   {
41     if( d->GetActionCount() > 0 )
42       tmp.push_back( AtspiDbusInterfaceAction );
43   }
44   return tmp;
45 }
46
47 Accessible::Accessible()
48 {
49 }
50
51 Accessible::~Accessible()
52 {
53   auto b = bridgeData.lock();
54   if( b )
55     b->knownObjects.erase( this );
56 }
57
58 void Accessible::EmitActiveDescendantChanged( Accessible* obj, Accessible *child )
59 {
60   if( auto b = GetBridgeData() )
61   {
62     b->bridge->EmitActiveDescendantChanged( obj, child );
63   }
64 }
65
66 void Accessible::EmitStateChanged( State state, int newValue1, int newValue2 )
67 {
68   if( auto b = GetBridgeData() )
69   {
70     b->bridge->EmitStateChanged( this, state, newValue1, newValue2 );
71   }
72 }
73
74 void Accessible::EmitShowing( bool showing )
75 {
76   if( auto b = GetBridgeData() )
77   {
78     b->bridge->EmitStateChanged( this, State::SHOWING, showing ? 1 : 0, 0 );
79   }
80 }
81
82 void Accessible::EmitVisible( bool visible )
83 {
84   if( auto b = GetBridgeData() )
85   {
86     b->bridge->EmitStateChanged( this, State::VISIBLE, visible ? 1 : 0, 0 );
87   }
88 }
89
90 void Accessible::EmitHighlighted( bool set )
91 {
92   if( auto b = GetBridgeData() )
93   {
94     b->bridge->EmitStateChanged( this, State::HIGHLIGHTED, set ? 1 : 0, 0 );
95   }
96 }
97
98 void Accessible::EmitFocused( bool set )
99 {
100   if ( auto b = GetBridgeData() ) {
101     b->bridge->EmitStateChanged( this, State::FOCUSED, set ? 1 : 0, 0 );
102   }
103 }
104 void Accessible::EmitTextInserted( unsigned int position, unsigned int length, const std::string &content )
105 {
106   if ( auto b = GetBridgeData() ) {
107     b->bridge->EmitTextChanged( this, TextChangedState::INSERT, position, length, content );
108   }
109 }
110 void Accessible::EmitTextDeleted( unsigned int position, unsigned int length, const std::string &content )
111 {
112   if ( auto b = GetBridgeData() ) {
113     b->bridge->EmitTextChanged( this, TextChangedState::DELETE, position, length, content );
114   }
115 }
116 void Accessible::EmitTextCaretMoved( unsigned int cursorPosition )
117 {
118   if ( auto b = GetBridgeData() ) {
119     b->bridge->EmitCaretMoved( this, cursorPosition );
120   }
121 }
122 void Accessible::Emit( WindowEvent we, unsigned int detail1 )
123 {
124   if( auto b = GetBridgeData() )
125   {
126     b->bridge->Emit( this, we, detail1 );
127   }
128 }
129 void Accessible::Emit( ObjectPropertyChangeEvent ev )
130 {
131   if( auto b = GetBridgeData() )
132   {
133     b->bridge->Emit( this, ev );
134   }
135 }
136
137 void Accessible::EmitBoundsChanged( Rect<> rect )
138 {
139   if( auto b = GetBridgeData() )
140   {
141     b->bridge->EmitBoundsChanged( this, rect );
142   }
143 }
144
145 std::vector< Accessible* > Accessible::GetChildren()
146 {
147   std::vector< Accessible* > tmp( GetChildCount() );
148   for( auto i = 0u; i < tmp.size(); ++i )
149   {
150     tmp[i] = GetChildAtIndex( i );
151   }
152   return tmp;
153 }
154
155 std::shared_ptr< Bridge::Data > Accessible::GetBridgeData()
156 {
157   auto b = bridgeData.lock();
158   if( !b )
159   {
160     auto p = Bridge::GetCurrentBridge();
161     b = p->data;
162   }
163   return b;
164 }
165
166 Address Accessible::GetAddress()
167 {
168   auto b = bridgeData.lock();
169   if( !b )
170   {
171     b = GetBridgeData();
172     if ( b )
173       b->bridge->RegisterOnBridge( this );
174   }
175   std::ostringstream tmp;
176   tmp << this;
177   return {b ? b->busName : "", tmp.str() };
178 }
179
180 void Bridge::RegisterOnBridge( Accessible* obj )
181 {
182   assert( !obj->bridgeData.lock() || obj->bridgeData.lock() == data );
183   if( !obj->bridgeData.lock() )
184   {
185     assert( data );
186     data->knownObjects.insert( obj );
187     obj->bridgeData = data;
188   }
189 }
190
191 bool Accessible::IsProxy()
192 {
193   return false;
194 }
195
196 Accessible* Accessible::GetDefaultLabel()
197 {
198         return this;
199 }
200
201 void Accessible::NotifyAccessibilityStateChange( Dali::Accessibility::States states, bool doRecursive )
202 {
203   if( auto b = GetBridgeData() )
204   {
205     auto s = GetStates() & states;
206     for( auto i = 0u; i < s.size(); i++ )
207     {
208       auto index = static_cast< Dali::Accessibility::State >( i );
209       if( s[index] )
210         b->bridge->EmitStateChanged( this, index, 1, 0 );
211     }
212     if( doRecursive )
213     {
214       auto children = GetChildren();
215       for( auto c : children )
216         c->NotifyAccessibilityStateChange( states, doRecursive );
217     }
218   }
219 }
220
221 void Accessible::FindWordSeparationsUtf8( const utf8_t *s, size_t length, const char *language, char *breaks )
222 {
223   set_wordbreaks_utf8( s, length, language, breaks );
224 }
225
226 void Accessible::FindLineSeparationsUtf8( const utf8_t *s, size_t length, const char *language, char *breaks )
227 {
228   set_linebreaks_utf8( s, length, language, breaks );
229 }