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, Toolkit::Text::DecorationType type, 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
276 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
278 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
281 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
283 Uint32Pair GetTextSelectionRange() const override;
286 * @copydoc Text::SelectableControlInterface::SelectWholeText()
288 void SelectWholeText() override;
291 * @copydoc Text::SelectableControlInterface::SelectNone()
293 void SelectNone() override;
296 * @copydoc Text::SelectableControlInterface::SelectText()
298 void SelectText(const uint32_t start, const uint32_t end) override;
301 * @copydoc Text::SelectableControlInterface::GetSelectedText()
303 string GetSelectedText() const override;
306 * @copydoc Text::EditableControlInterface::IsEditable()
308 bool IsEditable() const override;
311 * @copydoc Text::EditableControlInterface::SetEditable()
313 void SetEditable(bool editable) override;
316 * @copydoc Dali::EditableControlInterface::CopyText()
318 string CopyText() override;
321 * @copydoc Dali::EditableControlInterface::CutText()
323 string CutText() override;
326 * @copydoc Text::EditableControlInterface::PasteText()
328 void PasteText() override;
330 // From AnchorControlInterface
333 * @copydoc Text::AnchorControlInterface::AnchorClicked()
335 void AnchorClicked(const std::string& href) override;
338 * @brief Get the rendered size of a specific text range.
339 * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
340 * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
342 * @param[in] startIndex start index of the text requested to calculate size for.
343 * @param[in] endIndex end index(included) of the text requested to calculate size for.
344 * @return list of sizes of the reuested text.
346 Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
349 * @brief Get the top/left rendered position of a specific text range.
350 * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
351 * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
353 * @param[in] startIndex start index of the text requested to get position to.
354 * @param[in] endIndex end index(included) of the text requested to get position to.
355 * @return list of positions of the requested text.
357 Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
359 private: // Implementation
361 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
363 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
366 * @brief Callback when Clipboard signals an item should be pasted
367 * @param[in] clipboard handle to Clipboard Event Notifier
369 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
372 * @brief Get a Property Map for the image used for the required Handle Image
373 * @param[out] value the returned image property
374 * @param[in] handleType the type of handle
375 * @param[in] handleImageType the type of image for the given handleType
377 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
380 * @brief Enable or disable clipping.
382 void EnableClipping();
385 * @brief Callback when keyboard is shown/hidden.
387 * @param[in] keyboardShown True if keyboard is shown.
389 void KeyboardStatusChanged(bool keyboardShown);
392 * @brief Callback when TextField is touched
394 * @param[in] actor TextField touched
395 * @param[in] touch Touch information
397 bool OnTouched(Actor actor, const TouchEvent& touch);
400 * @brief Callbacks called on idle.
402 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
407 * @brief Emits TextChanged signal.
409 void EmitTextChangedSignal();
412 * @brief Emits CursorPositionChanged signal.
414 void EmitCursorPositionChangedSignal();
417 * @brief Emits SelectionChanged signal.
419 void EmitSelectionChangedSignal();
422 * @brief Emits SelectionCleared signal.
424 void EmitSelectionClearedSignal();
427 * @brief Emits SelectionStarted signal.
429 void EmitSelectionStartedSignal();
432 * @brief Callback function for when the layout is changed.
433 * @param[in] actor The actor whose layoutDirection is changed.
434 * @param[in] type The layoutDirection.
436 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
439 * @brief Add a layer for active or cursor.
440 * @param[in] layer The actor in which to store the layer.
441 * @param[in] actor The new layer to add.
443 void AddLayer(Actor& layer, Actor& actor);
446 * Construct a new TextField.
451 * A reference counted object may only be deleted by calling Unreference()
453 virtual ~TextField();
455 // Undefined copy constructor and assignment operators
456 TextField(const TextField&);
457 TextField& operator=(const TextField& rhs);
460 * @brief Resize actor to the given size.
462 * @param[in] actor The actor to be resized.
463 * @param[in] size Size to change.
465 void ResizeActor(Actor& actor, const Vector2& size);
468 * @brief Render view, create and attach actor(s) to this Text Field.
470 void RenderText(Text::Controller::UpdateTextType updateTextType);
472 // Connection needed to re-render text, when a Text Field returns to the scene.
473 void OnSceneConnect(Dali::Actor actor);
475 // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
476 void OnAccessibilityStatusChanged();
480 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
481 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
482 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
483 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
484 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
485 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
486 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
487 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
488 Toolkit::DevelTextField::SelectionStartedSignalType mSelectionStartedSignal;
490 InputMethodContext mInputMethodContext;
491 Text::ControllerPtr mController;
492 Text::RendererPtr mRenderer;
493 Text::DecoratorPtr mDecorator;
494 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
495 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
496 std::vector<Toolkit::TextAnchor> mAnchorActors;
497 Dali::InputMethodOptions mInputMethodOptions;
499 Actor mRenderableActor;
502 Actor mBackgroundActor;
503 CallbackBase* mIdleCallback;
505 float mAlignmentOffset;
506 int mRenderingBackend;
508 bool mHasBeenStaged : 1;
509 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
510 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
511 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
512 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
514 //args for cursor position changed event
515 unsigned int mOldPosition;
517 //args for selection changed event
518 uint32_t mOldSelectionStart;
519 uint32_t mOldSelectionEnd;
521 bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
524 struct PropertyHandler;
527 * @brief This structure is to connect TextField with Accessible functions.
529 struct AccessibleImpl : public DevelControl::ControlAccessible,
530 public virtual Dali::Accessibility::EditableText,
531 public virtual Dali::Accessibility::Hypertext
533 using DevelControl::ControlAccessible::ControlAccessible;
536 * @copydoc Dali::Accessibility::Accessible::GetName()
538 std::string GetName() const override;
541 * @copydoc Dali::Accessibility::Text::GetText()
543 std::string GetText(size_t startOffset, size_t endOffset) const override;
546 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
548 size_t GetCharacterCount() const override;
551 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
553 size_t GetCursorOffset() const override;
556 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
558 bool SetCursorOffset(size_t offset) override;
561 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
563 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) const override;
566 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
568 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) const override;
571 * @copydoc Dali::Accessibility::Text::RemoveSelection()
573 bool RemoveSelection(size_t selectionIndex) override;
576 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
578 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
581 * @copydoc Dali::Accessibility::Text::GetRangeExtents()
583 Rect<> GetRangeExtents(size_t startOffset, size_t endOffset, Accessibility::CoordinateType type) override;
586 * @copydoc Dali::Accessibility::EditableText::CopyText()
588 bool CopyText(size_t startPosition, size_t endPosition) override;
591 * @copydoc Dali::Accessibility::EditableText::CutText()
593 bool CutText(size_t startPosition, size_t endPosition) override;
596 * @copydoc Dali::Accessibility::Accessible::GetStates()
598 Accessibility::States CalculateStates() override;
601 * @copydoc Dali::Accessibility::EditableText::InsertText()
603 bool InsertText(size_t startPosition, std::string text) override;
606 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
608 bool SetTextContents(std::string newContents) override;
611 * @copydoc Dali::Accessibility::EditableText::DeleteText()
613 bool DeleteText(size_t startPosition, size_t endPosition) override;
616 * @copydoc Dali::Accessibility::Hypertext::GetLink()
618 Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
621 * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
623 int32_t GetLinkIndex(int32_t characterOffset) const override;
626 * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
628 int32_t GetLinkCount() const override;
632 } // namespace Internal
634 // Helpers for public-api forwarding methods
636 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
638 DALI_ASSERT_ALWAYS(textField);
640 Dali::RefObject& handle = textField.GetImplementation();
642 return static_cast<Toolkit::Internal::TextField&>(handle);
645 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
647 DALI_ASSERT_ALWAYS(textField);
649 const Dali::RefObject& handle = textField.GetImplementation();
651 return static_cast<const Toolkit::Internal::TextField&>(handle);
654 } // namespace Toolkit
658 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H