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/default-controls/solid-color-actor.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
42 const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
46 return Toolkit::TextSelectionToolbar::New();
49 // Setup properties, signals and actions using the type-registry.
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
53 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "max-size", VECTOR2, MAX_SIZE )
55 DALI_TYPE_REGISTRATION_END()
59 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
61 // Create the implementation, temporarily owned by this handle on stack
62 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
64 // Pass ownership to CustomActor handle
65 Dali::Toolkit::TextSelectionToolbar handle( *impl );
67 // Second-phase init of the implementation
68 // This can only be done after the CustomActor connection has been made...
74 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
76 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
80 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
84 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
86 impl.SetPopupMaxSize( value.Get< Vector2 >() );
91 } // TextSelectionToolbar
94 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
96 Property::Value value;
98 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
102 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
106 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
108 value = impl.GetPopupMaxSize();
116 void TextSelectionToolbar::OnInitialize()
121 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
123 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
124 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
125 mScrollView.SetRulerX( mRulerX );
128 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
133 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
138 void TextSelectionToolbar::SetUpScrollView( Toolkit::ScrollView& scrollView )
140 scrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
141 scrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
142 scrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
143 scrollView.SetMaximumSize( mMaxSize );
145 scrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
146 scrollView.SetAxisAutoLock( true );
147 scrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
148 scrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
150 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
152 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
154 scrollView.SetRulerY( rulerY );
157 void TextSelectionToolbar::SetUp()
160 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
162 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
163 Layer stencilLayer = Layer::New();
164 stencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
165 stencilLayer.SetParentOrigin( ParentOrigin::CENTER );
166 stencilLayer.SetMaximumSize( mMaxSize );
168 ImageActor stencil = CreateSolidColorActor( Color::RED );
169 stencil.SetDrawMode( DrawMode::STENCIL );
170 stencil.SetVisible( true );
171 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
172 stencil.SetParentOrigin( ParentOrigin::CENTER );
174 mScrollView = Toolkit::ScrollView::New();
175 SetUpScrollView( mScrollView );
177 // Toolbar must start with at least one option, adding further options with increase it's size
178 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
179 mTableOfButtons.SetFitHeight( 0 );
180 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
181 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
184 stencilLayer.Add( stencil );
185 stencilLayer.Add( mScrollView );
186 mScrollView.Add( mTableOfButtons );
187 self.Add( stencilLayer );
189 stencilLayer.RaiseToTop();
192 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
194 mTableOfButtons.SetSensitive( false );
197 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
199 mTableOfButtons.SetSensitive( true );
202 void TextSelectionToolbar::AddOption( Actor& option )
204 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
205 mTableOfButtons.SetFitWidth( mIndexInTable );
209 void TextSelectionToolbar::AddDivider( Actor& divider )
211 AddOption( divider );
212 mDividerIndexes.PushBack( mIndexInTable );
215 void TextSelectionToolbar::ResizeDividers( Size& size )
217 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
219 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
220 divider.SetSize( size );
225 TextSelectionToolbar::TextSelectionToolbar()
226 : Control( ControlBehaviour( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ) ),
227 mMaxSize ( DEFAULT_MAX_SIZE ),
233 TextSelectionToolbar::~TextSelectionToolbar()
239 } // namespace Internal
241 } // namespace Toolkit