2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "tool-bar-impl.h"
21 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
22 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
39 return Toolkit::ToolBar::New();
42 TypeRegistration mType( typeid(Toolkit::ToolBar), typeid(Toolkit::Control), Create );
44 const float DEFAULT_RELATIVE_SIZE( 0.1f );
45 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
48 Toolkit::ToolBar ToolBar::New()
50 // Create the implementation, temporarily owned on stack
51 IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
53 // Pass ownership to Toolkit::View
54 Toolkit::ToolBar toolBar( *internalToolBar );
56 // Second-phase init of the implementation
57 // This can only be done after the CustomActor connection has been made...
58 internalToolBar->Initialize();
63 void ToolBar::SetBackground( Actor background )
65 Lock lock( mInitializing );
68 background.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
69 background.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
70 background.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
72 RenderableActor renderableActor = RenderableActor::DownCast( background );
73 if ( renderableActor )
75 renderableActor.SetSortModifier( 1.f );
78 Self().Add( background );
81 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
83 // Work out index and update bases and offsets for further insertions.
84 unsigned int index = 0;
87 case Toolkit::Alignment::HorizontalLeft:
95 case Toolkit::Alignment::HorizontalCenter:
97 index = mCenterBase + mCenterOffset;
102 case Toolkit::Alignment::HorizontalRight:
104 index = mRightBase - mRightOffset;
111 DALI_ASSERT_ALWAYS( false );
115 // Create a new column for the new control.
116 mLayout.InsertColumn( index );
118 // Create an alignment container where to place the control.
119 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
120 alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
121 alignmentContainer.SetPadding( padding );
122 alignmentContainer.Add( control );
124 // Insert the control in the table view.
125 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
126 mLayout.SetRelativeWidth( index, relativeSize );
128 // Relate control and alignmentContainer in order to allow removing controls.
129 mControls[control] = alignmentContainer;
131 // Update accumulated relative space.
132 mAccumulatedRelativeSpace += relativeSize;
134 // Update spaces between left, center and right groups of controls.
137 case Toolkit::Alignment::HorizontalLeft:
139 mLeftRelativeSpace -= relativeSize;
140 if ( mLeftRelativeSpace < 0.f )
142 mLeftRelativeSpace = 0.f;
146 case Toolkit::Alignment::HorizontalCenter:
148 mLeftRelativeSpace -= 0.5f * relativeSize;
149 if ( mLeftRelativeSpace < 0.f )
151 mLeftRelativeSpace = 0.f;
153 mRightRelativeSpace -= 0.5f * relativeSize;
154 if ( mRightRelativeSpace < 0.f )
156 mRightRelativeSpace = 0.f;
160 case Toolkit::Alignment::HorizontalRight:
162 mRightRelativeSpace -= relativeSize;
163 if ( mRightRelativeSpace < 0.f )
165 mRightRelativeSpace = 0.f;
171 DALI_ASSERT_ALWAYS( false );
175 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
176 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
179 void ToolBar::RemoveControl( Actor control )
181 Toolkit::TableView::CellPosition position;
183 // Find the alignment where the control is placed.
184 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
186 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
188 // Update accumulated relative space.
189 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
191 // Update spaces between left, center and right groups of controls.
192 if( 1.0 > mAccumulatedRelativeSpace )
194 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
195 if( position.columnIndex < mLeftOffset )
197 alignment = Toolkit::Alignment::HorizontalLeft;
199 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
201 alignment = Toolkit::Alignment::HorizontalCenter;
203 else if( position.columnIndex > mCenterBase + mCenterOffset )
205 alignment = Toolkit::Alignment::HorizontalRight;
209 DALI_ASSERT_ALWAYS( false );
212 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
216 case Toolkit::Alignment::HorizontalLeft:
218 mLeftRelativeSpace += relativeSize;
219 if ( mLeftRelativeSpace < 0.f )
221 mLeftRelativeSpace = 0.f;
225 case Toolkit::Alignment::HorizontalCenter:
227 mLeftRelativeSpace += 0.5f * relativeSize;
228 if ( mLeftRelativeSpace < 0.f )
230 mLeftRelativeSpace = 0.f;
232 mRightRelativeSpace += 0.5f * relativeSize;
233 if ( mRightRelativeSpace < 0.f )
235 mRightRelativeSpace = 0.f;
239 case Toolkit::Alignment::HorizontalRight:
241 mRightRelativeSpace += relativeSize;
242 if ( mRightRelativeSpace < 0.f )
244 mRightRelativeSpace = 0.f;
250 DALI_ASSERT_ALWAYS( false );
253 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
254 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
257 // Remove alignment as parent of control.
258 it->second.Remove( control );
260 // Remove the relationship between control and alignment.
261 mControls.erase( it );
263 // Remove column from tableview.
264 mLayout.DeleteColumn( position.columnIndex );
266 // Update bases and offsets.
267 if( position.columnIndex < mCenterBase )
269 // Control is on the left side. Decrease left offset and center and right bases.
274 else if( position.columnIndex < mCenterBase + mCenterOffset )
276 // Control is on the center side. Decrease center offset and right base.
282 // Control is on the right side. Decrease right base and right offset.
290 : Control( false ), // doesn't require touch events
297 mLeftRelativeSpace( 0.5f ),
298 mRightRelativeSpace( 0.5f ),
299 mAccumulatedRelativeSpace( 0.f ),
300 mInitializing( false ),
309 void ToolBar::OnInitialize()
311 Lock lock( mInitializing );
314 mLayout = Toolkit::TableView::New( 1, 1 );
315 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
317 Self().Add( mLayout );
319 // Add two default actors to create spaces between controls grouped on the left, center and right.
320 Actor leftSpace = Actor::New();
321 Actor rightSpace = Actor::New();
322 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
323 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
324 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
325 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
328 void ToolBar::OnControlChildAdd(Actor& child)
332 // An actor is being added through the Actor's API.
334 // Remove child from tool bar actor and insert it in table view with some 'default' values
335 if ( child && child.GetParent() )
337 child.GetParent().Remove( child );
340 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
343 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
344 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
345 // actor is in mLayout not in Self().
348 void ToolBar::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
350 Relayout( mLayout, size, container );
353 } // namespace Internal
355 } // namespace Toolkit