[dali_1.2.22] Merge branch 'devel/master'
[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 BaseHandle Create()
46 {
47   return Toolkit::TextSelectionToolbar::New();
48 }
49
50 // Setup properties, signals and actions using the type-registry.
51
52 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
53
54 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "maxSize",  VECTOR2, MAX_SIZE )
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enableOvershoot",  BOOLEAN, ENABLE_OVERSHOOT )
56 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "scrollView",  MAP, SCROLL_VIEW )
57
58 DALI_TYPE_REGISTRATION_END()
59
60 } // namespace
61
62 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
63 {
64   // Create the implementation, temporarily owned by this handle on stack
65   IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
66
67   // Pass ownership to CustomActor handle
68   Dali::Toolkit::TextSelectionToolbar handle( *impl );
69
70   // Second-phase init of the implementation
71   // This can only be done after the CustomActor connection has been made...
72   impl->Initialize();
73
74   return handle;
75 }
76
77 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
78 {
79   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
80
81   if( selectionPopup )
82   {
83     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
84
85     switch( index )
86     {
87       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
88       {
89        impl.SetPopupMaxSize( value.Get< Vector2 >() );
90        break;
91       }
92       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
93       {
94         if( !impl.mScrollView )
95         {
96           impl.mScrollView  = Toolkit::ScrollView::New();
97         }
98         impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
99         break;
100       }
101       case Toolkit::TextSelectionToolbar::Property::SCROLL_VIEW:
102       {
103         // Get a Property::Map from the property if possible.
104         Property::Map setPropertyMap;
105         if( value.Get( setPropertyMap ) )
106         {
107           impl.ConfigureScrollview( setPropertyMap );
108         }
109         break;
110       }
111     } // switch
112   } // TextSelectionToolbar
113 }
114
115 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
116 {
117   Property::Value value;
118
119   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
120
121   if( selectionPopup )
122   {
123     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
124
125     switch( index )
126     {
127       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
128       {
129         value = impl.GetPopupMaxSize();
130         break;
131       }
132       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
133       {
134         value = impl.mScrollView.IsOvershootEnabled();
135         break;
136       }
137     } // switch
138   }
139   return value;
140 }
141
142 void TextSelectionToolbar::OnInitialize()
143 {
144   SetUp();
145 }
146
147 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
148 {
149   float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
150   mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
151   mScrollView.SetRulerX( mRulerX );
152 }
153
154 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
155 {
156   mMaxSize = maxSize;
157   if (mScrollView && mToolbarLayer )
158   {
159     mScrollView.SetMaximumSize( mMaxSize );
160     mToolbarLayer.SetMaximumSize( mMaxSize );
161   }
162 }
163
164 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
165 {
166   return mMaxSize;
167 }
168
169 void TextSelectionToolbar::SetUpScrollView()
170 {
171   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
172   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
173   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
174
175   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
176   mScrollView.SetAxisAutoLock( true );
177   mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
178   mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
179
180   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
181
182   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
183   rulerY->Disable();
184   mScrollView.SetRulerY( rulerY );
185
186   mScrollView.SetOvershootEnabled( true );
187 }
188
189 void TextSelectionToolbar::SetUp()
190 {
191   Actor self = Self();
192
193   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
194
195   // Create Layer to house the toolbar.
196   mToolbarLayer = Layer::New();
197   mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
198   mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
199   mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
200
201   if( !mScrollView )
202   {
203     mScrollView = Toolkit::ScrollView::New();
204   }
205   SetUpScrollView();
206
207   // Toolbar must start with at least one option, adding further options with increase it's size
208   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
209   mTableOfButtons.SetFitHeight( 0 );
210   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
211   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
212
213   mScrollView.Add( mTableOfButtons );
214   mToolbarLayer.Add( mScrollView );
215
216   self.Add( mToolbarLayer );
217 }
218
219 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
220 {
221   mTableOfButtons.SetSensitive( false );
222 }
223
224 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
225 {
226   mTableOfButtons.SetSensitive( true );
227 }
228
229 void TextSelectionToolbar::AddOption( Actor& option )
230 {
231   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
232   mTableOfButtons.SetFitWidth( mIndexInTable );
233   mIndexInTable++;
234 }
235
236 void TextSelectionToolbar::AddDivider( Actor& divider )
237 {
238   AddOption( divider );
239   mDividerIndexes.PushBack( mIndexInTable - 1u );
240 }
241
242 void TextSelectionToolbar::ResizeDividers( Size& size )
243 {
244   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
245   {
246     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
247     divider.SetSize( size );
248   }
249   RelayoutRequest();
250 }
251
252 void TextSelectionToolbar::RaiseAbove( Layer target )
253 {
254   mToolbarLayer.RaiseAbove( target );
255 }
256
257 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
258 {
259   // Set any properties specified for the label by iterating through all property key-value pairs.
260   for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
261   {
262     const StringValuePair& propertyPair( properties.GetPair( i ) );
263
264     // Convert the property string to a property index.
265     Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
266     if( setPropertyIndex != Property::INVALID_INDEX )
267     {
268       // If the conversion worked, we have a valid property index,
269       // Set the property to the new value.
270       mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
271     }
272   }
273
274   RelayoutRequest();
275 }
276
277
278 TextSelectionToolbar::TextSelectionToolbar()
279 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
280   mMaxSize (),
281   mIndexInTable( 0 ),
282   mDividerIndexes()
283 {
284 }
285
286 TextSelectionToolbar::~TextSelectionToolbar()
287 {
288   mRulerX.Reset();
289 }
290
291 } // namespace Internal
292
293 } // namespace Toolkit
294
295 } // namespace Dali