Add SelectionCleared signal
[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/control-devel.h>
29 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
30 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
33 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
34 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
35 #include <dali-toolkit/internal/text/text-control-interface.h>
36 #include <dali-toolkit/internal/text/text-controller.h>
37 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
38 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
39 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
40 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
42
43 namespace Dali
44 {
45 namespace Toolkit
46 {
47 namespace Internal
48 {
49 /**
50  * @brief A control which renders a long text string with styles.
51  */
52 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
53 {
54 public:
55   /**
56    * @copydoc Dali::Toollkit::TextEditor::New()
57    */
58   static Toolkit::TextEditor New();
59
60   // Properties
61
62   /**
63    * @brief Called when a property of an object of this type is set.
64    *
65    * @param[in] object The object whose property is set.
66    * @param[in] index The property index.
67    * @param[in] value The new property value.
68    */
69   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
70
71   /**
72    * @brief Called to retrieve a property of an object of this type.
73    *
74    * @param[in] object The object whose property is to be retrieved.
75    * @param[in] index The property index.
76    * @return The current value of the property.
77    */
78   static Property::Value GetProperty(BaseObject* object, Property::Index index);
79
80   /**
81    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
82    */
83   InputMethodContext GetInputMethodContext();
84
85   /**
86    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
87    */
88   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
89
90   /**
91    * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
92    */
93   DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
94
95   /**
96    * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
97    */
98   DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
99
100   /**
101    * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
102    */
103   DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
104
105   /**
106    * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
107    */
108   DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
109
110   /**
111    * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
112    */
113   DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
114
115   /**
116    * Connects a callback function with the object's signals.
117    * @param[in] object The object providing the signal.
118    * @param[in] tracker Used to disconnect the signal.
119    * @param[in] signalName The signal to connect to.
120    * @param[in] functor A newly allocated FunctorDelegate.
121    * @return True if the signal was connected.
122    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
123    */
124   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
125
126   /**
127    * @brief Gets text controller
128    *
129    * @return The text controller
130    */
131   Text::ControllerPtr GetTextController();
132
133   /**
134    * @copydoc TextEditor::TextChangedSignal()
135    */
136   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
137
138   /**
139    * @copydoc TextEditor::TextChangedSignal()
140    */
141   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
142
143   /**
144    * @copydoc TextEditor::ScrollStateChangedSignal()
145    */
146   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
147
148 private: // From Control
149   /**
150    * @copydoc Control::OnInitialize()
151    */
152   void OnInitialize() override;
153
154   /**
155    * @copydoc Control::OnStyleChange()
156    */
157   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
158
159   /**
160    * @copydoc Control::GetNaturalSize()
161    */
162   Vector3 GetNaturalSize() override;
163
164   /**
165    * @copydoc Control::GetHeightForWidth()
166    */
167   float GetHeightForWidth(float width) override;
168
169   /**
170    * @copydoc Control::OnInitialize()
171    */
172   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
173
174   /**
175    * @copydoc Control::OnKeyInputFocusGained()
176    */
177   void OnKeyInputFocusGained() override;
178
179   /**
180    * @copydoc Control::OnKeyInputFocusLost()
181    */
182   void OnKeyInputFocusLost() override;
183
184   /**
185    * @copydoc Control::OnAccessibilityActivated()
186    */
187   bool OnAccessibilityActivated() override;
188
189   /**
190    * @copydoc Control::OnTap()
191    */
192   void OnTap(const TapGesture& tap) override;
193
194   /**
195    * @copydoc Control::OnPan()
196    */
197   void OnPan(const PanGesture& gesture) override;
198
199   /**
200    * @copydoc Control::OnLongPress()
201    */
202   void OnLongPress(const LongPressGesture& gesture) override;
203
204   /**
205    * @copydoc Control::OnSceneConnection()
206    */
207   void OnSceneConnection(int depth) override;
208
209   /**
210    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
211    */
212   bool OnKeyEvent(const KeyEvent& event) override;
213
214   // From ControlInterface
215
216   /**
217    * @copydoc Text::ControlInterface::RequestTextRelayout()
218    */
219   void RequestTextRelayout() override;
220
221   // From EditableControlInterface
222
223   /**
224    * @copydoc Text::EditableControlInterface::TextChanged()
225    */
226   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
227
228   /**
229    * @copydoc Text::EditableControlInterface::TextDeleted()
230    */
231   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
232
233   /**
234    * @copydoc Text::EditableControlInterface::CursorPositionChanged()
235    */
236   void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
237
238   /**
239    * @copydoc Text::EditableControlInterface::TextChanged()
240    */
241   void TextChanged(bool immediate) override;
242
243   /**
244    * @copydoc Text::EditableControlInterface::MaxLengthReached()
245    */
246   void MaxLengthReached() override;
247
248   /**
249    * @copydoc Text::EditableControlInterface::InputStyleChanged()
250    */
251   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
252
253   /**
254    * @copydoc Text::SelectableControlInterface::SelectionChanged()
255    */
256   void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
257
258   /**
259    * @copydoc Text::EditableControlInterface::AddDecoration()
260    */
261   void AddDecoration(Actor& actor, bool needsClipping) override;
262
263   /**
264    * @copydoc Text::EditableControlInterface::InputFiltered()
265    */
266   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
267
268   /**
269    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
270    */
271   void GetControlBackgroundColor(Vector4& color) const override;
272
273   // From SelectableControlInterface
274 public:
275   /**
276    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
277    */
278   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
279
280   /**
281    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
282    */
283   Uint32Pair GetTextSelectionRange() const override;
284
285   /**
286    * @copydoc Text::SelectableControlInterface::SelectWholeText()
287    */
288   void SelectWholeText() override;
289
290   /**
291    * @copydoc Text::SelectableControlInterface::SelectNone()
292    */
293   void SelectNone() override;
294
295   /**
296    * @copydoc Text::SelectableControlInterface::SelectText()
297    */
298   void SelectText(const uint32_t start, const uint32_t end) override;
299
300   /**
301    * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
302    */
303   void ScrollBy(Vector2 Scroll);
304
305   /**
306    * @brief Get Horizontal scroll position of TextEditor.
307    *
308    * @return Horizontal scroll position (in pixels) of TextEditor.
309    */
310   float GetHorizontalScrollPosition();
311
312   /**
313    * @brief Get Vertical scroll position of TextEditor.
314    *
315    * @return Vertical scroll position (in pixels) of TextEditor.
316    */
317   float GetVerticalScrollPosition();
318
319   /**
320    * @copydoc Text::SelectableControlInterface::GetSelectedText()
321    */
322   string GetSelectedText() const override;
323
324   /**
325    * @copydoc Text::EditableControlInterface::IsEditable()
326    */
327   bool IsEditable() const override;
328
329   /**
330    * @copydoc Text::EditableControlInterface::SetEditable()
331    */
332   void SetEditable(bool editable) override;
333
334   // From AnchorControlInterface
335
336   /**
337    * @copydoc Text::AnchorControlInterface::AnchorClicked()
338    */
339   void AnchorClicked(const std::string& href) override;
340
341 private: // Implementation
342   /**
343    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
344    */
345   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
346
347   /**
348    * @brief Callback when Clipboard signals an item should be pasted
349    * @param[in] clipboard handle to Clipboard Event Notifier
350    */
351   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
352
353   /**
354    * @brief Get a Property Map for the image used for the required Handle Image
355    * @param[out] value the returned image property
356    * @param[in] handleType the type of handle
357    * @param[in] handleImageType the type of image for the given handleType
358    */
359   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
360
361   /**
362    * @brief Callback when keyboard is shown/hidden.
363    *
364    * @param[in] keyboardShown True if keyboard is shown.
365    */
366   void KeyboardStatusChanged(bool keyboardShown);
367
368   /**
369    * @brief update scroll bar position
370    *
371    * If text scroll is occurred, create or update scroll bar position
372    */
373   void UpdateScrollBar();
374
375   /**
376    * @brief Callback when TextEditor is touched
377    *
378    * @param[in] actor TextEditor touched
379    * @param[in] touch Touch information
380    */
381   bool OnTouched(Actor actor, const TouchEvent& touch);
382
383   /**
384    * @brief Callbacks called on idle.
385    *
386    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
387    */
388   void OnIdleSignal();
389
390   /**
391    * @brief Emits CursorPositionChanged signal.
392    */
393   void EmitCursorPositionChangedSignal();
394
395   /**
396    * @brief Emits TextChanged signal.
397    */
398   void EmitTextChangedSignal();
399
400   /**
401    * @brief Emits SelectionChanged signal.
402    */
403   void EmitSelectionChangedSignal();
404
405   /**
406    * @brief Emits SelectionCleared signal.
407    */
408   void EmitSelectionClearedSignal();
409
410   /**
411    * @brief set RenderActor's position with new scrollPosition
412    *
413    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
414    */
415   void ApplyScrollPosition();
416
417   /**
418    * @brief Callback function for ScrollBar indicator animation finished signal
419    *
420    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
421    */
422   void OnScrollIndicatorAnimationFinished(Animation& animation);
423
424   /**
425   * @brief Callback function for when the layout is changed.
426   * @param[in] actor The actor whose layoutDirection is changed.
427   * @param[in] type  The layoutDirection.
428   */
429   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
430
431   /**
432    * Construct a new TextEditor.
433    */
434   TextEditor();
435
436   /**
437    * A reference counted object may only be deleted by calling Unreference()
438    */
439   virtual ~TextEditor();
440
441   // Undefined copy constructor and assignment operators
442   TextEditor(const TextEditor&);
443   TextEditor& operator=(const TextEditor& rhs);
444
445   /**
446    * @brief Resize actor to the given size.
447    *
448    * @param[in] actor The actor to be resized.
449    * @param[in] size Size to change.
450    */
451   void ResizeActor(Actor& actor, const Vector2& size);
452
453   /**
454    * @brief Render view, create and attach actor(s) to this text editor.
455    */
456   void RenderText(Text::Controller::UpdateTextType updateTextType);
457
458   // Connection needed to re-render text, when a text editor returns to the scene.
459   void OnSceneConnect(Dali::Actor actor);
460
461 private: // Data
462   // Signals
463   Toolkit::TextEditor::TextChangedSignalType                mTextChangedSignal;
464   Toolkit::TextEditor::InputStyleChangedSignalType          mInputStyleChangedSignal;
465   Toolkit::TextEditor::ScrollStateChangedSignalType         mScrollStateChangedSignal;
466   Toolkit::DevelTextEditor::MaxLengthReachedSignalType      mMaxLengthReachedSignal;
467   Toolkit::DevelTextEditor::AnchorClickedSignalType         mAnchorClickedSignal;
468   Toolkit::DevelTextEditor::InputFilteredSignalType         mInputFilteredSignal;
469   Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
470   Toolkit::DevelTextEditor::SelectionChangedSignalType      mSelectionChangedSignal;
471   Toolkit::DevelTextEditor::SelectionClearedSignalType      mSelectionClearedSignal;
472
473   InputMethodContext            mInputMethodContext;
474   Text::ControllerPtr           mController;
475   Text::RendererPtr             mRenderer;
476   Text::DecoratorPtr            mDecorator;
477   Text::TextVerticalScrollerPtr mTextVerticalScroller;
478   Toolkit::Control              mStencil;
479   Toolkit::ScrollBar            mScrollBar;
480   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
481   Dali::TimePeriod              mAnimationPeriod;
482   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
483   Dali::InputMethodOptions      mInputMethodOptions;
484
485   Actor         mRenderableActor;
486   Actor         mActiveLayer;
487   Actor         mBackgroundActor;
488   CallbackBase* mIdleCallback;
489
490   float mAlignmentOffset;
491   float mScrollAnimationDuration;
492   float mLineSpacing;
493   int   mRenderingBackend;
494   bool  mHasBeenStaged : 1;
495   bool  mScrollAnimationEnabled : 1;
496   bool  mScrollBarEnabled : 1;
497   bool  mScrollStarted : 1;
498   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
499   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
500   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
501   bool  mSelectionCleared : 1;      ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
502
503   //args for cursor PositionChanged event
504   unsigned int mOldPosition;
505
506   //args for selection changed event
507   uint32_t mOldSelectionStart;
508   uint32_t mOldSelectionEnd;
509
510   /**
511    * @brief This structure is to connect TextEditor with Accessible functions.
512    */
513   struct AccessibleImpl : public DevelControl::AccessibleImpl,
514                           public virtual Dali::Accessibility::Text,
515                           public virtual Dali::Accessibility::EditableText
516   {
517     using DevelControl::AccessibleImpl::AccessibleImpl;
518
519     /**
520      * @copydoc Dali::Accessibility::Accessible::GetName()
521      */
522     std::string GetName() override;
523
524     /**
525      * @copydoc Dali::Accessibility::Text::GetText()
526      */
527     std::string GetText(size_t startOffset, size_t endOffset) override;
528
529     /**
530      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
531      */
532     size_t GetCharacterCount() override;
533
534     /**
535      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
536      */
537     size_t GetCursorOffset() override;
538
539     /**
540      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
541      */
542     bool SetCursorOffset(size_t offset) override;
543
544     /**
545      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
546      */
547     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
548
549     /**
550      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
551      */
552     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
553
554     /**
555      * @copydoc Dali::Accessibility::Text::RemoveSelection()
556      */
557     bool RemoveSelection(size_t selectionIndex) override;
558
559     /**
560      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
561      */
562     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
563
564     /**
565      * @copydoc Dali::Accessibility::EditableText::CopyText()
566      */
567     bool CopyText(size_t startPosition, size_t endPosition) override;
568
569     /**
570      * @copydoc Dali::Accessibility::EditableText::CutText()
571      */
572     bool CutText(size_t startPosition, size_t endPosition) override;
573
574     /**
575      * @copydoc Dali::Accessibility::Accessible::GetStates()
576      */
577     Accessibility::States CalculateStates() override;
578
579     /**
580      * @copydoc Dali::Accessibility::EditableText::InsertText()
581      */
582     bool InsertText(size_t startPosition, std::string text) override;
583
584     /**
585      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
586      */
587     bool SetTextContents(std::string newContents) override;
588
589     /**
590      * @copydoc Dali::Accessibility::EditableText::DeleteText()
591      */
592     bool DeleteText(size_t startPosition, size_t endPosition) override;
593   };
594 };
595
596 } // namespace Internal
597
598 // Helpers for public-api forwarding methods
599
600 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
601 {
602   DALI_ASSERT_ALWAYS(textEditor);
603
604   Dali::RefObject& handle = textEditor.GetImplementation();
605
606   return static_cast<Toolkit::Internal::TextEditor&>(handle);
607 }
608
609 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
610 {
611   DALI_ASSERT_ALWAYS(textEditor);
612
613   const Dali::RefObject& handle = textEditor.GetImplementation();
614
615   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
616 }
617
618 } // namespace Toolkit
619
620 } // namespace Dali
621
622 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H