Add Text's popup overshoot property.
[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/devel-api/object/type-registry-helper.h>
30 #include <cfloat>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43 const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
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, "max-size", VECTOR2, MAX_SIZE )
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "enable-overshoot", BOOLEAN, ENABLE_OVERSHOOT )
56
57 DALI_TYPE_REGISTRATION_END()
58
59 } // namespace
60
61 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
62 {
63   // Create the implementation, temporarily owned by this handle on stack
64   IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
65
66   // Pass ownership to CustomActor handle
67   Dali::Toolkit::TextSelectionToolbar handle( *impl );
68
69   // Second-phase init of the implementation
70   // This can only be done after the CustomActor connection has been made...
71   impl->Initialize();
72
73   return handle;
74 }
75
76 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
77 {
78   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
79
80   if( selectionPopup )
81   {
82     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
83
84     switch( index )
85     {
86       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
87       {
88        impl.SetPopupMaxSize( value.Get< Vector2 >() );
89        break;
90       }
91       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
92       {
93         impl.mScrollView.SetOvershootEnabled( value.Get< bool >() );
94         break;
95       }
96     } // switch
97   } // TextSelectionToolbar
98 }
99
100 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
101 {
102   Property::Value value;
103
104   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
105
106   if( selectionPopup )
107   {
108     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
109
110     switch( index )
111     {
112       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
113       {
114         value = impl.GetPopupMaxSize();
115         break;
116       }
117       case Toolkit::TextSelectionToolbar::Property::ENABLE_OVERSHOOT:
118       {
119         value = impl.mScrollView.IsOvershootEnabled();
120         break;
121       }
122     } // switch
123   }
124   return value;
125 }
126
127 void TextSelectionToolbar::OnInitialize()
128 {
129   SetUp();
130 }
131
132 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
133 {
134   float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
135   mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
136   mScrollView.SetRulerX( mRulerX );
137 }
138
139 void TextSelectionToolbar::OnStageConnection( int depth )
140 {
141   // Call the Control::OnStageConnection() to set the depth of the background.
142   Control::OnStageConnection( depth );
143
144   // Traverse the dividers and set the depth.
145   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
146   {
147     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
148
149     ImageActor dividerImageActor = ImageActor::DownCast( divider );
150     if( dividerImageActor )
151     {
152       dividerImageActor.SetSortModifier( DECORATION_DEPTH_INDEX + depth );
153     }
154     else
155     {
156       // TODO at the moment divider are image actors.
157     }
158   }
159
160   // Texts are controls, they have their own OnStageConnection() implementation.
161   // Icons are inside a TableView. It has it's own OnStageConnection() implementation.
162 }
163
164 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
165 {
166   mMaxSize = maxSize;
167   if (mScrollView && mStencilLayer )
168   {
169     mScrollView.SetMaximumSize( mMaxSize );
170     mStencilLayer.SetMaximumSize( mMaxSize );
171   }
172 }
173
174 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
175 {
176   return mMaxSize;
177 }
178
179 void TextSelectionToolbar::SetUpScrollView( Toolkit::ScrollView& scrollView )
180 {
181   scrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
182   scrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
183   scrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
184
185   scrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
186   scrollView.SetAxisAutoLock( true );
187   scrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
188   scrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
189
190   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
191
192   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
193   rulerY->Disable();
194   scrollView.SetRulerY( rulerY );
195 }
196
197 void TextSelectionToolbar::SetUp()
198 {
199   Actor self = Self();
200   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
201
202   // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
203   mStencilLayer = Layer::New();
204   mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
205   mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
206
207   ImageActor stencil = CreateSolidColorActor( Color::RED );
208   stencil.SetDrawMode( DrawMode::STENCIL );
209   stencil.SetVisible( true );
210   stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
211   stencil.SetParentOrigin( ParentOrigin::CENTER );
212
213   mScrollView  = Toolkit::ScrollView::New();
214   SetUpScrollView( mScrollView );
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
223   mStencilLayer.Add( stencil );
224   mStencilLayer.Add( mScrollView );
225   mScrollView.Add( mTableOfButtons );
226   self.Add( mStencilLayer );
227 }
228
229 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
230 {
231   mTableOfButtons.SetSensitive( false );
232 }
233
234 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
235 {
236   mTableOfButtons.SetSensitive( true );
237 }
238
239 void TextSelectionToolbar::AddOption( Actor& option )
240 {
241   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
242   mTableOfButtons.SetFitWidth( mIndexInTable );
243   mIndexInTable++;
244 }
245
246 void TextSelectionToolbar::AddDivider( Actor& divider )
247 {
248   AddOption( divider );
249   mDividerIndexes.PushBack( mIndexInTable - 1u );
250 }
251
252 void TextSelectionToolbar::ResizeDividers( Size& size )
253 {
254   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
255   {
256     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
257     divider.SetSize( size );
258   }
259   RelayoutRequest();
260 }
261
262 void TextSelectionToolbar::RaiseAbove( Layer target )
263 {
264   mStencilLayer.RaiseAbove( target );
265 }
266
267 TextSelectionToolbar::TextSelectionToolbar()
268 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
269   mMaxSize (),
270   mIndexInTable( 0 ),
271   mDividerIndexes()
272 {
273 }
274
275 TextSelectionToolbar::~TextSelectionToolbar()
276 {
277   mRulerX.Reset();
278 }
279
280 } // namespace Internal
281
282 } // namespace Toolkit
283
284 } // namespace Dali