b4a0705737e2b4298ee94375f8529f7476c17910
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / decorator / text-decorator.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/actors/image-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/actors/mesh-actor.h>
27 #include <dali/public-api/common/constants.h>
28 #include <dali/public-api/events/tap-gesture.h>
29 #include <dali/public-api/events/tap-gesture-detector.h>
30 #include <dali/public-api/events/pan-gesture.h>
31 #include <dali/public-api/events/pan-gesture-detector.h>
32 #include <dali/public-api/geometry/mesh.h>
33 #include <dali/public-api/geometry/mesh-data.h>
34 #include <dali/public-api/images/resource-image.h>
35 #include <dali/public-api/math/vector2.h>
36 #include <dali/public-api/math/vector4.h>
37 //#include <dali/public-api/images/nine-patch-image.h>
38 #include <dali/public-api/signals/connection-tracker.h>
39
40 // INTERNAL INCLUDES
41 #include <dali-toolkit/public-api/controls/control.h>
42 #include <dali-toolkit/public-api/controls/control-impl.h>
43 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
44 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
45 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
46 #include <dali-toolkit/public-api/controls/text-controls/text-selection-popup.h>
47
48 #ifdef DEBUG_ENABLED
49 #define DECORATOR_DEBUG
50 #endif
51
52 // Local Data
53 namespace
54 {
55
56 const char* DEFAULT_GRAB_HANDLE_IMAGE( DALI_IMAGE_DIR "insertpoint-icon.png" );
57 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
58 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
59 //const char* DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
60 //const char* DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
61
62 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
63 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
64
65 const std::size_t CURSOR_BLINK_INTERVAL = 500; // Cursor blink interval
66 const std::size_t MILLISECONDS = 1000;
67
68 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
69
70 /**
71  * structure to hold coordinates of each quad, which will make up the mesh.
72  */
73 struct QuadCoordinates
74 {
75   /**
76    * Default constructor
77    */
78   QuadCoordinates()
79   {
80   }
81
82   /**
83    * Constructor
84    * @param[in] x1 left co-ordinate
85    * @param[in] y1 top co-ordinate
86    * @param[in] x2 right co-ordinate
87    * @param[in] y2 bottom co-ordinate
88    */
89   QuadCoordinates(float x1, float y1, float x2, float y2)
90   : min(x1, y1),
91     max(x2, y2)
92   {
93   }
94
95   Dali::Vector2 min;                          ///< top-left (minimum) position of quad
96   Dali::Vector2 max;                          ///< bottom-right (maximum) position of quad
97 };
98
99 typedef std::vector<QuadCoordinates> QuadContainer;
100
101 } // end of namespace
102
103 namespace Dali
104 {
105
106 namespace Toolkit
107 {
108
109 namespace Text
110 {
111
112 struct Decorator::Impl : public ConnectionTracker
113 {
114   struct CursorImpl
115   {
116     CursorImpl()
117     : x(0.0f),
118       y(0.0f),
119       height(0.0f),
120       color(Dali::Color::WHITE)
121     {
122     }
123
124     float x;
125     float y;
126     float height;
127
128     Vector4 color;
129   };
130
131   struct SelectionHandleImpl
132   {
133     SelectionHandleImpl()
134     : x(0.0f),
135       y(0.0f),
136       cursorHeight(0.0f),
137       flipped(false)
138     {
139     }
140
141     float x;
142     float y;
143     float cursorHeight; ///< Not the handle height
144     bool flipped;
145
146     ImageActor actor;
147     Actor grabArea;
148
149     Image pressedImage;
150     Image releasedImage;
151   };
152
153   Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
154   : mTextControlParent(parent),
155     mObserver(observer),
156     mActiveCursor(ACTIVE_CURSOR_NONE),
157     mActiveGrabHandle(false),
158     mActiveSelection( false ),
159     mActiveCopyPastePopup( false ),
160     mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
161     mCursorBlinkDuration( 0.0f ),
162     mCursorBlinkStatus( true ),
163     mGrabDisplacementX( 0.0f ),
164     mGrabDisplacementY( 0.0f ),
165     mHighlightColor( 0.07f, 0.41f, 0.59f, 1.0f ), // light blue
166     mBoundingBox( Rect<int>() )
167   {
168   }
169
170   /**
171    * Relayout of the decorations owned by the decorator.
172    * @param[in] size The Size of the UI control the decorater is adding it's decorations to.
173    */
174   void Relayout( const Vector2& size, const Vector2& scrollPosition )
175   {
176     // TODO - Remove this if nothing is active
177     CreateActiveLayer();
178
179     // Show or hide the cursors
180     CreateCursors();
181     if( mPrimaryCursor )
182     {
183       mPrimaryCursor.SetPosition( mCursor[PRIMARY_CURSOR].x + scrollPosition.x,
184                                   mCursor[PRIMARY_CURSOR].y + scrollPosition.y );
185       mPrimaryCursor.SetSize( Vector2( 1.0f, mCursor[PRIMARY_CURSOR].height ) );
186     }
187     if( mSecondaryCursor )
188     {
189       mSecondaryCursor.SetPosition( mCursor[SECONDARY_CURSOR].x + scrollPosition.x,
190                                     mCursor[SECONDARY_CURSOR].y + scrollPosition.y );
191       mSecondaryCursor.SetSize( Vector2( 1.0f, mCursor[SECONDARY_CURSOR].height ) );
192     }
193
194     // Show or hide the grab handle
195     if( mActiveGrabHandle )
196     {
197       SetupTouchEvents();
198
199       CreateGrabHandle();
200
201       mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x + scrollPosition.x,
202                                mCursor[PRIMARY_CURSOR].y + scrollPosition.y + mCursor[PRIMARY_CURSOR].height );
203     }
204     else if( mGrabHandle )
205     {
206       UnparentAndReset( mGrabHandle );
207     }
208
209     // Show or hide the selection handles/highlight
210     if( mActiveSelection )
211     {
212       SetupTouchEvents();
213
214       CreateSelectionHandles();
215
216       SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
217       primary.actor.SetPosition( primary.x + scrollPosition.x,
218                                  primary.y + scrollPosition.y + primary.cursorHeight );
219
220       SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
221       secondary.actor.SetPosition( secondary.x + scrollPosition.x,
222                                    secondary.y + scrollPosition.y + secondary.cursorHeight );
223
224       CreateHighlight();
225       UpdateHighlight();
226     }
227     else
228     {
229       UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
230       UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
231       UnparentAndReset( mHighlightMeshActor );
232     }
233
234     if ( mActiveCopyPastePopup )
235     {
236       if ( !mCopyPastePopup )
237       {
238         mCopyPastePopup = TextSelectionPopup::New();
239         mActiveLayer.Add ( mCopyPastePopup );
240       }
241       mCopyPastePopup.SetPosition( Vector3( 200.0f, -100.0f, 0.0f ) ); //todo grabhandle or selection handle positions to be used
242     }
243     else
244     {
245      if ( mCopyPastePopup )
246      {
247        UnparentAndReset( mCopyPastePopup );
248      }
249     }
250   }
251
252   void CreateCursor( ImageActor& cursor )
253   {
254     cursor = CreateSolidColorActor( Color::WHITE );
255     cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
256     cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
257     cursor.SetRelayoutEnabled( false );
258   }
259
260   // Add or Remove cursor(s) from parent
261   void CreateCursors()
262   {
263     if( mActiveCursor == ACTIVE_CURSOR_NONE )
264     {
265       UnparentAndReset( mPrimaryCursor );
266       UnparentAndReset( mSecondaryCursor );
267     }
268     else
269     {
270       /* Create Primary and or Secondary Cursor(s) if active and add to parent */
271       if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
272            mActiveCursor == ACTIVE_CURSOR_BOTH )
273       {
274         if ( !mPrimaryCursor )
275         {
276           CreateCursor( mPrimaryCursor );
277 #ifdef DECORATOR_DEBUG
278           mPrimaryCursor.SetName( "PrimaryCursorActor" );
279 #endif
280           mActiveLayer.Add( mPrimaryCursor);
281         }
282       }
283
284       if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
285       {
286         if ( !mSecondaryCursor )
287         {
288           CreateCursor( mSecondaryCursor );
289 #ifdef DECORATOR_DEBUG
290           mSecondaryCursor.SetName( "SecondaryCursorActor" );
291 #endif
292           mActiveLayer.Add( mSecondaryCursor);
293         }
294       }
295     }
296   }
297
298   bool OnCursorBlinkTimerTick()
299   {
300     // Cursor blinking
301     if ( mPrimaryCursor )
302     {
303       mPrimaryCursor.SetVisible( mCursorBlinkStatus );
304     }
305     if ( mSecondaryCursor )
306     {
307       mSecondaryCursor.SetVisible( mCursorBlinkStatus );
308     }
309
310     mCursorBlinkStatus = !mCursorBlinkStatus;
311
312     return true;
313   }
314
315   void SetupTouchEvents()
316   {
317     if ( !mTapDetector )
318     {
319       mTapDetector = TapGestureDetector::New();
320       mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
321     }
322
323     if ( !mPanGestureDetector )
324     {
325       mPanGestureDetector = PanGestureDetector::New();
326       mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
327     }
328   }
329
330   void CreateActiveLayer()
331   {
332     if( !mActiveLayer )
333     {
334       Actor parent = mTextControlParent.Self();
335
336       mActiveLayer = Layer::New();
337 #ifdef DECORATOR_DEBUG
338       mActiveLayer.SetName ( "ActiveLayerActor" );
339 #endif
340
341       mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
342       mActiveLayer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
343       mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
344
345       parent.Add( mActiveLayer );
346     }
347
348     mActiveLayer.RaiseToTop();
349   }
350
351   void CreateGrabHandle()
352   {
353     if( !mGrabHandle )
354     {
355       if ( !mGrabHandleImage )
356       {
357         mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
358       }
359
360       mGrabHandle = ImageActor::New( mGrabHandleImage );
361 #ifdef DECORATOR_DEBUG
362       mGrabHandle.SetName( "GrabHandleActor" );
363 #endif
364       mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
365       mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
366       // Area that Grab handle responds to, larger than actual handle so easier to move
367 #ifdef DECORATOR_DEBUG
368      mGrabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
369      mGrabArea.SetName( "GrabArea" );
370 #else
371       mGrabArea = Actor::New();
372       mGrabArea.SetRelayoutEnabled( true );
373 #endif
374       mGrabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
375       mGrabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
376       mGrabArea.SetResizePolicy( SIZE_RELATIVE_TO_PARENT, ALL_DIMENSIONS );
377       mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
378       mGrabHandle.Add( mGrabArea );
379
380       mTapDetector.Attach( mGrabArea );
381       mPanGestureDetector.Attach( mGrabArea );
382
383       mActiveLayer.Add(mGrabHandle);
384     }
385   }
386
387   void CreateSelectionHandles()
388   {
389     SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
390     if ( !primary.actor )
391     {
392       if ( !primary.releasedImage )
393       {
394         primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
395       }
396
397       primary.actor = ImageActor::New( primary.releasedImage );
398 #ifdef DECORATOR_DEBUG
399       primary.actor.SetName("SelectionHandleOne");
400 #endif
401       primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
402       primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
403       primary.flipped = false;
404
405       primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
406       primary.grabArea.SetRelayoutEnabled( true );
407 #ifdef DECORATOR_DEBUG
408       primary.grabArea.SetName("SelectionHandleOneGrabArea");
409 #endif
410       primary.grabArea.SetResizePolicy( SIZE_RELATIVE_TO_PARENT, ALL_DIMENSIONS );
411       primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
412       primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
413
414       mTapDetector.Attach( primary.grabArea );
415       mPanGestureDetector.Attach( primary.grabArea );
416       primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
417
418       primary.actor.Add( primary.grabArea );
419       mActiveLayer.Add( primary.actor );
420     }
421
422     SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
423     if ( !secondary.actor )
424     {
425       if ( !secondary.releasedImage )
426       {
427         secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
428       }
429
430       secondary.actor = ImageActor::New( secondary.releasedImage );
431 #ifdef DECORATOR_DEBUG
432       secondary.actor.SetName("SelectionHandleTwo");
433 #endif
434       secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
435       secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
436       secondary.flipped = false;
437
438       secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
439       secondary.grabArea.SetRelayoutEnabled( true );
440 #ifdef DECORATOR_DEBUG
441       secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
442 #endif
443       secondary.grabArea.SetResizePolicy( SIZE_RELATIVE_TO_PARENT, ALL_DIMENSIONS );
444       secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
445       secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
446
447       mTapDetector.Attach( secondary.grabArea );
448       mPanGestureDetector.Attach( secondary.grabArea );
449       secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
450
451       secondary.actor.Add( secondary.grabArea );
452       mActiveLayer.Add( secondary.actor );
453     }
454
455     //SetUpHandlePropertyNotifications(); TODO
456   }
457
458   void CreateHighlight()
459   {
460     if ( !mHighlightMeshActor )
461     {
462       mHighlightMaterial = Material::New( "HighlightMaterial" );
463       mHighlightMaterial.SetDiffuseColor( mHighlightColor );
464
465       mHighlightMeshData.SetMaterial( mHighlightMaterial );
466       mHighlightMeshData.SetHasNormals( true );
467
468       mHighlightMesh = Mesh::New( mHighlightMeshData );
469
470       mHighlightMeshActor = MeshActor::New( mHighlightMesh );
471 #ifdef DECORATOR_DEBUG
472       mHighlightMeshActor.SetName( "HighlightMeshActor" );
473 #endif
474       mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
475       mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
476
477       Actor parent = mTextControlParent.Self();
478       parent.Add( mHighlightMeshActor );
479     }
480   }
481
482   void UpdateHighlight()
483   {
484     //  Construct a Mesh with a texture to be used as the highlight 'box' for selected text
485     //
486     //  Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
487     //
488     //   [ TOP   ]  [ TOP ]      [TOP ]  [ TOP    ]      [ TOP  ]      [ TOP  ]
489     //  [ MIDDLE ]             [BOTTOM]  [BOTTOM]      [ MIDDLE ]   [ MIDDLE  ]
490     //  [ BOTTOM]                                      [ MIDDLE ]   [ MIDDLE  ]
491     //                                                 [BOTTOM]     [ MIDDLE  ]
492     //                                                              [BOTTOM]
493     //
494     //  Each quad is created as 2 triangles.
495     //  Middle is just 1 quad regardless of its size.
496     //
497     //  (0,0)         (0,0)
498     //     0*    *2     0*       *2
499     //     TOP          TOP
500     //     3*    *1     3*       *1
501     //  4*       *1     4*     *6
502     //     MIDDLE         BOTTOM
503     //  6*       *5     7*     *5
504     //  6*    *8
505     //   BOTTOM
506     //  9*    *7
507     //
508
509     if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
510     {
511       MeshData::VertexContainer vertices;
512       Dali::MeshData::FaceIndices faceIndices;
513
514       std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
515       std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
516
517       // vertex position defaults to (0 0 0)
518       MeshData::Vertex vertex;
519       // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
520       vertex.nZ = 1.0f;
521
522       for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
523       {
524         // Add each quad geometry (a sub-selection) to the mesh data.
525
526         // 0-----1
527         // |\    |
528         // | \ A |
529         // |  \  |
530         // | B \ |
531         // |    \|
532         // 2-----3
533
534         QuadCoordinates& quad = *iter;
535         // top-left (v+0)
536         vertex.x = quad.min.x;
537         vertex.y = quad.min.y;
538         vertices.push_back( vertex );
539
540         // top-right (v+1)
541         vertex.x = quad.max.x;
542         vertex.y = quad.min.y;
543         vertices.push_back( vertex );
544
545         // bottom-left (v+2)
546         vertex.x = quad.min.x;
547         vertex.y = quad.max.y;
548         vertices.push_back( vertex );
549
550         // bottom-right (v+3)
551         vertex.x = quad.max.x;
552         vertex.y = quad.max.y;
553         vertices.push_back( vertex );
554
555         // triangle A (3, 1, 0)
556         faceIndices.push_back( v + 3 );
557         faceIndices.push_back( v + 1 );
558         faceIndices.push_back( v );
559
560         // triangle B (0, 2, 3)
561         faceIndices.push_back( v );
562         faceIndices.push_back( v + 2 );
563         faceIndices.push_back( v + 3 );
564
565         mHighlightMeshData.SetFaceIndices( faceIndices );
566       }
567
568       BoneContainer bones(0); // passed empty as bones not required
569       mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
570       mHighlightMesh.UpdateMeshData( mHighlightMeshData );
571     }
572   }
573
574   void OnTap( Actor actor, const TapGesture& tap )
575   {
576     if( actor == mGrabHandle )
577     {
578       // TODO
579     }
580   }
581
582   void OnPan( Actor actor, const PanGesture& gesture )
583   {
584     if( actor == mGrabArea )
585     {
586       if( Gesture::Started == gesture.state )
587       {
588         mGrabDisplacementX = mGrabDisplacementY = 0;
589       }
590
591       mGrabDisplacementX += gesture.displacement.x;
592       mGrabDisplacementY += gesture.displacement.y;
593
594       float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
595       float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
596
597       if( Gesture::Started    == gesture.state ||
598           Gesture::Continuing == gesture.state )
599       {
600         mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
601       }
602       else if( Gesture::Finished  == gesture.state ||
603                Gesture::Cancelled == gesture.state )
604       {
605         mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
606       }
607     }
608   }
609
610   bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
611   {
612     // TODO
613     return false;
614   }
615
616   bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
617   {
618     // TODO
619     return false;
620   }
621
622
623   Internal::Control& mTextControlParent;
624   Observer& mObserver;
625
626   Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
627
628   unsigned int mActiveCursor;
629   bool         mActiveGrabHandle;
630   bool         mActiveSelection;
631   bool         mActiveCopyPastePopup;
632
633   CursorImpl mCursor[CURSOR_COUNT];
634
635   Timer mCursorBlinkTimer; // Timer to signal cursor to blink
636   unsigned int mCursorBlinkInterval;
637   float mCursorBlinkDuration;
638   bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
639
640   ImageActor mPrimaryCursor;
641   ImageActor mSecondaryCursor;
642
643   ImageActor mGrabHandle;
644   Actor mGrabArea;
645   float mGrabDisplacementX;
646   float mGrabDisplacementY;
647
648   SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
649
650   MeshActor         mHighlightMeshActor;        ///< Mesh Actor to display highlight
651   Mesh              mHighlightMesh;             ///< Mesh for highlight
652   MeshData          mHighlightMeshData;         ///< Mesh Data for highlight
653   Material          mHighlightMaterial;         ///< Material used for highlight
654   Vector4           mHighlightColor;            ///< Color of the highlight
655   QuadContainer     mHighlightQuadList;         ///< Sub-selections that combine to create the complete selection highlight
656
657   TextSelectionPopup mCopyPastePopup;
658
659   Image mCursorImage;
660   Image mGrabHandleImage;
661
662   TapGestureDetector mTapDetector;
663   PanGestureDetector mPanGestureDetector;
664
665   Rect<int> mBoundingBox;
666 };
667
668 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
669 {
670   return DecoratorPtr( new Decorator(parent, observer) );
671 }
672
673 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
674 {
675   mImpl->mBoundingBox = boundingBox;
676 }
677
678 const Rect<int>& Decorator::GetBoundingBox() const
679 {
680   return mImpl->mBoundingBox;
681 }
682
683 void Decorator::Relayout( const Vector2& size, const Vector2& scrollPosition )
684 {
685   mImpl->Relayout( size, scrollPosition );
686 }
687
688 /** Cursor **/
689
690 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
691 {
692   mImpl->mActiveCursor = activeCursor;
693 }
694
695 unsigned int Decorator::GetActiveCursor() const
696 {
697   return mImpl->mActiveCursor;
698 }
699
700 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
701 {
702   // Adjust grab handle displacement
703   mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
704   mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
705
706   mImpl->mCursor[cursor].x = x;
707   mImpl->mCursor[cursor].y = y;
708   mImpl->mCursor[cursor].height = height;
709 }
710
711 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
712 {
713   x = mImpl->mCursor[cursor].x;
714   y = mImpl->mCursor[cursor].y;
715   height = mImpl->mCursor[cursor].height;
716 }
717
718 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
719 {
720   mImpl->mCursor[cursor].color = color;
721 }
722
723 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
724 {
725   return mImpl->mCursor[cursor].color;
726 }
727
728 void Decorator::StartCursorBlink()
729 {
730   if ( !mImpl->mCursorBlinkTimer )
731   {
732     mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
733     mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
734   }
735
736   if ( !mImpl->mCursorBlinkTimer.IsRunning() )
737   {
738     mImpl->mCursorBlinkTimer.Start();
739   }
740 }
741
742 void Decorator::StopCursorBlink()
743 {
744   if ( mImpl->mCursorBlinkTimer )
745   {
746     mImpl->mCursorBlinkTimer.Stop();
747   }
748 }
749
750 void Decorator::SetCursorBlinkInterval( float seconds )
751 {
752   mImpl->mCursorBlinkInterval = seconds*MILLISECONDS; // Convert to milliseconds
753 }
754
755 float Decorator::GetCursorBlinkInterval() const
756 {
757   return mImpl->mCursorBlinkInterval;
758 }
759
760 void Decorator::SetCursorBlinkDuration( float seconds )
761 {
762   mImpl->mCursorBlinkDuration = seconds;
763 }
764
765 float Decorator::GetCursorBlinkDuration() const
766 {
767   return mImpl->mCursorBlinkDuration;
768 }
769
770 /** GrabHandle **/
771
772 void Decorator::SetGrabHandleActive( bool active )
773 {
774   mImpl->mActiveGrabHandle = active;
775 }
776
777 bool Decorator::IsGrabHandleActive() const
778 {
779   return mImpl->mActiveGrabHandle;
780 }
781
782 void Decorator::SetGrabHandleImage( Dali::Image image )
783 {
784   mImpl->mGrabHandleImage = image;
785 }
786
787 Dali::Image Decorator::GetGrabHandleImage() const
788 {
789   return mImpl->mGrabHandleImage;
790 }
791
792 /** Selection **/
793
794 void Decorator::SetSelectionActive( bool active )
795 {
796   mImpl->mActiveSelection = active;
797 }
798
799 bool Decorator::IsSelectionActive() const
800 {
801   return mImpl->mActiveSelection;
802 }
803
804 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
805 {
806   mImpl->mSelectionHandle[handle].x = x;
807   mImpl->mSelectionHandle[handle].y = y;
808   mImpl->mSelectionHandle[handle].cursorHeight = height;
809 }
810
811 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
812 {
813   x = mImpl->mSelectionHandle[handle].x;
814   y = mImpl->mSelectionHandle[handle].y;
815   height = mImpl->mSelectionHandle[handle].cursorHeight;
816 }
817
818 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
819 {
820   if( SELECTION_HANDLE_PRESSED == state )
821   {
822     mImpl->mSelectionHandle[handle].pressedImage = image;
823   }
824   else
825   {
826     mImpl->mSelectionHandle[handle].releasedImage = image;
827   }
828 }
829
830 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
831 {
832   if( SELECTION_HANDLE_PRESSED == state )
833   {
834     return mImpl->mSelectionHandle[handle].pressedImage;
835   }
836
837   return mImpl->mSelectionHandle[handle].releasedImage;
838 }
839
840 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
841 {
842   mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
843 }
844
845 void Decorator::ClearHighlights()
846 {
847   mImpl->mHighlightQuadList.clear();
848 }
849
850 void Decorator::SetPopupActive( bool active )
851 {
852   mImpl->mActiveCopyPastePopup = active;
853 }
854
855 bool Decorator::IsPopupActive() const
856 {
857   return mImpl->mActiveCopyPastePopup ;
858 }
859
860 Decorator::~Decorator()
861 {
862   delete mImpl;
863 }
864
865 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
866 : mImpl( NULL )
867 {
868   mImpl = new Decorator::Impl( parent, observer );
869 }
870
871 } // namespace Text
872
873 } // namespace Toolkit
874
875 } // namespace Dali