e26614d15158747d6f9e0fec429fe464e6b5c3fa
[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()
180 {
181   mScrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
182   mScrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
183   mScrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
184
185   mScrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
186   mScrollView.SetAxisAutoLock( true );
187   mScrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
188   mScrollView.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   mScrollView.SetRulerY( rulerY );
195
196   mScrollView.SetOvershootEnabled( true );
197 }
198
199 void TextSelectionToolbar::SetUp()
200 {
201   Actor self = Self();
202   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
203
204   // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
205   mStencilLayer = Layer::New();
206   mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
207   mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
208
209   ImageActor stencil = CreateSolidColorActor( Color::RED );
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   mScrollView  = Toolkit::ScrollView::New();
216   SetUpScrollView();
217
218   // Toolbar must start with at least one option, adding further options with increase it's size
219   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
220   mTableOfButtons.SetFitHeight( 0 );
221   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
222   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
223
224
225   mStencilLayer.Add( stencil );
226   mStencilLayer.Add( mScrollView );
227   mScrollView.Add( mTableOfButtons );
228   self.Add( mStencilLayer );
229 }
230
231 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
232 {
233   mTableOfButtons.SetSensitive( false );
234 }
235
236 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
237 {
238   mTableOfButtons.SetSensitive( true );
239 }
240
241 void TextSelectionToolbar::AddOption( Actor& option )
242 {
243   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
244   mTableOfButtons.SetFitWidth( mIndexInTable );
245   mIndexInTable++;
246 }
247
248 void TextSelectionToolbar::AddDivider( Actor& divider )
249 {
250   AddOption( divider );
251   mDividerIndexes.PushBack( mIndexInTable - 1u );
252 }
253
254 void TextSelectionToolbar::ResizeDividers( Size& size )
255 {
256   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
257   {
258     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
259     divider.SetSize( size );
260   }
261   RelayoutRequest();
262 }
263
264 void TextSelectionToolbar::RaiseAbove( Layer target )
265 {
266   mStencilLayer.RaiseAbove( target );
267 }
268
269 TextSelectionToolbar::TextSelectionToolbar()
270 : Control( ControlBehaviour( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ) ),
271   mMaxSize (),
272   mIndexInTable( 0 ),
273   mDividerIndexes()
274 {
275 }
276
277 TextSelectionToolbar::~TextSelectionToolbar()
278 {
279   mRulerX.Reset();
280 }
281
282 } // namespace Internal
283
284 } // namespace Toolkit
285
286 } // namespace Dali