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