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/devel-api/object/type-registry-helper.h>
43 const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
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 )
56 DALI_TYPE_REGISTRATION_END()
60 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
62 // Create the implementation, temporarily owned by this handle on stack
63 IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
65 // Pass ownership to CustomActor handle
66 Dali::Toolkit::TextSelectionToolbar handle( *impl );
68 // Second-phase init of the implementation
69 // This can only be done after the CustomActor connection has been made...
75 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
77 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
81 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
85 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
87 impl.SetPopupMaxSize( value.Get< Vector2 >() );
92 } // TextSelectionToolbar
95 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
97 Property::Value value;
99 Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
103 TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
107 case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
109 value = impl.GetPopupMaxSize();
117 void TextSelectionToolbar::OnInitialize()
122 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
124 float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
125 mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
126 mScrollView.SetRulerX( mRulerX );
129 void TextSelectionToolbar::OnStageConnection( int depth )
131 // Call the Control::OnStageConnection() to set the depth of the background.
132 Control::OnStageConnection( depth );
134 // Traverse the dividers and set the depth.
135 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
137 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
139 ImageActor dividerImageActor = ImageActor::DownCast( divider );
140 if( dividerImageActor )
142 dividerImageActor.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
146 // TODO at the moment divider are image actors.
150 // Texts are controls, they have their own OnStageConnection() implementation.
151 // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
154 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
159 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
164 void TextSelectionToolbar::SetUpScrollView( Toolkit::ScrollView& scrollView )
166 scrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
167 scrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
168 scrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
169 scrollView.SetMaximumSize( mMaxSize );
171 scrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
172 scrollView.SetAxisAutoLock( true );
173 scrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
174 scrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
176 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
178 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
180 scrollView.SetRulerY( rulerY );
183 void TextSelectionToolbar::SetUp()
186 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
188 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
189 mStencilLayer = Layer::New();
190 mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
191 mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
192 mStencilLayer.SetMaximumSize( mMaxSize );
194 ImageActor stencil = CreateSolidColorActor( Color::RED );
195 stencil.SetDrawMode( DrawMode::STENCIL );
196 stencil.SetVisible( true );
197 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
198 stencil.SetParentOrigin( ParentOrigin::CENTER );
200 mScrollView = Toolkit::ScrollView::New();
201 SetUpScrollView( mScrollView );
203 // Toolbar must start with at least one option, adding further options with increase it's size
204 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
205 mTableOfButtons.SetFitHeight( 0 );
206 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
207 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
210 mStencilLayer.Add( stencil );
211 mStencilLayer.Add( mScrollView );
212 mScrollView.Add( mTableOfButtons );
213 self.Add( mStencilLayer );
216 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
218 mTableOfButtons.SetSensitive( false );
221 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
223 mTableOfButtons.SetSensitive( true );
226 void TextSelectionToolbar::AddOption( Actor& option )
228 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
229 mTableOfButtons.SetFitWidth( mIndexInTable );
233 void TextSelectionToolbar::AddDivider( Actor& divider )
235 AddOption( divider );
236 mDividerIndexes.PushBack( mIndexInTable - 1u );
239 void TextSelectionToolbar::ResizeDividers( Size& size )
241 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
243 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
244 divider.SetSize( size );
249 void TextSelectionToolbar::RaiseAbove( Layer target )
251 mStencilLayer.RaiseAbove( target );
254 TextSelectionToolbar::TextSelectionToolbar()
255 : Control( ControlBehaviour( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ) ),
256 mMaxSize ( DEFAULT_MAX_SIZE ),
262 TextSelectionToolbar::~TextSelectionToolbar()
267 } // namespace Internal
269 } // namespace Toolkit