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()
58 static Toolkit::TextField 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::TextField::GetInputMethodContext()
83 InputMethodContext GetInputMethodContext();
86 * Connects a callback function with the object's signals.
87 * @param[in] object The object providing the signal.
88 * @param[in] tracker Used to disconnect the signal.
89 * @param[in] signalName The signal to connect to.
90 * @param[in] functor A newly allocated FunctorDelegate.
91 * @return True if the signal was connected.
92 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
94 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
97 * @brief Gets text controller
99 * @return The text controller
101 Text::ControllerPtr GetTextController();
104 * @copydoc TextField::TextChangedSignal()
106 Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
109 * @copydoc TextField::MaxLengthReachedSignal()
111 Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
114 * @copydoc TextField::TextChangedSignal()
116 Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
119 * @copydoc TextField::AnchorClickedSignal()
121 DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
124 * @copydoc TextField::CursorPositionChangedSignal()
126 DevelTextField::CursorPositionChangedSignalType& CursorPositionChangedSignal();
129 * @copydoc TextField::InputFilteredSignal()
131 DevelTextField::InputFilteredSignalType& InputFilteredSignal();
134 * @copydoc TextField::SelectionChangedSignal()
136 DevelTextField::SelectionChangedSignalType& SelectionChangedSignal();
139 * @copydoc TextField::SelectionClearedSignal()
141 DevelTextField::SelectionClearedSignalType& SelectionClearedSignal();
144 * @copydoc TextField::SelectionStartedSignal()
146 DevelTextField::SelectionStartedSignalType& SelectionStartedSignal();
148 private: // From Control
150 * @copydoc Control::OnInitialize()
152 void OnInitialize() override;
155 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
157 DevelControl::ControlAccessible* CreateAccessibleObject() override;
160 * @copydoc Control::OnStyleChange()
162 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
165 * @copydoc Control::GetNaturalSize()
167 Vector3 GetNaturalSize() override;
170 * @copydoc Control::GetHeightForWidth()
172 float GetHeightForWidth(float width) override;
175 * @copydoc Control::OnInitialize()
177 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
180 * @copydoc Control::OnKeyInputFocusGained()
182 void OnKeyInputFocusGained() override;
185 * @copydoc Control::OnKeyInputFocusLost()
187 void OnKeyInputFocusLost() override;
190 * @copydoc Control::OnAccessibilityActivated()
192 bool OnAccessibilityActivated() override;
195 * @copydoc Control::OnTap()
197 void OnTap(const TapGesture& tap) override;
200 * @copydoc Control::OnPan()
202 void OnPan(const PanGesture& gesture) override;
205 * @copydoc Control::OnLongPress()
207 void OnLongPress(const LongPressGesture& gesture) override;
210 * @copydoc Control::OnSceneConnection()
212 void OnSceneConnection(int depth) override;
215 * @copydoc Control::OnPropertySet()
217 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
220 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
222 bool OnKeyEvent(const KeyEvent& event) override;
224 // From ControlInterface
227 * @copydoc Text::ControlInterface::RequestTextRelayout()
229 void RequestTextRelayout() override;
231 // From EditableControlInterface
234 * @copydoc Text::EditableControlInterface::TextChanged()
236 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
239 * @copydoc Text::EditableControlInterface::TextDeleted()
241 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
244 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
246 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
249 * @copydoc Text::EditableControlInterface::TextChanged()
251 void TextChanged(bool immediate) override;
254 * @copydoc Text::EditableControlInterface::MaxLengthReached()
256 void MaxLengthReached() override;
259 * @copydoc Text::EditableControlInterface::InputStyleChanged()
261 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
264 * @copydoc Text::SelectableControlInterface::SelectionChanged()
266 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
269 * @copydoc Text::EditableControlInterface::AddDecoration()
271 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
274 * @copydoc Text::EditableControlInterface::InputFiltered()
276 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
279 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
281 void GetControlBackgroundColor(Vector4& color) const override;
283 // From SelectableControlInterface
286 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
288 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
291 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
293 Uint32Pair GetTextSelectionRange() const override;
296 * @copydoc Text::SelectableControlInterface::SelectWholeText()
298 void SelectWholeText() override;
301 * @copydoc Text::SelectableControlInterface::SelectNone()
303 void SelectNone() override;
306 * @copydoc Text::SelectableControlInterface::SelectText()
308 void SelectText(const uint32_t start, const uint32_t end) override;
311 * @copydoc Text::SelectableControlInterface::GetSelectedText()
313 string GetSelectedText() const override;
316 * @copydoc Text::EditableControlInterface::IsEditable()
318 bool IsEditable() const override;
321 * @copydoc Text::EditableControlInterface::SetEditable()
323 void SetEditable(bool editable) override;
326 * @copydoc Dali::EditableControlInterface::CopyText()
328 string CopyText() override;
331 * @copydoc Dali::EditableControlInterface::CutText()
333 string CutText() override;
336 * @copydoc Text::EditableControlInterface::PasteText()
338 void PasteText() override;
340 // From AnchorControlInterface
343 * @copydoc Text::AnchorControlInterface::AnchorClicked()
345 void AnchorClicked(const std::string& href) override;
348 * @brief Get the rendered size of a specific text range.
349 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
350 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
352 * @param[in] startIndex start index of the text requested to calculate size for.
353 * @param[in] endIndex end index(included) of the text requested to calculate size for.
354 * @return list of sizes of the reuested text.
356 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
359 * @brief Get the top/left rendered position of a specific text range.
360 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
361 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
363 * @param[in] startIndex start index of the text requested to get position to.
364 * @param[in] endIndex end index(included) of the text requested to get position to.
365 * @return list of positions of the requested text.
367 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
370 * @brief Get the line bounding rectangle.
371 * if the requested index is out of range or the line is not yet rendered, a rect of {0, 0, 0, 0} is returned.
373 * @param[in] lineIndex line index to which we want to calculate the geometry for.
374 * @return bounding rectangle.
376 Rect<float> GetLineBoundingRectangle(const uint32_t lineIndex) const;
379 * @brief Get the character bounding rectangle.
380 * If the text is not yet rendered or the index > text.Count(); a rect of {0, 0, 0, 0} is returned.
382 * @param[in] charIndex character index to which we want to calculate the geometry for.
383 * @return bounding rectangle.
385 Rect<float> GetCharacterBoundingRectangle(const uint32_t charIndex) const;
388 * @brief Set the @p spannedText into current textField
389 * the spanned text contains content (text) and format (spans with ranges)
390 * the text is copied into text-controller and the spans are applied on ranges
392 * @param[in] spannedText the text with spans.
394 void SetSpannedText(const Text::Spanned& spannedText);
396 private: // Implementation
398 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
400 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
403 * @brief Callback when Clipboard signals an item should be pasted
404 * @param[in] clipboard handle to Clipboard Event Notifier
406 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
409 * @brief Get a Property Map for the image used for the required Handle Image
410 * @param[out] value the returned image property
411 * @param[in] handleType the type of handle
412 * @param[in] handleImageType the type of image for the given handleType
414 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
417 * @brief Enable or disable clipping.
419 void EnableClipping();
422 * @brief Callback when keyboard is shown/hidden.
424 * @param[in] keyboardShown True if keyboard is shown.
426 void KeyboardStatusChanged(bool keyboardShown);
429 * @brief Callback when TextField is touched
431 * @param[in] actor TextField touched
432 * @param[in] touch Touch information
434 bool OnTouched(Actor actor, const TouchEvent& touch);
437 * @brief Callbacks called on idle.
439 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
444 * @brief Emits TextChanged signal.
446 void EmitTextChangedSignal();
449 * @brief Emits CursorPositionChanged signal.
451 void EmitCursorPositionChangedSignal();
454 * @brief Emits SelectionChanged signal.
456 void EmitSelectionChangedSignal();
459 * @brief Emits SelectionCleared signal.
461 void EmitSelectionClearedSignal();
464 * @brief Emits SelectionStarted signal.
466 void EmitSelectionStartedSignal();
469 * @brief Callback function for when the layout is changed.
470 * @param[in] actor The actor whose layoutDirection is changed.
471 * @param[in] type The layoutDirection.
473 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
476 * @brief Add a layer for active or cursor.
477 * @param[in] layer The actor in which to store the layer.
478 * @param[in] actor The new layer to add.
480 void AddLayer(Actor& layer, Actor& actor);
483 * Construct a new TextField.
488 * A reference counted object may only be deleted by calling Unreference()
490 virtual ~TextField();
492 // Undefined copy constructor and assignment operators
493 TextField(const TextField&);
494 TextField& operator=(const TextField& rhs);
497 * @brief Resize actor to the given size.
499 * @param[in] actor The actor to be resized.
500 * @param[in] size Size to change.
502 void ResizeActor(Actor& actor, const Vector2& size);
505 * @brief Render view, create and attach actor(s) to this Text Field.
507 void RenderText(Text::Controller::UpdateTextType updateTextType);
509 // Connection needed to re-render text, when a Text Field returns to the scene.
510 void OnSceneConnect(Dali::Actor actor);
512 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
513 void OnAccessibilityStatusChanged();
517 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
518 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
519 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
520 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
521 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
522 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
523 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
524 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
525 Toolkit::DevelTextField::SelectionStartedSignalType mSelectionStartedSignal;
527 InputMethodContext mInputMethodContext;
528 Text::ControllerPtr mController;
529 Text::RendererPtr mRenderer;
530 Text::DecoratorPtr mDecorator;
531 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
532 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
533 std::vector<Toolkit::TextAnchor> mAnchorActors;
534 Dali::InputMethodOptions mInputMethodOptions;
536 Actor mRenderableActor;
539 Actor mBackgroundActor;
540 CallbackBase* mIdleCallback;
542 float mAlignmentOffset;
543 int mRenderingBackend;
545 bool mHasBeenStaged : 1;
546 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
547 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
548 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
549 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
551 //args for cursor position changed event
552 unsigned int mOldPosition;
554 //args for selection changed event
555 uint32_t mOldSelectionStart;
556 uint32_t mOldSelectionEnd;
558 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
561 struct PropertyHandler;
564 * @brief This structure is to connect TextField with Accessible functions.
566 class TextFieldAccessible : public EditableTextControlAccessible
569 using EditableTextControlAccessible::EditableTextControlAccessible;
572 * @copydoc Dali::Accessibility::Accessible::GetName()
574 std::string GetName() const override;
578 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
580 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
583 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
585 Toolkit::Text::ControllerPtr GetTextController() const override;
588 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetSubstituteCharacter()
590 std::uint32_t GetSubstituteCharacter() const override;
593 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::IsHiddenInput()
595 bool IsHiddenInput() const override;
598 * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
600 void RequestTextRelayout() override;
604 } // namespace Internal
606 // Helpers for public-api forwarding methods
608 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
610 DALI_ASSERT_ALWAYS(textField);
612 Dali::RefObject& handle = textField.GetImplementation();
614 return static_cast<Toolkit::Internal::TextField&>(handle);
617 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
619 DALI_ASSERT_ALWAYS(textField);
621 const Dali::RefObject& handle = textField.GetImplementation();
623 return static_cast<const Toolkit::Internal::TextField&>(handle);
626 } // namespace Toolkit
630 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H