8ebda422b2a45a54efc2edca50541571a948ab4e
[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   // From SelectableControlInterface
203 public:
204   /**
205    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
206    */
207   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
208
209   /**
210    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
211    */
212   Uint32Pair GetTextSelectionRange() const override;
213
214   /**
215    * @copydoc Text::SelectableControlInterface::SelectWholeText()
216    */
217   void SelectWholeText() override;
218
219   /**
220    * @copydoc Text::SelectableControlInterface::SelectNone()
221    */
222   void SelectNone() override;
223
224   /**
225    * @copydoc Text::SelectableControlInterface::GetSelectedText()
226    */
227   string GetSelectedText() const override;
228
229   /**
230    * @copydoc Text::EditableControlInterface::IsEditable()
231    */
232   bool IsEditable() const override;
233
234   /**
235    * @copydoc Text::EditableControlInterface::SetEditable()
236    */
237   void SetEditable(bool editable) override;
238
239 private: // Implementation
240   /**
241    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
242    */
243   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
244
245   /**
246    * @brief Callback when Clipboard signals an item should be pasted
247    * @param[in] clipboard handle to Clipboard Event Notifier
248    */
249   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
250
251   /**
252    * @brief Get a Property Map for the image used for the required Handle Image
253    * @param[out] value the returned image property
254    * @param[in] handleType the type of handle
255    * @param[in] handleImageType the type of image for the given handleType
256    */
257   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
258
259   /**
260    * @brief Callback when keyboard is shown/hidden.
261    *
262    * @param[in] keyboardShown True if keyboard is shown.
263    */
264   void KeyboardStatusChanged(bool keyboardShown);
265
266   /**
267    * @brief update scroll bar position
268    *
269    * If text scroll is occurred, create or update scroll bar position
270    */
271   void UpdateScrollBar();
272
273   /**
274    * @brief Callback when TextEditor is touched
275    *
276    * @param[in] actor TextEditor touched
277    * @param[in] touch Touch information
278    */
279   bool OnTouched(Actor actor, const TouchEvent& touch);
280
281   /**
282    * @brief Callbacks called on idle.
283    *
284    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
285    */
286   void OnIdleSignal();
287
288   /**
289    * @brief Emits TextChanged signal.
290    */
291   void EmitTextChangedSignal();
292
293   /**
294    * @brief set RenderActor's position with new scrollPosition
295    *
296    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
297    */
298   void ApplyScrollPosition();
299
300   /**
301    * @brief Callback function for ScrollBar indicator animation finished signal
302    *
303    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
304    */
305   void OnScrollIndicatorAnimationFinished(Animation& animation);
306
307   /**
308    * Construct a new TextEditor.
309    */
310   TextEditor();
311
312   /**
313    * A reference counted object may only be deleted by calling Unreference()
314    */
315   virtual ~TextEditor();
316
317   // Undefined copy constructor and assignment operators
318   TextEditor(const TextEditor&);
319   TextEditor& operator=(const TextEditor& rhs);
320
321   /**
322    * @brief Render view, create and attach actor(s) to this text editor.
323    */
324   void RenderText(Text::Controller::UpdateTextType updateTextType);
325
326   // Connection needed to re-render text, when a text editor returns to the scene.
327   void OnSceneConnect(Dali::Actor actor);
328
329 private: // Data
330   // Signals
331   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
332   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
333   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
334   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
335
336   InputMethodContext            mInputMethodContext;
337   Text::ControllerPtr           mController;
338   Text::RendererPtr             mRenderer;
339   Text::DecoratorPtr            mDecorator;
340   Text::TextVerticalScrollerPtr mTextVerticalScroller;
341   Toolkit::Control              mStencil;
342   Toolkit::ScrollBar            mScrollBar;
343   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
344   Dali::TimePeriod              mAnimationPeriod;
345   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
346
347   Actor         mRenderableActor;
348   Actor         mActiveLayer;
349   CallbackBase* mIdleCallback;
350
351   float mAlignmentOffset;
352   float mScrollAnimationDuration;
353   float mLineSpacing;
354   int   mRenderingBackend;
355   bool  mHasBeenStaged : 1;
356   bool  mScrollAnimationEnabled : 1;
357   bool  mScrollBarEnabled : 1;
358   bool  mScrollStarted : 1;
359   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
360 };
361
362 } // namespace Internal
363
364 // Helpers for public-api forwarding methods
365
366 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
367 {
368   DALI_ASSERT_ALWAYS(textEditor);
369
370   Dali::RefObject& handle = textEditor.GetImplementation();
371
372   return static_cast<Toolkit::Internal::TextEditor&>(handle);
373 }
374
375 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
376 {
377   DALI_ASSERT_ALWAYS(textEditor);
378
379   const Dali::RefObject& handle = textEditor.GetImplementation();
380
381   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
382 }
383
384 } // namespace Toolkit
385
386 } // namespace Dali
387
388 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H