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>
27 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
28 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
45 return Toolkit::ToolBar::New();
48 TypeRegistration mType( typeid(Toolkit::ToolBar), typeid(Toolkit::Control), Create );
50 const float DEFAULT_RELATIVE_SIZE( 0.1f );
51 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
54 Toolkit::ToolBar ToolBar::New()
56 // Create the implementation, temporarily owned on stack
57 IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
59 // Pass ownership to Toolkit::View
60 Toolkit::ToolBar toolBar( *internalToolBar );
62 // Second-phase init of the implementation
63 // This can only be done after the CustomActor connection has been made...
64 internalToolBar->Initialize();
69 void ToolBar::SetBackground( Actor background )
71 Lock lock( mInitializing );
74 background.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
75 background.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
76 background.SetSize( mToolBarSize );
78 RenderableActor renderableActor = RenderableActor::DownCast( background );
79 if ( renderableActor )
81 renderableActor.SetSortModifier( 1.f );
84 Self().Add( background );
85 mBackground = background;
88 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
90 // Work out index and update bases and offsets for further insertions.
91 unsigned int index = 0;
94 case Toolkit::Alignment::HorizontalLeft:
102 case Toolkit::Alignment::HorizontalCenter:
104 index = mCenterBase + mCenterOffset;
109 case Toolkit::Alignment::HorizontalRight:
111 index = mRightBase - mRightOffset;
118 DALI_ASSERT_ALWAYS( false );
122 // Create a new column for the new control.
123 mLayout.InsertColumn( index );
125 // Create an alignment container where to place the control.
126 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
127 alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
128 alignmentContainer.SetPadding( padding );
129 alignmentContainer.Add( control );
131 // Insert the control in the table view.
132 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
133 mLayout.SetRelativeWidth( index, relativeSize );
135 // Relate control and alignmentContainer in order to allow removing controls.
136 mControls[control] = alignmentContainer;
138 // Update accumulated relative space.
139 mAccumulatedRelativeSpace += relativeSize;
141 // Update spaces between left, center and right groups of controls.
144 case Toolkit::Alignment::HorizontalLeft:
146 mLeftRelativeSpace -= relativeSize;
147 if ( mLeftRelativeSpace < 0.f )
149 mLeftRelativeSpace = 0.f;
153 case Toolkit::Alignment::HorizontalCenter:
155 mLeftRelativeSpace -= 0.5f * relativeSize;
156 if ( mLeftRelativeSpace < 0.f )
158 mLeftRelativeSpace = 0.f;
160 mRightRelativeSpace -= 0.5f * relativeSize;
161 if ( mRightRelativeSpace < 0.f )
163 mRightRelativeSpace = 0.f;
167 case Toolkit::Alignment::HorizontalRight:
169 mRightRelativeSpace -= relativeSize;
170 if ( mRightRelativeSpace < 0.f )
172 mRightRelativeSpace = 0.f;
178 DALI_ASSERT_ALWAYS( false );
182 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
183 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
186 void ToolBar::RemoveControl( Actor control )
188 Toolkit::TableView::CellPosition position;
190 // Find the alignment where the control is placed.
191 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
193 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
195 // Update accumulated relative space.
196 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
198 // Update spaces between left, center and right groups of controls.
199 if( 1.0 > mAccumulatedRelativeSpace )
201 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
202 if( position.columnIndex < mLeftOffset )
204 alignment = Toolkit::Alignment::HorizontalLeft;
206 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
208 alignment = Toolkit::Alignment::HorizontalCenter;
210 else if( position.columnIndex > mCenterBase + mCenterOffset )
212 alignment = Toolkit::Alignment::HorizontalRight;
216 DALI_ASSERT_ALWAYS( false );
219 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
223 case Toolkit::Alignment::HorizontalLeft:
225 mLeftRelativeSpace += relativeSize;
226 if ( mLeftRelativeSpace < 0.f )
228 mLeftRelativeSpace = 0.f;
232 case Toolkit::Alignment::HorizontalCenter:
234 mLeftRelativeSpace += 0.5f * relativeSize;
235 if ( mLeftRelativeSpace < 0.f )
237 mLeftRelativeSpace = 0.f;
239 mRightRelativeSpace += 0.5f * relativeSize;
240 if ( mRightRelativeSpace < 0.f )
242 mRightRelativeSpace = 0.f;
246 case Toolkit::Alignment::HorizontalRight:
248 mRightRelativeSpace += relativeSize;
249 if ( mRightRelativeSpace < 0.f )
251 mRightRelativeSpace = 0.f;
257 DALI_ASSERT_ALWAYS( false );
260 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
261 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
264 // Remove alignment as parent of control.
265 it->second.Remove( control );
267 // Remove the relationship between control and alignment.
268 mControls.erase( it );
270 // Remove column from tableview.
271 mLayout.DeleteColumn( position.columnIndex );
273 // Update bases and offsets.
274 if( position.columnIndex < mCenterBase )
276 // Control is on the left side. Decrease left offset and center and right bases.
281 else if( position.columnIndex < mCenterBase + mCenterOffset )
283 // Control is on the center side. Decrease center offset and right base.
289 // Control is on the right side. Decrease right base and right offset.
297 : Control( CONTROL_BEHAVIOUR_NONE ),
304 mLeftRelativeSpace( 0.5f ),
305 mRightRelativeSpace( 0.5f ),
306 mAccumulatedRelativeSpace( 0.f ),
307 mInitializing( false ),
316 void ToolBar::OnInitialize()
318 Lock lock( mInitializing );
321 mLayout = Toolkit::TableView::New( 1, 1 );
322 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
324 Self().Add( mLayout );
326 // Add two default actors to create spaces between controls grouped on the left, center and right.
327 Actor leftSpace = Actor::New();
328 Actor rightSpace = Actor::New();
329 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
330 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
331 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
332 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
335 void ToolBar::OnControlChildAdd(Actor& child)
339 // An actor is being added through the Actor's API.
341 // Remove child from tool bar actor and insert it in table view with some 'default' values
342 if ( child && child.GetParent() )
344 child.GetParent().Remove( child );
347 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
350 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
351 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
352 // actor is in mLayout not in Self().
355 void ToolBar::OnControlSizeSet( const Vector3& targetSize )
357 mToolBarSize = targetSize;
358 mBackground.SetSize( mToolBarSize );
361 } // namespace Internal
363 } // namespace Toolkit