1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
5 * Copyright (c) 2021 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 * Connects a callback function with the object's signals.
120 * @param[in] object The object providing the signal.
121 * @param[in] tracker Used to disconnect the signal.
122 * @param[in] signalName The signal to connect to.
123 * @param[in] functor A newly allocated FunctorDelegate.
124 * @return True if the signal was connected.
125 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
127 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
130 * @brief Gets text controller
132 * @return The text controller
134 Text::ControllerPtr GetTextController();
137 * @copydoc TextEditor::TextChangedSignal()
139 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
142 * @copydoc TextEditor::TextChangedSignal()
144 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
147 * @copydoc TextEditor::ScrollStateChangedSignal()
149 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
151 private: // From Control
153 * @copydoc Control::OnInitialize()
155 void OnInitialize() override;
158 * @copydoc Control::OnStyleChange()
160 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
163 * @copydoc Control::GetNaturalSize()
165 Vector3 GetNaturalSize() override;
168 * @copydoc Control::GetHeightForWidth()
170 float GetHeightForWidth(float width) override;
173 * @copydoc Control::OnInitialize()
175 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
178 * @copydoc Control::OnKeyInputFocusGained()
180 void OnKeyInputFocusGained() override;
183 * @copydoc Control::OnKeyInputFocusLost()
185 void OnKeyInputFocusLost() override;
188 * @copydoc Control::OnAccessibilityActivated()
190 bool OnAccessibilityActivated() override;
193 * @copydoc Control::OnTap()
195 void OnTap(const TapGesture& tap) override;
198 * @copydoc Control::OnPan()
200 void OnPan(const PanGesture& gesture) override;
203 * @copydoc Control::OnLongPress()
205 void OnLongPress(const LongPressGesture& gesture) override;
208 * @copydoc Control::OnSceneConnection()
210 void OnSceneConnection(int depth) override;
213 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
215 bool OnKeyEvent(const KeyEvent& event) override;
217 // From ControlInterface
220 * @copydoc Text::ControlInterface::RequestTextRelayout()
222 void RequestTextRelayout() override;
224 // From EditableControlInterface
227 * @copydoc Text::EditableControlInterface::TextChanged()
229 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
232 * @copydoc Text::EditableControlInterface::TextDeleted()
234 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
237 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
239 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
242 * @copydoc Text::EditableControlInterface::TextChanged()
244 void TextChanged(bool immediate) override;
247 * @copydoc Text::EditableControlInterface::MaxLengthReached()
249 void MaxLengthReached() override;
252 * @copydoc Text::EditableControlInterface::InputStyleChanged()
254 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
257 * @copydoc Text::SelectableControlInterface::SelectionChanged()
259 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
262 * @copydoc Text::EditableControlInterface::AddDecoration()
264 void AddDecoration(Actor& actor, bool needsClipping) override;
267 * @copydoc Text::EditableControlInterface::InputFiltered()
269 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
272 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
274 void GetControlBackgroundColor(Vector4& color) const override;
276 // From SelectableControlInterface
279 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
281 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
284 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
286 Uint32Pair GetTextSelectionRange() const override;
289 * @copydoc Text::SelectableControlInterface::SelectWholeText()
291 void SelectWholeText() override;
294 * @copydoc Text::SelectableControlInterface::SelectNone()
296 void SelectNone() override;
299 * @copydoc Text::SelectableControlInterface::SelectText()
301 void SelectText(const uint32_t start, const uint32_t end) override;
304 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
306 void ScrollBy(Vector2 Scroll);
309 * @brief Get Horizontal scroll position of TextEditor.
311 * @return Horizontal scroll position (in pixels) of TextEditor.
313 float GetHorizontalScrollPosition();
316 * @brief Get Vertical scroll position of TextEditor.
318 * @return Vertical scroll position (in pixels) of TextEditor.
320 float GetVerticalScrollPosition();
323 * @brief Get the rendered size of a specific text range.
324 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
325 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
327 * @param[in] startIndex start index of the text requested to calculate size for.
328 * @param[in] endIndex end index(included) of the text requested to calculate size for.
329 * @return list of sizes of the reuested text.
331 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
334 * @brief Get the top/left rendered position of a specific text range.
335 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
336 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
338 * @param[in] startIndex start index of the text requested to get position to.
339 * @param[in] endIndex end index(included) of the text requested to get position to.
340 * @return list of positions of the requested text.
342 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
345 * @copydoc Text::SelectableControlInterface::GetSelectedText()
347 string GetSelectedText() const override;
350 * @copydoc Text::EditableControlInterface::IsEditable()
352 bool IsEditable() const override;
355 * @copydoc Text::EditableControlInterface::SetEditable()
357 void SetEditable(bool editable) override;
360 * @copydoc Text::EditableControlInterface::CopyText()
362 string CopyText() override;
365 * @copydoc Text::EditableControlInterface::CutText()
367 string CutText() override;
370 * @copydoc Text::EditableControlInterface::PasteText()
372 void PasteText() override;
374 // From AnchorControlInterface
377 * @copydoc Text::AnchorControlInterface::AnchorClicked()
379 void AnchorClicked(const std::string& href) override;
381 private: // Implementation
383 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
385 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
388 * @brief Callback when Clipboard signals an item should be pasted
389 * @param[in] clipboard handle to Clipboard Event Notifier
391 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
394 * @brief Get a Property Map for the image used for the required Handle Image
395 * @param[out] value the returned image property
396 * @param[in] handleType the type of handle
397 * @param[in] handleImageType the type of image for the given handleType
399 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
402 * @brief Callback when keyboard is shown/hidden.
404 * @param[in] keyboardShown True if keyboard is shown.
406 void KeyboardStatusChanged(bool keyboardShown);
409 * @brief update scroll bar position
411 * If text scroll is occurred, create or update scroll bar position
413 void UpdateScrollBar();
416 * @brief Callback when TextEditor is touched
418 * @param[in] actor TextEditor touched
419 * @param[in] touch Touch information
421 bool OnTouched(Actor actor, const TouchEvent& touch);
424 * @brief Callbacks called on idle.
426 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
431 * @brief Emits CursorPositionChanged signal.
433 void EmitCursorPositionChangedSignal();
436 * @brief Emits TextChanged signal.
438 void EmitTextChangedSignal();
441 * @brief Emits SelectionChanged signal.
443 void EmitSelectionChangedSignal();
446 * @brief Emits SelectionCleared signal.
448 void EmitSelectionClearedSignal();
451 * @brief set RenderActor's position with new scrollPosition
453 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
455 void ApplyScrollPosition();
458 * @brief Callback function for ScrollBar indicator animation finished signal
460 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
462 void OnScrollIndicatorAnimationFinished(Animation& animation);
465 * @brief Callback function for when the layout is changed.
466 * @param[in] actor The actor whose layoutDirection is changed.
467 * @param[in] type The layoutDirection.
469 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
472 * Construct a new TextEditor.
477 * A reference counted object may only be deleted by calling Unreference()
479 virtual ~TextEditor();
481 // Undefined copy constructor and assignment operators
482 TextEditor(const TextEditor&);
483 TextEditor& operator=(const TextEditor& rhs);
486 * @brief Resize actor to the given size.
488 * @param[in] actor The actor to be resized.
489 * @param[in] size Size to change.
491 void ResizeActor(Actor& actor, const Vector2& size);
494 * @brief Render view, create and attach actor(s) to this text editor.
496 void RenderText(Text::Controller::UpdateTextType updateTextType);
498 // Connection needed to re-render text, when a text editor returns to the scene.
499 void OnSceneConnect(Dali::Actor actor);
501 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
502 void OnAccessibilityStatusChanged();
506 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
507 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
508 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
509 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
510 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
511 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
512 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
513 Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal;
514 Toolkit::DevelTextEditor::SelectionClearedSignalType mSelectionClearedSignal;
516 InputMethodContext mInputMethodContext;
517 Text::ControllerPtr mController;
518 Text::RendererPtr mRenderer;
519 Text::DecoratorPtr mDecorator;
520 Text::TextVerticalScrollerPtr mTextVerticalScroller;
521 Toolkit::Control mStencil;
522 Toolkit::ScrollBar mScrollBar;
523 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
524 Dali::TimePeriod mAnimationPeriod;
525 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
526 std::vector<Toolkit::TextAnchor> mAnchorActors;
527 Dali::InputMethodOptions mInputMethodOptions;
529 Actor mRenderableActor;
531 Actor mBackgroundActor;
532 CallbackBase* mIdleCallback;
534 float mAlignmentOffset;
535 float mScrollAnimationDuration;
537 int mRenderingBackend;
538 bool mHasBeenStaged : 1;
539 bool mScrollAnimationEnabled : 1;
540 bool mScrollBarEnabled : 1;
541 bool mScrollStarted : 1;
542 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
543 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
544 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
545 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
547 //args for cursor PositionChanged event
548 unsigned int mOldPosition;
550 //args for selection changed event
551 uint32_t mOldSelectionStart;
552 uint32_t mOldSelectionEnd;
554 struct PropertyHandler;
557 * @brief This structure is to connect TextEditor with Accessible functions.
559 struct AccessibleImpl : public DevelControl::AccessibleImpl,
560 public virtual Dali::Accessibility::Text,
561 public virtual Dali::Accessibility::EditableText,
562 public virtual Dali::Accessibility::Hypertext
564 using DevelControl::AccessibleImpl::AccessibleImpl;
567 * @copydoc Dali::Accessibility::Accessible::GetName()
569 std::string GetName() override;
572 * @copydoc Dali::Accessibility::Text::GetText()
574 std::string GetText(size_t startOffset, size_t endOffset) override;
577 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
579 size_t GetCharacterCount() override;
582 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
584 size_t GetCursorOffset() override;
587 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
589 bool SetCursorOffset(size_t offset) override;
592 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
594 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
597 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
599 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
602 * @copydoc Dali::Accessibility::Text::RemoveSelection()
604 bool RemoveSelection(size_t selectionIndex) override;
607 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
609 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
612 * @copydoc Dali::Accessibility::EditableText::CopyText()
614 bool CopyText(size_t startPosition, size_t endPosition) override;
617 * @copydoc Dali::Accessibility::EditableText::CutText()
619 bool CutText(size_t startPosition, size_t endPosition) override;
622 * @copydoc Dali::Accessibility::Accessible::GetStates()
624 Accessibility::States CalculateStates() override;
627 * @copydoc Dali::Accessibility::EditableText::InsertText()
629 bool InsertText(size_t startPosition, std::string text) override;
632 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
634 bool SetTextContents(std::string newContents) override;
637 * @copydoc Dali::Accessibility::EditableText::DeleteText()
639 bool DeleteText(size_t startPosition, size_t endPosition) override;
642 * @copydoc Dali::Accessibility::Hypertext::GetLink()
644 Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
647 * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
649 int32_t GetLinkIndex(int32_t characterOffset) const override;
652 * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
654 int32_t GetLinkCount() const override;
658 } // namespace Internal
660 // Helpers for public-api forwarding methods
662 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
664 DALI_ASSERT_ALWAYS(textEditor);
666 Dali::RefObject& handle = textEditor.GetImplementation();
668 return static_cast<Toolkit::Internal::TextEditor&>(handle);
671 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
673 DALI_ASSERT_ALWAYS(textEditor);
675 const Dali::RefObject& handle = textEditor.GetImplementation();
677 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
680 } // namespace Toolkit
684 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H