Reposition the grab/selection handles position.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / decorator / text-decorator.cpp
index 5c45bf3..5d3976c 100644 (file)
@@ -85,11 +85,6 @@ Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLog
 // Local Data
 namespace
 {
-
-const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "cursor_handler_center.png" );
-
-const int DEFAULT_POPUP_OFFSET( -100.0f ); // Vertical offset of Popup from cursor or handles position.
-
 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.25f, 1.5f, 1.0f );
 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.25f, 1.5f, 1.0f );
 
@@ -107,6 +102,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.
  */
@@ -157,6 +154,16 @@ void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangl
                                originY + boundingRectangle.height );
 }
 
+void WorldToLocalCoordinatesBoundingBox( const Dali::Vector4& boundingBox, Dali::Rect<int>& boundingRectangle )
+{
+  // Convert to local coordinates and store as a Dali::Rect.
+  Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
+
+  boundingRectangle.x = boundingBox.x + 0.5f * stageSize.width;
+  boundingRectangle.y = boundingBox.y + 0.5f * stageSize.height;
+  boundingRectangle.width = boundingBox.z - boundingBox.x;
+  boundingRectangle.height = boundingBox.w - boundingBox.y;
+}
 
 } // end of namespace
 
@@ -200,13 +207,16 @@ struct Decorator::Impl : public ConnectionTracker
   {
     HandleImpl()
     : position(),
+      size(),
       lineHeight( 0.0f ),
       grabDisplacementX( 0.f ),
       grabDisplacementY( 0.f ),
       active( false ),
       visible( false ),
       pressed( false ),
-      flipped( false )
+      verticallyFlippedPreferred( false ),
+      horizontallyFlipped( false ),
+      verticallyFlipped( false )
     {
     }
 
@@ -215,26 +225,27 @@ struct Decorator::Impl : public ConnectionTracker
     ImageActor markerActor;
 
     Vector2 position;
-    float lineHeight; ///< Not the handle height
-    float grabDisplacementX;
-    float grabDisplacementY;
-    bool active  : 1;
-    bool visible : 1;
-    bool pressed : 1;
-    bool flipped : 1;
+    Size    size;
+    float   lineHeight;              ///< Not the handle height
+    float   grabDisplacementX;
+    float   grabDisplacementY;
+    bool    active  : 1;
+    bool    visible : 1;
+    bool    pressed : 1;
+    bool    verticallyFlippedPreferred : 1; ///< Whether the handle is preferred to be vertically flipped.
+    bool    horizontallyFlipped        : 1; ///< Whether the handle has been horizontally flipped.
+    bool    verticallyFlipped          : 1; ///< Whether the handle has been vertically flipped.
   };
 
   struct PopupImpl
   {
     PopupImpl()
-    : position(),
-      offset( DEFAULT_POPUP_OFFSET )
+    : position()
     {
     }
 
     TextSelectionPopup actor;
     Vector3 position;
-    int offset;
   };
 
   Impl( ControllerInterface& controller,
@@ -243,12 +254,13 @@ struct Decorator::Impl : public ConnectionTracker
     mEnabledPopupButtons( TextSelectionPopup::NONE ),
     mTextSelectionPopupCallbackInterface( callbackInterface ),
     mHandleColor( HANDLE_COLOR ),
-    mBoundingBox( Rect<int>() ),
+    mBoundingBox(),
     mHighlightColor( LIGHT_BLUE ),
     mHighlightPosition( Vector2::ZERO ),
     mActiveCursor( ACTIVE_CURSOR_NONE ),
     mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
     mCursorBlinkDuration( 0.0f ),
+    mCursorWidth( CURSOR_WIDTH ),
     mHandleScrolling( HANDLE_TYPE_COUNT ),
     mScrollDirection( SCROLL_NONE ),
     mScrollThreshold( SCROLL_THRESHOLD ),
@@ -256,15 +268,24 @@ struct Decorator::Impl : public ConnectionTracker
     mScrollDistance( SCROLL_DISTANCE ),
     mTextDepth( 0u ),
     mActiveCopyPastePopup( false ),
+    mPopupSetNewPosition( true ),
     mCursorBlinkStatus( true ),
+    mDelayCursorBlink( false ),
     mPrimaryCursorVisible( false ),
     mSecondaryCursorVisible( false ),
-    mSwapSelectionHandles( false ),
+    mFlipSelectionHandlesOnCross( false ),
+    mFlipLeftSelectionHandleDirection( false ),
+    mFlipRightSelectionHandleDirection( false ),
+    mHandlePanning( false ),
+    mHandleCurrentCrossed( false ),
+    mHandlePreviousCrossed( false ),
     mNotifyEndOfScroll( false )
   {
     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
     mQuadIndexFormat[ "indices" ] = Property::INTEGER;
     mHighlightMaterial = Material::New( Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ) );
+
+    SetupTouchEvents();
   }
 
   /**
@@ -273,6 +294,8 @@ struct Decorator::Impl : public ConnectionTracker
    */
   void Relayout( const Vector2& size )
   {
+    mControlSize = size;
+
     // TODO - Remove this if nothing is active
     CreateActiveLayer();
 
@@ -282,48 +305,52 @@ struct Decorator::Impl : public ConnectionTracker
     if( mPrimaryCursor )
     {
       const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
-      mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
+      mPrimaryCursorVisible = ( cursor.position.x + mCursorWidth <= mControlSize.width ) && ( cursor.position.x >= 0.f );
       if( mPrimaryCursorVisible )
       {
-        Vector2 position = cursor.position;
-
-        mPrimaryCursor.SetPosition( position.x,
-                                    position.y );
-        mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
+        mPrimaryCursor.SetPosition( cursor.position.x,
+                                    cursor.position.y );
+        mPrimaryCursor.SetSize( Size( mCursorWidth, cursor.cursorHeight ) );
       }
-      mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
+      mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
     }
     if( mSecondaryCursor )
     {
       const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
-      mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
+      mSecondaryCursorVisible = ( cursor.position.x + mCursorWidth <= mControlSize.width ) && ( cursor.position.x >= 0.f );
       if( mSecondaryCursorVisible )
       {
         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
     HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+    bool newGrabHandlePosition = false;
     if( grabHandle.active )
     {
-      Vector2 position = grabHandle.position;
-
-      const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
+      const bool isVisible = ( grabHandle.position.x + floor( 0.5f * mCursorWidth ) <= mControlSize.width ) && ( grabHandle.position.x >= 0.f );
 
       if( isVisible )
       {
-        SetupTouchEvents();
-
         CreateGrabHandle();
 
-        grabHandle.actor.SetPosition( position.x,
-                                      position.y + grabHandle.lineHeight ); // TODO : Fix for multiline.
+        // Sets the grab handle position and calculate if it needs to be vertically flipped if it exceeds the boundary box.
+        SetGrabHandlePosition();
+
+        // Sets the grab handle image according if it's pressed, flipped, etc.
+        SetHandleImage( GRAB_HANDLE );
+
+        newGrabHandlePosition = true;
+      }
+
+      if( grabHandle.actor )
+      {
+        grabHandle.actor.SetVisible( isVisible );
       }
-      grabHandle.actor.SetVisible( isVisible );
     }
     else if( grabHandle.actor )
     {
@@ -333,38 +360,50 @@ struct Decorator::Impl : public ConnectionTracker
     // Show or hide the selection handles/highlight
     HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
     HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
+    bool newPrimaryHandlePosition = false;
+    bool newSecondaryHandlePosition = false;
     if( primary.active || secondary.active )
     {
-      Vector2 primaryPosition = primary.position;
-      Vector2 secondaryPosition = secondary.position;
-
-      const bool isPrimaryVisible = ( primaryPosition.x <= size.width ) && ( primaryPosition.x >= 0.f );
-      const bool isSecondaryVisible = ( secondaryPosition.x <= size.width ) && ( secondaryPosition.x >= 0.f );
+      const bool isPrimaryVisible = ( primary.position.x <= mControlSize.width ) && ( primary.position.x >= 0.f );
+      const bool isSecondaryVisible = ( secondary.position.x <= mControlSize.width ) && ( secondary.position.x >= 0.f );
 
       if( isPrimaryVisible || isSecondaryVisible )
       {
-        SetupTouchEvents();
-
         CreateSelectionHandles();
 
         if( isPrimaryVisible )
         {
-          primary.actor.SetPosition( primaryPosition.x,
-                                     primaryPosition.y + primary.lineHeight ); // TODO : Fix for multiline.
+          SetSelectionHandlePosition( LEFT_SELECTION_HANDLE );
+
+          // Sets the primary handle image according if it's pressed, flipped, etc.
+          SetHandleImage( LEFT_SELECTION_HANDLE );
 
           SetSelectionHandleMarkerSize( primary );
+
+          newPrimaryHandlePosition = true;
         }
 
         if( isSecondaryVisible )
         {
-          secondary.actor.SetPosition( secondaryPosition.x,
-                                       secondaryPosition.y + secondary.lineHeight ); // TODO : Fix for multiline.
+          SetSelectionHandlePosition( RIGHT_SELECTION_HANDLE );
+
+          // Sets the secondary handle image according if it's pressed, flipped, etc.
+          SetHandleImage( RIGHT_SELECTION_HANDLE );
 
           SetSelectionHandleMarkerSize( secondary );
+
+          newSecondaryHandlePosition = true;
         }
       }
-      primary.actor.SetVisible( isPrimaryVisible );
-      secondary.actor.SetVisible( isSecondaryVisible );
+
+      if( primary.actor )
+      {
+        primary.actor.SetVisible( isPrimaryVisible );
+      }
+      if( secondary.actor )
+      {
+        secondary.actor.SetVisible( isSecondaryVisible );
+      }
 
       CreateHighlight();
       UpdateHighlight();
@@ -385,6 +424,14 @@ struct Decorator::Impl : public ConnectionTracker
       }
     }
 
+    if( newGrabHandlePosition    ||
+        newPrimaryHandlePosition ||
+        newSecondaryHandlePosition )
+    {
+      // Setup property notifications to find whether the handles leave the boundaries of the current display.
+      SetupActiveLayerPropertyNotifications();
+    }
+
     if( mActiveCopyPastePopup )
     {
       ShowPopup();
@@ -394,6 +441,7 @@ struct Decorator::Impl : public ConnectionTracker
       if( mCopyPastePopup.actor )
       {
         mCopyPastePopup.actor.HidePopup();
+        mPopupSetNewPosition = true;
       }
     }
   }
@@ -406,7 +454,6 @@ struct Decorator::Impl : public ConnectionTracker
     mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
     mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
     mHighlightPosition += scrollOffset;
-    DeterminePositionPopup();
   }
 
   void ShowPopup()
@@ -427,33 +474,45 @@ struct Decorator::Impl : public ConnectionTracker
 
   void DeterminePositionPopup()
   {
-    if ( !mActiveCopyPastePopup )
+    if( !mActiveCopyPastePopup )
     {
       return;
     }
 
-    if ( mHandle[LEFT_SELECTION_HANDLE].active || mHandle[RIGHT_SELECTION_HANDLE].active )
+    // Retrieves the popup's size after relayout.
+    const Vector3 popupSize = Vector3( mCopyPastePopup.actor.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.actor.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
+
+    if( mPopupSetNewPosition )
     {
-      float minHandleXPosition = std::min (  mHandle[LEFT_SELECTION_HANDLE].position.x, mHandle[RIGHT_SELECTION_HANDLE].position.x );
-      float maxHandleXPosition = std::max (  mHandle[LEFT_SELECTION_HANDLE].position.x, mHandle[RIGHT_SELECTION_HANDLE].position.x );
+      const HandleImpl& primaryHandle = mHandle[LEFT_SELECTION_HANDLE];
+      const HandleImpl& secondaryHandle = mHandle[RIGHT_SELECTION_HANDLE];
+      const HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+      const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
 
-      float minHandleYPosition = std::min (  mHandle[LEFT_SELECTION_HANDLE].position.y, mHandle[RIGHT_SELECTION_HANDLE].position.y );
+      if( primaryHandle.active || secondaryHandle.active )
+      {
+        // Calculates the popup's position if selection handles are active.
+        const float minHandleXPosition = std::min( primaryHandle.position.x, secondaryHandle.position.x );
+        const float maxHandleXPosition = std::max( primaryHandle.position.x, secondaryHandle.position.x );
+        const float maxHandleHeight = std::max( primaryHandle.size.height, secondaryHandle.size.height );
 
-      mCopyPastePopup.position.x = minHandleXPosition + ( ( maxHandleXPosition - minHandleXPosition ) *0.5f );
-      mCopyPastePopup.position.y = minHandleYPosition + mCopyPastePopup.offset;
-    }
-    else
-    {
-      mCopyPastePopup.position = Vector3( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f ); //todo 100 to be an offset Property
+        mCopyPastePopup.position.x = minHandleXPosition + ( ( maxHandleXPosition - minHandleXPosition ) * 0.5f );
+        mCopyPastePopup.position.y = -0.5f * popupSize.height - maxHandleHeight + std::min( primaryHandle.position.y, secondaryHandle.position.y );
+      }
+      else
+      {
+        // Calculates the popup's position if the grab handle is active.
+        mCopyPastePopup.position = Vector3( cursor.position.x, -0.5f * popupSize.height - grabHandle.size.height + cursor.position.y, 0.0f );
+      }
     }
 
-    Vector3 popupSize = Vector3( mCopyPastePopup.actor.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.actor.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
-
-    GetConstrainedPopupPosition( mCopyPastePopup.position, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
+    // Checks if there is enough space above the text control. If not it places the popup under it.
+    GetConstrainedPopupPosition( mCopyPastePopup.position, popupSize * AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
 
     SetUpPopupPositionNotifications();
 
     mCopyPastePopup.actor.SetPosition( mCopyPastePopup.position );
+    mPopupSetNewPosition = false;
   }
 
   void PopupRelayoutComplete( Actor actor )
@@ -468,7 +527,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_LEFT );
   }
 
   // Add or Remove cursor(s) from parent
@@ -532,34 +591,36 @@ 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;
   }
 
   void SetupTouchEvents()
   {
-    if ( !mTapDetector )
-    {
-      mTapDetector = TapGestureDetector::New();
-      mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
-    }
+    mTapDetector = TapGestureDetector::New();
+    mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
 
-    if ( !mPanGestureDetector )
-    {
-      mPanGestureDetector = PanGestureDetector::New();
-      mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
-    }
+    mPanGestureDetector = PanGestureDetector::New();
+    mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
   }
 
   void CreateActiveLayer()
@@ -584,9 +645,8 @@ struct Decorator::Impl : public ConnectionTracker
 
   void SetSelectionHandleMarkerSize( HandleImpl& handle )
   {
-    if ( handle.markerActor )
+    if( handle.markerActor )
     {
-      handle.markerActor.SetResizePolicy ( ResizePolicy::FIXED, Dimension::HEIGHT );
       handle.markerActor.SetSize( 0, handle.lineHeight );
     }
   }
@@ -596,11 +656,6 @@ struct Decorator::Impl : public ConnectionTracker
     HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
     if( !grabHandle.actor )
     {
-      if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
-      {
-        mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
-      }
-
       grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
       grabHandle.actor.SetSortModifier( DECORATION_DEPTH_INDEX );
       grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
@@ -643,18 +698,20 @@ struct Decorator::Impl : public ConnectionTracker
 
   void CreateHandleMarker( HandleImpl& handle, Image& image, HandleType handleType )
   {
-    if ( image)
+    if( image )
     {
       handle.markerActor = ImageActor::New( image );
       handle.markerActor.SetColor( mHandleColor );
       handle.actor.Add( handle.markerActor );
 
-      if ( LEFT_SELECTION_HANDLE == handleType )
+      handle.markerActor.SetResizePolicy ( ResizePolicy::FIXED, Dimension::HEIGHT );
+
+      if( LEFT_SELECTION_HANDLE == handleType )
       {
         handle.markerActor.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
         handle.markerActor.SetParentOrigin( ParentOrigin::TOP_RIGHT );
       }
-      else if ( RIGHT_SELECTION_HANDLE == handleType )
+      else if( RIGHT_SELECTION_HANDLE == handleType )
       {
         handle.markerActor.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
         handle.markerActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
@@ -673,7 +730,6 @@ struct Decorator::Impl : public ConnectionTracker
 #endif
       primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
       primary.actor.SetSortModifier( DECORATION_DEPTH_INDEX );
-      primary.flipped = false;
       primary.actor.SetColor( mHandleColor );
 
       primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
@@ -708,7 +764,6 @@ struct Decorator::Impl : public ConnectionTracker
 #endif
       secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
       secondary.actor.SetSortModifier( DECORATION_DEPTH_INDEX );
-      secondary.flipped = false;
       secondary.actor.SetColor( mHandleColor );
 
       secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
@@ -735,6 +790,177 @@ struct Decorator::Impl : public ConnectionTracker
     }
   }
 
+  void CalculateHandleWorldCoordinates( HandleImpl& handle, Vector2& position )
+  {
+    // Gets the world position of the active layer. The active layer is where the handles are added.
+    const Vector3 parentWorldPosition = mActiveLayer.GetCurrentWorldPosition();
+
+    // The grab handle position in world coords.
+    // The active layer's world position is the center of the active layer. The origin of the
+    // coord system of the handles is the top left of the active layer.
+    position.x = parentWorldPosition.x - 0.5f * mControlSize.width + handle.position.x;
+    position.y = parentWorldPosition.y - 0.5f * mControlSize.height + handle.position.y;
+  }
+
+  void SetGrabHandlePosition()
+  {
+    // Reference to the grab handle.
+    HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+
+    // Transforms the handle position into world coordinates.
+    // @note This is not the same value as grabHandle.actor.GetCurrentWorldPosition()
+    // as it's transforming the handle's position set by the text-controller and not
+    // the final position set to the actor. Another difference is the GetCurrentWorldPosition()
+    // retrieves the position of the center of the actor but the handle's position set
+    // by the text controller is not the center of the actor.
+    Vector2 grabHandleWorldPosition;
+    CalculateHandleWorldCoordinates( grabHandle, grabHandleWorldPosition );
+
+    // Check if the grab handle exceeds the boundaries of the decoration box.
+    // At the moment only the height is checked for the grab handle.
+
+    grabHandle.verticallyFlipped = ( grabHandle.verticallyFlippedPreferred &&
+                                     ( ( grabHandleWorldPosition.y - grabHandle.size.height ) > mBoundingBox.y ) ) ||
+                                   ( grabHandleWorldPosition.y + grabHandle.lineHeight + grabHandle.size.height > mBoundingBox.w );
+
+    // The grab handle 'y' position in local coords.
+    // If the grab handle exceeds the bottom of the decoration box,
+    // set the 'y' position to the top of the line.
+    // The SetGrabHandleImage() method will change the orientation.
+    const float yLocalPosition = grabHandle.verticallyFlipped ? grabHandle.position.y : grabHandle.position.y + grabHandle.lineHeight;
+
+    grabHandle.actor.SetPosition( grabHandle.position.x + floor( 0.5f * mCursorWidth ),
+                                  yLocalPosition ); // TODO : Fix for multiline.
+  }
+
+  void SetSelectionHandlePosition( HandleType type )
+  {
+    const bool isPrimaryHandle = LEFT_SELECTION_HANDLE == type;
+
+    // Reference to the selection handle.
+    HandleImpl& handle = mHandle[type];
+
+    // Transforms the handle position into world coordinates.
+    // @note This is not the same value as handle.actor.GetCurrentWorldPosition()
+    // as it's transforming the handle's position set by the text-controller and not
+    // the final position set to the actor. Another difference is the GetCurrentWorldPosition()
+    // retrieves the position of the center of the actor but the handle's position set
+    // by the text controller is not the center of the actor.
+    Vector2 handleWorldPosition;
+    CalculateHandleWorldCoordinates( handle, handleWorldPosition );
+
+    // Whether to flip the handle (horizontally).
+    bool flipHandle = isPrimaryHandle ? mFlipLeftSelectionHandleDirection : mFlipRightSelectionHandleDirection;
+
+    // Whether to flip the handles if they are crossed.
+    bool crossFlip = false;
+    if( mFlipSelectionHandlesOnCross || !mHandlePanning )
+    {
+      crossFlip = mHandleCurrentCrossed;
+    }
+
+    // Does not flip if both conditions are true (double flip)
+    flipHandle = flipHandle != ( crossFlip || mHandlePreviousCrossed );
+
+    // Will flip the handles vertically if the user prefers it.
+    bool verticallyFlippedPreferred = handle.verticallyFlippedPreferred;
+
+    if( crossFlip || mHandlePreviousCrossed )
+    {
+      if( isPrimaryHandle )
+      {
+        verticallyFlippedPreferred = mHandle[RIGHT_SELECTION_HANDLE].verticallyFlippedPreferred;
+      }
+      else
+      {
+        verticallyFlippedPreferred = mHandle[LEFT_SELECTION_HANDLE].verticallyFlippedPreferred;
+      }
+    }
+
+    // Check if the selection handle exceeds the boundaries of the decoration box.
+    const bool exceedsLeftEdge = ( isPrimaryHandle ? !flipHandle : flipHandle ) && ( handleWorldPosition.x - handle.size.width < mBoundingBox.x );
+    const bool exceedsRightEdge = ( isPrimaryHandle ? flipHandle : !flipHandle ) && ( handleWorldPosition.x + handle.size.width > mBoundingBox.z );
+
+    // Does not flip if both conditions are true (double flip)
+    flipHandle = flipHandle != ( exceedsLeftEdge || exceedsRightEdge );
+
+    if( flipHandle )
+    {
+      if( !handle.horizontallyFlipped )
+      {
+        // Change the anchor point to flip the image.
+        handle.actor.SetAnchorPoint( isPrimaryHandle ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
+
+        handle.horizontallyFlipped = true;
+      }
+    }
+    else
+    {
+      if( handle.horizontallyFlipped )
+      {
+        // Reset the anchor point.
+        handle.actor.SetAnchorPoint( isPrimaryHandle ? AnchorPoint::TOP_RIGHT : AnchorPoint::TOP_LEFT );
+
+        handle.horizontallyFlipped = false;
+      }
+    }
+
+    // Whether to flip the handle vertically.
+    handle.verticallyFlipped = ( verticallyFlippedPreferred &&
+                                 ( ( handleWorldPosition.y - handle.size.height ) > mBoundingBox.y ) ) ||
+                               ( handleWorldPosition.y + handle.lineHeight + handle.size.height > mBoundingBox.w );
+
+    // The primary selection handle 'y' position in local coords.
+    // If the handle exceeds the bottom of the decoration box,
+    // set the 'y' position to the top of the line.
+    // The SetHandleImage() method will change the orientation.
+    const float yLocalPosition = handle.verticallyFlipped ? handle.position.y : handle.position.y + handle.lineHeight;
+
+    handle.actor.SetPosition( handle.position.x,
+                              yLocalPosition ); // TODO : Fix for multiline.
+  }
+
+  void SetHandleImage( HandleType type )
+  {
+    HandleImpl& handle = mHandle[type];
+
+    HandleType markerType = HANDLE_TYPE_COUNT;
+    // If the selection handle is flipped it chooses the image of the other selection handle. Does nothing for the grab handle.
+    if( LEFT_SELECTION_HANDLE == type )
+    {
+      type = handle.horizontallyFlipped ? RIGHT_SELECTION_HANDLE : LEFT_SELECTION_HANDLE;
+      markerType = handle.horizontallyFlipped ? RIGHT_SELECTION_HANDLE_MARKER : LEFT_SELECTION_HANDLE_MARKER;
+    }
+    else if( RIGHT_SELECTION_HANDLE == type )
+    {
+      type = handle.horizontallyFlipped ? LEFT_SELECTION_HANDLE : RIGHT_SELECTION_HANDLE;
+      markerType = handle.horizontallyFlipped ? LEFT_SELECTION_HANDLE_MARKER : RIGHT_SELECTION_HANDLE_MARKER;
+    }
+
+    // Chooses between the released or pressed image. It checks whether the pressed image exists.
+    if( handle.actor )
+    {
+      const HandleImageType imageType = ( handle.pressed ? ( mHandleImages[type][HANDLE_IMAGE_PRESSED] ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED ) : HANDLE_IMAGE_RELEASED );
+
+      handle.actor.SetImage( mHandleImages[type][imageType] );
+    }
+
+    if( HANDLE_TYPE_COUNT != markerType )
+    {
+      if( handle.markerActor )
+      {
+        const HandleImageType markerImageType = ( handle.pressed ? ( mHandleImages[markerType][HANDLE_IMAGE_PRESSED] ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED ) : HANDLE_IMAGE_RELEASED );
+        handle.markerActor.SetImage( mHandleImages[markerType][markerImageType] );
+      }
+    }
+
+    // Whether to flip the handle vertically.
+    if( handle.actor )
+    {
+      handle.actor.SetOrientation( handle.verticallyFlipped ? ANGLE_180 : ANGLE_0, Vector3::XAXIS );
+    }
+  }
+
   void CreateHighlight()
   {
     if( !mHighlightActor )
@@ -769,7 +995,6 @@ struct Decorator::Impl : public ConnectionTracker
 
         for( std::size_t v = 0; iter != endIter; ++iter,v+=4 )
         {
-
           QuadCoordinates& quad = *iter;
 
           // top-left (v+0)
@@ -843,7 +1068,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.
+      }
     }
   }
 
@@ -892,6 +1120,8 @@ struct Decorator::Impl : public ConnectionTracker
         StopScrollTimer();
         mController.DecorationEvent( type, HANDLE_PRESSED, x, y );
       }
+
+      mHandlePanning = true;
     }
     else if( Gesture::Finished  == gesture.state ||
              Gesture::Cancelled == gesture.state )
@@ -909,22 +1139,10 @@ struct Decorator::Impl : public ConnectionTracker
         mController.DecorationEvent( type, HANDLE_RELEASED, x, y );
       }
 
-      if( GRAB_HANDLE == type )
-      {
-        handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
-      }
-      else
-      {
-        HandleType selectionHandleType = type;
-
-        if( mSwapSelectionHandles != handle.flipped )
-        {
-          selectionHandleType = ( LEFT_SELECTION_HANDLE == type ) ? RIGHT_SELECTION_HANDLE : LEFT_SELECTION_HANDLE;
-        }
-
-        handle.actor.SetImage( mHandleImages[selectionHandleType][HANDLE_IMAGE_RELEASED] );
-      }
+      handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
       handle.pressed = false;
+
+      mHandlePanning = false;
     }
   }
 
@@ -959,22 +1177,14 @@ struct Decorator::Impl : public ConnectionTracker
       if( TouchPoint::Down == point.state )
       {
         mHandle[GRAB_HANDLE].pressed = true;
-        Image imagePressed = mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED];
-        if( imagePressed )
-        {
-          mHandle[GRAB_HANDLE].actor.SetImage( imagePressed );
-        }
       }
       else if( ( TouchPoint::Up == point.state ) ||
                ( TouchPoint::Interrupted == point.state ) )
       {
         mHandle[GRAB_HANDLE].pressed = false;
-        Image imageReleased = mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED];
-        if( imageReleased )
-        {
-          mHandle[GRAB_HANDLE].actor.SetImage( imageReleased );
-        }
       }
+
+      SetHandleImage( GRAB_HANDLE );
     }
 
     // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
@@ -989,26 +1199,19 @@ struct Decorator::Impl : public ConnectionTracker
     {
       const TouchPoint& point = event.GetPoint(0);
 
-      const bool flip = mSwapSelectionHandles != mHandle[LEFT_SELECTION_HANDLE].flipped;
       if( TouchPoint::Down == point.state )
       {
         mHandle[LEFT_SELECTION_HANDLE].pressed = true;
-        Image imagePressed = mHandleImages[flip ? RIGHT_SELECTION_HANDLE : LEFT_SELECTION_HANDLE][HANDLE_IMAGE_PRESSED];
-        if( imagePressed )
-        {
-          mHandle[LEFT_SELECTION_HANDLE].actor.SetImage( imagePressed );
-        }
       }
       else if( ( TouchPoint::Up == point.state ) ||
                ( TouchPoint::Interrupted == point.state ) )
       {
         mHandle[LEFT_SELECTION_HANDLE].pressed = false;
-        Image imageReleased = mHandleImages[flip ? RIGHT_SELECTION_HANDLE : LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED];
-        if( imageReleased )
-        {
-          mHandle[LEFT_SELECTION_HANDLE].actor.SetImage( imageReleased );
-        }
+        mHandlePreviousCrossed = mHandleCurrentCrossed;
+        mHandlePanning = false;
       }
+
+      SetHandleImage( LEFT_SELECTION_HANDLE );
     }
 
     // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
@@ -1023,56 +1226,253 @@ struct Decorator::Impl : public ConnectionTracker
     {
       const TouchPoint& point = event.GetPoint(0);
 
-      const bool flip = mSwapSelectionHandles != mHandle[RIGHT_SELECTION_HANDLE].flipped;
       if( TouchPoint::Down == point.state )
       {
-        Image imagePressed = mHandleImages[flip ? LEFT_SELECTION_HANDLE : RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_PRESSED];
         mHandle[RIGHT_SELECTION_HANDLE].pressed = true;
-        if( imagePressed )
-        {
-          mHandle[RIGHT_SELECTION_HANDLE].actor.SetImage( imagePressed );
-        }
       }
       else if( ( TouchPoint::Up == point.state ) ||
                ( TouchPoint::Interrupted == point.state ) )
       {
-        Image imageReleased = mHandleImages[flip ? LEFT_SELECTION_HANDLE : RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED];
         mHandle[RIGHT_SELECTION_HANDLE].pressed = false;
-        if( imageReleased )
-        {
-          mHandle[RIGHT_SELECTION_HANDLE].actor.SetImage( imageReleased );
-        }
+        mHandlePreviousCrossed = mHandleCurrentCrossed;
+        mHandlePanning = false;
       }
+
+      SetHandleImage( RIGHT_SELECTION_HANDLE );
     }
 
     // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
     return true;
   }
 
-  // Popup
+  void HandleResetPosition( PropertyNotification& source )
+  {
+    const HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
 
-  float AlternatePopUpPositionRelativeToCursor()
+    if( grabHandle.active )
+    {
+      // Sets the grab handle position and calculates if it needs to be vertically flipped if it exceeds the boundary box.
+      SetGrabHandlePosition();
+
+      // Sets the grab handle image according if it's pressed, flipped, etc.
+      SetHandleImage( GRAB_HANDLE );
+    }
+    else
+    {
+      // Sets the primary selection handle position and calculates if it needs to be vertically flipped if it exceeds the boundary box.
+      SetSelectionHandlePosition( LEFT_SELECTION_HANDLE );
+
+      // Sets the primary handle image according if it's pressed, flipped, etc.
+      SetHandleImage( LEFT_SELECTION_HANDLE );
+
+      // Sets the secondary selection handle position and calculates if it needs to be vertically flipped if it exceeds the boundary box.
+      SetSelectionHandlePosition( RIGHT_SELECTION_HANDLE );
+
+      // Sets the secondary handle image according if it's pressed, flipped, etc.
+      SetHandleImage( RIGHT_SELECTION_HANDLE );
+    }
+  }
+
+  void SetupActiveLayerPropertyNotifications()
   {
-    float alternativePosition=0.0f;;
+    if( !mActiveLayer )
+    {
+      return;
+    }
+
+    // Vertical notifications.
+
+    // Disconnect any previous connected callback.
+    if( mVerticalLessThanNotification )
+    {
+      mVerticalLessThanNotification.NotifySignal().Disconnect( this, &Decorator::Impl::HandleResetPosition );
+      mActiveLayer.RemovePropertyNotification( mVerticalLessThanNotification );
+    }
+
+    if( mVerticalGreaterThanNotification )
+    {
+      mVerticalGreaterThanNotification.NotifySignal().Disconnect( this, &Decorator::Impl::HandleResetPosition );
+      mActiveLayer.RemovePropertyNotification( mVerticalGreaterThanNotification );
+    }
+
+    const HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+    const HandleImpl& primaryHandle = mHandle[LEFT_SELECTION_HANDLE];
+    const HandleImpl& secondaryHandle = mHandle[RIGHT_SELECTION_HANDLE];
+
+    if( grabHandle.active )
+    {
+      if( grabHandle.verticallyFlipped )
+      {
+        // The grab handle is vertically flipped. Never is going to exceed the bottom edje of the display.
+        mVerticalGreaterThanNotification.Reset();
+
+        // The vertical distance from the center of the active layer to the top edje of the display.
+        const float topHeight = 0.5f * mControlSize.height - grabHandle.position.y + grabHandle.size.height;
+
+        mVerticalLessThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                              LessThanCondition( mBoundingBox.y + topHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalLessThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalLessThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+      }
+      else
+      {
+        // The grab handle is not vertically flipped. Never is going to exceed the top edje of the display.
+        mVerticalLessThanNotification.Reset();
+
+        // The vertical distance from the center of the active layer to the bottom edje of the display.
+        const float bottomHeight = -0.5f * mControlSize.height + grabHandle.position.y + grabHandle.lineHeight + grabHandle.size.height;
+
+        mVerticalGreaterThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                                 GreaterThanCondition( mBoundingBox.w - bottomHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalGreaterThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalGreaterThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+      }
+    }
+    else // The selection handles are active
+    {
+      if( primaryHandle.verticallyFlipped && secondaryHandle.verticallyFlipped )
+      {
+        // Both selection handles are vertically flipped. Never are going to exceed the bottom edje of the display.
+        mVerticalGreaterThanNotification.Reset();
+
+        // The vertical distance from the center of the active layer to the top edje of the display.
+        const float topHeight = 0.5f * mControlSize.height + std::max( -primaryHandle.position.y + primaryHandle.size.height, -secondaryHandle.position.y + secondaryHandle.size.height );
+
+        mVerticalLessThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                              LessThanCondition( mBoundingBox.y + topHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalLessThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalLessThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+      }
+      else if( !primaryHandle.verticallyFlipped && !secondaryHandle.verticallyFlipped )
+      {
+        // Both selection handles aren't vertically flipped. Never are going to exceed the top edje of the display.
+        mVerticalLessThanNotification.Reset();
+
+        // The vertical distance from the center of the active layer to the bottom edje of the display.
+        const float bottomHeight = -0.5f * mControlSize.height + std::max( primaryHandle.position.y + primaryHandle.lineHeight + primaryHandle.size.height,
+                                                                           secondaryHandle.position.y + secondaryHandle.lineHeight + secondaryHandle.size.height );
+
+        mVerticalGreaterThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                                 GreaterThanCondition( mBoundingBox.w - bottomHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalGreaterThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalGreaterThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+      }
+      else
+      {
+        // Only one of the selection handles is vertically flipped. Both vertical notifications are needed.
 
-    if ( mPrimaryCursor ) // Secondary cursor not used for paste
+        // The vertical distance from the center of the active layer to the top edje of the display.
+        const float topHeight = 0.5f * mControlSize.height + ( primaryHandle.verticallyFlipped                              ?
+                                                               -primaryHandle.position.y + primaryHandle.size.height        :
+                                                               -secondaryHandle.position.y + secondaryHandle.size.height );
+
+        mVerticalLessThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                              LessThanCondition( mBoundingBox.y + topHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalLessThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalLessThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+
+        // The vertical distance from the center of the active layer to the bottom edje of the display.
+        const float bottomHeight = -0.5f * mControlSize.height + ( primaryHandle.verticallyFlipped                                                       ?
+                                                                   secondaryHandle.position.y + secondaryHandle.lineHeight + secondaryHandle.size.height :
+                                                                   primaryHandle.position.y + primaryHandle.lineHeight + primaryHandle.size.height );
+
+        mVerticalGreaterThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
+                                                                                 GreaterThanCondition( mBoundingBox.w - bottomHeight ) );
+
+        // Notifies the change from false to true and from true to false.
+        mVerticalGreaterThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+        // Connects the signals with the callbacks.
+        mVerticalGreaterThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+      }
+    }
+
+    // Horizontal notifications.
+
+    // Disconnect any previous connected callback.
+    if( mHorizontalLessThanNotification )
+    {
+      mHorizontalLessThanNotification.NotifySignal().Disconnect( this, &Decorator::Impl::HandleResetPosition );
+      mActiveLayer.RemovePropertyNotification( mHorizontalLessThanNotification );
+    }
+
+    if( mHorizontalGreaterThanNotification )
     {
-      Cursor cursor = PRIMARY_CURSOR;
-      alternativePosition = mCursor[cursor].position.y;
+      mHorizontalGreaterThanNotification.NotifySignal().Disconnect( this, &Decorator::Impl::HandleResetPosition );
+      mActiveLayer.RemovePropertyNotification( mHorizontalGreaterThanNotification );
+    }
+
+    if( primaryHandle.active || secondaryHandle.active )
+    {
+      // The horizontal distance from the center of the active layer to the left edje of the display.
+      const float leftWidth = 0.5f * mControlSize.width + std::max( -primaryHandle.position.x + primaryHandle.size.width,
+                                                                    -secondaryHandle.position.x + secondaryHandle.size.width );
+
+      mHorizontalLessThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_X,
+                                                                              LessThanCondition( mBoundingBox.x + leftWidth ) );
+
+      // Notifies the change from false to true and from true to false.
+      mHorizontalLessThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+      // Connects the signals with the callbacks.
+      mHorizontalLessThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
+
+      // The horizontal distance from the center of the active layer to the right edje of the display.
+      const float rightWidth = -0.5f * mControlSize.width + std::max( primaryHandle.position.x + primaryHandle.size.width,
+                                                                      secondaryHandle.position.x + secondaryHandle.size.width );
+
+      mHorizontalGreaterThanNotification = mActiveLayer.AddPropertyNotification( Actor::Property::WORLD_POSITION_X,
+                                                                                 GreaterThanCondition( mBoundingBox.z - rightWidth ) );
+
+      // Notifies the change from false to true and from true to false.
+      mHorizontalGreaterThanNotification.SetNotifyMode( PropertyNotification::NotifyOnChanged );
+
+      // Connects the signals with the callbacks.
+      mHorizontalGreaterThanNotification.NotifySignal().Connect( this, &Decorator::Impl::HandleResetPosition );
     }
+  }
+
+  // Popup
 
-    const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
+  float AlternatePopUpPositionRelativeToCursor()
+  {
+    float alternativePosition = 0.0f;
+
+    const float popupHeight = mCopyPastePopup.actor.GetRelayoutSize( Dimension::HEIGHT );
 
-    if( mHandle[GRAB_HANDLE].active )
+    const HandleImpl& primaryHandle = mHandle[LEFT_SELECTION_HANDLE];
+    const HandleImpl& secondaryHandle = mHandle[RIGHT_SELECTION_HANDLE];
+    const HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+    const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
+
+    if( primaryHandle.active || secondaryHandle.active )
     {
-      // If grab handle enabled then position pop-up below the grab handle.
-      const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
-      const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
-      alternativePosition +=  grabHandleSize.height  + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
+      const float maxHandleHeight = std::max( primaryHandle.size.height, secondaryHandle.size.height );
+      alternativePosition = 0.5f * popupHeight + cursor.lineHeight + maxHandleHeight + std::min( primaryHandle.position.y, secondaryHandle.position.y );
     }
     else
     {
-      alternativePosition += popupHeight;
+      alternativePosition = 0.5f * popupHeight + cursor.lineHeight + grabHandle.size.height + cursor.position.y;
     }
 
     return alternativePosition;
@@ -1080,7 +1480,7 @@ struct Decorator::Impl : public ConnectionTracker
 
   void PopUpLeavesVerticalBoundary( PropertyNotification& source )
   {
-    float alternativeYPosition=0.0f;
+    float alternativeYPosition = 0.0f;
     // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
     // if can't be positioned above, then position below row.
     alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
@@ -1088,38 +1488,28 @@ struct Decorator::Impl : public ConnectionTracker
     mCopyPastePopup.actor.SetY( alternativeYPosition );
   }
 
-
-  void SetUpPopupPositionNotifications( )
+  void SetUpPopupPositionNotifications()
   {
     // Note Property notifications ignore any set anchor point so conditions must allow for this.  Default is Top Left.
 
     // Exceeding vertical boundary
 
-    Vector4 worldCoordinatesBoundingBox;
-    LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
-
-    float popupHeight = mCopyPastePopup.actor.GetRelayoutSize( Dimension::HEIGHT);
+    const float popupHeight = mCopyPastePopup.actor.GetRelayoutSize( Dimension::HEIGHT );
 
     PropertyNotification verticalExceedNotification = mCopyPastePopup.actor.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
-                                                      OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight * 0.5f,
-                                                                        worldCoordinatesBoundingBox.w - popupHeight * 0.5f ) );
+                                                                                                     OutsideCondition( mBoundingBox.y + popupHeight * 0.5f,
+                                                                                                                       mBoundingBox.w - popupHeight * 0.5f ) );
 
     verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
   }
 
-  void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
+  void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, const Vector3& popupDistanceFromAnchorPoint, Actor parent, const Vector4& boundingRectangleWorld )
   {
     DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
 
     // Parent must already by added to Stage for these Get calls to work
-    Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
-    Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
-    Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition;  // Parent World position plus popup local position gives World Position
-    Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
-
-    // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
-    Vector4 boundingRectangleWorld;
-    LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
+    const Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize() * parent.GetCurrentAnchorPoint();
+    const Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition;  // Parent World position plus popup local position gives World Position
 
     // Calculate distance to move popup (in local space) so fits within the boundary
     float xOffSetToKeepWithinBounds = 0.0f;
@@ -1127,13 +1517,13 @@ struct Decorator::Impl : public ConnectionTracker
     {
       xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
     }
-    else if ( popupWorldPosition.x +  popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
+    else if( popupWorldPosition.x +  popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
     {
       xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x +  popupDistanceFromAnchorPoint.x );
     }
 
     // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
-    if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
+    if( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
     {
       requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
     }
@@ -1141,43 +1531,16 @@ struct Decorator::Impl : public ConnectionTracker
     requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
 
     // Prevent pixel mis-alignment by rounding down.
-    requiredPopupPosition.x = static_cast<int>( requiredPopupPosition.x );
-    requiredPopupPosition.y = static_cast<int>( requiredPopupPosition.y );
-
+    requiredPopupPosition.x = floor( requiredPopupPosition.x );
+    requiredPopupPosition.y = floor( requiredPopupPosition.y );
   }
 
-  void FlipSelectionHandleImages()
+  void SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
   {
-    SetupTouchEvents();
+    HandleImpl& handle = mHandle[handleType];
+    handle.size = Size( image.GetWidth(), image.GetHeight() );
 
-    CreateSelectionHandles();
-
-    HandleImpl& leftHandle = mHandle[LEFT_SELECTION_HANDLE];
-    HandleImpl& rightHandle = mHandle[RIGHT_SELECTION_HANDLE];
-
-    // If handle pressed and pressed image exists then use pressed image else stick with released image
-    const HandleImageType leftImageType = ( leftHandle.pressed && mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_PRESSED] ) ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED;
-    const HandleImageType rightImageType = ( rightHandle.pressed && mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_PRESSED] ) ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED;
-
-    const bool leftFlipped = mSwapSelectionHandles != leftHandle.flipped;
-    const bool rightFlipped = mSwapSelectionHandles != rightHandle.flipped;
-
-    leftHandle.actor.SetImage( leftFlipped ? mHandleImages[RIGHT_SELECTION_HANDLE][leftImageType] : mHandleImages[LEFT_SELECTION_HANDLE][leftImageType] );
-
-    leftHandle.actor.SetAnchorPoint( leftFlipped ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
-
-    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] );
-    }
+    mHandleImages[handleType][handleImageType] = image;
   }
 
   void SetScrollThreshold( float threshold )
@@ -1266,11 +1629,15 @@ struct Decorator::Impl : public ConnectionTracker
   Timer               mCursorBlinkTimer;          ///< Timer to signal cursor to blink
   Timer               mScrollTimer;               ///< Timer used to scroll the text when the grab handle is moved close to the edges.
 
-  Layer               mActiveLayer;               ///< Layer for active handles and alike that ensures they are above all else.
-  ImageActor          mPrimaryCursor;
-  ImageActor          mSecondaryCursor;
+  Layer                mActiveLayer;                       ///< Layer for active handles and alike that ensures they are above all else.
+  PropertyNotification mVerticalLessThanNotification;      ///< Notifies when the 'y' coord of the active layer is less than a given value.
+  PropertyNotification mVerticalGreaterThanNotification;   ///< Notifies when the 'y' coord of the active layer is grater than a given value.
+  PropertyNotification mHorizontalLessThanNotification;    ///< Notifies when the 'x' coord of the active layer is less than a given value.
+  PropertyNotification mHorizontalGreaterThanNotification; ///< Notifies when the 'x' coord of the active layer is grater than a given value.
+  ImageActor           mPrimaryCursor;
+  ImageActor           mSecondaryCursor;
 
-  Actor               mHighlightActor;        ///< Actor to display highlight
+  Actor               mHighlightActor;            ///< Actor to display highlight
   Renderer            mHighlightRenderer;
   Material            mHighlightMaterial;         ///< Material used for highlight
   Property::Map       mQuadVertexFormat;
@@ -1290,13 +1657,15 @@ struct Decorator::Impl : public ConnectionTracker
   Geometry            mQuadGeometry;
   QuadContainer       mHighlightQuadList;         ///< Sub-selections that combine to create the complete selection highlight
 
-  Rect<int>           mBoundingBox;
+  Vector4             mBoundingBox;               ///< The bounding box in world coords.
   Vector4             mHighlightColor;            ///< Color of the highlight
   Vector2             mHighlightPosition;         ///< The position of the highlight actor.
+  Vector2             mControlSize;               ///< The control's size. Set by the Relayout.
 
   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.
@@ -1304,12 +1673,19 @@ struct Decorator::Impl : public ConnectionTracker
   float               mScrollDistance;          ///< Distance the text scrolls during a scroll interval.
   int                 mTextDepth;               ///< The depth used to render the text.
 
-  bool                mActiveCopyPastePopup   : 1;
-  bool                mCursorBlinkStatus      : 1; ///< Flag to switch between blink on and blink off.
-  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.
-  bool                mNotifyEndOfScroll      : 1; ///< Whether to notify the end of the scroll.
+  bool                mActiveCopyPastePopup              : 1;
+  bool                mPopupSetNewPosition               : 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                mFlipSelectionHandlesOnCross       : 1; ///< Whether to flip the selection handles as soon as they cross.
+  bool                mFlipLeftSelectionHandleDirection  : 1; ///< Whether to flip the left selection handle image because of the character's direction.
+  bool                mFlipRightSelectionHandleDirection : 1; ///< Whether to flip the right selection handle image because of the character's direction.
+  bool                mHandlePanning                     : 1; ///< Whether any of the handles is moving.
+  bool                mHandleCurrentCrossed              : 1; ///< Whether the handles are crossed.
+  bool                mHandlePreviousCrossed             : 1; ///< Whether the handles where crossed at the last handle touch up.
+  bool                mNotifyEndOfScroll                 : 1; ///< Whether to notify the end of the scroll.
 };
 
 DecoratorPtr Decorator::New( ControllerInterface& controller,
@@ -1321,12 +1697,12 @@ DecoratorPtr Decorator::New( ControllerInterface& controller,
 
 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
 {
-  mImpl->mBoundingBox = boundingBox;
+  LocalToWorldCoordinatesBoundingBox( boundingBox, mImpl->mBoundingBox );
 }
 
-const Rect<int>& Decorator::GetBoundingBox() const
+void Decorator::GetBoundingBox( Rect<int>& boundingBox ) const
 {
-  return mImpl->mBoundingBox;
+  WorldToLocalCoordinatesBoundingBox( mImpl->mBoundingBox, boundingBox );
 }
 
 void Decorator::Relayout( const Vector2& size )
@@ -1402,6 +1778,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 )
@@ -1424,6 +1808,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 )
@@ -1432,6 +1826,11 @@ void Decorator::SetHandleActive( HandleType handleType, bool active )
 
   if( !active )
   {
+    if( ( LEFT_SELECTION_HANDLE == handleType ) || ( RIGHT_SELECTION_HANDLE == handleType ) )
+    {
+      mImpl->mHandlePreviousCrossed = false;
+    }
+
     // TODO: this is a work-around.
     // The problem is the handle actor does not receive the touch event with the Interrupt
     // state when the power button is pressed and the application goes to background.
@@ -1443,6 +1842,7 @@ void Decorator::SetHandleActive( HandleType handleType, bool active )
        imageActor.SetImage( imageReleased );
     }
   }
+
 }
 
 bool Decorator::IsHandleActive( HandleType handleType ) const
@@ -1452,7 +1852,7 @@ bool Decorator::IsHandleActive( HandleType handleType ) const
 
 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
 {
-  mImpl->mHandleImages[handleType][handleImageType] = image;
+  mImpl->SetHandleImage( handleType, handleImageType, image );
 }
 
 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
@@ -1497,11 +1897,26 @@ const Vector2& Decorator::GetPosition( HandleType handleType ) const
   return mImpl->mHandle[handleType].position;
 }
 
-void Decorator::SwapSelectionHandlesEnabled( bool enable )
+void Decorator::FlipHandleVertically( HandleType handleType, bool flip )
+{
+  mImpl->mHandle[handleType].verticallyFlippedPreferred = flip;
+}
+
+bool Decorator::IsHandleVerticallyFlipped( HandleType handleType ) const
 {
-  mImpl->mSwapSelectionHandles = enable;
+  return mImpl->mHandle[handleType].verticallyFlippedPreferred;
+}
 
-  mImpl->FlipSelectionHandleImages();
+void Decorator::FlipSelectionHandlesOnCrossEnabled( bool enable )
+{
+  mImpl->mFlipSelectionHandlesOnCross = enable;
+}
+
+void Decorator::SetSelectionHandleFlipState( bool indicesSwapped, bool left, bool right )
+{
+  mImpl->mHandleCurrentCrossed = indicesSwapped;
+  mImpl->mFlipLeftSelectionHandleDirection = left;
+  mImpl->mFlipRightSelectionHandleDirection = right;
 }
 
 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
@@ -1542,19 +1957,19 @@ bool Decorator::IsPopupActive() const
 
 void Decorator::SetEnabledPopupButtons( TextSelectionPopup::Buttons& enabledButtonsBitMask )
 {
-   mImpl->mEnabledPopupButtons = enabledButtonsBitMask;
+  mImpl->mEnabledPopupButtons = enabledButtonsBitMask;
 
-   if ( !mImpl->mCopyPastePopup.actor )
-   {
-     mImpl->mCopyPastePopup.actor = TextSelectionPopup::New( &mImpl->mTextSelectionPopupCallbackInterface );
+  if ( !mImpl->mCopyPastePopup.actor )
+  {
+    mImpl->mCopyPastePopup.actor = TextSelectionPopup::New( &mImpl->mTextSelectionPopupCallbackInterface );
 #ifdef DECORATOR_DEBUG
-     mImpl->mCopyPastePopup.actor.SetName("mCopyPastePopup");
+    mImpl->mCopyPastePopup.actor.SetName("mCopyPastePopup");
 #endif
-     mImpl->mCopyPastePopup.actor.SetAnchorPoint( AnchorPoint::CENTER );
-     mImpl->mCopyPastePopup.actor.OnRelayoutSignal().Connect( mImpl,  &Decorator::Impl::PopupRelayoutComplete  ); // Position popup after size negotiation
-   }
+    mImpl->mCopyPastePopup.actor.SetAnchorPoint( AnchorPoint::CENTER );
+    mImpl->mCopyPastePopup.actor.OnRelayoutSignal().Connect( mImpl,  &Decorator::Impl::PopupRelayoutComplete  ); // Position popup after size negotiation
+  }
 
-   mImpl->mCopyPastePopup.actor.EnableButtons( mImpl->mEnabledPopupButtons );
+  mImpl->mCopyPastePopup.actor.EnableButtons( mImpl->mEnabledPopupButtons );
 }
 
 TextSelectionPopup::Buttons& Decorator::GetEnabledPopupButtons()