[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-demo.git] / examples / layouting / layouting-examples.cpp
1 /*
2  * Copyright (c) 2018 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 // EXTERNAL INCLUDES
19 #include <memory>
20 #include <string>
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24
25 // INTERNAL INCLUDES
26 #include "shared/view.h"
27 #include "linear-example.h"
28 #include "padding-example.h"
29 #include "flex-example.h"
30 #include "grid-example.h"
31 #include "example.h"
32 #include "absolute-example.h"
33
34 using namespace Dali;
35 using namespace Dali::Toolkit;
36
37 namespace
38 {
39
40 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
41 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
42
43 typedef std::unique_ptr< Demo::Example > ExamplePointer;
44
45 typedef std::vector< ExamplePointer > ExampleContainer;
46
47 /// All layouting examples to be shown should be added to this method
48 void CreateExamples( ExampleContainer& container )
49 {
50   container.push_back( ExamplePointer(new Demo::LinearExample) );
51   container.push_back( ExamplePointer(new Demo::PaddingExample) );
52   container.push_back( ExamplePointer(new Demo::AbsoluteExample) );
53   container.push_back( ExamplePointer(new Demo::FlexExample) ) ;
54   container.push_back( ExamplePointer(new Demo::GridExample) ) ;
55 }
56
57 } // anonymous namespace
58
59 class LayoutingExample: public ConnectionTracker
60 {
61  public:
62
63   LayoutingExample( Application& application )
64   : mApplication( application ),
65     mLayoutingExamples(),
66     mLayoutIndex( 0 )
67   {
68     // Connect to the Application's Init signal
69     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
70   }
71
72 private:
73
74   void Create( Application& application )
75   {
76     auto stage = Stage::GetCurrent();
77     stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent );
78
79     auto bg = ImageView::New( BACKGROUND_IMAGE );
80     bg.SetParentOrigin( ParentOrigin::CENTER );
81     stage.Add( bg );
82     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
83     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
84     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
85     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
86     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
87
88     stage.Add( toolbar );
89
90     mToolbarTitle = TextLabel::New( "");
91     mToolbarTitle.SetParentOrigin( ParentOrigin::CENTER );
92     mToolbarTitle.SetAnchorPoint( AnchorPoint::CENTER );
93     mToolbarTitle.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
94     mToolbarTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
95     toolbar.Add( mToolbarTitle );
96
97     mNextLayout = PushButton::New();
98     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "change layout");
99     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
100     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
101     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
102     mNextLayout.SetSize(175, 50);
103     toolbar.Add( mNextLayout );
104
105     CreateExamples( mLayoutingExamples );
106     if( ! mLayoutingExamples.empty() )
107     {
108       mLayoutingExamples[ mLayoutIndex ]->Create();
109       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->GetExampleTitle() );
110     }
111   }
112
113   bool ChangeLayout( Button button )
114   {
115     if( ! mLayoutingExamples.empty() )
116     {
117       mLayoutingExamples[ mLayoutIndex ]->Remove();
118       mLayoutIndex = ( mLayoutIndex + 1 ) % mLayoutingExamples.size();
119       mLayoutingExamples[ mLayoutIndex ]->Create();
120       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->Example::GetExampleTitle() );
121     }
122     return true;
123   }
124
125   /**
126    * Main key event handler
127    */
128   void OnKeyEvent(const KeyEvent& event)
129   {
130     if(event.state == KeyEvent::Down)
131     {
132       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
133       {
134         mApplication.Quit();
135       }
136     }
137   }
138
139
140 private:
141   Application& mApplication;
142   ExampleContainer mLayoutingExamples;
143   PushButton mNextLayout;
144   unsigned int mLayoutIndex;
145   TextLabel mToolbarTitle;
146 };
147
148 int DALI_EXPORT_API main( int argc, char **argv )
149 {
150   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
151   LayoutingExample test( application );
152   application.MainLoop();
153   return 0;
154 };