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