Size negotiation patch 1: Remove SetPreferred size
[platform/core/uifw/dali-demo.git] / shared / view.h
1 #ifndef __DALI_DEMO_HELPER_VIEW_H__
2 #define __DALI_DEMO_HELPER_VIEW_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali-toolkit/dali-toolkit.h>
22
23 namespace DemoHelper
24 {
25
26 /**
27  * Provide a style for the view and its tool bar.
28  */
29 struct ViewStyle
30 {
31   ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float toolBarHeight, float toolBarPadding )
32   : mToolBarButtonPercentage( toolBarButtonPercentage ),
33     mToolBarTitlePercentage( toolBarTitlePercentage ),
34     mToolBarHeight( toolBarHeight ),
35     mToolBarPadding( toolBarPadding )
36   {}
37
38   float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
39   float mToolBarTitlePercentage;  ///< The tool bar title width is a percentage of the tool bar width.
40   float mToolBarHeight;           ///< The tool bar height (in pixels).
41   float mToolBarPadding;          ///< The tool bar padding (in pixels)..
42 };
43
44 const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 80.f, 4.f );
45
46 const char*                   DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
47 const char*                   DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
48 const Dali::PointSize         DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
49 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::EXTRALIGHT);
50 const Dali::Vector4           DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
51
52 const Dali::Toolkit::Alignment::Padding DEFAULT_PLAY_PADDING(12.0f, 12.0f, 12.0f, 12.0f);
53 const Dali::Toolkit::Alignment::Padding DEFAULT_MODE_SWITCH_PADDING(8.0f, 8.0f, 8.0f, 8.0f);
54
55 static Dali::TextStyle defaultTextStyle;
56 static bool textStyleSet=false;
57
58 float ScalePointSize(int pointSize)
59 {
60   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
61   float meanDpi = (dpi.height + dpi.width) * 0.5f;
62   return pointSize * 220.0f / meanDpi;        // 220 is the default horizontal DPI defined in adaptor Application
63 }
64
65 Dali::TextStyle& GetDefaultTextStyle()
66 {
67   if(!textStyleSet)
68   {
69     defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
70     defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
71     defaultTextStyle.SetFontPointSize(Dali::PointSize(ScalePointSize(DEFAULT_TEXT_STYLE_POINT_SIZE)));
72     defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
73     defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
74     textStyleSet = true;
75   }
76
77   return defaultTextStyle;
78 }
79
80 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
81                            const std::string& toolbarImagePath,
82                            const std::string& title,
83                            const ViewStyle& style,
84                            const Dali::TextStyle& textStyle )
85 {
86   Dali::Layer toolBarLayer = Dali::Layer::New();
87   toolBarLayer.SetName( "TOOLBAR_LAYER" );
88   toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
89   toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
90   toolBarLayer.SetSize( 0.0f, style.mToolBarHeight );
91   toolBarLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::WIDTH );
92
93   // Raise tool bar layer to the top.
94   toolBarLayer.RaiseToTop();
95
96   // Tool bar
97   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
98   Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
99   toolBarBackground.SetName( "TOOLBAR_BACKGROUND" );
100   toolBarBackground.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
101   toolBar = Dali::Toolkit::ToolBar::New();
102   toolBar.SetName( "TOOLBAR" );
103   toolBar.SetBackground( toolBarBackground );
104   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
105   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
106   toolBar.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
107   toolBarBackground.SetSortModifier(1.0f);
108
109   // Add the tool bar to the too bar layer.
110   toolBarLayer.Add( toolBar );
111
112   Dali::Font font = Dali::Font::New();
113
114   // Tool bar text.
115   if( !title.empty() )
116   {
117     Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
118     titleActor.SetName( "TOOLBAR_TITLE" );
119     titleActor.SetText( title );
120     titleActor.SetSize( font.MeasureText( title ) );
121     titleActor.SetStyleToCurrentText(textStyle);
122
123     // Add title to the tool bar.
124     const float padding( style.mToolBarPadding );
125     toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
126   }
127
128   return toolBarLayer;
129 }
130
131 Dali::Layer CreateView( Dali::Application& application,
132                         Dali::Toolkit::View& view,
133                         Dali::Toolkit::ToolBar& toolBar,
134                         const std::string& backgroundImagePath,
135                         const std::string& toolbarImagePath,
136                         const std::string& title,
137                         const ViewStyle& style,
138                         const Dali::TextStyle& textStyle )
139 {
140   Dali::Stage stage = Dali::Stage::GetCurrent();
141
142   // Create default View.
143   view = Dali::Toolkit::View::New();
144   view.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
145
146   // Add the view to the stage before setting the background.
147   stage.Add( view );
148
149   // Set background image, loading it at screen resolution:
150   if ( !backgroundImagePath.empty() )
151   {
152     Dali::ImageAttributes backgroundAttributes;
153     backgroundAttributes.SetSize( stage.GetSize() );
154     backgroundAttributes.SetFilterMode( Dali::ImageAttributes::BoxThenLinear );
155     backgroundAttributes.SetScalingMode( Dali::ImageAttributes::ScaleToFill );
156     Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath, backgroundAttributes );
157     Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
158     view.SetBackground( backgroundImageActor );
159   }
160
161   // FIXME
162   // Connects the orientation signal with the View::OrientationChanged method.
163   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
164
165   // Create default ToolBar
166   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style, textStyle );
167
168   // Add tool bar layer to the view.
169   view.AddContentLayer( toolBarLayer );
170
171   // Create a content layer.
172   Dali::Layer contentLayer = Dali::Layer::New();
173   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
174   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
175   contentLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
176   view.AddContentLayer( contentLayer );
177   contentLayer.LowerBelow( toolBarLayer );
178
179   return contentLayer;
180 }
181
182 Dali::Layer CreateView( Dali::Application& application,
183                         Dali::Toolkit::View& view,
184                         Dali::Toolkit::ToolBar& toolBar,
185                         const std::string& backgroundImagePath,
186                         const std::string& toolbarImagePath,
187                         const std::string& title,
188                         const ViewStyle& style  = DEFAULT_VIEW_STYLE )
189 {
190   return CreateView( application, view, toolBar, backgroundImagePath, toolbarImagePath, title, style,
191                      GetDefaultTextStyle() );
192 }
193
194
195 } // DemoHelper
196
197 #endif // __DALI_DEMO_HELPER_VIEW_H__