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::InputFilteredSignal()
98 DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
101 * Connects a callback function with the object's signals.
102 * @param[in] object The object providing the signal.
103 * @param[in] tracker Used to disconnect the signal.
104 * @param[in] signalName The signal to connect to.
105 * @param[in] functor A newly allocated FunctorDelegate.
106 * @return True if the signal was connected.
107 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
109 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
112 * @copydoc TextEditor::TextChangedSignal()
114 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
117 * @copydoc TextEditor::TextChangedSignal()
119 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
122 * @copydoc TextEditor::ScrollStateChangedSignal()
124 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
126 private: // From Control
128 * @copydoc Control::OnInitialize()
130 void OnInitialize() override;
133 * @copydoc Control::OnStyleChange()
135 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
138 * @copydoc Control::GetNaturalSize()
140 Vector3 GetNaturalSize() override;
143 * @copydoc Control::GetHeightForWidth()
145 float GetHeightForWidth(float width) override;
148 * @copydoc Control::OnInitialize()
150 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
153 * @copydoc Control::OnKeyInputFocusGained()
155 void OnKeyInputFocusGained() override;
158 * @copydoc Control::OnKeyInputFocusLost()
160 void OnKeyInputFocusLost() override;
163 * @copydoc Control::OnAccessibilityActivated()
165 bool OnAccessibilityActivated() override;
168 * @copydoc Control::OnTap()
170 void OnTap(const TapGesture& tap) override;
173 * @copydoc Control::OnPan()
175 void OnPan(const PanGesture& gesture) override;
178 * @copydoc Control::OnLongPress()
180 void OnLongPress(const LongPressGesture& gesture) override;
183 * @copydoc Control::OnSceneConnection()
185 void OnSceneConnection(int depth) override;
188 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
190 bool OnKeyEvent(const KeyEvent& event) override;
192 // From ControlInterface
195 * @copydoc Text::ControlInterface::RequestTextRelayout()
197 void RequestTextRelayout() override;
199 // From EditableControlInterface
202 * @copydoc Text::EditableControlInterface::TextChanged()
204 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
207 * @copydoc Text::EditableControlInterface::TextDeleted()
209 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
212 * @copydoc Text::EditableControlInterface::CaretMoved()
214 void CaretMoved(unsigned int position) override;
217 * @copydoc Text::EditableControlInterface::TextChanged()
219 void TextChanged(bool immediate) override;
222 * @copydoc Text::EditableControlInterface::MaxLengthReached()
224 void MaxLengthReached() override;
227 * @copydoc Text::EditableControlInterface::InputStyleChanged()
229 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
232 * @copydoc Text::EditableControlInterface::AddDecoration()
234 void AddDecoration(Actor& actor, bool needsClipping) override;
237 * @copydoc Text::EditableControlInterface::InputFiltered()
239 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
241 // From SelectableControlInterface
244 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
246 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
249 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
251 Uint32Pair GetTextSelectionRange() const override;
254 * @copydoc Text::SelectableControlInterface::SelectWholeText()
256 void SelectWholeText() override;
259 * @copydoc Text::SelectableControlInterface::SelectNone()
261 void SelectNone() override;
264 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
266 void ScrollBy(Vector2 Scroll);
269 * @brief Get Horizontal scroll position of TextEditor.
271 * @return Horizontal scroll position (in pixels) of TextEditor.
273 float GetHorizontalScrollPosition();
276 * @brief Get Vertical scroll position of TextEditor.
278 * @return Vertical scroll position (in pixels) of TextEditor.
280 float GetVerticalScrollPosition();
283 * @copydoc Text::SelectableControlInterface::GetSelectedText()
285 string GetSelectedText() const override;
288 * @copydoc Text::EditableControlInterface::IsEditable()
290 bool IsEditable() const override;
293 * @copydoc Text::EditableControlInterface::SetEditable()
295 void SetEditable(bool editable) override;
297 // From AnchorControlInterface
300 * @copydoc Text::AnchorControlInterface::AnchorClicked()
302 void AnchorClicked(const std::string& href) override;
304 Text::ControllerPtr getController();
306 private: // Implementation
308 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
310 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
313 * @brief Callback when Clipboard signals an item should be pasted
314 * @param[in] clipboard handle to Clipboard Event Notifier
316 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
319 * @brief Get a Property Map for the image used for the required Handle Image
320 * @param[out] value the returned image property
321 * @param[in] handleType the type of handle
322 * @param[in] handleImageType the type of image for the given handleType
324 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
327 * @brief Callback when keyboard is shown/hidden.
329 * @param[in] keyboardShown True if keyboard is shown.
331 void KeyboardStatusChanged(bool keyboardShown);
334 * @brief update scroll bar position
336 * If text scroll is occurred, create or update scroll bar position
338 void UpdateScrollBar();
341 * @brief Callback when TextEditor is touched
343 * @param[in] actor TextEditor touched
344 * @param[in] touch Touch information
346 bool OnTouched(Actor actor, const TouchEvent& touch);
349 * @brief Callbacks called on idle.
351 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
356 * @brief Emits TextChanged signal.
358 void EmitTextChangedSignal();
361 * @brief set RenderActor's position with new scrollPosition
363 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
365 void ApplyScrollPosition();
368 * @brief Callback function for ScrollBar indicator animation finished signal
370 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
372 void OnScrollIndicatorAnimationFinished(Animation& animation);
375 * Construct a new TextEditor.
380 * A reference counted object may only be deleted by calling Unreference()
382 virtual ~TextEditor();
384 // Undefined copy constructor and assignment operators
385 TextEditor(const TextEditor&);
386 TextEditor& operator=(const TextEditor& rhs);
389 * @brief Resize actor to the given size.
391 * @param[in] actor The actor to be resized.
392 * @param[in] size Size to change.
394 void ResizeActor(Actor& actor, const Vector2& size);
397 * @brief Render view, create and attach actor(s) to this text editor.
399 void RenderText(Text::Controller::UpdateTextType updateTextType);
401 // Connection needed to re-render text, when a text editor returns to the scene.
402 void OnSceneConnect(Dali::Actor actor);
406 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
407 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
408 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
409 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
410 Toolkit::DevelTextEditor::AnchorClickedSignalType mAnchorClickedSignal;
411 Toolkit::DevelTextEditor::InputFilteredSignalType mInputFilteredSignal;
413 InputMethodContext mInputMethodContext;
414 Text::ControllerPtr mController;
415 Text::RendererPtr mRenderer;
416 Text::DecoratorPtr mDecorator;
417 Text::TextVerticalScrollerPtr mTextVerticalScroller;
418 Toolkit::Control mStencil;
419 Toolkit::ScrollBar mScrollBar;
420 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
421 Dali::TimePeriod mAnimationPeriod;
422 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
423 Dali::InputMethodOptions mInputMethodOptions;
425 Actor mRenderableActor;
427 Actor mBackgroundActor;
428 CallbackBase* mIdleCallback;
430 float mAlignmentOffset;
431 float mScrollAnimationDuration;
433 int mRenderingBackend;
434 bool mHasBeenStaged : 1;
435 bool mScrollAnimationEnabled : 1;
436 bool mScrollBarEnabled : 1;
437 bool mScrollStarted : 1;
438 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
440 struct AccessibleImpl : public DevelControl::AccessibleImpl,
441 public virtual Dali::Accessibility::Text,
442 public virtual Dali::Accessibility::EditableText
444 using DevelControl::AccessibleImpl::AccessibleImpl;
446 std::string GetName() override;
447 std::string GetText(size_t startOffset, size_t endOffset) override;
448 size_t GetCharacterCount() override;
449 size_t GetCaretOffset() override;
450 bool SetCaretOffset(size_t offset) override;
451 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
452 Accessibility::Range GetSelection(size_t selectionNum) override;
453 bool RemoveSelection(size_t selectionNum) override;
454 bool SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
455 bool CopyText(size_t startPosition, size_t endPosition) override;
456 bool CutText(size_t startPosition, size_t endPosition) override;
457 Accessibility::States CalculateStates() override;
458 bool InsertText(size_t startPosition, std::string text) override;
459 bool SetTextContents(std::string newContents) override;
460 bool DeleteText(size_t startPosition, size_t endPosition) override;
464 } // namespace Internal
466 // Helpers for public-api forwarding methods
468 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
470 DALI_ASSERT_ALWAYS(textEditor);
472 Dali::RefObject& handle = textEditor.GetImplementation();
474 return static_cast<Toolkit::Internal::TextEditor&>(handle);
477 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
479 DALI_ASSERT_ALWAYS(textEditor);
481 const Dali::RefObject& handle = textEditor.GetImplementation();
483 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
486 } // namespace Toolkit
490 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H