Merge branch 'devel/master' into devel/graphics
[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() 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 set RenderActor's position with new scrollPosition
333    *
334    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
335    */
336   void ApplyScrollPosition();
337
338   /**
339    * @brief Callback function for ScrollBar indicator animation finished signal
340    *
341    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
342    */
343   void OnScrollIndicatorAnimationFinished(Animation& animation);
344
345   /**
346    * Construct a new TextEditor.
347    */
348   TextEditor();
349
350   /**
351    * A reference counted object may only be deleted by calling Unreference()
352    */
353   virtual ~TextEditor();
354
355   // Undefined copy constructor and assignment operators
356   TextEditor(const TextEditor&);
357   TextEditor& operator=(const TextEditor& rhs);
358
359   /**
360    * @brief Resize actor to the given size.
361    *
362    * @param[in] actor The actor to be resized.
363    * @param[in] size Size to change.
364    */
365   void ResizeActor(Actor& actor, const Vector2& size);
366
367   /**
368    * @brief Render view, create and attach actor(s) to this text editor.
369    */
370   void RenderText(Text::Controller::UpdateTextType updateTextType);
371
372   // Connection needed to re-render text, when a text editor returns to the scene.
373   void OnSceneConnect(Dali::Actor actor);
374
375 private: // Data
376   // Signals
377   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
378   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
379   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
380   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
381
382   InputMethodContext            mInputMethodContext;
383   Text::ControllerPtr           mController;
384   Text::RendererPtr             mRenderer;
385   Text::DecoratorPtr            mDecorator;
386   Text::TextVerticalScrollerPtr mTextVerticalScroller;
387   Toolkit::Control              mStencil;
388   Toolkit::ScrollBar            mScrollBar;
389   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
390   Dali::TimePeriod              mAnimationPeriod;
391   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
392
393   Actor         mRenderableActor;
394   Actor         mActiveLayer;
395   CallbackBase* mIdleCallback;
396
397   float mAlignmentOffset;
398   float mScrollAnimationDuration;
399   float mLineSpacing;
400   int   mRenderingBackend;
401   bool  mHasBeenStaged : 1;
402   bool  mScrollAnimationEnabled : 1;
403   bool  mScrollBarEnabled : 1;
404   bool  mScrollStarted : 1;
405   bool  mTextChanged : 1;
406
407   struct AccessibleImpl : public DevelControl::AccessibleImpl,
408                           public virtual Dali::Accessibility::Text,
409                           public virtual Dali::Accessibility::EditableText
410   {
411     using DevelControl::AccessibleImpl::AccessibleImpl;
412
413     std::string           GetName() override;
414     std::string           GetText(size_t startOffset, size_t endOffset) override;
415     size_t                GetCharacterCount() override;
416     size_t                GetCaretOffset() override;
417     bool                  SetCaretOffset(size_t offset) override;
418     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
419     Accessibility::Range  GetSelection(size_t selectionNum) override;
420     bool                  RemoveSelection(size_t selectionNum) override;
421     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
422     bool                  CopyText(size_t startPosition, size_t endPosition) override;
423     bool                  CutText(size_t startPosition, size_t endPosition) override;
424     Accessibility::States CalculateStates() override;
425   };
426 };
427
428 } // namespace Internal
429
430 // Helpers for public-api forwarding methods
431
432 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
433 {
434   DALI_ASSERT_ALWAYS(textEditor);
435
436   Dali::RefObject& handle = textEditor.GetImplementation();
437
438   return static_cast<Toolkit::Internal::TextEditor&>(handle);
439 }
440
441 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
442 {
443   DALI_ASSERT_ALWAYS(textEditor);
444
445   const Dali::RefObject& handle = textEditor.GetImplementation();
446
447   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
448 }
449
450 } // namespace Toolkit
451
452 } // namespace Dali
453
454 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H