df94e65f4ab7ff0a495f48054d83efb82e762fba
[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 #include <string>
19 #include "shared/view.h"
20 #include "linear-example.h"
21 #include "padding-example.h"
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31
32 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
33 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
34
35 const char* APPLICATION_TITLE( "Layout tester" );
36
37 }  // namespace
38
39 class LayoutingExample: public ConnectionTracker
40 {
41  public:
42
43   LayoutingExample( Application& application )
44   : mApplication( application ),
45     mLinearExample(),
46     mPaddedExample(),
47     mLayoutIndex( 0 )
48   {
49     // Connect to the Application's Init signal
50     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
51   }
52
53   ~LayoutingExample()
54   {
55     // Nothing to do here
56   }
57
58   void Create( Application& application )
59   {
60     // The Init signal is received once (only) during the Application lifetime
61
62     auto stage = Stage::GetCurrent();
63     stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent );
64
65     auto bg = ImageView::New( BACKGROUND_IMAGE );
66     bg.SetParentOrigin( ParentOrigin::CENTER );
67     stage.Add( bg );
68     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
69     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
70     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
71     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
72     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
73
74     stage.Add( toolbar );
75
76     auto title = TextLabel::New( APPLICATION_TITLE );
77     title.SetParentOrigin( ParentOrigin::CENTER );
78     title.SetAnchorPoint( AnchorPoint::CENTER );
79     title.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
80     title.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
81     toolbar.Add( title );
82
83     mNextLayout = PushButton::New();
84     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "change layout");
85     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
86     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
87     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
88     mNextLayout.SetSize(175, 50);
89     toolbar.Add( mNextLayout );
90
91     mLinearExample.Demo::LinearExample::Create();
92   }
93
94   bool ChangeLayout( Button button )
95   {
96     mLayoutIndex++;
97
98     switch( mLayoutIndex )
99     {
100       case 1 :
101       {
102         mLinearExample.Remove();
103         mPaddedExample.Create();
104         break;
105       }
106       case 2 :
107       {
108         mPaddedExample.Remove();
109         mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "end of test");
110         mNextLayout.SetProperty( Toolkit::Button::Property::DISABLED, true );
111         break;
112       }
113       default :
114       {
115         break;
116       }
117     }
118
119     return true;
120   }
121
122 private:
123   /**
124    * Main key event handler
125    */
126   void OnKeyEvent(const KeyEvent& event)
127   {
128     if(event.state == KeyEvent::Down)
129     {
130       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
131       {
132         mApplication.Quit();
133       }
134     }
135   }
136
137
138 private:
139   Application& mApplication;
140   Demo::LinearExample mLinearExample;
141   Demo::PaddingExample mPaddedExample;
142   PushButton mNextLayout;
143   unsigned int mLayoutIndex;
144 };
145
146 int DALI_EXPORT_API main( int argc, char **argv )
147 {
148   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
149   LayoutingExample test( application );
150   application.MainLoop();
151   return 0;
152 };