1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/devel-api/atspi-interfaces/editable-text.h>
26 #include <dali/devel-api/atspi-interfaces/hypertext.h>
27 #include <dali/devel-api/atspi-interfaces/text.h>
28 #include <dali/public-api/animation/animation.h>
31 #include <dali-toolkit/devel-api/controls/control-devel.h>
32 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
33 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
34 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
35 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
36 #include <dali-toolkit/internal/text/controller/text-controller.h>
37 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
38 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
39 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
40 #include <dali-toolkit/internal/text/text-control-interface.h>
41 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
42 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
43 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
44 #include <dali-toolkit/public-api/controls/control-impl.h>
45 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
54 * @brief A control which renders a long text string with styles.
56 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
60 * @copydoc Dali::Toollkit::TextEditor::New()
61 * @param[in] additionalBehaviour custom behavior flags for this TextEditor. Default is CONTROL_BEHAVIOUR_DEFAULT
63 static Toolkit::TextEditor New(ControlBehaviour additionalBehaviour = ControlBehaviour::CONTROL_BEHAVIOUR_DEFAULT);
68 * @brief Called when a property of an object of this type is set.
70 * @param[in] object The object whose property is set.
71 * @param[in] index The property index.
72 * @param[in] value The new property value.
74 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
77 * @brief Called to retrieve a property of an object of this type.
79 * @param[in] object The object whose property is to be retrieved.
80 * @param[in] index The property index.
81 * @return The current value of the property.
83 static Property::Value GetProperty(BaseObject* object, Property::Index index);
86 * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
88 InputMethodContext GetInputMethodContext();
91 * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
93 DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
96 * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
98 DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
101 * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
103 DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
106 * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
108 DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
111 * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
113 DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
116 * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
118 DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
121 * @copydoc Dali::Toollkit::TextEditor::SelectionStartedSignal()
123 DevelTextEditor::SelectionStartedSignalType& SelectionStartedSignal();
126 * Connects a callback function with the object's signals.
127 * @param[in] object The object providing the signal.
128 * @param[in] tracker Used to disconnect the signal.
129 * @param[in] signalName The signal to connect to.
130 * @param[in] functor A newly allocated FunctorDelegate.
131 * @return True if the signal was connected.
132 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
134 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
137 * @brief Gets text controller
139 * @return The text controller
141 Text::ControllerPtr GetTextController();
144 * @copydoc TextEditor::TextChangedSignal()
146 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
149 * @copydoc TextEditor::TextChangedSignal()
151 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
154 * @copydoc TextEditor::ScrollStateChangedSignal()
156 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
158 private: // From Control
160 * @copydoc Control::OnInitialize()
162 void OnInitialize() override;
165 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
167 DevelControl::ControlAccessible* CreateAccessibleObject() override;
170 * @copydoc Control::OnStyleChange()
172 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
175 * @copydoc Control::GetNaturalSize()
177 Vector3 GetNaturalSize() override;
180 * @copydoc Control::GetHeightForWidth()
182 float GetHeightForWidth(float width) override;
185 * @copydoc Control::OnInitialize()
187 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
190 * @copydoc Control::OnKeyInputFocusGained()
192 void OnKeyInputFocusGained() override;
195 * @copydoc Control::OnKeyInputFocusLost()
197 void OnKeyInputFocusLost() override;
200 * @copydoc Control::OnAccessibilityActivated()
202 bool OnAccessibilityActivated() override;
205 * @copydoc Control::OnTap()
207 void OnTap(const TapGesture& tap) override;
210 * @copydoc Control::OnPan()
212 void OnPan(const PanGesture& gesture) override;
215 * @copydoc Control::OnLongPress()
217 void OnLongPress(const LongPressGesture& gesture) override;
220 * @copydoc Control::OnSceneConnection()
222 void OnSceneConnection(int depth) override;
225 * @copydoc Control::OnPropertySet()
227 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
230 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
232 bool OnKeyEvent(const KeyEvent& event) override;
234 // From ControlInterface
237 * @copydoc Text::ControlInterface::RequestTextRelayout()
239 void RequestTextRelayout() override;
241 // From EditableControlInterface
244 * @copydoc Text::EditableControlInterface::TextChanged()
246 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
249 * @copydoc Text::EditableControlInterface::TextDeleted()
251 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
254 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
256 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
259 * @copydoc Text::EditableControlInterface::TextChanged()
261 void TextChanged(bool immediate) override;
264 * @copydoc Text::EditableControlInterface::MaxLengthReached()
266 void MaxLengthReached() override;
269 * @copydoc Text::EditableControlInterface::InputStyleChanged()
271 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
274 * @copydoc Text::SelectableControlInterface::SelectionChanged()
276 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
279 * @copydoc Text::EditableControlInterface::AddDecoration()
281 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
284 * @copydoc Text::EditableControlInterface::InputFiltered()
286 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
289 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
291 void GetControlBackgroundColor(Vector4& color) const override;
293 // From SelectableControlInterface
296 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
298 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
301 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
303 Uint32Pair GetTextSelectionRange() const override;
306 * @copydoc Text::SelectableControlInterface::SelectWholeText()
308 void SelectWholeText() override;
311 * @copydoc Text::SelectableControlInterface::SelectNone()
313 void SelectNone() override;
316 * @copydoc Text::SelectableControlInterface::SelectText()
318 void SelectText(const uint32_t start, const uint32_t end) override;
321 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
323 void ScrollBy(Vector2 Scroll);
326 * @brief Get Horizontal scroll position of TextEditor.
328 * @return Horizontal scroll position (in pixels) of TextEditor.
330 float GetHorizontalScrollPosition();
333 * @brief Get Vertical scroll position of TextEditor.
335 * @return Vertical scroll position (in pixels) of TextEditor.
337 float GetVerticalScrollPosition();
340 * @brief Get the rendered size of a specific text range.
341 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
342 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
344 * @param[in] startIndex start index of the text requested to calculate size for.
345 * @param[in] endIndex end index(included) of the text requested to calculate size for.
346 * @return list of sizes of the reuested text.
348 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
351 * @brief Get the top/left rendered position of a specific text range.
352 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
353 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
355 * @param[in] startIndex start index of the text requested to get position to.
356 * @param[in] endIndex end index(included) of the text requested to get position to.
357 * @return list of positions of the requested text.
359 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
362 * @brief Get the line bounding rectangle.
363 * if the requested index is out of range or the line is not yet rendered, a rect of {0, 0, 0, 0} is returned.
365 * @param[in] lineIndex line index to which we want to calculate the geometry for.
366 * @return bounding rectangle.
368 Rect<float> GetLineBoundingRectangle(const uint32_t lineIndex) const;
371 * @brief Get the character bounding rectangle.
372 * If the text is not yet rendered or the index > text.Count(); a rect of {0, 0, 0, 0} is returned.
374 * @param[in] charIndex character index to which we want to calculate the geometry for.
375 * @return bounding rectangle.
377 Rect<float> GetCharacterBoundingRectangle(const uint32_t charIndex) const;
380 * @brief Get the character index.
381 * If the text is not yet rendered or the text is empty, -1 is returned.
383 * @param[in] visualX visual x position.
384 * @param[in] visualY visual y position.
385 * @return character index.
387 int GetCharacterIndexAtPosition(float visualX, float visualY) const;
390 * @brief Set the @p spannedText into current textEditor
391 * the spanned text contains content (text) and format (spans with ranges)
392 * the text is copied into text-controller and the spans are applied on ranges
394 * @param[in] spannedText the text with spans.
396 void SetSpannedText(const Text::Spanned& spannedText);
399 * @copydoc Text::SelectableControlInterface::GetSelectedText()
401 string GetSelectedText() const override;
404 * @copydoc Text::EditableControlInterface::IsEditable()
406 bool IsEditable() const override;
409 * @copydoc Text::EditableControlInterface::SetEditable()
411 void SetEditable(bool editable) override;
414 * @copydoc Text::EditableControlInterface::CopyText()
416 string CopyText() override;
419 * @copydoc Text::EditableControlInterface::CutText()
421 string CutText() override;
424 * @copydoc Text::EditableControlInterface::PasteText()
426 void PasteText() override;
428 // From AnchorControlInterface
431 * @copydoc Text::AnchorControlInterface::AnchorClicked()
433 void AnchorClicked(const std::string& href) override;
435 private: // Implementation
437 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
439 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
442 * @brief Callback when Clipboard signals an item should be pasted
443 * @param[in] clipboard handle to Clipboard Event Notifier
445 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
448 * @brief Get a Property Map for the image used for the required Handle Image
449 * @param[out] value the returned image property
450 * @param[in] handleType the type of handle
451 * @param[in] handleImageType the type of image for the given handleType
453 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
456 * @brief Callback when keyboard is shown/hidden.
458 * @param[in] keyboardShown True if keyboard is shown.
460 void KeyboardStatusChanged(bool keyboardShown);
463 * @brief update scroll bar position
465 * If text scroll is occurred, create or update scroll bar position
467 void UpdateScrollBar();
470 * @brief Callback when TextEditor is touched
472 * @param[in] actor TextEditor touched
473 * @param[in] touch Touch information
475 bool OnTouched(Actor actor, const TouchEvent& touch);
478 * @brief Callbacks called on idle.
480 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
485 * @brief Emits CursorPositionChanged signal.
487 void EmitCursorPositionChangedSignal();
490 * @brief Emits TextChanged signal.
492 void EmitTextChangedSignal();
495 * @brief Emits SelectionChanged signal.
497 void EmitSelectionChangedSignal();
500 * @brief Emits SelectionCleared signal.
502 void EmitSelectionClearedSignal();
505 * @brief Emits SelectionStarted signal.
507 void EmitSelectionStartedSignal();
510 * @brief set RenderActor's position with new scrollPosition
512 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
514 void ApplyScrollPosition();
517 * @brief Callback function for ScrollBar indicator animation finished signal
519 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
521 void OnScrollIndicatorAnimationFinished(Animation& animation);
524 * @brief Callback function for when the layout is changed.
525 * @param[in] actor The actor whose layoutDirection is changed.
526 * @param[in] type The layoutDirection.
528 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
531 * @brief Add a layer for active or cursor.
532 * @param[in] layer The actor in which to store the layer.
533 * @param[in] actor The new layer to add.
535 void AddLayer(Actor& layer, Actor& actor);
538 * Construct a new TextEditor.
540 * @param[in] additionalBehaviour additional behaviour flags for this TextEditor
542 TextEditor(ControlBehaviour additionalBehaviour);
545 * A reference counted object may only be deleted by calling Unreference()
547 virtual ~TextEditor();
549 // Undefined copy constructor and assignment operators
550 TextEditor(const TextEditor&);
551 TextEditor& operator=(const TextEditor& rhs);
554 * @brief Resize actor to the given size.
556 * @param[in] actor The actor to be resized.
557 * @param[in] size Size to change.
559 void ResizeActor(Actor& actor, const Vector2& size);
562 * @brief Render view, create and attach actor(s) to this text editor.
564 void RenderText(Text::Controller::UpdateTextType updateTextType);
566 // Connection needed to re-render text, when a text editor returns to the scene.
567 void OnSceneConnect(Dali::Actor actor);
569 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
570 void OnAccessibilityStatusChanged();
574 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
575 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
576 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
577 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
578 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
579 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
580 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
581 Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal;
582 Toolkit::DevelTextEditor::SelectionClearedSignalType mSelectionClearedSignal;
583 Toolkit::DevelTextEditor::SelectionStartedSignalType mSelectionStartedSignal;
585 InputMethodContext mInputMethodContext;
586 Text::ControllerPtr mController;
587 Text::RendererPtr mRenderer;
588 Text::DecoratorPtr mDecorator;
589 Text::TextVerticalScrollerPtr mTextVerticalScroller;
590 Toolkit::Control mStencil;
591 Toolkit::ScrollBar mScrollBar;
592 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
593 Dali::TimePeriod mAnimationPeriod;
594 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
595 std::vector<Toolkit::TextAnchor> mAnchorActors;
596 Dali::InputMethodOptions mInputMethodOptions;
598 Actor mRenderableActor;
601 Actor mBackgroundActor;
602 CallbackBase* mIdleCallback;
604 float mAlignmentOffset;
605 float mScrollAnimationDuration;
607 int mRenderingBackend;
608 bool mHasBeenStaged : 1;
609 bool mScrollAnimationEnabled : 1;
610 bool mScrollBarEnabled : 1;
611 bool mScrollStarted : 1;
612 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
613 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
614 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
615 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
617 //args for cursor PositionChanged event
618 unsigned int mOldPosition;
620 //args for selection changed event
621 uint32_t mOldSelectionStart;
622 uint32_t mOldSelectionEnd;
624 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
626 struct PropertyHandler;
629 * @brief This structure is to connect TextEditor with Accessible functions.
631 class TextEditorAccessible : public EditableTextControlAccessible
634 using EditableTextControlAccessible::EditableTextControlAccessible;
637 * @copydoc Dali::Accessibility::Accessible::GetName()
639 std::string GetName() const override;
643 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
645 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
648 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
650 Toolkit::Text::ControllerPtr GetTextController() const override;
653 * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
655 void RequestTextRelayout() override;
659 } // namespace Internal
661 // Helpers for public-api forwarding methods
663 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
665 DALI_ASSERT_ALWAYS(textEditor);
667 Dali::RefObject& handle = textEditor.GetImplementation();
669 return static_cast<Toolkit::Internal::TextEditor&>(handle);
672 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
674 DALI_ASSERT_ALWAYS(textEditor);
676 const Dali::RefObject& handle = textEditor.GetImplementation();
678 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
681 } // namespace Toolkit
685 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H