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/public-api/animation/animation.h>
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>
50 * @brief A control which renders a long text string with styles.
52 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
56 * @copydoc Dali::Toollkit::TextEditor::New()
58 static Toolkit::TextEditor New();
63 * @brief Called when a property of an object of this type is set.
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.
69 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
72 * @brief Called to retrieve a property of an object of this type.
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.
78 static Property::Value GetProperty(BaseObject* object, Property::Index index);
81 * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
83 InputMethodContext GetInputMethodContext();
86 * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
88 DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
91 * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
93 DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
96 * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
98 DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
101 * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
103 DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
106 * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
108 DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
111 * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
113 DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
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.
124 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
127 * @brief Gets text controller
129 * @return The text controller
131 Text::ControllerPtr GetTextController();
134 * @copydoc TextEditor::TextChangedSignal()
136 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
139 * @copydoc TextEditor::TextChangedSignal()
141 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
144 * @copydoc TextEditor::ScrollStateChangedSignal()
146 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
148 private: // From Control
150 * @copydoc Control::OnInitialize()
152 void OnInitialize() override;
155 * @copydoc Control::OnStyleChange()
157 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
160 * @copydoc Control::GetNaturalSize()
162 Vector3 GetNaturalSize() override;
165 * @copydoc Control::GetHeightForWidth()
167 float GetHeightForWidth(float width) override;
170 * @copydoc Control::OnInitialize()
172 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
175 * @copydoc Control::OnKeyInputFocusGained()
177 void OnKeyInputFocusGained() override;
180 * @copydoc Control::OnKeyInputFocusLost()
182 void OnKeyInputFocusLost() override;
185 * @copydoc Control::OnAccessibilityActivated()
187 bool OnAccessibilityActivated() override;
190 * @copydoc Control::OnTap()
192 void OnTap(const TapGesture& tap) override;
195 * @copydoc Control::OnPan()
197 void OnPan(const PanGesture& gesture) override;
200 * @copydoc Control::OnLongPress()
202 void OnLongPress(const LongPressGesture& gesture) override;
205 * @copydoc Control::OnSceneConnection()
207 void OnSceneConnection(int depth) override;
210 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
212 bool OnKeyEvent(const KeyEvent& event) override;
214 // From ControlInterface
217 * @copydoc Text::ControlInterface::RequestTextRelayout()
219 void RequestTextRelayout() override;
221 // From EditableControlInterface
224 * @copydoc Text::EditableControlInterface::TextChanged()
226 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
229 * @copydoc Text::EditableControlInterface::TextDeleted()
231 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
234 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
236 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
239 * @copydoc Text::EditableControlInterface::TextChanged()
241 void TextChanged(bool immediate) override;
244 * @copydoc Text::EditableControlInterface::MaxLengthReached()
246 void MaxLengthReached() override;
249 * @copydoc Text::EditableControlInterface::InputStyleChanged()
251 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
254 * @copydoc Text::SelectableControlInterface::SelectionChanged()
256 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
259 * @copydoc Text::EditableControlInterface::AddDecoration()
261 void AddDecoration(Actor& actor, bool needsClipping) override;
264 * @copydoc Text::EditableControlInterface::InputFiltered()
266 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
269 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
271 void GetControlBackgroundColor(Vector4& color) const override;
273 // From SelectableControlInterface
276 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
278 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
281 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
283 Uint32Pair GetTextSelectionRange() const override;
286 * @copydoc Text::SelectableControlInterface::SelectWholeText()
288 void SelectWholeText() override;
291 * @copydoc Text::SelectableControlInterface::SelectNone()
293 void SelectNone() override;
296 * @copydoc Text::SelectableControlInterface::SelectText()
298 void SelectText(const uint32_t start, const uint32_t end) override;
301 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
303 void ScrollBy(Vector2 Scroll);
306 * @brief Get Horizontal scroll position of TextEditor.
308 * @return Horizontal scroll position (in pixels) of TextEditor.
310 float GetHorizontalScrollPosition();
313 * @brief Get Vertical scroll position of TextEditor.
315 * @return Vertical scroll position (in pixels) of TextEditor.
317 float GetVerticalScrollPosition();
320 * @brief Get the rendered size of a specific text range.
321 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
322 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
324 * @param[in] startIndex start index of the text requested to calculate size for.
325 * @param[in] endIndex end index(included) of the text requested to calculate size for.
326 * @return list of sizes of the reuested text.
328 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
331 * @brief Get the top/left rendered position of a specific text range.
332 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
333 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
335 * @param[in] startIndex start index of the text requested to get position to.
336 * @param[in] endIndex end index(included) of the text requested to get position to.
337 * @return list of positions of the requested text.
339 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
342 * @copydoc Text::SelectableControlInterface::GetSelectedText()
344 string GetSelectedText() const override;
347 * @copydoc Text::EditableControlInterface::IsEditable()
349 bool IsEditable() const override;
352 * @copydoc Text::EditableControlInterface::SetEditable()
354 void SetEditable(bool editable) override;
357 * @copydoc Text::EditableControlInterface::CopyText()
359 string CopyText() override;
362 * @copydoc Text::EditableControlInterface::CutText()
364 string CutText() override;
367 * @copydoc Text::EditableControlInterface::PasteText()
369 void PasteText() override;
371 // From AnchorControlInterface
374 * @copydoc Text::AnchorControlInterface::AnchorClicked()
376 void AnchorClicked(const std::string& href) override;
378 private: // Implementation
380 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
382 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
385 * @brief Callback when Clipboard signals an item should be pasted
386 * @param[in] clipboard handle to Clipboard Event Notifier
388 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
391 * @brief Get a Property Map for the image used for the required Handle Image
392 * @param[out] value the returned image property
393 * @param[in] handleType the type of handle
394 * @param[in] handleImageType the type of image for the given handleType
396 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
399 * @brief Callback when keyboard is shown/hidden.
401 * @param[in] keyboardShown True if keyboard is shown.
403 void KeyboardStatusChanged(bool keyboardShown);
406 * @brief update scroll bar position
408 * If text scroll is occurred, create or update scroll bar position
410 void UpdateScrollBar();
413 * @brief Callback when TextEditor is touched
415 * @param[in] actor TextEditor touched
416 * @param[in] touch Touch information
418 bool OnTouched(Actor actor, const TouchEvent& touch);
421 * @brief Callbacks called on idle.
423 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
428 * @brief Emits CursorPositionChanged signal.
430 void EmitCursorPositionChangedSignal();
433 * @brief Emits TextChanged signal.
435 void EmitTextChangedSignal();
438 * @brief Emits SelectionChanged signal.
440 void EmitSelectionChangedSignal();
443 * @brief Emits SelectionCleared signal.
445 void EmitSelectionClearedSignal();
448 * @brief set RenderActor's position with new scrollPosition
450 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
452 void ApplyScrollPosition();
455 * @brief Callback function for ScrollBar indicator animation finished signal
457 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
459 void OnScrollIndicatorAnimationFinished(Animation& animation);
462 * @brief Callback function for when the layout is changed.
463 * @param[in] actor The actor whose layoutDirection is changed.
464 * @param[in] type The layoutDirection.
466 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
469 * Construct a new TextEditor.
474 * A reference counted object may only be deleted by calling Unreference()
476 virtual ~TextEditor();
478 // Undefined copy constructor and assignment operators
479 TextEditor(const TextEditor&);
480 TextEditor& operator=(const TextEditor& rhs);
483 * @brief Resize actor to the given size.
485 * @param[in] actor The actor to be resized.
486 * @param[in] size Size to change.
488 void ResizeActor(Actor& actor, const Vector2& size);
491 * @brief Render view, create and attach actor(s) to this text editor.
493 void RenderText(Text::Controller::UpdateTextType updateTextType);
495 // Connection needed to re-render text, when a text editor returns to the scene.
496 void OnSceneConnect(Dali::Actor actor);
498 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
499 void OnAccessibilityStatusChanged();
503 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
504 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
505 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
506 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
507 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
508 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
509 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
510 Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal;
511 Toolkit::DevelTextEditor::SelectionClearedSignalType mSelectionClearedSignal;
513 InputMethodContext mInputMethodContext;
514 Text::ControllerPtr mController;
515 Text::RendererPtr mRenderer;
516 Text::DecoratorPtr mDecorator;
517 Text::TextVerticalScrollerPtr mTextVerticalScroller;
518 Toolkit::Control mStencil;
519 Toolkit::ScrollBar mScrollBar;
520 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
521 Dali::TimePeriod mAnimationPeriod;
522 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
523 std::vector<Toolkit::TextAnchor> mAnchorActors;
524 Dali::InputMethodOptions mInputMethodOptions;
526 Actor mRenderableActor;
528 Actor mBackgroundActor;
529 CallbackBase* mIdleCallback;
531 float mAlignmentOffset;
532 float mScrollAnimationDuration;
534 int mRenderingBackend;
535 bool mHasBeenStaged : 1;
536 bool mScrollAnimationEnabled : 1;
537 bool mScrollBarEnabled : 1;
538 bool mScrollStarted : 1;
539 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
540 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
541 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
542 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
544 //args for cursor PositionChanged event
545 unsigned int mOldPosition;
547 //args for selection changed event
548 uint32_t mOldSelectionStart;
549 uint32_t mOldSelectionEnd;
551 struct PropertyHandler;
554 * @brief This structure is to connect TextEditor with Accessible functions.
556 struct AccessibleImpl : public DevelControl::AccessibleImpl,
557 public virtual Dali::Accessibility::Text,
558 public virtual Dali::Accessibility::EditableText,
559 public virtual Dali::Accessibility::Hypertext
561 using DevelControl::AccessibleImpl::AccessibleImpl;
564 * @copydoc Dali::Accessibility::Accessible::GetName()
566 std::string GetName() override;
569 * @copydoc Dali::Accessibility::Text::GetText()
571 std::string GetText(size_t startOffset, size_t endOffset) override;
574 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
576 size_t GetCharacterCount() override;
579 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
581 size_t GetCursorOffset() override;
584 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
586 bool SetCursorOffset(size_t offset) override;
589 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
591 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
594 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
596 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
599 * @copydoc Dali::Accessibility::Text::RemoveSelection()
601 bool RemoveSelection(size_t selectionIndex) override;
604 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
606 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
609 * @copydoc Dali::Accessibility::EditableText::CopyText()
611 bool CopyText(size_t startPosition, size_t endPosition) override;
614 * @copydoc Dali::Accessibility::EditableText::CutText()
616 bool CutText(size_t startPosition, size_t endPosition) override;
619 * @copydoc Dali::Accessibility::Accessible::GetStates()
621 Accessibility::States CalculateStates() override;
624 * @copydoc Dali::Accessibility::EditableText::InsertText()
626 bool InsertText(size_t startPosition, std::string text) override;
629 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
631 bool SetTextContents(std::string newContents) override;
634 * @copydoc Dali::Accessibility::EditableText::DeleteText()
636 bool DeleteText(size_t startPosition, size_t endPosition) override;
639 * @copydoc Dali::Accessibility::Hypertext::GetLink()
641 Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
644 * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
646 int32_t GetLinkIndex(int32_t characterOffset) const override;
649 * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
651 int32_t GetLinkCount() const override;
655 } // namespace Internal
657 // Helpers for public-api forwarding methods
659 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
661 DALI_ASSERT_ALWAYS(textEditor);
663 Dali::RefObject& handle = textEditor.GetImplementation();
665 return static_cast<Toolkit::Internal::TextEditor&>(handle);
668 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
670 DALI_ASSERT_ALWAYS(textEditor);
672 const Dali::RefObject& handle = textEditor.GetImplementation();
674 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
677 } // namespace Toolkit
681 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H