Merge "Changed all property & signal names to lowerCamelCase" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-toolbar-impl.cpp
1 /*
2  * Copyright (c) 2015 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 // INTERNAL INCLUDES
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>
24
25 // EXTERNAL INCLUDES
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/public-api/object/property-map.h>
30 #include <dali/devel-api/object/type-registry-helper.h>
31 #include <cfloat>
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   // Traverse the dividers and set the depth.
160   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
161   {
162     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
163
164     ImageActor dividerImageActor = ImageActor::DownCast( divider );
165     if( dividerImageActor )
166     {
167       dividerImageActor.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
168     }
169     else
170     {
171       // TODO at the moment divider are image actors.
172     }
173   }
174
175   // Texts are controls, they have their own OnStageConnection() implementation.
176   // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
177 }
178
179 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
180 {
181   mMaxSize = maxSize;
182   if (mScrollView && mStencilLayer )
183   {
184     mScrollView.SetMaximumSize( mMaxSize );
185     mStencilLayer.SetMaximumSize( mMaxSize );
186   }
187 }
188
189 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
190 {
191   return mMaxSize;
192 }
193
194 void TextSelectionToolbar::SetUpScrollView()
195 {
196   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
197   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
198   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
199
200   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
201   mScrollView.SetAxisAutoLock( true );
202   mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
203   mScrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
204
205   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
206
207   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
208   rulerY->Disable();
209   mScrollView.SetRulerY( rulerY );
210
211   mScrollView.SetOvershootEnabled( true );
212 }
213
214 void TextSelectionToolbar::SetUp()
215 {
216   Actor self = Self();
217   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
218
219   // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
220   mStencilLayer = Layer::New();
221   mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
222   mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
223
224   ImageActor stencil = CreateSolidColorActor( Color::RED );
225   stencil.SetDrawMode( DrawMode::STENCIL );
226   stencil.SetVisible( true );
227   stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
228   stencil.SetParentOrigin( ParentOrigin::CENTER );
229
230   if ( !mScrollView )
231   {
232     mScrollView  = Toolkit::ScrollView::New();
233   }
234   SetUpScrollView();
235
236   // Toolbar must start with at least one option, adding further options with increase it's size
237   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
238   mTableOfButtons.SetFitHeight( 0 );
239   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
240   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
241
242   mStencilLayer.Add( stencil );
243   mStencilLayer.Add( mScrollView );
244   mScrollView.Add( mTableOfButtons );
245   self.Add( mStencilLayer );
246 }
247
248 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
249 {
250   mTableOfButtons.SetSensitive( false );
251 }
252
253 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
254 {
255   mTableOfButtons.SetSensitive( true );
256 }
257
258 void TextSelectionToolbar::AddOption( Actor& option )
259 {
260   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
261   mTableOfButtons.SetFitWidth( mIndexInTable );
262   mIndexInTable++;
263 }
264
265 void TextSelectionToolbar::AddDivider( Actor& divider )
266 {
267   AddOption( divider );
268   mDividerIndexes.PushBack( mIndexInTable - 1u );
269 }
270
271 void TextSelectionToolbar::ResizeDividers( Size& size )
272 {
273   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
274   {
275     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
276     divider.SetSize( size );
277   }
278   RelayoutRequest();
279 }
280
281 void TextSelectionToolbar::RaiseAbove( Layer target )
282 {
283   mStencilLayer.RaiseAbove( target );
284 }
285
286 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
287 {
288   // Set any properties specified for the label by iterating through all property key-value pairs.
289   for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
290   {
291     const StringValuePair& propertyPair( properties.GetPair( i ) );
292
293     // Convert the property string to a property index.
294     Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
295     if( setPropertyIndex != Property::INVALID_INDEX )
296     {
297       // If the conversion worked, we have a valid property index,
298       // Set the property to the new value.
299       mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
300     }
301   }
302
303   RelayoutRequest();
304 }
305
306
307 TextSelectionToolbar::TextSelectionToolbar()
308 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
309   mMaxSize (),
310   mIndexInTable( 0 ),
311   mDividerIndexes()
312 {
313 }
314
315 TextSelectionToolbar::~TextSelectionToolbar()
316 {
317   mRulerX.Reset();
318 }
319
320 } // namespace Internal
321
322 } // namespace Toolkit
323
324 } // namespace Dali