Text - Move cursor's position related code to a different file.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-impl.h
index 351ff82..c996a1f 100644 (file)
@@ -23,6 +23,7 @@
 #include <dali/devel-api/text-abstraction/font-client.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/internal/text/input-style.h>
 #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>
@@ -38,6 +39,9 @@ namespace Toolkit
 namespace Text
 {
 
+//Forward declarations
+struct CursorInfo;
+
 struct Event
 {
   // Used to queue input events until DoRelayout()
@@ -75,28 +79,6 @@ struct Event
   Param p3;
 };
 
-struct CursorInfo
-{
-  CursorInfo()
-  : primaryPosition(),
-    secondaryPosition(),
-    lineHeight( 0.f ),
-    primaryCursorHeight( 0.f ),
-    secondaryCursorHeight( 0.f ),
-    isSecondaryCursor( false )
-  {}
-
-  ~CursorInfo()
-  {}
-
-  Vector2 primaryPosition;       ///< The primary cursor's position.
-  Vector2 secondaryPosition;     ///< The secondary cursor's position.
-  float   lineHeight;            ///< The height of the line where the cursor is placed.
-  float   primaryCursorHeight;   ///< The primary cursor's height.
-  float   secondaryCursorHeight; ///< The secondary cursor's height.
-  bool    isSecondaryCursor;     ///< Whether the secondary cursor is valid.
-};
-
 struct EventData
 {
   enum State
@@ -104,10 +86,10 @@ struct EventData
     INACTIVE,
     INTERRUPTED,
     SELECTING,
-    SELECTION_CHANGED,
     EDITING,
     EDITING_WITH_POPUP,
     EDITING_WITH_GRAB_HANDLE,
+    EDITING_WITH_PASTE_POPUP,
     GRAB_HANDLE_PANNING,
     SELECTION_HANDLE_PANNING
   };
@@ -116,10 +98,16 @@ struct EventData
 
   ~EventData();
 
-  DecoratorPtr       mDecorator;               ///< Pointer to the decorator
-  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
+  static bool IsEditingState( State stateToCheck )
+  {
+    return ( stateToCheck == EDITING || stateToCheck == EDITING_WITH_POPUP || stateToCheck == EDITING_WITH_GRAB_HANDLE || stateToCheck == EDITING_WITH_PASTE_POPUP );
+  }
+
+  DecoratorPtr       mDecorator;               ///< Pointer to the decorator.
+  ImfManager         mImfManager;              ///< The Input Method Framework Manager.
+  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.
@@ -127,6 +115,8 @@ struct EventData
    */
   std::vector<Event> mEventQueue;              ///< The queue of touch events etc.
 
+  InputStyle         mInputStyle;              ///< The style to be set to the new inputed text.
+
   /**
    * 0,0 means that the top-left corner of the layout matches the top-left corner of the UI control.
    * Typically this will have a negative value with scrolling occurs.
@@ -156,7 +146,8 @@ struct EventData
   bool mUpdateRightSelectionPosition    : 1;   ///< True if the visual position of the right selection handle must be recalculated.
   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
+  bool mAllTextSelected                 : 1;   ///< True if the selection handles are selecting all the text.
+  bool mUpdateInputStyle                : 1;   ///< Whether to update the input style after moving the cursor.
 };
 
 struct ModifyEvent
@@ -176,8 +167,13 @@ struct FontDefaults
   FontDefaults()
   : mFontDescription(),
     mFontStyle(),
-    mDefaultPointSize(0.0f),
-    mFontId(0u)
+    mDefaultPointSize( 0.f ),
+    mFontId( 0u ),
+    familyDefined( false ),
+    weightDefined( false ),
+    widthDefined( false ),
+    slantDefined( false ),
+    sizeDefined( false )
   {
     // Initially use the default platform font
     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
@@ -188,17 +184,79 @@ struct FontDefaults
   {
     if( !mFontId )
     {
-      Dali::TextAbstraction::PointSize26Dot6 pointSize = mDefaultPointSize*64;
+      const PointSize26Dot6 pointSize = static_cast<PointSize26Dot6>( mDefaultPointSize * 64.f );
       mFontId = fontClient.GetFontId( mFontDescription, pointSize );
     }
 
     return mFontId;
   }
 
-  TextAbstraction::FontDescription mFontDescription;
-  std::string mFontStyle;
-  float mDefaultPointSize;
-  FontId mFontId;
+  TextAbstraction::FontDescription mFontDescription;  ///< The default font's description.
+  std::string                      mFontStyle;        ///< The font's style string set through the property system.
+  float                            mDefaultPointSize; ///< The default font's point size.
+  FontId                           mFontId;           ///< The font's id of the default font.
+  bool familyDefined:1; ///< Whether the default font's family name is defined.
+  bool weightDefined:1; ///< Whether the default font's weight is defined.
+  bool  widthDefined:1; ///< Whether the default font's width is defined.
+  bool  slantDefined:1; ///< Whether the default font's slant is defined.
+  bool   sizeDefined:1; ///< Whether the default font's point size is defined.
+};
+
+/**
+ * @brief Stores indices used to update the text.
+ * Stores the character index where the text is updated and the number of characters removed and added.
+ * Stores as well indices to the first and the last paragraphs to be updated.
+ */
+struct TextUpdateInfo
+{
+  TextUpdateInfo()
+  : mCharacterIndex( 0u ),
+    mNumberOfCharactersToRemove( 0u ),
+    mNumberOfCharactersToAdd( 0u ),
+    mPreviousNumberOfCharacters( 0u ),
+    mParagraphCharacterIndex( 0u ),
+    mRequestedNumberOfCharacters( 0u ),
+    mStartGlyphIndex( 0u ),
+    mStartLineIndex( 0u ),
+    mEstimatedNumberOfLines( 0u ),
+    mClearAll( true ),
+    mFullRelayoutNeeded( true ),
+    mIsLastCharacterNewParagraph( false )
+  {}
+
+  ~TextUpdateInfo()
+  {}
+
+  CharacterIndex    mCharacterIndex;                ///< Index to the first character to be updated.
+  Length            mNumberOfCharactersToRemove;    ///< The number of characters to be removed.
+  Length            mNumberOfCharactersToAdd;       ///< The number of characters to be added.
+  Length            mPreviousNumberOfCharacters;    ///< The number of characters before the text update.
+
+  CharacterIndex    mParagraphCharacterIndex;       ///< Index of the first character of the first paragraph to be updated.
+  Length            mRequestedNumberOfCharacters;   ///< The requested number of characters.
+  GlyphIndex        mStartGlyphIndex;
+  LineIndex         mStartLineIndex;
+  Length            mEstimatedNumberOfLines;         ///< The estimated number of lines. Used to avoid reallocations when layouting.
+
+  bool              mClearAll:1;                    ///< Whether the whole text is cleared. i.e. when the text is reset.
+  bool              mFullRelayoutNeeded:1;          ///< Whether a full re-layout is needed. i.e. when a new size is set to the text control.
+  bool              mIsLastCharacterNewParagraph:1; ///< Whether the last character is a new paragraph character.
+
+  void Clear()
+  {
+    // Clear all info except the mPreviousNumberOfCharacters member.
+    mCharacterIndex = static_cast<CharacterIndex>( -1 );
+    mNumberOfCharactersToRemove = 0u;
+    mNumberOfCharactersToAdd = 0u;
+    mParagraphCharacterIndex = 0u;
+    mRequestedNumberOfCharacters = 0u;
+    mStartGlyphIndex = 0u;
+    mStartLineIndex = 0u;
+    mEstimatedNumberOfLines = 0u;
+    mClearAll = false;
+    mFullRelayoutNeeded = false;
+    mIsLastCharacterNewParagraph = false;
+  }
 };
 
 struct Controller::Impl
@@ -217,10 +275,14 @@ struct Controller::Impl
     mModifyEvents(),
     mTextColor( Color::BLACK ),
     mAlignmentOffset(),
+    mTextUpdateInfo(),
     mOperationsPending( NO_OPERATION ),
-    mMaximumNumberOfCharacters( 50 ),
+    mMaximumNumberOfCharacters( 50u ),
     mRecalculateNaturalSize( true ),
-    mUserDefinedFontFamily( false )
+    mMarkupProcessorEnabled( false ),
+    mClipboardHideEnabled( true ),
+    mAutoScrollEnabled( false ),
+    mAutoScrollDirectionRTL( false )
   {
     mLogicalModel = LogicalModel::New();
     mVisualModel  = VisualModel::New();
@@ -258,12 +320,12 @@ struct Controller::Impl
     if( ModifyEvent::TEXT_REPLACED == type)
     {
       // Cancel previously queued inserts etc.
-      mModifyEvents.clear();
+      mModifyEvents.Clear();
     }
 
     ModifyEvent event;
     event.type = type;
-    mModifyEvents.push_back( event );
+    mModifyEvents.PushBack( event );
 
     // The event will be processed during relayout
     RequestRelayout();
@@ -290,6 +352,14 @@ struct Controller::Impl
     return ( mEventData && mEventData->mIsShowingPlaceholderText );
   }
 
+  /**
+   * @brief Helper to check whether active place-holder text is available.
+   */
+  bool IsFocusedPlaceholderAvailable() const
+  {
+    return ( mEventData && !mEventData->mPlaceholderTextActive.empty() );
+  }
+
   bool IsShowingRealText() const
   {
     return ( !IsShowingPlaceholderText() &&
@@ -322,31 +392,93 @@ struct Controller::Impl
 
   void ResetImfManager()
   {
-    // Reset incase we are in a pre-edit state.
-    ImfManager imfManager = ImfManager::Get();
-    if ( imfManager )
+    if( mEventData )
     {
-      imfManager.Reset(); // Will trigger a commit message
-    }
+      // Reset incase we are in a pre-edit state.
+      if( mEventData->mImfManager )
+      {
+        mEventData->mImfManager.Reset(); // Will trigger a message ( commit, get surrounding )
+      }
 
-    ClearPreEditFlag();
+      ClearPreEditFlag();
+    }
   }
 
   bool IsClipboardEmpty()
   {
     bool result( mClipboard && mClipboard.NumberOfItems() );
-    return !result; // // If NumberOfItems greater than 0, return false
+    return !result; // If NumberOfItems greater than 0, return false
   }
 
-  void UpdateModel( OperationsMask operationsRequired );
+  /**
+   * @brief Calculates the start character index of the first paragraph to be updated and
+   * the end character index of the last paragraph to be updated.
+   *
+   * @param[out] numberOfCharacters The number of characters to be updated.
+   */
+  void CalculateTextUpdateIndices( Length& numberOfCharacters );
 
   /**
-   * @brief Retrieve the default fonts.
+   * @brief Helper to clear completely the parts of the model specified by the given @p operations.
    *
-   * @param[out] fonts The default font family, style and point sizes.
-   * @param[in] numberOfCharacters The number of characters in the logical model.
+   * @note It never clears the text stored in utf32.
    */
-  void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
+  void ClearFullModelData( OperationsMask operations );
+
+  /**
+   * @brief Helper to clear completely the parts of the model related with the characters specified by the given @p operations.
+   *
+   * @note It never clears the text stored in utf32.
+   *
+   * @param[in] startIndex Index to the first character to be cleared.
+   * @param[in] endIndex Index to the last character to be cleared.
+   * @param[in] operations The operations required.
+   */
+  void ClearCharacterModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations );
+
+  /**
+   * @brief Helper to clear completely the parts of the model related with the glyphs specified by the given @p operations.
+   *
+   * @note It never clears the text stored in utf32.
+   * @note Character indices are transformed to glyph indices.
+   *
+   * @param[in] startIndex Index to the first character to be cleared.
+   * @param[in] endIndex Index to the last character to be cleared.
+   * @param[in] operations The operations required.
+   */
+  void ClearGlyphModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations );
+
+  /**
+   * @brief Helper to clear the parts of the model specified by the given @p operations and from @p startIndex to @p endIndex.
+   *
+   * @note It never clears the text stored in utf32.
+   *
+   * @param[in] startIndex Index to the first character to be cleared.
+   * @param[in] endIndex Index to the last character to be cleared.
+   * @param[in] operations The operations required.
+   */
+  void ClearModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations );
+
+  /**
+   * @brief Updates the logical and visual models. Updates the style runs in the visual model when the text's styles changes.
+   *
+   * When text or style changes the model is set with some operations pending.
+   * When i.e. the text's size or a relayout is required this method is called
+   * with a given @p operationsRequired parameter. The operations required are
+   * matched with the operations pending to perform the minimum number of operations.
+   *
+   * @param[in] operationsRequired The operations required.
+   *
+   * @return @e true if the model has been modified.
+   */
+  bool UpdateModel( OperationsMask operationsRequired );
+
+  /**
+   * @brief Retreieves the default style.
+   *
+   * @param[out] inputStyle The default style.
+   */
+  void RetrieveDefaultInputStyle( InputStyle& inputStyle );
 
   /**
    * @brief Retrieve the line height of the default font.
@@ -367,38 +499,26 @@ struct Controller::Impl
 
   void OnSelectAllEvent();
 
-  void RetrieveSelection( std::string& selectedText, bool deleteAfterRetreival );
+  void RetrieveSelection( std::string& selectedText, bool deleteAfterRetrieval );
 
   void ShowClipboard();
 
   void HideClipboard();
 
+  void SetClipboardHideEnable(bool enable);
+
   bool CopyStringToClipboard( std::string& source );
 
   void SendSelectionToClipboard( bool deleteAfterSending );
 
-  void GetTextFromClipboard( unsigned int itemIndex, std::string& retreivedString );
+  void GetTextFromClipboard( unsigned int itemIndex, std::string& retrievedString );
 
-  void RepositionSelectionHandles( CharacterIndex selectionStart, CharacterIndex selectionEnd );
+  void RepositionSelectionHandles();
   void RepositionSelectionHandles( float visualX, float visualY );
 
   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
-   *
-   * @param[in] visualX The touch point x.
-   * @param[in] visualY The touch point y.
-   *
-   * @return The logical cursor position (in characters). 0 is just before the first character, a value equal to the number of characters is just after the last character.
-   */
-  CharacterIndex GetClosestCursorIndex( float visualX,
-                                        float visualY );
 
   /**
    * @brief Calculates the cursor's position for a given character index in the logical order.
@@ -427,18 +547,23 @@ struct Controller::Impl
   /**
    * @brief Updates the cursor position.
    *
-   * Retrieves the x,y position of the cursor logical position and sets it into the decorator.
+   * Sets the cursor's position into the decorator. It transforms the cursor's position into decorator's coords.
    * It sets the position of the secondary cursor if it's a valid one.
    * Sets which cursors are active.
+   *
+   * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
+   *
    */
-  void UpdateCursorPosition();
+  void UpdateCursorPosition( const CursorInfo& cursorInfo );
 
   /**
-   * @brief Updates the position of the given selection handle.
+   * @brief Updates the position of the given selection handle. It transforms the handle's position into decorator's coords.
    *
    * @param[in] handleType One of the selection handles.
+   * @param[in] cursorInfo Contains the selection handle position in Actor's coords.
    */
-  void UpdateSelectionHandle( HandleType handleType );
+  void UpdateSelectionHandle( HandleType handleType,
+                              const CursorInfo& cursorInfo );
 
   /**
    * @biref Clamps the horizontal scrolling to get the control always filled with text.
@@ -459,7 +584,7 @@ struct Controller::Impl
    *
    * @pre mEventData must not be NULL. (there is a text-input or selection capabilities).
    *
-   * @param[in] position A position in decorator coords.
+   * @param[in] position A position in text coords.
    *
    * This method is called after inserting text, moving the cursor with the grab handle or the keypad,
    * or moving the selection handles.
@@ -471,7 +596,15 @@ struct Controller::Impl
    *
    * This method is called after deleting text.
    */
-  void ScrollTextToMatchCursor();
+  void ScrollTextToMatchCursor( const CursorInfo& cursorInfo );
+
+private:
+  // Declared private and left undefined to avoid copies.
+  Impl( const Impl& );
+  // Declared private and left undefined to avoid copies.
+  Impl& operator=( const Impl& );
+
+public:
 
   ControlInterface& mControlInterface;     ///< Reference to the text controller.
   LogicalModelPtr mLogicalModel;           ///< Pointer to the logical model.
@@ -483,14 +616,19 @@ struct Controller::Impl
   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.
+  Vector<ModifyEvent> mModifyEvents;       ///< Temporary stores the text set until the next relayout.
   Vector4 mTextColor;                      ///< The regular text color
   Vector2 mAlignmentOffset;                ///< Vertical and horizontal offset of the whole text inside the control due to alignment.
+  TextUpdateInfo mTextUpdateInfo;          ///< Info of the characters updated.
   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.
+  bool mMarkupProcessorEnabled:1;          ///< Whether the mark-up procesor is enabled.
+  bool mClipboardHideEnabled:1;            ///< Whether the ClipboardHide function work or not
+  bool mAutoScrollEnabled:1;               ///< Whether auto text scrolling is enabled.
+  CharacterDirection mAutoScrollDirectionRTL:1;  ///< Direction of auto scrolling, true if rtl
+
 };
 
 } // namespace Text