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/image-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>
44 return Toolkit::ToolBar::New();
47 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ToolBar, Toolkit::Control, Create )
48 DALI_TYPE_REGISTRATION_END()
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::Toolbar
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::AddControl( Actor control, float relativeSize, Toolkit::Alignment::Type alignment, const Toolkit::Alignment::Padding& padding )
71 // Work out index and update bases and offsets for further insertions.
72 unsigned int index = 0;
75 case Toolkit::Alignment::HorizontalLeft:
83 case Toolkit::Alignment::HorizontalCenter:
85 index = mCenterBase + mCenterOffset;
90 case Toolkit::Alignment::HorizontalRight:
92 index = mRightBase - mRightOffset;
99 DALI_ASSERT_ALWAYS( false );
103 // Create a new column for the new control.
104 mLayout.InsertColumn( index );
106 // Create an alignment container where to place the control.
107 Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( alignment );
108 alignmentContainer.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
109 alignmentContainer.SetPadding( padding );
110 alignmentContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
111 alignmentContainer.Add( control );
113 // Insert the control in the table view.
114 mLayout.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( 0, index ) );
115 mLayout.SetRelativeWidth( index, relativeSize );
117 // Relate control and alignmentContainer in order to allow removing controls.
118 mControls[control] = alignmentContainer;
120 // Update accumulated relative space.
121 mAccumulatedRelativeSpace += relativeSize;
123 // Update spaces between left, center and right groups of controls.
126 case Toolkit::Alignment::HorizontalLeft:
128 mLeftRelativeSpace -= relativeSize;
129 if ( mLeftRelativeSpace < 0.f )
131 mLeftRelativeSpace = 0.f;
135 case Toolkit::Alignment::HorizontalCenter:
137 mLeftRelativeSpace -= 0.5f * relativeSize;
138 if ( mLeftRelativeSpace < 0.f )
140 mLeftRelativeSpace = 0.f;
142 mRightRelativeSpace -= 0.5f * relativeSize;
143 if ( mRightRelativeSpace < 0.f )
145 mRightRelativeSpace = 0.f;
149 case Toolkit::Alignment::HorizontalRight:
151 mRightRelativeSpace -= relativeSize;
152 if ( mRightRelativeSpace < 0.f )
154 mRightRelativeSpace = 0.f;
160 DALI_ASSERT_ALWAYS( false );
164 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
165 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
168 void ToolBar::RemoveControl( Actor control )
170 Toolkit::TableView::CellPosition position;
172 // Find the alignment where the control is placed.
173 std::map<Actor,Toolkit::Alignment>::iterator it = mControls.find( control );
175 if( ( it != mControls.end() ) && ( mLayout.FindChildPosition( it->second, position ) ) )
177 // Update accumulated relative space.
178 mAccumulatedRelativeSpace -= mLayout.GetRelativeWidth( position.columnIndex );
180 // Update spaces between left, center and right groups of controls.
181 if( 1.0 > mAccumulatedRelativeSpace )
183 Toolkit::Alignment::Type alignment = Toolkit::Alignment::HorizontalLeft;
184 if( position.columnIndex < mLeftOffset )
186 alignment = Toolkit::Alignment::HorizontalLeft;
188 else if( ( position.columnIndex > mLeftOffset ) && ( position.columnIndex < mCenterBase + mCenterOffset ) )
190 alignment = Toolkit::Alignment::HorizontalCenter;
192 else if( position.columnIndex > mCenterBase + mCenterOffset )
194 alignment = Toolkit::Alignment::HorizontalRight;
198 DALI_ASSERT_ALWAYS( false );
201 float relativeSize = mLayout.GetRelativeWidth( position.columnIndex );
205 case Toolkit::Alignment::HorizontalLeft:
207 mLeftRelativeSpace += relativeSize;
208 if ( mLeftRelativeSpace < 0.f )
210 mLeftRelativeSpace = 0.f;
214 case Toolkit::Alignment::HorizontalCenter:
216 mLeftRelativeSpace += 0.5f * relativeSize;
217 if ( mLeftRelativeSpace < 0.f )
219 mLeftRelativeSpace = 0.f;
221 mRightRelativeSpace += 0.5f * relativeSize;
222 if ( mRightRelativeSpace < 0.f )
224 mRightRelativeSpace = 0.f;
228 case Toolkit::Alignment::HorizontalRight:
230 mRightRelativeSpace += relativeSize;
231 if ( mRightRelativeSpace < 0.f )
233 mRightRelativeSpace = 0.f;
239 DALI_ASSERT_ALWAYS( false );
242 mLayout.SetRelativeWidth( mLeftOffset, mLeftRelativeSpace );
243 mLayout.SetRelativeWidth( mCenterBase + mCenterOffset, mRightRelativeSpace );
246 // Remove alignment as parent of control.
247 it->second.Remove( control );
249 // Remove the relationship between control and alignment.
250 mControls.erase( it );
252 // Remove column from tableview.
253 mLayout.DeleteColumn( position.columnIndex );
255 // Update bases and offsets.
256 if( position.columnIndex < mCenterBase )
258 // Control is on the left side. Decrease left offset and center and right bases.
263 else if( position.columnIndex < mCenterBase + mCenterOffset )
265 // Control is on the center side. Decrease center offset and right base.
271 // Control is on the right side. Decrease right base and right offset.
279 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
286 mLeftRelativeSpace( 0.5f ),
287 mRightRelativeSpace( 0.5f ),
288 mAccumulatedRelativeSpace( 0.f ),
289 mInitializing( false ),
298 void ToolBar::OnInitialize()
300 Lock lock( mInitializing );
303 mLayout = Toolkit::TableView::New( 1, 1 );
304 mLayout.SetName( "TOOLBAR_LAYOUT" );
305 mLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
306 mLayout.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
308 Self().Add( mLayout );
310 // Add two default actors to create spaces between controls grouped on the left, center and right.
311 Actor leftSpace = Actor::New();
312 Actor rightSpace = Actor::New();
313 mLayout.AddChild( leftSpace, Toolkit::TableView::CellPosition( 0, 0 ) );
314 mLayout.AddChild( rightSpace, Toolkit::TableView::CellPosition( 0, 1 ) );
315 mLayout.SetRelativeWidth( 0, mLeftRelativeSpace );
316 mLayout.SetRelativeWidth( 1, mRightRelativeSpace );
319 void ToolBar::OnControlChildAdd(Actor& child)
323 // An actor is being added through the Actor's API.
325 // Remove child from tool bar actor and insert it in table view with some 'default' values
326 if ( child && child.GetParent() )
328 child.GetParent().Remove( child );
331 AddControl( child, DEFAULT_RELATIVE_SIZE, DEFAULT_ALIGNMENT, Toolkit::ToolBar::DEFAULT_PADDING );
334 // No OnControlChildRemove method required because Actors are added to the mLayout table view, so if an
335 // actor is removed using the Actor::RemoveChild method it will not remove anything because the
336 // actor is in mLayout not in Self().
339 } // namespace Internal
341 } // namespace Toolkit