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