Merge "Add text selection popup style" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / decorator / text-decorator.h
1 #ifndef DALI_TOOLKIT_TEXT_DECORATOR_H
2 #define DALI_TOOLKIT_TEXT_DECORATOR_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/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/math/rect.h>
24 #include <dali/public-api/object/ref-object.h>
25 #include <dali/public-api/object/property-map.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
29
30 namespace Dali
31 {
32 struct Vector2;
33 struct Vector4;
34
35 namespace Toolkit
36 {
37 namespace Text
38 {
39 class Decorator;
40 typedef IntrusivePtr<Decorator> DecoratorPtr;
41
42 // Used to set the cursor positions etc.
43 enum Cursor
44 {
45   PRIMARY_CURSOR,   ///< The primary cursor for bidirectional text (or the regular cursor for single-direction text)
46   SECONDARY_CURSOR, ///< The secondary cursor for bidirectional text
47   CURSOR_COUNT
48 };
49
50 // Determines which of the cursors are active (if any).
51 enum ActiveCursor
52 {
53   ACTIVE_CURSOR_NONE,    ///< Neither primary nor secondary cursor are active
54   ACTIVE_CURSOR_PRIMARY, ///< Primary cursor is active (only)
55   ACTIVE_CURSOR_BOTH     ///< Both primary and secondary cursor are active
56 };
57
58 // The state information for handle events.
59 enum HandleState
60 {
61   HANDLE_TAPPED,
62   HANDLE_PRESSED,
63   HANDLE_RELEASED,
64   HANDLE_SCROLLING,
65   HANDLE_STOP_SCROLLING
66 };
67
68 // Used to set different handle images
69 enum HandleImageType
70 {
71   HANDLE_IMAGE_PRESSED,
72   HANDLE_IMAGE_RELEASED,
73   HANDLE_IMAGE_TYPE_COUNT
74 };
75
76 // Types of handles.
77 enum HandleType
78 {
79   GRAB_HANDLE,
80   LEFT_SELECTION_HANDLE,
81   RIGHT_SELECTION_HANDLE,
82   LEFT_SELECTION_HANDLE_MARKER,
83   RIGHT_SELECTION_HANDLE_MARKER,
84   HANDLE_TYPE_COUNT
85 };
86
87 // Types of decoration.
88 enum DecorationType
89 {
90   NONE_LAYER,
91   ACTIVE_LAYER,
92   CURSOR_LAYER
93 };
94
95 /**
96  * @brief A Text Decorator is used to display cursors, handles, selection highlights and pop-ups.
97  *
98  * The decorator is responsible for clipping decorations which are positioned outside of the parent area.
99  *
100  * The Popup decoration will be positioned either above the Grab handle or above the selection handles but if doing so
101  * would cause the Popup to exceed the Decoration Bounding Box ( see SetBoundingBox API ) the the Popup will be repositioned below the handle(s).
102  *
103  * Selection handles will be flipped around to ensure they do not exceed the Decoration Bounding Box. ( Stay visible ).
104  *
105  * Decorator components forward input events to a controller class through an interface.
106  * The controller is responsible for selecting which components are active.
107  */
108 class Decorator : public RefObject
109 {
110 public:
111   class ControllerInterface
112   {
113   public:
114     /**
115      * @brief Constructor.
116      */
117     ControllerInterface(){};
118
119     /**
120      * @brief Virtual destructor.
121      */
122     virtual ~ControllerInterface(){};
123
124     /**
125      * @brief Query the target size of the UI control.
126      *
127      * @param[out] targetSize The size of the UI control the decorator is adding it's decorations to.
128      */
129     virtual void GetTargetSize(Vector2& targetSize) = 0;
130
131     /**
132      * @brief Add a decoration to the parent UI control.
133      *
134      * @param[in] actor The actor displaying a decoration.
135      * @param[in] type Whether this decoration is a layer or not, which layer it is.
136      * @param[in] needsClipping Whether the actor needs clipping.
137      */
138     virtual void AddDecoration(Actor& actor, DecorationType type, bool needsClipping) = 0;
139
140     /**
141      * @brief An input event from one of the handles.
142      *
143      * @param[in] handleType The handle's type.
144      * @param[in] state The handle's state.
145      * @param[in] x The x position relative to the top-left of the parent control.
146      * @param[in] y The y position relative to the top-left of the parent control.
147      */
148     virtual void DecorationEvent(HandleType handleType, HandleState state, float x, float y) = 0;
149   };
150
151   /**
152    * @brief Create a new instance of a Decorator.
153    *
154    * @param[in] controller The controller which receives input events from Decorator components.
155    * @param[in] callbackInterface The text popup callback interface which receives the button click callbacks.
156    *
157    * @return A pointer to a new Decorator.
158    */
159   static DecoratorPtr New(ControllerInterface&                 controller,
160                           TextSelectionPopupCallbackInterface& callbackInterface);
161
162   /**
163    * @brief Set the bounding box which handles, popup and similar decorations will not exceed.
164    *
165    * The default value is the width and height of the stage from the top left origin.
166    * If a title bar for example is on the top of the screen then the y should be the title's height and
167    * the boundary height the stage height minus the title's height.
168    * Restrictions - The boundary box should be set up with a fixed z position for the text-input and the default camera.
169    *
170    * ------------------------------------------
171    * |(x,y)                                   |
172    * |o---------------------------------------|
173    * ||                                      ||
174    * ||            Bounding Box              || boundary height
175    * ||                                      ||
176    * |----------------------------------------|
177    * ------------------------------------------
178    *               boundary width
179    *
180    * @param[in] boundingBox Vector( x coordinate, y coordinate, width, height )
181    */
182   void SetBoundingBox(const Rect<int>& boundingBox);
183
184   /**
185    * @brief Retrieve the bounding box origin and dimensions.
186    *
187    * default is set once control is added to stage, before this the return vector will be Vector4:ZERO
188    * @param[out] boundingBox The bounding box origin, width and height.
189    */
190   void GetBoundingBox(Rect<int>& boundingBox) const;
191
192   /**
193    * @brief The decorator waits until a relayout before creating actors etc.
194    *
195    * @param[in] size The size of the parent control after size-negotiation.
196    */
197   void Relayout(const Dali::Vector2& size);
198
199   /**
200    * @brief Updates the decorator's actor positions after scrolling.
201    *
202    * @param[in] scrollOffset The scroll offset.
203    */
204   void UpdatePositions(const Vector2& scrollOffset);
205
206   /**
207    * @brief Sets which of the cursors are active.
208    *
209    * @note Cursor will only be visible if within the parent area.
210    * @param[in] activeCursor Which of the cursors should be active (if any).
211    */
212   void SetActiveCursor(ActiveCursor activeCursor);
213
214   /**
215    * @brief Query which of the cursors are active.
216    *
217    * @return  Which of the cursors are active (if any).
218    */
219   unsigned int GetActiveCursor() const;
220
221   /**
222    * @brief Sets the position of a cursor.
223    *
224    * @param[in] cursor The cursor to set.
225    * @param[in] x The x position relative to the top-left of the parent control.
226    * @param[in] y The y position relative to the top-left of the parent control.
227    * @param[in] cursorHeight The logical height of the cursor.
228    * @param[in] lineHeight The logical height of the line.
229    */
230   void SetPosition(Cursor cursor, float x, float y, float cursorHeight, float lineHeight);
231
232   /**
233    * @brief Retrieves the position, height and lineHeight of a cursor.
234    *
235    * @param[in] cursor The cursor to get.
236    * @param[out] x The x position relative to the top-left of the parent control.
237    * @param[out] y The y position relative to the top-left of the parent control.
238    * @param[out] cursorHeight The logical height of the cursor.
239    * @param[out] lineHeight The logical height of the line.
240    */
241   void GetPosition(Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight) const;
242
243   /**
244    * @brief Retrieves the position of a cursor.
245    *
246    * @param[in] cursor The cursor to get.
247    *
248    * @return The position.
249    */
250   const Vector2& GetPosition(Cursor cursor) const;
251
252   /**
253    * @brief Sets the glyph offset of a cursor.
254    *
255    * @param[in] cursor The cursor to set.
256    * @param[in] glyphoffset The difference of line ascender and glyph ascender.
257    */
258   void SetGlyphOffset(Cursor cursor, float glyphOffset);
259
260   /**
261    * @brief Retrieves the glyph offset of a cursor.
262    *
263    * @param[in] cursor The cursor to get.
264    *
265    * @return The glyph offset. glyph offset means difference of line ascender and glyph ascender.
266    */
267   const float GetGlyphOffset(Cursor cursor) const;
268
269   /**
270    * @brief Sets the color for a cursor.
271    *
272    * @param[in] cursor Whether this color is for the primary or secondary cursor.
273    * @param[in] color The color to use.
274    */
275   void SetCursorColor(Cursor cursor, const Dali::Vector4& color);
276
277   /**
278    * @brief Retrieves the color for a cursor.
279    *
280    * @param[in] cursor Whether this color is for the primary or secondary cursor.
281    * @return The cursor color.
282    */
283   const Dali::Vector4& GetColor(Cursor cursor) const;
284
285   /**
286    * @brief Start blinking the cursor; see also SetCursorBlinkDuration().
287    */
288   void StartCursorBlink();
289
290   /**
291    * @brief Stop blinking the cursor.
292    */
293   void StopCursorBlink();
294
295   /**
296    * @brief Temporarily stops the cursor from blinking.
297    */
298   void DelayCursorBlink();
299
300   /**
301    * @brief Set the interval between cursor blinks.
302    *
303    * @param[in] seconds The interval in seconds.
304    */
305   void SetCursorBlinkInterval(float seconds);
306
307   /**
308    * @brief Retrieves the blink-interval for a cursor.
309    *
310    * @return The cursor blink-interval in seconds.
311    */
312   float GetCursorBlinkInterval() const;
313
314   /**
315    * @brief The cursor will stop blinking after this duration.
316    *
317    * @param[in] seconds The duration in seconds.
318    */
319   void SetCursorBlinkDuration(float seconds);
320
321   /**
322    * @brief Retrieves the blink-duration for a cursor.
323    *
324    * @return The cursor blink-duration in seconds.
325    */
326   float GetCursorBlinkDuration() const;
327
328   /**
329    * @brief Sets the width of the cursors.
330    *
331    * @param[in] width The width of the cursor in pixels.
332    */
333   void SetCursorWidth(int width);
334
335   /**
336    * @brief Retrieves the width of the cursors.
337    *
338    * @return The width of the cursors in pixels.
339    */
340   int GetCursorWidth() const;
341
342   /**
343    * @brief Sets whether a handle is active.
344    *
345    * @param[in] handleType One of the handles.
346    * @param[in] active True if the handle should be active.
347    */
348   void SetHandleActive(HandleType handleType,
349                        bool       active);
350
351   /**
352    * @brief Query whether a handle is active.
353    *
354    * @param[in] handleType One of the handles.
355    *
356    * @return True if the handle is active.
357    */
358   bool IsHandleActive(HandleType handleType) const;
359
360   /**
361    * @brief Sets the image file name for one of the handles.
362    *
363    * @param[in] handleType One of the handles.
364    * @param[in] handleImageType A different image can be set for the pressed/released states.
365    * @param[in] imageFileName The image filename to use.
366    */
367   void SetHandleImage(HandleType handleType, HandleImageType handleImageType, const std::string& imageFileName);
368
369   /**
370    * @brief Retrieves the file name of the image for one of the handles.
371    *
372    * @param[in] handleType One of the handles.
373    * @param[in] handleImageType A different image can be set for the pressed/released states.
374    *
375    * @return The grab handle image string.
376    */
377   const std::string& GetHandleImage(HandleType handleType, HandleImageType handleImageType) const;
378
379   /**
380    * @brief Sets the color of the handles
381    *
382    * @param[in] color The color to use.
383    */
384   void SetHandleColor(const Vector4& color);
385
386   /**
387    * @brief Retrieves the handles color.
388    *
389    * @return The color of the handles.
390    */
391   const Vector4& GetHandleColor() const;
392
393   /**
394    * @brief Sets the position of a selection handle.
395    *
396    * @param[in] handleType The handle to set.
397    * @param[in] x The x position relative to the top-left of the parent control.
398    * @param[in] y The y position relative to the top-left of the parent control.
399    * @param[in] lineHeight The logical line height at this position.
400    */
401   void SetPosition(HandleType handleType, float x, float y, float lineHeight);
402
403   /**
404    * @brief Retrieves the position of a selection handle.
405    *
406    * @param[in] handleType The handle to get.
407    * @param[out] x The x position relative to the top-left of the parent control.
408    * @param[out] y The y position relative to the top-left of the parent control.
409    * @param[out] lineHeight The logical line height at this position.
410    */
411   void GetPosition(HandleType handleType, float& x, float& y, float& lineHeight) const;
412
413   /**
414    * @brief Retrieves the position of a selection handle.
415    *
416    * @param[in] handleType The handle to get.
417    *
418    * @return The position of the selection handle relative to the top-left of the parent control.
419    */
420   const Vector2& GetPosition(HandleType handleType) const;
421
422   /**
423    * @brief Whether to flip vertically a handle.
424    *
425    * @param[in] handleType The handle to flip vertically.
426    * @param[in] flip Whether to flip vertically.
427    */
428   void FlipHandleVertically(HandleType handleType, bool flip);
429
430   /**
431    * @brief Retrieves whether the handle is vertically flipped.
432    *
433    * @param[in] handleType The handle to query.
434    *
435    * @return @e ture if the handle is vertically flipped.
436    */
437   bool IsHandleVerticallyFlipped(HandleType handleType) const;
438
439   /**
440    * @brief Whether to flip the selection handles as soon as they are crossed.
441    *
442    * By default they flip when the handle is released.
443    *
444    * @param[in] enable If @e true the selection handles will flip as soon as they are crossed.
445    */
446   void FlipSelectionHandlesOnCrossEnabled(bool enable);
447
448   /**
449    * @brief Sets info to calculate the handle flip state.
450    *
451    * Sets the character's direction where the handles are pointing.
452    * It resets the decorator internal flip state when there is a new selection.
453    *
454    * @param[in] indicesSwapped Whether the selection handle indices are swapped (start > end).
455    * @param[in] left The direction of the character pointed by the primary selection handle.
456    * @param[in] right The direction of the character pointed by the secondary selection handle.
457    */
458   void SetSelectionHandleFlipState(bool indicesSwapped, bool left, bool right);
459
460   /**
461    * @brief Adds a quad to the existing selection highlights. Vertices are in decorator's coordinates.
462    *
463    * @param[in] index Position in the vector where to add the quad.
464    * @param[in] quad The quad. The 'x' and 'y' coordinates store the min 'x' and min 'y'. The 'z' and 'w' coordinates store the max 'x' and max 'y'.
465    */
466   void AddHighlight(unsigned int index, const Vector4& quad);
467
468   /**
469    * @brief Sets the min 'x,y' coordinates and the size of the highlighted box.
470    *
471    * It's used to set the size and position of the highlight's actor and to translate each highlight quad from
472    * decorator's coordinates to the local coords of the highlight's actor.
473    *
474    * @param[in] position The position of the highlighted text in decorator's coords.
475    * @param[in] size The size of the highlighted text.
476    * @param[in] outlineOffset The outline's offset.
477    */
478   void SetHighLightBox(const Vector2& position,
479                        const Size&    size,
480                        float          outlineOffset);
481
482   /**
483    * @brief Removes all of the previously added highlights.
484    */
485   void ClearHighlights();
486
487   /**
488    * @brief Reserves space for the highlight quads.
489    *
490    * @param[in] numberOfQuads The expected number of quads.
491    */
492   void ResizeHighlightQuads(unsigned int numberOfQuads);
493
494   /**
495    * @brief Sets the selection highlight color.
496    *
497    * @param[in] color The color to use.
498    */
499   void SetHighlightColor(const Vector4& color);
500
501   /**
502    * @brief Retrieves the selection highlight color.
503    *
504    * @return The color of the highlight
505    */
506   const Vector4& GetHighlightColor() const;
507
508   /**
509    * @brief Sets whether the highlight is active.
510    *
511    * @param[in] active Whether the highlight is active.
512    */
513   void SetHighlightActive(bool active);
514
515   /**
516    * @brief Retrieves whether the highlight is active.
517    *
518    * @return @e true if the highlight is active, @e false otherwise.
519    */
520   bool IsHighlightActive() const;
521
522   /**
523    * @brief Retreives whether the highlight is shown or not.
524    *
525    * @return true if the highlight is visible, false otherwise.
526    */
527   bool IsHighlightVisible() const;
528
529   /**
530    * @brief Sets into the decorator the depth used to render the text.
531    *
532    * @param[in] depth The text's depth.
533    */
534   void SetTextDepth(int textDepth);
535
536   /**
537    * @brief Set the Selection Popup to show or hide via the active flaf
538    * @param[in] active true to show, false to hide
539    */
540   void SetPopupActive(bool active);
541
542   /**
543    * @brief Query whether the Selection Popup is active.
544    *
545    * @return True if the Selection Popup should be active.
546    */
547   bool IsPopupActive() const;
548
549   /**
550    * @brief Set a bit mask of the buttons to be shown by Popup
551    * @param[in] enabledButtonsBitMask from TextSelectionPopup::Buttons enum
552    */
553   void SetEnabledPopupButtons(TextSelectionPopup::Buttons& enabledButtonsBitMask);
554
555   /**
556    * @brief Get the current bit mask of buttons to be shown by Popup
557    * @return bitmask of TextSelectionPopup::Buttons
558    */
559   TextSelectionPopup::Buttons& GetEnabledPopupButtons();
560
561   /**
562    * @brief Used to set the selection popup options
563    *
564    * @param[in] options The property map of selection popup options
565    */
566   void SetSelectionPopupStyle(const Property::Map& options);
567
568   /**
569    * @brief Used to get the selection popup options
570    *
571    * @param[out] options The property map of selection popup options
572    */
573   void GetSelectionPopupStyle(Property::Map& options);
574
575   /**
576    * @brief Sets the scroll threshold.
577    *
578    * It defines a square area inside the control, close to the edge.
579    * When the cursor enters this area, the decorator starts to send scroll events.
580    *
581    * @param[in] threshold The scroll threshold in pixels.
582    */
583   void SetScrollThreshold(float threshold);
584
585   /**
586    * @brief Retrieves the scroll threshold.
587    *
588    * @retunr The scroll threshold in pixels.
589    */
590   float GetScrollThreshold() const;
591
592   /**
593    * @brief Sets the scroll speed.
594    *
595    * Is the distance the text is going to be scrolled during a scroll interval.
596    *
597    * @param[in] speed The scroll speed in pixels/second.
598    */
599   void SetScrollSpeed(float speed);
600
601   /**
602    * @brief Sets Editable mode decoration.
603    *
604    * If this set to false, Primary cursor and grab will always be hidden.
605    *
606    * @param[in] isEditable enable or disable Editing.
607    */
608   void SetEditable(bool isEditable);
609
610   /**
611    * @brief Retrieves the scroll speed.
612    *
613    * @return The scroll speed in pixels/second.
614    */
615   float GetScrollSpeed() const;
616
617   /**
618    * @brief Notifies the decorator the whole text has been scrolled.
619    */
620   void NotifyEndOfScroll();
621
622   /**
623    * @copydoc Text::Controller::SetHorizontalScrollEnabled()
624    */
625   void SetHorizontalScrollEnabled(bool enable);
626
627   /**
628    * @copydoc Text::Controller::IsHorizontalScrollEnabled()
629    */
630   bool IsHorizontalScrollEnabled() const;
631
632   /**
633    * @copydoc Text::Controller::SetVerticalScrollEnabled()
634    */
635   void SetVerticalScrollEnabled(bool enable);
636
637   /**
638    * @copydoc Text::Controller::IsVerticalScrollEnabled()
639    */
640   bool IsVerticalScrollEnabled() const;
641
642   /**
643    * @copydoc Text::Controller::SetSmoothHandlePanEnabled()
644    */
645   void SetSmoothHandlePanEnabled(bool enable);
646
647   /**
648    * @copydoc Text::Controller::IsSmoothHandlePanEnabled()
649    */
650   bool IsSmoothHandlePanEnabled() const;
651
652 protected:
653   /**
654    * @brief A reference counted object may only be deleted by calling Unreference().
655    */
656   virtual ~Decorator();
657
658 private:
659   /**
660    * @brief Private constructor.
661    * @param[in] controller The controller which receives input events from Decorator components.
662    * @param[in] callbackInterface The text popup callback interface which receives the button click callbacks.
663    */
664   Decorator(ControllerInterface&                 controller,
665             TextSelectionPopupCallbackInterface& callbackInterface);
666
667   // Undefined
668   Decorator(const Decorator& handle);
669
670   // Undefined
671   Decorator& operator=(const Decorator& handle);
672
673 private:
674   struct Impl;
675   Impl* mImpl;
676 };
677 } // namespace Text
678
679 } // namespace Toolkit
680
681 } // namespace Dali
682
683 #endif // DALI_TOOLKIT_TEXT_DECORATOR_H