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