b87dab268fcaa2596cbf3cbec6df7a9c98e7cb57
[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 // 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 && mStencilLayer )
167   {
168     mScrollView.SetMaximumSize( mMaxSize );
169     mStencilLayer.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   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
202
203   // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
204   mStencilLayer = Layer::New();
205   mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
206   mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
207
208   BufferImage stencilImage = BufferImage::WHITE(); // ImageView needs an Image or does nothing
209   Toolkit::ImageView stencil = Toolkit::ImageView::New(stencilImage);
210   stencil.SetDrawMode( DrawMode::STENCIL );
211   stencil.SetVisible( true );
212   stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
213   stencil.SetParentOrigin( ParentOrigin::CENTER );
214
215   if ( !mScrollView )
216   {
217     mScrollView  = Toolkit::ScrollView::New();
218   }
219   SetUpScrollView();
220
221   // Toolbar must start with at least one option, adding further options with increase it's size
222   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
223   mTableOfButtons.SetFitHeight( 0 );
224   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
225   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
226
227   mStencilLayer.Add( stencil );
228   mStencilLayer.Add( mScrollView );
229   mScrollView.Add( mTableOfButtons );
230   self.Add( mStencilLayer );
231 }
232
233 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
234 {
235   mTableOfButtons.SetSensitive( false );
236 }
237
238 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
239 {
240   mTableOfButtons.SetSensitive( true );
241 }
242
243 void TextSelectionToolbar::AddOption( Actor& option )
244 {
245   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
246   mTableOfButtons.SetFitWidth( mIndexInTable );
247   mIndexInTable++;
248 }
249
250 void TextSelectionToolbar::AddDivider( Actor& divider )
251 {
252   AddOption( divider );
253   mDividerIndexes.PushBack( mIndexInTable - 1u );
254 }
255
256 void TextSelectionToolbar::ResizeDividers( Size& size )
257 {
258   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
259   {
260     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
261     divider.SetSize( size );
262   }
263   RelayoutRequest();
264 }
265
266 void TextSelectionToolbar::RaiseAbove( Layer target )
267 {
268   mStencilLayer.RaiseAbove( target );
269 }
270
271 void TextSelectionToolbar::ConfigureScrollview( const Property::Map& properties )
272 {
273   // Set any properties specified for the label by iterating through all property key-value pairs.
274   for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
275   {
276     const StringValuePair& propertyPair( properties.GetPair( i ) );
277
278     // Convert the property string to a property index.
279     Property::Index setPropertyIndex = mScrollView.GetPropertyIndex( propertyPair.first );
280     if( setPropertyIndex != Property::INVALID_INDEX )
281     {
282       // If the conversion worked, we have a valid property index,
283       // Set the property to the new value.
284       mScrollView.SetProperty( setPropertyIndex, propertyPair.second );
285     }
286   }
287
288   RelayoutRequest();
289 }
290
291
292 TextSelectionToolbar::TextSelectionToolbar()
293 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
294   mMaxSize (),
295   mIndexInTable( 0 ),
296   mDividerIndexes()
297 {
298 }
299
300 TextSelectionToolbar::~TextSelectionToolbar()
301 {
302   mRulerX.Reset();
303 }
304
305 } // namespace Internal
306
307 } // namespace Toolkit
308
309 } // namespace Dali