10f21886004268bff232f418f2575e0736beb80e
[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 #include <dali/devel-api/adaptor-framework/autofill-item.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/controls/control-devel.h>
28 #include <dali-toolkit/devel-api/controls/text-controls/text-field-devel.h>
29 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
31 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
32 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
33 #include <dali-toolkit/internal/text/text-control-interface.h>
34 #include <dali-toolkit/internal/text/text-controller.h>
35 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
36 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
37 #include <dali-toolkit/public-api/controls/control-impl.h>
38 #include <dali-toolkit/public-api/controls/text-controls/text-field.h>
39
40 namespace Dali
41 {
42 namespace Toolkit
43 {
44 namespace Internal
45 {
46 /**
47  * @brief A control which renders a short text string.
48  */
49 class TextField : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
50 {
51 public:
52   /**
53    * @copydoc Dali::Toollkit::TextField::New()
54    */
55   static Toolkit::TextField New();
56
57   // Properties
58
59   /**
60    * @brief Called when a property of an object of this type is set.
61    *
62    * @param[in] object The object whose property is set.
63    * @param[in] index The property index.
64    * @param[in] value The new property value.
65    */
66   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
67
68   /**
69    * @brief Called to retrieve a property of an object of this type.
70    *
71    * @param[in] object The object whose property is to be retrieved.
72    * @param[in] index The property index.
73    * @return The current value of the property.
74    */
75   static Property::Value GetProperty(BaseObject* object, Property::Index index);
76
77   /**
78    * @copydoc Dali::Toollkit::TextField::GetInputMethodContext()
79    */
80   InputMethodContext GetInputMethodContext();
81
82   /**
83    * Connects a callback function with the object's signals.
84    * @param[in] object The object providing the signal.
85    * @param[in] tracker Used to disconnect the signal.
86    * @param[in] signalName The signal to connect to.
87    * @param[in] functor A newly allocated FunctorDelegate.
88    * @return True if the signal was connected.
89    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
90    */
91   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
92
93   /**
94    * @copydoc TextField::TextChangedSignal()
95    */
96   Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
97
98   /**
99    * @copydoc TextField::MaxLengthReachedSignal()
100    */
101   Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
102
103   /**
104    * @copydoc TextField::TextChangedSignal()
105    */
106   Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
107
108   /**
109    * @copydoc TextField::AnchorClickedSignal()
110    */
111   DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
112
113   Text::ControllerPtr getController();
114
115 private: // From Control
116   /**
117    * @copydoc Control::OnInitialize()
118    */
119   void OnInitialize() override;
120
121   /**
122    * @copydoc Control::OnStyleChange()
123    */
124   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
125
126   /**
127    * @copydoc Control::GetNaturalSize()
128    */
129   Vector3 GetNaturalSize() override;
130
131   /**
132    * @copydoc Control::GetHeightForWidth()
133    */
134   float GetHeightForWidth(float width) override;
135
136   /**
137    * @copydoc Control::OnInitialize()
138    */
139   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
140
141   /**
142    * @copydoc Control::OnKeyInputFocusGained()
143    */
144   void OnKeyInputFocusGained() override;
145
146   /**
147    * @copydoc Control::OnKeyInputFocusLost()
148    */
149   void OnKeyInputFocusLost() override;
150
151   /**
152    * @copydoc Control::OnAccessibilityActivated()
153    */
154   bool OnAccessibilityActivated() override;
155
156   /**
157    * @copydoc Control::OnTap()
158    */
159   void OnTap(const TapGesture& tap) override;
160
161   /**
162    * @copydoc Control::OnPan()
163    */
164   void OnPan(const PanGesture& gesture) override;
165
166   /**
167    * @copydoc Control::OnLongPress()
168    */
169   void OnLongPress(const LongPressGesture& gesture) override;
170
171   /**
172    * @copydoc Control::OnSceneConnection()
173    */
174   void OnSceneConnection(int depth) override;
175
176   /**
177    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
178    */
179   bool OnKeyEvent(const KeyEvent& event) override;
180
181   // From ControlInterface
182
183   /**
184    * @copydoc Text::ControlInterface::RequestTextRelayout()
185    */
186   void RequestTextRelayout() override;
187
188   // From EditableControlInterface
189
190   /**
191    * @copydoc Text::EditableControlInterface::TextChanged()
192    */
193   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
194
195   /**
196    * @copydoc Text::EditableControlInterface::TextDeleted()
197    */
198   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
199
200   /**
201    * @copydoc Text::EditableControlInterface::CaretMoved()
202    */
203   void CaretMoved(unsigned int position) override;
204
205   /**
206    * @copydoc Text::EditableControlInterface::TextChanged()
207    */
208   void TextChanged(bool immediate) override;
209
210   /**
211    * @copydoc Text::EditableControlInterface::MaxLengthReached()
212    */
213   void MaxLengthReached() override;
214
215   /**
216    * @copydoc Text::EditableControlInterface::InputStyleChanged()
217    */
218   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
219
220   /**
221    * @copydoc Text::EditableControlInterface::AddDecoration()
222    */
223   void AddDecoration(Actor& actor, bool needsClipping) override;
224
225   // From SelectableControlInterface
226 public:
227   /**
228    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
229    */
230   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
231
232   /**
233    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
234    */
235   Uint32Pair GetTextSelectionRange() const override;
236
237   /**
238    * @copydoc Text::SelectableControlInterface::SelectWholeText()
239    */
240   void SelectWholeText() override;
241
242   /**
243    * @copydoc Text::SelectableControlInterface::SelectNone()
244    */
245   void SelectNone() override;
246
247   /**
248    * @copydoc Text::SelectableControlInterface::GetSelectedText()
249    */
250   string GetSelectedText() const override;
251
252   /**
253    * @copydoc Text::EditableControlInterface::IsEditable()
254    */
255   bool IsEditable() const override;
256
257   /**
258    * @copydoc Text::EditableControlInterface::SetEditable()
259    */
260   void SetEditable(bool editable) override;
261
262   // From AnchorControlInterface
263
264   /**
265    * @copydoc Text::AnchorControlInterface::AnchorClicked()
266    */
267   void AnchorClicked(const std::string& href) override;
268
269 private: // Implementation
270   /**
271    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
272    */
273   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
274
275   /**
276    * @brief Callback when Clipboard signals an item should be pasted
277    * @param[in] clipboard handle to Clipboard Event Notifier
278    */
279   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
280
281   /**
282    * @brief Get a Property Map for the image used for the required Handle Image
283    * @param[out] value the returned image property
284    * @param[in] handleType the type of handle
285    * @param[in] handleImageType the type of image for the given handleType
286    */
287   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
288
289   /**
290    * @brief Enable or disable clipping.
291    */
292   void EnableClipping();
293
294   /**
295    * @brief Callback when keyboard is shown/hidden.
296    *
297    * @param[in] keyboardShown True if keyboard is shown.
298    */
299   void KeyboardStatusChanged(bool keyboardShown);
300
301   /**
302    * @brief Callback when TextField is touched
303    *
304    * @param[in] actor TextField touched
305    * @param[in] touch Touch information
306    */
307   bool OnTouched(Actor actor, const TouchEvent& touch);
308
309   /**
310    * @brief Callbacks called on idle.
311    *
312    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
313    */
314   void OnIdleSignal();
315
316   /**
317    * @brief Emits TextChanged signal.
318    */
319   void EmitTextChangedSignal();
320
321   /**
322    * Construct a new TextField.
323    */
324   TextField();
325
326   /**
327    * A reference counted object may only be deleted by calling Unreference()
328    */
329   virtual ~TextField();
330
331   // Undefined copy constructor and assignment operators
332   TextField(const TextField&);
333   TextField& operator=(const TextField& rhs);
334
335   /**
336    * @brief Resize actor to the given size.
337    *
338    * @param[in] actor The actor to be resized.
339    * @param[in] size Size to change.
340    */
341   void ResizeActor(Actor& actor, const Vector2& size);
342
343   /**
344    * @brief Render view, create and attach actor(s) to this Text Field.
345    */
346   void RenderText(Text::Controller::UpdateTextType updateTextType);
347
348   // Connection needed to re-render text, when a Text Field returns to the scene.
349   void OnSceneConnect(Dali::Actor actor);
350
351 public: // For UTC only
352   Text::ControllerPtr GetTextController()
353   {
354     return mController;
355   }
356
357 private: // Data
358   // Signals
359   Toolkit::TextField::TextChangedSignalType        mTextChangedSignal;
360   Toolkit::TextField::MaxLengthReachedSignalType   mMaxLengthReachedSignal;
361   Toolkit::TextField::InputStyleChangedSignalType  mInputStyleChangedSignal;
362   Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
363
364   InputMethodContext       mInputMethodContext;
365   Text::ControllerPtr      mController;
366   Text::RendererPtr        mRenderer;
367   Text::DecoratorPtr       mDecorator;
368   Toolkit::Control         mStencil;                  ///< For EXCEED_POLICY_CLIP
369   std::vector<Actor>       mClippingDecorationActors; ///< Decoration actors which need clipping.
370   Dali::InputMethodOptions mInputMethodOptions;
371   Dali::AutofillItem mAutofillItem;
372
373   Actor         mRenderableActor;
374   Actor         mActiveLayer;
375   Actor         mBackgroundActor;
376   CallbackBase* mIdleCallback;
377
378   float mAlignmentOffset;
379   int   mRenderingBackend;
380   int   mExceedPolicy;
381   bool  mHasBeenStaged : 1;
382   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
383
384 protected:
385   struct AccessibleImpl : public DevelControl::AccessibleImpl,
386                           public virtual Dali::Accessibility::Text,
387                           public virtual Dali::Accessibility::EditableText
388   {
389     using DevelControl::AccessibleImpl::AccessibleImpl;
390
391     std::string           GetName() override;
392     std::string           GetText(size_t startOffset, size_t endOffset) override;
393     size_t                GetCharacterCount() override;
394     size_t                GetCaretOffset() override;
395     bool                  SetCaretOffset(size_t offset) override;
396     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
397     Accessibility::Range  GetSelection(size_t selectionNum) override;
398     bool                  RemoveSelection(size_t selectionNum) override;
399     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
400     bool                  CopyText(size_t startPosition, size_t endPosition) override;
401     bool                  CutText(size_t startPosition, size_t endPosition) override;
402     Accessibility::States CalculateStates() override;
403     bool                  InsertText(size_t startPosition, std::string text) override;
404     bool                  SetTextContents(std::string newContents) override;
405     bool                  DeleteText(size_t startPosition, size_t endPosition) override;
406   };
407 };
408
409 } // namespace Internal
410
411 // Helpers for public-api forwarding methods
412
413 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
414 {
415   DALI_ASSERT_ALWAYS(textField);
416
417   Dali::RefObject& handle = textField.GetImplementation();
418
419   return static_cast<Toolkit::Internal::TextField&>(handle);
420 }
421
422 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
423 {
424   DALI_ASSERT_ALWAYS(textField);
425
426   const Dali::RefObject& handle = textField.GetImplementation();
427
428   return static_cast<const Toolkit::Internal::TextField&>(handle);
429 }
430
431 } // namespace Toolkit
432
433 } // namespace Dali
434
435 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H