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/decorator/text-decorator.h>
37 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
38 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
39 #include <dali-toolkit/internal/text/text-control-interface.h>
40 #include <dali-toolkit/internal/text/text-controller.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()
62 static Toolkit::TextEditor New();
67 * @brief Called when a property of an object of this type is set.
69 * @param[in] object The object whose property is set.
70 * @param[in] index The property index.
71 * @param[in] value The new property value.
73 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
76 * @brief Called to retrieve a property of an object of this type.
78 * @param[in] object The object whose property is to be retrieved.
79 * @param[in] index The property index.
80 * @return The current value of the property.
82 static Property::Value GetProperty(BaseObject* object, Property::Index index);
85 * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
87 InputMethodContext GetInputMethodContext();
90 * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
92 DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
95 * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
97 DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
100 * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
102 DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
105 * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
107 DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
110 * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
112 DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
115 * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
117 DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
120 * @copydoc Dali::Toollkit::TextEditor::SelectionStartedSignal()
122 DevelTextEditor::SelectionStartedSignalType& SelectionStartedSignal();
125 * Connects a callback function with the object's signals.
126 * @param[in] object The object providing the signal.
127 * @param[in] tracker Used to disconnect the signal.
128 * @param[in] signalName The signal to connect to.
129 * @param[in] functor A newly allocated FunctorDelegate.
130 * @return True if the signal was connected.
131 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
133 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
136 * @brief Gets text controller
138 * @return The text controller
140 Text::ControllerPtr GetTextController();
143 * @copydoc TextEditor::TextChangedSignal()
145 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
148 * @copydoc TextEditor::TextChangedSignal()
150 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
153 * @copydoc TextEditor::ScrollStateChangedSignal()
155 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
157 private: // From Control
159 * @copydoc Control::OnInitialize()
161 void OnInitialize() override;
164 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
166 DevelControl::ControlAccessible* CreateAccessibleObject() override;
169 * @copydoc Control::OnStyleChange()
171 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
174 * @copydoc Control::GetNaturalSize()
176 Vector3 GetNaturalSize() override;
179 * @copydoc Control::GetHeightForWidth()
181 float GetHeightForWidth(float width) override;
184 * @copydoc Control::OnInitialize()
186 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
189 * @copydoc Control::OnKeyInputFocusGained()
191 void OnKeyInputFocusGained() override;
194 * @copydoc Control::OnKeyInputFocusLost()
196 void OnKeyInputFocusLost() override;
199 * @copydoc Control::OnAccessibilityActivated()
201 bool OnAccessibilityActivated() override;
204 * @copydoc Control::OnTap()
206 void OnTap(const TapGesture& tap) override;
209 * @copydoc Control::OnPan()
211 void OnPan(const PanGesture& gesture) override;
214 * @copydoc Control::OnLongPress()
216 void OnLongPress(const LongPressGesture& gesture) override;
219 * @copydoc Control::OnSceneConnection()
221 void OnSceneConnection(int depth) override;
224 * @copydoc Control::OnPropertySet()
226 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
229 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
231 bool OnKeyEvent(const KeyEvent& event) override;
233 // From ControlInterface
236 * @copydoc Text::ControlInterface::RequestTextRelayout()
238 void RequestTextRelayout() override;
240 // From EditableControlInterface
243 * @copydoc Text::EditableControlInterface::TextChanged()
245 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
248 * @copydoc Text::EditableControlInterface::TextDeleted()
250 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
253 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
255 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
258 * @copydoc Text::EditableControlInterface::TextChanged()
260 void TextChanged(bool immediate) override;
263 * @copydoc Text::EditableControlInterface::MaxLengthReached()
265 void MaxLengthReached() override;
268 * @copydoc Text::EditableControlInterface::InputStyleChanged()
270 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
273 * @copydoc Text::SelectableControlInterface::SelectionChanged()
275 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
278 * @copydoc Text::EditableControlInterface::AddDecoration()
280 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
283 * @copydoc Text::EditableControlInterface::InputFiltered()
285 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
288 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
290 void GetControlBackgroundColor(Vector4& color) const override;
292 // From SelectableControlInterface
295 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
297 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
300 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
302 Uint32Pair GetTextSelectionRange() const override;
305 * @copydoc Text::SelectableControlInterface::SelectWholeText()
307 void SelectWholeText() override;
310 * @copydoc Text::SelectableControlInterface::SelectNone()
312 void SelectNone() override;
315 * @copydoc Text::SelectableControlInterface::SelectText()
317 void SelectText(const uint32_t start, const uint32_t end) override;
320 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
322 void ScrollBy(Vector2 Scroll);
325 * @brief Get Horizontal scroll position of TextEditor.
327 * @return Horizontal scroll position (in pixels) of TextEditor.
329 float GetHorizontalScrollPosition();
332 * @brief Get Vertical scroll position of TextEditor.
334 * @return Vertical scroll position (in pixels) of TextEditor.
336 float GetVerticalScrollPosition();
339 * @brief Get the rendered size of a specific text range.
340 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
341 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
343 * @param[in] startIndex start index of the text requested to calculate size for.
344 * @param[in] endIndex end index(included) of the text requested to calculate size for.
345 * @return list of sizes of the reuested text.
347 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
350 * @brief Get the top/left rendered position of a specific text range.
351 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
352 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
354 * @param[in] startIndex start index of the text requested to get position to.
355 * @param[in] endIndex end index(included) of the text requested to get position to.
356 * @return list of positions of the requested text.
358 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
361 * @copydoc Text::SelectableControlInterface::GetSelectedText()
363 string GetSelectedText() const override;
366 * @copydoc Text::EditableControlInterface::IsEditable()
368 bool IsEditable() const override;
371 * @copydoc Text::EditableControlInterface::SetEditable()
373 void SetEditable(bool editable) override;
376 * @copydoc Text::EditableControlInterface::CopyText()
378 string CopyText() override;
381 * @copydoc Text::EditableControlInterface::CutText()
383 string CutText() override;
386 * @copydoc Text::EditableControlInterface::PasteText()
388 void PasteText() override;
390 // From AnchorControlInterface
393 * @copydoc Text::AnchorControlInterface::AnchorClicked()
395 void AnchorClicked(const std::string& href) override;
397 private: // Implementation
399 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
401 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
404 * @brief Callback when Clipboard signals an item should be pasted
405 * @param[in] clipboard handle to Clipboard Event Notifier
407 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
410 * @brief Get a Property Map for the image used for the required Handle Image
411 * @param[out] value the returned image property
412 * @param[in] handleType the type of handle
413 * @param[in] handleImageType the type of image for the given handleType
415 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
418 * @brief Callback when keyboard is shown/hidden.
420 * @param[in] keyboardShown True if keyboard is shown.
422 void KeyboardStatusChanged(bool keyboardShown);
425 * @brief update scroll bar position
427 * If text scroll is occurred, create or update scroll bar position
429 void UpdateScrollBar();
432 * @brief Callback when TextEditor is touched
434 * @param[in] actor TextEditor touched
435 * @param[in] touch Touch information
437 bool OnTouched(Actor actor, const TouchEvent& touch);
440 * @brief Callbacks called on idle.
442 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
447 * @brief Emits CursorPositionChanged signal.
449 void EmitCursorPositionChangedSignal();
452 * @brief Emits TextChanged signal.
454 void EmitTextChangedSignal();
457 * @brief Emits SelectionChanged signal.
459 void EmitSelectionChangedSignal();
462 * @brief Emits SelectionCleared signal.
464 void EmitSelectionClearedSignal();
467 * @brief Emits SelectionStarted signal.
469 void EmitSelectionStartedSignal();
472 * @brief set RenderActor's position with new scrollPosition
474 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
476 void ApplyScrollPosition();
479 * @brief Callback function for ScrollBar indicator animation finished signal
481 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
483 void OnScrollIndicatorAnimationFinished(Animation& animation);
486 * @brief Callback function for when the layout is changed.
487 * @param[in] actor The actor whose layoutDirection is changed.
488 * @param[in] type The layoutDirection.
490 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
493 * @brief Add a layer for active or cursor.
494 * @param[in] layer The actor in which to store the layer.
495 * @param[in] actor The new layer to add.
497 void AddLayer(Actor& layer, Actor& actor);
500 * Construct a new TextEditor.
505 * A reference counted object may only be deleted by calling Unreference()
507 virtual ~TextEditor();
509 // Undefined copy constructor and assignment operators
510 TextEditor(const TextEditor&);
511 TextEditor& operator=(const TextEditor& rhs);
514 * @brief Resize actor to the given size.
516 * @param[in] actor The actor to be resized.
517 * @param[in] size Size to change.
519 void ResizeActor(Actor& actor, const Vector2& size);
522 * @brief Render view, create and attach actor(s) to this text editor.
524 void RenderText(Text::Controller::UpdateTextType updateTextType);
526 // Connection needed to re-render text, when a text editor returns to the scene.
527 void OnSceneConnect(Dali::Actor actor);
529 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
530 void OnAccessibilityStatusChanged();
534 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
535 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
536 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
537 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
538 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
539 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
540 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
541 Toolkit::DevelTextEditor::SelectionChangedSignalType mSelectionChangedSignal;
542 Toolkit::DevelTextEditor::SelectionClearedSignalType mSelectionClearedSignal;
543 Toolkit::DevelTextEditor::SelectionStartedSignalType mSelectionStartedSignal;
545 InputMethodContext mInputMethodContext;
546 Text::ControllerPtr mController;
547 Text::RendererPtr mRenderer;
548 Text::DecoratorPtr mDecorator;
549 Text::TextVerticalScrollerPtr mTextVerticalScroller;
550 Toolkit::Control mStencil;
551 Toolkit::ScrollBar mScrollBar;
552 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
553 Dali::TimePeriod mAnimationPeriod;
554 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
555 std::vector<Toolkit::TextAnchor> mAnchorActors;
556 Dali::InputMethodOptions mInputMethodOptions;
558 Actor mRenderableActor;
561 Actor mBackgroundActor;
562 CallbackBase* mIdleCallback;
564 float mAlignmentOffset;
565 float mScrollAnimationDuration;
567 int mRenderingBackend;
568 bool mHasBeenStaged : 1;
569 bool mScrollAnimationEnabled : 1;
570 bool mScrollBarEnabled : 1;
571 bool mScrollStarted : 1;
572 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
573 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
574 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
575 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
577 //args for cursor PositionChanged event
578 unsigned int mOldPosition;
580 //args for selection changed event
581 uint32_t mOldSelectionStart;
582 uint32_t mOldSelectionEnd;
584 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
586 struct PropertyHandler;
589 * @brief This structure is to connect TextEditor with Accessible functions.
591 class TextEditorAccessible : public EditableTextControlAccessible
594 using EditableTextControlAccessible::EditableTextControlAccessible;
597 * @copydoc Dali::Accessibility::Accessible::GetName()
599 std::string GetName() const override;
603 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
605 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
608 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
610 Toolkit::Text::ControllerPtr GetTextController() const override;
613 * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
615 void RequestTextRelayout() override;
619 } // namespace Internal
621 // Helpers for public-api forwarding methods
623 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
625 DALI_ASSERT_ALWAYS(textEditor);
627 Dali::RefObject& handle = textEditor.GetImplementation();
629 return static_cast<Toolkit::Internal::TextEditor&>(handle);
632 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
634 DALI_ASSERT_ALWAYS(textEditor);
636 const Dali::RefObject& handle = textEditor.GetImplementation();
638 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
641 } // namespace Toolkit
645 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H