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/devel-api/object/type-registry-helper.h>
28 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
29 #include <dali-toolkit/devel-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::Toolbar
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( Vector2( mToolBarSize.width, mToolBarSize.height ) );
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.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
129 alignmentContainer.SetPadding( padding );
130 alignmentContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
131 alignmentContainer.Add( control );
133 // Insert the control in the table view.
134 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
135 mLayout.SetRelativeWidth( index, relativeSize );
137 // Relate control and alignmentContainer in order to allow removing controls.
138 mControls[control] = alignmentContainer;
140 // Update accumulated relative space.
141 mAccumulatedRelativeSpace += relativeSize;
143 // Update spaces between left, center and right groups of controls.
146 case Toolkit::Alignment::HorizontalLeft:
148 mLeftRelativeSpace -= relativeSize;
149 if ( mLeftRelativeSpace < 0.f )
151 mLeftRelativeSpace = 0.f;
155 case Toolkit::Alignment::HorizontalCenter:
157 mLeftRelativeSpace -= 0.5f * relativeSize;
158 if ( mLeftRelativeSpace < 0.f )
160 mLeftRelativeSpace = 0.f;
162 mRightRelativeSpace -= 0.5f * relativeSize;
163 if ( mRightRelativeSpace < 0.f )
165 mRightRelativeSpace = 0.f;
169 case Toolkit::Alignment::HorizontalRight:
171 mRightRelativeSpace -= relativeSize;
172 if ( mRightRelativeSpace < 0.f )
174 mRightRelativeSpace = 0.f;
180 DALI_ASSERT_ALWAYS( false );
184 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
185 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
188 void ToolBar::RemoveControl( Actor control )
190 Toolkit::TableView::CellPosition position;
192 // Find the alignment where the control is placed.
193 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
195 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
197 // Update accumulated relative space.
198 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
200 // Update spaces between left, center and right groups of controls.
201 if( 1.0 > mAccumulatedRelativeSpace )
203 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
204 if( position.columnIndex < mLeftOffset )
206 alignment = Toolkit::Alignment::HorizontalLeft;
208 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
210 alignment = Toolkit::Alignment::HorizontalCenter;
212 else if( position.columnIndex > mCenterBase + mCenterOffset )
214 alignment = Toolkit::Alignment::HorizontalRight;
218 DALI_ASSERT_ALWAYS( false );
221 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
225 case Toolkit::Alignment::HorizontalLeft:
227 mLeftRelativeSpace += relativeSize;
228 if ( mLeftRelativeSpace < 0.f )
230 mLeftRelativeSpace = 0.f;
234 case Toolkit::Alignment::HorizontalCenter:
236 mLeftRelativeSpace += 0.5f * relativeSize;
237 if ( mLeftRelativeSpace < 0.f )
239 mLeftRelativeSpace = 0.f;
241 mRightRelativeSpace += 0.5f * relativeSize;
242 if ( mRightRelativeSpace < 0.f )
244 mRightRelativeSpace = 0.f;
248 case Toolkit::Alignment::HorizontalRight:
250 mRightRelativeSpace += relativeSize;
251 if ( mRightRelativeSpace < 0.f )
253 mRightRelativeSpace = 0.f;
259 DALI_ASSERT_ALWAYS( false );
262 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
263 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
266 // Remove alignment as parent of control.
267 it->second.Remove( control );
269 // Remove the relationship between control and alignment.
270 mControls.erase( it );
272 // Remove column from tableview.
273 mLayout.DeleteColumn( position.columnIndex );
275 // Update bases and offsets.
276 if( position.columnIndex < mCenterBase )
278 // Control is on the left side. Decrease left offset and center and right bases.
283 else if( position.columnIndex < mCenterBase + mCenterOffset )
285 // Control is on the center side. Decrease center offset and right base.
291 // Control is on the right side. Decrease right base and right offset.
299 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
306 mLeftRelativeSpace( 0.5f ),
307 mRightRelativeSpace( 0.5f ),
308 mAccumulatedRelativeSpace( 0.f ),
309 mInitializing( false ),
318 void ToolBar::OnInitialize()
320 Lock lock( mInitializing );
323 mLayout = Toolkit::TableView::New( 1, 1 );
324 mLayout.SetName( "TOOLBAR_LAYOUT" );
325 mLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
326 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
328 Self().Add( mLayout );
330 // Add two default actors to create spaces between controls grouped on the left, center and right.
331 Actor leftSpace = Actor::New();
332 Actor rightSpace = Actor::New();
333 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
334 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
335 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
336 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
339 void ToolBar::OnControlChildAdd(Actor& child)
343 // An actor is being added through the Actor's API.
345 // Remove child from tool bar actor and insert it in table view with some 'default' values
346 if ( child && child.GetParent() )
348 child.GetParent().Remove( child );
351 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
354 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
355 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
356 // actor is in mLayout not in Self().
359 void ToolBar::OnControlSizeSet( const Vector3& targetSize )
361 mToolBarSize = targetSize;
362 mBackground.SetSize( Vector2( mToolBarSize.width, mToolBarSize.height ) );
365 } // namespace Internal
367 } // namespace Toolkit