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