d51ba2a09109f5bad65bfcae2b9dc2a3bead0fec
[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/public-api/animation/animation.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
30 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
31 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
32 #include <dali-toolkit/internal/text/text-control-interface.h>
33 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
34 #include <dali-toolkit/internal/text/text-controller.h>
35 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
36 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
37
38 namespace Dali
39 {
40
41 namespace Toolkit
42 {
43
44 namespace Internal
45 {
46 /**
47  * @brief A control which renders a long text string with styles.
48  */
49 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface
50 {
51 public:
52
53   /**
54    * @copydoc Dali::Toollkit::TextEditor::New()
55    */
56   static Toolkit::TextEditor New();
57
58   // Properties
59
60   /**
61    * @brief Called when a property of an object of this type is set.
62    *
63    * @param[in] object The object whose property is set.
64    * @param[in] index The property index.
65    * @param[in] value The new property value.
66    */
67   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
68
69   /**
70    * @brief Called to retrieve a property of an object of this type.
71    *
72    * @param[in] object The object whose property is to be retrieved.
73    * @param[in] index The property index.
74    * @return The current value of the property.
75    */
76   static Property::Value GetProperty( BaseObject* object, Property::Index index );
77
78   /**
79    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
80    */
81   InputMethodContext GetInputMethodContext();
82
83   /**
84    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
85    */
86   DevelTextEditor::MaxLengthReachedSignalType&  MaxLengthReachedSignal();
87
88   /**
89    * Connects a callback function with the object's signals.
90    * @param[in] object The object providing the signal.
91    * @param[in] tracker Used to disconnect the signal.
92    * @param[in] signalName The signal to connect to.
93    * @param[in] functor A newly allocated FunctorDelegate.
94    * @return True if the signal was connected.
95    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
96    */
97   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
98
99   /**
100    * @copydoc TextEditor::TextChangedSignal()
101    */
102   Toolkit::TextEditor::TextChangedSignalType&  TextChangedSignal();
103
104   /**
105    * @copydoc TextEditor::TextChangedSignal()
106    */
107   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
108
109   /**
110    * @copydoc TextEditor::ScrollStateChangedSignal()
111    */
112   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
113
114 private: // From Control
115
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::OnTap()
153    */
154   void OnTap( const TapGesture& tap ) override;
155
156   /**
157    * @copydoc Control::OnPan()
158    */
159   void OnPan( const PanGesture& gesture ) override;
160
161   /**
162    * @copydoc Control::OnLongPress()
163    */
164   void OnLongPress( const LongPressGesture& gesture ) override;
165
166   /**
167    * @copydoc Control::OnSceneConnection()
168    */
169   void OnSceneConnection( int depth ) override;
170
171   /**
172    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
173    */
174   bool OnKeyEvent(const KeyEvent& event) override;
175
176 // From ControlInterface
177
178   /**
179    * @copydoc Text::ControlInterface::RequestTextRelayout()
180    */
181   void RequestTextRelayout() override;
182
183 // From EditableControlInterface
184
185   /**
186    * @copydoc Text::ControlInterface::TextChanged()
187    */
188   void TextChanged() override;
189
190   /**
191    * @copydoc Text::ControlInterface::MaxLengthReached()
192    */
193   void MaxLengthReached() override;
194
195   /**
196    * @copydoc Text::ControlInterface::InputStyleChanged()
197    */
198   void InputStyleChanged( Text::InputStyle::Mask inputStyleMask ) override;
199
200   /**
201    * @copydoc Text::ControlInterface::AddDecoration()
202    */
203   void AddDecoration( Actor& actor, bool needsClipping ) override;
204
205 private: // Implementation
206
207   /**
208    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
209    */
210   InputMethodContext::CallbackData OnInputMethodContextEvent( InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent );
211
212   /**
213    * @brief Callback when Clipboard signals an item should be pasted
214    * @param[in] clipboard handle to Clipboard Event Notifier
215    */
216   void OnClipboardTextSelected( ClipboardEventNotifier& clipboard );
217
218   /**
219    * @brief Get a Property Map for the image used for the required Handle Image
220    * @param[out] value the returned image property
221    * @param[in] handleType the type of handle
222    * @param[in] handleImageType the type of image for the given handleType
223    */
224   void GetHandleImagePropertyValue(  Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType );
225
226   /**
227    * @brief Callback when keyboard is shown/hidden.
228    *
229    * @param[in] keyboardShown True if keyboard is shown.
230    */
231   void KeyboardStatusChanged( bool keyboardShown );
232
233   /**
234    * @brief update scroll bar position
235    *
236    * If text scroll is occurred, create or update scroll bar position
237    */
238   void UpdateScrollBar();
239
240   /**
241    * @brief Callback when TextEditor is touched
242    *
243    * @param[in] actor TextEditor touched
244    * @param[in] touch Touch information
245    */
246   bool OnTouched( Actor actor, const TouchEvent& touch );
247
248   /**
249    * @brief Callbacks called on idle.
250    *
251    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
252    */
253   void OnIdleSignal();
254
255   /**
256    * @brief set RenderActor's position with new scrollPosition
257    *
258    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
259    */
260   void ApplyScrollPosition();
261
262   /**
263    * @brief Callback function for ScrollBar indicator animation finished signal
264    *
265    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
266    */
267   void OnScrollIndicatorAnimationFinished( Animation& animation );
268
269   /**
270    * Construct a new TextEditor.
271    */
272   TextEditor();
273
274   /**
275    * A reference counted object may only be deleted by calling Unreference()
276    */
277   virtual ~TextEditor();
278
279   // Undefined copy constructor and assignment operators
280   TextEditor(const TextEditor&);
281   TextEditor& operator=(const TextEditor& rhs);
282
283   /**
284    * @brief Render view, create and attach actor(s) to this text editor.
285    */
286   void RenderText( Text::Controller::UpdateTextType updateTextType );
287
288   // Connection needed to re-render text, when a text editor returns to the scene.
289   void OnSceneConnect( Dali::Actor actor );
290
291 private: // Data
292   // Signals
293   Toolkit::TextEditor::TextChangedSignalType mTextChangedSignal;
294   Toolkit::TextEditor::InputStyleChangedSignalType mInputStyleChangedSignal;
295   Toolkit::TextEditor::ScrollStateChangedSignalType mScrollStateChangedSignal;
296   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
297
298   InputMethodContext mInputMethodContext;
299   Text::ControllerPtr mController;
300   Text::RendererPtr mRenderer;
301   Text::DecoratorPtr mDecorator;
302   Text::TextVerticalScrollerPtr mTextVerticalScroller;
303   Toolkit::Control mStencil;
304   Toolkit::ScrollBar mScrollBar;
305   Dali::Animation mAnimation;                                              ///< Scroll indicator Show/Hide Animation.
306   Dali::TimePeriod mAnimationPeriod;
307   std::vector<Actor> mClippingDecorationActors;   ///< Decoration actors which need clipping.
308
309   Actor mRenderableActor;
310   Actor mActiveLayer;
311   CallbackBase* mIdleCallback;
312
313   float mAlignmentOffset;
314   float mScrollAnimationDuration;
315   float mLineSpacing;
316   int mRenderingBackend;
317   bool mHasBeenStaged:1;
318   bool mScrollAnimationEnabled:1;
319   bool mScrollBarEnabled:1;
320   bool mScrollStarted:1;
321 };
322
323 } // namespace Internal
324
325 // Helpers for public-api forwarding methods
326
327 inline Toolkit::Internal::TextEditor& GetImpl( Toolkit::TextEditor& textEditor )
328 {
329   DALI_ASSERT_ALWAYS(textEditor);
330
331   Dali::RefObject& handle = textEditor.GetImplementation();
332
333   return static_cast<Toolkit::Internal::TextEditor&>(handle);
334 }
335
336 inline const Toolkit::Internal::TextEditor& GetImpl( const Toolkit::TextEditor& textEditor )
337 {
338   DALI_ASSERT_ALWAYS(textEditor);
339
340   const Dali::RefObject& handle = textEditor.GetImplementation();
341
342   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
343 }
344
345 } // namespace Toolkit
346
347 } // namespace Dali
348
349 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H