Do not re-position the text popup when the text scrolls.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / decorator / text-decorator.cpp
index bdfc356..69ecac3 100644 (file)
@@ -87,8 +87,6 @@ namespace
 {
 
 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "cursor_handler_center.png" );
-const char* DEFAULT_SELECTION_HANDLE_ONE_RELEASED( DALI_IMAGE_DIR "selection_handle_left.png" );
-const char* DEFAULT_SELECTION_HANDLE_TWO_RELEASED( DALI_IMAGE_DIR "selection_handle_right.png" );
 
 const int DEFAULT_POPUP_OFFSET( -100.0f ); // Vertical offset of Popup from cursor or handles position.
 
@@ -109,6 +107,8 @@ const float SCROLL_THRESHOLD = 10.f;
 const float SCROLL_SPEED = 300.f;
 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
 
+const float CURSOR_WIDTH = 1.f;
+
 /**
  * structure to hold coordinates of each quad, which will make up the mesh.
  */
@@ -214,6 +214,7 @@ struct Decorator::Impl : public ConnectionTracker
 
     ImageActor actor;
     Actor grabArea;
+    ImageActor markerActor;
 
     Vector2 position;
     float lineHeight; ///< Not the handle height
@@ -250,6 +251,7 @@ struct Decorator::Impl : public ConnectionTracker
     mActiveCursor( ACTIVE_CURSOR_NONE ),
     mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
     mCursorBlinkDuration( 0.0f ),
+    mCursorWidth( CURSOR_WIDTH ),
     mHandleScrolling( HANDLE_TYPE_COUNT ),
     mScrollDirection( SCROLL_NONE ),
     mScrollThreshold( SCROLL_THRESHOLD ),
@@ -258,6 +260,7 @@ struct Decorator::Impl : public ConnectionTracker
     mTextDepth( 0u ),
     mActiveCopyPastePopup( false ),
     mCursorBlinkStatus( true ),
+    mDelayCursorBlink( false ),
     mPrimaryCursorVisible( false ),
     mSecondaryCursorVisible( false ),
     mSwapSelectionHandles( false ),
@@ -290,9 +293,9 @@ struct Decorator::Impl : public ConnectionTracker
 
         mPrimaryCursor.SetPosition( position.x,
                                     position.y );
-        mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
+        mPrimaryCursor.SetSize( Size( mCursorWidth, cursor.cursorHeight ) );
       }
-      mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
+      mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
     }
     if( mSecondaryCursor )
     {
@@ -302,9 +305,9 @@ struct Decorator::Impl : public ConnectionTracker
       {
         mSecondaryCursor.SetPosition( cursor.position.x,
                                       cursor.position.y );
-        mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
+        mSecondaryCursor.SetSize( Size( mCursorWidth, cursor.cursorHeight ) );
       }
-      mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
+      mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
     }
 
     // Show or hide the grab handle
@@ -321,7 +324,7 @@ struct Decorator::Impl : public ConnectionTracker
 
         CreateGrabHandle();
 
-        grabHandle.actor.SetPosition( position.x,
+        grabHandle.actor.SetPosition( position.x - floor( 0.5f * mCursorWidth ),
                                       position.y + grabHandle.lineHeight ); // TODO : Fix for multiline.
       }
       grabHandle.actor.SetVisible( isVisible );
@@ -352,12 +355,16 @@ struct Decorator::Impl : public ConnectionTracker
         {
           primary.actor.SetPosition( primaryPosition.x,
                                      primaryPosition.y + primary.lineHeight ); // TODO : Fix for multiline.
+
+          SetSelectionHandleMarkerSize( primary );
         }
 
         if( isSecondaryVisible )
         {
           secondary.actor.SetPosition( secondaryPosition.x,
                                        secondaryPosition.y + secondary.lineHeight ); // TODO : Fix for multiline.
+
+          SetSelectionHandleMarkerSize( secondary );
         }
       }
       primary.actor.SetVisible( isPrimaryVisible );
@@ -403,7 +410,6 @@ struct Decorator::Impl : public ConnectionTracker
     mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
     mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
     mHighlightPosition += scrollOffset;
-    DeterminePositionPopup();
   }
 
   void ShowPopup()
@@ -465,7 +471,7 @@ struct Decorator::Impl : public ConnectionTracker
     cursor = CreateSolidColorActor( color );
     cursor.SetSortModifier( DECORATION_DEPTH_INDEX );
     cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
-    cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
+    cursor.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
   }
 
   // Add or Remove cursor(s) from parent
@@ -529,18 +535,26 @@ struct Decorator::Impl : public ConnectionTracker
 
   bool OnCursorBlinkTimerTick()
   {
-    // Cursor blinking
-    if ( mPrimaryCursor )
+    if( !mDelayCursorBlink )
     {
-      mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
+      // Cursor blinking
+      if ( mPrimaryCursor )
+      {
+        mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
+      }
+      if ( mSecondaryCursor )
+      {
+        mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
+      }
+
+      mCursorBlinkStatus = !mCursorBlinkStatus;
     }
-    if ( mSecondaryCursor )
+    else
     {
-      mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
+      // Resume blinking
+      mDelayCursorBlink = false;
     }
 
-    mCursorBlinkStatus = !mCursorBlinkStatus;
-
     return true;
   }
 
@@ -579,6 +593,15 @@ struct Decorator::Impl : public ConnectionTracker
     mActiveLayer.RaiseToTop();
   }
 
+  void SetSelectionHandleMarkerSize( HandleImpl& handle )
+  {
+    if ( handle.markerActor )
+    {
+      handle.markerActor.SetResizePolicy ( ResizePolicy::FIXED, Dimension::HEIGHT );
+      handle.markerActor.SetSize( 0, handle.lineHeight );
+    }
+  }
+
   void CreateGrabHandle()
   {
     HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
@@ -629,16 +652,32 @@ struct Decorator::Impl : public ConnectionTracker
     }
   }
 
-  void CreateSelectionHandles()
+  void CreateHandleMarker( HandleImpl& handle, Image& image, HandleType handleType )
   {
-    HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
-    if( !primary.actor )
+    if ( image)
     {
-      if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
+      handle.markerActor = ImageActor::New( image );
+      handle.markerActor.SetColor( mHandleColor );
+      handle.actor.Add( handle.markerActor );
+
+      if ( LEFT_SELECTION_HANDLE == handleType )
       {
-        mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE_RELEASED );
+        handle.markerActor.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
+        handle.markerActor.SetParentOrigin( ParentOrigin::TOP_RIGHT );
       }
+      else if ( RIGHT_SELECTION_HANDLE == handleType )
+      {
+        handle.markerActor.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
+        handle.markerActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
+      }
+    }
+  }
 
+  void CreateSelectionHandles()
+  {
+    HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
+    if( !primary.actor )
+    {
       primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
 #ifdef DECORATOR_DEBUG
       primary.actor.SetName("SelectionHandleOne");
@@ -662,6 +701,8 @@ struct Decorator::Impl : public ConnectionTracker
       primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
 
       primary.actor.Add( primary.grabArea );
+
+      CreateHandleMarker( primary, mHandleImages[LEFT_SELECTION_HANDLE_MARKER][HANDLE_IMAGE_RELEASED], LEFT_SELECTION_HANDLE );
     }
 
     if( !primary.actor.GetParent() )
@@ -672,11 +713,6 @@ struct Decorator::Impl : public ConnectionTracker
     HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
     if( !secondary.actor )
     {
-      if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
-      {
-        mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO_RELEASED );
-      }
-
       secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
 #ifdef DECORATOR_DEBUG
       secondary.actor.SetName("SelectionHandleTwo");
@@ -700,6 +736,8 @@ struct Decorator::Impl : public ConnectionTracker
       secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
 
       secondary.actor.Add( secondary.grabArea );
+
+      CreateHandleMarker( secondary, mHandleImages[RIGHT_SELECTION_HANDLE_MARKER][HANDLE_IMAGE_RELEASED], RIGHT_SELECTION_HANDLE  );
     }
 
     if( !secondary.actor.GetParent() )
@@ -742,7 +780,6 @@ struct Decorator::Impl : public ConnectionTracker
 
         for( std::size_t v = 0; iter != endIter; ++iter,v+=4 )
         {
-
           QuadCoordinates& quad = *iter;
 
           // top-left (v+0)
@@ -816,7 +853,10 @@ struct Decorator::Impl : public ConnectionTracker
 
       mHighlightQuadList.clear();
 
-      mHighlightRenderer.SetDepthIndex( mTextDepth - 2u ); // text is rendered at mTextDepth and text's shadow at mTextDepth -1u.
+      if( mHighlightRenderer )
+      {
+        mHighlightRenderer.SetDepthIndex( mTextDepth - 2u ); // text is rendered at mTextDepth and text's shadow at mTextDepth -1u.
+      }
     }
   }
 
@@ -1142,6 +1182,15 @@ struct Decorator::Impl : public ConnectionTracker
     rightHandle.actor.SetImage( rightFlipped ? mHandleImages[LEFT_SELECTION_HANDLE][rightImageType] : mHandleImages[RIGHT_SELECTION_HANDLE][rightImageType] );
 
     rightHandle.actor.SetAnchorPoint( rightFlipped ? AnchorPoint::TOP_RIGHT : AnchorPoint::TOP_LEFT );
+
+    if ( leftHandle.markerActor )
+    {
+      leftHandle.markerActor.SetImage( leftFlipped ? mHandleImages[RIGHT_SELECTION_HANDLE_MARKER][leftImageType] : mHandleImages[LEFT_SELECTION_HANDLE_MARKER][leftImageType] );
+    }
+    if ( rightHandle.markerActor )
+    {
+      rightHandle.markerActor.SetImage( rightFlipped ? mHandleImages[LEFT_SELECTION_HANDLE_MARKER][rightImageType] : mHandleImages[RIGHT_SELECTION_HANDLE_MARKER][rightImageType] );
+    }
   }
 
   void SetScrollThreshold( float threshold )
@@ -1261,6 +1310,7 @@ struct Decorator::Impl : public ConnectionTracker
   unsigned int        mActiveCursor;
   unsigned int        mCursorBlinkInterval;
   float               mCursorBlinkDuration;
+  float               mCursorWidth;             ///< The width of the cursors in pixels.
   HandleType          mHandleScrolling;         ///< The handle which is scrolling.
   ScrollDirection     mScrollDirection;         ///< The direction of the scroll.
   float               mScrollThreshold;         ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
@@ -1270,6 +1320,7 @@ struct Decorator::Impl : public ConnectionTracker
 
   bool                mActiveCopyPastePopup   : 1;
   bool                mCursorBlinkStatus      : 1; ///< Flag to switch between blink on and blink off.
+  bool                mDelayCursorBlink       : 1; ///< Used to avoid cursor blinking when entering text.
   bool                mPrimaryCursorVisible   : 1; ///< Whether the primary cursor is visible.
   bool                mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
   bool                mSwapSelectionHandles   : 1; ///< Whether to swap the selection handle images.
@@ -1366,6 +1417,14 @@ void Decorator::StopCursorBlink()
   {
     mImpl->mCursorBlinkTimer.Stop();
   }
+
+  mImpl->mCursorBlinkStatus = true; // Keep cursor permanently shown
+}
+
+void Decorator::DelayCursorBlink()
+{
+  mImpl->mCursorBlinkStatus = true; // Show cursor for a bit longer
+  mImpl->mDelayCursorBlink = true;
 }
 
 void Decorator::SetCursorBlinkInterval( float seconds )
@@ -1388,6 +1447,16 @@ float Decorator::GetCursorBlinkDuration() const
   return mImpl->mCursorBlinkDuration;
 }
 
+void Decorator::SetCursorWidth( int width )
+{
+  mImpl->mCursorWidth = static_cast<float>( width );
+}
+
+int Decorator::GetCursorWidth() const
+{
+  return static_cast<int>( mImpl->mCursorWidth );
+}
+
 /** Handles **/
 
 void Decorator::SetHandleActive( HandleType handleType, bool active )