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