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/controls/text-controls/common-text-utils.h>
33 #include <dali-toolkit/internal/text/controller/text-controller.h>
34 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
35 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
36 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
37 #include <dali-toolkit/internal/text/text-control-interface.h>
38 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
39 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
40 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #include <dali-toolkit/public-api/controls/text-controls/text-field.h>
50 * @brief A control which renders a short text string.
52 class TextField : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
56 * @copydoc Dali::Toollkit::TextField::New()
57 * @param[in] additionalBehaviour custom behavior flags for this TextField. Default is CONTROL_BEHAVIOUR_DEFAULT
59 static Toolkit::TextField New(ControlBehaviour additionalBehaviour = ControlBehaviour::CONTROL_BEHAVIOUR_DEFAULT);
64 * @brief Called when a property of an object of this type is set.
66 * @param[in] object The object whose property is set.
67 * @param[in] index The property index.
68 * @param[in] value The new property value.
70 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
73 * @brief Called to retrieve a property of an object of this type.
75 * @param[in] object The object whose property is to be retrieved.
76 * @param[in] index The property index.
77 * @return The current value of the property.
79 static Property::Value GetProperty(BaseObject* object, Property::Index index);
82 * @copydoc Dali::Toollkit::TextField::GetInputMethodContext()
84 InputMethodContext GetInputMethodContext();
87 * Connects a callback function with the object's signals.
88 * @param[in] object The object providing the signal.
89 * @param[in] tracker Used to disconnect the signal.
90 * @param[in] signalName The signal to connect to.
91 * @param[in] functor A newly allocated FunctorDelegate.
92 * @return True if the signal was connected.
93 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
95 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
98 * @brief Gets text controller
100 * @return The text controller
102 Text::ControllerPtr GetTextController();
105 * @copydoc TextField::TextChangedSignal()
107 Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
110 * @copydoc TextField::MaxLengthReachedSignal()
112 Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
115 * @copydoc TextField::TextChangedSignal()
117 Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
120 * @copydoc TextField::AnchorClickedSignal()
122 DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
125 * @copydoc TextField::CursorPositionChangedSignal()
127 DevelTextField::CursorPositionChangedSignalType& CursorPositionChangedSignal();
130 * @copydoc TextField::InputFilteredSignal()
132 DevelTextField::InputFilteredSignalType& InputFilteredSignal();
135 * @copydoc TextField::SelectionChangedSignal()
137 DevelTextField::SelectionChangedSignalType& SelectionChangedSignal();
140 * @copydoc TextField::SelectionClearedSignal()
142 DevelTextField::SelectionClearedSignalType& SelectionClearedSignal();
145 * @copydoc TextField::SelectionStartedSignal()
147 DevelTextField::SelectionStartedSignalType& SelectionStartedSignal();
149 private: // From Control
151 * @copydoc Control::OnInitialize()
153 void OnInitialize() override;
156 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
158 DevelControl::ControlAccessible* CreateAccessibleObject() override;
161 * @copydoc Control::OnStyleChange()
163 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
166 * @copydoc Control::GetNaturalSize()
168 Vector3 GetNaturalSize() override;
171 * @copydoc Control::GetHeightForWidth()
173 float GetHeightForWidth(float width) override;
176 * @copydoc Control::OnInitialize()
178 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
181 * @copydoc Control::OnKeyInputFocusGained()
183 void OnKeyInputFocusGained() override;
186 * @copydoc Control::OnKeyInputFocusLost()
188 void OnKeyInputFocusLost() override;
191 * @copydoc Control::OnAccessibilityActivated()
193 bool OnAccessibilityActivated() override;
196 * @copydoc Control::OnTap()
198 void OnTap(const TapGesture& tap) override;
201 * @copydoc Control::OnPan()
203 void OnPan(const PanGesture& gesture) override;
206 * @copydoc Control::OnLongPress()
208 void OnLongPress(const LongPressGesture& gesture) override;
211 * @copydoc Control::OnSceneConnection()
213 void OnSceneConnection(int depth) override;
216 * @copydoc Control::OnPropertySet()
218 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
221 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
223 bool OnKeyEvent(const KeyEvent& event) override;
225 // From ControlInterface
228 * @copydoc Text::ControlInterface::RequestTextRelayout()
230 void RequestTextRelayout() override;
232 // From EditableControlInterface
235 * @copydoc Text::EditableControlInterface::TextChanged()
237 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
240 * @copydoc Text::EditableControlInterface::TextDeleted()
242 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
245 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
247 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
250 * @copydoc Text::EditableControlInterface::TextChanged()
252 void TextChanged(bool immediate) override;
255 * @copydoc Text::EditableControlInterface::MaxLengthReached()
257 void MaxLengthReached() override;
260 * @copydoc Text::EditableControlInterface::InputStyleChanged()
262 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
265 * @copydoc Text::SelectableControlInterface::SelectionChanged()
267 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
270 * @copydoc Text::EditableControlInterface::AddDecoration()
272 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
275 * @copydoc Text::EditableControlInterface::InputFiltered()
277 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
280 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
282 void GetControlBackgroundColor(Vector4& color) const override;
284 // From SelectableControlInterface
287 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
289 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
292 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
294 Uint32Pair GetTextSelectionRange() const override;
297 * @copydoc Text::SelectableControlInterface::SelectWholeText()
299 void SelectWholeText() override;
302 * @copydoc Text::SelectableControlInterface::SelectNone()
304 void SelectNone() override;
307 * @copydoc Text::SelectableControlInterface::SelectText()
309 void SelectText(const uint32_t start, const uint32_t end) override;
312 * @copydoc Text::SelectableControlInterface::GetSelectedText()
314 string GetSelectedText() const override;
317 * @copydoc Text::EditableControlInterface::IsEditable()
319 bool IsEditable() const override;
322 * @copydoc Text::EditableControlInterface::SetEditable()
324 void SetEditable(bool editable) override;
327 * @copydoc Dali::EditableControlInterface::CopyText()
329 string CopyText() override;
332 * @copydoc Dali::EditableControlInterface::CutText()
334 string CutText() override;
337 * @copydoc Text::EditableControlInterface::PasteText()
339 void PasteText() override;
341 // From AnchorControlInterface
344 * @copydoc Text::AnchorControlInterface::AnchorClicked()
346 void AnchorClicked(const std::string& href) override;
349 * @brief Get the rendered size of a specific text range.
350 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
351 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
353 * @param[in] startIndex start index of the text requested to calculate size for.
354 * @param[in] endIndex end index(included) of the text requested to calculate size for.
355 * @return list of sizes of the reuested text.
357 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
360 * @brief Get the top/left rendered position of a specific text range.
361 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
362 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
364 * @param[in] startIndex start index of the text requested to get position to.
365 * @param[in] endIndex end index(included) of the text requested to get position to.
366 * @return list of positions of the requested text.
368 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
371 * @brief Get the line bounding rectangle.
372 * if the requested index is out of range or the line is not yet rendered, a rect of {0, 0, 0, 0} is returned.
374 * @param[in] lineIndex line index to which we want to calculate the geometry for.
375 * @return bounding rectangle.
377 Rect<float> GetLineBoundingRectangle(const uint32_t lineIndex) const;
380 * @brief Get the character bounding rectangle.
381 * If the text is not yet rendered or the index > text.Count(); a rect of {0, 0, 0, 0} is returned.
383 * @param[in] charIndex character index to which we want to calculate the geometry for.
384 * @return bounding rectangle.
386 Rect<float> GetCharacterBoundingRectangle(const uint32_t charIndex) const;
389 * @brief Set the @p spannedText into current textField
390 * the spanned text contains content (text) and format (spans with ranges)
391 * the text is copied into text-controller and the spans are applied on ranges
393 * @param[in] spannedText the text with spans.
395 void SetSpannedText(const Text::Spanned& spannedText);
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 Enable or disable clipping.
420 void EnableClipping();
423 * @brief Callback when keyboard is shown/hidden.
425 * @param[in] keyboardShown True if keyboard is shown.
427 void KeyboardStatusChanged(bool keyboardShown);
430 * @brief Callback when TextField is touched
432 * @param[in] actor TextField touched
433 * @param[in] touch Touch information
435 bool OnTouched(Actor actor, const TouchEvent& touch);
438 * @brief Callbacks called on idle.
440 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
445 * @brief Emits TextChanged signal.
447 void EmitTextChangedSignal();
450 * @brief Emits CursorPositionChanged signal.
452 void EmitCursorPositionChangedSignal();
455 * @brief Emits SelectionChanged signal.
457 void EmitSelectionChangedSignal();
460 * @brief Emits SelectionCleared signal.
462 void EmitSelectionClearedSignal();
465 * @brief Emits SelectionStarted signal.
467 void EmitSelectionStartedSignal();
470 * @brief Callback function for when the layout is changed.
471 * @param[in] actor The actor whose layoutDirection is changed.
472 * @param[in] type The layoutDirection.
474 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
477 * @brief Add a layer for active or cursor.
478 * @param[in] layer The actor in which to store the layer.
479 * @param[in] actor The new layer to add.
481 void AddLayer(Actor& layer, Actor& actor);
484 * Construct a new TextField.
486 * @param[in] additionalBehaviour additional behaviour flags for this TextField
488 TextField(ControlBehaviour additionalBehaviour);
491 * A reference counted object may only be deleted by calling Unreference()
493 virtual ~TextField();
495 // Undefined copy constructor and assignment operators
496 TextField(const TextField&);
497 TextField& operator=(const TextField& rhs);
500 * @brief Resize actor to the given size.
502 * @param[in] actor The actor to be resized.
503 * @param[in] size Size to change.
505 void ResizeActor(Actor& actor, const Vector2& size);
508 * @brief Render view, create and attach actor(s) to this Text Field.
510 void RenderText(Text::Controller::UpdateTextType updateTextType);
512 // Connection needed to re-render text, when a Text Field returns to the scene.
513 void OnSceneConnect(Dali::Actor actor);
515 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
516 void OnAccessibilityStatusChanged();
520 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
521 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
522 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
523 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
524 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
525 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
526 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
527 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
528 Toolkit::DevelTextField::SelectionStartedSignalType mSelectionStartedSignal;
530 InputMethodContext mInputMethodContext;
531 Text::ControllerPtr mController;
532 Text::RendererPtr mRenderer;
533 Text::DecoratorPtr mDecorator;
534 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
535 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
536 std::vector<Toolkit::TextAnchor> mAnchorActors;
537 Dali::InputMethodOptions mInputMethodOptions;
539 Actor mRenderableActor;
542 Actor mBackgroundActor;
543 CallbackBase* mIdleCallback;
545 float mAlignmentOffset;
546 int mRenderingBackend;
548 bool mHasBeenStaged : 1;
549 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
550 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
551 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
552 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
554 //args for cursor position changed event
555 unsigned int mOldPosition;
557 //args for selection changed event
558 uint32_t mOldSelectionStart;
559 uint32_t mOldSelectionEnd;
561 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
564 struct PropertyHandler;
567 * @brief This structure is to connect TextField with Accessible functions.
569 class TextFieldAccessible : public EditableTextControlAccessible
572 using EditableTextControlAccessible::EditableTextControlAccessible;
575 * @copydoc Dali::Accessibility::Accessible::GetName()
577 std::string GetName() const override;
581 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
583 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
586 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
588 Toolkit::Text::ControllerPtr GetTextController() const override;
591 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetSubstituteCharacter()
593 std::uint32_t GetSubstituteCharacter() const override;
596 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::IsHiddenInput()
598 bool IsHiddenInput() const override;
601 * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
603 void RequestTextRelayout() override;
607 } // namespace Internal
609 // Helpers for public-api forwarding methods
611 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
613 DALI_ASSERT_ALWAYS(textField);
615 Dali::RefObject& handle = textField.GetImplementation();
617 return static_cast<Toolkit::Internal::TextField&>(handle);
620 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
622 DALI_ASSERT_ALWAYS(textField);
624 const Dali::RefObject& handle = textField.GetImplementation();
626 return static_cast<const Toolkit::Internal::TextField&>(handle);
629 } // namespace Toolkit
633 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H