System font family change to update font in TextField
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-impl.h
index 103013a..e60fe37 100644 (file)
  */
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/text-abstraction/font-client.h>
+#include <dali/devel-api/adaptor-framework/clipboard.h>
+#include <dali/devel-api/text-abstraction/font-client.h>
+#include <iostream>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
 #include <dali-toolkit/internal/text/logical-model-impl.h>
 #include <dali-toolkit/internal/text/text-controller.h>
+#include <dali-toolkit/internal/text/text-view.h>
 #include <dali-toolkit/internal/text/visual-model-impl.h>
 
 namespace Dali
@@ -41,14 +44,15 @@ struct Event
   // Used to queue input events until DoRelayout()
   enum Type
   {
-    KEYBOARD_FOCUS_GAIN_EVENT,
-    KEYBOARD_FOCUS_LOST_EVENT,
     CURSOR_KEY_EVENT,
     TAP_EVENT,
     PAN_EVENT,
+    LONG_PRESS_EVENT,
     GRAB_HANDLE_EVENT,
     LEFT_SELECTION_HANDLE_EVENT,
-    RIGHT_SELECTION_HANDLE_EVENT
+    RIGHT_SELECTION_HANDLE_EVENT,
+    SELECT,
+    SELECT_ALL
   };
 
   union Param
@@ -99,9 +103,14 @@ struct EventData
   enum State
   {
     INACTIVE,
+    INTERRUPTED,
     SELECTING,
+    SELECTION_CHANGED,
     EDITING,
-    EDITING_WITH_POPUP
+    EDITING_WITH_POPUP,
+    EDITING_WITH_GRAB_HANDLE,
+    GRAB_HANDLE_PANNING,
+    SELECTION_HANDLE_PANNING
   };
 
   EventData( DecoratorPtr decorator );
@@ -109,7 +118,9 @@ struct EventData
   ~EventData();
 
   DecoratorPtr       mDecorator;               ///< Pointer to the decorator
-  std::string        mPlaceholderText;         ///< The plaxe holder text
+  std::string        mPlaceholderTextActive;   ///< The text to display when the TextField is empty with key-input focus
+  std::string        mPlaceholderTextInactive; ///< The text to display when the TextField is empty and inactive
+  Vector4            mPlaceholderTextColor;    ///< The in/active placeholder text color
 
   /**
    * This is used to delay handling events until after the model has been updated.
@@ -129,6 +140,11 @@ struct EventData
   CharacterIndex     mLeftSelectionPosition;   ///< Index into logical model for left selection handle.
   CharacterIndex     mRightSelectionPosition;  ///< Index into logical model for right selection handle.
 
+  CharacterIndex     mPreEditStartPosition;    ///< Used to remove the pre-edit text if necessary.
+  Length             mPreEditLength;           ///< Used to remove the pre-edit text if necessary.
+
+  bool mIsShowingPlaceholderText        : 1;   ///< True if the place-holder text is being displayed.
+  bool mPreEditFlag                     : 1;   ///< True if the model contains text in pre-edit state.
   bool mDecoratorUpdated                : 1;   ///< True if the decorator was updated during event processing.
   bool mCursorBlinkEnabled              : 1;   ///< True if cursor should blink when active.
   bool mGrabHandleEnabled               : 1;   ///< True if grab handle is enabled.
@@ -139,26 +155,29 @@ struct EventData
   bool mUpdateCursorPosition            : 1;   ///< True if the visual position of the cursor must be recalculated.
   bool mUpdateLeftSelectionPosition     : 1;   ///< True if the visual position of the left selection handle must be recalculated.
   bool mUpdateRightSelectionPosition    : 1;   ///< True if the visual position of the right selection handle must be recalculated.
-  bool mScrollAfterUpdateCursorPosition : 1;   ///< Whether to scroll after the cursor position is updated.
+  bool mScrollAfterUpdatePosition       : 1;   ///< Whether to scroll after the cursor position is updated.
+  bool mScrollAfterDelete               : 1;   ///< Whether to scroll after delete characters.
+  bool mAllTextSelected                 : 1;   ///< True if the selection handles are selecting all the text
 };
 
 struct ModifyEvent
 {
   enum Type
   {
-    REPLACE_TEXT, ///< Replace the entire text
-    INSERT_TEXT,  ///< Insert characters at the current cursor position
-    DELETE_TEXT   ///< Delete a character at the current cursor position
+    TEXT_REPLACED,    ///< The entire text was replaced
+    TEXT_INSERTED,    ///< Insert characters at the current cursor position
+    TEXT_DELETED      ///< Characters were deleted
   };
 
   Type type;
-  std::string text;
 };
 
 struct FontDefaults
 {
   FontDefaults()
-  : mDefaultPointSize(0.0f),
+  : mDefaultFontFamily(""),
+    mDefaultFontStyle(""),
+    mDefaultPointSize(0.0f),
     mFontId(0u)
   {
   }
@@ -189,31 +208,38 @@ struct Controller::Impl
     mFontDefaults( NULL ),
     mEventData( NULL ),
     mFontClient(),
+    mClipboard(),
     mView(),
+    mMetrics(),
     mLayoutEngine(),
     mModifyEvents(),
-    mControlSize(),
+    mTextColor( Color::BLACK ),
     mAlignmentOffset(),
     mOperationsPending( NO_OPERATION ),
-    mRecalculateNaturalSize( true )
+    mMaximumNumberOfCharacters( 50 ),
+    mRecalculateNaturalSize( true ),
+    mUserDefinedFontFamily( false)
   {
     mLogicalModel = LogicalModel::New();
     mVisualModel  = VisualModel::New();
 
     mFontClient = TextAbstraction::FontClient::Get();
+    mClipboard = Clipboard::Get();
 
     mView.SetVisualModel( mVisualModel );
 
+    // Use this to access FontClient i.e. to get down-scaled Emoji metrics.
+    mMetrics = Metrics::New( mFontClient );
+    mLayoutEngine.SetMetrics( mMetrics );
+
     // Set the text properties to default
-    mVisualModel->SetTextColor( Color::WHITE );
-    mVisualModel->SetShadowOffset( Vector2::ZERO );
-    mVisualModel->SetShadowColor( Color::BLACK );
     mVisualModel->SetUnderlineEnabled( false );
     mVisualModel->SetUnderlineHeight( 0.0f );
   }
 
   ~Impl()
   {
+    delete mFontDefaults;
     delete mEventData;
   }
 
@@ -222,30 +248,145 @@ struct Controller::Impl
    */
   void RequestRelayout();
 
+  /**
+   * @brief Request a relayout using the ControlInterface.
+   */
+  void QueueModifyEvent( ModifyEvent::Type type )
+  {
+    if( ModifyEvent::TEXT_REPLACED == type)
+    {
+      // Cancel previously queued inserts etc.
+      mModifyEvents.clear();
+    }
+
+    ModifyEvent event;
+    event.type = type;
+    mModifyEvents.push_back( event );
+
+    // The event will be processed during relayout
+    RequestRelayout();
+  }
 
   /**
    * @brief Helper to move the cursor, grab handle etc.
    */
   bool ProcessInputEvents();
 
-  void OnKeyboardFocus( bool hasFocus );
+  /**
+   * @brief Helper to check whether any place-holder text is available.
+   */
+  bool IsPlaceholderAvailable() const
+  {
+    return ( mEventData &&
+             ( !mEventData->mPlaceholderTextInactive.empty() ||
+               !mEventData->mPlaceholderTextActive.empty() )
+           );
+  }
 
-  void OnCursorKeyEvent( const Event& event );
+  bool IsShowingPlaceholderText() const
+  {
+    return ( mEventData && mEventData->mIsShowingPlaceholderText );
+  }
 
-  void HandleCursorKey( int keyCode );
+  bool IsShowingRealText() const
+  {
+    return ( !IsShowingPlaceholderText() &&
+             0u != mLogicalModel->mText.Count() );
+  }
+
+  /**
+   * @brief Called when placeholder-text is hidden
+   */
+  void PlaceholderCleared()
+  {
+    if( mEventData )
+    {
+      mEventData->mIsShowingPlaceholderText = false;
+
+      // Remove mPlaceholderTextColor
+      mVisualModel->SetTextColor( mTextColor );
+    }
+  }
+
+  void ClearPreEditFlag()
+  {
+    if( mEventData )
+    {
+      mEventData->mPreEditFlag = false;
+      mEventData->mPreEditStartPosition = 0;
+      mEventData->mPreEditLength = 0;
+    }
+  }
+
+  void ResetImfManager()
+  {
+    // Reset incase we are in a pre-edit state.
+    ImfManager imfManager = ImfManager::Get();
+    if ( imfManager )
+    {
+      imfManager.Reset(); // Will trigger a commit message
+    }
+
+    ClearPreEditFlag();
+  }
+
+  bool IsClipboardEmpty()
+  {
+    bool result( mClipboard && mClipboard.NumberOfItems() );
+    return !result; // // If NumberOfItems greater than 0, return false
+  }
+
+  void UpdateModel( OperationsMask operationsRequired );
+
+  /**
+   * @brief Retrieve the default fonts.
+   *
+   * @param[out] fonts The default font family, style and point sizes.
+   * @param[in] numberOfCharacters The number of characters in the logical model.
+   */
+  void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
+
+  /**
+   * @brief Retrieve the line height of the default font.
+   */
+  float GetDefaultFontLineHeight();
+
+  void OnCursorKeyEvent( const Event& event );
 
   void OnTapEvent( const Event& event );
 
   void OnPanEvent( const Event& event );
 
+  void OnLongPressEvent( const Event& event );
+
   void OnHandleEvent( const Event& event );
 
+  void OnSelectEvent( const Event& event );
+
+  void OnSelectAllEvent();
+
+  void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
+
+  void ShowClipboard();
+
+  void HideClipboard();
+
+  bool CopyStringToClipboard( std::string& source );
+
+  void SendSelectionToClipboard( bool deleteAfterSending );
+
+  void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
+
+  void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
   void RepositionSelectionHandles( float visualX, float visualY );
 
-  void ChangeState( EventData::State newState );
+  void SetPopupButtons();
 
+  void ChangeState( EventData::State newState );
   LineIndex GetClosestLine( float y ) const;
 
+  void FindSelectionIndices( float visualX, float visualY, CharacterIndex& startIndex, CharacterIndex& endIndex );
+
   /**
    * @brief Retrieves the cursor's logical position for a given touch point x,y
    *
@@ -312,11 +453,23 @@ struct Controller::Impl
   void ClampVerticalScroll( const Vector2& actualSize );
 
   /**
+   * @brief Scrolls the text to make a position visible.
+   *
+   * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
+   *
+   * @param[in] position A position in decorator coords.
+   *
+   * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
+   * or moving the selection handles.
+   */
+  void ScrollToMakePositionVisible( const Vector2& position );
+
+  /**
    * @brief Scrolls the text to make the cursor visible.
    *
-   * This method is called after inserting, deleting or moving the cursor with the keypad.
+   * This method is called after deleting text.
    */
-  void ScrollToMakeCursorVisible();
+  void ScrollTextToMatchCursor();
 
   ControlInterface& mControlInterface;     ///< Reference to the text controller.
   LogicalModelPtr mLogicalModel;           ///< Pointer to the logical model.
@@ -324,13 +477,18 @@ struct Controller::Impl
   FontDefaults* mFontDefaults;             ///< Avoid allocating this when the user does not specify a font.
   EventData* mEventData;                   ///< Avoid allocating everything for text input until EnableTextInput().
   TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
+  Clipboard mClipboard;                    ///< Handle to the system clipboard
   View mView;                              ///< The view interface to the rendering back-end.
+  MetricsPtr mMetrics;                     ///< A wrapper around FontClient used to get metrics & potentially down-scaled Emoji metrics.
   LayoutEngine mLayoutEngine;              ///< The layout engine.
   std::vector<ModifyEvent> mModifyEvents;  ///< Temporary stores the text set until the next relayout.
-  Size mControlSize;                       ///< The size of the control.
+  Vector4 mTextColor;                      ///< The regular text color
   Vector2 mAlignmentOffset;                ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
   OperationsMask mOperationsPending;       ///< Operations pending to be done to layout the text.
+  Length mMaximumNumberOfCharacters;       ///< Maximum number of characters that can be inserted.
+
   bool mRecalculateNaturalSize:1;          ///< Whether the natural size needs to be recalculated.
+  bool mUserDefinedFontFamily:1;           ///< Whether the Font family was Set by the user instead of being left as sytem default
 };
 
 } // namespace Text