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