Markup processor.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
index eddde06..27f5cbf 100644 (file)
@@ -28,6 +28,7 @@
 #include <dali-toolkit/internal/text/bidirectional-support.h>
 #include <dali-toolkit/internal/text/character-set-conversion.h>
 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
+#include <dali-toolkit/internal/text/markup-processor.h>
 #include <dali-toolkit/internal/text/text-controller-impl.h>
 
 namespace
@@ -73,6 +74,16 @@ void Controller::EnableTextInput( DecoratorPtr decorator )
   }
 }
 
+void Controller::SetMarkupProcessorEnabled( bool enable )
+{
+  mImpl->mMarkupProcessorEnabled = enable;
+}
+
+bool Controller::IsMarkupProcessorEnabled() const
+{
+  return mImpl->mMarkupProcessorEnabled;
+}
+
 void Controller::SetText( const std::string& text )
 {
   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
@@ -90,7 +101,8 @@ 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::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 );
     }
@@ -98,20 +110,37 @@ void Controller::SetText( const std::string& text )
 
   if( !text.empty() )
   {
+    MarkupProcessData markupProcessData;
+
+    Length textSize = 0u;
+    const uint8_t* utf8 = NULL;
+    if( mImpl->mMarkupProcessorEnabled )
+    {
+      ProcessMarkupString( text, markupProcessData );
+      textSize = markupProcessData.markupProcessedText.size();
+
+      // This is a bit horrible but std::string returns a (signed) char*
+      utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
+    }
+    else
+    {
+      textSize = text.size();
+
+      // This is a bit horrible but std::string returns a (signed) char*
+      utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
+    }
+
     //  Convert text into UTF-32
     Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
-    utf32Characters.Resize( text.size() );
-
-    // This is a bit horrible but std::string returns a (signed) char*
-    const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
+    utf32Characters.Resize( textSize );
 
     // Transform a text array encoded in utf8 into an array encoded in utf32.
     // It returns the actual number of characters.
-    Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
+    Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
     utf32Characters.Resize( characterCount );
 
-    DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
-    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
+    DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
+    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
 
     // To reset the cursor position
     lastCursorIndex = characterCount;
@@ -667,7 +696,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  |
@@ -783,22 +812,33 @@ bool Controller::Relayout( const Size& size )
 
 void Controller::ProcessModifyEvents()
 {
-  std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
+  Vector<ModifyEvent>& events = mImpl->mModifyEvents;
+
+  if( 0u == events.Count() )
+  {
+    // Nothing to do.
+    return;
+  }
 
-  for( unsigned int i=0; i<events.size(); ++i )
+  for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
+         endIt = events.End();
+       it != endIt;
+       ++it )
   {
-    if( ModifyEvent::TEXT_REPLACED == events[i].type )
+    const ModifyEvent& event = *it;
+
+    if( ModifyEvent::TEXT_REPLACED == event.type )
     {
       // A (single) replace event should come first, otherwise we wasted time processing NOOP events
-      DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
+      DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
 
       TextReplacedEvent();
     }
-    else if( ModifyEvent::TEXT_INSERTED == events[i].type )
+    else if( ModifyEvent::TEXT_INSERTED == event.type )
     {
       TextInsertedEvent();
     }
-    else if( ModifyEvent::TEXT_DELETED == events[i].type )
+    else if( ModifyEvent::TEXT_DELETED == event.type )
     {
       // Placeholder-text cannot be deleted
       if( !mImpl->IsShowingPlaceholderText() )
@@ -808,15 +848,14 @@ void Controller::ProcessModifyEvents()
     }
   }
 
-  if( mImpl->mEventData &&
-      0 != events.size() )
+  if( mImpl->mEventData )
   {
     // When the text is being modified, delay cursor blinking
     mImpl->mEventData->mDecorator->DelayCursorBlink();
   }
 
   // Discard temporary text
-  events.clear();
+  events.Clear();
 }
 
 void Controller::ResetText()
@@ -843,9 +882,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;
     }
@@ -898,9 +935,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;
@@ -1435,9 +1470,10 @@ void Controller::InsertText( const std::string& text, Controller::InsertType typ
     Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
     maxLengthReached = ( characterCount > maxSizeOfNewText );
 
-    // Insert at current cursor position
+    // The cursor position.
     CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
 
+    // Insert at current cursor position.
     Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
 
     if( cursorIndex < numberOfCharactersInModel )
@@ -1505,33 +1541,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 )
+      {
+        mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE);  // If Popup shown hide it here so can be shown again if required.
+      }
+
+      if( mImpl->IsShowingRealText() && ( EventData::INACTIVE !=  mImpl->mEventData->mState ) )
       {
-        // Show grab handle on second tap
-        mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
+        // Already in an active state so show a popup
+        if ( !mImpl->IsClipboardEmpty() )
+        {
+          // 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 );
+        }
         relayoutNeeded = true;
       }
-      else if( EventData::EDITING                  != mImpl->mEventData->mState &&
-               EventData::EDITING_WITH_GRAB_HANDLE != mImpl->mEventData->mState )
+      else
       {
         if( mImpl->IsShowingPlaceholderText() &&  !mImpl->IsFocusedPlaceholderAvailable() )
         {
           // Hide placeholder text
           ResetText();
         }
-        // Show cursor on first tap
-        mImpl->ChangeState( EventData::EDITING );
-        relayoutNeeded = true;
-      }
-      else if( mImpl->IsShowingRealText() )
-      {
-        // Move the cursor
+
+        if ( EventData::INACTIVE == mImpl->mEventData->mState )
+        {
+          mImpl->ChangeState( EventData::EDITING );
+        }
+        else if ( !mImpl->IsClipboardEmpty() )
+        {
+          mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
+        }
         relayoutNeeded = true;
       }
 
@@ -1562,6 +1613,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" );
 
@@ -1619,6 +1671,8 @@ 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 )
   {
     mImpl->ChangeState( EventData::SELECTING );