9ca694c111ed833c4715a10b566b0cb8d82b01bf
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-text.cpp
1 /*
2  * Copyright (c) 2021 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 <dali/internal/accessibility/bridge/bridge-text.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/input/common/input-method-context-factory.h>
23
24 using namespace Dali::Accessibility;
25
26 void BridgeText::RegisterInterfaces()
27 {
28   // The second arguments below are the names (or signatures) of DBus methods.
29   // Screen Reader will call the methods with the exact names as specified in the AT-SPI Text interface:
30   // https://gitlab.gnome.org/GNOME/at-spi2-core/-/blob/master/xml/Text.xml
31
32   DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceText};
33   AddFunctionToInterface(desc, "GetText", &BridgeText::GetText);
34   AddGetPropertyToInterface(desc, "CharacterCount", &BridgeText::GetCharacterCount);
35   AddGetPropertyToInterface(desc, "CaretOffset", &BridgeText::GetCursorOffset);
36   AddFunctionToInterface(desc, "SetCaretOffset", &BridgeText::SetCursorOffset);
37   AddFunctionToInterface(desc, "GetTextAtOffset", &BridgeText::GetTextAtOffset);
38   AddFunctionToInterface(desc, "GetSelection", &BridgeText::GetRangeOfSelection);
39   AddFunctionToInterface(desc, "SetSelection", &BridgeText::SetRangeOfSelection);
40   AddFunctionToInterface(desc, "RemoveSelection", &BridgeText::RemoveSelection);
41   dbusServer.addInterface("/", desc, true);
42 }
43
44 Text* BridgeText::FindSelf() const
45 {
46   auto self = BridgeBase::FindSelf();
47   assert(self);
48   auto textObject = dynamic_cast<Text*>(self);
49   if(!textObject)
50   {
51     throw std::domain_error{"object " + textObject->GetAddress().ToString() + " doesn't have Text interface"};
52   }
53   return textObject;
54 }
55
56 DBus::ValueOrError<std::string> BridgeText::GetText(int startOffset, int endOffset)
57 {
58   return FindSelf()->GetText(startOffset, endOffset);
59 }
60
61 DBus::ValueOrError<int32_t> BridgeText::GetCharacterCount()
62 {
63   return FindSelf()->GetCharacterCount();
64 }
65
66 DBus::ValueOrError<int32_t> BridgeText::GetCursorOffset()
67 {
68   return FindSelf()->GetCursorOffset();
69 }
70
71 DBus::ValueOrError<bool> BridgeText::SetCursorOffset(int32_t offset)
72 {
73   return FindSelf()->SetCursorOffset(offset);
74 }
75
76 DBus::ValueOrError<std::string, int, int> BridgeText::GetTextAtOffset(int32_t offset, uint32_t boundary)
77 {
78   auto range = FindSelf()->GetTextAtOffset(offset, static_cast<TextBoundary>(boundary));
79   return {range.content, static_cast<int>(range.startOffset), static_cast<int>(range.endOffset)};
80 }
81
82 DBus::ValueOrError<int, int> BridgeText::GetRangeOfSelection(int32_t selectionNum)
83 {
84   auto range = FindSelf()->GetRangeOfSelection(selectionNum);
85   return {static_cast<int>(range.startOffset), static_cast<int>(range.endOffset)};
86 }
87
88 DBus::ValueOrError<bool> BridgeText::RemoveSelection(int32_t selectionNum)
89 {
90   return FindSelf()->RemoveSelection(selectionNum);
91 }
92
93 DBus::ValueOrError<bool> BridgeText::SetRangeOfSelection(int32_t selectionNum, int32_t startOffset, int32_t endOffset)
94 {
95   return FindSelf()->SetRangeOfSelection(selectionNum, startOffset, endOffset);
96 }