01211c2b8ef246d55a7ebd17f1ece0f0a6be9970
[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) 2021 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/public-api/object/property-map.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
26 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
27 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
28 #include <dali-toolkit/internal/text/text-control-interface.h>
29 #include <dali-toolkit/internal/text/text-controller.h>
30 #include <dali-toolkit/internal/text/text-scroller-interface.h>
31 #include <dali-toolkit/internal/text/text-scroller.h>
32 #include <dali-toolkit/internal/visuals/text/text-visual.h>
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 /**
43  * @brief A control which renders a short text string.
44  */
45 class TextLabel : public Control, public Text::ControlInterface, public Text::ScrollerInterface, public Text::AnchorControlInterface
46 {
47 public:
48   /**
49    * @copydoc Dali::Toollkit::TextLabel::New()
50    */
51   static Toolkit::TextLabel New();
52
53   // Properties
54
55   /**
56    * @brief Called when a property of an object of this type is set.
57    *
58    * @param[in] object The object whose property is set.
59    * @param[in] index The property index.
60    * @param[in] value The new property value.
61    */
62   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
63
64   /**
65    * @brief Called to retrieve a property of an object of this type.
66    *
67    * @param[in] object The object whose property is to be retrieved.
68    * @param[in] index The property index.
69    * @return The current value of the property.
70    */
71   static Property::Value GetProperty(BaseObject* object, Property::Index index);
72
73   /**
74    * @copydoc Dali::Toollkit::TextLabel::AnchorClickedSignal()
75    */
76   DevelTextLabel::AnchorClickedSignalType& AnchorClickedSignal();
77
78   /**
79    * Connects a callback function with the object's signals.
80    * @param[in] object The object providing the signal.
81    * @param[in] tracker Used to disconnect the signal.
82    * @param[in] signalName The signal to connect to.
83    * @param[in] functor A newly allocated FunctorDelegate.
84    * @return True if the signal was connected.
85    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
86    */
87   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
88
89   /**
90    * @brief Gets text controller
91    *
92    * @return The text controller
93    */
94   Text::ControllerPtr GetTextController();
95
96 private: // From Control
97   /**
98    * @copydoc Control::OnInitialize()
99    */
100   void OnInitialize() override;
101
102   /**
103    * @copydoc Control::OnStyleChange()
104    */
105   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
106
107   /**
108    * @copydoc Control::OnRelayout()
109    */
110   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
111
112   /**
113    * @copydoc Control::OnTap()
114    */
115   void OnTap(const TapGesture& tap) override;
116
117   /**
118    * @copydoc Control::GetNaturalSize()
119    */
120   Vector3 GetNaturalSize() override;
121
122   /**
123    * @copydoc Control::GetHeightForWidth()
124    */
125   float GetHeightForWidth(float width) override;
126
127   /**
128    * @copydoc Control::OnPropertySet()
129    */
130   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
131
132   // From ControlInterface
133
134   /**
135    * @copydoc Text::ControlInterface::RequestTextRelayout()
136    */
137   void RequestTextRelayout() override;
138
139 private: // from TextScroller
140   /**
141    * @copydoc Text::ScrollerInterface::ScrollingFinished()
142    */
143   void ScrollingFinished() override;
144
145 public: // From AnchorControlInterface
146   /**
147    * @copydoc Text::AnchorControlInterface::AnchorClicked()
148    */
149   void AnchorClicked(const std::string& href) override;
150
151 private: // Implementation
152   /**
153    * Construct a new TextLabel.
154    */
155   TextLabel();
156
157   /**
158    * A reference counted object may only be deleted by calling Unreference()
159    */
160   virtual ~TextLabel();
161
162 private:
163   // Undefined copy constructor and assignment operators
164   TextLabel(const TextLabel&);
165   TextLabel& operator=(const TextLabel& rhs);
166
167   /**
168    * @brief Set up Autoscrolling
169    */
170   void SetUpAutoScrolling();
171
172   /**
173    * Creates a text-scroller if one has not been created.
174    * @return The text scroller.
175    */
176   Text::TextScrollerPtr GetTextScroller()
177   {
178     if(!mTextScroller)
179     {
180       mTextScroller = Text::TextScroller::New(*this);
181     }
182     return mTextScroller;
183   }
184
185 private: // Data
186   Text::ControllerPtr   mController;
187   Text::TextScrollerPtr mTextScroller;
188
189   Toolkit::Visual::Base mVisual;
190
191   // Signals
192   Toolkit::DevelTextLabel::AnchorClickedSignalType mAnchorClickedSignal;
193
194   int  mRenderingBackend;
195   bool mTextUpdateNeeded : 1;
196
197 protected:
198   /**
199    * @brief This structure is to connect TextLabel with Accessible functions.
200    */
201   struct AccessibleImpl : public DevelControl::AccessibleImpl,
202                           public virtual Dali::Accessibility::Text
203   {
204     using DevelControl::AccessibleImpl::AccessibleImpl;
205
206     /**
207      * @copydoc Dali::Accessibility::Text::GetText()
208      */
209     std::string GetText(size_t startOffset, size_t endOffset) override;
210
211     /**
212      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
213      */
214     size_t GetCharacterCount() override;
215
216     /**
217      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
218      */
219     size_t GetCursorOffset() override;
220
221     /**
222      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
223      */
224     bool SetCursorOffset(size_t offset) override;
225
226     /**
227      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
228      */
229     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
230
231     /**
232      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
233      */
234     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
235
236     /**
237      * @copydoc Dali::Accessibility::Text::RemoveSelection()
238      */
239     bool RemoveSelection(size_t selectionIndex) override;
240
241     /**
242      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
243      */
244     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
245
246     /**
247      * @copydoc Dali::Accessibility::Text::GetNameRaw()
248      */
249     std::string GetNameRaw() override;
250
251     /**
252      * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
253      */
254     Property::Index GetNamePropertyIndex() override;
255   };
256 };
257
258 } // namespace Internal
259
260 // Helpers for public-api forwarding methods
261
262 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
263 {
264   DALI_ASSERT_ALWAYS(textLabel);
265
266   Dali::RefObject& handle = textLabel.GetImplementation();
267
268   return static_cast<Toolkit::Internal::TextLabel&>(handle);
269 }
270
271 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
272 {
273   DALI_ASSERT_ALWAYS(textLabel);
274
275   const Dali::RefObject& handle = textLabel.GetImplementation();
276
277   return static_cast<const Toolkit::Internal::TextLabel&>(handle);
278 }
279
280 } // namespace Toolkit
281
282 } // namespace Dali
283
284 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H