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();
129 private: // From Control
131 * @copydoc Control::OnInitialize()
133 void OnInitialize() override;
136 * @copydoc Control::OnStyleChange()
138 void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
141 * @copydoc Control::GetNaturalSize()
143 Vector3 GetNaturalSize() override;
146 * @copydoc Control::GetHeightForWidth()
148 float GetHeightForWidth(float width) override;
151 * @copydoc Control::OnInitialize()
153 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
156 * @copydoc Control::OnKeyInputFocusGained()
158 void OnKeyInputFocusGained() override;
161 * @copydoc Control::OnKeyInputFocusLost()
163 void OnKeyInputFocusLost() override;
166 * @copydoc Control::OnAccessibilityActivated()
168 bool OnAccessibilityActivated() override;
171 * @copydoc Control::OnTap()
173 void OnTap(const TapGesture& tap) override;
176 * @copydoc Control::OnPan()
178 void OnPan(const PanGesture& gesture) override;
181 * @copydoc Control::OnLongPress()
183 void OnLongPress(const LongPressGesture& gesture) override;
186 * @copydoc Control::OnSceneConnection()
188 void OnSceneConnection(int depth) override;
191 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
193 bool OnKeyEvent(const KeyEvent& event) override;
195 // From ControlInterface
198 * @copydoc Text::ControlInterface::RequestTextRelayout()
200 void RequestTextRelayout() override;
202 // From EditableControlInterface
205 * @copydoc Text::EditableControlInterface::TextChanged()
207 void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
210 * @copydoc Text::EditableControlInterface::TextDeleted()
212 void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
215 * @copydoc Text::EditableControlInterface::CursorPositionChanged()
217 void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
220 * @copydoc Text::EditableControlInterface::TextChanged()
222 void TextChanged(bool immediate) override;
225 * @copydoc Text::EditableControlInterface::MaxLengthReached()
227 void MaxLengthReached() override;
230 * @copydoc Text::EditableControlInterface::InputStyleChanged()
232 void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
235 * @copydoc Text::EditableControlInterface::AddDecoration()
237 void AddDecoration(Actor& actor, bool needsClipping) override;
240 * @copydoc Text::EditableControlInterface::InputFiltered()
242 void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
245 * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
247 void GetControlBackgroundColor(Vector4& color) const override;
249 // From SelectableControlInterface
252 * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
254 void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
257 * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
259 Uint32Pair GetTextSelectionRange() const override;
262 * @copydoc Text::SelectableControlInterface::SelectWholeText()
264 void SelectWholeText() override;
267 * @copydoc Text::SelectableControlInterface::SelectNone()
269 void SelectNone() override;
272 * @copydoc Text::SelectableControlInterface::SelectText()
274 void SelectText(const uint32_t start, const uint32_t end) override;
277 * @copydoc Text::SelectableControlInterface::GetSelectedText()
279 string GetSelectedText() const override;
282 * @copydoc Text::EditableControlInterface::IsEditable()
284 bool IsEditable() const override;
287 * @copydoc Text::EditableControlInterface::SetEditable()
289 void SetEditable(bool editable) override;
291 // From AnchorControlInterface
294 * @copydoc Text::AnchorControlInterface::AnchorClicked()
296 void AnchorClicked(const std::string& href) override;
298 private: // Implementation
300 * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
302 InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
305 * @brief Callback when Clipboard signals an item should be pasted
306 * @param[in] clipboard handle to Clipboard Event Notifier
308 void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
311 * @brief Get a Property Map for the image used for the required Handle Image
312 * @param[out] value the returned image property
313 * @param[in] handleType the type of handle
314 * @param[in] handleImageType the type of image for the given handleType
316 void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
319 * @brief Enable or disable clipping.
321 void EnableClipping();
324 * @brief Callback when keyboard is shown/hidden.
326 * @param[in] keyboardShown True if keyboard is shown.
328 void KeyboardStatusChanged(bool keyboardShown);
331 * @brief Callback when TextField is touched
333 * @param[in] actor TextField touched
334 * @param[in] touch Touch information
336 bool OnTouched(Actor actor, const TouchEvent& touch);
339 * @brief Callbacks called on idle.
341 * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
346 * @brief Emits TextChanged signal.
348 void EmitTextChangedSignal();
351 * @brief Emits CursorPositionChanged signal.
353 void EmitCursorPositionChangedSignal();
356 * @brief Callback function for when the layout is changed.
357 * @param[in] actor The actor whose layoutDirection is changed.
358 * @param[in] type The layoutDirection.
360 void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
363 * Construct a new TextField.
368 * A reference counted object may only be deleted by calling Unreference()
370 virtual ~TextField();
372 // Undefined copy constructor and assignment operators
373 TextField(const TextField&);
374 TextField& operator=(const TextField& rhs);
377 * @brief Resize actor to the given size.
379 * @param[in] actor The actor to be resized.
380 * @param[in] size Size to change.
382 void ResizeActor(Actor& actor, const Vector2& size);
385 * @brief Render view, create and attach actor(s) to this Text Field.
387 void RenderText(Text::Controller::UpdateTextType updateTextType);
389 // Connection needed to re-render text, when a Text Field returns to the scene.
390 void OnSceneConnect(Dali::Actor actor);
394 Toolkit::TextField::TextChangedSignalType mTextChangedSignal;
395 Toolkit::TextField::MaxLengthReachedSignalType mMaxLengthReachedSignal;
396 Toolkit::TextField::InputStyleChangedSignalType mInputStyleChangedSignal;
397 Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
398 Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
399 Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
401 InputMethodContext mInputMethodContext;
402 Text::ControllerPtr mController;
403 Text::RendererPtr mRenderer;
404 Text::DecoratorPtr mDecorator;
405 Toolkit::Control mStencil; ///< For EXCEED_POLICY_CLIP
406 std::vector<Actor> mClippingDecorationActors; ///< Decoration actors which need clipping.
407 Dali::InputMethodOptions mInputMethodOptions;
409 Actor mRenderableActor;
411 Actor mBackgroundActor;
412 CallbackBase* mIdleCallback;
414 float mAlignmentOffset;
415 int mRenderingBackend;
417 bool mHasBeenStaged : 1;
418 bool mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
419 bool mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
421 //args for cursor position changed event
422 unsigned int mOldPosition;
426 * @brief This structure is to connect TextField with Accessible functions.
428 struct AccessibleImpl : public DevelControl::AccessibleImpl,
429 public virtual Dali::Accessibility::Text,
430 public virtual Dali::Accessibility::EditableText
432 using DevelControl::AccessibleImpl::AccessibleImpl;
435 * @copydoc Dali::Accessibility::Accessible::GetName()
437 std::string GetName() override;
440 * @copydoc Dali::Accessibility::Text::GetText()
442 std::string GetText(size_t startOffset, size_t endOffset) override;
445 * @copydoc Dali::Accessibility::Text::GetCharacterCount()
447 size_t GetCharacterCount() override;
450 * @copydoc Dali::Accessibility::Text::GetCursorOffset()
452 size_t GetCursorOffset() override;
455 * @copydoc Dali::Accessibility::Text::SetCursorOffset()
457 bool SetCursorOffset(size_t offset) override;
460 * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
462 Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
465 * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
467 Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
470 * @copydoc Dali::Accessibility::Text::RemoveSelection()
472 bool RemoveSelection(size_t selectionIndex) override;
475 * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
477 bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
480 * @copydoc Dali::Accessibility::EditableText::CopyText()
482 bool CopyText(size_t startPosition, size_t endPosition) override;
485 * @copydoc Dali::Accessibility::EditableText::CutText()
487 bool CutText(size_t startPosition, size_t endPosition) override;
490 * @copydoc Dali::Accessibility::Accessible::GetStates()
492 Accessibility::States CalculateStates() override;
495 * @copydoc Dali::Accessibility::EditableText::InsertText()
497 bool InsertText(size_t startPosition, std::string text) override;
500 * @copydoc Dali::Accessibility::EditableText::SetTextContents()
502 bool SetTextContents(std::string newContents) override;
505 * @copydoc Dali::Accessibility::EditableText::DeleteText()
507 bool DeleteText(size_t startPosition, size_t endPosition) override;
511 } // namespace Internal
513 // Helpers for public-api forwarding methods
515 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
517 DALI_ASSERT_ALWAYS(textField);
519 Dali::RefObject& handle = textField.GetImplementation();
521 return static_cast<Toolkit::Internal::TextField&>(handle);
524 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
526 DALI_ASSERT_ALWAYS(textField);
528 const Dali::RefObject& handle = textField.GetImplementation();
530 return static_cast<const Toolkit::Internal::TextField&>(handle);
533 } // namespace Toolkit
537 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H