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-toolkit/public-api/controls/alignment/alignment.h>
23 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
40 return Toolkit::ToolBar::New();
43 TypeRegistration mType( typeid(Toolkit::ToolBar), typeid(Toolkit::Control), Create );
45 const float DEFAULT_RELATIVE_SIZE( 0.1f );
46 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
49 Toolkit::ToolBar ToolBar::New()
51 // Create the implementation, temporarily owned on stack
52 IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
54 // Pass ownership to Toolkit::View
55 Toolkit::ToolBar toolBar( *internalToolBar );
57 // Second-phase init of the implementation
58 // This can only be done after the CustomActor connection has been made...
59 internalToolBar->Initialize();
64 void ToolBar::SetBackground( Actor background )
66 Lock lock( mInitializing );
69 background.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
70 background.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
71 background.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
73 RenderableActor renderableActor = RenderableActor::DownCast( background );
74 if ( renderableActor )
76 renderableActor.SetSortModifier( 1.f );
79 Self().Add( background );
82 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
84 // Work out index and update bases and offsets for further insertions.
85 unsigned int index = 0;
88 case Toolkit::Alignment::HorizontalLeft:
96 case Toolkit::Alignment::HorizontalCenter:
98 index = mCenterBase + mCenterOffset;
103 case Toolkit::Alignment::HorizontalRight:
105 index = mRightBase - mRightOffset;
112 DALI_ASSERT_ALWAYS( false );
116 // Create a new column for the new control.
117 mLayout.InsertColumn( index );
119 // Create an alignment container where to place the control.
120 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
121 alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
122 alignmentContainer.SetPadding( padding );
123 alignmentContainer.Add( control );
125 // Insert the control in the table view.
126 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
127 mLayout.SetRelativeWidth( index, relativeSize );
129 // Relate control and alignmentContainer in order to allow removing controls.
130 mControls[control] = alignmentContainer;
132 // Update accumulated relative space.
133 mAccumulatedRelativeSpace += relativeSize;
135 // Update spaces between left, center and right groups of controls.
138 case Toolkit::Alignment::HorizontalLeft:
140 mLeftRelativeSpace -= relativeSize;
141 if ( mLeftRelativeSpace < 0.f )
143 mLeftRelativeSpace = 0.f;
147 case Toolkit::Alignment::HorizontalCenter:
149 mLeftRelativeSpace -= 0.5f * relativeSize;
150 if ( mLeftRelativeSpace < 0.f )
152 mLeftRelativeSpace = 0.f;
154 mRightRelativeSpace -= 0.5f * relativeSize;
155 if ( mRightRelativeSpace < 0.f )
157 mRightRelativeSpace = 0.f;
161 case Toolkit::Alignment::HorizontalRight:
163 mRightRelativeSpace -= relativeSize;
164 if ( mRightRelativeSpace < 0.f )
166 mRightRelativeSpace = 0.f;
172 DALI_ASSERT_ALWAYS( false );
176 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
177 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
180 void ToolBar::RemoveControl( Actor control )
182 Toolkit::TableView::CellPosition position;
184 // Find the alignment where the control is placed.
185 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
187 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
189 // Update accumulated relative space.
190 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
192 // Update spaces between left, center and right groups of controls.
193 if( 1.0 > mAccumulatedRelativeSpace )
195 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
196 if( position.columnIndex < mLeftOffset )
198 alignment = Toolkit::Alignment::HorizontalLeft;
200 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
202 alignment = Toolkit::Alignment::HorizontalCenter;
204 else if( position.columnIndex > mCenterBase + mCenterOffset )
206 alignment = Toolkit::Alignment::HorizontalRight;
210 DALI_ASSERT_ALWAYS( false );
213 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
217 case Toolkit::Alignment::HorizontalLeft:
219 mLeftRelativeSpace += relativeSize;
220 if ( mLeftRelativeSpace < 0.f )
222 mLeftRelativeSpace = 0.f;
226 case Toolkit::Alignment::HorizontalCenter:
228 mLeftRelativeSpace += 0.5f * relativeSize;
229 if ( mLeftRelativeSpace < 0.f )
231 mLeftRelativeSpace = 0.f;
233 mRightRelativeSpace += 0.5f * relativeSize;
234 if ( mRightRelativeSpace < 0.f )
236 mRightRelativeSpace = 0.f;
240 case Toolkit::Alignment::HorizontalRight:
242 mRightRelativeSpace += relativeSize;
243 if ( mRightRelativeSpace < 0.f )
245 mRightRelativeSpace = 0.f;
251 DALI_ASSERT_ALWAYS( false );
254 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
255 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
258 // Remove alignment as parent of control.
259 it->second.Remove( control );
261 // Remove the relationship between control and alignment.
262 mControls.erase( it );
264 // Remove column from tableview.
265 mLayout.DeleteColumn( position.columnIndex );
267 // Update bases and offsets.
268 if( position.columnIndex < mCenterBase )
270 // Control is on the left side. Decrease left offset and center and right bases.
275 else if( position.columnIndex < mCenterBase + mCenterOffset )
277 // Control is on the center side. Decrease center offset and right base.
283 // Control is on the right side. Decrease right base and right offset.
291 : ControlImpl( false ), // doesn't require touch events
298 mLeftRelativeSpace( 0.5f ),
299 mRightRelativeSpace( 0.5f ),
300 mAccumulatedRelativeSpace( 0.f ),
301 mInitializing( false ),
310 void ToolBar::OnInitialize()
312 Lock lock( mInitializing );
315 mLayout = Toolkit::TableView::New( 1, 1 );
316 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
318 Self().Add( mLayout );
320 // Add two default actors to create spaces between controls grouped on the left, center and right.
321 Actor leftSpace = Actor::New();
322 Actor rightSpace = Actor::New();
323 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
324 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
325 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
326 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
329 void ToolBar::OnControlChildAdd(Actor& child)
333 // An actor is being added through the Actor's API.
335 // Remove child from tool bar actor and insert it in table view with some 'default' values
336 if ( child && child.GetParent() )
338 child.GetParent().Remove( child );
341 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
344 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
345 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
346 // actor is in mLayout not in Self().
349 void ToolBar::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
351 Relayout( mLayout, size, container );
354 } // namespace Internal
356 } // namespace Toolkit