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>
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/devel-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::OnStageConnection( int depth )
156 // Call the Control::OnStageConnection() to set the depth of the background.
157 Control::OnStageConnection( depth );
159 // Texts are controls, they have their own OnStageConnection() implementation.
160 // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
163 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
166 if (mScrollView && mStencilLayer )
168 mScrollView.SetMaximumSize( mMaxSize );
169 mStencilLayer.SetMaximumSize( mMaxSize );
173 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
178 void TextSelectionToolbar::SetUpScrollView()
180 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
181 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
182 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
184 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
185 mScrollView.SetAxisAutoLock( true );
186 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
187 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
189 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
191 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
193 mScrollView.SetRulerY( rulerY );
195 mScrollView.SetOvershootEnabled( true );
198 void TextSelectionToolbar::SetUp()
201 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
203 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
204 mStencilLayer = Layer::New();
205 mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
206 mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
208 BufferImage stencilImage = BufferImage::WHITE(); // ImageView needs an Image or does nothing
209 Toolkit::ImageView stencil = Toolkit::ImageView::New(stencilImage);
210 stencil.SetDrawMode( DrawMode::STENCIL );
211 stencil.SetVisible( true );
212 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
213 stencil.SetParentOrigin( ParentOrigin::CENTER );
217 mScrollView = Toolkit::ScrollView::New();
221 // Toolbar must start with at least one option, adding further options with increase it's size
222 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
223 mTableOfButtons.SetFitHeight( 0 );
224 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
225 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
227 mStencilLayer.Add( stencil );
228 mStencilLayer.Add( mScrollView );
229 mScrollView.Add( mTableOfButtons );
230 self.Add( mStencilLayer );
233 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
235 mTableOfButtons.SetSensitive( false );
238 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
240 mTableOfButtons.SetSensitive( true );
243 void TextSelectionToolbar::AddOption( Actor& option )
245 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
246 mTableOfButtons.SetFitWidth( mIndexInTable );
250 void TextSelectionToolbar::AddDivider( Actor& divider )
252 AddOption( divider );
253 mDividerIndexes.PushBack( mIndexInTable - 1u );
256 void TextSelectionToolbar::ResizeDividers( Size& size )
258 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
260 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
261 divider.SetSize( size );
266 void TextSelectionToolbar::RaiseAbove( Layer target )
268 mStencilLayer.RaiseAbove( target );
271 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
273 // Set any properties specified for the label by iterating through all property key-value pairs.
274 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
276 const StringValuePair& propertyPair( properties.GetPair( i ) );
278 // Convert the property string to a property index.
279 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
280 if( setPropertyIndex != Property::INVALID_INDEX )
282 // If the conversion worked, we have a valid property index,
283 // Set the property to the new value.
284 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
292 TextSelectionToolbar::TextSelectionToolbar()
293 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
300 TextSelectionToolbar::~TextSelectionToolbar()
305 } // namespace Internal
307 } // namespace Toolkit