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 )
157 if (mScrollView && mStencilLayer )
159 mScrollView.SetMaximumSize( mMaxSize );
160 mStencilLayer.SetMaximumSize( mMaxSize );
164 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
169 void TextSelectionToolbar::SetUpScrollView( Toolkit::ScrollView& scrollView )
171 scrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
172 scrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
173 scrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
175 scrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
176 scrollView.SetAxisAutoLock( true );
177 scrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
178 scrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
180 mRulerX = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
182 RulerPtr rulerY = new DefaultRuler(); // IntrusivePtr which is unreferenced when ScrollView is destroyed.
184 scrollView.SetRulerY( rulerY );
187 void TextSelectionToolbar::SetUp()
190 self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
192 // Create Layer and Stencil. Layer enable's clipping when content exceed maximum defined width.
193 mStencilLayer = Layer::New();
194 mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
195 mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
197 ImageActor stencil = CreateSolidColorActor( Color::RED );
198 stencil.SetDrawMode( DrawMode::STENCIL );
199 stencil.SetVisible( true );
200 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
201 stencil.SetParentOrigin( ParentOrigin::CENTER );
203 mScrollView = Toolkit::ScrollView::New();
204 SetUpScrollView( mScrollView );
206 // Toolbar must start with at least one option, adding further options with increase it's size
207 mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
208 mTableOfButtons.SetFitHeight( 0 );
209 mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
210 mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
213 mStencilLayer.Add( stencil );
214 mStencilLayer.Add( mScrollView );
215 mScrollView.Add( mTableOfButtons );
216 self.Add( mStencilLayer );
219 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
221 mTableOfButtons.SetSensitive( false );
224 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
226 mTableOfButtons.SetSensitive( true );
229 void TextSelectionToolbar::AddOption( Actor& option )
231 mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable ) );
232 mTableOfButtons.SetFitWidth( mIndexInTable );
236 void TextSelectionToolbar::AddDivider( Actor& divider )
238 AddOption( divider );
239 mDividerIndexes.PushBack( mIndexInTable - 1u );
242 void TextSelectionToolbar::ResizeDividers( Size& size )
244 for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
246 Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
247 divider.SetSize( size );
252 void TextSelectionToolbar::RaiseAbove( Layer target )
254 mStencilLayer.RaiseAbove( target );
257 TextSelectionToolbar::TextSelectionToolbar()
258 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
265 TextSelectionToolbar::~TextSelectionToolbar()
270 } // namespace Internal
272 } // namespace Toolkit