Fixed the ItemView already scrolled to end logic
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-toolbar-impl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-selection-toolbar-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cfloat>
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/public-api/object/type-registry-helper.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44
45 const Dali::Vector2 DEFAULT_SCROLL_BAR_PADDING( 8.0f, 6.0f );
46
47 BaseHandle Create()
48 {
49   return Toolkit::TextSelectionToolbar::New();
50 }
51
52 // Setup properties, signals and actions using the type-registry.
53
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
55
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize",  VECTOR2, MAX_SIZE )
57 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot",  BOOLEAN, ENABLE_OVERSHOOT )
58 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableScrollBar", BOOLEAN, ENABLE_SCROLL_BAR )
59 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollBarPadding", VECTOR2, SCROLL_BAR_PADDING )
60 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView",  MAP, SCROLL_VIEW )
61
62 DALI_TYPE_REGISTRATION_END()
63
64 } // namespace
65
66 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
67 {
68   // Create the implementation, temporarily owned by this handle on stack
69   IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
70
71   // Pass ownership to CustomActor handle
72   Dali::Toolkit::TextSelectionToolbar handle( *impl );
73
74   // Second-phase init of the implementation
75   // This can only be done after the CustomActor connection has been made...
76   impl->Initialize();
77
78   return handle;
79 }
80
81 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
82 {
83   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
84
85   if( selectionPopup )
86   {
87     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
88
89     switch( index )
90     {
91       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
92       {
93        impl.SetPopupMaxSize( value.Get< Vector2 >() );
94        break;
95       }
96       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
97       {
98         if( !impl.mScrollView )
99         {
100           impl.mScrollView  = Toolkit::ScrollView::New();
101         }
102         impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
103         break;
104       }
105       case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
106       {
107         impl.SetUpScrollBar( value.Get< bool >() );
108         break;
109       }
110       case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
111       {
112         impl.SetScrollBarPadding( value.Get< Vector2 >() );
113         break;
114       }
115       case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
116       {
117         // Get a Property::Map from the property if possible.
118         Property::Map setPropertyMap;
119         if( value.Get( setPropertyMap ) )
120         {
121           impl.ConfigureScrollview( setPropertyMap );
122         }
123         break;
124       }
125     } // switch
126   } // TextSelectionToolbar
127 }
128
129 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
130 {
131   Property::Value value;
132
133   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
134
135   if( selectionPopup )
136   {
137     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
138
139     switch( index )
140     {
141       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
142       {
143         value = impl.GetPopupMaxSize();
144         break;
145       }
146       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
147       {
148         value = impl.mScrollView.IsOvershootEnabled();
149         break;
150       }
151       case Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR:
152       {
153         value = impl.mScrollBar ? true : false;
154         break;
155       }
156       case Toolkit::TextSelectionToolbar::Property::SCROLL_BAR_PADDING:
157       {
158         value = impl.GetScrollBarPadding();
159         break;
160       }
161     } // switch
162   }
163   return value;
164 }
165
166 void TextSelectionToolbar::OnInitialize()
167 {
168   SetUp();
169 }
170
171 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
172 {
173   float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
174   mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
175   mScrollView.SetRulerX( mRulerX );
176
177   if( mScrollBar )
178   {
179     float barWidth = std::min( mTableOfButtons.GetNaturalSize().width, size.width ) - 2.f * mScrollBarPadding.x;
180     mScrollBar.SetSize( Vector2( 0.0f, barWidth ) );
181   }
182 }
183
184 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
185 {
186   mMaxSize = maxSize;
187   if (mScrollView && mToolbarLayer )
188   {
189     mScrollView.SetMaximumSize( mMaxSize );
190     mToolbarLayer.SetMaximumSize( mMaxSize );
191   }
192 }
193
194 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
195 {
196   return mMaxSize;
197 }
198
199 void TextSelectionToolbar::SetUpScrollView()
200 {
201   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
202   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
203   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
204
205   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
206   mScrollView.SetAxisAutoLock( true );
207   mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
208   mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
209
210   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
211
212   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
213   rulerY->Disable();
214   mScrollView.SetRulerY( rulerY );
215
216   mScrollView.SetOvershootEnabled( true );
217 }
218
219 void TextSelectionToolbar::SetUp()
220 {
221   Actor self = Self();
222
223   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
224
225   // Create Layer to house the toolbar.
226   mToolbarLayer = Layer::New();
227   mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
228   mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
229   mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
230
231   if( !mScrollView )
232   {
233     mScrollView = Toolkit::ScrollView::New();
234   }
235   SetUpScrollView();
236
237   // Toolbar must start with at least one option, adding further options with increase it's size
238   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
239   mTableOfButtons.SetFitHeight( 0 );
240   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
241   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
242
243   mScrollView.Add( mTableOfButtons );
244   mToolbarLayer.Add( mScrollView );
245
246   self.Add( mToolbarLayer );
247 }
248
249 void TextSelectionToolbar::SetUpScrollBar( bool enable )
250 {
251   if( enable )
252   {
253     if( ! mScrollBar )
254     {
255       mScrollBar = Toolkit::ScrollBar::New( Toolkit::ScrollBar::Horizontal );
256       mScrollBar.SetName( "Text popup scroll bar" );
257       mScrollBar.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
258       mScrollBar.SetAnchorPoint( AnchorPoint::TOP_LEFT );
259       mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
260       mScrollBar.SetResizePolicy( Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH );
261       mScrollBar.SetOrientation( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS ) );
262       mScrollBar.GetPanGestureDetector().DetachAll();
263       mScrollView.Add( mScrollBar );
264     }
265   }
266   else
267   {
268     UnparentAndReset( mScrollBar );
269   }
270 }
271
272 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
273 {
274   mTableOfButtons.SetSensitive( false );
275 }
276
277 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
278 {
279   mTableOfButtons.SetSensitive( true );
280 }
281
282 void TextSelectionToolbar::AddOption( Actor& option )
283 {
284   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
285   mTableOfButtons.SetFitWidth( mIndexInTable );
286   mIndexInTable++;
287 }
288
289 void TextSelectionToolbar::AddDivider( Actor& divider )
290 {
291   AddOption( divider );
292   mDividerIndexes.PushBack( mIndexInTable - 1u );
293 }
294
295 void TextSelectionToolbar::ResizeDividers( Size& size )
296 {
297   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
298   {
299     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
300     divider.SetSize( size );
301   }
302   RelayoutRequest();
303 }
304
305 void TextSelectionToolbar::RaiseAbove( Layer target )
306 {
307   mToolbarLayer.RaiseAbove( target );
308 }
309
310 void TextSelectionToolbar::SetScrollBarPadding( const Vector2& padding )
311 {
312   mScrollBarPadding = padding;
313   if( mScrollBar )
314   {
315     mScrollBar.SetPosition( mScrollBarPadding.x, -mScrollBarPadding.y );
316   }
317
318   RelayoutRequest();
319 }
320
321 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
322 {
323   // Set any properties specified for the label by iterating through all property key-value pairs.
324   for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
325   {
326     const StringValuePair& propertyPair( properties.GetPair( i ) );
327
328     // Convert the property string to a property index.
329     Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
330     if( setPropertyIndex != Property::INVALID_INDEX )
331     {
332       // If the conversion worked, we have a valid property index,
333       // Set the property to the new value.
334       mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
335     }
336   }
337
338   RelayoutRequest();
339 }
340
341 const Vector2& TextSelectionToolbar::GetScrollBarPadding() const
342 {
343   return mScrollBarPadding;
344 }
345
346 TextSelectionToolbar::TextSelectionToolbar()
347 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
348   mMaxSize (),
349   mScrollBarPadding( DEFAULT_SCROLL_BAR_PADDING ),
350   mIndexInTable( 0 ),
351   mDividerIndexes()
352 {
353 }
354
355 TextSelectionToolbar::~TextSelectionToolbar()
356 {
357   mRulerX.Reset();
358 }
359
360 } // namespace Internal
361
362 } // namespace Toolkit
363
364 } // namespace Dali