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>
45 const Dali::Vector2 DEFAULT_SCROLL_BAR_PADDING( 8.0f, 6.0f );
49 return Toolkit::TextSelectionToolbar::New();
52 // Setup properties, signals and actions using the type-registry.
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize", VECTOR2, MAX_SIZE )
57 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot", BOOLEAN, ENABLE_OVERSHOOT )
58 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableScrollBar", BOOLEAN, ENABLE_SCROLL_BAR )
59 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollBarPadding", VECTOR2, SCROLL_BAR_PADDING )
60 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView", MAP, SCROLL_VIEW )
62 DALI_TYPE_REGISTRATION_END()
66 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
68 // Create the implementation, temporarily owned by this handle on stack
69 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
71 // Pass ownership to CustomActor handle
72 Dali::Toolkit::TextSelectionToolbar handle( *impl );
74 // Second-phase init of the implementation
75 // This can only be done after the CustomActor connection has been made...
81 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
83 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
87 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
91 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
93 impl.SetPopupMaxSize( value.Get< Vector2 >() );
96 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
98 if( !impl.mScrollView )
100 impl.mScrollView = Toolkit::ScrollView::New();
102 impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
105 case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
107 impl.SetUpScrollBar( value.Get< bool >() );
110 case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
112 impl.SetScrollBarPadding( value.Get< Vector2 >() );
115 case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
117 // Get a Property::Map from the property if possible.
118 Property::Map setPropertyMap;
119 if( value.Get( setPropertyMap ) )
121 impl.ConfigureScrollview( setPropertyMap );
126 } // TextSelectionToolbar
129 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
131 Property::Value value;
133 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
137 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
141 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
143 value = impl.GetPopupMaxSize();
146 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
148 value = impl.mScrollView.IsOvershootEnabled();
151 case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
153 value = impl.mScrollBar ? true : false;
156 case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
158 value = impl.GetScrollBarPadding();
166 void TextSelectionToolbar::OnInitialize()
171 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
173 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
174 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
175 mScrollView.SetRulerX( mRulerX );
179 float barWidth = std::min( mTableOfButtons.GetNaturalSize().width, size.width ) - 2.f * mScrollBarPadding.x;
180 mScrollBar.SetSize( Vector2( 0.0f, barWidth ) );
184 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
187 if (mScrollView && mToolbarLayer )
189 mScrollView.SetMaximumSize( mMaxSize );
190 mToolbarLayer.SetMaximumSize( mMaxSize );
194 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
199 void TextSelectionToolbar::SetUpScrollView()
201 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
202 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
203 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
205 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
206 mScrollView.SetAxisAutoLock( true );
207 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
208 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
210 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
212 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
214 mScrollView.SetRulerY( rulerY );
216 mScrollView.SetOvershootEnabled( true );
219 void TextSelectionToolbar::SetUp()
223 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
225 // Create Layer to house the toolbar.
226 mToolbarLayer = Layer::New();
227 mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
228 mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
229 mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
233 mScrollView = Toolkit::ScrollView::New();
237 // Toolbar must start with at least one option, adding further options with increase it's size
238 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
239 mTableOfButtons.SetFitHeight( 0 );
240 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
241 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
243 mScrollView.Add( mTableOfButtons );
244 mToolbarLayer.Add( mScrollView );
246 self.Add( mToolbarLayer );
249 void TextSelectionToolbar::SetUpScrollBar( bool enable )
255 Toolkit::ImageView indicator = Toolkit::ImageView::New();
256 indicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
257 indicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
258 indicator.SetStyleName( "TextSelectionScrollIndicator" );
260 mScrollBar = Toolkit::ScrollBar::New( Toolkit::ScrollBar::Horizontal );
261 mScrollBar.SetName( "Text popup scroll bar" );
262 mScrollBar.SetStyleName( "TextSelectionScrollBar" );
263 mScrollBar.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
264 mScrollBar.SetAnchorPoint( AnchorPoint::TOP_LEFT );
265 mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
266 mScrollBar.SetResizePolicy( Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH );
267 mScrollBar.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
268 mScrollBar.SetScrollIndicator( indicator );
269 mScrollBar.GetPanGestureDetector().DetachAll();
270 mScrollView.Add( mScrollBar );
275 UnparentAndReset( mScrollBar );
279 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
281 mTableOfButtons.SetSensitive( false );
284 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
286 mTableOfButtons.SetSensitive( true );
289 void TextSelectionToolbar::AddOption( Actor& option )
291 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
292 mTableOfButtons.SetFitWidth( mIndexInTable );
296 void TextSelectionToolbar::AddDivider( Actor& divider )
298 AddOption( divider );
299 mDividerIndexes.PushBack( mIndexInTable - 1u );
302 void TextSelectionToolbar::ResizeDividers( Size& size )
304 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
306 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
307 divider.SetSize( size );
312 void TextSelectionToolbar::RaiseAbove( Layer target )
314 mToolbarLayer.RaiseAbove( target );
317 void TextSelectionToolbar::SetScrollBarPadding( const Vector2& padding )
319 mScrollBarPadding = padding;
322 mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
328 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
330 // Set any properties specified for the label by iterating through all property key-value pairs.
331 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
333 const StringValuePair& propertyPair( properties.GetPair( i ) );
335 // Convert the property string to a property index.
336 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
337 if( setPropertyIndex != Property::INVALID_INDEX )
339 // If the conversion worked, we have a valid property index,
340 // Set the property to the new value.
341 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
348 const Vector2& TextSelectionToolbar::GetScrollBarPadding() const
350 return mScrollBarPadding;
353 TextSelectionToolbar::TextSelectionToolbar()
354 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
356 mScrollBarPadding( DEFAULT_SCROLL_BAR_PADDING ),
362 TextSelectionToolbar::~TextSelectionToolbar()
367 } // namespace Internal
369 } // namespace Toolkit