[AT-SPI] Introduce TextControlAccessible
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/atspi-interfaces/hypertext.h>
23 #include <dali/devel-api/atspi-interfaces/text.h>
24 #include <dali/public-api/object/property-map.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
28 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
29 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
30 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
31 #include <dali-toolkit/internal/text/text-control-interface.h>
32 #include <dali-toolkit/internal/text/text-controller.h>
33 #include <dali-toolkit/internal/text/text-scroller-interface.h>
34 #include <dali-toolkit/internal/text/text-scroller.h>
35 #include <dali-toolkit/internal/visuals/text/text-visual.h>
36 #include <dali-toolkit/public-api/controls/control-impl.h>
37 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Internal
44 {
45 /**
46  * @brief A control which renders a short text string.
47  */
48 class TextLabel : public Control, public Text::ControlInterface, public Text::ScrollerInterface, public Text::AnchorControlInterface
49 {
50 public:
51   /**
52    * @copydoc Dali::Toollkit::TextLabel::New()
53    * @param[in] additionalBehaviour custom behavior flags for this TextLabel. Default is CONTROL_BEHAVIOUR_DEFAULT
54    */
55   static Toolkit::TextLabel New(ControlBehaviour additionalBehaviour = ControlBehaviour::CONTROL_BEHAVIOUR_DEFAULT);
56
57   // Properties
58
59   /**
60    * @brief Called when a property of an object of this type is set.
61    *
62    * @param[in] object The object whose property is set.
63    * @param[in] index The property index.
64    * @param[in] value The new property value.
65    */
66   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
67
68   /**
69    * @brief Called to retrieve a property of an object of this type.
70    *
71    * @param[in] object The object whose property is to be retrieved.
72    * @param[in] index The property index.
73    * @return The current value of the property.
74    */
75   static Property::Value GetProperty(BaseObject* object, Property::Index index);
76
77   /**
78    * @copydoc Dali::Toollkit::TextLabel::AnchorClickedSignal()
79    */
80   DevelTextLabel::AnchorClickedSignalType& AnchorClickedSignal();
81
82   /**
83    * @copydoc Dali::Toollkit::TextLabel::TextFitChangedSignal()
84    */
85   DevelTextLabel::TextFitChangedSignalType& TextFitChangedSignal();
86
87   /**
88    * Connects a callback function with the object's signals.
89    * @param[in] object The object providing the signal.
90    * @param[in] tracker Used to disconnect the signal.
91    * @param[in] signalName The signal to connect to.
92    * @param[in] functor A newly allocated FunctorDelegate.
93    * @return True if the signal was connected.
94    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
95    */
96   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
97
98   /**
99    * @brief Gets text controller
100    *
101    * @return The text controller
102    */
103   Text::ControllerPtr GetTextController();
104
105   /**
106    * @brief Get the rendered size of a specific text range.
107    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
108    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
109    *
110    * @param[in] startIndex start index of the text requested to calculate size for.
111    * @param[in] endIndex end index(included) of the text requested to calculate size for.
112    * @return list of sizes of the reuested text.
113    */
114   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
115
116   /**
117    * @brief Get the top/left rendered position of a specific text range.
118    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
119    * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
120    *
121    * @param[in] startIndex start index of the text requested to get position to.
122    * @param[in] endIndex end index(included) of the text requested to get position to.
123    * @return list of positions of the requested text.
124    */
125   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
126
127 private: // From Control
128   /**
129    * @copydoc Control::OnInitialize()
130    */
131   void OnInitialize() override;
132
133   /**
134    * @copydoc Control::OnStyleChange()
135    */
136   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
137
138   /**
139    * @copydoc Control::OnRelayout()
140    */
141   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
142
143   /**
144    * @copydoc Control::OnTap()
145    */
146   void OnTap(const TapGesture& tap) override;
147
148   /**
149    * @copydoc Control::GetNaturalSize()
150    */
151   Vector3 GetNaturalSize() override;
152
153   /**
154    * @copydoc Control::GetHeightForWidth()
155    */
156   float GetHeightForWidth(float width) override;
157
158   /**
159    * @copydoc Control::OnPropertySet()
160    */
161   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
162
163   // From ControlInterface
164
165   /**
166    * @copydoc Text::ControlInterface::RequestTextRelayout()
167    */
168   void RequestTextRelayout() override;
169
170 private: // from TextScroller
171   /**
172    * @copydoc Text::ScrollerInterface::ScrollingFinished()
173    */
174   void ScrollingFinished() override;
175
176 public: // From AnchorControlInterface
177   /**
178    * @copydoc Text::AnchorControlInterface::AnchorClicked()
179    */
180   void AnchorClicked(const std::string& href) override;
181
182 private: // Implementation
183   /**
184    * Construct a new TextLabel.
185    *
186    * @param[in] additionalBehaviour additional behaviour flags for this TextLabel
187    */
188   TextLabel(ControlBehaviour additionalBehaviour);
189
190   /**
191    * A reference counted object may only be deleted by calling Unreference()
192    */
193   virtual ~TextLabel();
194
195 private:
196   // Undefined copy constructor and assignment operators
197   TextLabel(const TextLabel&);
198   TextLabel& operator=(const TextLabel& rhs);
199
200   /**
201    * @brief Set up Autoscrolling
202    */
203   void SetUpAutoScrolling();
204
205   /**
206    * Creates a text-scroller if one has not been created.
207    * @return The text scroller.
208    */
209   Text::TextScrollerPtr GetTextScroller()
210   {
211     if(!mTextScroller)
212     {
213       mTextScroller = Text::TextScroller::New(*this);
214     }
215     return mTextScroller;
216   }
217
218   /**
219    * @brief Callback function for when the layout is changed.
220    * @param[in] actor The actor whose layoutDirection is changed.
221    * @param[in] type  The layoutDirection.
222    */
223   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
224
225   /**
226    * @brief Emits TextFitChanged signal.
227    */
228   void EmitTextFitChangedSignal();
229   void OnAccessibilityStatusChanged();
230
231 private: // Data
232   Text::ControllerPtr   mController;
233   Text::TextScrollerPtr mTextScroller;
234
235   Toolkit::Visual::Base mVisual;
236
237   std::vector<Toolkit::TextAnchor> mAnchorActors;
238
239   // Signals
240   Toolkit::DevelTextLabel::AnchorClickedSignalType  mAnchorClickedSignal;
241   Toolkit::DevelTextLabel::TextFitChangedSignalType mTextFitChangedSignal;
242
243   int  mRenderingBackend;
244   bool mTextUpdateNeeded : 1;
245
246 protected:
247   /**
248    * @brief This structure is to connect TextLabel with Accessible functions.
249    */
250   class TextLabelAccessible : public TextControlAccessible
251   {
252   public:
253     using TextControlAccessible::TextControlAccessible;
254
255     /**
256      * @copydoc Dali::Accessibility::Text::GetNameRaw()
257      */
258     std::string GetNameRaw() const override;
259
260     /**
261      * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
262      */
263     Property::Index GetNamePropertyIndex() override;
264
265   protected:
266     /**
267      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
268      */
269     const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
270
271     /**
272      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
273      */
274     Toolkit::Text::ControllerPtr GetTextController() const override;
275   };
276 };
277
278 } // namespace Internal
279
280 // Helpers for public-api forwarding methods
281
282 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
283 {
284   DALI_ASSERT_ALWAYS(textLabel);
285
286   Dali::RefObject& handle = textLabel.GetImplementation();
287
288   return static_cast<Toolkit::Internal::TextLabel&>(handle);
289 }
290
291 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
292 {
293   DALI_ASSERT_ALWAYS(textLabel);
294
295   const Dali::RefObject& handle = textLabel.GetImplementation();
296
297   return static_cast<const Toolkit::Internal::TextLabel&>(handle);
298 }
299
300 } // namespace Toolkit
301
302 } // namespace Dali
303
304 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H