Property refactor in dali-toolkit: Toolkit changes
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / popup / popup-impl.cpp
index 8f1c9ef..ffd79e4 100755 (executable)
@@ -1,36 +1,74 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
+// CLASS HEADER
 #include <dali-toolkit/internal/controls/popup/popup-impl.h>
 
+// EXTERNAL INCLUDES
+#include <dali/public-api/adaptor-framework/key.h>
+#include <dali/public-api/adaptor-framework/physical-keyboard.h>
+#include <dali/public-api/animation/constraints.h>
+#include <dali/public-api/common/stage.h>
+#include <dali/public-api/events/key-event.h>
+#include <dali/public-api/events/touch-event.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/controls/buttons/button.h>
-#include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
 #include <dali-toolkit/public-api/controls/control-impl.h>
-
+#include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
+#include <dali-toolkit/public-api/focus-manager/focus-manager.h>
 #include <dali-toolkit/internal/controls/relayout-helper.h>
 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
 
-#include <dali-toolkit/public-api/focus-manager/focus-manager.h>
-#include <dali/integration-api/debug.h>
-
 using namespace Dali;
-using namespace std;
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
 
 namespace
 {
+
+BaseHandle Create()
+{
+  return Toolkit::Popup::New();
+}
+
+// Setup properties, signals and actions using the type-registry.
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Popup, Toolkit::Control, Create )
+
+DALI_SIGNAL_REGISTRATION( Popup, "touched-outside", SIGNAL_TOUCHED_OUTSIDE )
+DALI_SIGNAL_REGISTRATION( Popup, "hidden",          SIGNAL_HIDDEN          )
+
+DALI_TYPE_REGISTRATION_END()
+
+// Properties
+const char* const PROPERTY_TITLE = "title";
+const char* const PROPERTY_STATE = "state";
+
 const float CONTENT_DEPTH = 1.0f;                                 ///< 3D Effect of buttons/title etc. appearing off the popup.
 const float POPUP_ANIMATION_DURATION = 0.5f;                      ///< Duration of hide/show animations
 const float BACKING_DEPTH = -1.0f;                                ///< Depth of backing (positioned just behind dialog, so dialog catches hit events first)
@@ -43,109 +81,41 @@ 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";
-
-// Constraints ///////////////////////////////////////////////////////////////////////////
-
 /**
- * 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 )
-  {
-  }
-
-  /**
-   * (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;
+  Vector3 size( parentSize );
+  size.width += outerBoarder.x + outerBoarder.y;
+  size.height += outerBoarder.z + outerBoarder.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;
-  }
-
-  const Vector4 mOuterBorder;  ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
-};
-
-} // unnamed namespace
-
-namespace Dali
-{
-
-namespace Toolkit
-{
-
-namespace Internal
-{
-
-namespace
-{
-
-BaseHandle Create()
+Vector3 ButtonAreaSize( const Vector4& outBoarder, const Vector3& parentSize )
 {
-  return Toolkit::Popup::New();
-}
-
-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 );
-
+  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;
 
+  return size;
 }
 
+} // unnamed namespace
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Popup
@@ -169,11 +139,13 @@ Dali::Toolkit::Popup Popup::New()
 }
 
 Popup::Popup(PopupStyle& style)
-: ControlImpl(true),
+: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
   mShowing(false),
   mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
   mAlterAddedChild(false),
-  mPopupStyle(PopupStylePtr(&style))
+  mPopupStyle(PopupStylePtr(&style)),
+  mPropertyTitle(Property::INVALID_INDEX),
+  mPropertyState(Property::INVALID_INDEX)
 {
   SetKeyboardNavigationSupport( true );
 }
@@ -188,13 +160,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
@@ -370,9 +340,9 @@ 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;
+    const Vector3 anchorPoint = AnchorPoint::BOTTOM_RIGHT - position;
 
     mTailImage.SetParentOrigin(position);
     mTailImage.SetAnchorPoint(anchorPoint);
@@ -399,12 +369,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 );
@@ -531,13 +501,13 @@ void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration
 
     if(mShowing)
     {
-      mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
-      mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
+      mAnimation.AnimateTo( Property(mBacking, Actor::Property::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
+      mAnimation.AnimateTo( Property(mPopupBg, Actor::Property::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
     }
     else
     {
-      mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
-      mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
+      mAnimation.AnimateTo( Property(mBacking, Actor::Property::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
+      mAnimation.AnimateTo( Property(mPopupBg, Actor::Property::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
     }
     mAnimation.Play();
     mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
@@ -561,18 +531,18 @@ void Popup::HandleStateChangeComplete()
 
     // Guard against destruction during signal emission
     Toolkit::Popup handle( GetOwner() );
-    mHiddenSignalV2.Emit();
+    mHiddenSignal.Emit();
   }
 }
 
-Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
+Toolkit::Popup::TouchedOutsideSignalType& Popup::OutsideTouchedSignal()
 {
-  return mTouchedOutsideSignalV2;
+  return mTouchedOutsideSignal;
 }
 
-Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
+Toolkit::Popup::HiddenSignalType& Popup::HiddenSignal()
 {
-  return mHiddenSignalV2;
+  return mHiddenSignal;
 }
 
 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
@@ -580,13 +550,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 );
   }
@@ -615,7 +585,7 @@ bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
       // Guard against destruction during signal emission
       Toolkit::Popup handle( GetOwner() );
 
-      mTouchedOutsideSignalV2.Emit();
+      mTouchedOutsideSignal.Emit();
     }
   }
 
@@ -655,7 +625,24 @@ void Popup::OnControlChildAdd( Actor& child )
   }
 }
 
-void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
+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
   Vector2 popupSize;
@@ -672,13 +659,7 @@ void Popup::OnRelaidOut( 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 );
@@ -693,13 +674,7 @@ void Popup::OnRelaidOut( 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 );
@@ -732,16 +707,11 @@ void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
   // Relayout content
   if( mContent )
   {
-    Vector2 contentSize;
-    contentSize.width = popupSize.width;
-
-    Toolkit::Control control = Toolkit::Control::DownCast( mContent );
-    if( control )
-    {
-      contentSize.height = control.GetHeightForWidth( contentSize.width );
-    }
-    else
+    // If the content width is greater than popup width then scale it down/wrap text as needed
+    Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
+    if( contentSize.width > popupSize.width )
     {
+      contentSize.width = popupSize.width;
       contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
     }
 
@@ -826,29 +796,44 @@ bool Popup::OnKeyEvent(const KeyEvent& event)
 
 Vector3 Popup::GetNaturalSize()
 {
-  Vector3 naturalSize;
+  float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
+  const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
 
-  if ( mTitle )
-  {
-    naturalSize += mTitle.GetImplementation().GetNaturalSize();
-    naturalSize.height += mPopupStyle->margin;
-  }
+  Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
 
-  if( mContent )
+  if ( mTitle )
   {
-    Vector3 contentSize;
+    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;
 
-    Toolkit::Control control = Toolkit::Control::DownCast( mContent );
-    if( control )
+    // As TextView GetNaturalSize does not take wrapping into account, limit the width
+    // to that of the stage
+    if( titleNaturalSize.width >= maxWidth)
     {
-      contentSize = control.GetImplementation().GetNaturalSize();
+      naturalSize.width = maxWidth;
+      naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
     }
     else
     {
-      contentSize = RelayoutHelper::GetNaturalSize( mContent );
+      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;
   }
 
@@ -858,7 +843,6 @@ Vector3 Popup::GetNaturalSize()
   }
 
   // Add the margins
-  float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
   naturalSize.width += margin;
   naturalSize.height += margin;
 
@@ -878,19 +862,7 @@ float Popup::GetHeightForWidth( float width )
 
   if( mContent )
   {
-    float contentHeight;
-
-    Toolkit::Control control = Toolkit::Control::DownCast( mContent );
-    if( control )
-    {
-      contentHeight = control.GetImplementation().GetHeightForWidth( popupWidth );
-    }
-    else
-    {
-      contentHeight = RelayoutHelper::GetHeightForWidth( mContent, popupWidth );
-    }
-
-    height += contentHeight + mPopupStyle->margin;
+    height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
   }
 
   if( !mButtons.empty() )
@@ -910,7 +882,7 @@ float Popup::GetWidthForHeight( float height )
   return GetNaturalSize().width;
 }
 
-Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
+Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
 {
   Actor nextFocusableActor( currentFocusedActor );
 
@@ -953,7 +925,7 @@ Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::K
       {
         switch ( direction )
         {
-          case Control::Left:
+          case Toolkit::Control::Left:
           {
             if ( iter == focusableActors.begin() )
             {
@@ -965,7 +937,7 @@ Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::K
             }
             break;
           }
-          case Control::Right:
+          case Toolkit::Control::Right:
           {
             if ( iter == focusableActors.end() - 1 )
             {
@@ -978,7 +950,7 @@ Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::K
             break;
           }
 
-          case Control::Up:
+          case Toolkit::Control::Up:
           {
             if ( *iter == mContent )
             {
@@ -1005,7 +977,7 @@ Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::K
             break;
           }
 
-          case Control::Down:
+          case Toolkit::Control::Down:
           {
             if ( mContent && mContent.IsKeyboardFocusable() )
             {