Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / popup / popup-impl.cpp
index 1134b6e..83170bf 100755 (executable)
@@ -27,6 +27,7 @@
 #include <dali/public-api/events/touch-event.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/integration-api/debug.h>
+#include <dali/public-api/images/resource-image.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/buttons/button.h>
@@ -52,81 +53,49 @@ const float POPUP_BUTTON_BG_HEIGHT = 96.f;                    ///< Height of But
 const Vector3 DEFAULT_DIALOG_SIZE = Vector3(POPUP_TITLE_WIDTH/POPUP_WIDTH, 0.5f, 0.0f);
 const Vector3 DEFAULT_BOTTOM_SIZE = Vector3(1.0f, 0.2f, 0.0f);
 
-const char* const PROPERTY_TITLE = "title";
-const char* const PROPERTY_STATE = "state";
+// Signals
 
-// Constraints ///////////////////////////////////////////////////////////////////////////
+const char* const SIGNAL_TOUCHED_OUTSIDE = "touched-outside";
+const char* const SIGNAL_HIDDEN =          "hidden";
+
+// Properties
+
+const char* const PROPERTY_TITLE =         "title";
+const char* const PROPERTY_STATE =         "state";
 
 /**
- * BackgroundSizeConstraint
- *
  * The background size should be at least as big as the Dialog.
  * In some cases a background may have graphics which are visible
  * outside of the Dialog, e.g. A Shadow. For this we need to alter
  * the size of Background.
+ *
+ * @param[in] outerBorder The border to extend beyond parent's Size.
+ * @param[in] parentSize  The parent's size
  */
-struct BackgroundSizeConstraint
+Vector3 BackgroundSize(const Vector4& outerBoarder, const Vector3& parentSize)
 {
-  /**
-   * Constraint that sets size to parent's size plus a border.
-   *
-   * @param[in] outerBorder The border to extend beyond parent's Size.
-   */
-  BackgroundSizeConstraint( Vector4 outerBorder )
-  : mOuterBorder( outerBorder )
-  {
-  }
+  Vector3 size( parentSize );
+  size.width += outerBoarder.x + outerBoarder.y;
+  size.height += outerBoarder.z + outerBoarder.w;
 
-  /**
-   * (render thread code)
-   * @param[in] current The current size.
-   * @param[in] parentSizeProperty The parent's size
-   */
-  Vector3 operator()( const Vector3& current,
-                      const PropertyInput& parentSizeProperty )
-  {
-    Vector3 size = parentSizeProperty.GetVector3();
-
-    size.width += mOuterBorder.x + mOuterBorder.y;
-    size.height += mOuterBorder.z + mOuterBorder.w;
-
-    return size;
-  }
-
-  const Vector4 mOuterBorder;  ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
-};
+  return size;
+}
 
-struct ButtonAreaSizeConstraint
-{
   /**
-   * Constraint that sets size to parent's size plus a border.
+   * sets button area size to parent's size plus a border.
    *
    * @param[in] outerBorder The border to extend beyond parent's Size.
+   * @param[in] parentSize  The parent's size
    */
-  ButtonAreaSizeConstraint( Vector4 outerBorder )
-  : mOuterBorder( outerBorder )
-  {
-  }
-
-  /**
-   * (render thread code)
-   * @param[in] current The current size.
-   * @param[in] parentSizeProperty The parent's size
-   */
-  Vector3 operator()( const Vector3& current,
-                      const PropertyInput& parentSizeProperty )
-  {
-    Vector3 size = parentSizeProperty.GetVector3();
-
-    size.width += mOuterBorder.x + mOuterBorder.y;
-    size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
-    size.height = POPUP_BUTTON_BG_HEIGHT;
-
-    return size;
-  }
+Vector3 ButtonAreaSize( const Vector4& outBoarder, const Vector3& parentSize )
+{
+  Vector3 size( parentSize );
+  size.width += outBoarder.x + outBoarder.y;
+  size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
+  size.height = POPUP_BUTTON_BG_HEIGHT;
 
-  const Vector4 mOuterBorder;  ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
-};
+  return size;
+}
 
 } // unnamed namespace
 
@@ -147,10 +116,10 @@ BaseHandle Create()
   return Toolkit::Popup::New();
 }
 
-TypeRegistration typeRegistration( typeid(Toolkit::Popup), typeid(Toolkit::Control), Create );
+TypeRegistration typeRegistration( typeid( Toolkit::Popup ), typeid( Toolkit::Control ), Create );
 
-SignalConnectorType signalConnector1( typeRegistration, Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
-SignalConnectorType signalConnector2( typeRegistration, Toolkit::Popup::SIGNAL_HIDDEN, &Popup::DoConnectSignal );
+SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
+SignalConnectorType signalConnector2( typeRegistration, SIGNAL_HIDDEN, &Popup::DoConnectSignal );
 
 
 }
@@ -199,13 +168,11 @@ void Popup::OnInitialize()
   mLayer.SetParentOrigin(ParentOrigin::CENTER);
   mLayer.SetAnchorPoint(AnchorPoint::CENTER);
   mLayer.RaiseToTop();
-  mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
   self.Add(mLayer);
 
   mPopupBg = Actor::New();
   mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
   mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
-  mPopupBg.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
   mLayer.Add(mPopupBg);
 
   // Any content after this point which is added to Self() will be reparented to
@@ -297,33 +264,12 @@ void Popup::SetButtonAreaImage( Actor image )
 
 void Popup::SetTitle( const std::string& text )
 {
-  Toolkit::TextView titleActor = Toolkit::TextView::New();
-  titleActor.SetText( text );
-  titleActor.SetColor( Color::BLACK );
-  titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
-  titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
-  titleActor.SetLineJustification( Toolkit::TextView::Center );
-
-  SetTitle( titleActor );
-}
-
-void Popup::SetTitle( Toolkit::TextView titleActor )
-{
-  // Replaces the current title actor.
-  if( mTitle && mPopupBg )
-  {
-    mPopupBg.Remove( mTitle );
-  }
-  mTitle = titleActor;
-
-  mPopupBg.Add( mTitle );
-
-  RelayoutRequest();
 }
 
-Toolkit::TextView Popup::GetTitle() const
+const std::string& Popup::GetTitle() const
 {
-  return mTitle;
+  static std::string temp("");
+  return temp;
 }
 
 void Popup::AddButton( Toolkit::Button button )
@@ -381,7 +327,7 @@ void Popup::ShowTail(const Vector3& position)
 
   if(image != "")
   {
-    Image tail = Image::New( image );
+    Image tail = ResourceImage::New( image );
     mTailImage = ImageActor::New(tail);
     const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
 
@@ -410,12 +356,12 @@ PopupStylePtr Popup::GetStyle() const
 
 void Popup::SetDefaultBackgroundImage()
 {
-  Image bg = Image::New( mPopupStyle->backgroundImage );
+  Image bg = ResourceImage::New( mPopupStyle->backgroundImage );
   ImageActor bgImage = ImageActor::New( bg );
   bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
   bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
 
-  Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
+  Image buttonBg = ResourceImage::New( mPopupStyle->buttonAreaImage );
   ImageActor buttonBgImage = ImageActor::New( buttonBg );
   buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
   buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
@@ -591,13 +537,13 @@ bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tra
   Dali::BaseHandle handle( object );
 
   bool connected( true );
-  Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
+  Toolkit::Popup popup = Toolkit::Popup::DownCast( handle );
 
-  if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
+  if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED_OUTSIDE ) )
   {
     popup.OutsideTouchedSignal().Connect( tracker, functor );
   }
-  else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
+  else if( 0 == strcmp( signalName.c_str(), SIGNAL_HIDDEN ) )
   {
     popup.HiddenSignal().Connect( tracker, functor );
   }
@@ -666,6 +612,23 @@ void Popup::OnControlChildAdd( Actor& child )
   }
 }
 
+void Popup::OnControlSizeSet( const Vector3& targetSize )
+{
+  mLayer.SetSize( targetSize );
+  mPopupBg.SetSize( targetSize );
+
+  const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
+  if( mBackgroundImage )
+  {
+    mBackgroundImage.SetSize( BackgroundSize( outerBorder,targetSize ) );
+  }
+  if( mButtonAreaImage )
+  {
+    mButtonAreaImage.SetSize( ButtonAreaSize( outerBorder, targetSize ) );
+  }
+
+}
+
 void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
 {
   // Set the popup size
@@ -683,13 +646,7 @@ void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
 
   if( mBackgroundImage )
   {
-    Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                                      ParentSource( Actor::SIZE ),
-                                                      BackgroundSizeConstraint(outerBorder) );
-
-    mBackgroundImage.RemoveConstraints();
-    mBackgroundImage.ApplyConstraint( constraint );
-
+    mBackgroundImage.SetSize(BackgroundSize(outerBorder, Vector3(size)));
     mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
     mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
     mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
@@ -704,13 +661,7 @@ void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
     }
     else
     {
-      Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
-                                                      ParentSource( Actor::SIZE ),
-                                                      ButtonAreaSizeConstraint(outerBorder) );
-
-      mButtonAreaImage.RemoveConstraints();
-      mButtonAreaImage.ApplyConstraint( constraint );
-
+      mButtonAreaImage.SetSize( ButtonAreaSize(outerBorder, Vector3(size)) );
       mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
       mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
       mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
@@ -721,24 +672,7 @@ void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
 
   // Relayout title
   Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
-  if( mTitle )
-  {
-    Vector2 titleSize;
-    titleSize.width  = popupSize.width;
-    titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
-
-    // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
-    // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
-    // into account the multiline and exceed policies, giving as result a wrong size.
-    mTitle.SetSize( titleSize );
-    Relayout( mTitle, titleSize, container );
-
-    mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    mTitle.SetPosition( positionOffset );
-
-    positionOffset.y += titleSize.height + mPopupStyle->margin;
-  }
+  // TODO
 
   // Relayout content
   if( mContent )
@@ -837,34 +771,16 @@ Vector3 Popup::GetNaturalSize()
 
   Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
 
-  if ( mTitle )
-  {
-    Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
-    // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
-    // It was observed in this case that text wrapping was then inconsistent when seen on device
-    const float titleBuffer = 0.5f;
-    titleNaturalSize.width += titleBuffer;
-
-    // As TextView GetNaturalSize does not take wrapping into account, limit the width
-    // to that of the stage
-    if( titleNaturalSize.width >= maxWidth)
-    {
-      naturalSize.width = maxWidth;
-      naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
-    }
-    else
-    {
-      naturalSize += titleNaturalSize;
-    }
-
-    naturalSize.height += mPopupStyle->margin;
-  }
-
   if( mContent )
   {
     Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
     // Choose the biggest width
     naturalSize.width = std::max( naturalSize.width, contentSize.width );
+    if( naturalSize.width > maxWidth )
+    {
+      naturalSize.width = maxWidth;
+      contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, maxWidth );
+    }
     naturalSize.height += contentSize.height + mPopupStyle->margin;
   }
 
@@ -885,12 +801,6 @@ float Popup::GetHeightForWidth( float width )
   float height( 0.0f );
   float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
 
-  if ( mTitle )
-  {
-    height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
-    height += mPopupStyle->margin;
-  }
-
   if( mContent )
   {
     height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;