Merge "Add parameter (bool immediate) to TextChanged signal in interface" into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-editor-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_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/accessibility.h>
23 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
24 #include <dali/devel-api/adaptor-framework/input-method-context.h>
25 #include <dali/public-api/animation/animation.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
30 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
33 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
34 #include <dali-toolkit/internal/text/text-control-interface.h>
35 #include <dali-toolkit/internal/text/text-controller.h>
36 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
37 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
38 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
39 #include <dali-toolkit/public-api/controls/control-impl.h>
40 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
41
42 namespace Dali
43 {
44 namespace Toolkit
45 {
46 namespace Internal
47 {
48 /**
49  * @brief A control which renders a long text string with styles.
50  */
51 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface
52 {
53 public:
54   /**
55    * @copydoc Dali::Toollkit::TextEditor::New()
56    */
57   static Toolkit::TextEditor New();
58
59   // Properties
60
61   /**
62    * @brief Called when a property of an object of this type is set.
63    *
64    * @param[in] object The object whose property is set.
65    * @param[in] index The property index.
66    * @param[in] value The new property value.
67    */
68   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
69
70   /**
71    * @brief Called to retrieve a property of an object of this type.
72    *
73    * @param[in] object The object whose property is to be retrieved.
74    * @param[in] index The property index.
75    * @return The current value of the property.
76    */
77   static Property::Value GetProperty(BaseObject* object, Property::Index index);
78
79   /**
80    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
81    */
82   InputMethodContext GetInputMethodContext();
83
84   /**
85    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
86    */
87   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
88
89   /**
90    * Connects a callback function with the object's signals.
91    * @param[in] object The object providing the signal.
92    * @param[in] tracker Used to disconnect the signal.
93    * @param[in] signalName The signal to connect to.
94    * @param[in] functor A newly allocated FunctorDelegate.
95    * @return True if the signal was connected.
96    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
97    */
98   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
99
100   /**
101    * @copydoc TextEditor::TextChangedSignal()
102    */
103   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
104
105   /**
106    * @copydoc TextEditor::TextChangedSignal()
107    */
108   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
109
110   /**
111    * @copydoc TextEditor::ScrollStateChangedSignal()
112    */
113   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
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 Dali::Toolkit::DevelTextEditor::ScrollBy()
249    */
250   void ScrollBy(Vector2 Scroll);
251
252   /**
253    * @brief Get Horizontal scroll position of TextEditor.
254    *
255    * @return Horizontal scroll position (in pixels) of TextEditor.
256    */
257   float GetHorizontalScrollPosition();
258
259   /**
260    * @brief Get Vertical scroll position of TextEditor.
261    *
262    * @return Vertical scroll position (in pixels) of TextEditor.
263    */
264   float GetVerticalScrollPosition();
265
266   /**
267    * @copydoc Text::SelectableControlInterface::GetSelectedText()
268    */
269   string GetSelectedText() const override;
270
271   /**
272    * @copydoc Text::EditableControlInterface::IsEditable()
273    */
274   bool IsEditable() const override;
275
276   /**
277    * @copydoc Text::EditableControlInterface::SetEditable()
278    */
279   void                SetEditable(bool editable) override;
280   Text::ControllerPtr getController();
281
282 private: // Implementation
283   /**
284    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
285    */
286   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
287
288   /**
289    * @brief Callback when Clipboard signals an item should be pasted
290    * @param[in] clipboard handle to Clipboard Event Notifier
291    */
292   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
293
294   /**
295    * @brief Get a Property Map for the image used for the required Handle Image
296    * @param[out] value the returned image property
297    * @param[in] handleType the type of handle
298    * @param[in] handleImageType the type of image for the given handleType
299    */
300   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
301
302   /**
303    * @brief Callback when keyboard is shown/hidden.
304    *
305    * @param[in] keyboardShown True if keyboard is shown.
306    */
307   void KeyboardStatusChanged(bool keyboardShown);
308
309   /**
310    * @brief update scroll bar position
311    *
312    * If text scroll is occurred, create or update scroll bar position
313    */
314   void UpdateScrollBar();
315
316   /**
317    * @brief Callback when TextEditor is touched
318    *
319    * @param[in] actor TextEditor touched
320    * @param[in] touch Touch information
321    */
322   bool OnTouched(Actor actor, const TouchEvent& touch);
323
324   /**
325    * @brief Callbacks called on idle.
326    *
327    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
328    */
329   void OnIdleSignal();
330
331   /**
332    * @brief Emits TextChanged signal.
333    */
334   void EmitTextChangedSignal();
335
336   /**
337    * @brief set RenderActor's position with new scrollPosition
338    *
339    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
340    */
341   void ApplyScrollPosition();
342
343   /**
344    * @brief Callback function for ScrollBar indicator animation finished signal
345    *
346    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
347    */
348   void OnScrollIndicatorAnimationFinished(Animation& animation);
349
350   /**
351    * Construct a new TextEditor.
352    */
353   TextEditor();
354
355   /**
356    * A reference counted object may only be deleted by calling Unreference()
357    */
358   virtual ~TextEditor();
359
360   // Undefined copy constructor and assignment operators
361   TextEditor(const TextEditor&);
362   TextEditor& operator=(const TextEditor& rhs);
363
364   /**
365    * @brief Resize actor to the given size.
366    *
367    * @param[in] actor The actor to be resized.
368    * @param[in] size Size to change.
369    */
370   void ResizeActor(Actor& actor, const Vector2& size);
371
372   /**
373    * @brief Render view, create and attach actor(s) to this text editor.
374    */
375   void RenderText(Text::Controller::UpdateTextType updateTextType);
376
377   // Connection needed to re-render text, when a text editor returns to the scene.
378   void OnSceneConnect(Dali::Actor actor);
379
380 private: // Data
381   // Signals
382   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
383   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
384   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
385   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
386
387   InputMethodContext            mInputMethodContext;
388   Text::ControllerPtr           mController;
389   Text::RendererPtr             mRenderer;
390   Text::DecoratorPtr            mDecorator;
391   Text::TextVerticalScrollerPtr mTextVerticalScroller;
392   Toolkit::Control              mStencil;
393   Toolkit::ScrollBar            mScrollBar;
394   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
395   Dali::TimePeriod              mAnimationPeriod;
396   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
397
398   Actor         mRenderableActor;
399   Actor         mActiveLayer;
400   CallbackBase* mIdleCallback;
401
402   float mAlignmentOffset;
403   float mScrollAnimationDuration;
404   float mLineSpacing;
405   int   mRenderingBackend;
406   bool  mHasBeenStaged : 1;
407   bool  mScrollAnimationEnabled : 1;
408   bool  mScrollBarEnabled : 1;
409   bool  mScrollStarted : 1;
410   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
411
412   struct AccessibleImpl : public DevelControl::AccessibleImpl,
413                           public virtual Dali::Accessibility::Text,
414                           public virtual Dali::Accessibility::EditableText
415   {
416     using DevelControl::AccessibleImpl::AccessibleImpl;
417
418     std::string           GetName() override;
419     std::string           GetText(size_t startOffset, size_t endOffset) override;
420     size_t                GetCharacterCount() override;
421     size_t                GetCaretOffset() override;
422     bool                  SetCaretOffset(size_t offset) override;
423     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
424     Accessibility::Range  GetSelection(size_t selectionNum) override;
425     bool                  RemoveSelection(size_t selectionNum) override;
426     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
427     bool                  CopyText(size_t startPosition, size_t endPosition) override;
428     bool                  CutText(size_t startPosition, size_t endPosition) override;
429     Accessibility::States CalculateStates() override;
430   };
431 };
432
433 } // namespace Internal
434
435 // Helpers for public-api forwarding methods
436
437 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
438 {
439   DALI_ASSERT_ALWAYS(textEditor);
440
441   Dali::RefObject& handle = textEditor.GetImplementation();
442
443   return static_cast<Toolkit::Internal::TextEditor&>(handle);
444 }
445
446 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
447 {
448   DALI_ASSERT_ALWAYS(textEditor);
449
450   const Dali::RefObject& handle = textEditor.GetImplementation();
451
452   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
453 }
454
455 } // namespace Toolkit
456
457 } // namespace Dali
458
459 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H