Update position & size of web view.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
index 2369acf..27fee70 100755 (executable)
@@ -39,6 +39,7 @@
 #include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/public-api/image-loader/image.h>
 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 
 namespace Dali
@@ -65,6 +66,8 @@ DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent",               STRING,
 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition",          VECTOR2, SCROLL_POSITION            )
 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize",              VECTOR2, SCROLL_SIZE                )
 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize",             VECTOR2, CONTENT_SIZE               )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "title",                   STRING,  TITLE                      )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "videoHoleEnabled",        BOOLEAN, VIDEO_HOLE_ENABLED         )
 
 DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadStarted",         PAGE_LOAD_STARTED_SIGNAL            )
 DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadFinished",        PAGE_LOAD_FINISHED_SIGNAL           )
@@ -91,7 +94,9 @@ WebView::WebView( const std::string& locale, const std::string& timezoneId )
   mWebEngine(),
   mPageLoadStartedSignal(),
   mPageLoadFinishedSignal(),
-  mPageLoadErrorSignal()
+  mPageLoadErrorSignal(),
+  mVideoHoleEnabled( true ),
+  mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
 {
   mWebEngine = Dali::WebEngine::New();
 
@@ -102,6 +107,27 @@ WebView::WebView( const std::string& locale, const std::string& timezoneId )
   }
 }
 
+WebView::WebView( int argc, char** argv )
+: Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
+  mUrl(),
+  mVisual(),
+  mWebViewSize( Stage::GetCurrent().GetSize() ),
+  mWebEngine(),
+  mPageLoadStartedSignal(),
+  mPageLoadFinishedSignal(),
+  mPageLoadErrorSignal(),
+  mVideoHoleEnabled( true ),
+  mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
+{
+  mWebEngine = Dali::WebEngine::New();
+
+  // WebEngine is empty when it is not properly initialized.
+  if ( mWebEngine )
+  {
+    mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
+  }
+}
+
 WebView::WebView()
 : WebView( "", "" )
 {
@@ -109,6 +135,10 @@ WebView::WebView()
 
 WebView::~WebView()
 {
+  if( mWebEngine )
+  {
+    mWebEngine.Destroy();
+  }
 }
 
 Toolkit::WebView WebView::New()
@@ -129,10 +159,28 @@ Toolkit::WebView WebView::New( const std::string& locale, const std::string& tim
   return handle;
 }
 
+Toolkit::WebView WebView::New( int argc, char** argv )
+{
+  WebView* impl = new WebView( argc, argv );
+  Toolkit::WebView handle = Toolkit::WebView( *impl );
+
+  impl->Initialize();
+  return handle;
+}
+
 void WebView::OnInitialize()
 {
-  Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
-  Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
+  Actor self = Self();
+
+  self.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
+  self.TouchedSignal().Connect( this, &WebView::OnTouchEvent );
+
+  mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
+  mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
+  mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
+  mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
+  mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
+  mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
 
   if( mWebEngine )
   {
@@ -168,6 +216,19 @@ Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
   return mWebBackForwardList.get();
 }
 
+Dali::Toolkit::ImageView& WebView::GetFavicon()
+{
+  if ( mWebEngine )
+  {
+    Dali::PixelData pixelData = mWebEngine.GetFavicon();
+    std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
+    mFaviconView = Dali::Toolkit::ImageView::New( url );
+    mFaviconView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+    mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
+  }
+  return mFaviconView;
+}
+
 void WebView::LoadUrl( const std::string& url )
 {
   mUrl = url;
@@ -185,6 +246,11 @@ void WebView::LoadUrl( const std::string& url )
       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
       mWebEngine.LoadUrl( url );
     }
+
+    if ( mVideoHoleEnabled )
+    {
+      EnableBlendMode( false );
+    }
   }
 }
 
@@ -203,6 +269,11 @@ void WebView::LoadHtmlString( const std::string& htmlString )
       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
       mWebEngine.LoadHtmlString( htmlString );
     }
+
+    if ( mVideoHoleEnabled )
+    {
+      EnableBlendMode( false );
+    }
   }
 }
 
@@ -288,6 +359,14 @@ void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName,
   }
 }
 
+void WebView::ClearAllTilesResources()
+{
+  if( mWebEngine )
+  {
+    mWebEngine.ClearAllTilesResources();
+  }
+}
+
 void WebView::ClearHistory()
 {
   if( mWebEngine )
@@ -296,6 +375,59 @@ void WebView::ClearHistory()
   }
 }
 
+void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
+{
+  if( !mWebEngine )
+    return;
+
+  Actor self( Self() );
+
+  bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
+  Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
+  Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
+  Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
+
+  Dali::Rect< int > displayArea;
+  displayArea.x = screenPosition.x - anchorPointOffSet.x;
+  displayArea.y = screenPosition.y - anchorPointOffSet.y;
+  displayArea.width = actorSize.x;
+  displayArea.height = actorSize.y;
+
+  Size displaySize = Size( displayArea.width, displayArea.height );
+  if ( mWebViewSize != displaySize )
+  {
+    mWebViewSize = displaySize;
+  }
+
+  if (mWebViewArea != displayArea )
+  {
+    mWebViewArea = displayArea;
+    mWebEngine.UpdateDisplayArea( mWebViewArea );
+  }
+}
+
+void WebView::EnableVideoHole( bool enabled )
+{
+  mVideoHoleEnabled = enabled;
+
+  EnableBlendMode( !mVideoHoleEnabled );
+
+  if( mWebEngine )
+  {
+    mWebEngine.EnableVideoHole( mVideoHoleEnabled );
+  }
+}
+
+void WebView::EnableBlendMode( bool blendEnabled )
+{
+  Actor self = Self();
+  for (uint32_t i = 0; i < self.GetRendererCount(); i++)
+  {
+    Dali::Renderer render = self.GetRendererAt( i );
+    render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
+  }
+}
+
 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
 {
   return mPageLoadStartedSignal;
@@ -395,19 +527,11 @@ Vector3 WebView::GetNaturalSize()
   return Vector3( mWebViewSize );
 }
 
-void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
+void WebView::OnSceneConnection( int depth )
 {
-  Control::OnRelayout( size, container );
-
-  if( size.width > 0 && size.height > 0 && mWebViewSize != size )
-  {
-    mWebViewSize = size;
+  Control::OnSceneConnection( depth );
 
-    if( mWebEngine )
-    {
-      mWebEngine.SetSize( size.width, size.height );
-    }
-  }
+  EnableBlendMode( !mVideoHoleEnabled );
 }
 
 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
@@ -446,6 +570,15 @@ void WebView::SetProperty( BaseObject* object, Property::Index index, const Prop
         }
         break;
       }
+      case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
+      {
+        bool input;
+        if( value.Get( input ) )
+        {
+          impl.EnableVideoHole( input );
+        }
+        break;
+      }
     }
   }
 }
@@ -473,23 +606,27 @@ Property::Value WebView::GetProperty( BaseObject* object, Property::Index proper
       }
       case Toolkit::WebView::Property::SCROLL_POSITION:
       {
-        int x, y;
-        impl.GetScrollPosition( x, y );
-        value = Vector2( x, y );
+        value = impl.GetScrollPosition();
         break;
       }
       case Toolkit::WebView::Property::SCROLL_SIZE:
       {
-        int width, height;
-        impl.GetScrollSize( width, height );
-        value = Vector2( width, height );
+        value = impl.GetScrollSize();
         break;
       }
       case Toolkit::WebView::Property::CONTENT_SIZE:
       {
-        int width, height;
-        impl.GetContentSize( width, height );
-        value = Vector2( width, height );
+        value = impl.GetContentSize();
+        break;
+      }
+      case Toolkit::WebView::Property::TITLE:
+      {
+        value = impl.GetTitle();
+        break;
+      }
+      case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
+      {
+        value = impl.mVideoHoleEnabled;
         break;
       }
       default:
@@ -550,28 +687,24 @@ void WebView::SetScrollPosition( int x, int y )
   }
 }
 
-void WebView::GetScrollPosition( int& x, int& y ) const
+Dali::Vector2 WebView::GetScrollPosition() const
 {
-  if( mWebEngine )
-  {
-    mWebEngine.GetScrollPosition( x, y );
-  }
+  return mWebEngine ? mWebEngine.GetScrollPosition() : Dali::Vector2::ZERO;
 }
 
-void WebView::GetScrollSize( int& width, int& height ) const
+Dali::Vector2 WebView::GetScrollSize() const
 {
-  if( mWebEngine )
-  {
-    mWebEngine.GetScrollSize( width, height );
-  }
+  return mWebEngine ? mWebEngine.GetScrollSize() : Dali::Vector2::ZERO;
 }
 
-void WebView::GetContentSize( int& width, int& height ) const
+Dali::Vector2 WebView::GetContentSize() const
 {
-  if( mWebEngine )
-  {
-    mWebEngine.GetContentSize( width, height );
-  }
+  return mWebEngine ? mWebEngine.GetContentSize() : Dali::Vector2::ZERO;
+}
+
+std::string WebView::GetTitle() const
+{
+  return mWebEngine ?  mWebEngine.GetTitle() : kEmptyString;
 }
 
 const std::string& WebView::GetUserAgent() const