Merge "DALi Version 1.0.45" 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) 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 namespace Dali
28 {
29
30 class Actor;
31 class Image;
32 class Vector2;
33 class Vector4;
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40 class Control;
41 }
42
43 namespace Text
44 {
45
46 class Decorator;
47 typedef IntrusivePtr<Decorator> DecoratorPtr;
48
49 // Used to set the cursor positions etc.
50 enum Cursor
51 {
52   PRIMARY_CURSOR,   ///< The primary cursor for bidirectional text (or the regular cursor for single-direction text)
53   SECONDARY_CURSOR, ///< The secondary cursor for bidirectional text
54   CURSOR_COUNT
55 };
56
57 // Determines which of the cursors are active (if any).
58 enum ActiveCursor
59 {
60   ACTIVE_CURSOR_NONE,    ///< Neither primary nor secondary cursor are active
61   ACTIVE_CURSOR_PRIMARY, ///< Primary cursor is active (only)
62   ACTIVE_CURSOR_BOTH     ///< Both primary and secondary cursor are active
63 };
64
65 // The state information for handle events.
66 enum HandleState
67 {
68   HANDLE_TAPPED,
69   HANDLE_PRESSED,
70   HANDLE_RELEASED,
71   HANDLE_SCROLLING,
72   HANDLE_STOP_SCROLLING
73 };
74
75 // Used to set different handle images
76 enum HandleImageType
77 {
78   HANDLE_IMAGE_PRESSED,
79   HANDLE_IMAGE_RELEASED,
80   HANDLE_IMAGE_TYPE_COUNT
81 };
82
83 // Types of handles.
84 enum HandleType
85 {
86   GRAB_HANDLE,
87   LEFT_SELECTION_HANDLE,
88   RIGHT_SELECTION_HANDLE,
89   HANDLE_TYPE_COUNT
90 };
91
92 /**
93  * @brief A Text Decorator is used to display cursors, handles, selection highlights and pop-ups.
94  *
95  * The decorator is responsible for clipping decorations which are positioned outside of the parent area.
96  *
97  * The Popup decoration will be positioned either above the Grab handle or above the selection handles but if doing so
98  * would cause the Popup to exceed the Decoration Bounding Box ( see SetBoundingBox API ) the the Popup will be repositioned below the handle(s).
99  *
100  * Selection handles will be flipped around to ensure they do not exceed the Decoration Bounding Box. ( Stay visible ).
101  *
102  * Decorator components forward input events to a controller class through an interface.
103  * The controller is responsible for selecting which components are active.
104  */
105 class Decorator : public RefObject
106 {
107 public:
108
109   class ControllerInterface
110   {
111   public:
112
113     /**
114      * @brief Constructor.
115      */
116     ControllerInterface() {};
117
118     /**
119      * @brief Virtual destructor.
120      */
121     virtual ~ControllerInterface() {};
122
123     /**
124      * @brief An input event from one of the handles.
125      *
126      * @param[out] targetSize The Size of the UI control the decorator is adding it's decorations to.
127      */
128     virtual void GetTargetSize( Vector2& targetSize ) = 0;
129
130     /**
131      * @brief Add a decoration to the parent UI control.
132      *
133      * @param[in] decoration The actor displaying a decoration.
134      */
135     virtual void AddDecoration( Actor& actor, bool needsClipping ) = 0;
136
137     /**
138      * @brief An input event from one of the handles.
139      *
140      * @param[in] handleType The handle's type.
141      * @param[in] state The handle's state.
142      * @param[in] x The x position relative to the top-left of the parent control.
143      * @param[in] y The y position relative to the top-left of the parent control.
144      */
145     virtual void DecorationEvent( HandleType handleType, HandleState state, float x, float y ) = 0;
146   };
147
148   /**
149    * @brief Create a new instance of a Decorator.
150    *
151    * @param[in] controller The controller which receives input events from Decorator components.
152    * @return A pointer to a new Decorator.
153    */
154   static DecoratorPtr New( ControllerInterface& controller );
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    * @return Rect<int> the bounding box origin, width and height
183    */
184   const Rect<int>& GetBoundingBox() 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    * @brief Sets the color for a cursor.
248    *
249    * @param[in] cursor Whether this color is for the primary or secondary cursor.
250    * @param[in] color The color to use.
251    */
252   void SetColor( Cursor cursor, const Dali::Vector4& color );
253
254   /**
255    * @brief Retrieves the color for a cursor.
256    *
257    * @param[in] cursor Whether this color is for the primary or secondary cursor.
258    * @return The cursor color.
259    */
260   const Dali::Vector4& GetColor( Cursor cursor ) const;
261
262   /**
263    * @brief Start blinking the cursor; see also SetCursorBlinkDuration().
264    */
265   void StartCursorBlink();
266
267   /**
268    * @brief Stop blinking the cursor.
269    */
270   void StopCursorBlink();
271
272   /**
273    * @brief Set the interval between cursor blinks.
274    *
275    * @param[in] seconds The interval in seconds.
276    */
277   void SetCursorBlinkInterval( float seconds );
278
279   /**
280    * @brief Retrieves the blink-interval for a cursor.
281    *
282    * @return The cursor blink-interval.
283    */
284   float GetCursorBlinkInterval() const;
285
286   /**
287    * @brief The cursor will stop blinking after this duration.
288    *
289    * @param[in] seconds The duration in seconds.
290    */
291   void SetCursorBlinkDuration( float seconds );
292
293   /**
294    * @brief Retrieves the blink-duration for a cursor.
295    *
296    * @return The cursor blink-duration.
297    */
298   float GetCursorBlinkDuration() const;
299
300   /**
301    * @brief Sets whether a handle is active.
302    *
303    * @param[in] handleType One of the handles.
304    * @param[in] active True if the handle should be active.
305    */
306   void SetHandleActive( HandleType handleType,
307                         bool active );
308
309   /**
310    * @brief Query whether a handle is active.
311    *
312    * @param[in] handleType One of the handles.
313    *
314    * @return True if the handle is active.
315    */
316   bool IsHandleActive( HandleType handleType ) const;
317
318   /**
319    * @brief Sets the image for one of the handles.
320    *
321    * @param[in] handleType One of the handles.
322    * @param[in] handleImageType A different image can be set for the pressed/released states.
323    * @param[in] image The image to use.
324    */
325   void SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image );
326
327   /**
328    * @brief Retrieves the image for one of the handles.
329    *
330    * @param[in] handleType One of the handles.
331    * @param[in] handleImageType A different image can be set for the pressed/released states.
332    *
333    * @return The grab handle image.
334    */
335   Dali::Image GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const;
336
337   /**
338    * @brief Sets the position of a selection handle.
339    *
340    * @param[in] handleType The handle to set.
341    * @param[in] x The x position relative to the top-left of the parent control.
342    * @param[in] y The y position relative to the top-left of the parent control.
343    * @param[in] lineHeight The logical line height at this position.
344    */
345   void SetPosition( HandleType handleType, float x, float y, float lineHeight );
346
347   /**
348    * @brief Retrieves the position of a selection handle.
349    *
350    * @param[in] handleType The handle to get.
351    * @param[out] x The x position relative to the top-left of the parent control.
352    * @param[out] y The y position relative to the top-left of the parent control.
353    * @param[out] lineHeight The logical line height at this position.
354    */
355   void GetPosition( HandleType handleType, float& x, float& y, float& lineHeight ) const;
356
357   /**
358    * @brief Retrieves the position of a selection handle.
359    *
360    * @param[in] handleType The handle to get.
361    *
362    * @return The position of the selection handle relative to the top-left of the parent control.
363    */
364   const Vector2& GetPosition( HandleType handleType ) const;
365
366   /**
367    * @brief Swaps the selection handle's images.
368    *
369    * This method is called by the text controller to swap the handles
370    * when the start index is bigger than the end one.
371    */
372   void SwapSelectionHandlesEnabled( bool enable );
373
374   /**
375    * @brief Adds a quad to the existing selection highlights.
376    *
377    * @param[in] x1 The top-left x position.
378    * @param[in] y1 The top-left y position.
379    * @param[in] x2 The bottom-right x position.
380    * @param[in] y3 The bottom-right y position.
381    */
382   void AddHighlight( float x1, float y1, float x2, float y2 );
383
384   /**
385    * @brief Removes all of the previously added highlights.
386    */
387   void ClearHighlights();
388
389   /**
390    * @brief Sets the selection highlight color.
391    *
392    * @param[in] image The image to use.
393    */
394   void SetHighlightColor( const Vector4& color );
395
396   /**
397    * @brief Retrieves the selection highlight color.
398    *
399    * @return The image.
400    */
401   const Vector4& GetHighlightColor() const;
402
403   /**
404    * @brief Set the Selection Popup to show or hide via the active flaf
405    * @param[in] active true to show, false to hide
406    */
407   void SetPopupActive( bool active );
408
409   /**
410    * @brief Query whether the Selection Popup is active.
411    *
412    * @return True if the Selection Popup should be active.
413    */
414   bool IsPopupActive() const;
415
416   /**
417    * @brief Sets the scroll threshold.
418    *
419    * It defines a square area inside the control, close to the edge.
420    * When the cursor enters this area, the decorator starts to send scroll events.
421    *
422    * @param[in] threshold The scroll threshold.
423    */
424   void SetScrollThreshold( float threshold );
425
426   /**
427    * @brief Retrieves the scroll threshold.
428    *
429    * @retunr The scroll threshold.
430    */
431   float GetScrollThreshold() const;
432
433   /**
434    * @brief Sets the scroll speed.
435    *
436    * Is the distance the text is going to be scrolled during a scroll interval.
437    *
438    * @param[in] speed The scroll speed.
439    */
440   void SetScrollSpeed( float speed );
441
442   /**
443    * @brief Retrieves the scroll speed.
444    *
445    * @return The scroll speed.
446    */
447   float GetScrollSpeed() const;
448
449   /**
450    * @brief Sets the scroll interval.
451    *
452    * @param[in] seconds The scroll interval in seconds.
453    */
454   void SetScrollTickInterval( float seconds );
455
456   /**
457    * @brief Retrieves the scroll interval.
458    *
459    * @return The scroll interval.
460    */
461   float GetScrollTickInterval() const;
462
463 protected:
464
465   /**
466    * @brief A reference counted object may only be deleted by calling Unreference().
467    */
468   virtual ~Decorator();
469
470 private:
471
472   /**
473    * @brief Private constructor.
474    * @param[in] controller The controller which receives input events from Decorator components.
475    */
476   Decorator( ControllerInterface& controller );
477
478   // Undefined
479   Decorator( const Decorator& handle );
480
481   // Undefined
482   Decorator& operator=( const Decorator& handle );
483
484 private:
485
486   struct Impl;
487   Impl* mImpl;
488 };
489 } // namespace Text
490
491 } // namespace Toolkit
492
493 } // namespace Dali
494
495 #endif // __DALI_TOOLKIT_TEXT_DECORATOR_H__