X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Ftext-controller-impl.cpp;h=f7f173969f07d4228c0e591bac434a4c0a51cdff;hp=07e647c90ff39a767e456a7da09576cb8bcb449d;hb=4bce4a632f936959d3be32a106d534459d10e88d;hpb=48ce7053cae43576126273091e0a865e5a87aea6 diff --git a/dali-toolkit/internal/text/text-controller-impl.cpp b/dali-toolkit/internal/text/text-controller-impl.cpp index 07e647c..f7f1739 100644 --- a/dali-toolkit/internal/text/text-controller-impl.cpp +++ b/dali-toolkit/internal/text/text-controller-impl.cpp @@ -101,15 +101,19 @@ EventData::EventData( DecoratorPtr decorator ) mScrollPosition(), mState( INACTIVE ), mPrimaryCursorPosition( 0u ), - mSecondaryCursorPosition( 0u ), + mLeftSelectionPosition( 0u ), + mRightSelectionPosition( 0u ), mDecoratorUpdated( false ), mCursorBlinkEnabled( true ), mGrabHandleEnabled( true ), - mGrabHandlePopupEnabled( true ), - mSelectionEnabled( true ), + mGrabHandlePopupEnabled( false ), + mSelectionEnabled( false ), mHorizontalScrollingEnabled( true ), mVerticalScrollingEnabled( false ), - mUpdateCursorPosition( false ) + mUpdateCursorPosition( false ), + mUpdateLeftSelectionPosition( false ), + mUpdateRightSelectionPosition( false ), + mScrollAfterUpdateCursorPosition( false ) {} EventData::~EventData() @@ -159,8 +163,10 @@ bool Controller::Impl::ProcessInputEvents() break; } case Event::GRAB_HANDLE_EVENT: + case Event::LEFT_SELECTION_HANDLE_EVENT: + case Event::RIGHT_SELECTION_HANDLE_EVENT: // Fall through { - OnGrabHandleEvent( *iter ); + OnHandleEvent( *iter ); break; } } @@ -170,9 +176,45 @@ bool Controller::Impl::ProcessInputEvents() // The cursor must also be repositioned after inserts into the model if( mEventData->mUpdateCursorPosition ) { + // Updates the cursor position and scrolls the text to make it visible. + UpdateCursorPosition(); + + if( mEventData->mScrollAfterUpdateCursorPosition ) + { + ScrollToMakeCursorVisible(); + mEventData->mScrollAfterUpdateCursorPosition = false; + } + + mEventData->mDecoratorUpdated = true; mEventData->mUpdateCursorPosition = false; } + else if( mEventData->mUpdateLeftSelectionPosition ) + { + UpdateSelectionHandle( LEFT_SELECTION_HANDLE ); + + if( mEventData->mScrollAfterUpdateCursorPosition ) + { + ScrollToMakeCursorVisible(); + mEventData->mScrollAfterUpdateCursorPosition = false; + } + + mEventData->mDecoratorUpdated = true; + mEventData->mUpdateLeftSelectionPosition = false; + } + else if( mEventData->mUpdateRightSelectionPosition ) + { + UpdateSelectionHandle( RIGHT_SELECTION_HANDLE ); + + if( mEventData->mScrollAfterUpdateCursorPosition ) + { + ScrollToMakeCursorVisible(); + mEventData->mScrollAfterUpdateCursorPosition = false; + } + + mEventData->mDecoratorUpdated = true; + mEventData->mUpdateRightSelectionPosition = false; + } mEventData->mEventQueue.clear(); @@ -230,7 +272,8 @@ void Controller::Impl::OnCursorKeyEvent( const Event& event ) // TODO } - UpdateCursorPosition(); + mEventData->mUpdateCursorPosition = true; + mEventData->mScrollAfterUpdateCursorPosition = true; } void Controller::Impl::HandleCursorKey( int keyCode ) @@ -263,7 +306,8 @@ void Controller::Impl::OnTapEvent( const Event& event ) mEventData->mPrimaryCursorPosition = GetClosestCursorIndex( xPosition, yPosition ); - UpdateCursorPosition(); + mEventData->mUpdateCursorPosition = true; + mEventData->mScrollAfterUpdateCursorPosition = true; } else if( mEventData->mSelectionEnabled && ( 2u == tapCount ) ) @@ -295,19 +339,7 @@ void Controller::Impl::OnPanEvent( const Event& event ) const float displacementX = event.p2.mFloat; mEventData->mScrollPosition.x += displacementX; - // Clamp between -space & 0 (and the text alignment). - if( actualSize.width > mControlSize.width ) - { - const float space = ( actualSize.width - mControlSize.width ) + mAlignmentOffset.x; - mEventData->mScrollPosition.x = ( mEventData->mScrollPosition.x < -space ) ? -space : mEventData->mScrollPosition.x; - mEventData->mScrollPosition.x = ( mEventData->mScrollPosition.x > -mAlignmentOffset.x ) ? -mAlignmentOffset.x : mEventData->mScrollPosition.x; - - mEventData->mDecoratorUpdated = true; - } - else - { - mEventData->mScrollPosition.x = 0.f; - } + ClampHorizontalScroll( actualSize ); } if( mEventData->mVerticalScrollingEnabled ) @@ -315,19 +347,7 @@ void Controller::Impl::OnPanEvent( const Event& event ) const float displacementY = event.p3.mFloat; mEventData->mScrollPosition.y += displacementY; - // Clamp between -space & 0 (and the text alignment). - if( actualSize.height > mControlSize.height ) - { - const float space = ( actualSize.height - mControlSize.height ) + mAlignmentOffset.y; - mEventData->mScrollPosition.y = ( mEventData->mScrollPosition.y < -space ) ? -space : mEventData->mScrollPosition.y; - mEventData->mScrollPosition.y = ( mEventData->mScrollPosition.y > -mAlignmentOffset.y ) ? -mAlignmentOffset.y : mEventData->mScrollPosition.y; - - mEventData->mDecoratorUpdated = true; - } - else - { - mEventData->mScrollPosition.y = 0.f; - } + ClampVerticalScroll( actualSize ); } if( mEventData->mDecorator ) @@ -337,7 +357,7 @@ void Controller::Impl::OnPanEvent( const Event& event ) } } -void Controller::Impl::OnGrabHandleEvent( const Event& event ) +void Controller::Impl::OnHandleEvent( const Event& event ) { if( NULL == mEventData ) { @@ -345,28 +365,78 @@ void Controller::Impl::OnGrabHandleEvent( const Event& event ) return; } - unsigned int state = event.p1.mUint; + const unsigned int state = event.p1.mUint; - if( GRAB_HANDLE_PRESSED == state ) + if( HANDLE_PRESSED == state ) { // The event.p2 and event.p3 are in decorator coords. Need to transforms to text coords. const float xPosition = event.p2.mFloat - mEventData->mScrollPosition.x - mAlignmentOffset.x; const float yPosition = event.p3.mFloat - mEventData->mScrollPosition.y - mAlignmentOffset.y; - mEventData->mPrimaryCursorPosition = GetClosestCursorIndex( xPosition, yPosition ); + const CharacterIndex handleNewPosition = GetClosestCursorIndex( xPosition, yPosition ); - UpdateCursorPosition(); + if( Event::GRAB_HANDLE_EVENT == event.type ) + { + ChangeState ( EventData::EDITING ); - //mDecorator->HidePopup(); - ChangeState ( EventData::EDITING ); + if( handleNewPosition != mEventData->mPrimaryCursorPosition ) + { + mEventData->mPrimaryCursorPosition = handleNewPosition; + mEventData->mUpdateCursorPosition = true; + } + } + else if( Event::LEFT_SELECTION_HANDLE_EVENT == event.type ) + { + if( handleNewPosition != mEventData->mLeftSelectionPosition ) + { + mEventData->mLeftSelectionPosition = handleNewPosition; + mEventData->mUpdateLeftSelectionPosition = true; + } + } + else if( Event::RIGHT_SELECTION_HANDLE_EVENT == event.type ) + { + if( handleNewPosition != mEventData->mRightSelectionPosition ) + { + mEventData->mRightSelectionPosition = handleNewPosition; + mEventData->mUpdateRightSelectionPosition = true; + } + } } - else if( mEventData->mGrabHandlePopupEnabled && - ( GRAB_HANDLE_RELEASED == state ) ) + else if( ( HANDLE_RELEASED == state ) || + ( HANDLE_STOP_SCROLLING == state ) ) { - //mDecorator->ShowPopup(); - ChangeState ( EventData::EDITING_WITH_POPUP ); + if( mEventData->mGrabHandlePopupEnabled ) + { + ChangeState( EventData::EDITING_WITH_POPUP ); + } + if( Event::GRAB_HANDLE_EVENT == event.type ) + { + mEventData->mUpdateCursorPosition = true; + + if( HANDLE_STOP_SCROLLING == state ) + { + // The event.p2 and event.p3 are in decorator coords. Need to transforms to text coords. + const float xPosition = event.p2.mFloat - mEventData->mScrollPosition.x - mAlignmentOffset.x; + const float yPosition = event.p3.mFloat - mEventData->mScrollPosition.y - mAlignmentOffset.y; + + mEventData->mPrimaryCursorPosition = GetClosestCursorIndex( xPosition, yPosition ); + + mEventData->mScrollAfterUpdateCursorPosition = true; + } + } mEventData->mDecoratorUpdated = true; } + else if( HANDLE_SCROLLING == state ) + { + const float xSpeed = event.p2.mFloat; + const Vector2& actualSize = mVisualModel->GetActualSize(); + + mEventData->mScrollPosition.x += xSpeed; + + ClampHorizontalScroll( actualSize ); + + mEventData->mDecoratorUpdated = true; + } } void Controller::Impl::RepositionSelectionHandles( float visualX, float visualY ) @@ -397,8 +467,8 @@ void Controller::Impl::RepositionSelectionHandles( float visualX, float visualY const Vector& lines = mVisualModel->mLines; float height = lines.Count() ? lines[0].ascender + -lines[0].descender : 0.0f; - mEventData->mDecorator->SetPosition( PRIMARY_SELECTION_HANDLE, primaryX, mEventData->mScrollPosition.y, height ); - mEventData->mDecorator->SetPosition( SECONDARY_SELECTION_HANDLE, secondaryX, mEventData->mScrollPosition.y, height ); + mEventData->mDecorator->SetPosition( LEFT_SELECTION_HANDLE, primaryX, mEventData->mScrollPosition.y, height ); + mEventData->mDecorator->SetPosition( RIGHT_SELECTION_HANDLE, secondaryX, mEventData->mScrollPosition.y, height ); mEventData->mDecorator->ClearHighlights(); mEventData->mDecorator->AddHighlight( primaryX, mEventData->mScrollPosition.y, secondaryX, height + mEventData->mScrollPosition.y ); @@ -421,8 +491,9 @@ void Controller::Impl::ChangeState( EventData::State newState ) { mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE ); mEventData->mDecorator->StopCursorBlink(); - mEventData->mDecorator->SetGrabHandleActive( false ); - mEventData->mDecorator->SetSelectionActive( false ); + mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false ); + mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false ); + mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false ); mEventData->mDecorator->SetPopupActive( false ); mEventData->mDecoratorUpdated = true; } @@ -430,8 +501,9 @@ void Controller::Impl::ChangeState( EventData::State newState ) { mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_NONE ); mEventData->mDecorator->StopCursorBlink(); - mEventData->mDecorator->SetGrabHandleActive( false ); - mEventData->mDecorator->SetSelectionActive( true ); + mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false ); + mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, true ); + mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, true ); mEventData->mDecoratorUpdated = true; } else if( EventData::EDITING == mEventData->mState ) @@ -443,13 +515,14 @@ void Controller::Impl::ChangeState( EventData::State newState ) } if( mEventData->mGrabHandleEnabled ) { - mEventData->mDecorator->SetGrabHandleActive( true ); + mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true ); } if( mEventData->mGrabHandlePopupEnabled ) { mEventData->mDecorator->SetPopupActive( false ); } - mEventData->mDecorator->SetSelectionActive( false ); + mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false ); + mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false ); mEventData->mDecoratorUpdated = true; } else if( EventData::EDITING_WITH_POPUP == mEventData->mState ) @@ -461,13 +534,15 @@ void Controller::Impl::ChangeState( EventData::State newState ) } if( mEventData->mGrabHandleEnabled ) { - mEventData->mDecorator->SetGrabHandleActive( true ); + mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, true ); + mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, true ); } if( mEventData->mGrabHandlePopupEnabled ) { mEventData->mDecorator->SetPopupActive( true ); } - mEventData->mDecorator->SetSelectionActive( false ); + mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false ); + mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false ); mEventData->mDecoratorUpdated = true; } } @@ -827,18 +902,28 @@ void Controller::Impl::UpdateCursorPosition() GetCursorPosition( mEventData->mPrimaryCursorPosition, cursorInfo ); + const Vector2 offset = mEventData->mScrollPosition + mAlignmentOffset; + const Vector2 cursorPosition = cursorInfo.primaryPosition + offset; + + // Sets the cursor position. mEventData->mDecorator->SetPosition( PRIMARY_CURSOR, - cursorInfo.primaryPosition.x + mEventData->mScrollPosition.x + mAlignmentOffset.x, - cursorInfo.primaryPosition.y + mEventData->mScrollPosition.y + mAlignmentOffset.y, + cursorPosition.x, + cursorPosition.y, cursorInfo.primaryCursorHeight, cursorInfo.lineHeight ); + // Sets the grab handle position. + mEventData->mDecorator->SetPosition( GRAB_HANDLE, + cursorPosition.x, + cursorPosition.y, + cursorInfo.lineHeight ); + if( cursorInfo.isSecondaryCursor ) { mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_BOTH ); mEventData->mDecorator->SetPosition( SECONDARY_CURSOR, - cursorInfo.secondaryPosition.x + mEventData->mScrollPosition.x + mAlignmentOffset.x, - cursorInfo.secondaryPosition.y + mEventData->mScrollPosition.y + mAlignmentOffset.y, + cursorInfo.secondaryPosition.x + offset.x, + cursorInfo.secondaryPosition.y + offset.y, cursorInfo.secondaryCursorHeight, cursorInfo.lineHeight ); } @@ -846,9 +931,98 @@ void Controller::Impl::UpdateCursorPosition() { mEventData->mDecorator->SetActiveCursor( ACTIVE_CURSOR_PRIMARY ); } +} + +void Controller::Impl::UpdateSelectionHandle( HandleType handleType ) +{ + if( ( LEFT_SELECTION_HANDLE != handleType ) && + ( RIGHT_SELECTION_HANDLE != handleType ) ) + { + return; + } + + const bool leftSelectionHandle = LEFT_SELECTION_HANDLE == handleType; + const CharacterIndex index = leftSelectionHandle ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition; + + CursorInfo cursorInfo; + GetCursorPosition( index, + cursorInfo ); + + const Vector2 offset = mEventData->mScrollPosition + mAlignmentOffset; + const Vector2 cursorPosition = cursorInfo.primaryPosition + offset; + + // Sets the grab handle position. + mEventData->mDecorator->SetPosition( handleType, + cursorPosition.x, + cursorPosition.y, + cursorInfo.lineHeight ); +} + +void Controller::Impl::ClampHorizontalScroll( const Vector2& actualSize ) +{ + // Clamp between -space & 0 (and the text alignment). + if( actualSize.width > mControlSize.width ) + { + const float space = ( actualSize.width - mControlSize.width ) + mAlignmentOffset.x; + mEventData->mScrollPosition.x = ( mEventData->mScrollPosition.x < -space ) ? -space : mEventData->mScrollPosition.x; + mEventData->mScrollPosition.x = ( mEventData->mScrollPosition.x > -mAlignmentOffset.x ) ? -mAlignmentOffset.x : mEventData->mScrollPosition.x; + + mEventData->mDecoratorUpdated = true; + } + else + { + mEventData->mScrollPosition.x = 0.f; + } +} + +void Controller::Impl::ClampVerticalScroll( const Vector2& actualSize ) +{ + // Clamp between -space & 0 (and the text alignment). + if( actualSize.height > mControlSize.height ) + { + const float space = ( actualSize.height - mControlSize.height ) + mAlignmentOffset.y; + mEventData->mScrollPosition.y = ( mEventData->mScrollPosition.y < -space ) ? -space : mEventData->mScrollPosition.y; + mEventData->mScrollPosition.y = ( mEventData->mScrollPosition.y > -mAlignmentOffset.y ) ? -mAlignmentOffset.y : mEventData->mScrollPosition.y; + + mEventData->mDecoratorUpdated = true; + } + else + { + mEventData->mScrollPosition.y = 0.f; + } +} + +void Controller::Impl::ScrollToMakeCursorVisible() +{ + if( NULL == mEventData ) + { + // Nothing to do if there is no text input. + return; + } + + const Vector2& primaryCursorPosition = mEventData->mDecorator->GetPosition( PRIMARY_CURSOR ); + + Vector2 offset; + bool updateDecorator = false; + if( primaryCursorPosition.x < 0.f ) + { + offset.x = -primaryCursorPosition.x; + mEventData->mScrollPosition.x += offset.x; + updateDecorator = true; + } + else if( primaryCursorPosition.x > mControlSize.width ) + { + offset.x = mControlSize.width - primaryCursorPosition.x; + mEventData->mScrollPosition.x += offset.x; + updateDecorator = true; + } + + if( updateDecorator && mEventData->mDecorator ) + { + mEventData->mDecorator->UpdatePositions( offset ); + } - mEventData->mUpdateCursorPosition = false; - mEventData->mDecoratorUpdated = true; + // TODO : calculate the vertical scroll. } void Controller::Impl::RequestRelayout()