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>
24 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
27 #include <dali/public-api/images/buffer-image.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/public-api/object/property-map.h>
31 #include <dali/devel-api/object/type-registry-helper.h>
48 return Toolkit::TextSelectionToolbar::New();
51 // Setup properties, signals and actions using the type-registry.
53 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize", VECTOR2, MAX_SIZE )
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot", BOOLEAN, ENABLE_OVERSHOOT )
57 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView", MAP, SCROLL_VIEW )
59 DALI_TYPE_REGISTRATION_END()
63 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
65 // Create the implementation, temporarily owned by this handle on stack
66 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
68 // Pass ownership to CustomActor handle
69 Dali::Toolkit::TextSelectionToolbar handle( *impl );
71 // Second-phase init of the implementation
72 // This can only be done after the CustomActor connection has been made...
78 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
80 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
84 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
88 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
90 impl.SetPopupMaxSize( value.Get< Vector2 >() );
93 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
95 if( !impl.mScrollView )
97 impl.mScrollView = Toolkit::ScrollView::New();
99 impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
102 case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
104 // Get a Property::Map from the property if possible.
105 Property::Map setPropertyMap;
106 if( value.Get( setPropertyMap ) )
108 impl.ConfigureScrollview( setPropertyMap );
113 } // TextSelectionToolbar
116 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
118 Property::Value value;
120 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
124 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
128 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
130 value = impl.GetPopupMaxSize();
133 case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
135 value = impl.mScrollView.IsOvershootEnabled();
143 void TextSelectionToolbar::OnInitialize()
148 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
150 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
151 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
152 mScrollView.SetRulerX( mRulerX );
155 void TextSelectionToolbar::OnStageConnection( int depth )
157 // Call the Control::OnStageConnection() to set the depth of the background.
158 Control::OnStageConnection( depth );
160 // Texts are controls, they have their own OnStageConnection() implementation.
161 // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
164 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
167 if (mScrollView && mStencilLayer )
169 mScrollView.SetMaximumSize( mMaxSize );
170 mStencilLayer.SetMaximumSize( mMaxSize );
174 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
179 void TextSelectionToolbar::SetUpScrollView()
181 mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
182 mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
183 mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
185 mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
186 mScrollView.SetAxisAutoLock( true );
187 mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
188 mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
190 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
192 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
194 mScrollView.SetRulerY( rulerY );
196 mScrollView.SetOvershootEnabled( true );
199 void TextSelectionToolbar::SetUp()
202 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
204 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
205 mStencilLayer = Layer::New();
206 mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
207 mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
209 BufferImage stencilImage = BufferImage::WHITE(); // ImageView needs an Image or does nothing
210 Toolkit::ImageView stencil = Toolkit::ImageView::New(stencilImage);
211 stencil.SetDrawMode( DrawMode::STENCIL );
212 stencil.SetVisible( true );
213 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
214 stencil.SetParentOrigin( ParentOrigin::CENTER );
218 mScrollView = Toolkit::ScrollView::New();
222 // Toolbar must start with at least one option, adding further options with increase it's size
223 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
224 mTableOfButtons.SetFitHeight( 0 );
225 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
226 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
228 mStencilLayer.Add( stencil );
229 mStencilLayer.Add( mScrollView );
230 mScrollView.Add( mTableOfButtons );
231 self.Add( mStencilLayer );
234 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
236 mTableOfButtons.SetSensitive( false );
239 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
241 mTableOfButtons.SetSensitive( true );
244 void TextSelectionToolbar::AddOption( Actor& option )
246 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
247 mTableOfButtons.SetFitWidth( mIndexInTable );
251 void TextSelectionToolbar::AddDivider( Actor& divider )
253 AddOption( divider );
254 mDividerIndexes.PushBack( mIndexInTable - 1u );
257 void TextSelectionToolbar::ResizeDividers( Size& size )
259 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
261 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
262 divider.SetSize( size );
267 void TextSelectionToolbar::RaiseAbove( Layer target )
269 mStencilLayer.RaiseAbove( target );
272 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
274 // Set any properties specified for the label by iterating through all property key-value pairs.
275 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
277 const StringValuePair& propertyPair( properties.GetPair( i ) );
279 // Convert the property string to a property index.
280 Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
281 if( setPropertyIndex != Property::INVALID_INDEX )
283 // If the conversion worked, we have a valid property index,
284 // Set the property to the new value.
285 mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
293 TextSelectionToolbar::TextSelectionToolbar()
294 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
301 TextSelectionToolbar::~TextSelectionToolbar()
306 } // namespace Internal
308 } // namespace Toolkit