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