Fix for cursor position when there is no text and no place holder text.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
index 32e3ed3..68555b7 100644 (file)
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <dali/public-api/adaptor-framework/key.h>
 #include <dali/integration-api/debug.h>
+#include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/bidirectional-support.h>
@@ -1015,7 +1016,12 @@ void Controller::CalculateTextAlignment( const Size& size )
   // Get the direction of the first character.
   const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
 
-  const Size& actualSize = mImpl->mVisualModel->GetActualSize();
+  Size actualSize = mImpl->mVisualModel->GetActualSize();
+  if( fabsf( actualSize.height ) < Math::MACHINE_EPSILON_1000 )
+  {
+    // Get the line height of the default font.
+    actualSize.height = mImpl->GetDefaultFontLineHeight();
+  }
 
   // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
   LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
@@ -1108,16 +1114,18 @@ void Controller::KeyboardFocusLostEvent()
 
   if( mImpl->mEventData )
   {
-    mImpl->ChangeState( EventData::INACTIVE );
-
-    if( mImpl->IsShowingPlaceholderText() )
+    if ( EventData::INTERRUPTED != mImpl->mEventData->mState )
     {
-      // Revert to regular placeholder-text when not editing
-      ShowPlaceholderText();
-    }
+      mImpl->ChangeState( EventData::INACTIVE );
 
-    mImpl->RequestRelayout();
+      if( mImpl->IsShowingPlaceholderText() )
+      {
+        // Revert to regular placeholder-text when not editing
+        ShowPlaceholderText();
+      }
+    }
   }
+  mImpl->RequestRelayout();
 }
 
 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
@@ -1151,6 +1159,18 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
     {
       textChanged = BackspaceKeyEvent();
     }
+    else if ( IsKey( keyEvent,  Dali::DALI_KEY_POWER ) )
+    {
+      mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
+      // Avoids calling the InsertText() method which can delete selected text
+    }
+    else if ( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
+              IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
+    {
+      mImpl->ChangeState( EventData::INACTIVE );
+      // Menu/Home key behaviour does not allow edit mode to resume like Power key
+      // Avoids calling the InsertText() method which can delete selected text
+    }
     else
     {
       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
@@ -1162,7 +1182,10 @@ bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
       textChanged = true;
     }
 
-    mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
+    if ( mImpl->mEventData->mState != EventData::INTERRUPTED &&  mImpl->mEventData->mState != EventData::INACTIVE )
+    {
+      mImpl->ChangeState( EventData::EDITING );
+    }
 
     mImpl->RequestRelayout();
   }
@@ -1345,11 +1368,11 @@ void Controller::TapEvent( unsigned int tapCount, float x, float y )
         mImpl->mEventData->mDecorator->SetPopupActive( false );
       }
 
-      mImpl->ChangeState( EventData::EDITING );
-
       // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
       if( mImpl->mEventData )
       {
+        mImpl->ChangeState( EventData::EDITING );
+
         Event event( Event::TAP_EVENT );
         event.p1.mUint = tapCount;
         event.p2.mFloat = x;
@@ -1387,6 +1410,26 @@ void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
   }
 }
 
+void Controller::LongPressEvent( Gesture::State state, float x, float y  )
+{
+  DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
+
+  if  ( mImpl->IsShowingPlaceholderText() || mImpl->mLogicalModel->mText.Count() == 0u )
+  {
+    if ( mImpl->mEventData )
+    {
+      Event event( Event::LONG_PRESS_EVENT );
+      event.p1.mInt = state;
+      mImpl->mEventData->mEventQueue.push_back( event );
+      mImpl->RequestRelayout();
+    }
+  }
+  else if( mImpl->mEventData )
+  {
+    SelectEvent( x, y, false );
+  }
+}
+
 void Controller::SelectEvent( float x, float y, bool selectAll )
 {
   if( mImpl->mEventData )
@@ -1475,6 +1518,20 @@ void Controller::DecorationEvent( HandleType handleType, HandleState state, floa
   }
 }
 
+void Controller::PasteText( const std::string& stringToPaste )
+{
+  InsertText( stringToPaste, Text::Controller::COMMIT );
+  mImpl->ChangeState( EventData::EDITING );
+  mImpl->RequestRelayout();
+}
+
+void Controller::PasteClipboardItemEvent()
+{
+  ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
+  std::string stringToPaste( notifier.GetContent() );
+  PasteText( stringToPaste );
+}
+
 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
 {
   if( NULL == mImpl->mEventData )
@@ -1512,9 +1569,7 @@ void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Butt
     {
       std::string stringToPaste("");
       mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
-      InsertText( stringToPaste, Text::Controller::COMMIT );
-      mImpl->ChangeState( EventData::EDITING );
-      mImpl->RequestRelayout();
+      PasteText( stringToPaste );
       break;
     }
     case Toolkit::TextSelectionPopup::SELECT:
@@ -1536,6 +1591,7 @@ void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Butt
     }
     case Toolkit::TextSelectionPopup::CLIPBOARD:
     {
+      mImpl->ShowClipboard();
       break;
     }
     case Toolkit::TextSelectionPopup::NONE:
@@ -1611,7 +1667,6 @@ ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, cons
   return callbackData;
 }
 
-
 Controller::~Controller()
 {
   delete mImpl;