2 * Copyright (c) 2015 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>
22 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
23 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
26 #include <dali/public-api/images/resource-image.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/public-api/object/property-map.h>
30 #include <dali/devel-api/object/type-registry-helper.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, "max-size", VECTOR2, MAX_SIZE )
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enable-overshoot", BOOLEAN, ENABLE_OVERSHOOT )
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scroll-view", 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::OnStageConnection( int depth )
156 // Call the Control::OnStageConnection() to set the depth of the background.
157 Control::OnStageConnection( depth );
159 // Traverse the dividers and set the depth.
160 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
162 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
164 ImageActor dividerImageActor = ImageActor::DownCast( divider );
165 if( dividerImageActor )
167 dividerImageActor.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
171 // TODO at the moment divider are image actors.
175 // Texts are controls, they have their own OnStageConnection() implementation.
176 // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
179 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
182 if (mScrollView && mStencilLayer )
184 mScrollView.SetMaximumSize( mMaxSize );
185 mStencilLayer.SetMaximumSize( mMaxSize );
189 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
194 void TextSelectionToolbar::SetUpScrollView()
196 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
197 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
198 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
200 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
201 mScrollView.SetAxisAutoLock( true );
202 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
203 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
205 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
207 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
209 mScrollView.SetRulerY( rulerY );
211 mScrollView.SetOvershootEnabled( true );
214 void TextSelectionToolbar::SetUp()
217 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
219 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
220 mStencilLayer = Layer::New();
221 mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
222 mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
224 ImageActor stencil = CreateSolidColorActor( Color::RED );
225 stencil.SetDrawMode( DrawMode::STENCIL );
226 stencil.SetVisible( true );
227 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
228 stencil.SetParentOrigin( ParentOrigin::CENTER );
232 mScrollView = Toolkit::ScrollView::New();
236 // Toolbar must start with at least one option, adding further options with increase it's size
237 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
238 mTableOfButtons.SetFitHeight( 0 );
239 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
240 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
242 mStencilLayer.Add( stencil );
243 mStencilLayer.Add( mScrollView );
244 mScrollView.Add( mTableOfButtons );
245 self.Add( mStencilLayer );
248 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
250 mTableOfButtons.SetSensitive( false );
253 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
255 mTableOfButtons.SetSensitive( true );
258 void TextSelectionToolbar::AddOption( Actor& option )
260 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
261 mTableOfButtons.SetFitWidth( mIndexInTable );
265 void TextSelectionToolbar::AddDivider( Actor& divider )
267 AddOption( divider );
268 mDividerIndexes.PushBack( mIndexInTable - 1u );
271 void TextSelectionToolbar::ResizeDividers( Size& size )
273 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
275 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
276 divider.SetSize( size );
281 void TextSelectionToolbar::RaiseAbove( Layer target )
283 mStencilLayer.RaiseAbove( target );
286 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
288 // Set any properties specified for the label by iterating through all property key-value pairs.
289 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
291 const StringValuePair& propertyPair( properties.GetPair( i ) );
293 // Convert the property string to a property index.
294 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
295 if( setPropertyIndex != Property::INVALID_INDEX )
297 // If the conversion worked, we have a valid property index,
298 // Set the property to the new value.
299 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
307 TextSelectionToolbar::TextSelectionToolbar()
308 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
315 TextSelectionToolbar::~TextSelectionToolbar()
320 } // namespace Internal
322 } // namespace Toolkit