Framework for Font styles
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-field-impl.cpp
index 5799da0..5f8d498 100644 (file)
 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/images/image.h>
+#include <string>
+#include <dali/public-api/adaptor-framework/key.h>
+#include <dali/public-api/common/stage.h>
+#include <dali/public-api/images/resource-image.h>
 #include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/object/type-registry-helper.h>
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/public-api/text/layouts/layout-engine.h>
-#include <dali-toolkit/public-api/text/rendering/basic/text-basic-renderer.h> // TODO - Get from RendererFactory
+#include <dali-toolkit/public-api/text/rendering-backend.h>
+#include <dali-toolkit/internal/text/layouts/layout-engine.h>
+#include <dali-toolkit/internal/text/rendering/text-backend.h>
+#include <dali-toolkit/internal/styling/style-manager-impl.h>
 
 using namespace Dali::Toolkit::Text;
 
-namespace
-{
-
-} // namespace
-
 namespace Dali
 {
 
 namespace Toolkit
 {
 
-const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT(       Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
-const Property::Index TextField::PROPERTY_TEXT(                   Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
-const Property::Index TextField::PROPERTY_CURSOR_IMAGE(           Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
-const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR(   Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
-const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
-const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK(    Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
-const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL(  Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
-const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION(  Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
-
 namespace Internal
 {
 
 namespace
 {
+  const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
+}
+
+namespace
+{
 
 // Type registration
 BaseHandle Create()
@@ -61,16 +58,26 @@ BaseHandle Create()
   return Toolkit::TextField::New();
 }
 
-TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
-
-PropertyRegistration property1( mType, "placeholder-text",       Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT,       Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property2( mType, "text",                   Toolkit::TextField::PROPERTY_TEXT,                   Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property3( mType, "cursor-image",           Toolkit::TextField::PROPERTY_CURSOR_IMAGE,           Property::STRING,  &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property4( mType, "primary-cursor-color",   Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR,   Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property5( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property6( mType, "enable-cursor-blink",    Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK,    Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property7( mType, "cursor-blink-interval",  Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL,  Property::FLOAT,   &TextField::SetProperty, &TextField::GetProperty );
-PropertyRegistration property8( mType, "cursor-blink-duration",  Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION,  Property::FLOAT,   &TextField::SetProperty, &TextField::GetProperty );
+// Setup properties, signals and actions using the type-registry.
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
+
+DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend",       INTEGER,   RENDERING_BACKEND       )
+DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text",        STRING,    PLACEHOLDER_TEXT        )
+DALI_PROPERTY_REGISTRATION( TextField, "text",                    STRING,    TEXT                    )
+DALI_PROPERTY_REGISTRATION( TextField, "font-family",             STRING,    FONT_FAMILY             )
+DALI_PROPERTY_REGISTRATION( TextField, "font-style",              STRING,    FONT_STYLE              )
+DALI_PROPERTY_REGISTRATION( TextField, "point-size",              FLOAT,     POINT_SIZE              )
+DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy",           INTEGER,   EXCEED_POLICY           )
+DALI_PROPERTY_REGISTRATION( TextField, "cursor-image",            STRING,    CURSOR_IMAGE            )
+DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color",    VECTOR4,   PRIMARY_CURSOR_COLOR    )
+DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color",  VECTOR4,   SECONDARY_CURSOR_COLOR  )
+DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink",     BOOLEAN,   ENABLE_CURSOR_BLINK     )
+DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval",   FLOAT,     CURSOR_BLINK_INTERVAL   )
+DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration",   FLOAT,     CURSOR_BLINK_DURATION   )
+DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image",       STRING,    GRAB_HANDLE_IMAGE       )
+DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
+
+DALI_TYPE_REGISTRATION_END()
 
 } // namespace
 
@@ -89,11 +96,6 @@ Toolkit::TextField TextField::New()
   return handle;
 }
 
-void TextField::SetRenderer( Text::RendererPtr renderer )
-{
-  mRenderer = renderer;
-}
-
 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
 {
   Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
@@ -104,7 +106,18 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
 
     switch( index )
     {
-      case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
+      case Toolkit::TextField::Property::RENDERING_BACKEND:
+      {
+        int backend = value.Get< int >();
+
+        if( impl.mRenderingBackend != backend )
+        {
+          impl.mRenderingBackend = backend;
+          impl.mRenderer.Reset();
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
       {
         if( impl.mController )
         {
@@ -112,7 +125,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_TEXT:
+      case Toolkit::TextField::Property::TEXT:
       {
         if( impl.mController )
         {
@@ -120,10 +133,56 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
+      case Toolkit::TextField::Property::FONT_FAMILY:
+      {
+        if( impl.mController )
+        {
+          std::string fontFamily = value.Get< std::string >();
+
+          if( impl.mController->GetDefaultFontFamily() != fontFamily )
+          {
+            impl.mController->SetDefaultFontFamily( fontFamily );
+            impl.RequestTextRelayout();
+          }
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::FONT_STYLE:
+      {
+        if( impl.mController )
+        {
+          std::string fontStyle = value.Get< std::string >();
+
+          if( impl.mController->GetDefaultFontStyle() != fontStyle )
+          {
+            impl.mController->SetDefaultFontStyle( fontStyle );
+            impl.RequestTextRelayout();
+          }
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::POINT_SIZE:
+      {
+        if( impl.mController )
+        {
+          float pointSize = value.Get< float >();
+
+          if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
+          {
+            impl.mController->SetDefaultPointSize( pointSize );
+            impl.RequestTextRelayout();
+          }
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::EXCEED_POLICY:
+      {
+        impl.mExceedPolicy = value.Get< int >();
+        break;
+      }
+      case Toolkit::TextField::Property::CURSOR_IMAGE:
       {
-        Image image = Image::New( value.Get< std::string >() );
-        //ResourceImage image = ResourceImage::New( value.Get< std::string >() );
+        ResourceImage image = ResourceImage::New( value.Get< std::string >() );
 
         if( impl.mDecorator )
         {
@@ -131,7 +190,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
+      case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
       {
         if( impl.mDecorator )
         {
@@ -139,7 +198,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
+      case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
       {
         if( impl.mDecorator )
         {
@@ -147,7 +206,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
+      case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
       {
         if( impl.mController )
         {
@@ -155,7 +214,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
+      case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
       {
         if( impl.mDecorator )
         {
@@ -163,7 +222,7 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
+      case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
       {
         if( impl.mDecorator )
         {
@@ -171,6 +230,24 @@ void TextField::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
+      case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
+      {
+        ResourceImage image = ResourceImage::New( value.Get< std::string >() );
+
+        if( impl.mDecorator )
+        {
+          impl.mDecorator->SetGrabHandleImage( image );
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
+      {
+        if( impl.mDecorator )
+        {
+          impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
+        }
+        break;
+      }
     }
   }
 }
@@ -187,31 +264,49 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
 
     switch( index )
     {
-      case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
+      case Toolkit::TextField::Property::RENDERING_BACKEND:
       {
-        DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
+        value = impl.mRenderingBackend;
         break;
       }
-      case Toolkit::TextField::PROPERTY_TEXT:
+      case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
       {
-        DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
+        if( impl.mController )
+        {
+          std::string text;
+          impl.mController->GetPlaceholderText( text );
+          value = text;
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::TEXT:
+      {
+        if( impl.mController )
+        {
+          std::string text;
+          impl.mController->GetText( text );
+          value = text;
+        }
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
+      case Toolkit::TextField::Property::EXCEED_POLICY:
+      {
+        value = impl.mExceedPolicy;
+        break;
+      }
+      case Toolkit::TextField::Property::CURSOR_IMAGE:
       {
         if( impl.mDecorator )
         {
-          Image image = impl.mDecorator->GetCursorImage();
-          //ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
+          ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
           if( image )
           {
-            value = image.GetFilename();
-            //value = image.GetUrl();
+            value = image.GetUrl();
           }
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
+      case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
       {
         if( impl.mDecorator )
         {
@@ -219,7 +314,7 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
+      case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
       {
         if( impl.mDecorator )
         {
@@ -227,12 +322,12 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
+      case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
       {
         //value = impl.mController->GetEnableCursorBlink(); TODO
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
+      case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
       {
         if( impl.mDecorator )
         {
@@ -240,7 +335,7 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
         }
         break;
       }
-      case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
+      case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
       {
         if( impl.mDecorator )
         {
@@ -248,6 +343,26 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
         }
         break;
       }
+      case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
+      {
+        if( impl.mDecorator )
+        {
+          ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
+          if( image )
+          {
+            value = image.GetUrl();
+          }
+        }
+        break;
+      }
+      case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
+      {
+        if( impl.mDecorator )
+        {
+          value = impl.mDecorator->GetBoundingBox();
+        }
+        break;
+      }
     }
   }
 
@@ -256,42 +371,145 @@ Property::Value TextField::GetProperty( BaseObject* object, Property::Index inde
 
 void TextField::OnInitialize()
 {
-  mDecorator = Text::Decorator::New( *this );
+  mController = Text::Controller::New( *this );
+
+  mDecorator = Text::Decorator::New( *this, *mController );
 
-  mController = Text::Controller::New();
   mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
-  //mController->EnableTextInput( mDecorator ); TODO
+
+  mController->EnableTextInput( mDecorator );
+
+  // Forward input events to controller
+  EnableGestureDetection(Gesture::Tap);
+  GetTapGestureDetector().SetMaximumTapsRequired( 2 );
+
+  // Set BoundingBox to stage size if not already set.
+  if ( mDecorator->GetBoundingBox().IsEmpty() )
+  {
+    Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
+    mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
+  }
+}
+
+void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
+{
+  GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
+}
+
+Vector3 TextField::GetNaturalSize()
+{
+  return mController->GetNaturalSize();
+}
+
+float TextField::GetHeightForWidth( float width )
+{
+  return mController->GetHeightForWidth( width );
 }
 
 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
 {
-  if( mController->Relayout( size ) )
+  if( mController->Relayout( size ) ||
+      !mRenderer )
   {
+    if( mDecorator )
+    {
+      mDecorator->Relayout( size );
+    }
+
     if( !mRenderer )
     {
-      // TODO - Get from RendererFactory
-      mRenderer = Dali::Toolkit::Text::BasicRenderer::New();
+      mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
     }
 
+    RenderableActor renderableActor;
     if( mRenderer )
     {
-      Actor renderableActor = mRenderer->Render( mController->GetView() );
+      renderableActor = mRenderer->Render( mController->GetView() );
+    }
+
+    EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
+
+    if( renderableActor != mRenderableActor )
+    {
+      UnparentAndReset( mRenderableActor );
+      mRenderableActor = renderableActor;
+    }
 
-      if( renderableActor )
+    if( mRenderableActor )
+    {
+      // Make sure the actor is parented correctly with/without clipping
+      if( mClipper )
+      {
+        mClipper->GetRootActor().Add( mRenderableActor );
+      }
+      else
       {
-        Self().Add( renderableActor );
+        Self().Add( mRenderableActor );
       }
     }
   }
 }
 
+void TextField::OnTap( const TapGesture& gesture )
+{
+  SetKeyInputFocus();
+
+  mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
+}
+
+bool TextField::OnKeyEvent( const KeyEvent& event )
+{
+  if( Dali::DALI_KEY_ESCAPE == event.keyCode )
+  {
+    ClearKeyInputFocus();
+  }
+
+  return mController->KeyEvent( event );
+}
+
+void TextField::RequestTextRelayout()
+{
+  RelayoutRequest();
+}
+
+void TextField::EnableClipping( bool clipping, const Vector2& size )
+{
+  if( clipping )
+  {
+    // Not worth to created clip actor if width or height is equal to zero.
+    if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
+    {
+      if( !mClipper )
+      {
+        Actor self = Self();
+
+        mClipper = Clipper::New( size );
+        self.Add( mClipper->GetRootActor() );
+        self.Add( mClipper->GetImageActor() );
+      }
+      else if ( mClipper )
+      {
+        mClipper->Refresh( size );
+      }
+    }
+  }
+  else
+  {
+    // Note - this will automatically remove the root & image actors
+    mClipper.Reset();
+  }
+}
+
 TextField::TextField()
-: Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
+: Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
+  mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
+  mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
 {
 }
 
 TextField::~TextField()
 {
+  mClipper.Reset();
 }
 
 } // namespace Internal