Merge "This patch is for refining dali application to support tizen c# application...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-toolbar-impl.cpp
1 /*
2  * Copyright (c) 2016 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::OnStageConnection( int depth )
155 {
156   // Call the Control::OnStageConnection() to set the depth of the background.
157   Control::OnStageConnection( depth );
158
159   // Texts are controls, they have their own OnStageConnection() implementation.
160   // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
161 }
162
163 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
164 {
165   mMaxSize = maxSize;
166   if (mScrollView && mToolbarLayer )
167   {
168     mScrollView.SetMaximumSize( mMaxSize );
169     mToolbarLayer.SetMaximumSize( mMaxSize );
170   }
171 }
172
173 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
174 {
175   return mMaxSize;
176 }
177
178 void TextSelectionToolbar::SetUpScrollView()
179 {
180   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
181   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
182   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
183
184   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
185   mScrollView.SetAxisAutoLock( true );
186   mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
187   mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
188
189   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
190
191   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
192   rulerY->Disable();
193   mScrollView.SetRulerY( rulerY );
194
195   mScrollView.SetOvershootEnabled( true );
196 }
197
198 void TextSelectionToolbar::SetUp()
199 {
200   Actor self = Self();
201
202   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
203
204   // Create Layer to house the toolbar.
205   mToolbarLayer = Layer::New();
206   mToolbarLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
207   mToolbarLayer.SetAnchorPoint( AnchorPoint::CENTER );
208   mToolbarLayer.SetParentOrigin( ParentOrigin::CENTER );
209
210   if( !mScrollView )
211   {
212     mScrollView = Toolkit::ScrollView::New();
213   }
214   SetUpScrollView();
215
216   // Toolbar must start with at least one option, adding further options with increase it's size
217   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
218   mTableOfButtons.SetFitHeight( 0 );
219   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
220   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
221
222   mScrollView.Add( mTableOfButtons );
223   mToolbarLayer.Add( mScrollView );
224
225   self.Add( mToolbarLayer );
226 }
227
228 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
229 {
230   mTableOfButtons.SetSensitive( false );
231 }
232
233 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
234 {
235   mTableOfButtons.SetSensitive( true );
236 }
237
238 void TextSelectionToolbar::AddOption( Actor& option )
239 {
240   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
241   mTableOfButtons.SetFitWidth( mIndexInTable );
242   mIndexInTable++;
243 }
244
245 void TextSelectionToolbar::AddDivider( Actor& divider )
246 {
247   AddOption( divider );
248   mDividerIndexes.PushBack( mIndexInTable - 1u );
249 }
250
251 void TextSelectionToolbar::ResizeDividers( Size& size )
252 {
253   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
254   {
255     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
256     divider.SetSize( size );
257   }
258   RelayoutRequest();
259 }
260
261 void TextSelectionToolbar::RaiseAbove( Layer target )
262 {
263   mToolbarLayer.RaiseAbove( target );
264 }
265
266 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
267 {
268   // Set any properties specified for the label by iterating through all property key-value pairs.
269   for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
270   {
271     const StringValuePair& propertyPair( properties.GetPair( i ) );
272
273     // Convert the property string to a property index.
274     Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
275     if( setPropertyIndex != Property::INVALID_INDEX )
276     {
277       // If the conversion worked, we have a valid property index,
278       // Set the property to the new value.
279       mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
280     }
281   }
282
283   RelayoutRequest();
284 }
285
286
287 TextSelectionToolbar::TextSelectionToolbar()
288 : Control( ControlBehaviour( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) ),
289   mMaxSize (),
290   mIndexInTable( 0 ),
291   mDividerIndexes()
292 {
293 }
294
295 TextSelectionToolbar::~TextSelectionToolbar()
296 {
297   mRulerX.Reset();
298 }
299
300 } // namespace Internal
301
302 } // namespace Toolkit
303
304 } // namespace Dali