Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / controls / navigation-frame / navigation-title-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-title-bar.h"
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace Internal
27 {
28
29 //ToDo: use const variables instead of magic numbers
30
31 NavigationTitleBar::NavigationTitleBar(NavigationControl& naviControl,
32                                        Toolkit::NaviTitleBarStyle titleBarStylePortrait,
33                                        Toolkit::NaviTitleBarStyle titleBarStyleLandscape )
34 : NavigationBar(naviControl, titleBarStylePortrait, titleBarStyleLandscape ),
35   mStylePortrait( titleBarStylePortrait ),
36   mStyleLandscape( titleBarStyleLandscape ),
37   mCurrentStyle( &mStylePortrait )
38 {
39   // title bar is located at the top of the frame
40   mLayout.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
41   mLayout.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
42   if(mBackground)
43   {
44     mBackground.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
45     mBackground.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
46   }
47
48   // button layout: three rows, controls will be put in the middle row, the top and bottom rows are just for margins
49   mButtonLayout = Toolkit::TableView::New(3, 1);
50   // title layout: fours rows, the top and bottom rows are just for margins
51   // if subtitle exists, title in the second row and subtitle in the third row
52   // if no subtitle, title will occupy both second and third row
53   mTitleLayout= Toolkit::TableView::New( 4,1 );
54   // title icon layout: the top row, the bottom row and the left column are all for margins
55   mTitleIconLayout= Toolkit::TableView::New( 3,2 );
56   SetFixedSizes();
57 }
58
59 void NavigationTitleBar::Update( Toolkit::Page page )
60 {
61   const std::vector<Actor>& controls = page.GetControlsOnTitleBar();
62
63   // if there is no control to place on the bar ano no title is set, hide the bar
64   if(controls.empty() && page.GetTitle().empty())
65   {
66     mVisible = false;
67     mLayout.SetVisible(false);
68     mBackground.SetVisible(false);
69     return;
70   }
71
72   if(mLayout.GetColumns() == 4)// | leftMargin | titleLayout(may have icon and subtitle) | buttonLayout | rightMargin |
73   {
74     //remove buttonLayout
75     mLayout.DeleteColumn(2);
76   }
77   // remove titleLayout
78   mLayout.RemoveChildAt( Toolkit::TableView::CellPosition(0,1) );
79   //Remove the controls in the buttonLayout
80   mButtonLayout.Resize(3,1);
81   //remove titleIcon
82   if(mTitleLayout.GetColumns() == 2)
83   {
84     mTitleLayout.DeleteColumn( 0 );
85   }
86   // remove title and subtitle
87   mTitleLayout.RemoveChildAt( Toolkit::TableView::CellPosition(2,0) );
88   mTitleLayout.RemoveChildAt( Toolkit::TableView::CellPosition(1,0) );
89
90   // add controls at the right part of the bar(if exist)
91   if(!controls.empty())
92   {
93     int numControls = controls.size();
94
95     for( int index = 0; index < numControls; index++)
96     {
97       mButtonLayout.AddChild( controls[index], Toolkit::TableView::CellPosition(1, 2*index + 1) );
98       mButtonLayout.SetFixedWidth (2*index, mCurrentStyle->gapBetweenButtons);
99       mButtonLayout.SetFixedWidth (2*index+1, mCurrentStyle->buttonSize);
100     }
101
102     mLayout.InsertColumn(2);
103     mLayout.SetFixedWidth(2, numControls * ( mCurrentStyle->buttonSize + mCurrentStyle->gapBetweenButtons) );
104     mLayout.AddChild(mButtonLayout, Toolkit::TableView::CellPosition(0,2));
105   }
106
107   // insert title icon to the left of the title(if exist)
108   if( page.GetTitleIcon() )
109   {
110     mTitleIconLayout.RemoveChildAt(Toolkit::TableView::CellPosition(1,0) );
111     mTitleIconLayout.AddChild( page.GetTitleIcon(), Toolkit::TableView::CellPosition(1,0) );
112     mTitleLayout.InsertColumn( 0 );
113     mTitleLayout.SetFixedWidth( 0, mCurrentStyle->titleLeftMargin + mCurrentStyle->titleIconSize);
114     mTitleLayout.AddChild( mTitleIconLayout, Toolkit::TableView::CellPosition(1,0,2,1) );
115   }
116
117   mLayout.AddChild( mTitleLayout, Toolkit::TableView::CellPosition(0,1) );
118
119   mVisible = true;
120   mLayout.SetVisible(true);
121   mBackground.SetVisible(true);
122 }
123
124 void NavigationTitleBar::OrientationUpdate( bool isPortrait )
125 {
126   mCurrentStyle = isPortrait ? &mStylePortrait : &mStyleLandscape;
127   SetFixedSizes();
128   Update( mCurrentItem );
129 }
130
131 void NavigationTitleBar::SetFixedSizes()
132 {
133   mLayout.SetFixedWidth(0, mCurrentStyle->titleLeftMargin);
134   mLayout.SetFixedWidth(2, mCurrentStyle->buttonRightMargin);
135
136   mButtonLayout.SetFixedHeight(2, mCurrentStyle->buttonBottomMargin);
137   mButtonLayout.SetFixedHeight(1, mCurrentStyle->buttonSize);
138
139   mTitleLayout.SetFixedHeight( 3,mCurrentStyle->titleBottomMargin );
140   mTitleLayout.SetFixedHeight( 2,mCurrentStyle->subtitleHeight );
141
142   mTitleIconLayout.SetFixedWidth( 0, mCurrentStyle->titleIconSize );
143   mTitleIconLayout.SetFixedHeight( 1, mCurrentStyle->titleIconSize );
144 }
145
146 } // namespace Internal
147
148 } // namespace Toolkit
149
150 } // namespace Dali