ed8d039f1875ff5daa31b665292ba94580cedf6c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-editor-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_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/devel-api/adaptor-framework/accessibility.h>
23 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
24 #include <dali/devel-api/adaptor-framework/input-method-context.h>
25 #include <dali/public-api/animation/animation.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
29 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
30 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
31 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
32 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
33 #include <dali-toolkit/internal/text/text-control-interface.h>
34 #include <dali-toolkit/internal/text/text-controller.h>
35 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
36 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
37 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
38 #include <dali-toolkit/public-api/controls/control-impl.h>
39 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
40
41 namespace Dali
42 {
43 namespace Toolkit
44 {
45 namespace Internal
46 {
47 /**
48  * @brief A control which renders a long text string with styles.
49  */
50 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface
51 {
52 public:
53   /**
54    * @copydoc Dali::Toollkit::TextEditor::New()
55    */
56   static Toolkit::TextEditor New();
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::TextEditor::GetInputMethodContext()
80    */
81   InputMethodContext GetInputMethodContext();
82
83   /**
84    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
85    */
86   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
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    * @copydoc TextEditor::TextChangedSignal()
101    */
102   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
103
104   /**
105    * @copydoc TextEditor::TextChangedSignal()
106    */
107   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
108
109   /**
110    * @copydoc TextEditor::ScrollStateChangedSignal()
111    */
112   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
113
114 private: // From Control
115   /**
116    * @copydoc Control::OnInitialize()
117    */
118   void OnInitialize() override;
119
120   /**
121    * @copydoc Control::OnStyleChange()
122    */
123   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
124
125   /**
126    * @copydoc Control::GetNaturalSize()
127    */
128   Vector3 GetNaturalSize() override;
129
130   /**
131    * @copydoc Control::GetHeightForWidth()
132    */
133   float GetHeightForWidth(float width) override;
134
135   /**
136    * @copydoc Control::OnInitialize()
137    */
138   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
139
140   /**
141    * @copydoc Control::OnKeyInputFocusGained()
142    */
143   void OnKeyInputFocusGained() override;
144
145   /**
146    * @copydoc Control::OnKeyInputFocusLost()
147    */
148   void OnKeyInputFocusLost() override;
149
150   /**
151    * @copydoc Control::OnAccessibilityActivated()
152    */
153   bool OnAccessibilityActivated() override;
154
155   /**
156    * @copydoc Control::OnTap()
157    */
158   void OnTap(const TapGesture& tap) override;
159
160   /**
161    * @copydoc Control::OnPan()
162    */
163   void OnPan(const PanGesture& gesture) override;
164
165   /**
166    * @copydoc Control::OnLongPress()
167    */
168   void OnLongPress(const LongPressGesture& gesture) override;
169
170   /**
171    * @copydoc Control::OnSceneConnection()
172    */
173   void OnSceneConnection(int depth) override;
174
175   /**
176    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
177    */
178   bool OnKeyEvent(const KeyEvent& event) override;
179
180   // From ControlInterface
181
182   /**
183    * @copydoc Text::ControlInterface::RequestTextRelayout()
184    */
185   void RequestTextRelayout() override;
186
187   // From EditableControlInterface
188
189   /**
190    * @copydoc Text::EditableControlInterface::TextChanged()
191    */
192   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
193
194   /**
195    * @copydoc Text::EditableControlInterface::TextDeleted()
196    */
197   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
198
199   /**
200    * @copydoc Text::EditableControlInterface::CaretMoved()
201    */
202   void CaretMoved(unsigned int position) override;
203
204   /**
205    * @copydoc Text::EditableControlInterface::TextChanged()
206    */
207   void TextChanged() override;
208
209   /**
210    * @copydoc Text::EditableControlInterface::MaxLengthReached()
211    */
212   void MaxLengthReached() override;
213
214   /**
215    * @copydoc Text::EditableControlInterface::InputStyleChanged()
216    */
217   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
218
219   /**
220    * @copydoc Text::EditableControlInterface::AddDecoration()
221    */
222   void AddDecoration(Actor& actor, bool needsClipping) override;
223
224   // From SelectableControlInterface
225 public:
226   /**
227    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
228    */
229   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
230
231   /**
232    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
233    */
234   Uint32Pair GetTextSelectionRange() const override;
235
236   /**
237    * @copydoc Text::SelectableControlInterface::SelectWholeText()
238    */
239   void SelectWholeText() override;
240
241   /**
242    * @copydoc Text::SelectableControlInterface::SelectNone()
243    */
244   void SelectNone() override;
245
246   /**
247    * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
248    */
249   void ScrollBy(Vector2 Scroll);
250
251   /**
252    * @brief Get Horizontal scroll position of TextEditor.
253    *
254    * @return Horizontal scroll position (in pixels) of TextEditor.
255    */
256   float GetHorizontalScrollPosition();
257
258   /**
259    * @brief Get Vertical scroll position of TextEditor.
260    *
261    * @return Vertical scroll position (in pixels) of TextEditor.
262    */
263   float GetVerticalScrollPosition();
264
265   /**
266    * @copydoc Text::SelectableControlInterface::GetSelectedText()
267    */
268   string GetSelectedText() const override;
269
270   /**
271    * @copydoc Text::EditableControlInterface::IsEditable()
272    */
273   bool IsEditable() const override;
274
275   /**
276    * @copydoc Text::EditableControlInterface::SetEditable()
277    */
278   void                SetEditable(bool editable) override;
279   Text::ControllerPtr getController();
280
281 private: // Implementation
282   /**
283    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
284    */
285   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
286
287   /**
288    * @brief Callback when Clipboard signals an item should be pasted
289    * @param[in] clipboard handle to Clipboard Event Notifier
290    */
291   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
292
293   /**
294    * @brief Get a Property Map for the image used for the required Handle Image
295    * @param[out] value the returned image property
296    * @param[in] handleType the type of handle
297    * @param[in] handleImageType the type of image for the given handleType
298    */
299   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
300
301   /**
302    * @brief Callback when keyboard is shown/hidden.
303    *
304    * @param[in] keyboardShown True if keyboard is shown.
305    */
306   void KeyboardStatusChanged(bool keyboardShown);
307
308   /**
309    * @brief update scroll bar position
310    *
311    * If text scroll is occurred, create or update scroll bar position
312    */
313   void UpdateScrollBar();
314
315   /**
316    * @brief Callback when TextEditor is touched
317    *
318    * @param[in] actor TextEditor touched
319    * @param[in] touch Touch information
320    */
321   bool OnTouched(Actor actor, const TouchEvent& touch);
322
323   /**
324    * @brief Callbacks called on idle.
325    *
326    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
327    */
328   void OnIdleSignal();
329
330   /**
331    * @brief set RenderActor's position with new scrollPosition
332    *
333    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
334    */
335   void ApplyScrollPosition();
336
337   /**
338    * @brief Callback function for ScrollBar indicator animation finished signal
339    *
340    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
341    */
342   void OnScrollIndicatorAnimationFinished(Animation& animation);
343
344   /**
345    * Construct a new TextEditor.
346    */
347   TextEditor();
348
349   /**
350    * A reference counted object may only be deleted by calling Unreference()
351    */
352   virtual ~TextEditor();
353
354   // Undefined copy constructor and assignment operators
355   TextEditor(const TextEditor&);
356   TextEditor& operator=(const TextEditor& rhs);
357
358   /**
359    * @brief Resize actor to the given size.
360    *
361    * @param[in] actor The actor to be resized.
362    * @param[in] size Size to change.
363    */
364   void ResizeActor( Actor& actor, const Vector2& size );
365
366   /**
367    * @brief Render view, create and attach actor(s) to this text editor.
368    */
369   void RenderText(Text::Controller::UpdateTextType updateTextType);
370
371   // Connection needed to re-render text, when a text editor returns to the scene.
372   void OnSceneConnect(Dali::Actor actor);
373
374 private: // Data
375   // Signals
376   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
377   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
378   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
379   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
380
381   InputMethodContext            mInputMethodContext;
382   Text::ControllerPtr           mController;
383   Text::RendererPtr             mRenderer;
384   Text::DecoratorPtr            mDecorator;
385   Text::TextVerticalScrollerPtr mTextVerticalScroller;
386   Toolkit::Control              mStencil;
387   Toolkit::ScrollBar            mScrollBar;
388   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
389   Dali::TimePeriod              mAnimationPeriod;
390   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
391
392   Actor         mRenderableActor;
393   Actor         mActiveLayer;
394   CallbackBase* mIdleCallback;
395
396   float mAlignmentOffset;
397   float mScrollAnimationDuration;
398   float mLineSpacing;
399   int   mRenderingBackend;
400   bool  mHasBeenStaged : 1;
401   bool  mScrollAnimationEnabled : 1;
402   bool  mScrollBarEnabled : 1;
403   bool  mScrollStarted : 1;
404
405   struct AccessibleImpl : public Control::Impl::AccessibleImpl,
406                           public virtual Dali::Accessibility::Text,
407                           public virtual Dali::Accessibility::EditableText
408   {
409     using Control::Impl::AccessibleImpl::AccessibleImpl;
410
411     std::string           GetName() override;
412     std::string           GetText(size_t startOffset, size_t endOffset) override;
413     size_t                GetCharacterCount() override;
414     size_t                GetCaretOffset() override;
415     bool                  SetCaretOffset(size_t offset) override;
416     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
417     Accessibility::Range  GetSelection(size_t selectionNum) override;
418     bool                  RemoveSelection(size_t selectionNum) override;
419     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
420     bool                  CopyText(size_t startPosition, size_t endPosition) override;
421     bool                  CutText(size_t startPosition, size_t endPosition) override;
422     Accessibility::States CalculateStates() override;
423   };
424 };
425
426 } // namespace Internal
427
428 // Helpers for public-api forwarding methods
429
430 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
431 {
432   DALI_ASSERT_ALWAYS(textEditor);
433
434   Dali::RefObject& handle = textEditor.GetImplementation();
435
436   return static_cast<Toolkit::Internal::TextEditor&>(handle);
437 }
438
439 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
440 {
441   DALI_ASSERT_ALWAYS(textEditor);
442
443   const Dali::RefObject& handle = textEditor.GetImplementation();
444
445   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
446 }
447
448 } // namespace Toolkit
449
450 } // namespace Dali
451
452 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H