2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "tool-bar-impl.h"
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>
28 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
29 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
45 return Toolkit::ToolBar::New();
48 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ToolBar, Toolkit::Control, Create )
49 DALI_TYPE_REGISTRATION_END()
51 const float DEFAULT_RELATIVE_SIZE( 0.1f );
52 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
55 Toolkit::ToolBar ToolBar::New()
57 // Create the implementation, temporarily owned on stack
58 IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
60 // Pass ownership to Toolkit::View
61 Toolkit::ToolBar toolBar( *internalToolBar );
63 // Second-phase init of the implementation
64 // This can only be done after the CustomActor connection has been made...
65 internalToolBar->Initialize();
70 void ToolBar::SetBackground( Actor background )
72 Lock lock( mInitializing );
75 background.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
76 background.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
77 background.SetSize( mToolBarSize );
79 RenderableActor renderableActor = RenderableActor::DownCast( background );
80 if ( renderableActor )
82 renderableActor.SetSortModifier( 1.f );
85 Self().Add( background );
86 mBackground = background;
89 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
91 // Work out index and update bases and offsets for further insertions.
92 unsigned int index = 0;
95 case Toolkit::Alignment::HorizontalLeft:
103 case Toolkit::Alignment::HorizontalCenter:
105 index = mCenterBase + mCenterOffset;
110 case Toolkit::Alignment::HorizontalRight:
112 index = mRightBase - mRightOffset;
119 DALI_ASSERT_ALWAYS( false );
123 // Create a new column for the new control.
124 mLayout.InsertColumn( index );
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 );
132 // Insert the control in the table view.
133 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
134 mLayout.SetRelativeWidth( index, relativeSize );
136 // Relate control and alignmentContainer in order to allow removing controls.
137 mControls[control] = alignmentContainer;
139 // Update accumulated relative space.
140 mAccumulatedRelativeSpace += relativeSize;
142 // Update spaces between left, center and right groups of controls.
145 case Toolkit::Alignment::HorizontalLeft:
147 mLeftRelativeSpace -= relativeSize;
148 if ( mLeftRelativeSpace < 0.f )
150 mLeftRelativeSpace = 0.f;
154 case Toolkit::Alignment::HorizontalCenter:
156 mLeftRelativeSpace -= 0.5f * relativeSize;
157 if ( mLeftRelativeSpace < 0.f )
159 mLeftRelativeSpace = 0.f;
161 mRightRelativeSpace -= 0.5f * relativeSize;
162 if ( mRightRelativeSpace < 0.f )
164 mRightRelativeSpace = 0.f;
168 case Toolkit::Alignment::HorizontalRight:
170 mRightRelativeSpace -= relativeSize;
171 if ( mRightRelativeSpace < 0.f )
173 mRightRelativeSpace = 0.f;
179 DALI_ASSERT_ALWAYS( false );
183 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
184 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
187 void ToolBar::RemoveControl( Actor control )
189 Toolkit::TableView::CellPosition position;
191 // Find the alignment where the control is placed.
192 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
194 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
196 // Update accumulated relative space.
197 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
199 // Update spaces between left, center and right groups of controls.
200 if( 1.0 > mAccumulatedRelativeSpace )
202 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
203 if( position.columnIndex < mLeftOffset )
205 alignment = Toolkit::Alignment::HorizontalLeft;
207 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
209 alignment = Toolkit::Alignment::HorizontalCenter;
211 else if( position.columnIndex > mCenterBase + mCenterOffset )
213 alignment = Toolkit::Alignment::HorizontalRight;
217 DALI_ASSERT_ALWAYS( false );
220 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
224 case Toolkit::Alignment::HorizontalLeft:
226 mLeftRelativeSpace += relativeSize;
227 if ( mLeftRelativeSpace < 0.f )
229 mLeftRelativeSpace = 0.f;
233 case Toolkit::Alignment::HorizontalCenter:
235 mLeftRelativeSpace += 0.5f * relativeSize;
236 if ( mLeftRelativeSpace < 0.f )
238 mLeftRelativeSpace = 0.f;
240 mRightRelativeSpace += 0.5f * relativeSize;
241 if ( mRightRelativeSpace < 0.f )
243 mRightRelativeSpace = 0.f;
247 case Toolkit::Alignment::HorizontalRight:
249 mRightRelativeSpace += relativeSize;
250 if ( mRightRelativeSpace < 0.f )
252 mRightRelativeSpace = 0.f;
258 DALI_ASSERT_ALWAYS( false );
261 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
262 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
265 // Remove alignment as parent of control.
266 it->second.Remove( control );
268 // Remove the relationship between control and alignment.
269 mControls.erase( it );
271 // Remove column from tableview.
272 mLayout.DeleteColumn( position.columnIndex );
274 // Update bases and offsets.
275 if( position.columnIndex < mCenterBase )
277 // Control is on the left side. Decrease left offset and center and right bases.
282 else if( position.columnIndex < mCenterBase + mCenterOffset )
284 // Control is on the center side. Decrease center offset and right base.
290 // Control is on the right side. Decrease right base and right offset.
298 : Control( CONTROL_BEHAVIOUR_NONE ),
305 mLeftRelativeSpace( 0.5f ),
306 mRightRelativeSpace( 0.5f ),
307 mAccumulatedRelativeSpace( 0.f ),
308 mInitializing( false ),
317 void ToolBar::OnInitialize()
319 Lock lock( mInitializing );
322 mLayout = Toolkit::TableView::New( 1, 1 );
323 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
325 Self().Add( mLayout );
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 );
336 void ToolBar::OnControlChildAdd(Actor& child)
340 // An actor is being added through the Actor's API.
342 // Remove child from tool bar actor and insert it in table view with some 'default' values
343 if ( child && child.GetParent() )
345 child.GetParent().Remove( child );
348 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
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().
356 void ToolBar::OnControlSizeSet( const Vector3& targetSize )
358 mToolBarSize = targetSize;
359 mBackground.SetSize( mToolBarSize );
362 } // namespace Internal
364 } // namespace Toolkit