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-control-interface.h>
35 #include <dali-toolkit/internal/text/text-controller.h>
36 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
37 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
38 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
39 #include <dali-toolkit/public-api/controls/control-impl.h>
40 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
49 * @brief A control which renders a long text string with styles.
51 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface
55 * @copydoc Dali::Toollkit::TextEditor::New()
57 static Toolkit::TextEditor New();
62 * @brief Called when a property of an object of this type is set.
64 * @param[in] object The object whose property is set.
65 * @param[in] index The property index.
66 * @param[in] value The new property value.
68 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
71 * @brief Called to retrieve a property of an object of this type.
73 * @param[in] object The object whose property is to be retrieved.
74 * @param[in] index The property index.
75 * @return The current value of the property.
77 static Property::Value GetProperty(BaseObject* object, Property::Index index);
80 * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
82 InputMethodContext GetInputMethodContext();
85 * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
87 DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
90 * Connects a callback function with the object's signals.
91 * @param[in] object The object providing the signal.
92 * @param[in] tracker Used to disconnect the signal.
93 * @param[in] signalName The signal to connect to.
94 * @param[in] functor A newly allocated FunctorDelegate.
95 * @return True if the signal was connected.
96 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
98 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
101 * @copydoc TextEditor::TextChangedSignal()
103 Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
106 * @copydoc TextEditor::TextChangedSignal()
108 Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
111 * @copydoc TextEditor::ScrollStateChangedSignal()
113 Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
115 private: // From Control
117 * @copydoc Control::OnInitialize()
119 void OnInitialize() override;
122 * @copydoc Control::OnStyleChange()
124 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
127 * @copydoc Control::GetNaturalSize()
129 Vector3 GetNaturalSize() override;
132 * @copydoc Control::GetHeightForWidth()
134 float GetHeightForWidth(float width) override;
137 * @copydoc Control::OnInitialize()
139 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
142 * @copydoc Control::OnKeyInputFocusGained()
144 void OnKeyInputFocusGained() override;
147 * @copydoc Control::OnKeyInputFocusLost()
149 void OnKeyInputFocusLost() override;
152 * @copydoc Control::OnAccessibilityActivated()
154 bool OnAccessibilityActivated() override;
157 * @copydoc Control::OnTap()
159 void OnTap(const TapGesture& tap) override;
162 * @copydoc Control::OnPan()
164 void OnPan(const PanGesture& gesture) override;
167 * @copydoc Control::OnLongPress()
169 void OnLongPress(const LongPressGesture& gesture) override;
172 * @copydoc Control::OnSceneConnection()
174 void OnSceneConnection(int depth) override;
177 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
179 bool OnKeyEvent(const KeyEvent& event) override;
181 // From ControlInterface
184 * @copydoc Text::ControlInterface::RequestTextRelayout()
186 void RequestTextRelayout() override;
188 // From EditableControlInterface
191 * @copydoc Text::EditableControlInterface::TextChanged()
193 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
196 * @copydoc Text::EditableControlInterface::TextDeleted()
198 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
201 * @copydoc Text::EditableControlInterface::CaretMoved()
203 void CaretMoved(unsigned int position) override;
206 * @copydoc Text::EditableControlInterface::TextChanged()
208 void TextChanged() override;
211 * @copydoc Text::EditableControlInterface::MaxLengthReached()
213 void MaxLengthReached() override;
216 * @copydoc Text::EditableControlInterface::InputStyleChanged()
218 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
221 * @copydoc Text::EditableControlInterface::AddDecoration()
223 void AddDecoration(Actor& actor, bool needsClipping) override;
225 // From SelectableControlInterface
228 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
230 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
233 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
235 Uint32Pair GetTextSelectionRange() const override;
238 * @copydoc Text::SelectableControlInterface::SelectWholeText()
240 void SelectWholeText() override;
243 * @copydoc Text::SelectableControlInterface::SelectNone()
245 void SelectNone() override;
248 * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
250 void ScrollBy(Vector2 Scroll);
253 * @brief Get Horizontal scroll position of TextEditor.
255 * @return Horizontal scroll position (in pixels) of TextEditor.
257 float GetHorizontalScrollPosition();
260 * @brief Get Vertical scroll position of TextEditor.
262 * @return Vertical scroll position (in pixels) of TextEditor.
264 float GetVerticalScrollPosition();
267 * @copydoc Text::SelectableControlInterface::GetSelectedText()
269 string GetSelectedText() const override;
272 * @copydoc Text::EditableControlInterface::IsEditable()
274 bool IsEditable() const override;
277 * @copydoc Text::EditableControlInterface::SetEditable()
279 void SetEditable(bool editable) override;
280 Text::ControllerPtr getController();
282 private: // Implementation
284 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
286 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
289 * @brief Callback when Clipboard signals an item should be pasted
290 * @param[in] clipboard handle to Clipboard Event Notifier
292 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
295 * @brief Get a Property Map for the image used for the required Handle Image
296 * @param[out] value the returned image property
297 * @param[in] handleType the type of handle
298 * @param[in] handleImageType the type of image for the given handleType
300 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
303 * @brief Callback when keyboard is shown/hidden.
305 * @param[in] keyboardShown True if keyboard is shown.
307 void KeyboardStatusChanged(bool keyboardShown);
310 * @brief update scroll bar position
312 * If text scroll is occurred, create or update scroll bar position
314 void UpdateScrollBar();
317 * @brief Callback when TextEditor is touched
319 * @param[in] actor TextEditor touched
320 * @param[in] touch Touch information
322 bool OnTouched(Actor actor, const TouchEvent& touch);
325 * @brief Callbacks called on idle.
327 * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
332 * @brief set RenderActor's position with new scrollPosition
334 * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
336 void ApplyScrollPosition();
339 * @brief Callback function for ScrollBar indicator animation finished signal
341 * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
343 void OnScrollIndicatorAnimationFinished(Animation& animation);
346 * Construct a new TextEditor.
351 * A reference counted object may only be deleted by calling Unreference()
353 virtual ~TextEditor();
355 // Undefined copy constructor and assignment operators
356 TextEditor(const TextEditor&);
357 TextEditor& operator=(const TextEditor& rhs);
360 * @brief Resize actor to the given size.
362 * @param[in] actor The actor to be resized.
363 * @param[in] size Size to change.
365 void ResizeActor( Actor& actor, const Vector2& size );
368 * @brief Render view, create and attach actor(s) to this text editor.
370 void RenderText(Text::Controller::UpdateTextType updateTextType);
372 // Connection needed to re-render text, when a text editor returns to the scene.
373 void OnSceneConnect(Dali::Actor actor);
377 Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
378 Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
379 Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
380 Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
382 InputMethodContext mInputMethodContext;
383 Text::ControllerPtr mController;
384 Text::RendererPtr mRenderer;
385 Text::DecoratorPtr mDecorator;
386 Text::TextVerticalScrollerPtr mTextVerticalScroller;
387 Toolkit::Control mStencil;
388 Toolkit::ScrollBar mScrollBar;
389 Dali::Animation mAnimation; ///< Scroll indicator Show/Hide Animation.
390 Dali::TimePeriod mAnimationPeriod;
391 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
393 Actor mRenderableActor;
395 CallbackBase* mIdleCallback;
397 float mAlignmentOffset;
398 float mScrollAnimationDuration;
400 int mRenderingBackend;
401 bool mHasBeenStaged : 1;
402 bool mScrollAnimationEnabled : 1;
403 bool mScrollBarEnabled : 1;
404 bool mScrollStarted : 1;
406 struct AccessibleImpl : public DevelControl::AccessibleImpl,
407 public virtual Dali::Accessibility::Text,
408 public virtual Dali::Accessibility::EditableText
410 using DevelControl::AccessibleImpl::AccessibleImpl;
412 std::string GetName() override;
413 std::string GetText(size_t startOffset, size_t endOffset) override;
414 size_t GetCharacterCount() override;
415 size_t GetCaretOffset() override;
416 bool SetCaretOffset(size_t offset) override;
417 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
418 Accessibility::Range GetSelection(size_t selectionNum) override;
419 bool RemoveSelection(size_t selectionNum) override;
420 bool SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
421 bool CopyText(size_t startPosition, size_t endPosition) override;
422 bool CutText(size_t startPosition, size_t endPosition) override;
423 Accessibility::States CalculateStates() override;
427 } // namespace Internal
429 // Helpers for public-api forwarding methods
431 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
433 DALI_ASSERT_ALWAYS(textEditor);
435 Dali::RefObject& handle = textEditor.GetImplementation();
437 return static_cast<Toolkit::Internal::TextEditor&>(handle);
440 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
442 DALI_ASSERT_ALWAYS(textEditor);
444 const Dali::RefObject& handle = textEditor.GetImplementation();
446 return static_cast<const Toolkit::Internal::TextEditor&>(handle);
449 } // namespace Toolkit
453 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H