Fix for TextSelectionToolbar overshootEffectColor styling
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-toolbar-impl.cpp
index e26614d..957e152 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
 // CLASS HEADER
 #include <dali-toolkit/internal/controls/text-controls/text-selection-toolbar-impl.h>
 
-// INTERNAL INCLUDES
-#include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
-#include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
-
 // EXTERNAL INCLUDES
-#include <dali/public-api/images/resource-image.h>
+#include <cfloat>
+#include <dali/public-api/images/buffer-image.h>
 #include <dali/public-api/math/vector2.h>
 #include <dali/public-api/math/vector4.h>
-#include <dali/devel-api/object/type-registry-helper.h>
-#include <cfloat>
+#include <dali/public-api/object/property-map.h>
+#include <dali/public-api/object/type-registry-helper.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/public-api/controls/image-view/image-view.h>
+#include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
+#include <dali-toolkit/internal/helpers/color-conversion.h>
 
 namespace Dali
 {
@@ -40,7 +42,8 @@ namespace Internal
 
 namespace
 {
-const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
+
+const Dali::Vector2 DEFAULT_SCROLL_BAR_PADDING( 8.0f, 6.0f );
 
 BaseHandle Create()
 {
@@ -51,8 +54,11 @@ BaseHandle Create()
 
 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
 
-DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "max-size", VECTOR2, MAX_SIZE )
-DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enable-overshoot", BOOLEAN, ENABLE_OVERSHOOT )
+DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize",  VECTOR2, MAX_SIZE )
+DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot",  BOOLEAN, ENABLE_OVERSHOOT )
+DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableScrollBar", BOOLEAN, ENABLE_SCROLL_BAR )
+DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollBarPadding", VECTOR2, SCROLL_BAR_PADDING )
+DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView",  MAP, SCROLL_VIEW )
 
 DALI_TYPE_REGISTRATION_END()
 
@@ -90,9 +96,33 @@ void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index inde
       }
       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
       {
+        if( !impl.mScrollView )
+        {
+          impl.mScrollView  = Toolkit::ScrollView::New();
+        }
         impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
         break;
       }
+      case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
+      {
+        impl.SetUpScrollBar( value.Get< bool >() );
+        break;
+      }
+      case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
+      {
+        impl.SetScrollBarPadding( value.Get< Vector2 >() );
+        break;
+      }
+      case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
+      {
+        // Get a Property::Map from the property if possible.
+        Property::Map setPropertyMap;
+        if( value.Get( setPropertyMap ) )
+        {
+          impl.ConfigureScrollview( setPropertyMap );
+        }
+        break;
+      }
     } // switch
   } // TextSelectionToolbar
 }
@@ -119,6 +149,16 @@ Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property:
         value = impl.mScrollView.IsOvershootEnabled();
         break;
       }
+      case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
+      {
+        value = impl.mScrollBar ? true : false;
+        break;
+      }
+      case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
+      {
+        value = impl.GetScrollBarPadding();
+        break;
+      }
     } // switch
   }
   return value;
@@ -134,40 +174,21 @@ void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& c
   float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
   mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
   mScrollView.SetRulerX( mRulerX );
-}
 
-void TextSelectionToolbar::OnStageConnection( int depth )
-{
-  // Call the Control::OnStageConnection() to set the depth of the background.
-  Control::OnStageConnection( depth );
-
-  // Traverse the dividers and set the depth.
-  for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
+  if( mScrollBar )
   {
-    Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
-
-    ImageActor dividerImageActor = ImageActor::DownCast( divider );
-    if( dividerImageActor )
-    {
-      dividerImageActor.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
-    }
-    else
-    {
-      // TODO at the moment divider are image actors.
-    }
+    float barWidth = std::min( mTableOfButtons.GetNaturalSize().width, size.width ) - 2.f * mScrollBarPadding.x;
+    mScrollBar.SetSize( Vector2( 0.0f, barWidth ) );
   }
-
-  // Texts are controls, they have their own OnStageConnection() implementation.
-  // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
 }
 
 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
 {
   mMaxSize = maxSize;
-  if (mScrollView && mStencilLayer )
+  if (mScrollView && mToolbarLayer )
   {
     mScrollView.SetMaximumSize( mMaxSize );
-    mStencilLayer.SetMaximumSize( mMaxSize );
+    mToolbarLayer.SetMaximumSize( mMaxSize );
   }
 }
 
@@ -178,6 +199,7 @@ const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
 
 void TextSelectionToolbar::SetUpScrollView()
 {
+  mScrollView.SetName("TextSelectionScrollView");
   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
@@ -199,20 +221,19 @@ void TextSelectionToolbar::SetUpScrollView()
 void TextSelectionToolbar::SetUp()
 {
   Actor self = Self();
-  self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
 
-  // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
-  mStencilLayer = Layer::New();
-  mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
-  mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
+  self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
 
-  ImageActor stencil = CreateSolidColorActor( Color::RED );
-  stencil.SetDrawMode( DrawMode::STENCIL );
-  stencil.SetVisible( true );
-  stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
-  stencil.SetParentOrigin( ParentOrigin::CENTER );
+  // Create Layer to house the toolbar.
+  mToolbarLayer = Layer::New();
+  mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
+  mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
+  mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
 
-  mScrollView  = Toolkit::ScrollView::New();
+  if( !mScrollView )
+  {
+    mScrollView = Toolkit::ScrollView::New();
+  }
   SetUpScrollView();
 
   // Toolbar must start with at least one option, adding further options with increase it's size
@@ -221,11 +242,40 @@ void TextSelectionToolbar::SetUp()
   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
 
-
-  mStencilLayer.Add( stencil );
-  mStencilLayer.Add( mScrollView );
   mScrollView.Add( mTableOfButtons );
-  self.Add( mStencilLayer );
+  mToolbarLayer.Add( mScrollView );
+
+  self.Add( mToolbarLayer );
+}
+
+void TextSelectionToolbar::SetUpScrollBar( bool enable )
+{
+  if( enable )
+  {
+    if( ! mScrollBar )
+    {
+      Toolkit::ImageView indicator = Toolkit::ImageView::New();
+      indicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
+      indicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+      indicator.SetStyleName( "TextSelectionScrollIndicator" );
+
+      mScrollBar = Toolkit::ScrollBar::New( Toolkit::ScrollBar::Horizontal );
+      mScrollBar.SetName( "Text popup scroll bar" );
+      mScrollBar.SetStyleName( "TextSelectionScrollBar" );
+      mScrollBar.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
+      mScrollBar.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+      mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
+      mScrollBar.SetResizePolicy( Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH );
+      mScrollBar.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
+      mScrollBar.SetScrollIndicator( indicator );
+      mScrollBar.GetPanGestureDetector().DetachAll();
+      mScrollView.Add( mScrollBar );
+    }
+  }
+  else
+  {
+    UnparentAndReset( mScrollBar );
+  }
 }
 
 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
@@ -263,12 +313,61 @@ void TextSelectionToolbar::ResizeDividers( Size& size )
 
 void TextSelectionToolbar::RaiseAbove( Layer target )
 {
-  mStencilLayer.RaiseAbove( target );
+  mToolbarLayer.RaiseAbove( target );
+}
+
+void TextSelectionToolbar::SetScrollBarPadding( const Vector2& padding )
+{
+  mScrollBarPadding = padding;
+  if( mScrollBar )
+  {
+    mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
+  }
+
+  RelayoutRequest();
+}
+
+void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
+{
+  // Set any properties specified for the label by iterating through all property key-value pairs.
+  for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
+  {
+    const StringValuePair& propertyPair( properties.GetPair( i ) );
+
+    // Convert the property string to a property index.
+    Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
+    if( setPropertyIndex != Property::INVALID_INDEX )
+    {
+      // Convert the string representation of a color into a Vector4
+      if( setPropertyIndex == Toolkit::Scrollable::Property::OVERSHOOT_EFFECT_COLOR )
+      {
+        Vector4 color;
+        if( ConvertPropertyToColor( propertyPair.second, color ) )
+        {
+          mScrollView.SetOvershootEffectColor( color );
+        }
+      }
+      else
+      {
+        // If the conversion worked, we have a valid property index,
+        // Set the property to the new value.
+        mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
+      }
+    }
+  }
+
+  RelayoutRequest();
+}
+
+const Vector2& TextSelectionToolbar::GetScrollBarPadding() const
+{
+  return mScrollBarPadding;
 }
 
 TextSelectionToolbar::TextSelectionToolbar()
-: Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
+: Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
   mMaxSize (),
+  mScrollBarPadding( DEFAULT_SCROLL_BAR_PADDING ),
   mIndexInTable( 0 ),
   mDividerIndexes()
 {