85d50ef2d3140f50e235a24fc53c94504973f780
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-field-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
23 #include <dali/devel-api/adaptor-framework/input-method-context.h>
24
25 // INTERNAL INCLUDES
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>
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Internal
44 {
45 /**
46  * @brief A control which renders a short text string.
47  */
48 class TextField : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
49 {
50 public:
51   /**
52    * @copydoc Dali::Toollkit::TextField::New()
53    */
54   static Toolkit::TextField New();
55
56   // Properties
57
58   /**
59    * @brief Called when a property of an object of this type is set.
60    *
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.
64    */
65   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
66
67   /**
68    * @brief Called to retrieve a property of an object of this type.
69    *
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.
73    */
74   static Property::Value GetProperty(BaseObject* object, Property::Index index);
75
76   /**
77    * @copydoc Dali::Toollkit::TextField::GetInputMethodContext()
78    */
79   InputMethodContext GetInputMethodContext();
80
81   /**
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.
89    */
90   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
91
92   /**
93    * @brief Gets text controller
94    *
95    * @return The text controller
96    */
97   Text::ControllerPtr GetTextController();
98
99   /**
100    * @copydoc TextField::TextChangedSignal()
101    */
102   Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
103
104   /**
105    * @copydoc TextField::MaxLengthReachedSignal()
106    */
107   Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
108
109   /**
110    * @copydoc TextField::TextChangedSignal()
111    */
112   Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
113
114   /**
115    * @copydoc TextField::AnchorClickedSignal()
116    */
117   DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
118
119   /**
120    * @copydoc TextField::InputFilteredSignal()
121    */
122   DevelTextField::InputFilteredSignalType& InputFilteredSignal();
123
124 private: // From Control
125   /**
126    * @copydoc Control::OnInitialize()
127    */
128   void OnInitialize() override;
129
130   /**
131    * @copydoc Control::OnStyleChange()
132    */
133   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
134
135   /**
136    * @copydoc Control::GetNaturalSize()
137    */
138   Vector3 GetNaturalSize() override;
139
140   /**
141    * @copydoc Control::GetHeightForWidth()
142    */
143   float GetHeightForWidth(float width) override;
144
145   /**
146    * @copydoc Control::OnInitialize()
147    */
148   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
149
150   /**
151    * @copydoc Control::OnKeyInputFocusGained()
152    */
153   void OnKeyInputFocusGained() override;
154
155   /**
156    * @copydoc Control::OnKeyInputFocusLost()
157    */
158   void OnKeyInputFocusLost() override;
159
160   /**
161    * @copydoc Control::OnAccessibilityActivated()
162    */
163   bool OnAccessibilityActivated() override;
164
165   /**
166    * @copydoc Control::OnTap()
167    */
168   void OnTap(const TapGesture& tap) override;
169
170   /**
171    * @copydoc Control::OnPan()
172    */
173   void OnPan(const PanGesture& gesture) override;
174
175   /**
176    * @copydoc Control::OnLongPress()
177    */
178   void OnLongPress(const LongPressGesture& gesture) override;
179
180   /**
181    * @copydoc Control::OnSceneConnection()
182    */
183   void OnSceneConnection(int depth) override;
184
185   /**
186    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
187    */
188   bool OnKeyEvent(const KeyEvent& event) override;
189
190   // From ControlInterface
191
192   /**
193    * @copydoc Text::ControlInterface::RequestTextRelayout()
194    */
195   void RequestTextRelayout() override;
196
197   // From EditableControlInterface
198
199   /**
200    * @copydoc Text::EditableControlInterface::TextChanged()
201    */
202   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
203
204   /**
205    * @copydoc Text::EditableControlInterface::TextDeleted()
206    */
207   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
208
209   /**
210    * @copydoc Text::EditableControlInterface::CursorMoved()
211    */
212   void CursorMoved(unsigned int position) override;
213
214   /**
215    * @copydoc Text::EditableControlInterface::TextChanged()
216    */
217   void TextChanged(bool immediate) override;
218
219   /**
220    * @copydoc Text::EditableControlInterface::MaxLengthReached()
221    */
222   void MaxLengthReached() override;
223
224   /**
225    * @copydoc Text::EditableControlInterface::InputStyleChanged()
226    */
227   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
228
229   /**
230    * @copydoc Text::EditableControlInterface::AddDecoration()
231    */
232   void AddDecoration(Actor& actor, bool needsClipping) override;
233
234   /**
235    * @copydoc Text::EditableControlInterface::InputFiltered()
236    */
237   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
238
239   /**
240    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
241    */
242   void GetControlBackgroundColor(Vector4& color) const override;
243
244   // From SelectableControlInterface
245 public:
246   /**
247    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
248    */
249   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
250
251   /**
252    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
253    */
254   Uint32Pair GetTextSelectionRange() const override;
255
256   /**
257    * @copydoc Text::SelectableControlInterface::SelectWholeText()
258    */
259   void SelectWholeText() override;
260
261   /**
262    * @copydoc Text::SelectableControlInterface::SelectNone()
263    */
264   void SelectNone() override;
265
266   /**
267    * @copydoc Text::SelectableControlInterface::GetSelectedText()
268    */
269   string GetSelectedText() const override;
270
271   /**
272    * @copydoc Text::EditableControlInterface::IsEditable()
273    */
274   bool IsEditable() const override;
275
276   /**
277    * @copydoc Text::EditableControlInterface::SetEditable()
278    */
279   void SetEditable(bool editable) override;
280
281   // From AnchorControlInterface
282
283   /**
284    * @copydoc Text::AnchorControlInterface::AnchorClicked()
285    */
286   void AnchorClicked(const std::string& href) override;
287
288 private: // Implementation
289   /**
290    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
291    */
292   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
293
294   /**
295    * @brief Callback when Clipboard signals an item should be pasted
296    * @param[in] clipboard handle to Clipboard Event Notifier
297    */
298   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
299
300   /**
301    * @brief Get a Property Map for the image used for the required Handle Image
302    * @param[out] value the returned image property
303    * @param[in] handleType the type of handle
304    * @param[in] handleImageType the type of image for the given handleType
305    */
306   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
307
308   /**
309    * @brief Enable or disable clipping.
310    */
311   void EnableClipping();
312
313   /**
314    * @brief Callback when keyboard is shown/hidden.
315    *
316    * @param[in] keyboardShown True if keyboard is shown.
317    */
318   void KeyboardStatusChanged(bool keyboardShown);
319
320   /**
321    * @brief Callback when TextField is touched
322    *
323    * @param[in] actor TextField touched
324    * @param[in] touch Touch information
325    */
326   bool OnTouched(Actor actor, const TouchEvent& touch);
327
328   /**
329    * @brief Callbacks called on idle.
330    *
331    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
332    */
333   void OnIdleSignal();
334
335   /**
336    * @brief Emits TextChanged signal.
337    */
338   void EmitTextChangedSignal();
339
340   /**
341    * @brief Callback function for when the layout is changed.
342    * @param[in] actor The actor whose layoutDirection is changed.
343    * @param[in] type  The layoutDirection.
344    */
345   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
346
347   /**
348    * Construct a new TextField.
349    */
350   TextField();
351
352   /**
353    * A reference counted object may only be deleted by calling Unreference()
354    */
355   virtual ~TextField();
356
357   // Undefined copy constructor and assignment operators
358   TextField(const TextField&);
359   TextField& operator=(const TextField& rhs);
360
361   /**
362    * @brief Resize actor to the given size.
363    *
364    * @param[in] actor The actor to be resized.
365    * @param[in] size Size to change.
366    */
367   void ResizeActor(Actor& actor, const Vector2& size);
368
369   /**
370    * @brief Render view, create and attach actor(s) to this Text Field.
371    */
372   void RenderText(Text::Controller::UpdateTextType updateTextType);
373
374   // Connection needed to re-render text, when a Text Field returns to the scene.
375   void OnSceneConnect(Dali::Actor actor);
376
377 private: // Data
378   // Signals
379   Toolkit::TextField::TextChangedSignalType        mTextChangedSignal;
380   Toolkit::TextField::MaxLengthReachedSignalType   mMaxLengthReachedSignal;
381   Toolkit::TextField::InputStyleChangedSignalType  mInputStyleChangedSignal;
382   Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
383   Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
384
385   InputMethodContext       mInputMethodContext;
386   Text::ControllerPtr      mController;
387   Text::RendererPtr        mRenderer;
388   Text::DecoratorPtr       mDecorator;
389   Toolkit::Control         mStencil;                  ///< For EXCEED_POLICY_CLIP
390   std::vector<Actor>       mClippingDecorationActors; ///< Decoration actors which need clipping.
391   Dali::InputMethodOptions mInputMethodOptions;
392
393   Actor         mRenderableActor;
394   Actor         mActiveLayer;
395   Actor         mBackgroundActor;
396   CallbackBase* mIdleCallback;
397
398   float mAlignmentOffset;
399   int   mRenderingBackend;
400   int   mExceedPolicy;
401   bool  mHasBeenStaged : 1;
402   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
403
404 protected:
405   /**
406    * @brief This structure is to connect TextField with Accessible functions.
407    */
408   struct AccessibleImpl : public DevelControl::AccessibleImpl,
409                           public virtual Dali::Accessibility::Text,
410                           public virtual Dali::Accessibility::EditableText
411   {
412     using DevelControl::AccessibleImpl::AccessibleImpl;
413
414     /**
415      * @copydoc Dali::Accessibility::Accessible::GetName()
416      */
417     std::string GetName() override;
418
419     /**
420      * @copydoc Dali::Accessibility::Text::GetText()
421      */
422     std::string GetText(size_t startOffset, size_t endOffset) override;
423
424     /**
425      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
426      */
427     size_t GetCharacterCount() override;
428
429     /**
430      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
431      */
432     size_t GetCursorOffset() override;
433
434     /**
435      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
436      */
437     bool SetCursorOffset(size_t offset) override;
438
439     /**
440      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
441      */
442     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
443
444     /**
445      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
446      */
447     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
448
449     /**
450      * @copydoc Dali::Accessibility::Text::RemoveSelection()
451      */
452     bool RemoveSelection(size_t selectionIndex) override;
453
454     /**
455      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
456      */
457     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
458
459     /**
460      * @copydoc Dali::Accessibility::EditableText::CopyText()
461      */
462     bool CopyText(size_t startPosition, size_t endPosition) override;
463
464     /**
465      * @copydoc Dali::Accessibility::EditableText::CutText()
466      */
467     bool CutText(size_t startPosition, size_t endPosition) override;
468
469     /**
470      * @copydoc Dali::Accessibility::Accessible::GetStates()
471      */
472     Accessibility::States CalculateStates() override;
473
474     /**
475      * @copydoc Dali::Accessibility::EditableText::InsertText()
476      */
477     bool InsertText(size_t startPosition, std::string text) override;
478
479     /**
480      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
481      */
482     bool SetTextContents(std::string newContents) override;
483
484     /**
485      * @copydoc Dali::Accessibility::EditableText::DeleteText()
486      */
487     bool DeleteText(size_t startPosition, size_t endPosition) override;
488   };
489 };
490
491 } // namespace Internal
492
493 // Helpers for public-api forwarding methods
494
495 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
496 {
497   DALI_ASSERT_ALWAYS(textField);
498
499   Dali::RefObject& handle = textField.GetImplementation();
500
501   return static_cast<Toolkit::Internal::TextField&>(handle);
502 }
503
504 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
505 {
506   DALI_ASSERT_ALWAYS(textField);
507
508   const Dali::RefObject& handle = textField.GetImplementation();
509
510   return static_cast<const Toolkit::Internal::TextField&>(handle);
511 }
512
513 } // namespace Toolkit
514
515 } // namespace Dali
516
517 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H