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.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
78 RenderableActor renderableActor = RenderableActor::DownCast( background );
79 if ( renderableActor )
81 renderableActor.SetSortModifier( 1.f );
84 Self().Add( background );
87 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
89 // Work out index and update bases and offsets for further insertions.
90 unsigned int index = 0;
93 case Toolkit::Alignment::HorizontalLeft:
101 case Toolkit::Alignment::HorizontalCenter:
103 index = mCenterBase + mCenterOffset;
108 case Toolkit::Alignment::HorizontalRight:
110 index = mRightBase - mRightOffset;
117 DALI_ASSERT_ALWAYS( false );
121 // Create a new column for the new control.
122 mLayout.InsertColumn( index );
124 // Create an alignment container where to place the control.
125 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
126 alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
127 alignmentContainer.SetPadding( padding );
128 alignmentContainer.Add( control );
130 // Insert the control in the table view.
131 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
132 mLayout.SetRelativeWidth( index, relativeSize );
134 // Relate control and alignmentContainer in order to allow removing controls.
135 mControls[control] = alignmentContainer;
137 // Update accumulated relative space.
138 mAccumulatedRelativeSpace += relativeSize;
140 // Update spaces between left, center and right groups of controls.
143 case Toolkit::Alignment::HorizontalLeft:
145 mLeftRelativeSpace -= relativeSize;
146 if ( mLeftRelativeSpace < 0.f )
148 mLeftRelativeSpace = 0.f;
152 case Toolkit::Alignment::HorizontalCenter:
154 mLeftRelativeSpace -= 0.5f * relativeSize;
155 if ( mLeftRelativeSpace < 0.f )
157 mLeftRelativeSpace = 0.f;
159 mRightRelativeSpace -= 0.5f * relativeSize;
160 if ( mRightRelativeSpace < 0.f )
162 mRightRelativeSpace = 0.f;
166 case Toolkit::Alignment::HorizontalRight:
168 mRightRelativeSpace -= relativeSize;
169 if ( mRightRelativeSpace < 0.f )
171 mRightRelativeSpace = 0.f;
177 DALI_ASSERT_ALWAYS( false );
181 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
182 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
185 void ToolBar::RemoveControl( Actor control )
187 Toolkit::TableView::CellPosition position;
189 // Find the alignment where the control is placed.
190 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
192 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
194 // Update accumulated relative space.
195 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
197 // Update spaces between left, center and right groups of controls.
198 if( 1.0 > mAccumulatedRelativeSpace )
200 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
201 if( position.columnIndex < mLeftOffset )
203 alignment = Toolkit::Alignment::HorizontalLeft;
205 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
207 alignment = Toolkit::Alignment::HorizontalCenter;
209 else if( position.columnIndex > mCenterBase + mCenterOffset )
211 alignment = Toolkit::Alignment::HorizontalRight;
215 DALI_ASSERT_ALWAYS( false );
218 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
222 case Toolkit::Alignment::HorizontalLeft:
224 mLeftRelativeSpace += relativeSize;
225 if ( mLeftRelativeSpace < 0.f )
227 mLeftRelativeSpace = 0.f;
231 case Toolkit::Alignment::HorizontalCenter:
233 mLeftRelativeSpace += 0.5f * relativeSize;
234 if ( mLeftRelativeSpace < 0.f )
236 mLeftRelativeSpace = 0.f;
238 mRightRelativeSpace += 0.5f * relativeSize;
239 if ( mRightRelativeSpace < 0.f )
241 mRightRelativeSpace = 0.f;
245 case Toolkit::Alignment::HorizontalRight:
247 mRightRelativeSpace += relativeSize;
248 if ( mRightRelativeSpace < 0.f )
250 mRightRelativeSpace = 0.f;
256 DALI_ASSERT_ALWAYS( false );
259 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
260 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
263 // Remove alignment as parent of control.
264 it->second.Remove( control );
266 // Remove the relationship between control and alignment.
267 mControls.erase( it );
269 // Remove column from tableview.
270 mLayout.DeleteColumn( position.columnIndex );
272 // Update bases and offsets.
273 if( position.columnIndex < mCenterBase )
275 // Control is on the left side. Decrease left offset and center and right bases.
280 else if( position.columnIndex < mCenterBase + mCenterOffset )
282 // Control is on the center side. Decrease center offset and right base.
288 // Control is on the right side. Decrease right base and right offset.
296 : Control( CONTROL_BEHAVIOUR_NONE ),
303 mLeftRelativeSpace( 0.5f ),
304 mRightRelativeSpace( 0.5f ),
305 mAccumulatedRelativeSpace( 0.f ),
306 mInitializing( false ),
315 void ToolBar::OnInitialize()
317 Lock lock( mInitializing );
320 mLayout = Toolkit::TableView::New( 1, 1 );
321 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
323 Self().Add( mLayout );
325 // Add two default actors to create spaces between controls grouped on the left, center and right.
326 Actor leftSpace = Actor::New();
327 Actor rightSpace = Actor::New();
328 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
329 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
330 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
331 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
334 void ToolBar::OnControlChildAdd(Actor& child)
338 // An actor is being added through the Actor's API.
340 // Remove child from tool bar actor and insert it in table view with some 'default' values
341 if ( child && child.GetParent() )
343 child.GetParent().Remove( child );
346 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
349 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
350 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
351 // actor is in mLayout not in Self().
354 void ToolBar::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
356 Relayout( mLayout, size, container );
359 } // namespace Internal
361 } // namespace Toolkit