Introduce REMOVE_FRONT_INSET, REMOVE_BACK_INSET Property
[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/actors/actor-devel.h>
23 #include <dali/devel-api/atspi-interfaces/hypertext.h>
24 #include <dali/devel-api/atspi-interfaces/text.h>
25 #include <dali/public-api/object/property-map.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/text/spanned.h>
29 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
31 #include <dali-toolkit/internal/text/controller/text-controller.h>
32 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
33 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
34 #include <dali-toolkit/internal/text/text-control-interface.h>
35 #include <dali-toolkit/internal/text/text-scroller-interface.h>
36 #include <dali-toolkit/internal/text/text-scroller.h>
37 #include <dali-toolkit/internal/visuals/text/text-visual.h>
38 #include <dali-toolkit/public-api/controls/control-impl.h>
39 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
40 namespace Dali
41 {
42 namespace Toolkit
43 {
44 namespace Internal
45 {
46 /**
47  * @brief A control which renders a short text string.
48  */
49 class TextLabel : public Control, public Text::ControlInterface, public Text::ScrollerInterface, public Text::AnchorControlInterface
50 {
51 public:
52   /**
53    * @copydoc Dali::Toollkit::TextLabel::New()
54    * @param[in] additionalBehaviour custom behavior flags for this TextLabel. Default is CONTROL_BEHAVIOUR_DEFAULT
55    */
56   static Toolkit::TextLabel New(ControlBehaviour additionalBehaviour = ControlBehaviour::CONTROL_BEHAVIOUR_DEFAULT);
57
58   // Properties
59
60   /**
61    * @brief Called when a property of an object of this type is set.
62    *
63    * @param[in] object The object whose property is set.
64    * @param[in] index The property index.
65    * @param[in] value The new property value.
66    */
67   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
68
69   /**
70    * @brief Called to retrieve a property of an object of this type.
71    *
72    * @param[in] object The object whose property is to be retrieved.
73    * @param[in] index The property index.
74    * @return The current value of the property.
75    */
76   static Property::Value GetProperty(BaseObject* object, Property::Index index);
77
78   /**
79    * @copydoc Dali::Toollkit::TextLabel::AnchorClickedSignal()
80    */
81   DevelTextLabel::AnchorClickedSignalType& AnchorClickedSignal();
82
83   /**
84    * @copydoc Dali::Toollkit::TextLabel::TextFitChangedSignal()
85    */
86   DevelTextLabel::TextFitChangedSignalType& TextFitChangedSignal();
87
88   /**
89    * Connects a callback function with the object's signals.
90    * @param[in] object The object providing the signal.
91    * @param[in] tracker Used to disconnect the signal.
92    * @param[in] signalName The signal to connect to.
93    * @param[in] functor A newly allocated FunctorDelegate.
94    * @return True if the signal was connected.
95    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
96    */
97   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
98
99   /**
100    * @brief Gets text controller
101    *
102    * @return The text controller
103    */
104   Text::ControllerPtr GetTextController();
105
106   /**
107    * @brief Get the rendered size of a specific text range.
108    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
109    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
110    *
111    * @param[in] startIndex start index of the text requested to calculate size for.
112    * @param[in] endIndex end index(included) of the text requested to calculate size for.
113    * @return list of sizes of the reuested text.
114    */
115   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
116
117   /**
118    * @brief Get the top/left rendered position of a specific text range.
119    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
120    * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
121    *
122    * @param[in] startIndex start index of the text requested to get position to.
123    * @param[in] endIndex end index(included) of the text requested to get position to.
124    * @return list of positions of the requested text.
125    */
126   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
127
128   /**
129    * @brief Get the line bounding rectangle.
130    * if the requested index is out of range or the line is not yet rendered, a rect of {0, 0, 0, 0} is returned.
131    *
132    * @param[in] lineIndex line index to which we want to calculate the geometry for.
133    * @return bounding rectangle.
134    */
135   Rect<float> GetLineBoundingRectangle(const uint32_t lineIndex) const;
136
137   /**
138    * @brief Get the character bounding rectangle.
139    * If the text is not yet rendered or the index > text.Count(); a rect of {0, 0, 0, 0} is returned.
140    *
141    * @param[in] charIndex character index to which we want to calculate the geometry for.
142    * @return bounding rectangle.
143    */
144   Rect<float> GetCharacterBoundingRectangle(const uint32_t charIndex) const;
145
146   /**
147    * @brief Get the character index.
148    * If the text is not yet rendered or the textis empty, -1 is returned.
149    *
150    * @param[in] visualX visual x position.
151    * @param[in] visualY visual y position.
152    * @return character index.
153    */
154   int GetCharacterIndexAtPosition(float visualX, float visualY) const;
155
156   /**
157    * @brief Gets the bounding box of a specific text range.
158    *
159    * @param[in] startIndex start index of the text requested to get bounding box to.
160    * @param[in] endIndex end index(included) of the text requested to get bounding box to.
161    * @return bounding box of the requested text.
162    */
163   Rect<> GetTextBoundingRectangle(uint32_t startIndex, uint32_t endIndex) const;
164
165   /**
166    * @brief Set the @p spannedText into current textLabel
167    * the spanned text contains content (text) and  format (spans with ranges)
168    * the text is copied into text-controller and the spans are applied on ranges
169    *
170    * @param[in] spannedText the text with spans.
171    */
172   void SetSpannedText(const Text::Spanned& spannedText);
173
174   /**
175    * @brief Set text fit array to text label.
176    *
177    * @param[in] enable Whether the text fit array is enabled or not.
178    * @param[in] fitOptions list of the fit options.
179    */
180   void SetTextFitArray(const bool enable, std::vector<Toolkit::DevelTextLabel::FitOption>& fitOptions);
181
182   /**
183    * @brief Get the text fit array of text label.
184    *
185    * @return list of the fit options.
186    */
187   std::vector<Toolkit::DevelTextLabel::FitOption>& GetTextFitArray();
188
189   /**
190    * @brief Whether the text fit array is enabled or not.
191    *
192    * @return True if the text fit array is enabled.
193    */
194   bool IsTextFitArrayEnabled() const;
195
196   /**
197    * @brief Gets the locale.
198    */
199   std::string GetLocale();
200
201   /**
202    * @brief Set removing front inset to text label.
203    *
204    * @param[in] remove Whether front inset of text label has to be removed or not.
205    */
206   void SetRemoveFrontInset(const bool remove);
207
208   /**
209    * @brief Whether front inset of text label is removed or not.
210    *
211    * @return True if the front inset of text label is removed.
212    */
213   bool IsRemoveFrontInset() const;
214
215   /**
216    * @brief Set removing back inset to text label.
217    *
218    * @param[in] remove Whether back inset of text label has to be removed or not.
219    */
220   void SetRemoveBackInset(const bool remove);
221
222   /**
223    * @brief Whether back inset of text label is removed or not.
224    *
225    * @return True if the back inset of text label is removed.
226    */
227   bool IsRemoveBackInset() const;
228
229 private: // From Control
230   /**
231    * @copydoc Control::OnInitialize()
232    */
233   void OnInitialize() override;
234
235   /**
236    * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
237    */
238   DevelControl::ControlAccessible* CreateAccessibleObject() override;
239
240   /**
241    * @copydoc Control::OnStyleChange()
242    */
243   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
244
245   /**
246    * @copydoc Control::OnRelayout()
247    */
248   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
249
250   /**
251    * @copydoc Control::OnTap()
252    */
253   void OnTap(const TapGesture& tap) override;
254
255   /**
256    * @copydoc Control::GetNaturalSize()
257    */
258   Vector3 GetNaturalSize() override;
259
260   /**
261    * @copydoc Control::GetHeightForWidth()
262    */
263   float GetHeightForWidth(float width) override;
264
265   /**
266    * @copydoc Control::OnPropertySet()
267    */
268   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
269
270   /**
271    * @copydoc Control::OnSceneConnection()
272    */
273   void OnSceneConnection(int depth) override;
274
275   /**
276    * @copydoc Control::OnSceneDisconnection()
277    */
278   void OnSceneDisconnection() override;
279
280   // From ControlInterface
281
282   /**
283    * @copydoc Text::ControlInterface::RequestTextRelayout()
284    */
285   void RequestTextRelayout() override;
286
287 private: // from TextScroller
288   /**
289    * @copydoc Text::ScrollerInterface::ScrollingFinished()
290    */
291   void ScrollingFinished() override;
292
293 public: // From AnchorControlInterface
294   /**
295    * @copydoc Text::AnchorControlInterface::AnchorClicked()
296    */
297   void AnchorClicked(const std::string& href) override;
298
299 private: // Implementation
300   /**
301    * Construct a new TextLabel.
302    *
303    * @param[in] additionalBehaviour additional behaviour flags for this TextLabel
304    */
305   TextLabel(ControlBehaviour additionalBehaviour);
306
307   /**
308    * A reference counted object may only be deleted by calling Unreference()
309    */
310   virtual ~TextLabel();
311
312 private:
313   // Undefined copy constructor and assignment operators
314   TextLabel(const TextLabel&);
315   TextLabel& operator=(const TextLabel& rhs);
316
317   /**
318    * @brief Set up Autoscrolling
319    */
320   void SetUpAutoScrolling();
321
322   /**
323    * Creates a text-scroller if one has not been created.
324    * @return The text scroller.
325    */
326   Text::TextScrollerPtr GetTextScroller()
327   {
328     if(!mTextScroller)
329     {
330       mTextScroller = Text::TextScroller::New(*this);
331     }
332     return mTextScroller;
333   }
334
335   /**
336    * @brief Callback function for when the layout is changed.
337    * @param[in] actor The actor whose layoutDirection is changed.
338    * @param[in] type  The layoutDirection.
339    */
340   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
341
342   /**
343    * @brief Callback function for when the locale is changed.
344    * @param[in] locale The new system locale.
345    */
346   void OnLocaleChanged(std::string locale);
347
348   /**
349    * @brief Emits TextFitChanged signal.
350    */
351   void EmitTextFitChangedSignal();
352   void OnAccessibilityStatusChanged();
353
354 private: // Data
355   Text::ControllerPtr   mController;
356   Text::TextScrollerPtr mTextScroller;
357
358   Toolkit::Visual::Base mVisual;
359
360   std::vector<Toolkit::TextAnchor> mAnchorActors;
361
362   // Signals
363   Toolkit::DevelTextLabel::AnchorClickedSignalType  mAnchorClickedSignal;
364   Toolkit::DevelTextLabel::TextFitChangedSignalType mTextFitChangedSignal;
365
366   std::string mLocale;
367
368   int  mRenderingBackend;
369   bool mTextUpdateNeeded : 1;
370   bool mLastAutoScrollEnabled : 1;
371
372 protected:
373   /**
374    * @brief This structure is to connect TextLabel with Accessible functions.
375    */
376   class TextLabelAccessible : public TextControlAccessible
377   {
378   public:
379     using TextControlAccessible::TextControlAccessible;
380
381     /**
382      * @copydoc Dali::Accessibility::Text::GetNameRaw()
383      */
384     std::string GetNameRaw() const override;
385
386     /**
387      * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
388      */
389     Property::Index GetNamePropertyIndex() override;
390
391   protected:
392     /**
393      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
394      */
395     const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
396
397     /**
398      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
399      */
400     Toolkit::Text::ControllerPtr GetTextController() const override;
401   };
402 };
403
404 } // namespace Internal
405
406 // Helpers for public-api forwarding methods
407
408 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
409 {
410   DALI_ASSERT_ALWAYS(textLabel);
411
412   Dali::RefObject& handle = textLabel.GetImplementation();
413
414   return static_cast<Toolkit::Internal::TextLabel&>(handle);
415 }
416
417 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
418 {
419   DALI_ASSERT_ALWAYS(textLabel);
420
421   const Dali::RefObject& handle = textLabel.GetImplementation();
422
423   return static_cast<const Toolkit::Internal::TextLabel&>(handle);
424 }
425
426 } // namespace Toolkit
427
428 } // namespace Dali
429
430 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H