1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H
5 * Copyright (c) 2021 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>
26 #include <dali-toolkit/devel-api/controls/control-devel.h>
27 #include <dali-toolkit/devel-api/controls/text-controls/text-field-devel.h>
28 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
29 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
30 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
31 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
32 #include <dali-toolkit/internal/text/text-control-interface.h>
33 #include <dali-toolkit/internal/text/text-controller.h>
34 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
35 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
36 #include <dali-toolkit/public-api/controls/control-impl.h>
37 #include <dali-toolkit/public-api/controls/text-controls/text-field.h>
46 * @brief A control which renders a short text string.
48 class TextField : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
52 * @copydoc Dali::Toollkit::TextField::New()
54 static Toolkit::TextField New();
59 * @brief Called when a property of an object of this type is set.
61 * @param[in] object The object whose property is set.
62 * @param[in] index The property index.
63 * @param[in] value The new property value.
65 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
68 * @brief Called to retrieve a property of an object of this type.
70 * @param[in] object The object whose property is to be retrieved.
71 * @param[in] index The property index.
72 * @return The current value of the property.
74 static Property::Value GetProperty(BaseObject* object, Property::Index index);
77 * @copydoc Dali::Toollkit::TextField::GetInputMethodContext()
79 InputMethodContext GetInputMethodContext();
82 * Connects a callback function with the object's signals.
83 * @param[in] object The object providing the signal.
84 * @param[in] tracker Used to disconnect the signal.
85 * @param[in] signalName The signal to connect to.
86 * @param[in] functor A newly allocated FunctorDelegate.
87 * @return True if the signal was connected.
88 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
90 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
93 * @brief Gets text controller
95 * @return The text controller
97 Text::ControllerPtr GetTextController();
100 * @copydoc TextField::TextChangedSignal()
102 Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
105 * @copydoc TextField::MaxLengthReachedSignal()
107 Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
110 * @copydoc TextField::TextChangedSignal()
112 Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
115 * @copydoc TextField::AnchorClickedSignal()
117 DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
120 * @copydoc TextField::CursorPositionChangedSignal()
122 DevelTextField::CursorPositionChangedSignalType& CursorPositionChangedSignal();
125 * @copydoc TextField::InputFilteredSignal()
127 DevelTextField::InputFilteredSignalType& InputFilteredSignal();
130 * @copydoc TextField::SelectionChangedSignal()
132 DevelTextField::SelectionChangedSignalType& SelectionChangedSignal();
135 * @copydoc TextField::SelectionClearedSignal()
137 DevelTextField::SelectionClearedSignalType& SelectionClearedSignal();
139 private: // From Control
141 * @copydoc Control::OnInitialize()
143 void OnInitialize() override;
146 * @copydoc Control::OnStyleChange()
148 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
151 * @copydoc Control::GetNaturalSize()
153 Vector3 GetNaturalSize() override;
156 * @copydoc Control::GetHeightForWidth()
158 float GetHeightForWidth(float width) override;
161 * @copydoc Control::OnInitialize()
163 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
166 * @copydoc Control::OnKeyInputFocusGained()
168 void OnKeyInputFocusGained() override;
171 * @copydoc Control::OnKeyInputFocusLost()
173 void OnKeyInputFocusLost() override;
176 * @copydoc Control::OnAccessibilityActivated()
178 bool OnAccessibilityActivated() override;
181 * @copydoc Control::OnTap()
183 void OnTap(const TapGesture& tap) override;
186 * @copydoc Control::OnPan()
188 void OnPan(const PanGesture& gesture) override;
191 * @copydoc Control::OnLongPress()
193 void OnLongPress(const LongPressGesture& gesture) override;
196 * @copydoc Control::OnSceneConnection()
198 void OnSceneConnection(int depth) override;
201 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
203 bool OnKeyEvent(const KeyEvent& event) override;
205 // From ControlInterface
208 * @copydoc Text::ControlInterface::RequestTextRelayout()
210 void RequestTextRelayout() override;
212 // From EditableControlInterface
215 * @copydoc Text::EditableControlInterface::TextChanged()
217 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
220 * @copydoc Text::EditableControlInterface::TextDeleted()
222 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
225 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
227 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
230 * @copydoc Text::EditableControlInterface::TextChanged()
232 void TextChanged(bool immediate) override;
235 * @copydoc Text::EditableControlInterface::MaxLengthReached()
237 void MaxLengthReached() override;
240 * @copydoc Text::EditableControlInterface::InputStyleChanged()
242 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
245 * @copydoc Text::SelectableControlInterface::SelectionChanged()
247 void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
250 * @copydoc Text::EditableControlInterface::AddDecoration()
252 void AddDecoration(Actor& actor, bool needsClipping) override;
255 * @copydoc Text::EditableControlInterface::InputFiltered()
257 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
260 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
262 void GetControlBackgroundColor(Vector4& color) const override;
264 // From SelectableControlInterface
267 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
269 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
272 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
274 Uint32Pair GetTextSelectionRange() const override;
277 * @copydoc Text::SelectableControlInterface::SelectWholeText()
279 void SelectWholeText() override;
282 * @copydoc Text::SelectableControlInterface::SelectNone()
284 void SelectNone() override;
287 * @copydoc Text::SelectableControlInterface::SelectText()
289 void SelectText(const uint32_t start, const uint32_t end) override;
292 * @copydoc Text::SelectableControlInterface::GetSelectedText()
294 string GetSelectedText() const override;
297 * @copydoc Text::EditableControlInterface::IsEditable()
299 bool IsEditable() const override;
302 * @copydoc Text::EditableControlInterface::SetEditable()
304 void SetEditable(bool editable) override;
307 * @copydoc Dali::EditableControlInterface::CopyText()
309 string CopyText() override;
312 * @copydoc Dali::EditableControlInterface::CutText()
314 string CutText() override;
317 * @copydoc Text::EditableControlInterface::PasteText()
319 void PasteText() override;
321 // From AnchorControlInterface
324 * @copydoc Text::AnchorControlInterface::AnchorClicked()
326 void AnchorClicked(const std::string& href) override;
328 private: // Implementation
330 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
332 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
335 * @brief Callback when Clipboard signals an item should be pasted
336 * @param[in] clipboard handle to Clipboard Event Notifier
338 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
341 * @brief Get a Property Map for the image used for the required Handle Image
342 * @param[out] value the returned image property
343 * @param[in] handleType the type of handle
344 * @param[in] handleImageType the type of image for the given handleType
346 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
349 * @brief Enable or disable clipping.
351 void EnableClipping();
354 * @brief Callback when keyboard is shown/hidden.
356 * @param[in] keyboardShown True if keyboard is shown.
358 void KeyboardStatusChanged(bool keyboardShown);
361 * @brief Callback when TextField is touched
363 * @param[in] actor TextField touched
364 * @param[in] touch Touch information
366 bool OnTouched(Actor actor, const TouchEvent& touch);
369 * @brief Callbacks called on idle.
371 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
376 * @brief Emits TextChanged signal.
378 void EmitTextChangedSignal();
381 * @brief Emits CursorPositionChanged signal.
383 void EmitCursorPositionChangedSignal();
386 * @brief Emits SelectionChanged signal.
388 void EmitSelectionChangedSignal();
391 * @brief Emits SelectionCleared signal.
393 void EmitSelectionClearedSignal();
396 * @brief Callback function for when the layout is changed.
397 * @param[in] actor The actor whose layoutDirection is changed.
398 * @param[in] type The layoutDirection.
400 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
403 * Construct a new TextField.
408 * A reference counted object may only be deleted by calling Unreference()
410 virtual ~TextField();
412 // Undefined copy constructor and assignment operators
413 TextField(const TextField&);
414 TextField& operator=(const TextField& rhs);
417 * @brief Resize actor to the given size.
419 * @param[in] actor The actor to be resized.
420 * @param[in] size Size to change.
422 void ResizeActor(Actor& actor, const Vector2& size);
425 * @brief Render view, create and attach actor(s) to this Text Field.
427 void RenderText(Text::Controller::UpdateTextType updateTextType);
429 // Connection needed to re-render text, when a Text Field returns to the scene.
430 void OnSceneConnect(Dali::Actor actor);
434 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
435 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
436 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
437 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
438 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
439 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
440 Toolkit::DevelTextField::SelectionChangedSignalType mSelectionChangedSignal;
441 Toolkit::DevelTextField::SelectionClearedSignalType mSelectionClearedSignal;
443 InputMethodContext mInputMethodContext;
444 Text::ControllerPtr mController;
445 Text::RendererPtr mRenderer;
446 Text::DecoratorPtr mDecorator;
447 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
448 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
449 Dali::InputMethodOptions mInputMethodOptions;
451 Actor mRenderableActor;
453 Actor mBackgroundActor;
454 CallbackBase* mIdleCallback;
456 float mAlignmentOffset;
457 int mRenderingBackend;
459 bool mHasBeenStaged : 1;
460 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
461 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
462 bool mSelectionChanged : 1; ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
463 bool mSelectionCleared : 1; ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
465 //args for cursor position changed event
466 unsigned int mOldPosition;
468 //args for selection changed event
469 uint32_t mOldSelectionStart;
470 uint32_t mOldSelectionEnd;
474 * @brief This structure is to connect TextField with Accessible functions.
476 struct AccessibleImpl : public DevelControl::AccessibleImpl,
477 public virtual Dali::Accessibility::Text,
478 public virtual Dali::Accessibility::EditableText
480 using DevelControl::AccessibleImpl::AccessibleImpl;
483 * @copydoc Dali::Accessibility::Accessible::GetName()
485 std::string GetName() override;
488 * @copydoc Dali::Accessibility::Text::GetText()
490 std::string GetText(size_t startOffset, size_t endOffset) override;
493 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
495 size_t GetCharacterCount() override;
498 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
500 size_t GetCursorOffset() override;
503 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
505 bool SetCursorOffset(size_t offset) override;
508 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
510 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
513 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
515 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
518 * @copydoc Dali::Accessibility::Text::RemoveSelection()
520 bool RemoveSelection(size_t selectionIndex) override;
523 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
525 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
528 * @copydoc Dali::Accessibility::EditableText::CopyText()
530 bool CopyText(size_t startPosition, size_t endPosition) override;
533 * @copydoc Dali::Accessibility::EditableText::CutText()
535 bool CutText(size_t startPosition, size_t endPosition) override;
538 * @copydoc Dali::Accessibility::Accessible::GetStates()
540 Accessibility::States CalculateStates() override;
543 * @copydoc Dali::Accessibility::EditableText::InsertText()
545 bool InsertText(size_t startPosition, std::string text) override;
548 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
550 bool SetTextContents(std::string newContents) override;
553 * @copydoc Dali::Accessibility::EditableText::DeleteText()
555 bool DeleteText(size_t startPosition, size_t endPosition) override;
559 } // namespace Internal
561 // Helpers for public-api forwarding methods
563 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
565 DALI_ASSERT_ALWAYS(textField);
567 Dali::RefObject& handle = textField.GetImplementation();
569 return static_cast<Toolkit::Internal::TextField&>(handle);
572 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
574 DALI_ASSERT_ALWAYS(textField);
576 const Dali::RefObject& handle = textField.GetImplementation();
578 return static_cast<const Toolkit::Internal::TextField&>(handle);
581 } // namespace Toolkit
585 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H