TextSelection Popup to be aware of requirement to Paste and whitespace selectable
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
index a1283fb..592bc12 100644 (file)
@@ -89,9 +89,9 @@ void Controller::SetText( const std::string& text )
   {
     // If popup shown then hide it by switching to Editing state
     if( ( EventData::SELECTING == mImpl->mEventData->mState )          ||
-        ( EventData::SELECTION_CHANGED == mImpl->mEventData->mState )  ||
         ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
-        ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
+        ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
+        ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
     {
       mImpl->ChangeState( EventData::EDITING );
     }
@@ -229,7 +229,7 @@ int Controller::GetMaximumNumberOfCharacters()
   return mImpl->mMaximumNumberOfCharacters;
 }
 
-void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily, bool userDefined )
+void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
 {
   if( !mImpl->mFontDefaults )
   {
@@ -237,8 +237,8 @@ void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily, boo
   }
 
   mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
-  DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s userDefined: %s\n", defaultFontFamily.c_str(), userDefined ? "true":"false" );
-  mImpl->mUserDefinedFontFamily = userDefined;
+  DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
+  mImpl->mUserDefinedFontFamily = true;
 
   // Clear the font-specific data
   ClearFontData();
@@ -454,12 +454,12 @@ bool Controller::RemoveText( int cursorOffset, int numberOfChars )
       cursorIndex = oldCursorIndex + cursorOffset;
     }
 
-    if( (cursorIndex + numberOfChars) > currentText.Count() )
+    if( ( cursorIndex + numberOfChars ) > currentText.Count() )
     {
       numberOfChars = currentText.Count() - cursorIndex;
     }
 
-    if( (cursorIndex + numberOfChars) <= currentText.Count() )
+    if( ( cursorIndex + numberOfChars ) <= currentText.Count() )
     {
       Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
       Vector<Character>::Iterator last  = first + numberOfChars;
@@ -668,7 +668,7 @@ float Controller::GetHeightForWidth( float width )
   ProcessModifyEvents();
 
   Size layoutSize;
-  if( width != mImpl->mVisualModel->mControlSize.width )
+  if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
   {
     // Operations that can be done only once until the text changes.
     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
@@ -725,7 +725,9 @@ bool Controller::Relayout( const Size& size )
     return glyphsRemoved;
   }
 
-  if( size != mImpl->mVisualModel->mControlSize )
+  const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
+
+  if( newSize )
   {
     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
 
@@ -751,9 +753,9 @@ bool Controller::Relayout( const Size& size )
   // Do not re-do any operation until something changes.
   mImpl->mOperationsPending = NO_OPERATION;
 
-  // Keep the current offset and alignment as it will be used to update the decorator's positions.
+  // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
   Vector2 offset;
-  if( mImpl->mEventData )
+  if( newSize && mImpl->mEventData )
   {
     offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
   }
@@ -763,11 +765,14 @@ bool Controller::Relayout( const Size& size )
 
   if( mImpl->mEventData )
   {
-    // If there is a nex size, the scroll position needs to be clamped.
-    mImpl->ClampHorizontalScroll( layoutSize );
+    if( newSize )
+    {
+      // If there is a new size, the scroll position needs to be clamped.
+      mImpl->ClampHorizontalScroll( layoutSize );
 
-    // Update the decorator's positions.
-    mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
+      // Update the decorator's positions is needed if there is a new size.
+      mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
+    }
 
     // Move the cursor, grab handle etc.
     updated = mImpl->ProcessInputEvents() || updated;
@@ -783,18 +788,18 @@ void Controller::ProcessModifyEvents()
 
   for( unsigned int i=0; i<events.size(); ++i )
   {
-    if( ModifyEvent::TEXT_REPLACED == events[0].type )
+    if( ModifyEvent::TEXT_REPLACED == events[i].type )
     {
       // A (single) replace event should come first, otherwise we wasted time processing NOOP events
       DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
 
       TextReplacedEvent();
     }
-    else if( ModifyEvent::TEXT_INSERTED == events[0].type )
+    else if( ModifyEvent::TEXT_INSERTED == events[i].type )
     {
       TextInsertedEvent();
     }
-    else if( ModifyEvent::TEXT_DELETED == events[0].type )
+    else if( ModifyEvent::TEXT_DELETED == events[i].type )
     {
       // Placeholder-text cannot be deleted
       if( !mImpl->IsShowingPlaceholderText() )
@@ -839,9 +844,7 @@ void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
     mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
 
     // Update the cursor if it's in editing mode.
-    if( ( EventData::EDITING == mImpl->mEventData->mState )            ||
-        ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
-        ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
+    if ( EventData::IsEditingState( mImpl->mEventData->mState )  )
     {
       mImpl->mEventData->mUpdateCursorPosition = true;
     }
@@ -894,9 +897,7 @@ void Controller::TextInsertedEvent()
                                                            REORDER );
 
   // Queue a cursor reposition event; this must wait until after DoRelayout()
-  if( ( EventData::EDITING == mImpl->mEventData->mState )            ||
-      ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
-      ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) )
+  if ( EventData::IsEditingState( mImpl->mEventData->mState )  )
   {
     mImpl->mEventData->mUpdateCursorPosition = true;
     mImpl->mEventData->mScrollAfterUpdatePosition = true;
@@ -922,11 +923,8 @@ void Controller::TextDeletedEvent()
                                                            REORDER );
 
   // Queue a cursor reposition event; this must wait until after DoRelayout()
-  if( 0u == mImpl->mLogicalModel->mText.Count() )
-  {
-    mImpl->mEventData->mUpdateCursorPosition = true;
-  }
-  else
+  mImpl->mEventData->mUpdateCursorPosition = true;
+  if( 0u != mImpl->mLogicalModel->mText.Count() )
   {
     mImpl->mEventData->mScrollAfterDelete = true;
   }
@@ -1179,8 +1177,7 @@ void Controller::CalculateTextAlignment( const Size& size )
     }
     case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
     {
-      const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
-      mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
+      mImpl->mAlignmentOffset.x = floorf( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
       break;
     }
     case LayoutEngine::HORIZONTAL_ALIGN_END:
@@ -1200,8 +1197,7 @@ void Controller::CalculateTextAlignment( const Size& size )
     }
     case LayoutEngine::VERTICAL_ALIGN_CENTER:
     {
-      const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
-      mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
+      mImpl->mAlignmentOffset.y = floorf( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
       break;
     }
     case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
@@ -1485,8 +1481,7 @@ bool Controller::RemoveSelectedText()
 {
   bool textRemoved( false );
 
-  if ( EventData::SELECTING         == mImpl->mEventData->mState ||
-       EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
+  if( EventData::SELECTING == mImpl->mEventData->mState )
   {
     std::string removedString;
     mImpl->RetrieveSelection( removedString, true );
@@ -1507,33 +1502,48 @@ void Controller::TapEvent( unsigned int tapCount, float x, float y )
 
   if( NULL != mImpl->mEventData )
   {
+    DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
+
     if( 1u == tapCount )
     {
       // This is to avoid unnecessary relayouts when tapping an empty text-field
       bool relayoutNeeded( false );
 
-      if( mImpl->IsShowingRealText() &&
-          EventData::EDITING == mImpl->mEventData->mState )
+      if ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState || EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState )
       {
-        // Show grab handle on second tap
-        mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
-        relayoutNeeded = true;
+        mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE);  // If Popup shown hide it here so can be shown again if required.
       }
-      else if( EventData::EDITING                  != mImpl->mEventData->mState &&
-               EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
+
+      if( mImpl->IsShowingRealText() && ( EventData::INACTIVE !=  mImpl->mEventData->mState ) )
       {
-        if( mImpl->IsShowingPlaceholderText() &&  ! mImpl->IsFocusedPlaceholderAvailable() )
+        // Already in an active state so show a popup
+        if ( !mImpl->IsClipboardEmpty() )
         {
-          // Hide placeholder text
-          ResetText();
+          // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
+          mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
+        }
+        else
+        {
+          mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
         }
-        // Show cursor on first tap
-        mImpl->ChangeState( EventData::EDITING );
         relayoutNeeded = true;
       }
-      else if( mImpl->IsShowingRealText() )
+      else
       {
-        // Move the cursor
+        if( mImpl->IsShowingPlaceholderText() &&  !mImpl->IsFocusedPlaceholderAvailable() )
+        {
+          // Hide placeholder text
+          ResetText();
+        }
+
+        if ( EventData::INACTIVE == mImpl->mEventData->mState )
+        {
+          mImpl->ChangeState( EventData::EDITING );
+        }
+        else if ( !mImpl->IsClipboardEmpty() )
+        {
+          mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
+        }
         relayoutNeeded = true;
       }
 
@@ -1564,6 +1574,7 @@ void Controller::TapEvent( unsigned int tapCount, float x, float y )
 }
 
 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
+        // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
 {
   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
 
@@ -1621,16 +1632,11 @@ void Controller::LongPressEvent( Gesture::State state, float x, float y  )
 
 void Controller::SelectEvent( float x, float y, bool selectAll )
 {
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
+
   if( mImpl->mEventData )
   {
-    if ( mImpl->mEventData->mState == EventData::SELECTING )
-    {
-      mImpl->ChangeState( EventData::SELECTION_CHANGED );
-    }
-    else
-    {
-      mImpl->ChangeState( EventData::SELECTING );
-    }
+    mImpl->ChangeState( EventData::SELECTING );
 
     if( selectAll )
     {
@@ -1718,12 +1724,21 @@ void Controller::PasteText( const std::string& stringToPaste )
   InsertText( stringToPaste, Text::Controller::COMMIT );
   mImpl->ChangeState( EventData::EDITING );
   mImpl->RequestRelayout();
+
+  // Do this last since it provides callbacks into application code
+  mImpl->mControlInterface.TextChanged();
 }
 
 void Controller::PasteClipboardItemEvent()
 {
+  // Retrieve the clipboard contents first
   ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
   std::string stringToPaste( notifier.GetContent() );
+
+  // Commit the current pre-edit text; the contents of the clipboard should be appended
+  mImpl->ResetImfManager();
+
+  // Paste
   PasteText( stringToPaste );
 }
 
@@ -1806,7 +1821,7 @@ void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Butt
 
 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
 {
-  bool update( false );
+  bool update = false;
   bool requestRelayout = false;
 
   std::string text;
@@ -1817,6 +1832,7 @@ ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, cons
     case ImfManager::COMMIT:
     {
       InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
+      update=true;
       requestRelayout = true;
       break;
     }
@@ -1897,8 +1913,7 @@ bool Controller::BackspaceKeyEvent()
 
   bool removed( false );
 
-  if ( EventData::SELECTING         == mImpl->mEventData->mState ||
-       EventData::SELECTION_CHANGED == mImpl->mEventData->mState )
+  if( EventData::SELECTING == mImpl->mEventData->mState )
   {
     removed = RemoveSelectedText();
   }
@@ -1910,11 +1925,10 @@ bool Controller::BackspaceKeyEvent()
 
   if( removed )
   {
-    // This is to reset the virtual keyboard to Upper-case
-    if( 0u == mImpl->mLogicalModel->mText.Count() )
-    {
-      NotifyImfManager();
-    }
+    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
+    // Notifiy the IMF manager after text changed
+    // Automatic  Upper-case and restarting prediction on an existing word require this.
+    NotifyImfManager();
 
     if( 0u != mImpl->mLogicalModel->mText.Count() ||
         !mImpl->IsPlaceholderAvailable() )
@@ -1935,17 +1949,15 @@ void Controller::NotifyImfManager()
 {
   if( mImpl->mEventData )
   {
-    ImfManager imfManager = ImfManager::Get();
-
-    if( imfManager )
+    if( mImpl->mEventData->mImfManager )
     {
       // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
       std::string text;
       GetText( text );
-      imfManager.SetSurroundingText( text );
+      mImpl->mEventData->mImfManager.SetSurroundingText( text );
 
-      imfManager.SetCursorPosition( GetLogicalCursorPosition() );
-      imfManager.NotifyCursorPosition();
+      mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
+      mImpl->mEventData->mImfManager.NotifyCursorPosition();
     }
   }
 }