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