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