Merge branch 'devel/master' into devel/new_mesh
[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/default-controls/solid-color-actor.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
29 #include <cfloat>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42 const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
43
44 BaseHandle Create()
45 {
46   return Toolkit::TextSelectionToolbar::New();
47 }
48
49 // Setup properties, signals and actions using the type-registry.
50
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
52
53 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "max-size", VECTOR2, MAX_SIZE )
54
55 DALI_TYPE_REGISTRATION_END()
56
57 } // namespace
58
59 Dali::Toolkit::TextSelectionToolbar TextSelectionToolbar::New()
60 {
61   // Create the implementation, temporarily owned by this handle on stack
62   IntrusivePtr< TextSelectionToolbar > impl = new TextSelectionToolbar();
63
64   // Pass ownership to CustomActor handle
65   Dali::Toolkit::TextSelectionToolbar handle( *impl );
66
67   // Second-phase init of the implementation
68   // This can only be done after the CustomActor connection has been made...
69   impl->Initialize();
70
71   return handle;
72 }
73
74 void TextSelectionToolbar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
75 {
76   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
77
78   if( selectionPopup )
79   {
80     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
81
82     switch( index )
83     {
84       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
85       {
86        impl.SetPopupMaxSize( value.Get< Vector2 >() );
87        break;
88       }
89
90     } // switch
91   } // TextSelectionToolbar
92 }
93
94 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
95 {
96   Property::Value value;
97
98   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
99
100   if( selectionPopup )
101   {
102     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
103
104     switch( index )
105     {
106       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
107       {
108         value = impl.GetPopupMaxSize();
109         break;
110       }
111     } // switch
112   }
113   return value;
114 }
115
116 void TextSelectionToolbar::OnInitialize()
117 {
118   SetUp();
119 }
120
121 void TextSelectionToolbar::OnRelayout( const Vector2& size, RelayoutContainer& container )
122 {
123   float width = std::max ( mTableOfButtons.GetNaturalSize().width, size.width );
124   mRulerX->SetDomain( RulerDomain( 0.0, width, true ) );
125   mScrollView.SetRulerX( mRulerX );
126 }
127
128 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
129 {
130   mMaxSize = maxSize;
131 }
132
133 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
134 {
135   return mMaxSize;
136 }
137
138 void TextSelectionToolbar::SetUpScrollView( Toolkit::ScrollView& scrollView )
139 {
140   scrollView.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
141   scrollView.SetParentOrigin( ParentOrigin::CENTER_LEFT );
142   scrollView.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
143   scrollView.SetMaximumSize( mMaxSize );
144
145   scrollView.SetScrollingDirection( PanGestureDetector::DIRECTION_HORIZONTAL, Degree( 40.0f ) );
146   scrollView.SetAxisAutoLock( true );
147   scrollView.ScrollStartedSignal().Connect( this, &TextSelectionToolbar::OnScrollStarted );
148   scrollView.ScrollCompletedSignal().Connect( this, &TextSelectionToolbar::OnScrollCompleted );
149
150   mRulerX = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
151
152   RulerPtr rulerY = new DefaultRuler();  // IntrusivePtr which is unreferenced when ScrollView is destroyed.
153   rulerY->Disable();
154   scrollView.SetRulerY( rulerY );
155 }
156
157 void TextSelectionToolbar::SetUp()
158 {
159   Actor self = Self();
160   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
161
162   // Create Layer and Stencil.  Layer enable's clipping when content exceed maximum defined width.
163   Layer stencilLayer = Layer::New();
164   stencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
165   stencilLayer.SetParentOrigin( ParentOrigin::CENTER );
166   stencilLayer.SetMaximumSize( mMaxSize );
167
168   ImageActor stencil = CreateSolidColorActor( Color::RED );
169   stencil.SetDrawMode( DrawMode::STENCIL );
170   stencil.SetVisible( true );
171   stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
172   stencil.SetParentOrigin( ParentOrigin::CENTER );
173
174   mScrollView  = Toolkit::ScrollView::New();
175   SetUpScrollView( mScrollView );
176
177   // Toolbar must start with at least one option, adding further options with increase it's size
178   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
179   mTableOfButtons.SetFitHeight( 0 );
180   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER_LEFT );
181   mTableOfButtons.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
182
183
184   stencilLayer.Add( stencil );
185   stencilLayer.Add( mScrollView );
186   mScrollView.Add( mTableOfButtons );
187   self.Add( stencilLayer );
188
189   stencilLayer.RaiseToTop();
190 }
191
192 void TextSelectionToolbar::OnScrollStarted( const Vector2& position )
193 {
194   mTableOfButtons.SetSensitive( false );
195 }
196
197 void TextSelectionToolbar::OnScrollCompleted( const Vector2& position )
198 {
199   mTableOfButtons.SetSensitive( true );
200 }
201
202 void TextSelectionToolbar::AddOption( Actor& option )
203 {
204   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
205   mTableOfButtons.SetFitWidth( mIndexInTable );
206   mIndexInTable++;
207 }
208
209 void TextSelectionToolbar::AddDivider( Actor& divider )
210 {
211   AddOption( divider );
212   mDividerIndexes.PushBack( mIndexInTable );
213 }
214
215 void TextSelectionToolbar::ResizeDividers( Size& size )
216 {
217   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
218   {
219     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
220     divider.SetSize( size );
221   }
222   RelayoutRequest();
223 }
224
225 TextSelectionToolbar::TextSelectionToolbar()
226 : Control( ControlBehaviour( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ) ),
227   mMaxSize ( DEFAULT_MAX_SIZE ),
228   mIndexInTable( 0 ),
229   mDividerIndexes()
230 {
231 }
232
233 TextSelectionToolbar::~TextSelectionToolbar()
234 {
235   mRulerX.Reset();
236 }
237
238
239 } // namespace Internal
240
241 } // namespace Toolkit
242
243 } // namespace Dali