551c961b47a687fa8fbdee79ccc7d8681fbceed1
[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.SetResizePolicy( FIXED, ALL_DIMENSIONS );
186       mPrimaryCursor.SetPreferredSize( Vector2( 1.0f, mCursor[PRIMARY_CURSOR].height ) );
187     }
188     if( mSecondaryCursor )
189     {
190       mSecondaryCursor.SetPosition( mCursor[SECONDARY_CURSOR].x + scrollPosition.x,
191                                     mCursor[SECONDARY_CURSOR].y + scrollPosition.y );
192       mSecondaryCursor.SetResizePolicy( FIXED, ALL_DIMENSIONS );
193       mSecondaryCursor.SetPreferredSize( Vector2( 1.0f, mCursor[SECONDARY_CURSOR].height ) );
194     }
195
196     // Show or hide the grab handle
197     if( mActiveGrabHandle )
198     {
199       SetupTouchEvents();
200
201       CreateGrabHandle();
202
203       mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x + scrollPosition.x,
204                                mCursor[PRIMARY_CURSOR].y + scrollPosition.y + mCursor[PRIMARY_CURSOR].height );
205     }
206     else if( mGrabHandle )
207     {
208       UnparentAndReset( mGrabHandle );
209     }
210
211     // Show or hide the selection handles/highlight
212     if( mActiveSelection )
213     {
214       SetupTouchEvents();
215
216       CreateSelectionHandles();
217
218       SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
219       primary.actor.SetPosition( primary.x + scrollPosition.x,
220                                  primary.y + scrollPosition.y + primary.cursorHeight );
221
222       SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
223       secondary.actor.SetPosition( secondary.x + scrollPosition.x,
224                                    secondary.y + scrollPosition.y + secondary.cursorHeight );
225
226       CreateHighlight();
227       UpdateHighlight();
228     }
229     else
230     {
231       UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
232       UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
233       UnparentAndReset( mHighlightMeshActor );
234     }
235
236     if ( mActiveCopyPastePopup )
237     {
238       if ( !mCopyPastePopup )
239       {
240         mCopyPastePopup = TextSelectionPopup::New();
241         mActiveLayer.Add ( mCopyPastePopup );
242       }
243       mCopyPastePopup.SetPosition( Vector3( 200.0f, -100.0f, 0.0f ) ); //todo grabhandle or selection handle positions to be used
244     }
245     else
246     {
247      if ( mCopyPastePopup )
248      {
249        UnparentAndReset( mCopyPastePopup );
250      }
251     }
252   }
253
254   void CreateCursor( ImageActor& cursor )
255   {
256     cursor = CreateSolidColorActor( Color::WHITE );
257     cursor.SetParentOrigin( ParentOrigin::TOP_LEFT );
258     cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
259   }
260
261   // Add or Remove cursor(s) from parent
262   void CreateCursors()
263   {
264     if( mActiveCursor == ACTIVE_CURSOR_NONE )
265     {
266       UnparentAndReset( mPrimaryCursor );
267       UnparentAndReset( mSecondaryCursor );
268     }
269     else
270     {
271       /* Create Primary and or Secondary Cursor(s) if active and add to parent */
272       if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
273            mActiveCursor == ACTIVE_CURSOR_BOTH )
274       {
275         if ( !mPrimaryCursor )
276         {
277           CreateCursor( mPrimaryCursor );
278 #ifdef DECORATOR_DEBUG
279           mPrimaryCursor.SetName( "PrimaryCursorActor" );
280 #endif
281           mActiveLayer.Add( mPrimaryCursor);
282         }
283       }
284
285       if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
286       {
287         if ( !mSecondaryCursor )
288         {
289           CreateCursor( mSecondaryCursor );
290 #ifdef DECORATOR_DEBUG
291           mSecondaryCursor.SetName( "SecondaryCursorActor" );
292 #endif
293           mActiveLayer.Add( mSecondaryCursor);
294         }
295       }
296     }
297   }
298
299   bool OnCursorBlinkTimerTick()
300   {
301     // Cursor blinking
302     if ( mPrimaryCursor )
303     {
304       mPrimaryCursor.SetVisible( mCursorBlinkStatus );
305     }
306     if ( mSecondaryCursor )
307     {
308       mSecondaryCursor.SetVisible( mCursorBlinkStatus );
309     }
310
311     mCursorBlinkStatus = !mCursorBlinkStatus;
312
313     return true;
314   }
315
316   void SetupTouchEvents()
317   {
318     if ( !mTapDetector )
319     {
320       mTapDetector = TapGestureDetector::New();
321       mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
322     }
323
324     if ( !mPanGestureDetector )
325     {
326       mPanGestureDetector = PanGestureDetector::New();
327       mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
328     }
329   }
330
331   void CreateActiveLayer()
332   {
333     if( !mActiveLayer )
334     {
335       Actor parent = mTextControlParent.Self();
336
337       mActiveLayer = Layer::New();
338 #ifdef DECORATOR_DEBUG
339       mActiveLayer.SetName ( "ActiveLayerActor" );
340 #endif
341
342       mActiveLayer.SetAnchorPoint( AnchorPoint::CENTER);
343       mActiveLayer.SetParentOrigin( ParentOrigin::CENTER);
344       mActiveLayer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
345       mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
346
347       parent.Add( mActiveLayer );
348     }
349
350     mActiveLayer.RaiseToTop();
351   }
352
353   void CreateGrabHandle()
354   {
355     if( !mGrabHandle )
356     {
357       if ( !mGrabHandleImage )
358       {
359         mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
360       }
361
362       mGrabHandle = ImageActor::New( mGrabHandleImage );
363 #ifdef DECORATOR_DEBUG
364       mGrabHandle.SetName( "GrabHandleActor" );
365 #endif
366       mGrabHandle.SetParentOrigin( ParentOrigin::TOP_LEFT );
367       mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
368       mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
369       // Area that Grab handle responds to, larger than actual handle so easier to move
370 //#ifdef DECORATOR_DEBUG
371 //      mGrabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
372 //      mGrabArea.SetName( "GrabArea" );
373 //#else
374       mGrabArea = Actor::New();  //todo Force use of Actor until SolidColorActor fixed in Size Negotiation
375       mGrabArea.SetRelayoutEnabled( true );
376 //#endif
377       mGrabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
378       mGrabArea.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
379       mGrabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
380       mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
381       mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
382       mGrabHandle.Add( mGrabArea );
383
384       mTapDetector.Attach( mGrabArea );
385       mPanGestureDetector.Attach( mGrabArea );
386
387       mActiveLayer.Add(mGrabHandle);
388     }
389   }
390
391   void CreateSelectionHandles()
392   {
393     SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
394     if ( !primary.actor )
395     {
396       if ( !primary.releasedImage )
397       {
398         primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
399       }
400
401       primary.actor = ImageActor::New( primary.releasedImage );
402 #ifdef DECORATOR_DEBUG
403       primary.actor.SetName("SelectionHandleOne");
404 #endif
405       primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
406       primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
407       primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
408       primary.flipped = false;
409
410       primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
411 #ifdef DECORATOR_DEBUG
412       primary.grabArea.SetName("SelectionHandleOneGrabArea");
413 #endif
414       primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
415       primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
416       primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
417
418       mTapDetector.Attach( primary.grabArea );
419       mPanGestureDetector.Attach( primary.grabArea );
420       primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
421
422       primary.actor.Add( primary.grabArea );
423       mActiveLayer.Add( primary.actor );
424     }
425
426     SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
427     if ( !secondary.actor )
428     {
429       if ( !secondary.releasedImage )
430       {
431         secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
432       }
433
434       secondary.actor = ImageActor::New( secondary.releasedImage );
435 #ifdef DECORATOR_DEBUG
436       secondary.actor.SetName("SelectionHandleTwo");
437 #endif
438       secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
439       secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
440       secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
441       secondary.flipped = false;
442
443       secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
444 #ifdef DECORATOR_DEBUG
445       secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
446 #endif
447       secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
448       secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
449       secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
450
451       mTapDetector.Attach( secondary.grabArea );
452       mPanGestureDetector.Attach( secondary.grabArea );
453       secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
454
455       secondary.actor.Add( secondary.grabArea );
456       mActiveLayer.Add( secondary.actor );
457     }
458
459     //SetUpHandlePropertyNotifications(); TODO
460   }
461
462   void CreateHighlight()
463   {
464     if ( !mHighlightMeshActor )
465     {
466       mHighlightMaterial = Material::New( "HighlightMaterial" );
467       mHighlightMaterial.SetDiffuseColor( mHighlightColor );
468
469       mHighlightMeshData.SetMaterial( mHighlightMaterial );
470       mHighlightMeshData.SetHasNormals( true );
471
472       mHighlightMesh = Mesh::New( mHighlightMeshData );
473
474       mHighlightMeshActor = MeshActor::New( mHighlightMesh );
475 #ifdef DECORATOR_DEBUG
476       mHighlightMeshActor.SetName( "HighlightMeshActor" );
477 #endif
478       mHighlightMeshActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
479       mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
480       mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
481
482       Actor parent = mTextControlParent.Self();
483       parent.Add( mHighlightMeshActor );
484     }
485   }
486
487   void UpdateHighlight()
488   {
489     //  Construct a Mesh with a texture to be used as the highlight 'box' for selected text
490     //
491     //  Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
492     //
493     //   [ TOP   ]  [ TOP ]      [TOP ]  [ TOP    ]      [ TOP  ]      [ TOP  ]
494     //  [ MIDDLE ]             [BOTTOM]  [BOTTOM]      [ MIDDLE ]   [ MIDDLE  ]
495     //  [ BOTTOM]                                      [ MIDDLE ]   [ MIDDLE  ]
496     //                                                 [BOTTOM]     [ MIDDLE  ]
497     //                                                              [BOTTOM]
498     //
499     //  Each quad is created as 2 triangles.
500     //  Middle is just 1 quad regardless of its size.
501     //
502     //  (0,0)         (0,0)
503     //     0*    *2     0*       *2
504     //     TOP          TOP
505     //     3*    *1     3*       *1
506     //  4*       *1     4*     *6
507     //     MIDDLE         BOTTOM
508     //  6*       *5     7*     *5
509     //  6*    *8
510     //   BOTTOM
511     //  9*    *7
512     //
513
514     if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
515     {
516       MeshData::VertexContainer vertices;
517       Dali::MeshData::FaceIndices faceIndices;
518
519       std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
520       std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
521
522       // vertex position defaults to (0 0 0)
523       MeshData::Vertex vertex;
524       // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
525       vertex.nZ = 1.0f;
526
527       for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
528       {
529         // Add each quad geometry (a sub-selection) to the mesh data.
530
531         // 0-----1
532         // |\    |
533         // | \ A |
534         // |  \  |
535         // | B \ |
536         // |    \|
537         // 2-----3
538
539         QuadCoordinates& quad = *iter;
540         // top-left (v+0)
541         vertex.x = quad.min.x;
542         vertex.y = quad.min.y;
543         vertices.push_back( vertex );
544
545         // top-right (v+1)
546         vertex.x = quad.max.x;
547         vertex.y = quad.min.y;
548         vertices.push_back( vertex );
549
550         // bottom-left (v+2)
551         vertex.x = quad.min.x;
552         vertex.y = quad.max.y;
553         vertices.push_back( vertex );
554
555         // bottom-right (v+3)
556         vertex.x = quad.max.x;
557         vertex.y = quad.max.y;
558         vertices.push_back( vertex );
559
560         // triangle A (3, 1, 0)
561         faceIndices.push_back( v + 3 );
562         faceIndices.push_back( v + 1 );
563         faceIndices.push_back( v );
564
565         // triangle B (0, 2, 3)
566         faceIndices.push_back( v );
567         faceIndices.push_back( v + 2 );
568         faceIndices.push_back( v + 3 );
569
570         mHighlightMeshData.SetFaceIndices( faceIndices );
571       }
572
573       BoneContainer bones(0); // passed empty as bones not required
574       mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
575       mHighlightMesh.UpdateMeshData( mHighlightMeshData );
576     }
577   }
578
579   void OnTap( Actor actor, const TapGesture& tap )
580   {
581     if( actor == mGrabHandle )
582     {
583       // TODO
584     }
585   }
586
587   void OnPan( Actor actor, const PanGesture& gesture )
588   {
589     if( actor == mGrabArea )
590     {
591       if( Gesture::Started == gesture.state )
592       {
593         mGrabDisplacementX = mGrabDisplacementY = 0;
594       }
595
596       mGrabDisplacementX += gesture.displacement.x;
597       mGrabDisplacementY += gesture.displacement.y;
598
599       float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
600       float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
601
602       if( Gesture::Started    == gesture.state ||
603           Gesture::Continuing == gesture.state )
604       {
605         mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
606       }
607       else if( Gesture::Finished  == gesture.state ||
608                Gesture::Cancelled == gesture.state )
609       {
610         mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
611       }
612     }
613   }
614
615   bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
616   {
617     // TODO
618     return false;
619   }
620
621   bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
622   {
623     // TODO
624     return false;
625   }
626
627
628   Internal::Control& mTextControlParent;
629   Observer& mObserver;
630
631   Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
632
633   unsigned int mActiveCursor;
634   bool         mActiveGrabHandle;
635   bool         mActiveSelection;
636   bool         mActiveCopyPastePopup;
637
638   CursorImpl mCursor[CURSOR_COUNT];
639
640   Timer mCursorBlinkTimer; // Timer to signal cursor to blink
641   unsigned int mCursorBlinkInterval;
642   float mCursorBlinkDuration;
643   bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
644
645   ImageActor mPrimaryCursor;
646   ImageActor mSecondaryCursor;
647
648   ImageActor mGrabHandle;
649   Actor mGrabArea;
650   float mGrabDisplacementX;
651   float mGrabDisplacementY;
652
653   SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
654
655   MeshActor         mHighlightMeshActor;        ///< Mesh Actor to display highlight
656   Mesh              mHighlightMesh;             ///< Mesh for highlight
657   MeshData          mHighlightMeshData;         ///< Mesh Data for highlight
658   Material          mHighlightMaterial;         ///< Material used for highlight
659   Vector4           mHighlightColor;            ///< Color of the highlight
660   QuadContainer     mHighlightQuadList;         ///< Sub-selections that combine to create the complete selection highlight
661
662   TextSelectionPopup mCopyPastePopup;
663
664   Image mCursorImage;
665   Image mGrabHandleImage;
666
667   TapGestureDetector mTapDetector;
668   PanGestureDetector mPanGestureDetector;
669
670   Rect<int> mBoundingBox;
671 };
672
673 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
674 {
675   return DecoratorPtr( new Decorator(parent, observer) );
676 }
677
678 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
679 {
680   mImpl->mBoundingBox = boundingBox;
681 }
682
683 const Rect<int>& Decorator::GetBoundingBox() const
684 {
685   return mImpl->mBoundingBox;
686 }
687
688 void Decorator::Relayout( const Vector2& size, const Vector2& scrollPosition )
689 {
690   mImpl->Relayout( size, scrollPosition );
691 }
692
693 /** Cursor **/
694
695 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
696 {
697   mImpl->mActiveCursor = activeCursor;
698 }
699
700 unsigned int Decorator::GetActiveCursor() const
701 {
702   return mImpl->mActiveCursor;
703 }
704
705 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
706 {
707   // Adjust grab handle displacement
708   mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
709   mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
710
711   mImpl->mCursor[cursor].x = x;
712   mImpl->mCursor[cursor].y = y;
713   mImpl->mCursor[cursor].height = height;
714 }
715
716 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
717 {
718   x = mImpl->mCursor[cursor].x;
719   y = mImpl->mCursor[cursor].y;
720   height = mImpl->mCursor[cursor].height;
721 }
722
723 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
724 {
725   mImpl->mCursor[cursor].color = color;
726 }
727
728 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
729 {
730   return mImpl->mCursor[cursor].color;
731 }
732
733 void Decorator::StartCursorBlink()
734 {
735   if ( !mImpl->mCursorBlinkTimer )
736   {
737     mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
738     mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
739   }
740
741   if ( !mImpl->mCursorBlinkTimer.IsRunning() )
742   {
743     mImpl->mCursorBlinkTimer.Start();
744   }
745 }
746
747 void Decorator::StopCursorBlink()
748 {
749   if ( mImpl->mCursorBlinkTimer )
750   {
751     mImpl->mCursorBlinkTimer.Stop();
752   }
753 }
754
755 void Decorator::SetCursorBlinkInterval( float seconds )
756 {
757   mImpl->mCursorBlinkInterval = seconds*MILLISECONDS; // Convert to milliseconds
758 }
759
760 float Decorator::GetCursorBlinkInterval() const
761 {
762   return mImpl->mCursorBlinkInterval;
763 }
764
765 void Decorator::SetCursorBlinkDuration( float seconds )
766 {
767   mImpl->mCursorBlinkDuration = seconds;
768 }
769
770 float Decorator::GetCursorBlinkDuration() const
771 {
772   return mImpl->mCursorBlinkDuration;
773 }
774
775 /** GrabHandle **/
776
777 void Decorator::SetGrabHandleActive( bool active )
778 {
779   mImpl->mActiveGrabHandle = active;
780 }
781
782 bool Decorator::IsGrabHandleActive() const
783 {
784   return mImpl->mActiveGrabHandle;
785 }
786
787 void Decorator::SetGrabHandleImage( Dali::Image image )
788 {
789   mImpl->mGrabHandleImage = image;
790 }
791
792 Dali::Image Decorator::GetGrabHandleImage() const
793 {
794   return mImpl->mGrabHandleImage;
795 }
796
797 /** Selection **/
798
799 void Decorator::SetSelectionActive( bool active )
800 {
801   mImpl->mActiveSelection = active;
802 }
803
804 bool Decorator::IsSelectionActive() const
805 {
806   return mImpl->mActiveSelection;
807 }
808
809 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
810 {
811   mImpl->mSelectionHandle[handle].x = x;
812   mImpl->mSelectionHandle[handle].y = y;
813   mImpl->mSelectionHandle[handle].cursorHeight = height;
814 }
815
816 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
817 {
818   x = mImpl->mSelectionHandle[handle].x;
819   y = mImpl->mSelectionHandle[handle].y;
820   height = mImpl->mSelectionHandle[handle].cursorHeight;
821 }
822
823 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
824 {
825   if( SELECTION_HANDLE_PRESSED == state )
826   {
827     mImpl->mSelectionHandle[handle].pressedImage = image;
828   }
829   else
830   {
831     mImpl->mSelectionHandle[handle].releasedImage = image;
832   }
833 }
834
835 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
836 {
837   if( SELECTION_HANDLE_PRESSED == state )
838   {
839     return mImpl->mSelectionHandle[handle].pressedImage;
840   }
841
842   return mImpl->mSelectionHandle[handle].releasedImage;
843 }
844
845 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
846 {
847   mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
848 }
849
850 void Decorator::ClearHighlights()
851 {
852   mImpl->mHighlightQuadList.clear();
853 }
854
855 void Decorator::SetPopupActive( bool active )
856 {
857   mImpl->mActiveCopyPastePopup = active;
858 }
859
860 bool Decorator::IsPopupActive() const
861 {
862   return mImpl->mActiveCopyPastePopup ;
863 }
864
865 Decorator::~Decorator()
866 {
867   delete mImpl;
868 }
869
870 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
871 : mImpl( NULL )
872 {
873   mImpl = new Decorator::Impl( parent, observer );
874 }
875
876 } // namespace Text
877
878 } // namespace Toolkit
879
880 } // namespace Dali