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