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/text/decorator/text-decorator.h>
36 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
37 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
38 #include <dali-toolkit/internal/text/text-control-interface.h>
39 #include <dali-toolkit/internal/text/text-controller.h>
40 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
41 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
42 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
43 #include <dali-toolkit/public-api/controls/control-impl.h>
44 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
53 * @brief A control which renders a long text string with styles.
55 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
59 * @copydoc Dali::Toollkit::TextEditor::New()
61 static Toolkit::TextEditor New();
66 * @brief Called when a property of an object of this type is set.
68 * @param[in] object The object whose property is set.
69 * @param[in] index The property index.
70 * @param[in] value The new property value.
72 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
75 * @brief Called to retrieve a property of an object of this type.
77 * @param[in] object The object whose property is to be retrieved.
78 * @param[in] index The property index.
79 * @return The current value of the property.
81 static Property::Value GetProperty(BaseObject* object, Property::Index index);
84 * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
86 InputMethodContext GetInputMethodContext();
89 * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
91 DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
94 * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
96 DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
99 * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
101 DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
104 * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
106 DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
109 * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
111 DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
114 * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
116 DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
119 * @copydoc Dali::Toollkit::TextEditor::SelectionStartedSignal()
121 DevelTextEditor::SelectionStartedSignalType& SelectionStartedSignal();
124 * Connects a callback function with the object's signals.
125 * @param[in] object The object providing the signal.
126 * @param[in] tracker Used to disconnect the signal.
127 * @param[in] signalName The signal to connect to.
128 * @param[in] functor A newly allocated FunctorDelegate.
129 * @return True if the signal was connected.
130 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
132 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
135 * @brief Gets text controller
137 * @return The text controller
139 Text::ControllerPtr GetTextController();
142 * @copydoc TextEditor::TextChangedSignal()
144 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
147 * @copydoc TextEditor::TextChangedSignal()
149 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
152 * @copydoc TextEditor::ScrollStateChangedSignal()
154 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
156 private: // From Control
158 * @copydoc Control::OnInitialize()
160 void OnInitialize() override;
163 * @copydoc Control::OnStyleChange()
165 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
168 * @copydoc Control::GetNaturalSize()
170 Vector3 GetNaturalSize() override;
173 * @copydoc Control::GetHeightForWidth()
175 float GetHeightForWidth(float width) override;
178 * @copydoc Control::OnInitialize()
180 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
183 * @copydoc Control::OnKeyInputFocusGained()
185 void OnKeyInputFocusGained() override;
188 * @copydoc Control::OnKeyInputFocusLost()
190 void OnKeyInputFocusLost() override;
193 * @copydoc Control::OnAccessibilityActivated()
195 bool OnAccessibilityActivated() override;
198 * @copydoc Control::OnTap()
200 void OnTap(const TapGesture& tap) override;
203 * @copydoc Control::OnPan()
205 void OnPan(const PanGesture& gesture) override;
208 * @copydoc Control::OnLongPress()
210 void OnLongPress(const LongPressGesture& gesture) override;
213 * @copydoc Control::OnSceneConnection()
215 void OnSceneConnection(int depth) override;
218 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
220 bool OnKeyEvent(const KeyEvent& event) override;
222 // From ControlInterface
225 * @copydoc Text::ControlInterface::RequestTextRelayout()
227 void RequestTextRelayout() override;
229 // From EditableControlInterface
232 * @copydoc Text::EditableControlInterface::TextChanged()
234 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
237 * @copydoc Text::EditableControlInterface::TextDeleted()
239 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
242 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
244 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
247 * @copydoc Text::EditableControlInterface::TextChanged()
249 void TextChanged(bool immediate) override;
252 * @copydoc Text::EditableControlInterface::MaxLengthReached()
254 void MaxLengthReached() override;
257 * @copydoc Text::EditableControlInterface::InputStyleChanged()
259 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
262 * @copydoc Text::SelectableControlInterface::SelectionChanged()
264 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
267 * @copydoc Text::EditableControlInterface::AddDecoration()
269 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
272 * @copydoc Text::EditableControlInterface::InputFiltered()
274 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
277 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
279 void GetControlBackgroundColor(Vector4& color) const override;
281 // From SelectableControlInterface
284 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
286 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
289 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
291 Uint32Pair GetTextSelectionRange() const override;
294 * @copydoc Text::SelectableControlInterface::SelectWholeText()
296 void SelectWholeText() override;
299 * @copydoc Text::SelectableControlInterface::SelectNone()
301 void SelectNone() override;
304 * @copydoc Text::SelectableControlInterface::SelectText()
306 void SelectText(const uint32_t start, const uint32_t end) override;
309 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
311 void ScrollBy(Vector2 Scroll);
314 * @brief Get Horizontal scroll position of TextEditor.
316 * @return Horizontal scroll position (in pixels) of TextEditor.
318 float GetHorizontalScrollPosition();
321 * @brief Get Vertical scroll position of TextEditor.
323 * @return Vertical scroll position (in pixels) of TextEditor.
325 float GetVerticalScrollPosition();
328 * @brief Get the rendered size of a specific text range.
329 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
330 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
332 * @param[in] startIndex start index of the text requested to calculate size for.
333 * @param[in] endIndex end index(included) of the text requested to calculate size for.
334 * @return list of sizes of the reuested text.
336 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
339 * @brief Get the top/left rendered position of a specific text range.
340 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
341 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
343 * @param[in] startIndex start index of the text requested to get position to.
344 * @param[in] endIndex end index(included) of the text requested to get position to.
345 * @return list of positions of the requested text.
347 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
350 * @copydoc Text::SelectableControlInterface::GetSelectedText()
352 string GetSelectedText() const override;
355 * @copydoc Text::EditableControlInterface::IsEditable()
357 bool IsEditable() const override;
360 * @copydoc Text::EditableControlInterface::SetEditable()
362 void SetEditable(bool editable) override;
365 * @copydoc Text::EditableControlInterface::CopyText()
367 string CopyText() override;
370 * @copydoc Text::EditableControlInterface::CutText()
372 string CutText() override;
375 * @copydoc Text::EditableControlInterface::PasteText()
377 void PasteText() override;
379 // From AnchorControlInterface
382 * @copydoc Text::AnchorControlInterface::AnchorClicked()
384 void AnchorClicked(const std::string& href) override;
386 private: // Implementation
388 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
390 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
393 * @brief Callback when Clipboard signals an item should be pasted
394 * @param[in] clipboard handle to Clipboard Event Notifier
396 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
399 * @brief Get a Property Map for the image used for the required Handle Image
400 * @param[out] value the returned image property
401 * @param[in] handleType the type of handle
402 * @param[in] handleImageType the type of image for the given handleType
404 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
407 * @brief Callback when keyboard is shown/hidden.
409 * @param[in] keyboardShown True if keyboard is shown.
411 void KeyboardStatusChanged(bool keyboardShown);
414 * @brief update scroll bar position
416 * If text scroll is occurred, create or update scroll bar position
418 void UpdateScrollBar();
421 * @brief Callback when TextEditor is touched
423 * @param[in] actor TextEditor touched
424 * @param[in] touch Touch information
426 bool OnTouched(Actor actor, const TouchEvent& touch);
429 * @brief Callbacks called on idle.
431 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
436 * @brief Emits CursorPositionChanged signal.
438 void EmitCursorPositionChangedSignal();
441 * @brief Emits TextChanged signal.
443 void EmitTextChangedSignal();
446 * @brief Emits SelectionChanged signal.
448 void EmitSelectionChangedSignal();
451 * @brief Emits SelectionCleared signal.
453 void EmitSelectionClearedSignal();
456 * @brief Emits SelectionStarted signal.
458 void EmitSelectionStartedSignal();
461 * @brief set RenderActor's position with new scrollPosition
463 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
465 void ApplyScrollPosition();
468 * @brief Callback function for ScrollBar indicator animation finished signal
470 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
472 void OnScrollIndicatorAnimationFinished(Animation& animation);
475 * @brief Callback function for when the layout is changed.
476 * @param[in] actor The actor whose layoutDirection is changed.
477 * @param[in] type The layoutDirection.
479 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
482 * @brief Add a layer for active or cursor.
483 * @param[in] layer The actor in which to store the layer.
484 * @param[in] actor The new layer to add.
486 void AddLayer(Actor& layer, Actor& actor);
489 * Construct a new TextEditor.
494 * A reference counted object may only be deleted by calling Unreference()
496 virtual ~TextEditor();
498 // Undefined copy constructor and assignment operators
499 TextEditor(const TextEditor&);
500 TextEditor& operator=(const TextEditor& rhs);
503 * @brief Resize actor to the given size.
505 * @param[in] actor The actor to be resized.
506 * @param[in] size Size to change.
508 void ResizeActor(Actor& actor, const Vector2& size);
511 * @brief Render view, create and attach actor(s) to this text editor.
513 void RenderText(Text::Controller::UpdateTextType updateTextType);
515 // Connection needed to re-render text, when a text editor returns to the scene.
516 void OnSceneConnect(Dali::Actor actor);
518 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
519 void OnAccessibilityStatusChanged();
523 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
524 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
525 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
526 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
527 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
528 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
529 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
530 Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal;
531 Toolkit::DevelTextEditor::SelectionClearedSignalType mSelectionClearedSignal;
532 Toolkit::DevelTextEditor::SelectionStartedSignalType mSelectionStartedSignal;
534 InputMethodContext mInputMethodContext;
535 Text::ControllerPtr mController;
536 Text::RendererPtr mRenderer;
537 Text::DecoratorPtr mDecorator;
538 Text::TextVerticalScrollerPtr mTextVerticalScroller;
539 Toolkit::Control mStencil;
540 Toolkit::ScrollBar mScrollBar;
541 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
542 Dali::TimePeriod mAnimationPeriod;
543 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
544 std::vector<Toolkit::TextAnchor> mAnchorActors;
545 Dali::InputMethodOptions mInputMethodOptions;
547 Actor mRenderableActor;
550 Actor mBackgroundActor;
551 CallbackBase* mIdleCallback;
553 float mAlignmentOffset;
554 float mScrollAnimationDuration;
556 int mRenderingBackend;
557 bool mHasBeenStaged : 1;
558 bool mScrollAnimationEnabled : 1;
559 bool mScrollBarEnabled : 1;
560 bool mScrollStarted : 1;
561 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
562 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
563 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
564 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
566 //args for cursor PositionChanged event
567 unsigned int mOldPosition;
569 //args for selection changed event
570 uint32_t mOldSelectionStart;
571 uint32_t mOldSelectionEnd;
573 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
575 struct PropertyHandler;
578 * @brief This structure is to connect TextEditor with Accessible functions.
580 struct AccessibleImpl : public DevelControl::ControlAccessible,
581 public virtual Dali::Accessibility::EditableText,
582 public virtual Dali::Accessibility::Hypertext
584 using DevelControl::ControlAccessible::ControlAccessible;
587 * @copydoc Dali::Accessibility::Accessible::GetName()
589 std::string GetName() const override;
592 * @copydoc Dali::Accessibility::Text::GetText()
594 std::string GetText(size_t startOffset, size_t endOffset) const override;
597 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
599 size_t GetCharacterCount() const override;
602 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
604 size_t GetCursorOffset() const override;
607 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
609 bool SetCursorOffset(size_t offset) override;
612 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
614 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) const override;
617 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
619 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) const override;
622 * @copydoc Dali::Accessibility::Text::RemoveSelection()
624 bool RemoveSelection(size_t selectionIndex) override;
627 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
629 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
632 * @copydoc Dali::Accessibility::Text::GetRangeExtents()
634 Rect<> GetRangeExtents(size_t startOffset, size_t endOffset, Accessibility::CoordinateType type) override;
637 * @copydoc Dali::Accessibility::EditableText::CopyText()
639 bool CopyText(size_t startPosition, size_t endPosition) override;
642 * @copydoc Dali::Accessibility::EditableText::CutText()
644 bool CutText(size_t startPosition, size_t endPosition) override;
647 * @copydoc Dali::Accessibility::Accessible::GetStates()
649 Accessibility::States CalculateStates() override;
652 * @copydoc Dali::Accessibility::EditableText::InsertText()
654 bool InsertText(size_t startPosition, std::string text) override;
657 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
659 bool SetTextContents(std::string newContents) override;
662 * @copydoc Dali::Accessibility::EditableText::DeleteText()
664 bool DeleteText(size_t startPosition, size_t endPosition) override;
667 * @copydoc Dali::Accessibility::Hypertext::GetLink()
669 Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
672 * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
674 int32_t GetLinkIndex(int32_t characterOffset) const override;
677 * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
679 int32_t GetLinkCount() const override;
683 } // namespace Internal
685 // Helpers for public-api forwarding methods
687 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
689 DALI_ASSERT_ALWAYS(textEditor);
691 Dali::RefObject& handle = textEditor.GetImplementation();
693 return static_cast<Toolkit::Internal::TextEditor&>(handle);
696 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
698 DALI_ASSERT_ALWAYS(textEditor);
700 const Dali::RefObject& handle = textEditor.GetImplementation();
702 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
705 } // namespace Toolkit
709 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H