2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/controls/text-controls/text-selection-toolbar-impl.h>
23 #include <dali/public-api/images/buffer-image.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/math/vector4.h>
26 #include <dali/public-api/object/property-map.h>
27 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/internal/helpers/color-conversion.h>
46 const Dali::Vector2 DEFAULT_SCROLL_BAR_PADDING( 8.0f, 6.0f );
50 return Toolkit::TextSelectionToolbar::New();
53 // Setup properties, signals and actions using the type-registry.
55 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
57 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize", VECTOR2, MAX_SIZE )
58 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot", BOOLEAN, ENABLE_OVERSHOOT )
59 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableScrollBar", BOOLEAN, ENABLE_SCROLL_BAR )
60 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollBarPadding", VECTOR2, SCROLL_BAR_PADDING )
61 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView", MAP, SCROLL_VIEW )
63 DALI_TYPE_REGISTRATION_END()
67 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
69 // Create the implementation, temporarily owned by this handle on stack
70 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
72 // Pass ownership to CustomActor handle
73 Dali::Toolkit::TextSelectionToolbar handle( *impl );
75 // Second-phase init of the implementation
76 // This can only be done after the CustomActor connection has been made...
82 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
84 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
88 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
92 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
94 impl.SetPopupMaxSize( value.Get< Vector2 >() );
97 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
99 if( !impl.mScrollView )
101 impl.mScrollView = Toolkit::ScrollView::New();
103 impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
106 case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
108 impl.SetUpScrollBar( value.Get< bool >() );
111 case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
113 impl.SetScrollBarPadding( value.Get< Vector2 >() );
116 case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
118 // Get a Property::Map from the property if possible.
119 Property::Map setPropertyMap;
120 if( value.Get( setPropertyMap ) )
122 impl.ConfigureScrollview( setPropertyMap );
127 } // TextSelectionToolbar
130 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
132 Property::Value value;
134 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
138 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
142 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
144 value = impl.GetPopupMaxSize();
147 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
149 value = impl.mScrollView.IsOvershootEnabled();
152 case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
154 value = impl.mScrollBar ? true : false;
157 case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
159 value = impl.GetScrollBarPadding();
167 void TextSelectionToolbar::OnInitialize()
172 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
174 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
175 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
176 mScrollView.SetRulerX( mRulerX );
180 float barWidth = std::min( mTableOfButtons.GetNaturalSize().width, size.width ) - 2.f * mScrollBarPadding.x;
181 mScrollBar.SetSize( Vector2( 0.0f, barWidth ) );
185 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
188 if (mScrollView && mToolbarLayer )
190 mScrollView.SetMaximumSize( mMaxSize );
191 mToolbarLayer.SetMaximumSize( mMaxSize );
195 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
200 void TextSelectionToolbar::SetUpScrollView()
202 mScrollView.SetName("TextSelectionScrollView");
203 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
204 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
205 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
207 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
208 mScrollView.SetAxisAutoLock( true );
209 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
210 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
212 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
214 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
216 mScrollView.SetRulerY( rulerY );
218 mScrollView.SetOvershootEnabled( true );
221 void TextSelectionToolbar::SetUp()
225 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
227 // Create Layer to house the toolbar.
228 mToolbarLayer = Layer::New();
229 mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
230 mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
231 mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
235 mScrollView = Toolkit::ScrollView::New();
239 // Toolbar must start with at least one option, adding further options with increase it's size
240 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
241 mTableOfButtons.SetFitHeight( 0 );
242 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
243 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
245 mScrollView.Add( mTableOfButtons );
246 mToolbarLayer.Add( mScrollView );
248 self.Add( mToolbarLayer );
251 void TextSelectionToolbar::SetUpScrollBar( bool enable )
257 Toolkit::ImageView indicator = Toolkit::ImageView::New();
258 indicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
259 indicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
260 indicator.SetStyleName( "TextSelectionScrollIndicator" );
262 mScrollBar = Toolkit::ScrollBar::New( Toolkit::ScrollBar::Horizontal );
263 mScrollBar.SetName( "Text popup scroll bar" );
264 mScrollBar.SetStyleName( "TextSelectionScrollBar" );
265 mScrollBar.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
266 mScrollBar.SetAnchorPoint( AnchorPoint::TOP_LEFT );
267 mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
268 mScrollBar.SetResizePolicy( Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH );
269 mScrollBar.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
270 mScrollBar.SetScrollIndicator( indicator );
271 mScrollBar.GetPanGestureDetector().DetachAll();
272 mScrollView.Add( mScrollBar );
277 UnparentAndReset( mScrollBar );
281 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
283 if( mFirstScrollEnd )
285 mScrollView.SetOvershootEnabled( true );
287 mTableOfButtons.SetSensitive( false );
290 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
292 mFirstScrollEnd = true;
293 mTableOfButtons.SetSensitive( true );
296 void TextSelectionToolbar::AddOption( Actor& option )
298 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
299 mTableOfButtons.SetFitWidth( mIndexInTable );
303 void TextSelectionToolbar::AddDivider( Actor& divider )
305 AddOption( divider );
306 mDividerIndexes.PushBack( mIndexInTable - 1u );
309 void TextSelectionToolbar::ResizeDividers( Size& size )
311 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
313 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
314 divider.SetSize( size );
319 void TextSelectionToolbar::RaiseAbove( Layer target )
321 mToolbarLayer.RaiseAbove( target );
324 void TextSelectionToolbar::SetScrollBarPadding( const Vector2& padding )
326 mScrollBarPadding = padding;
329 mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
335 void TextSelectionToolbar::ScrollTo( const Vector2& position )
337 mFirstScrollEnd = false;
338 mScrollView.SetOvershootEnabled( false );
339 mScrollView.ScrollTo( position, 0.f );
342 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
344 // Set any properties specified for the label by iterating through all property key-value pairs.
345 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
347 const StringValuePair& propertyPair( properties.GetPair( i ) );
349 // Convert the property string to a property index.
350 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
351 if( setPropertyIndex != Property::INVALID_INDEX )
353 // Convert the string representation of a color into a Vector4
354 if( setPropertyIndex == Toolkit::Scrollable::Property::OVERSHOOT_EFFECT_COLOR )
357 if( ConvertPropertyToColor( propertyPair.second, color ) )
359 mScrollView.SetOvershootEffectColor( color );
364 // If the conversion worked, we have a valid property index,
365 // Set the property to the new value.
366 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
374 const Vector2& TextSelectionToolbar::GetScrollBarPadding() const
376 return mScrollBarPadding;
379 TextSelectionToolbar::TextSelectionToolbar()
380 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
382 mScrollBarPadding( DEFAULT_SCROLL_BAR_PADDING ),
385 mFirstScrollEnd( false )
389 TextSelectionToolbar::~TextSelectionToolbar()
394 } // namespace Internal
396 } // namespace Toolkit