+ 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.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION );
+
+ // 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 + ( mSmoothHandlePanEnabled ? handle.grabDisplacementX : 0.f );
+ position.y = parentWorldPosition.y - 0.5f * mControlSize.height + handle.position.y + ( mSmoothHandlePanEnabled ? handle.grabDisplacementY : 0.f );
+ }
+
+ 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.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION )
+ // 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.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION )
+ // 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;
+
+ if( grabHandle.actor )
+ {
+ grabHandle.actor.SetProperty( Actor::Property::POSITION, Vector2( grabHandle.position.x + floor( 0.5f * mCursorWidth ) + ( mSmoothHandlePanEnabled ? grabHandle.grabDisplacementX : 0.f ),
+ yLocalPosition + ( mSmoothHandlePanEnabled ? grabHandle.grabDisplacementY : 0.f ) ) );
+ }
+ }
+
+ 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.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION )
+ // 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.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION )
+ // 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 || !mIsHandlePanning )
+ {
+ crossFlip = mIsHandleCurrentlyCrossed;
+ }
+
+ // Whether the handle was crossed before start the panning.
+ const bool isHandlePreviouslyCrossed = mFlipSelectionHandlesOnCross ? false : mIsHandlePreviouslyCrossed;
+
+ // Does not flip if both conditions are true (double flip)
+ flipHandle = flipHandle != ( crossFlip || isHandlePreviouslyCrossed );
+
+ // Will flip the handles vertically if the user prefers it.
+ bool verticallyFlippedPreferred = handle.verticallyFlippedPreferred;
+
+ if( crossFlip || isHandlePreviouslyCrossed )
+ {
+ 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.actor && !handle.horizontallyFlipped )
+ {
+ // Change the anchor point to flip the image.
+ handle.actor.SetProperty( Actor::Property::ANCHOR_POINT, isPrimaryHandle ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
+
+ handle.horizontallyFlipped = true;
+ }
+ }
+ else
+ {
+ if( handle.actor && handle.horizontallyFlipped )
+ {
+ // Reset the anchor point.
+ handle.actor.SetProperty( Actor::Property::ANCHOR_POINT, 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;
+
+ if( handle.actor )
+ {
+ handle.actor.SetProperty( Actor::Property::POSITION, Vector2( handle.position.x + ( mSmoothHandlePanEnabled ? handle.grabDisplacementX : 0.f ),
+ yLocalPosition + ( mSmoothHandlePanEnabled ? handle.grabDisplacementY : 0.f ) ) );
+ }
+ }
+
+ 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].size() ? 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].size() ? 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.SetProperty( Actor::Property::ORIENTATION, Quaternion( handle.verticallyFlipped ? ANGLE_180 : ANGLE_0, Vector3::XAXIS ) );
+ }
+ }
+
+ void CreateHighlight()
+ {
+ if( !mHighlightActor )
+ {
+ mHighlightActor = Actor::New();
+
+ mHighlightActor.SetProperty( Dali::Actor::Property::NAME, "HighlightActor" );
+ mHighlightActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
+ mHighlightActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
+ mHighlightActor.SetProperty( Actor::Property::COLOR, mHighlightColor );
+ mHighlightActor.SetProperty( Actor::Property::COLOR_MODE, USE_OWN_COLOR );
+ }
+
+ // Add the highlight box telling the controller it needs clipping.
+ mController.AddDecoration( mHighlightActor, true );
+ }
+
+ void UpdateHighlight()
+ {
+ if ( mHighlightActor )
+ {
+ // Sets the position of the highlight actor inside the decorator.
+ mHighlightActor.SetProperty( Actor::Property::POSITION, Vector2( mHighlightPosition.x + mHighlightOutlineOffset,
+ mHighlightPosition.y + mHighlightOutlineOffset ) );
+
+ const unsigned int numberOfQuads = mHighlightQuadList.Count();
+ if( 0u != numberOfQuads )
+ {
+ // Set the size of the highlighted text to the actor.
+ mHighlightActor.SetProperty( Actor::Property::SIZE, mHighlightSize );
+
+ // Used to translate the vertices given in decorator's coords to the mHighlightActor's local coords.
+ const float offsetX = mHighlightPosition.x + 0.5f * mHighlightSize.width;
+ const float offsetY = mHighlightPosition.y + 0.5f * mHighlightSize.height;
+
+ Vector<Vector2> vertices;
+ Vector<unsigned short> indices;
+
+ vertices.Reserve( 4u * numberOfQuads );
+ indices.Reserve( 6u * numberOfQuads );
+
+ // Index to the vertex.
+ unsigned int v = 0u;
+
+ // Traverse all quads.
+ for( Vector<Vector4>::ConstIterator it = mHighlightQuadList.Begin(),
+ endIt = mHighlightQuadList.End();
+ it != endIt;
+ ++it, v += 4u )
+ {
+ const Vector4& quad = *it;
+
+ Vector2 vertex;
+
+ // top-left (v+0)
+ vertex.x = quad.x - offsetX;
+ vertex.y = quad.y - offsetY;
+ vertices.PushBack( vertex );
+
+ // top-right (v+1)
+ vertex.x = quad.z - offsetX;
+ vertex.y = quad.y - offsetY;
+ vertices.PushBack( vertex );
+
+ // bottom-left (v+2)
+ vertex.x = quad.x - offsetX;
+ vertex.y = quad.w - offsetY;
+ vertices.PushBack( vertex );
+
+ // bottom-right (v+3)
+ vertex.x = quad.z - offsetX;
+ vertex.y = quad.w - offsetY;
+ vertices.PushBack( vertex );
+
+ // triangle A (3, 1, 0)
+ indices.PushBack( v + 3 );
+ indices.PushBack( v + 1 );
+ indices.PushBack( v );
+
+ // triangle B (0, 2, 3)
+ indices.PushBack( v );
+ indices.PushBack( v + 2 );
+ indices.PushBack( v + 3 );
+ }
+
+ if( ! mQuadVertices )
+ {
+ mQuadVertices = PropertyBuffer::New( mQuadVertexFormat );
+ }
+
+ mQuadVertices.SetData( &vertices[ 0 ], vertices.Size() );
+
+ if( !mQuadGeometry )
+ {
+ mQuadGeometry = Geometry::New();
+ mQuadGeometry.AddVertexBuffer( mQuadVertices );
+ }
+ mQuadGeometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
+
+ if( !mHighlightRenderer )
+ {
+ mHighlightRenderer = Dali::Renderer::New( mQuadGeometry, mHighlightShader );
+ mHighlightActor.AddRenderer( mHighlightRenderer );
+ }
+ }
+
+ mHighlightQuadList.Clear();
+
+ if( mHighlightRenderer )
+ {
+ mHighlightRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mTextDepth - 2 ); // text is rendered at mTextDepth and text's shadow at mTextDepth -1u.
+ }
+ }
+ }
+
+ void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
+ {
+ if( Gesture::Started == gesture.state )
+ {
+ handle.grabDisplacementX = handle.grabDisplacementY = 0.f;
+
+ handle.globalPosition.x = handle.position.x;
+ handle.globalPosition.y = handle.position.y;
+ }
+
+ handle.grabDisplacementX += gesture.displacement.x;
+ handle.grabDisplacementY += ( handle.verticallyFlipped ? -gesture.displacement.y : gesture.displacement.y );
+
+ const float x = handle.globalPosition.x + handle.grabDisplacementX;
+ const float y = handle.globalPosition.y + handle.grabDisplacementY + 0.5f * handle.lineHeight;
+ const float yVerticallyFlippedCorrected = y - ( handle.verticallyFlippedOnTouch ? handle.lineHeight : 0.f );
+
+ if( ( Gesture::Started == gesture.state ) ||
+ ( Gesture::Continuing == gesture.state ) )
+ {
+ Vector2 targetSize;
+ mController.GetTargetSize( targetSize );
+
+ if( mHorizontalScrollingEnabled &&
+ ( x < mScrollThreshold ) )
+ {
+ mScrollDirection = SCROLL_RIGHT;
+ mHandleScrolling = type;
+ StartScrollTimer();
+ }
+ else if( mHorizontalScrollingEnabled &&
+ ( x > targetSize.width - mScrollThreshold ) )
+ {
+ mScrollDirection = SCROLL_LEFT;
+ mHandleScrolling = type;
+ StartScrollTimer();
+ }
+ else if( mVerticalScrollingEnabled &&
+ ( yVerticallyFlippedCorrected < mScrollThreshold ) )
+ {
+ mScrollDirection = SCROLL_TOP;
+ mHandleScrolling = type;
+ StartScrollTimer();
+ }
+ else if( mVerticalScrollingEnabled &&
+ ( yVerticallyFlippedCorrected + handle.lineHeight > targetSize.height - mScrollThreshold ) )
+ {
+ mScrollDirection = SCROLL_BOTTOM;
+ mHandleScrolling = type;
+ StartScrollTimer();
+ }
+ else
+ {
+ mHandleScrolling = HANDLE_TYPE_COUNT;
+ StopScrollTimer();
+ mController.DecorationEvent( type, HANDLE_PRESSED, x, y );
+ }
+
+ mIsHandlePanning = true;
+ }
+ else if( ( Gesture::Finished == gesture.state ) ||
+ ( Gesture::Cancelled == gesture.state ) )
+ {
+ if( mScrollTimer &&
+ ( mScrollTimer.IsRunning() || mNotifyEndOfScroll ) )
+ {
+ mNotifyEndOfScroll = false;
+ mHandleScrolling = HANDLE_TYPE_COUNT;
+ StopScrollTimer();
+ mController.DecorationEvent( type, HANDLE_STOP_SCROLLING, x, y );
+ }
+ else
+ {
+ mController.DecorationEvent( type, HANDLE_RELEASED, x, y );
+ }
+
+ if( handle.actor )
+ {
+ handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
+ }
+ handle.pressed = false;
+
+ mIsHandlePanning = false;
+ }
+ }
+
+ void OnPan( Actor actor, const PanGesture& gesture )
+ {
+ HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+ HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
+ HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
+
+ if( actor == grabHandle.grabArea )
+ {
+ DoPan( grabHandle, GRAB_HANDLE, gesture );
+ }
+ else if( actor == primarySelectionHandle.grabArea )
+ {
+ DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
+ }
+ else if( actor == secondarySelectionHandle.grabArea )
+ {
+ DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
+ }
+ }
+
+ bool OnGrabHandleTouched( Actor actor, const TouchData& touch )
+ {
+ HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+
+ // Switch between pressed/release grab-handle images
+ if( touch.GetPointCount() > 0 &&
+ grabHandle.actor )
+ {
+ const PointState::Type state = touch.GetState( 0 );
+
+ if( PointState::DOWN == state )
+ {
+ grabHandle.pressed = true;
+ }
+ else if( ( PointState::UP == state ) ||
+ ( PointState::INTERRUPTED == state ) )
+ {
+ grabHandle.pressed = false;
+ }
+
+ SetHandleImage( GRAB_HANDLE );
+ }
+
+ // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
+ return true;
+ }
+
+ bool OnHandleOneTouched( Actor actor, const TouchData& touch )
+ {
+ HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
+
+ // Switch between pressed/release selection handle images
+ if( touch.GetPointCount() > 0 &&
+ primarySelectionHandle.actor )
+ {
+ const PointState::Type state = touch.GetState( 0 );
+
+ if( PointState::DOWN == state )
+ {
+ primarySelectionHandle.pressed = true;
+ primarySelectionHandle.verticallyFlippedOnTouch = primarySelectionHandle.verticallyFlipped;
+ }
+ else if( ( PointState::UP == state ) ||
+ ( PointState::INTERRUPTED == state ) )
+ {
+ primarySelectionHandle.pressed = false;
+ mIsHandlePreviouslyCrossed = mIsHandleCurrentlyCrossed;
+ mIsHandlePanning = false;
+ mHandleReleased = LEFT_SELECTION_HANDLE;
+ }
+
+ SetHandleImage( LEFT_SELECTION_HANDLE );
+ }
+
+ // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
+ return true;
+ }
+
+ bool OnHandleTwoTouched( Actor actor, const TouchData& touch )
+ {
+ HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
+
+ // Switch between pressed/release selection handle images
+ if( touch.GetPointCount() > 0 &&
+ secondarySelectionHandle.actor )
+ {
+ const PointState::Type state = touch.GetState( 0 );
+
+ if( PointState::DOWN == state )
+ {
+ secondarySelectionHandle.pressed = true;
+ secondarySelectionHandle.verticallyFlippedOnTouch = secondarySelectionHandle.verticallyFlipped;
+ }
+ else if( ( PointState::UP == state ) ||
+ ( PointState::INTERRUPTED == state ) )
+ {
+ secondarySelectionHandle.pressed = false;
+ mIsHandlePreviouslyCrossed = mIsHandleCurrentlyCrossed;
+ mIsHandlePanning = false;
+ mHandleReleased = RIGHT_SELECTION_HANDLE;
+ }
+
+ SetHandleImage( RIGHT_SELECTION_HANDLE );
+ }
+
+ // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
+ return true;
+ }
+
+ void HandleResetPosition( PropertyNotification& source )