Merge "Add a callback to get textfitted font size." into devel/master
[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    * @copydoc Dali::Toollkit::TextLabel::TextFitChangedSignal()
80    */
81   DevelTextLabel::TextFitChangedSignalType& TextFitChangedSignal();
82
83   /**
84    * Connects a callback function with the object's signals.
85    * @param[in] object The object providing the signal.
86    * @param[in] tracker Used to disconnect the signal.
87    * @param[in] signalName The signal to connect to.
88    * @param[in] functor A newly allocated FunctorDelegate.
89    * @return True if the signal was connected.
90    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
91    */
92   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
93
94   /**
95    * @brief Gets text controller
96    *
97    * @return The text controller
98    */
99   Text::ControllerPtr GetTextController();
100
101 private: // From Control
102   /**
103    * @copydoc Control::OnInitialize()
104    */
105   void OnInitialize() override;
106
107   /**
108    * @copydoc Control::OnStyleChange()
109    */
110   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
111
112   /**
113    * @copydoc Control::OnRelayout()
114    */
115   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
116
117   /**
118    * @copydoc Control::OnTap()
119    */
120   void OnTap(const TapGesture& tap) override;
121
122   /**
123    * @copydoc Control::GetNaturalSize()
124    */
125   Vector3 GetNaturalSize() override;
126
127   /**
128    * @copydoc Control::GetHeightForWidth()
129    */
130   float GetHeightForWidth(float width) override;
131
132   /**
133    * @copydoc Control::OnPropertySet()
134    */
135   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
136
137   // From ControlInterface
138
139   /**
140    * @copydoc Text::ControlInterface::RequestTextRelayout()
141    */
142   void RequestTextRelayout() override;
143
144 private: // from TextScroller
145   /**
146    * @copydoc Text::ScrollerInterface::ScrollingFinished()
147    */
148   void ScrollingFinished() override;
149
150 public: // From AnchorControlInterface
151   /**
152    * @copydoc Text::AnchorControlInterface::AnchorClicked()
153    */
154   void AnchorClicked(const std::string& href) override;
155
156 private: // Implementation
157   /**
158    * Construct a new TextLabel.
159    */
160   TextLabel();
161
162   /**
163    * A reference counted object may only be deleted by calling Unreference()
164    */
165   virtual ~TextLabel();
166
167 private:
168   // Undefined copy constructor and assignment operators
169   TextLabel(const TextLabel&);
170   TextLabel& operator=(const TextLabel& rhs);
171
172   /**
173    * @brief Set up Autoscrolling
174    */
175   void SetUpAutoScrolling();
176
177   /**
178    * Creates a text-scroller if one has not been created.
179    * @return The text scroller.
180    */
181   Text::TextScrollerPtr GetTextScroller()
182   {
183     if(!mTextScroller)
184     {
185       mTextScroller = Text::TextScroller::New(*this);
186     }
187     return mTextScroller;
188   }
189
190   /**
191    * @brief Callback function for when the layout is changed.
192    * @param[in] actor The actor whose layoutDirection is changed.
193    * @param[in] type  The layoutDirection.
194    */
195   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
196
197   /**
198    * @brief Emits TextFitChanged signal.
199    */
200   void EmitTextFitChangedSignal();
201
202 private: // Data
203   Text::ControllerPtr   mController;
204   Text::TextScrollerPtr mTextScroller;
205
206   Toolkit::Visual::Base mVisual;
207
208   // Signals
209   Toolkit::DevelTextLabel::AnchorClickedSignalType mAnchorClickedSignal;
210   Toolkit::DevelTextLabel::TextFitChangedSignalType mTextFitChangedSignal;
211
212   int  mRenderingBackend;
213   bool mTextUpdateNeeded : 1;
214
215 protected:
216   /**
217    * @brief This structure is to connect TextLabel with Accessible functions.
218    */
219   struct AccessibleImpl : public DevelControl::AccessibleImpl,
220                           public virtual Dali::Accessibility::Text
221   {
222     using DevelControl::AccessibleImpl::AccessibleImpl;
223
224     /**
225      * @copydoc Dali::Accessibility::Text::GetText()
226      */
227     std::string GetText(size_t startOffset, size_t endOffset) override;
228
229     /**
230      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
231      */
232     size_t GetCharacterCount() override;
233
234     /**
235      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
236      */
237     size_t GetCursorOffset() override;
238
239     /**
240      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
241      */
242     bool SetCursorOffset(size_t offset) override;
243
244     /**
245      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
246      */
247     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
248
249     /**
250      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
251      */
252     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
253
254     /**
255      * @copydoc Dali::Accessibility::Text::RemoveSelection()
256      */
257     bool RemoveSelection(size_t selectionIndex) override;
258
259     /**
260      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
261      */
262     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
263
264     /**
265      * @copydoc Dali::Accessibility::Text::GetNameRaw()
266      */
267     std::string GetNameRaw() override;
268
269     /**
270      * @copydoc Dali::Accessibility::Text::GetNamePropertyIndex()
271      */
272     Property::Index GetNamePropertyIndex() override;
273   };
274 };
275
276 } // namespace Internal
277
278 // Helpers for public-api forwarding methods
279
280 inline Toolkit::Internal::TextLabel& GetImpl(Toolkit::TextLabel& textLabel)
281 {
282   DALI_ASSERT_ALWAYS(textLabel);
283
284   Dali::RefObject& handle = textLabel.GetImplementation();
285
286   return static_cast<Toolkit::Internal::TextLabel&>(handle);
287 }
288
289 inline const Toolkit::Internal::TextLabel& GetImpl(const Toolkit::TextLabel& textLabel)
290 {
291   DALI_ASSERT_ALWAYS(textLabel);
292
293   const Dali::RefObject& handle = textLabel.GetImplementation();
294
295   return static_cast<const Toolkit::Internal::TextLabel&>(handle);
296 }
297
298 } // namespace Toolkit
299
300 } // namespace Dali
301
302 #endif // DALI_TOOLKIT_INTERNAL_TEXT_LABEL_H