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