3ff40b373a2a4661788aa38501ad7f1c11432e15
[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 <iostream>
30 #include <libintl.h>
31 #include <cfloat>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 const Dali::Vector2 DEFAULT_MAX_SIZE( 400.0f, 65.0f ); ///< The maximum size of the Toolbar.
45
46 BaseHandle Create()
47 {
48   return Toolkit::TextSelectionToolbar::New();
49 }
50
51 // Setup properties, signals and actions using the type-registry.
52
53 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionToolbar, Toolkit::Control, Create );
54
55 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionToolbar, "max-size", VECTOR2, MAX_SIZE )
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
92     } // switch
93   } // TextSelectionToolbar
94 }
95
96 Property::Value TextSelectionToolbar::GetProperty( BaseObject* object, Property::Index index )
97 {
98   Property::Value value;
99
100   Toolkit::TextSelectionToolbar selectionPopup = Toolkit::TextSelectionToolbar::DownCast( Dali::BaseHandle( object ) );
101
102   if( selectionPopup )
103   {
104     TextSelectionToolbar& impl( GetImpl( selectionPopup ) );
105
106     switch( index )
107     {
108       case Toolkit::TextSelectionToolbar::Property::MAX_SIZE:
109       {
110         value = impl.GetPopupMaxSize();
111         break;
112       }
113     } // switch
114   }
115   return value;
116 }
117
118 void TextSelectionToolbar::OnInitialize()
119 {
120   SetUp();
121 }
122
123 void TextSelectionToolbar::SetPopupMaxSize( const Size& maxSize )
124 {
125   mMaxSize = maxSize;
126 }
127
128 const Dali::Vector2& TextSelectionToolbar::GetPopupMaxSize() const
129 {
130   return mMaxSize;
131 }
132
133 void TextSelectionToolbar::SetUp()
134 {
135   Actor self = Self();
136   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
137
138   // Create Layer and Stencil.
139   mStencilLayer = Layer::New();
140   mStencilLayer.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
141   mStencilLayer.SetParentOrigin( ParentOrigin::CENTER );
142   mStencilLayer.SetMaximumSize( mMaxSize );
143
144   ImageActor stencil = CreateSolidColorActor( Color::RED );
145   stencil.SetDrawMode( DrawMode::STENCIL );
146   stencil.SetVisible( true );
147   stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
148   stencil.SetParentOrigin( ParentOrigin::CENTER );
149
150   Actor scrollview = Actor::New(); //todo make a scrollview
151   scrollview.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
152   scrollview.SetParentOrigin( ParentOrigin::CENTER );
153
154   // Toolbar needs at least one option, adding further options with increase it's size
155   mTableOfButtons = Dali::Toolkit::TableView::New( 1, 1 );
156   mTableOfButtons.SetFitHeight( 0 );
157   mTableOfButtons.SetParentOrigin( ParentOrigin::CENTER );
158
159   mStencilLayer.Add( stencil );
160   mStencilLayer.Add( scrollview );
161   scrollview.Add( mTableOfButtons );
162   self.Add( mStencilLayer );
163
164   mStencilLayer.RaiseToTop();
165 }
166
167 void TextSelectionToolbar::AddOption( Actor& option )
168 {
169   mTableOfButtons.AddChild( option, Toolkit::TableView::CellPosition( 0, mIndexInTable )  );
170   mTableOfButtons.SetFitWidth( mIndexInTable );
171   mIndexInTable++;
172 }
173
174 void TextSelectionToolbar::AddDivider( Actor& divider )
175 {
176   AddOption( divider );
177   mDividerIndexes.PushBack( mIndexInTable );
178 }
179
180 void TextSelectionToolbar::ResizeDividers( Size& size )
181 {
182   for( unsigned int i = 0; i < mDividerIndexes.Count(); ++i )
183   {
184     Actor divider = mTableOfButtons.GetChildAt( Toolkit::TableView::CellPosition( 0, mDividerIndexes[ i ] ) );
185     divider.SetSize( size );
186   }
187   RelayoutRequest();
188 }
189
190 TextSelectionToolbar::TextSelectionToolbar()
191 : Control( ControlBehaviour( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ) ),
192   mMaxSize ( DEFAULT_MAX_SIZE ),
193   mIndexInTable( 0 ),
194   mDividerIndexes()
195 {
196 }
197
198 TextSelectionToolbar::~TextSelectionToolbar()
199 {
200 }
201
202
203 } // namespace Internal
204
205 } // namespace Toolkit
206
207 } // namespace Dali