7eb5852a276ef14f8a2255f68066624e78cd80a4
[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    * @copydoc TextField::TextChangedSignal()
94    */
95   Toolkit::TextField::TextChangedSignalType& TextChangedSignal();
96
97   /**
98    * @copydoc TextField::MaxLengthReachedSignal()
99    */
100   Toolkit::TextField::MaxLengthReachedSignalType& MaxLengthReachedSignal();
101
102   /**
103    * @copydoc TextField::TextChangedSignal()
104    */
105   Toolkit::TextField::InputStyleChangedSignalType& InputStyleChangedSignal();
106
107   /**
108    * @copydoc TextField::AnchorClickedSignal()
109    */
110   DevelTextField::AnchorClickedSignalType& AnchorClickedSignal();
111
112   /**
113    * @copydoc TextField::InputFilteredSignal()
114    */
115   DevelTextField::InputFilteredSignalType& InputFilteredSignal();
116
117   Text::ControllerPtr getController();
118
119 private: // From Control
120   /**
121    * @copydoc Control::OnInitialize()
122    */
123   void OnInitialize() override;
124
125   /**
126    * @copydoc Control::OnStyleChange()
127    */
128   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
129
130   /**
131    * @copydoc Control::GetNaturalSize()
132    */
133   Vector3 GetNaturalSize() override;
134
135   /**
136    * @copydoc Control::GetHeightForWidth()
137    */
138   float GetHeightForWidth(float width) override;
139
140   /**
141    * @copydoc Control::OnInitialize()
142    */
143   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
144
145   /**
146    * @copydoc Control::OnKeyInputFocusGained()
147    */
148   void OnKeyInputFocusGained() override;
149
150   /**
151    * @copydoc Control::OnKeyInputFocusLost()
152    */
153   void OnKeyInputFocusLost() override;
154
155   /**
156    * @copydoc Control::OnAccessibilityActivated()
157    */
158   bool OnAccessibilityActivated() override;
159
160   /**
161    * @copydoc Control::OnTap()
162    */
163   void OnTap(const TapGesture& tap) override;
164
165   /**
166    * @copydoc Control::OnPan()
167    */
168   void OnPan(const PanGesture& gesture) override;
169
170   /**
171    * @copydoc Control::OnLongPress()
172    */
173   void OnLongPress(const LongPressGesture& gesture) override;
174
175   /**
176    * @copydoc Control::OnSceneConnection()
177    */
178   void OnSceneConnection(int depth) override;
179
180   /**
181    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
182    */
183   bool OnKeyEvent(const KeyEvent& event) override;
184
185   // From ControlInterface
186
187   /**
188    * @copydoc Text::ControlInterface::RequestTextRelayout()
189    */
190   void RequestTextRelayout() override;
191
192   // From EditableControlInterface
193
194   /**
195    * @copydoc Text::EditableControlInterface::TextChanged()
196    */
197   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
198
199   /**
200    * @copydoc Text::EditableControlInterface::TextDeleted()
201    */
202   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
203
204   /**
205    * @copydoc Text::EditableControlInterface::CaretMoved()
206    */
207   void CaretMoved(unsigned int position) override;
208
209   /**
210    * @copydoc Text::EditableControlInterface::TextChanged()
211    */
212   void TextChanged(bool immediate) override;
213
214   /**
215    * @copydoc Text::EditableControlInterface::MaxLengthReached()
216    */
217   void MaxLengthReached() override;
218
219   /**
220    * @copydoc Text::EditableControlInterface::InputStyleChanged()
221    */
222   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
223
224   /**
225    * @copydoc Text::EditableControlInterface::AddDecoration()
226    */
227   void AddDecoration(Actor& actor, bool needsClipping) override;
228
229   /**
230    * @copydoc Text::EditableControlInterface::InputFiltered()
231    */
232   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
233
234   /**
235    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
236    */
237   void GetControlBackgroundColor(Vector4& color) const override;
238
239   // From SelectableControlInterface
240 public:
241   /**
242    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
243    */
244   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
245
246   /**
247    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
248    */
249   Uint32Pair GetTextSelectionRange() const override;
250
251   /**
252    * @copydoc Text::SelectableControlInterface::SelectWholeText()
253    */
254   void SelectWholeText() override;
255
256   /**
257    * @copydoc Text::SelectableControlInterface::SelectNone()
258    */
259   void SelectNone() override;
260
261   /**
262    * @copydoc Text::SelectableControlInterface::GetSelectedText()
263    */
264   string GetSelectedText() const override;
265
266   /**
267    * @copydoc Text::EditableControlInterface::IsEditable()
268    */
269   bool IsEditable() const override;
270
271   /**
272    * @copydoc Text::EditableControlInterface::SetEditable()
273    */
274   void SetEditable(bool editable) override;
275
276   // From AnchorControlInterface
277
278   /**
279    * @copydoc Text::AnchorControlInterface::AnchorClicked()
280    */
281   void AnchorClicked(const std::string& href) override;
282
283 private: // Implementation
284   /**
285    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
286    */
287   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
288
289   /**
290    * @brief Callback when Clipboard signals an item should be pasted
291    * @param[in] clipboard handle to Clipboard Event Notifier
292    */
293   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
294
295   /**
296    * @brief Get a Property Map for the image used for the required Handle Image
297    * @param[out] value the returned image property
298    * @param[in] handleType the type of handle
299    * @param[in] handleImageType the type of image for the given handleType
300    */
301   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
302
303   /**
304    * @brief Enable or disable clipping.
305    */
306   void EnableClipping();
307
308   /**
309    * @brief Callback when keyboard is shown/hidden.
310    *
311    * @param[in] keyboardShown True if keyboard is shown.
312    */
313   void KeyboardStatusChanged(bool keyboardShown);
314
315   /**
316    * @brief Callback when TextField is touched
317    *
318    * @param[in] actor TextField touched
319    * @param[in] touch Touch information
320    */
321   bool OnTouched(Actor actor, const TouchEvent& touch);
322
323   /**
324    * @brief Callbacks called on idle.
325    *
326    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
327    */
328   void OnIdleSignal();
329
330   /**
331    * @brief Emits TextChanged signal.
332    */
333   void EmitTextChangedSignal();
334
335   /**
336    * @brief Callback function for when the layout is changed.
337    * @param[in] actor The actor whose layoutDirection is changed.
338    * @param[in] type  The layoutDirection.
339    */
340   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
341
342   /**
343    * Construct a new TextField.
344    */
345   TextField();
346
347   /**
348    * A reference counted object may only be deleted by calling Unreference()
349    */
350   virtual ~TextField();
351
352   // Undefined copy constructor and assignment operators
353   TextField(const TextField&);
354   TextField& operator=(const TextField& rhs);
355
356   /**
357    * @brief Resize actor to the given size.
358    *
359    * @param[in] actor The actor to be resized.
360    * @param[in] size Size to change.
361    */
362   void ResizeActor(Actor& actor, const Vector2& size);
363
364   /**
365    * @brief Render view, create and attach actor(s) to this Text Field.
366    */
367   void RenderText(Text::Controller::UpdateTextType updateTextType);
368
369   // Connection needed to re-render text, when a Text Field returns to the scene.
370   void OnSceneConnect(Dali::Actor actor);
371
372 public: // For UTC only
373   Text::ControllerPtr GetTextController()
374   {
375     return mController;
376   }
377
378 private: // Data
379   // Signals
380   Toolkit::TextField::TextChangedSignalType        mTextChangedSignal;
381   Toolkit::TextField::MaxLengthReachedSignalType   mMaxLengthReachedSignal;
382   Toolkit::TextField::InputStyleChangedSignalType  mInputStyleChangedSignal;
383   Toolkit::DevelTextField::AnchorClickedSignalType mAnchorClickedSignal;
384   Toolkit::DevelTextField::InputFilteredSignalType mInputFilteredSignal;
385
386   InputMethodContext       mInputMethodContext;
387   Text::ControllerPtr      mController;
388   Text::RendererPtr        mRenderer;
389   Text::DecoratorPtr       mDecorator;
390   Toolkit::Control         mStencil;                  ///< For EXCEED_POLICY_CLIP
391   std::vector<Actor>       mClippingDecorationActors; ///< Decoration actors which need clipping.
392   Dali::InputMethodOptions mInputMethodOptions;
393
394   Actor         mRenderableActor;
395   Actor         mActiveLayer;
396   Actor         mBackgroundActor;
397   CallbackBase* mIdleCallback;
398
399   float mAlignmentOffset;
400   int   mRenderingBackend;
401   int   mExceedPolicy;
402   bool  mHasBeenStaged : 1;
403   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
404
405 protected:
406   struct AccessibleImpl : public DevelControl::AccessibleImpl,
407                           public virtual Dali::Accessibility::Text,
408                           public virtual Dali::Accessibility::EditableText
409   {
410     using DevelControl::AccessibleImpl::AccessibleImpl;
411
412     std::string           GetName() override;
413     std::string           GetText(size_t startOffset, size_t endOffset) override;
414     size_t                GetCharacterCount() override;
415     size_t                GetCaretOffset() override;
416     bool                  SetCaretOffset(size_t offset) override;
417     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
418     Accessibility::Range  GetSelection(size_t selectionNum) override;
419     bool                  RemoveSelection(size_t selectionNum) override;
420     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
421     bool                  CopyText(size_t startPosition, size_t endPosition) override;
422     bool                  CutText(size_t startPosition, size_t endPosition) override;
423     Accessibility::States CalculateStates() override;
424     bool                  InsertText(size_t startPosition, std::string text) override;
425     bool                  SetTextContents(std::string newContents) override;
426     bool                  DeleteText(size_t startPosition, size_t endPosition) override;
427   };
428 };
429
430 } // namespace Internal
431
432 // Helpers for public-api forwarding methods
433
434 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
435 {
436   DALI_ASSERT_ALWAYS(textField);
437
438   Dali::RefObject& handle = textField.GetImplementation();
439
440   return static_cast<Toolkit::Internal::TextField&>(handle);
441 }
442
443 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
444 {
445   DALI_ASSERT_ALWAYS(textField);
446
447   const Dali::RefObject& handle = textField.GetImplementation();
448
449   return static_cast<const Toolkit::Internal::TextField&>(handle);
450 }
451
452 } // namespace Toolkit
453
454 } // namespace Dali
455
456 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H