[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / controller / text-controller-impl.cpp
index cc38fd0..10a76f1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
 #include <dali-toolkit/internal/text/controller/text-controller-impl.h>
 
 // EXTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/window-devel.h>
+#include <dali/integration-api/adaptor-framework/scene-holder.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/actors/layer.h>
 #include <dali/public-api/rendering/renderer.h>
@@ -51,7 +51,9 @@ Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT
 
 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
 
-const char* EMPTY_STRING = "";
+const char* EMPTY_STRING         = "";
+const char* MIME_TYPE_TEXT_PLAIN = "text/plain;charset=utf-8";
+const char* MIME_TYPE_HTML       = "application/xhtml+xml";
 
 } // namespace
 
@@ -439,6 +441,83 @@ bool Controller::Impl::ProcessInputEvents()
   return ControllerImplEventHandler::ProcessInputEvents(*this);
 }
 
+void Controller::Impl::SetAnchorColor(const Vector4& color)
+{
+  mAnchorColor = color;
+  UpdateAnchorColor();
+}
+
+const Vector4& Controller::Impl::GetAnchorColor() const
+{
+  return mAnchorColor;
+}
+
+void Controller::Impl::SetAnchorClickedColor(const Vector4& color)
+{
+  mAnchorClickedColor = color;
+  UpdateAnchorColor();
+}
+
+const Vector4& Controller::Impl::GetAnchorClickedColor() const
+{
+  return mAnchorClickedColor;
+}
+
+void Controller::Impl::UpdateAnchorColor()
+{
+  if(!mAnchorControlInterface ||
+     !mMarkupProcessorEnabled ||
+     !mModel->mLogicalModel->mAnchors.Count() ||
+     !IsShowingRealText())
+  {
+    return;
+  }
+
+  bool updateNeeded = false;
+
+  // The anchor color & clicked color needs to be updated with the property's color.
+  for(auto& anchor : mModel->mLogicalModel->mAnchors)
+  {
+    if(!anchor.isMarkupColorSet && !anchor.isClicked)
+    {
+      if(mModel->mLogicalModel->mColorRuns.Count() > anchor.colorRunIndex)
+      {
+        ColorRun& colorRun = *(mModel->mLogicalModel->mColorRuns.Begin() + anchor.colorRunIndex);
+        colorRun.color     = mAnchorColor;
+        updateNeeded       = true;
+      }
+      if(mModel->mLogicalModel->mUnderlinedCharacterRuns.Count() > anchor.underlinedCharacterRunIndex)
+      {
+        UnderlinedCharacterRun& underlineRun = *(mModel->mLogicalModel->mUnderlinedCharacterRuns.Begin() + anchor.underlinedCharacterRunIndex);
+        underlineRun.properties.color        = mAnchorColor;
+        updateNeeded                         = true;
+      }
+    }
+    else if(!anchor.isMarkupClickedColorSet && anchor.isClicked)
+    {
+      if(mModel->mLogicalModel->mColorRuns.Count() > anchor.colorRunIndex)
+      {
+        ColorRun& colorRun = *(mModel->mLogicalModel->mColorRuns.Begin() + anchor.colorRunIndex);
+        colorRun.color     = mAnchorClickedColor;
+        updateNeeded       = true;
+      }
+      if(mModel->mLogicalModel->mUnderlinedCharacterRuns.Count() > anchor.underlinedCharacterRunIndex)
+      {
+        UnderlinedCharacterRun& underlineRun = *(mModel->mLogicalModel->mUnderlinedCharacterRuns.Begin() + anchor.underlinedCharacterRunIndex);
+        underlineRun.properties.color        = mAnchorClickedColor;
+        updateNeeded                         = true;
+      }
+    }
+  }
+
+  if(updateNeeded)
+  {
+    ClearFontData();
+    mOperationsPending = static_cast<OperationsMask>(mOperationsPending | COLOR);
+    RequestRelayout();
+  }
+}
+
 void Controller::Impl::NotifyInputMethodContext()
 {
   if(mEventData && mEventData->mInputMethodContext)
@@ -540,8 +619,8 @@ Dali::LayoutDirection::Type Controller::Impl::GetLayoutDirection(Dali::Actor& ac
   if(mModel->mMatchLayoutDirection == DevelText::MatchLayoutDirection::LOCALE ||
      (mModel->mMatchLayoutDirection == DevelText::MatchLayoutDirection::INHERIT && !mIsLayoutDirectionChanged))
   {
-    Window window = DevelWindow::Get(actor);
-    return static_cast<Dali::LayoutDirection::Type>(window ? window.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>() : LayoutDirection::LEFT_TO_RIGHT);
+    Integration::SceneHolder sceneHolder = Integration::SceneHolder::Get(actor);
+    return static_cast<Dali::LayoutDirection::Type>(sceneHolder ? sceneHolder.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>() : LayoutDirection::LEFT_TO_RIGHT);
   }
   else
   {
@@ -623,7 +702,7 @@ void Controller::Impl::CalculateTextUpdateIndices(Length& numberOfCharacters)
       mTextUpdateInfo.mRequestedNumberOfCharacters = mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
 
       mTextUpdateInfo.mStartGlyphIndex = mModel->mVisualModel->mGlyphs.Count();
-      mTextUpdateInfo.mStartLineIndex  = mModel->mVisualModel->mLines.Count() - 1u;
+      mTextUpdateInfo.mStartLineIndex  = (mModel->mVisualModel->mLines.Count() > 0u) ? mModel->mVisualModel->mLines.Count() - 1u : 0u;
 
       // Nothing else to do;
       return;
@@ -933,6 +1012,8 @@ void Controller::Impl::SetEditable(bool editable)
     {
       bool decoratorEditable = editable && mIsUserInteractionEnabled;
       mEventData->mDecorator->SetEditable(decoratorEditable);
+      mEventData->mDecoratorUpdated = true;
+      RequestRelayout();
     }
   }
 }
@@ -1072,8 +1153,19 @@ void Controller::Impl::SetClipboardHideEnable(bool enable)
 
 bool Controller::Impl::CopyStringToClipboard(const std::string& source)
 {
-  //Send string to clipboard
-  return (EnsureClipboardCreated() && mClipboard.SetItem(source));
+  if(EnsureClipboardCreated())
+  {
+    Dali::Clipboard::ClipData data(MIME_TYPE_TEXT_PLAIN, source.c_str());
+    return mClipboard.SetData(data); // Send clipboard data to clipboard.
+  }
+
+  return false;
+}
+
+bool Controller::Impl::IsClipboardEmpty()
+{
+  bool result(Clipboard::IsAvailable() && EnsureClipboardCreated() && (mClipboard.HasType(MIME_TYPE_TEXT_PLAIN) || mClipboard.HasType(MIME_TYPE_HTML)));
+  return !result;
 }
 
 void Controller::Impl::SendSelectionToClipboard(bool deleteAfterSending)
@@ -1084,14 +1176,6 @@ void Controller::Impl::SendSelectionToClipboard(bool deleteAfterSending)
   ChangeState(EventData::EDITING);
 }
 
-void Controller::Impl::RequestGetTextFromClipboard()
-{
-  if(EnsureClipboardCreated())
-  {
-    mClipboard.RequestItem();
-  }
-}
-
 void Controller::Impl::RepositionSelectionHandles()
 {
   SelectionHandleController::Reposition(*this);
@@ -1307,11 +1391,12 @@ CharacterIndex Controller::Impl::CalculateNewCursorIndex(CharacterIndex index) c
 
   if(index < mEventData->mPrimaryCursorPosition)
   {
-    cursorIndex -= numberOfCharacters;
+    cursorIndex = cursorIndex < numberOfCharacters ? 0u : cursorIndex - numberOfCharacters;
   }
   else
   {
-    cursorIndex += numberOfCharacters;
+    Length textLength = mModel->mVisualModel->mCharactersToGlyph.Count();
+    cursorIndex       = cursorIndex + numberOfCharacters > textLength ? textLength : cursorIndex + numberOfCharacters;
   }
 
   // Will update the cursor hook position.
@@ -1937,6 +2022,8 @@ void Controller::Impl::SetUserInteractionEnabled(bool enabled)
   {
     bool editable = mEventData->mEditingEnabled && enabled;
     mEventData->mDecorator->SetEditable(editable);
+    mEventData->mDecoratorUpdated = true;
+    RequestRelayout();
   }
 }
 
@@ -1972,6 +2059,7 @@ void Controller::Impl::ClearStyleData()
   mModel->mLogicalModel->mColorRuns.Clear();
   mModel->mLogicalModel->ClearFontDescriptionRuns();
   mModel->mLogicalModel->ClearStrikethroughRuns();
+  mModel->mLogicalModel->ClearUnderlineRuns();
 }
 
 void Controller::Impl::ResetScrollPosition()