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>
47 return Toolkit::TextSelectionToolbar::New();
50 // Setup properties, signals and actions using the type-registry.
52 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
54 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize", VECTOR2, MAX_SIZE )
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot", BOOLEAN, ENABLE_OVERSHOOT )
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView", MAP, SCROLL_VIEW )
58 DALI_TYPE_REGISTRATION_END()
62 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
64 // Create the implementation, temporarily owned by this handle on stack
65 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
67 // Pass ownership to CustomActor handle
68 Dali::Toolkit::TextSelectionToolbar handle( *impl );
70 // Second-phase init of the implementation
71 // This can only be done after the CustomActor connection has been made...
77 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
79 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
83 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
87 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
89 impl.SetPopupMaxSize( value.Get< Vector2 >() );
92 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
94 if( !impl.mScrollView )
96 impl.mScrollView = Toolkit::ScrollView::New();
98 impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
101 case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
103 // Get a Property::Map from the property if possible.
104 Property::Map setPropertyMap;
105 if( value.Get( setPropertyMap ) )
107 impl.ConfigureScrollview( setPropertyMap );
112 } // TextSelectionToolbar
115 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
117 Property::Value value;
119 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
123 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
127 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
129 value = impl.GetPopupMaxSize();
132 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
134 value = impl.mScrollView.IsOvershootEnabled();
142 void TextSelectionToolbar::OnInitialize()
147 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
149 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
150 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
151 mScrollView.SetRulerX( mRulerX );
154 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
157 if (mScrollView && mToolbarLayer )
159 mScrollView.SetMaximumSize( mMaxSize );
160 mToolbarLayer.SetMaximumSize( mMaxSize );
164 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
169 void TextSelectionToolbar::SetUpScrollView()
171 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
172 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
173 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
175 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
176 mScrollView.SetAxisAutoLock( true );
177 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
178 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
180 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
182 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
184 mScrollView.SetRulerY( rulerY );
186 mScrollView.SetOvershootEnabled( true );
189 void TextSelectionToolbar::SetUp()
193 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
195 // Create Layer to house the toolbar.
196 mToolbarLayer = Layer::New();
197 mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
198 mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
199 mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
203 mScrollView = Toolkit::ScrollView::New();
207 // Toolbar must start with at least one option, adding further options with increase it's size
208 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
209 mTableOfButtons.SetFitHeight( 0 );
210 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
211 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
213 mScrollView.Add( mTableOfButtons );
214 mToolbarLayer.Add( mScrollView );
216 self.Add( mToolbarLayer );
219 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
221 mTableOfButtons.SetSensitive( false );
224 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
226 mTableOfButtons.SetSensitive( true );
229 void TextSelectionToolbar::AddOption( Actor& option )
231 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
232 mTableOfButtons.SetFitWidth( mIndexInTable );
236 void TextSelectionToolbar::AddDivider( Actor& divider )
238 AddOption( divider );
239 mDividerIndexes.PushBack( mIndexInTable - 1u );
242 void TextSelectionToolbar::ResizeDividers( Size& size )
244 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
246 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
247 divider.SetSize( size );
252 void TextSelectionToolbar::RaiseAbove( Layer target )
254 mToolbarLayer.RaiseAbove( target );
257 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
259 // Set any properties specified for the label by iterating through all property key-value pairs.
260 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
262 const StringValuePair& propertyPair( properties.GetPair( i ) );
264 // Convert the property string to a property index.
265 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
266 if( setPropertyIndex != Property::INVALID_INDEX )
268 // If the conversion worked, we have a valid property index,
269 // Set the property to the new value.
270 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
278 TextSelectionToolbar::TextSelectionToolbar()
279 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
286 TextSelectionToolbar::~TextSelectionToolbar()
291 } // namespace Internal
293 } // namespace Toolkit