29d1631d331ded95eacf8bcec68a83ff6456740f
[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) 2015 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 #include <dali/public-api/math/vector2.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
29
30 namespace Dali
31 {
32
33 class Actor;
34 class Image;
35 class Vector2;
36 class Vector4;
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43 class Control;
44 }
45
46 namespace Text
47 {
48
49 class Decorator;
50 typedef IntrusivePtr<Decorator> DecoratorPtr;
51
52 // Used to set the cursor positions etc.
53 enum Cursor
54 {
55   PRIMARY_CURSOR,   ///< The primary cursor for bidirectional text (or the regular cursor for single-direction text)
56   SECONDARY_CURSOR, ///< The secondary cursor for bidirectional text
57   CURSOR_COUNT
58 };
59
60 // Determines which of the cursors are active (if any).
61 enum ActiveCursor
62 {
63   ACTIVE_CURSOR_NONE,    ///< Neither primary nor secondary cursor are active
64   ACTIVE_CURSOR_PRIMARY, ///< Primary cursor is active (only)
65   ACTIVE_CURSOR_BOTH     ///< Both primary and secondary cursor are active
66 };
67
68 // The state information for handle events.
69 enum HandleState
70 {
71   HANDLE_TAPPED,
72   HANDLE_PRESSED,
73   HANDLE_RELEASED,
74   HANDLE_SCROLLING,
75   HANDLE_STOP_SCROLLING
76 };
77
78 // Used to set different handle images
79 enum HandleImageType
80 {
81   HANDLE_IMAGE_PRESSED,
82   HANDLE_IMAGE_RELEASED,
83   HANDLE_IMAGE_TYPE_COUNT
84 };
85
86 // Types of handles.
87 enum HandleType
88 {
89   GRAB_HANDLE,
90   LEFT_SELECTION_HANDLE,
91   RIGHT_SELECTION_HANDLE,
92   HANDLE_TYPE_COUNT
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
112   class ControllerInterface
113   {
114   public:
115
116     /**
117      * @brief Constructor.
118      */
119     ControllerInterface() {};
120
121     /**
122      * @brief Virtual destructor.
123      */
124     virtual ~ControllerInterface() {};
125
126     /**
127      * @brief An input event from one of the handles.
128      *
129      * @param[out] targetSize The Size of the UI control the decorator is adding it's decorations to.
130      */
131     virtual void GetTargetSize( Vector2& targetSize ) = 0;
132
133     /**
134      * @brief Add a decoration to the parent UI control.
135      *
136      * @param[in] decoration The actor displaying a decoration.
137      */
138     virtual void AddDecoration( Actor& actor, 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    * @return A pointer to a new Decorator.
156    */
157   static DecoratorPtr New( ControllerInterface& controller );
158
159   /**
160    * @brief Set the bounding box which handles, popup and similar decorations will not exceed.
161    *
162    * The default value is the width and height of the stage from the top left origin.
163    * If a title bar for example is on the top of the screen then the y should be the title's height and
164    * the boundary height the stage height minus the title's height.
165    * Restrictions - The boundary box should be set up with a fixed z position for the text-input and the default camera.
166    *
167    * ------------------------------------------
168    * |(x,y)                                   |
169    * |o---------------------------------------|
170    * ||                                      ||
171    * ||            Bounding Box              || boundary height
172    * ||                                      ||
173    * |----------------------------------------|
174    * ------------------------------------------
175    *               boundary width
176    *
177    * @param[in] boundingBox Vector( x coordinate, y coordinate, width, height )
178    */
179   void SetBoundingBox( const Rect<int>& boundingBox );
180
181   /**
182    * @brief Retrieve the bounding box origin and dimensions.
183    *
184    * default is set once control is added to stage, before this the return vector will be Vector4:ZERO
185    * @return Rect<int> the bounding box origin, width and height
186    */
187   const Rect<int>& GetBoundingBox() const;
188
189   /**
190    * @brief The decorator waits until a relayout before creating actors etc.
191    *
192    * @param[in] size The size of the parent control after size-negotiation.
193    */
194   void Relayout( const Dali::Vector2& size );
195
196   /**
197    * @brief Updates the decorator's actor positions after scrolling.
198    *
199    * @param[in] scrollOffset The scroll offset.
200    */
201   void UpdatePositions( const Vector2& scrollOffset );
202
203   /**
204    * @brief Sets which of the cursors are active.
205    *
206    * @note Cursor will only be visible if within the parent area.
207    * @param[in] activeCursor Which of the cursors should be active (if any).
208    */
209   void SetActiveCursor( ActiveCursor activeCursor );
210
211   /**
212    * @brief Query which of the cursors are active.
213    *
214    * @return  Which of the cursors are active (if any).
215    */
216   unsigned int GetActiveCursor() const;
217
218   /**
219    * @brief Sets the position of a cursor.
220    *
221    * @param[in] cursor The cursor to set.
222    * @param[in] x The x position relative to the top-left of the parent control.
223    * @param[in] y The y position relative to the top-left of the parent control.
224    * @param[in] cursorHeight The logical height of the cursor.
225    * @param[in] lineHeight The logical height of the line.
226    */
227   void SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight );
228
229   /**
230    * @brief Retrieves the position, height and lineHeight of a cursor.
231    *
232    * @param[in] cursor The cursor to get.
233    * @param[out] x The x position relative to the top-left of the parent control.
234    * @param[out] y The y position relative to the top-left of the parent control.
235    * @param[out] cursorHeight The logical height of the cursor.
236    * @param[out] lineHeight The logical height of the line.
237    */
238   void GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const;
239
240   /**
241    * @brief Retrieves the position of a cursor.
242    *
243    * @param[in] cursor The cursor to get.
244    *
245    * @return The position.
246    */
247   const Vector2& GetPosition( Cursor cursor ) const;
248
249   /**
250    * @brief Sets the color for a cursor.
251    *
252    * @param[in] cursor Whether this color is for the primary or secondary cursor.
253    * @param[in] color The color to use.
254    */
255   void SetColor( Cursor cursor, const Dali::Vector4& color );
256
257   /**
258    * @brief Retrieves the color for a cursor.
259    *
260    * @param[in] cursor Whether this color is for the primary or secondary cursor.
261    * @return The cursor color.
262    */
263   const Dali::Vector4& GetColor( Cursor cursor ) const;
264
265   /**
266    * @brief Start blinking the cursor; see also SetCursorBlinkDuration().
267    */
268   void StartCursorBlink();
269
270   /**
271    * @brief Stop blinking the cursor.
272    */
273   void StopCursorBlink();
274
275   /**
276    * @brief Set the interval between cursor blinks.
277    *
278    * @param[in] seconds The interval in seconds.
279    */
280   void SetCursorBlinkInterval( float seconds );
281
282   /**
283    * @brief Retrieves the blink-interval for a cursor.
284    *
285    * @return The cursor blink-interval.
286    */
287   float GetCursorBlinkInterval() const;
288
289   /**
290    * @brief The cursor will stop blinking after this duration.
291    *
292    * @param[in] seconds The duration in seconds.
293    */
294   void SetCursorBlinkDuration( float seconds );
295
296   /**
297    * @brief Retrieves the blink-duration for a cursor.
298    *
299    * @return The cursor blink-duration.
300    */
301   float GetCursorBlinkDuration() const;
302
303   /**
304    * @brief Sets whether a handle is active.
305    *
306    * @param[in] handleType One of the handles.
307    * @param[in] active True if the handle should be active.
308    */
309   void SetHandleActive( HandleType handleType,
310                         bool active );
311
312   /**
313    * @brief Query whether a handle is active.
314    *
315    * @param[in] handleType One of the handles.
316    *
317    * @return True if the handle is active.
318    */
319   bool IsHandleActive( HandleType handleType ) const;
320
321   /**
322    * @brief Sets the image for one of the handles.
323    *
324    * @param[in] handleType One of the handles.
325    * @param[in] handleImageType A different image can be set for the pressed/released states.
326    * @param[in] image The image to use.
327    */
328   void SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image );
329
330   /**
331    * @brief Retrieves the image for one of the handles.
332    *
333    * @param[in] handleType One of the handles.
334    * @param[in] handleImageType A different image can be set for the pressed/released states.
335    *
336    * @return The grab handle image.
337    */
338   Dali::Image GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const;
339
340   /**
341    * @brief Sets the position of a selection handle.
342    *
343    * @param[in] handleType The handle to set.
344    * @param[in] x The x position relative to the top-left of the parent control.
345    * @param[in] y The y position relative to the top-left of the parent control.
346    * @param[in] lineHeight The logical line height at this position.
347    */
348   void SetPosition( HandleType handleType, float x, float y, float lineHeight );
349
350   /**
351    * @brief Retrieves the position of a selection handle.
352    *
353    * @param[in] handleType The handle to get.
354    * @param[out] x The x position relative to the top-left of the parent control.
355    * @param[out] y The y position relative to the top-left of the parent control.
356    * @param[out] lineHeight The logical line height at this position.
357    */
358   void GetPosition( HandleType handleType, float& x, float& y, float& lineHeight ) const;
359
360   /**
361    * @brief Retrieves the position of a selection handle.
362    *
363    * @param[in] handleType The handle to get.
364    *
365    * @return The position of the selection handle relative to the top-left of the parent control.
366    */
367   const Vector2& GetPosition( HandleType handleType ) const;
368
369   /**
370    * @brief Swaps the selection handle's images.
371    *
372    * This method is called by the text controller to swap the handles
373    * when the start index is bigger than the end one.
374    */
375   void SwapSelectionHandlesEnabled( bool enable );
376
377   /**
378    * @brief Adds a quad to the existing selection highlights.
379    *
380    * @param[in] x1 The top-left x position.
381    * @param[in] y1 The top-left y position.
382    * @param[in] x2 The bottom-right x position.
383    * @param[in] y3 The bottom-right y position.
384    */
385   void AddHighlight( float x1, float y1, float x2, float y2 );
386
387   /**
388    * @brief Removes all of the previously added highlights.
389    */
390   void ClearHighlights();
391
392   /**
393    * @brief Sets the selection highlight color.
394    *
395    * @param[in] image The image to use.
396    */
397   void SetHighlightColor( const Vector4& color );
398
399   /**
400    * @brief Retrieves the selection highlight color.
401    *
402    * @return The image.
403    */
404   const Vector4& GetHighlightColor() const;
405
406   /**
407    * @brief Set the Selection Popup to show or hide via the active flaf
408    * @param[in] active true to show, false to hide
409    */
410   void SetPopupActive( bool active );
411
412   /**
413    * @brief Query whether the Selection Popup is active.
414    *
415    * @return True if the Selection Popup should be active.
416    */
417   bool IsPopupActive() const;
418
419   /**
420    * @brief Set a bit mask of the buttons to be shown by Popup
421    * @param[in] enabledButtonsBitMask from TextSelectionPopup::Buttons enum
422    */
423   void SetEnabledPopupButtons( TextSelectionPopup::Buttons& enabledButtonsBitMask );
424
425   /**
426    * @brief Get the current bit mask of buttons to be shown by Popup
427    * @return bitmask of TextSelectionPopup::Buttons
428    */
429   TextSelectionPopup::Buttons& GetEnabledPopupButtons();
430
431   /**
432    * @brief Sets the scroll threshold.
433    *
434    * It defines a square area inside the control, close to the edge.
435    * When the cursor enters this area, the decorator starts to send scroll events.
436    *
437    * @param[in] threshold The scroll threshold.
438    */
439   void SetScrollThreshold( float threshold );
440
441   /**
442    * @brief Retrieves the scroll threshold.
443    *
444    * @retunr The scroll threshold.
445    */
446   float GetScrollThreshold() const;
447
448   /**
449    * @brief Sets the scroll speed.
450    *
451    * Is the distance the text is going to be scrolled during a scroll interval.
452    *
453    * @param[in] speed The scroll speed.
454    */
455   void SetScrollSpeed( float speed );
456
457   /**
458    * @brief Retrieves the scroll speed.
459    *
460    * @return The scroll speed.
461    */
462   float GetScrollSpeed() const;
463
464   /**
465    * @brief Sets the scroll interval.
466    *
467    * @param[in] seconds The scroll interval in seconds.
468    */
469   void SetScrollTickInterval( float seconds );
470
471   /**
472    * @brief Retrieves the scroll interval.
473    *
474    * @return The scroll interval.
475    */
476   float GetScrollTickInterval() const;
477
478 protected:
479
480   /**
481    * @brief A reference counted object may only be deleted by calling Unreference().
482    */
483   virtual ~Decorator();
484
485 private:
486
487   /**
488    * @brief Private constructor.
489    * @param[in] controller The controller which receives input events from Decorator components.
490    */
491   Decorator( ControllerInterface& controller );
492
493   // Undefined
494   Decorator( const Decorator& handle );
495
496   // Undefined
497   Decorator& operator=( const Decorator& handle );
498
499 private:
500
501   struct Impl;
502   Impl* mImpl;
503 };
504 } // namespace Text
505
506 } // namespace Toolkit
507
508 } // namespace Dali
509
510 #endif // __DALI_TOOLKIT_TEXT_DECORATOR_H__