1fb17a9c83ea02e496ef83df835a4d354c84fd20
[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/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/devel-api/controls/scroll-bar/scroll-bar.h>
28 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
29 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
30 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
31 #include <dali-toolkit/internal/text/text-control-interface.h>
32 #include <dali-toolkit/internal/text/text-controller.h>
33 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
34 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
35 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
36 #include <dali-toolkit/public-api/controls/control-impl.h>
37 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Internal
44 {
45 /**
46  * @brief A control which renders a long text string with styles.
47  */
48 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface
49 {
50 public:
51   /**
52    * @copydoc Dali::Toollkit::TextEditor::New()
53    */
54   static Toolkit::TextEditor New();
55
56   // Properties
57
58   /**
59    * @brief Called when a property of an object of this type is set.
60    *
61    * @param[in] object The object whose property is set.
62    * @param[in] index The property index.
63    * @param[in] value The new property value.
64    */
65   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
66
67   /**
68    * @brief Called to retrieve a property of an object of this type.
69    *
70    * @param[in] object The object whose property is to be retrieved.
71    * @param[in] index The property index.
72    * @return The current value of the property.
73    */
74   static Property::Value GetProperty(BaseObject* object, Property::Index index);
75
76   /**
77    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
78    */
79   InputMethodContext GetInputMethodContext();
80
81   /**
82    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
83    */
84   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
85
86   /**
87    * Connects a callback function with the object's signals.
88    * @param[in] object The object providing the signal.
89    * @param[in] tracker Used to disconnect the signal.
90    * @param[in] signalName The signal to connect to.
91    * @param[in] functor A newly allocated FunctorDelegate.
92    * @return True if the signal was connected.
93    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
94    */
95   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
96
97   /**
98    * @copydoc TextEditor::TextChangedSignal()
99    */
100   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
101
102   /**
103    * @copydoc TextEditor::TextChangedSignal()
104    */
105   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
106
107   /**
108    * @copydoc TextEditor::ScrollStateChangedSignal()
109    */
110   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
111
112 private: // From Control
113   /**
114    * @copydoc Control::OnInitialize()
115    */
116   void OnInitialize() override;
117
118   /**
119    * @copydoc Control::OnStyleChange()
120    */
121   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
122
123   /**
124    * @copydoc Control::GetNaturalSize()
125    */
126   Vector3 GetNaturalSize() override;
127
128   /**
129    * @copydoc Control::GetHeightForWidth()
130    */
131   float GetHeightForWidth(float width) override;
132
133   /**
134    * @copydoc Control::OnInitialize()
135    */
136   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
137
138   /**
139    * @copydoc Control::OnKeyInputFocusGained()
140    */
141   void OnKeyInputFocusGained() override;
142
143   /**
144    * @copydoc Control::OnKeyInputFocusLost()
145    */
146   void OnKeyInputFocusLost() 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 TextChanged(bool immediate) override;
186
187   /**
188    * @copydoc Text::EditableControlInterface::MaxLengthReached()
189    */
190   void MaxLengthReached() override;
191
192   /**
193    * @copydoc Text::EditableControlInterface::InputStyleChanged()
194    */
195   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
196
197   /**
198    * @copydoc Text::EditableControlInterface::AddDecoration()
199    */
200   void AddDecoration(Actor& actor, bool needsClipping) override;
201
202   /**
203    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
204    */
205   void GetControlBackgroundColor(Vector4& color) const override;
206
207   // From SelectableControlInterface
208 public:
209   /**
210    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
211    */
212   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
213
214   /**
215    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
216    */
217   Uint32Pair GetTextSelectionRange() const override;
218
219   /**
220    * @copydoc Text::SelectableControlInterface::SelectWholeText()
221    */
222   void SelectWholeText() override;
223
224   /**
225    * @copydoc Text::SelectableControlInterface::SelectNone()
226    */
227   void SelectNone() override;
228
229   /**
230    * @copydoc Text::SelectableControlInterface::GetSelectedText()
231    */
232   string GetSelectedText() const override;
233
234   /**
235    * @copydoc Text::EditableControlInterface::IsEditable()
236    */
237   bool IsEditable() const override;
238
239   /**
240    * @copydoc Text::EditableControlInterface::SetEditable()
241    */
242   void SetEditable(bool editable) override;
243
244 private: // Implementation
245   /**
246    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
247    */
248   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
249
250   /**
251    * @brief Callback when Clipboard signals an item should be pasted
252    * @param[in] clipboard handle to Clipboard Event Notifier
253    */
254   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
255
256   /**
257    * @brief Get a Property Map for the image used for the required Handle Image
258    * @param[out] value the returned image property
259    * @param[in] handleType the type of handle
260    * @param[in] handleImageType the type of image for the given handleType
261    */
262   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
263
264   /**
265    * @brief Callback when keyboard is shown/hidden.
266    *
267    * @param[in] keyboardShown True if keyboard is shown.
268    */
269   void KeyboardStatusChanged(bool keyboardShown);
270
271   /**
272    * @brief update scroll bar position
273    *
274    * If text scroll is occurred, create or update scroll bar position
275    */
276   void UpdateScrollBar();
277
278   /**
279    * @brief Callback when TextEditor is touched
280    *
281    * @param[in] actor TextEditor touched
282    * @param[in] touch Touch information
283    */
284   bool OnTouched(Actor actor, const TouchEvent& touch);
285
286   /**
287    * @brief Callbacks called on idle.
288    *
289    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
290    */
291   void OnIdleSignal();
292
293   /**
294    * @brief Emits TextChanged signal.
295    */
296   void EmitTextChangedSignal();
297
298   /**
299    * @brief set RenderActor's position with new scrollPosition
300    *
301    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
302    */
303   void ApplyScrollPosition();
304
305   /**
306    * @brief Callback function for ScrollBar indicator animation finished signal
307    *
308    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
309    */
310   void OnScrollIndicatorAnimationFinished(Animation& animation);
311
312   /**
313    * Construct a new TextEditor.
314    */
315   TextEditor();
316
317   /**
318    * A reference counted object may only be deleted by calling Unreference()
319    */
320   virtual ~TextEditor();
321
322   // Undefined copy constructor and assignment operators
323   TextEditor(const TextEditor&);
324   TextEditor& operator=(const TextEditor& rhs);
325
326   /**
327    * @brief Render view, create and attach actor(s) to this text editor.
328    */
329   void RenderText(Text::Controller::UpdateTextType updateTextType);
330
331   // Connection needed to re-render text, when a text editor returns to the scene.
332   void OnSceneConnect(Dali::Actor actor);
333
334 private: // Data
335   // Signals
336   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
337   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
338   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
339   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
340
341   InputMethodContext            mInputMethodContext;
342   Text::ControllerPtr           mController;
343   Text::RendererPtr             mRenderer;
344   Text::DecoratorPtr            mDecorator;
345   Text::TextVerticalScrollerPtr mTextVerticalScroller;
346   Toolkit::Control              mStencil;
347   Toolkit::ScrollBar            mScrollBar;
348   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
349   Dali::TimePeriod              mAnimationPeriod;
350   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
351
352   Actor         mRenderableActor;
353   Actor         mActiveLayer;
354   CallbackBase* mIdleCallback;
355
356   float mAlignmentOffset;
357   float mScrollAnimationDuration;
358   float mLineSpacing;
359   int   mRenderingBackend;
360   bool  mHasBeenStaged : 1;
361   bool  mScrollAnimationEnabled : 1;
362   bool  mScrollBarEnabled : 1;
363   bool  mScrollStarted : 1;
364   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
365 };
366
367 } // namespace Internal
368
369 // Helpers for public-api forwarding methods
370
371 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
372 {
373   DALI_ASSERT_ALWAYS(textEditor);
374
375   Dali::RefObject& handle = textEditor.GetImplementation();
376
377   return static_cast<Toolkit::Internal::TextEditor&>(handle);
378 }
379
380 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
381 {
382   DALI_ASSERT_ALWAYS(textEditor);
383
384   const Dali::RefObject& handle = textEditor.GetImplementation();
385
386   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
387 }
388
389 } // namespace Toolkit
390
391 } // namespace Dali
392
393 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H