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 * Connects a callback function with the object's signals.
107 * @param[in] object The object providing the signal.
108 * @param[in] tracker Used to disconnect the signal.
109 * @param[in] signalName The signal to connect to.
110 * @param[in] functor A newly allocated FunctorDelegate.
111 * @return True if the signal was connected.
112 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
114 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
117 * @brief Gets text controller
119 * @return The text controller
121 Text::ControllerPtr GetTextController();
124 * @copydoc TextEditor::TextChangedSignal()
126 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
129 * @copydoc TextEditor::TextChangedSignal()
131 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
134 * @copydoc TextEditor::ScrollStateChangedSignal()
136 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
138 private: // From Control
140 * @copydoc Control::OnInitialize()
142 void OnInitialize() override;
145 * @copydoc Control::OnStyleChange()
147 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
150 * @copydoc Control::GetNaturalSize()
152 Vector3 GetNaturalSize() override;
155 * @copydoc Control::GetHeightForWidth()
157 float GetHeightForWidth(float width) override;
160 * @copydoc Control::OnInitialize()
162 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
165 * @copydoc Control::OnKeyInputFocusGained()
167 void OnKeyInputFocusGained() override;
170 * @copydoc Control::OnKeyInputFocusLost()
172 void OnKeyInputFocusLost() override;
175 * @copydoc Control::OnAccessibilityActivated()
177 bool OnAccessibilityActivated() override;
180 * @copydoc Control::OnTap()
182 void OnTap(const TapGesture& tap) override;
185 * @copydoc Control::OnPan()
187 void OnPan(const PanGesture& gesture) override;
190 * @copydoc Control::OnLongPress()
192 void OnLongPress(const LongPressGesture& gesture) override;
195 * @copydoc Control::OnSceneConnection()
197 void OnSceneConnection(int depth) override;
200 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
202 bool OnKeyEvent(const KeyEvent& event) override;
204 // From ControlInterface
207 * @copydoc Text::ControlInterface::RequestTextRelayout()
209 void RequestTextRelayout() override;
211 // From EditableControlInterface
214 * @copydoc Text::EditableControlInterface::TextChanged()
216 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
219 * @copydoc Text::EditableControlInterface::TextDeleted()
221 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
224 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
226 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
229 * @copydoc Text::EditableControlInterface::TextChanged()
231 void TextChanged(bool immediate) override;
234 * @copydoc Text::EditableControlInterface::MaxLengthReached()
236 void MaxLengthReached() override;
239 * @copydoc Text::EditableControlInterface::InputStyleChanged()
241 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
244 * @copydoc Text::EditableControlInterface::AddDecoration()
246 void AddDecoration(Actor& actor, bool needsClipping) override;
249 * @copydoc Text::EditableControlInterface::InputFiltered()
251 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
254 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
256 void GetControlBackgroundColor(Vector4& color) const override;
258 // From SelectableControlInterface
261 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
263 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
266 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
268 Uint32Pair GetTextSelectionRange() const override;
271 * @copydoc Text::SelectableControlInterface::SelectWholeText()
273 void SelectWholeText() override;
276 * @copydoc Text::SelectableControlInterface::SelectNone()
278 void SelectNone() override;
281 * @copydoc Text::SelectableControlInterface::SelectText()
283 void SelectText(const uint32_t start, const uint32_t end) override;
286 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
288 void ScrollBy(Vector2 Scroll);
291 * @brief Get Horizontal scroll position of TextEditor.
293 * @return Horizontal scroll position (in pixels) of TextEditor.
295 float GetHorizontalScrollPosition();
298 * @brief Get Vertical scroll position of TextEditor.
300 * @return Vertical scroll position (in pixels) of TextEditor.
302 float GetVerticalScrollPosition();
305 * @copydoc Text::SelectableControlInterface::GetSelectedText()
307 string GetSelectedText() const override;
310 * @copydoc Text::EditableControlInterface::IsEditable()
312 bool IsEditable() const override;
315 * @copydoc Text::EditableControlInterface::SetEditable()
317 void SetEditable(bool editable) override;
319 // From AnchorControlInterface
322 * @copydoc Text::AnchorControlInterface::AnchorClicked()
324 void AnchorClicked(const std::string& href) override;
326 private: // Implementation
328 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
330 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
333 * @brief Callback when Clipboard signals an item should be pasted
334 * @param[in] clipboard handle to Clipboard Event Notifier
336 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
339 * @brief Get a Property Map for the image used for the required Handle Image
340 * @param[out] value the returned image property
341 * @param[in] handleType the type of handle
342 * @param[in] handleImageType the type of image for the given handleType
344 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
347 * @brief Callback when keyboard is shown/hidden.
349 * @param[in] keyboardShown True if keyboard is shown.
351 void KeyboardStatusChanged(bool keyboardShown);
354 * @brief update scroll bar position
356 * If text scroll is occurred, create or update scroll bar position
358 void UpdateScrollBar();
361 * @brief Callback when TextEditor is touched
363 * @param[in] actor TextEditor touched
364 * @param[in] touch Touch information
366 bool OnTouched(Actor actor, const TouchEvent& touch);
369 * @brief Callbacks called on idle.
371 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
376 * @brief Emits CursorPositionChanged signal.
378 void EmitCursorPositionChangedSignal();
381 * @brief Emits TextChanged signal.
383 void EmitTextChangedSignal();
386 * @brief set RenderActor's position with new scrollPosition
388 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
390 void ApplyScrollPosition();
393 * @brief Callback function for ScrollBar indicator animation finished signal
395 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
397 void OnScrollIndicatorAnimationFinished(Animation& animation);
400 * @brief Callback function for when the layout is changed.
401 * @param[in] actor The actor whose layoutDirection is changed.
402 * @param[in] type The layoutDirection.
404 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
407 * Construct a new TextEditor.
412 * A reference counted object may only be deleted by calling Unreference()
414 virtual ~TextEditor();
416 // Undefined copy constructor and assignment operators
417 TextEditor(const TextEditor&);
418 TextEditor& operator=(const TextEditor& rhs);
421 * @brief Resize actor to the given size.
423 * @param[in] actor The actor to be resized.
424 * @param[in] size Size to change.
426 void ResizeActor(Actor& actor, const Vector2& size);
429 * @brief Render view, create and attach actor(s) to this text editor.
431 void RenderText(Text::Controller::UpdateTextType updateTextType);
433 // Connection needed to re-render text, when a text editor returns to the scene.
434 void OnSceneConnect(Dali::Actor actor);
438 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
439 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
440 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
441 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
442 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
443 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
444 Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
446 InputMethodContext mInputMethodContext;
447 Text::ControllerPtr mController;
448 Text::RendererPtr mRenderer;
449 Text::DecoratorPtr mDecorator;
450 Text::TextVerticalScrollerPtr mTextVerticalScroller;
451 Toolkit::Control mStencil;
452 Toolkit::ScrollBar mScrollBar;
453 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
454 Dali::TimePeriod mAnimationPeriod;
455 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
456 Dali::InputMethodOptions mInputMethodOptions;
458 Actor mRenderableActor;
460 Actor mBackgroundActor;
461 CallbackBase* mIdleCallback;
463 float mAlignmentOffset;
464 float mScrollAnimationDuration;
466 int mRenderingBackend;
467 bool mHasBeenStaged : 1;
468 bool mScrollAnimationEnabled : 1;
469 bool mScrollBarEnabled : 1;
470 bool mScrollStarted : 1;
471 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
472 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
474 //args for cursor PositionChanged event
475 unsigned int mOldPosition;
478 * @brief This structure is to connect TextEditor with Accessible functions.
480 struct AccessibleImpl : public DevelControl::AccessibleImpl,
481 public virtual Dali::Accessibility::Text,
482 public virtual Dali::Accessibility::EditableText
484 using DevelControl::AccessibleImpl::AccessibleImpl;
487 * @copydoc Dali::Accessibility::Accessible::GetName()
489 std::string GetName() override;
492 * @copydoc Dali::Accessibility::Text::GetText()
494 std::string GetText(size_t startOffset, size_t endOffset) override;
497 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
499 size_t GetCharacterCount() override;
502 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
504 size_t GetCursorOffset() override;
507 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
509 bool SetCursorOffset(size_t offset) override;
512 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
514 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
517 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
519 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
522 * @copydoc Dali::Accessibility::Text::RemoveSelection()
524 bool RemoveSelection(size_t selectionIndex) override;
527 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
529 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
532 * @copydoc Dali::Accessibility::EditableText::CopyText()
534 bool CopyText(size_t startPosition, size_t endPosition) override;
537 * @copydoc Dali::Accessibility::EditableText::CutText()
539 bool CutText(size_t startPosition, size_t endPosition) override;
542 * @copydoc Dali::Accessibility::Accessible::GetStates()
544 Accessibility::States CalculateStates() override;
547 * @copydoc Dali::Accessibility::EditableText::InsertText()
549 bool InsertText(size_t startPosition, std::string text) override;
552 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
554 bool SetTextContents(std::string newContents) override;
557 * @copydoc Dali::Accessibility::EditableText::DeleteText()
559 bool DeleteText(size_t startPosition, size_t endPosition) override;
563 } // namespace Internal
565 // Helpers for public-api forwarding methods
567 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
569 DALI_ASSERT_ALWAYS(textEditor);
571 Dali::RefObject& handle = textEditor.GetImplementation();
573 return static_cast<Toolkit::Internal::TextEditor&>(handle);
576 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
578 DALI_ASSERT_ALWAYS(textEditor);
580 const Dali::RefObject& handle = textEditor.GetImplementation();
582 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
585 } // namespace Toolkit
589 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H