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