1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_FIELD_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/clipboard-event-notifier.h>
23 #include <dali/devel-api/adaptor-framework/input-method-context.h>
24 #include <dali/devel-api/atspi-interfaces/editable-text.h>
25 #include <dali/devel-api/atspi-interfaces/hypertext.h>
26 #include <dali/devel-api/atspi-interfaces/text.h>
29 #include <dali-toolkit/devel-api/controls/control-devel.h>
30 #include <dali-toolkit/devel-api/controls/text-controls/text-field-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/public-api/controls/control-impl.h>
40 #include <dali-toolkit/public-api/controls/text-controls/text-field.h>
49 * @brief A control which renders a short text string.
51 class TextField : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
55 * @copydoc Dali::Toollkit::TextField::New()
57 static Toolkit::TextField 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::TextField::GetInputMethodContext()
82 InputMethodContext GetInputMethodContext();
85 * Connects a callback function with the object's signals.
86 * @param[in] object The object providing the signal.
87 * @param[in] tracker Used to disconnect the signal.
88 * @param[in] signalName The signal to connect to.
89 * @param[in] functor A newly allocated FunctorDelegate.
90 * @return True if the signal was connected.
91 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
93 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
96 * @brief Gets text controller
98 * @return The text controller
100 Text::ControllerPtr GetTextController();
103 * @copydoc TextField::TextChangedSignal()
105 Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
108 * @copydoc TextField::MaxLengthReachedSignal()
110 Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
113 * @copydoc TextField::TextChangedSignal()
115 Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
118 * @copydoc TextField::AnchorClickedSignal()
120 DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
123 * @copydoc TextField::CursorPositionChangedSignal()
125 DevelTextField::CursorPositionChangedSignalType& CursorPositionChangedSignal();
128 * @copydoc TextField::InputFilteredSignal()
130 DevelTextField::InputFilteredSignalType& InputFilteredSignal();
133 * @copydoc TextField::SelectionChangedSignal()
135 DevelTextField::SelectionChangedSignalType& SelectionChangedSignal();
138 * @copydoc TextField::SelectionClearedSignal()
140 DevelTextField::SelectionClearedSignalType& SelectionClearedSignal();
143 * @copydoc TextField::SelectionStartedSignal()
145 DevelTextField::SelectionStartedSignalType& SelectionStartedSignal();
147 private: // From Control
149 * @copydoc Control::OnInitialize()
151 void OnInitialize() override;
154 * @copydoc Control::OnStyleChange()
156 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
159 * @copydoc Control::GetNaturalSize()
161 Vector3 GetNaturalSize() override;
164 * @copydoc Control::GetHeightForWidth()
166 float GetHeightForWidth(float width) override;
169 * @copydoc Control::OnInitialize()
171 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
174 * @copydoc Control::OnKeyInputFocusGained()
176 void OnKeyInputFocusGained() override;
179 * @copydoc Control::OnKeyInputFocusLost()
181 void OnKeyInputFocusLost() override;
184 * @copydoc Control::OnAccessibilityActivated()
186 bool OnAccessibilityActivated() override;
189 * @copydoc Control::OnTap()
191 void OnTap(const TapGesture& tap) override;
194 * @copydoc Control::OnPan()
196 void OnPan(const PanGesture& gesture) override;
199 * @copydoc Control::OnLongPress()
201 void OnLongPress(const LongPressGesture& gesture) override;
204 * @copydoc Control::OnSceneConnection()
206 void OnSceneConnection(int depth) override;
209 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
211 bool OnKeyEvent(const KeyEvent& event) override;
213 // From ControlInterface
216 * @copydoc Text::ControlInterface::RequestTextRelayout()
218 void RequestTextRelayout() override;
220 // From EditableControlInterface
223 * @copydoc Text::EditableControlInterface::TextChanged()
225 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
228 * @copydoc Text::EditableControlInterface::TextDeleted()
230 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
233 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
235 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
238 * @copydoc Text::EditableControlInterface::TextChanged()
240 void TextChanged(bool immediate) override;
243 * @copydoc Text::EditableControlInterface::MaxLengthReached()
245 void MaxLengthReached() override;
248 * @copydoc Text::EditableControlInterface::InputStyleChanged()
250 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
253 * @copydoc Text::SelectableControlInterface::SelectionChanged()
255 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
258 * @copydoc Text::EditableControlInterface::AddDecoration()
260 void AddDecoration(Actor& actor, bool needsClipping) override;
263 * @copydoc Text::EditableControlInterface::InputFiltered()
265 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
268 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
270 void GetControlBackgroundColor(Vector4& color) const override;
272 // From SelectableControlInterface
275 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
277 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
280 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
282 Uint32Pair GetTextSelectionRange() const override;
285 * @copydoc Text::SelectableControlInterface::SelectWholeText()
287 void SelectWholeText() override;
290 * @copydoc Text::SelectableControlInterface::SelectNone()
292 void SelectNone() override;
295 * @copydoc Text::SelectableControlInterface::SelectText()
297 void SelectText(const uint32_t start, const uint32_t end) override;
300 * @copydoc Text::SelectableControlInterface::GetSelectedText()
302 string GetSelectedText() const override;
305 * @copydoc Text::EditableControlInterface::IsEditable()
307 bool IsEditable() const override;
310 * @copydoc Text::EditableControlInterface::SetEditable()
312 void SetEditable(bool editable) override;
315 * @copydoc Dali::EditableControlInterface::CopyText()
317 string CopyText() override;
320 * @copydoc Dali::EditableControlInterface::CutText()
322 string CutText() override;
325 * @copydoc Text::EditableControlInterface::PasteText()
327 void PasteText() override;
329 // From AnchorControlInterface
332 * @copydoc Text::AnchorControlInterface::AnchorClicked()
334 void AnchorClicked(const std::string& href) override;
337 * @brief Get the rendered size of a specific text range.
338 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
339 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
341 * @param[in] startIndex start index of the text requested to calculate size for.
342 * @param[in] endIndex end index(included) of the text requested to calculate size for.
343 * @return list of sizes of the reuested text.
345 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
348 * @brief Get the top/left rendered position of a specific text range.
349 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
350 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
352 * @param[in] startIndex start index of the text requested to get position to.
353 * @param[in] endIndex end index(included) of the text requested to get position to.
354 * @return list of positions of the requested text.
356 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
358 private: // Implementation
360 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
362 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
365 * @brief Callback when Clipboard signals an item should be pasted
366 * @param[in] clipboard handle to Clipboard Event Notifier
368 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
371 * @brief Get a Property Map for the image used for the required Handle Image
372 * @param[out] value the returned image property
373 * @param[in] handleType the type of handle
374 * @param[in] handleImageType the type of image for the given handleType
376 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
379 * @brief Enable or disable clipping.
381 void EnableClipping();
384 * @brief Callback when keyboard is shown/hidden.
386 * @param[in] keyboardShown True if keyboard is shown.
388 void KeyboardStatusChanged(bool keyboardShown);
391 * @brief Callback when TextField is touched
393 * @param[in] actor TextField touched
394 * @param[in] touch Touch information
396 bool OnTouched(Actor actor, const TouchEvent& touch);
399 * @brief Callbacks called on idle.
401 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
406 * @brief Emits TextChanged signal.
408 void EmitTextChangedSignal();
411 * @brief Emits CursorPositionChanged signal.
413 void EmitCursorPositionChangedSignal();
416 * @brief Emits SelectionChanged signal.
418 void EmitSelectionChangedSignal();
421 * @brief Emits SelectionCleared signal.
423 void EmitSelectionClearedSignal();
426 * @brief Emits SelectionStarted signal.
428 void EmitSelectionStartedSignal();
431 * @brief Callback function for when the layout is changed.
432 * @param[in] actor The actor whose layoutDirection is changed.
433 * @param[in] type The layoutDirection.
435 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
438 * Construct a new TextField.
443 * A reference counted object may only be deleted by calling Unreference()
445 virtual ~TextField();
447 // Undefined copy constructor and assignment operators
448 TextField(const TextField&);
449 TextField& operator=(const TextField& rhs);
452 * @brief Resize actor to the given size.
454 * @param[in] actor The actor to be resized.
455 * @param[in] size Size to change.
457 void ResizeActor(Actor& actor, const Vector2& size);
460 * @brief Render view, create and attach actor(s) to this Text Field.
462 void RenderText(Text::Controller::UpdateTextType updateTextType);
464 // Connection needed to re-render text, when a Text Field returns to the scene.
465 void OnSceneConnect(Dali::Actor actor);
467 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
468 void OnAccessibilityStatusChanged();
472 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
473 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
474 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
475 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
476 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
477 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
478 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
479 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
480 Toolkit::DevelTextField::SelectionStartedSignalType mSelectionStartedSignal;
482 InputMethodContext mInputMethodContext;
483 Text::ControllerPtr mController;
484 Text::RendererPtr mRenderer;
485 Text::DecoratorPtr mDecorator;
486 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
487 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
488 std::vector<Toolkit::TextAnchor> mAnchorActors;
489 Dali::InputMethodOptions mInputMethodOptions;
491 Actor mRenderableActor;
493 Actor mBackgroundActor;
494 CallbackBase* mIdleCallback;
496 float mAlignmentOffset;
497 int mRenderingBackend;
499 bool mHasBeenStaged : 1;
500 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
501 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
502 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
503 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
505 //args for cursor position changed event
506 unsigned int mOldPosition;
508 //args for selection changed event
509 uint32_t mOldSelectionStart;
510 uint32_t mOldSelectionEnd;
512 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
515 struct PropertyHandler;
518 * @brief This structure is to connect TextField with Accessible functions.
520 struct AccessibleImpl : public DevelControl::ControlAccessible,
521 public virtual Dali::Accessibility::EditableText,
522 public virtual Dali::Accessibility::Hypertext
524 using DevelControl::ControlAccessible::ControlAccessible;
527 * @copydoc Dali::Accessibility::Accessible::GetName()
529 std::string GetName() const override;
532 * @copydoc Dali::Accessibility::Text::GetText()
534 std::string GetText(size_t startOffset, size_t endOffset) const override;
537 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
539 size_t GetCharacterCount() const override;
542 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
544 size_t GetCursorOffset() const override;
547 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
549 bool SetCursorOffset(size_t offset) override;
552 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
554 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) const override;
557 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
559 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) const override;
562 * @copydoc Dali::Accessibility::Text::RemoveSelection()
564 bool RemoveSelection(size_t selectionIndex) override;
567 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
569 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
572 * @copydoc Dali::Accessibility::EditableText::CopyText()
574 bool CopyText(size_t startPosition, size_t endPosition) override;
577 * @copydoc Dali::Accessibility::EditableText::CutText()
579 bool CutText(size_t startPosition, size_t endPosition) override;
582 * @copydoc Dali::Accessibility::Accessible::GetStates()
584 Accessibility::States CalculateStates() override;
587 * @copydoc Dali::Accessibility::EditableText::InsertText()
589 bool InsertText(size_t startPosition, std::string text) override;
592 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
594 bool SetTextContents(std::string newContents) override;
597 * @copydoc Dali::Accessibility::EditableText::DeleteText()
599 bool DeleteText(size_t startPosition, size_t endPosition) override;
602 * @copydoc Dali::Accessibility::Hypertext::GetLink()
604 Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
607 * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
609 int32_t GetLinkIndex(int32_t characterOffset) const override;
612 * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
614 int32_t GetLinkCount() const override;
618 } // namespace Internal
620 // Helpers for public-api forwarding methods
622 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
624 DALI_ASSERT_ALWAYS(textField);
626 Dali::RefObject& handle = textField.GetImplementation();
628 return static_cast<Toolkit::Internal::TextField&>(handle);
631 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
633 DALI_ASSERT_ALWAYS(textField);
635 const Dali::RefObject& handle = textField.GetImplementation();
637 return static_cast<const Toolkit::Internal::TextField&>(handle);
640 } // namespace Toolkit
644 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H