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/decorator/text-decorator.h>
34 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
35 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
36 #include <dali-toolkit/internal/text/text-control-interface.h>
37 #include <dali-toolkit/internal/text/text-controller.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 Control::OnStyleChange()
157 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
160 * @copydoc Control::GetNaturalSize()
162 Vector3 GetNaturalSize() override;
165 * @copydoc Control::GetHeightForWidth()
167 float GetHeightForWidth(float width) override;
170 * @copydoc Control::OnInitialize()
172 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
175 * @copydoc Control::OnKeyInputFocusGained()
177 void OnKeyInputFocusGained() override;
180 * @copydoc Control::OnKeyInputFocusLost()
182 void OnKeyInputFocusLost() override;
185 * @copydoc Control::OnAccessibilityActivated()
187 bool OnAccessibilityActivated() override;
190 * @copydoc Control::OnTap()
192 void OnTap(const TapGesture& tap) override;
195 * @copydoc Control::OnPan()
197 void OnPan(const PanGesture& gesture) override;
200 * @copydoc Control::OnLongPress()
202 void OnLongPress(const LongPressGesture& gesture) override;
205 * @copydoc Control::OnSceneConnection()
207 void OnSceneConnection(int depth) override;
210 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
212 bool OnKeyEvent(const KeyEvent& event) override;
214 // From ControlInterface
217 * @copydoc Text::ControlInterface::RequestTextRelayout()
219 void RequestTextRelayout() override;
221 // From EditableControlInterface
224 * @copydoc Text::EditableControlInterface::TextChanged()
226 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
229 * @copydoc Text::EditableControlInterface::TextDeleted()
231 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
234 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
236 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
239 * @copydoc Text::EditableControlInterface::TextChanged()
241 void TextChanged(bool immediate) override;
244 * @copydoc Text::EditableControlInterface::MaxLengthReached()
246 void MaxLengthReached() override;
249 * @copydoc Text::EditableControlInterface::InputStyleChanged()
251 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
254 * @copydoc Text::SelectableControlInterface::SelectionChanged()
256 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
259 * @copydoc Text::EditableControlInterface::AddDecoration()
261 void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
264 * @copydoc Text::EditableControlInterface::InputFiltered()
266 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
269 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
271 void GetControlBackgroundColor(Vector4& color) const override;
273 // From SelectableControlInterface
277 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
279 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
282 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
284 Uint32Pair GetTextSelectionRange() const override;
287 * @copydoc Text::SelectableControlInterface::SelectWholeText()
289 void SelectWholeText() override;
292 * @copydoc Text::SelectableControlInterface::SelectNone()
294 void SelectNone() override;
297 * @copydoc Text::SelectableControlInterface::SelectText()
299 void SelectText(const uint32_t start, const uint32_t end) override;
302 * @copydoc Text::SelectableControlInterface::GetSelectedText()
304 string GetSelectedText() const override;
307 * @copydoc Text::EditableControlInterface::IsEditable()
309 bool IsEditable() const override;
312 * @copydoc Text::EditableControlInterface::SetEditable()
314 void SetEditable(bool editable) override;
317 * @copydoc Dali::EditableControlInterface::CopyText()
319 string CopyText() override;
322 * @copydoc Dali::EditableControlInterface::CutText()
324 string CutText() override;
327 * @copydoc Text::EditableControlInterface::PasteText()
329 void PasteText() override;
331 // From AnchorControlInterface
334 * @copydoc Text::AnchorControlInterface::AnchorClicked()
336 void AnchorClicked(const std::string& href) override;
339 * @brief Get the rendered size of a specific text range.
340 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
341 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
343 * @param[in] startIndex start index of the text requested to calculate size for.
344 * @param[in] endIndex end index(included) of the text requested to calculate size for.
345 * @return list of sizes of the reuested text.
347 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
350 * @brief Get the top/left rendered position of a specific text range.
351 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
352 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
354 * @param[in] startIndex start index of the text requested to get position to.
355 * @param[in] endIndex end index(included) of the text requested to get position to.
356 * @return list of positions of the requested text.
358 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
360 private: // Implementation
362 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
364 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
367 * @brief Callback when Clipboard signals an item should be pasted
368 * @param[in] clipboard handle to Clipboard Event Notifier
370 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
373 * @brief Get a Property Map for the image used for the required Handle Image
374 * @param[out] value the returned image property
375 * @param[in] handleType the type of handle
376 * @param[in] handleImageType the type of image for the given handleType
378 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
381 * @brief Enable or disable clipping.
383 void EnableClipping();
386 * @brief Callback when keyboard is shown/hidden.
388 * @param[in] keyboardShown True if keyboard is shown.
390 void KeyboardStatusChanged(bool keyboardShown);
393 * @brief Callback when TextField is touched
395 * @param[in] actor TextField touched
396 * @param[in] touch Touch information
398 bool OnTouched(Actor actor, const TouchEvent& touch);
401 * @brief Callbacks called on idle.
403 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
408 * @brief Emits TextChanged signal.
410 void EmitTextChangedSignal();
413 * @brief Emits CursorPositionChanged signal.
415 void EmitCursorPositionChangedSignal();
418 * @brief Emits SelectionChanged signal.
420 void EmitSelectionChangedSignal();
423 * @brief Emits SelectionCleared signal.
425 void EmitSelectionClearedSignal();
428 * @brief Emits SelectionStarted signal.
430 void EmitSelectionStartedSignal();
433 * @brief Callback function for when the layout is changed.
434 * @param[in] actor The actor whose layoutDirection is changed.
435 * @param[in] type The layoutDirection.
437 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
440 * @brief Add a layer for active or cursor.
441 * @param[in] layer The actor in which to store the layer.
442 * @param[in] actor The new layer to add.
444 void AddLayer(Actor& layer, Actor& actor);
447 * Construct a new TextField.
452 * A reference counted object may only be deleted by calling Unreference()
454 virtual ~TextField();
456 // Undefined copy constructor and assignment operators
457 TextField(const TextField&);
458 TextField& operator=(const TextField& rhs);
461 * @brief Resize actor to the given size.
463 * @param[in] actor The actor to be resized.
464 * @param[in] size Size to change.
466 void ResizeActor(Actor& actor, const Vector2& size);
469 * @brief Render view, create and attach actor(s) to this Text Field.
471 void RenderText(Text::Controller::UpdateTextType updateTextType);
473 // Connection needed to re-render text, when a Text Field returns to the scene.
474 void OnSceneConnect(Dali::Actor actor);
476 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
477 void OnAccessibilityStatusChanged();
481 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
482 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
483 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
484 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
485 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
486 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
487 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
488 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
489 Toolkit::DevelTextField::SelectionStartedSignalType mSelectionStartedSignal;
491 InputMethodContext mInputMethodContext;
492 Text::ControllerPtr mController;
493 Text::RendererPtr mRenderer;
494 Text::DecoratorPtr mDecorator;
495 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
496 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
497 std::vector<Toolkit::TextAnchor> mAnchorActors;
498 Dali::InputMethodOptions mInputMethodOptions;
500 Actor mRenderableActor;
503 Actor mBackgroundActor;
504 CallbackBase* mIdleCallback;
506 float mAlignmentOffset;
507 int mRenderingBackend;
509 bool mHasBeenStaged : 1;
510 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
511 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
512 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
513 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
515 //args for cursor position changed event
516 unsigned int mOldPosition;
518 //args for selection changed event
519 uint32_t mOldSelectionStart;
520 uint32_t mOldSelectionEnd;
522 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
525 struct PropertyHandler;
528 * @brief This structure is to connect TextField with Accessible functions.
530 class TextFieldAccessible : public EditableTextControlAccessible
533 using EditableTextControlAccessible::EditableTextControlAccessible;
536 * @copydoc Dali::Accessibility::Accessible::GetName()
538 std::string GetName() const override;
542 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
544 const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
547 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
549 Toolkit::Text::ControllerPtr GetTextController() const override;
552 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetSubstituteCharacter()
554 std::uint32_t GetSubstituteCharacter() const override;
557 * @copydoc Dali::Toolkit::Internal::TextControlAccessible::IsHiddenInput()
559 bool IsHiddenInput() const override;
562 * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
564 void RequestTextRelayout() override;
568 } // namespace Internal
570 // Helpers for public-api forwarding methods
572 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
574 DALI_ASSERT_ALWAYS(textField);
576 Dali::RefObject& handle = textField.GetImplementation();
578 return static_cast<Toolkit::Internal::TextField&>(handle);
581 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
583 DALI_ASSERT_ALWAYS(textField);
585 const Dali::RefObject& handle = textField.GetImplementation();
587 return static_cast<const Toolkit::Internal::TextField&>(handle);
590 } // namespace Toolkit
594 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H