Support Public APIs - CopyText, CutText & PasteText
[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::CursorPositionChangedSignal()
121    */
122   DevelTextField::CursorPositionChangedSignalType& CursorPositionChangedSignal();
123
124   /**
125    * @copydoc TextField::InputFilteredSignal()
126    */
127   DevelTextField::InputFilteredSignalType& InputFilteredSignal();
128
129   /**
130    * @copydoc TextField::SelectionChangedSignal()
131    */
132   DevelTextField::SelectionChangedSignalType& SelectionChangedSignal();
133
134 private: // From Control
135   /**
136    * @copydoc Control::OnInitialize()
137    */
138   void OnInitialize() override;
139
140   /**
141    * @copydoc Control::OnStyleChange()
142    */
143   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
144
145   /**
146    * @copydoc Control::GetNaturalSize()
147    */
148   Vector3 GetNaturalSize() override;
149
150   /**
151    * @copydoc Control::GetHeightForWidth()
152    */
153   float GetHeightForWidth(float width) override;
154
155   /**
156    * @copydoc Control::OnInitialize()
157    */
158   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
159
160   /**
161    * @copydoc Control::OnKeyInputFocusGained()
162    */
163   void OnKeyInputFocusGained() override;
164
165   /**
166    * @copydoc Control::OnKeyInputFocusLost()
167    */
168   void OnKeyInputFocusLost() override;
169
170   /**
171    * @copydoc Control::OnAccessibilityActivated()
172    */
173   bool OnAccessibilityActivated() override;
174
175   /**
176    * @copydoc Control::OnTap()
177    */
178   void OnTap(const TapGesture& tap) override;
179
180   /**
181    * @copydoc Control::OnPan()
182    */
183   void OnPan(const PanGesture& gesture) override;
184
185   /**
186    * @copydoc Control::OnLongPress()
187    */
188   void OnLongPress(const LongPressGesture& gesture) override;
189
190   /**
191    * @copydoc Control::OnSceneConnection()
192    */
193   void OnSceneConnection(int depth) override;
194
195   /**
196    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
197    */
198   bool OnKeyEvent(const KeyEvent& event) override;
199
200   // From ControlInterface
201
202   /**
203    * @copydoc Text::ControlInterface::RequestTextRelayout()
204    */
205   void RequestTextRelayout() override;
206
207   // From EditableControlInterface
208
209   /**
210    * @copydoc Text::EditableControlInterface::TextChanged()
211    */
212   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
213
214   /**
215    * @copydoc Text::EditableControlInterface::TextDeleted()
216    */
217   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
218
219   /**
220    * @copydoc Text::EditableControlInterface::CursorPositionChanged()
221    */
222   void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
223
224   /**
225    * @copydoc Text::EditableControlInterface::TextChanged()
226    */
227   void TextChanged(bool immediate) override;
228
229   /**
230    * @copydoc Text::EditableControlInterface::MaxLengthReached()
231    */
232   void MaxLengthReached() override;
233
234   /**
235    * @copydoc Text::EditableControlInterface::InputStyleChanged()
236    */
237   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
238
239   /**
240    * @copydoc Text::SelectableControlInterface::SelectionChanged()
241    */
242   void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
243
244   /**
245    * @copydoc Text::EditableControlInterface::AddDecoration()
246    */
247   void AddDecoration(Actor& actor, bool needsClipping) override;
248
249   /**
250    * @copydoc Text::EditableControlInterface::InputFiltered()
251    */
252   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
253
254   /**
255    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
256    */
257   void GetControlBackgroundColor(Vector4& color) const override;
258
259   // From SelectableControlInterface
260 public:
261   /**
262    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
263    */
264   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
265
266   /**
267    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
268    */
269   Uint32Pair GetTextSelectionRange() const override;
270
271   /**
272    * @copydoc Text::SelectableControlInterface::SelectWholeText()
273    */
274   void SelectWholeText() override;
275
276   /**
277    * @copydoc Text::SelectableControlInterface::SelectNone()
278    */
279   void SelectNone() override;
280
281   /**
282    * @copydoc Text::SelectableControlInterface::SelectText()
283    */
284   void SelectText(const uint32_t start, const uint32_t end) override;
285
286   /**
287    * @copydoc Text::SelectableControlInterface::GetSelectedText()
288    */
289   string GetSelectedText() const override;
290
291   /**
292    * @copydoc Text::EditableControlInterface::IsEditable()
293    */
294   bool IsEditable() const override;
295
296   /**
297    * @copydoc Text::EditableControlInterface::SetEditable()
298    */
299   void SetEditable(bool editable) override;
300
301   /**
302    * @copydoc Dali::EditableControlInterface::CopyText()
303    */
304   string CopyText() override;
305
306   /**
307    * @copydoc Dali::EditableControlInterface::CutText()
308    */
309   string CutText() override;
310
311   /**
312    * @copydoc Text::EditableControlInterface::PasteText()
313    */
314   void PasteText() override;
315
316   // From AnchorControlInterface
317
318   /**
319    * @copydoc Text::AnchorControlInterface::AnchorClicked()
320    */
321   void AnchorClicked(const std::string& href) override;
322
323 private: // Implementation
324   /**
325    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
326    */
327   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
328
329   /**
330    * @brief Callback when Clipboard signals an item should be pasted
331    * @param[in] clipboard handle to Clipboard Event Notifier
332    */
333   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
334
335   /**
336    * @brief Get a Property Map for the image used for the required Handle Image
337    * @param[out] value the returned image property
338    * @param[in] handleType the type of handle
339    * @param[in] handleImageType the type of image for the given handleType
340    */
341   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
342
343   /**
344    * @brief Enable or disable clipping.
345    */
346   void EnableClipping();
347
348   /**
349    * @brief Callback when keyboard is shown/hidden.
350    *
351    * @param[in] keyboardShown True if keyboard is shown.
352    */
353   void KeyboardStatusChanged(bool keyboardShown);
354
355   /**
356    * @brief Callback when TextField is touched
357    *
358    * @param[in] actor TextField touched
359    * @param[in] touch Touch information
360    */
361   bool OnTouched(Actor actor, const TouchEvent& touch);
362
363   /**
364    * @brief Callbacks called on idle.
365    *
366    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
367    */
368   void OnIdleSignal();
369
370   /**
371    * @brief Emits TextChanged signal.
372    */
373   void EmitTextChangedSignal();
374
375   /**
376    * @brief Emits CursorPositionChanged signal.
377    */
378   void EmitCursorPositionChangedSignal();
379
380   /**
381    * @brief Emits SelectionChanged signal.
382    */
383   void EmitSelectionChangedSignal();
384
385   /**
386    * @brief Callback function for when the layout is changed.
387    * @param[in] actor The actor whose layoutDirection is changed.
388    * @param[in] type  The layoutDirection.
389    */
390   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
391
392   /**
393    * Construct a new TextField.
394    */
395   TextField();
396
397   /**
398    * A reference counted object may only be deleted by calling Unreference()
399    */
400   virtual ~TextField();
401
402   // Undefined copy constructor and assignment operators
403   TextField(const TextField&);
404   TextField& operator=(const TextField& rhs);
405
406   /**
407    * @brief Resize actor to the given size.
408    *
409    * @param[in] actor The actor to be resized.
410    * @param[in] size Size to change.
411    */
412   void ResizeActor(Actor& actor, const Vector2& size);
413
414   /**
415    * @brief Render view, create and attach actor(s) to this Text Field.
416    */
417   void RenderText(Text::Controller::UpdateTextType updateTextType);
418
419   // Connection needed to re-render text, when a Text Field returns to the scene.
420   void OnSceneConnect(Dali::Actor actor);
421
422 private: // Data
423   // Signals
424   Toolkit::TextField::TextChangedSignalType                mTextChangedSignal;
425   Toolkit::TextField::MaxLengthReachedSignalType           mMaxLengthReachedSignal;
426   Toolkit::TextField::InputStyleChangedSignalType          mInputStyleChangedSignal;
427   Toolkit::DevelTextField::AnchorClickedSignalType         mAnchorClickedSignal;
428   Toolkit::DevelTextField::InputFilteredSignalType         mInputFilteredSignal;
429   Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
430   Toolkit::DevelTextField::SelectionChangedSignalType      mSelectionChangedSignal;
431
432   InputMethodContext       mInputMethodContext;
433   Text::ControllerPtr      mController;
434   Text::RendererPtr        mRenderer;
435   Text::DecoratorPtr       mDecorator;
436   Toolkit::Control         mStencil;                  ///< For EXCEED_POLICY_CLIP
437   std::vector<Actor>       mClippingDecorationActors; ///< Decoration actors which need clipping.
438   Dali::InputMethodOptions mInputMethodOptions;
439
440   Actor         mRenderableActor;
441   Actor         mActiveLayer;
442   Actor         mBackgroundActor;
443   CallbackBase* mIdleCallback;
444
445   float mAlignmentOffset;
446   int   mRenderingBackend;
447   int   mExceedPolicy;
448   bool  mHasBeenStaged : 1;
449   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
450   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
451   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
452
453   //args for cursor position changed event
454   unsigned int mOldPosition;
455
456   //args for selection changed event
457   uint32_t mOldSelectionStart;
458   uint32_t mOldSelectionEnd;
459
460 protected:
461   /**
462    * @brief This structure is to connect TextField with Accessible functions.
463    */
464   struct AccessibleImpl : public DevelControl::AccessibleImpl,
465                           public virtual Dali::Accessibility::Text,
466                           public virtual Dali::Accessibility::EditableText
467   {
468     using DevelControl::AccessibleImpl::AccessibleImpl;
469
470     /**
471      * @copydoc Dali::Accessibility::Accessible::GetName()
472      */
473     std::string GetName() override;
474
475     /**
476      * @copydoc Dali::Accessibility::Text::GetText()
477      */
478     std::string GetText(size_t startOffset, size_t endOffset) override;
479
480     /**
481      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
482      */
483     size_t GetCharacterCount() override;
484
485     /**
486      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
487      */
488     size_t GetCursorOffset() override;
489
490     /**
491      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
492      */
493     bool SetCursorOffset(size_t offset) override;
494
495     /**
496      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
497      */
498     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
499
500     /**
501      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
502      */
503     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
504
505     /**
506      * @copydoc Dali::Accessibility::Text::RemoveSelection()
507      */
508     bool RemoveSelection(size_t selectionIndex) override;
509
510     /**
511      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
512      */
513     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
514
515     /**
516      * @copydoc Dali::Accessibility::EditableText::CopyText()
517      */
518     bool CopyText(size_t startPosition, size_t endPosition) override;
519
520     /**
521      * @copydoc Dali::Accessibility::EditableText::CutText()
522      */
523     bool CutText(size_t startPosition, size_t endPosition) override;
524
525     /**
526      * @copydoc Dali::Accessibility::Accessible::GetStates()
527      */
528     Accessibility::States CalculateStates() override;
529
530     /**
531      * @copydoc Dali::Accessibility::EditableText::InsertText()
532      */
533     bool InsertText(size_t startPosition, std::string text) override;
534
535     /**
536      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
537      */
538     bool SetTextContents(std::string newContents) override;
539
540     /**
541      * @copydoc Dali::Accessibility::EditableText::DeleteText()
542      */
543     bool DeleteText(size_t startPosition, size_t endPosition) override;
544   };
545 };
546
547 } // namespace Internal
548
549 // Helpers for public-api forwarding methods
550
551 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
552 {
553   DALI_ASSERT_ALWAYS(textField);
554
555   Dali::RefObject& handle = textField.GetImplementation();
556
557   return static_cast<Toolkit::Internal::TextField&>(handle);
558 }
559
560 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
561 {
562   DALI_ASSERT_ALWAYS(textField);
563
564   const Dali::RefObject& handle = textField.GetImplementation();
565
566   return static_cast<const Toolkit::Internal::TextField&>(handle);
567 }
568
569 } // namespace Toolkit
570
571 } // namespace Dali
572
573 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H