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/animation/constraints.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
43 return Toolkit::ToolBar::New();
46 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ToolBar, Toolkit::Control, Create )
47 DALI_TYPE_REGISTRATION_END()
49 const float DEFAULT_RELATIVE_SIZE( 0.1f );
50 const Toolkit::Alignment::Type DEFAULT_ALIGNMENT( Toolkit::Alignment::HorizontalLeft );
53 Toolkit::ToolBar ToolBar::New()
55 // Create the implementation, temporarily owned on stack
56 IntrusivePtr< ToolBar > internalToolBar = new ToolBar();
58 // Pass ownership to Toolkit::Toolbar
59 Toolkit::ToolBar toolBar( *internalToolBar );
61 // Second-phase init of the implementation
62 // This can only be done after the CustomActor connection has been made...
63 internalToolBar->Initialize();
68 void ToolBar::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
70 // Work out index and update bases and offsets for further insertions.
71 unsigned int index = 0;
74 case Toolkit::Alignment::HorizontalLeft:
82 case Toolkit::Alignment::HorizontalCenter:
84 index = mCenterBase + mCenterOffset;
89 case Toolkit::Alignment::HorizontalRight:
91 index = mRightBase - mRightOffset;
98 DALI_ASSERT_ALWAYS( false );
102 // Create a new column for the new control.
103 mLayout.InsertColumn( index );
105 // Create an alignment container where to place the control.
106 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
107 alignmentContainer.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
108 alignmentContainer.SetPadding( padding );
109 alignmentContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
110 alignmentContainer.Add( control );
112 // Insert the control in the table view.
113 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
114 mLayout.SetRelativeWidth( index, relativeSize );
116 // Relate control and alignmentContainer in order to allow removing controls.
117 mControls[control] = alignmentContainer;
119 // Update accumulated relative space.
120 mAccumulatedRelativeSpace += relativeSize;
122 // Update spaces between left, center and right groups of controls.
125 case Toolkit::Alignment::HorizontalLeft:
127 mLeftRelativeSpace -= relativeSize;
128 if ( mLeftRelativeSpace < 0.f )
130 mLeftRelativeSpace = 0.f;
134 case Toolkit::Alignment::HorizontalCenter:
136 mLeftRelativeSpace -= 0.5f * relativeSize;
137 if ( mLeftRelativeSpace < 0.f )
139 mLeftRelativeSpace = 0.f;
141 mRightRelativeSpace -= 0.5f * relativeSize;
142 if ( mRightRelativeSpace < 0.f )
144 mRightRelativeSpace = 0.f;
148 case Toolkit::Alignment::HorizontalRight:
150 mRightRelativeSpace -= relativeSize;
151 if ( mRightRelativeSpace < 0.f )
153 mRightRelativeSpace = 0.f;
159 DALI_ASSERT_ALWAYS( false );
163 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
164 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
167 void ToolBar::RemoveControl( Actor control )
169 Toolkit::TableView::CellPosition position;
171 // Find the alignment where the control is placed.
172 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
174 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
176 // Update accumulated relative space.
177 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
179 // Update spaces between left, center and right groups of controls.
180 if( 1.0 > mAccumulatedRelativeSpace )
182 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
183 if( position.columnIndex < mLeftOffset )
185 alignment = Toolkit::Alignment::HorizontalLeft;
187 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
189 alignment = Toolkit::Alignment::HorizontalCenter;
191 else if( position.columnIndex > mCenterBase + mCenterOffset )
193 alignment = Toolkit::Alignment::HorizontalRight;
197 DALI_ASSERT_ALWAYS( false );
200 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
204 case Toolkit::Alignment::HorizontalLeft:
206 mLeftRelativeSpace += relativeSize;
207 if ( mLeftRelativeSpace < 0.f )
209 mLeftRelativeSpace = 0.f;
213 case Toolkit::Alignment::HorizontalCenter:
215 mLeftRelativeSpace += 0.5f * relativeSize;
216 if ( mLeftRelativeSpace < 0.f )
218 mLeftRelativeSpace = 0.f;
220 mRightRelativeSpace += 0.5f * relativeSize;
221 if ( mRightRelativeSpace < 0.f )
223 mRightRelativeSpace = 0.f;
227 case Toolkit::Alignment::HorizontalRight:
229 mRightRelativeSpace += relativeSize;
230 if ( mRightRelativeSpace < 0.f )
232 mRightRelativeSpace = 0.f;
238 DALI_ASSERT_ALWAYS( false );
241 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
242 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
245 // Remove alignment as parent of control.
246 it->second.Remove( control );
248 // Remove the relationship between control and alignment.
249 mControls.erase( it );
251 // Remove column from tableview.
252 mLayout.DeleteColumn( position.columnIndex );
254 // Update bases and offsets.
255 if( position.columnIndex < mCenterBase )
257 // Control is on the left side. Decrease left offset and center and right bases.
262 else if( position.columnIndex < mCenterBase + mCenterOffset )
264 // Control is on the center side. Decrease center offset and right base.
270 // Control is on the right side. Decrease right base and right offset.
278 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
285 mLeftRelativeSpace( 0.5f ),
286 mRightRelativeSpace( 0.5f ),
287 mAccumulatedRelativeSpace( 0.f ),
288 mInitializing( false ),
297 void ToolBar::OnInitialize()
299 Lock lock( mInitializing );
302 mLayout = Toolkit::TableView::New( 1, 1 );
303 mLayout.SetName( "TOOLBAR_LAYOUT" );
304 mLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
305 mLayout.SetParentOrigin( ParentOrigin::CENTER );
307 Self().Add( mLayout );
309 // Add two default actors to create spaces between controls grouped on the left, center and right.
310 Actor leftSpace = Actor::New();
311 Actor rightSpace = Actor::New();
312 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
313 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
314 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
315 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
318 void ToolBar::OnChildAdd(Actor& child)
320 Control::OnChildAdd( child );
324 // An actor is being added through the Actor's API.
326 // Remove child from tool bar actor and insert it in table view with some 'default' values
327 if ( child && child.GetParent() )
329 child.GetParent().Remove( child );
332 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
335 // No OnChildRemove method required because Actors are added to the mLayout table view, so if an
336 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
337 // actor is in mLayout not in Self().
340 } // namespace Internal
342 } // namespace Toolkit