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