Remove dependency to std::vector and property::value from property notification API
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / navigation-frame / navigation-tool-bar.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "navigation-tool-bar.h"
20
21 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace Internal
28 {
29 //ToDo: use const variables instead of magic numbers
30
31 NavigationToolBar::NavigationToolBar( NavigationControl& naviControl,
32                                       Toolkit::NaviToolBarStyle toolBarStylePortrait,
33                                       Toolkit::NaviToolBarStyle toolBarStyleLandscape )
34 : NavigationBar(naviControl, toolBarStylePortrait, toolBarStyleLandscape),
35   mStylePortrait( toolBarStylePortrait ),
36   mStyleLandscape( toolBarStyleLandscape ),
37   mCurrentStyle( &mStylePortrait),
38   mNumCentralActor( 0 )
39 {
40   // tool bar is located at the bottom of the frame
41   mLayout.SetParentOrigin( Dali::ParentOrigin::BOTTOM_CENTER );
42   mLayout.SetAnchorPoint( Dali::AnchorPoint::BOTTOM_CENTER );
43   if(mBackground)
44   {
45     mBackground.SetParentOrigin( Dali::ParentOrigin::BOTTOM_CENTER );
46     mBackground.SetAnchorPoint( Dali::AnchorPoint::BOTTOM_CENTER );
47   }
48
49   // layout of the left part, which contains only one control at CellPosition(1,1)
50   mLeftLayout = Toolkit::TableView::New(3,3);
51   mLayout.AddChild(mLeftLayout, Toolkit::TableView::CellPosition(0,0));
52
53   // layout of the right part, which contains only one control at CellPosition(1,1)
54   mRightLayout = Toolkit::TableView::New(3,3);
55   mLayout.AddChild(mRightLayout, Toolkit::TableView::CellPosition(0,2));
56
57   // layout of the central part, which contains multiples control, will add cells dynamically
58   mCentralLayout = Toolkit::TableView::New(3,2);
59   mLayout.AddChild(mCentralLayout, Toolkit::TableView::CellPosition(0,1));
60
61   SetFixedSizes();
62 }
63
64 void NavigationToolBar::AddControl(Actor actor, Toolkit::Alignment::Type alignment)
65 {
66   switch( alignment )
67   {
68     case Toolkit::Alignment::HorizontalLeft:   // can only have one control on the left side of the bar
69     {
70       mLeftLayout.RemoveChildAt(Toolkit::TableView::CellPosition(1,1));
71       mLeftLayout.AddChild(actor, Toolkit::TableView::CellPosition(1,1));
72       break;
73     }
74     case Toolkit::Alignment::HorizontalRight:  // can only have one control on the right side of the bar
75     {
76       mRightLayout.RemoveChildAt(Toolkit::TableView::CellPosition(1,1));
77       mRightLayout.AddChild(actor, Toolkit::TableView::CellPosition(1,1));
78       break;
79     }
80     case Toolkit::Alignment::HorizontalCenter: // can only have multiple controls on the central part of the bar
81     {
82       // already have button in central part
83       if( mCentralLayout.GetChildAt(Toolkit::TableView::CellPosition(1,1)))
84       {
85         unsigned int columnIndex = mCentralLayout.GetColumns() ;
86         mCentralLayout.InsertColumn( columnIndex-1 );
87         mCentralLayout.SetFixedWidth( columnIndex-1, mCurrentStyle->centralButtonGap );
88
89         mCentralLayout.InsertColumn( columnIndex );
90         mCentralLayout.AddChild(actor, Toolkit::TableView::CellPosition( 1, columnIndex ) );
91       }
92       else // have no button in central part
93       {
94         mCentralLayout.InsertColumn( 1 );
95         mCentralLayout.AddChild(actor, Toolkit::TableView::CellPosition(1,1));
96       }
97       mNumCentralActor++;
98       break;
99     }
100     default:
101       DALI_ASSERT_ALWAYS( false );
102   }
103
104 }
105
106 void NavigationToolBar::Update( Toolkit::Page page )
107 {
108   const Toolkit::Page::ControlOnBarContainer& controls = page.GetControlsOnToolBar();
109
110   // if there is no control to place on the bar, hide the bar
111   if( controls.empty() )
112   {
113     mVisible = false;
114     mLayout.SetVisible(false);
115     mBackground.SetVisible(false);
116     return;
117   }
118
119   //clear central controls
120   unsigned int numColumns = mCentralLayout.GetColumns() ;
121   unsigned int idx = numColumns-2;
122   while(idx > 0)
123   {
124     mCentralLayout.DeleteColumn(idx);
125     idx--;
126   }
127   mNumCentralActor = 0;
128   mLeftLayout.RemoveChildAt(Toolkit::TableView::CellPosition(1,1));
129   mRightLayout.RemoveChildAt(Toolkit::TableView::CellPosition(1,1));
130
131   Toolkit::Page::ControlOnBarContainer::const_iterator iter;
132
133   for( iter = controls.begin(); iter != controls.end(); iter++ )
134   {
135     AddControl( (*iter)->control, (*iter)->alignment );
136   }
137
138   float buttonWidth = static_cast<float>( mCurrentStyle->centralMinimum );
139   int length = mNumCentralActor * (mCurrentStyle->centralMinimum + mCurrentStyle->centralButtonGap) - mCurrentStyle->centralButtonGap;
140   if(  length > mCurrentStyle->centralMaximum ) // shrink the width to make sure all the controls can be fit in
141   {
142     buttonWidth = static_cast<float>( mCurrentStyle->centralMaximum - (mNumCentralActor - 1) * mCurrentStyle->centralButtonGap )
143                 / static_cast<float>( mNumCentralActor );
144   }
145   numColumns = mCentralLayout.GetColumns();
146   idx = 1;
147   while(idx < numColumns-1 )
148   {
149     mCentralLayout.SetFixedWidth( idx, buttonWidth );
150     idx += 2;
151   }
152   mVisible = true;
153   mLayout.SetVisible(true);
154   mBackground.SetVisible(true);
155 }
156
157 void NavigationToolBar::OrientationUpdate( bool isPortrait )
158 {
159   mCurrentStyle = isPortrait ? &mStylePortrait : &mStyleLandscape;
160   SetFixedSizes();
161   Update( mCurrentItem );
162 }
163
164 void NavigationToolBar::SetFixedSizes()
165 {
166   mLayout.SetFixedWidth(1, mCurrentStyle->centralMaximum);
167
168   mLeftLayout.SetFixedWidth(0,mCurrentStyle->hotizontalMargin);
169   mLeftLayout.SetFixedWidth(1,mCurrentStyle->sideButtonSize );
170   mLeftLayout.SetFixedHeight(1,mCurrentStyle->sideButtonSize );
171
172   mRightLayout.SetFixedWidth(2,mCurrentStyle->hotizontalMargin);
173   mRightLayout.SetFixedWidth(1,mCurrentStyle->sideButtonSize );
174   mRightLayout.SetFixedHeight(1,mCurrentStyle->sideButtonSize );
175
176   mCentralLayout.SetFixedHeight(1,mCurrentStyle->centralButtonHeight );
177 }
178
179 } // namespace Internal
180
181 } // namespace Toolkit
182
183 } // namespace Dali