Merge "Internal::Control cleanup, remove dead an non-needed methods and adding missin...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / tool-bar / tool-bar-impl.cpp
1 /*
2  * Copyright (c) 2014 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 "tool-bar-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/renderable-actor.h>
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/devel-api/object/type-registry-helper.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41
42 BaseHandle Create()
43 {
44   return Toolkit::ToolBar::New();
45 }
46
47 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ToolBar, Toolkit::Control, Create )
48 DALI_TYPE_REGISTRATION_END()
49
50 const float DEFAULT_RELATIVE_SIZE( 0.1f );
51 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
52 } // namespace
53
54 Toolkit::ToolBar ToolBar::New()
55 {
56   // Create the implementation, temporarily owned on stack
57   IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
58
59   // Pass ownership to Toolkit::Toolbar
60   Toolkit::ToolBar toolBar( *internalToolBar );
61
62   // Second-phase init of the implementation
63   // This can only be done after the CustomActor connection has been made...
64   internalToolBar->Initialize();
65
66   return toolBar;
67 }
68
69 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
70 {
71   // Work out index and update bases and offsets for further insertions.
72   unsigned int index = 0;
73   switch( alignment )
74   {
75     case Toolkit::Alignment::HorizontalLeft:
76     {
77       index = mLeftOffset;
78       ++mLeftOffset;
79       ++mCenterBase;
80       ++mRightBase;
81       break;
82     }
83     case Toolkit::Alignment::HorizontalCenter:
84     {
85       index = mCenterBase + mCenterOffset;
86       ++mCenterOffset;
87       ++mRightBase;
88       break;
89     }
90     case Toolkit::Alignment::HorizontalRight:
91     {
92       index = mRightBase - mRightOffset;
93       ++mRightBase;
94       ++mRightOffset;
95       break;
96     }
97     default:
98     {
99       DALI_ASSERT_ALWAYS( false );
100     }
101   }
102
103   // Create a new column for the new control.
104   mLayout.InsertColumn( index );
105
106   // Create an alignment container where to place the control.
107   Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
108   alignmentContainer.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
109   alignmentContainer.SetPadding( padding );
110   alignmentContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
111   alignmentContainer.Add( control );
112
113   // Insert the control in the table view.
114   mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
115   mLayout.SetRelativeWidth( index, relativeSize );
116
117   // Relate control and alignmentContainer in order to allow removing controls.
118   mControls[control] = alignmentContainer;
119
120   // Update accumulated relative space.
121   mAccumulatedRelativeSpace += relativeSize;
122
123   // Update spaces between left, center and right groups of controls.
124   switch( alignment )
125   {
126     case Toolkit::Alignment::HorizontalLeft:
127     {
128       mLeftRelativeSpace -= relativeSize;
129       if ( mLeftRelativeSpace < 0.f )
130       {
131         mLeftRelativeSpace = 0.f;
132       }
133       break;
134     }
135     case Toolkit::Alignment::HorizontalCenter:
136     {
137       mLeftRelativeSpace -= 0.5f * relativeSize;
138       if ( mLeftRelativeSpace < 0.f )
139       {
140         mLeftRelativeSpace = 0.f;
141       }
142       mRightRelativeSpace -= 0.5f * relativeSize;
143       if ( mRightRelativeSpace < 0.f )
144       {
145         mRightRelativeSpace = 0.f;
146       }
147       break;
148     }
149     case Toolkit::Alignment::HorizontalRight:
150     {
151       mRightRelativeSpace -= relativeSize;
152       if ( mRightRelativeSpace < 0.f )
153       {
154         mRightRelativeSpace = 0.f;
155       }
156       break;
157     }
158     default:
159     {
160       DALI_ASSERT_ALWAYS( false );
161     }
162   }
163
164   mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
165   mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
166 }
167
168 void ToolBar::RemoveControl( Actor control )
169 {
170   Toolkit::TableView::CellPosition position;
171
172   // Find the alignment where the control is placed.
173   std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
174
175   if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
176   {
177     // Update accumulated relative space.
178     mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
179
180     // Update spaces between left, center and right groups of controls.
181     if( 1.0 > mAccumulatedRelativeSpace )
182     {
183       Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
184       if( position.columnIndex < mLeftOffset )
185       {
186         alignment = Toolkit::Alignment::HorizontalLeft;
187       }
188       else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
189       {
190         alignment = Toolkit::Alignment::HorizontalCenter;
191       }
192       else if( position.columnIndex > mCenterBase + mCenterOffset )
193       {
194         alignment = Toolkit::Alignment::HorizontalRight;
195       }
196       else
197       {
198         DALI_ASSERT_ALWAYS( false );
199       }
200
201       float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
202
203       switch( alignment )
204       {
205         case Toolkit::Alignment::HorizontalLeft:
206         {
207           mLeftRelativeSpace += relativeSize;
208           if ( mLeftRelativeSpace < 0.f )
209           {
210             mLeftRelativeSpace = 0.f;
211           }
212           break;
213         }
214         case Toolkit::Alignment::HorizontalCenter:
215         {
216           mLeftRelativeSpace += 0.5f * relativeSize;
217           if ( mLeftRelativeSpace < 0.f )
218           {
219             mLeftRelativeSpace = 0.f;
220           }
221           mRightRelativeSpace += 0.5f * relativeSize;
222           if ( mRightRelativeSpace < 0.f )
223           {
224             mRightRelativeSpace = 0.f;
225           }
226           break;
227         }
228         case Toolkit::Alignment::HorizontalRight:
229         {
230           mRightRelativeSpace += relativeSize;
231           if ( mRightRelativeSpace < 0.f )
232           {
233             mRightRelativeSpace = 0.f;
234           }
235           break;
236         }
237         default:
238         {
239           DALI_ASSERT_ALWAYS( false );
240         }
241       }
242       mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
243       mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
244     }
245
246     // Remove alignment as parent of control.
247     it->second.Remove( control );
248
249     // Remove the relationship between control and alignment.
250     mControls.erase( it );
251
252     // Remove column from tableview.
253     mLayout.DeleteColumn( position.columnIndex );
254
255     // Update bases and offsets.
256     if( position.columnIndex < mCenterBase )
257     {
258       // Control is on the left side. Decrease left offset and center and right bases.
259       --mLeftOffset;
260       --mCenterBase;
261       --mRightBase;
262     }
263     else if( position.columnIndex < mCenterBase + mCenterOffset )
264     {
265       // Control is on the center side. Decrease center offset and right base.
266       --mCenterOffset;
267       --mRightBase;
268     }
269     else
270     {
271       // Control is on the right side. Decrease right base and right offset.
272       --mRightBase;
273       --mRightOffset;
274     }
275   }
276 }
277
278 ToolBar::ToolBar()
279 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
280   mLayout(),
281   mLeftOffset( 0 ),
282   mCenterBase( 1 ),
283   mCenterOffset( 0 ),
284   mRightBase( 2 ),
285   mRightOffset( 0 ),
286   mLeftRelativeSpace( 0.5f ),
287   mRightRelativeSpace( 0.5f ),
288   mAccumulatedRelativeSpace( 0.f ),
289   mInitializing( false ),
290   mControls()
291 {
292 }
293
294 ToolBar::~ToolBar()
295 {
296 }
297
298 void ToolBar::OnInitialize()
299 {
300   Lock lock( mInitializing );
301
302   // Layout
303   mLayout = Toolkit::TableView::New( 1, 1 );
304   mLayout.SetName( "TOOLBAR_LAYOUT" );
305   mLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
306   mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
307
308   Self().Add( mLayout );
309
310   // Add two default actors to create spaces between controls grouped on the left, center and right.
311   Actor leftSpace = Actor::New();
312   Actor rightSpace = Actor::New();
313   mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
314   mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
315   mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
316   mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
317 }
318
319 void ToolBar::OnControlChildAdd(Actor& child)
320 {
321   if( !mInitializing )
322   {
323     // An actor is being added through the Actor's API.
324
325     // Remove child from tool bar actor and insert it in table view with some 'default' values
326     if ( child && child.GetParent() )
327     {
328       child.GetParent().Remove( child );
329     }
330
331     AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
332   }
333
334   // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
335   // actor is removed using the Actor::RemoveChild method it will not remove anything because the
336   // actor is in mLayout not in Self().
337 }
338
339 } // namespace Internal
340
341 } // namespace Toolkit
342
343 } // namespace Dali