[dali_1.0.39] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / docs / content / programming-guide / hello-world.h
1 /*! \page hello-world Hello World - explained
2
3 The following steps are required for displaying the sentence 'Hello World' with Dali:
4
5 - initialize the Dali library
6 - create an Actor showing text
7 - add it to the Stage
8
9 To understand the basic building blocks of the UI make sure to read the chapter on \link fundamentals Dali Fundamentals\endlink first.
10
11 Let's take a look at the code for this test application.
12
13 <h2 class="pg"> Example code </h2>
14 \code
15
16 #include <dali-toolkit/dali-toolkit.h>
17
18 using namespace Dali;
19 using Dali::Toolkit::TextLabel;
20
21 // This example shows how to create and display Hello World! using a simple TextActor
22 //
23 class HelloWorldController : public ConnectionTracker
24 {
25 public:
26
27   HelloWorldController( Application& application )
28   : mApplication( application )
29   {
30     // Connect to the Application's Init signal
31     mApplication.InitSignal().Connect( this, &HelloWorldController::Create );
32   }
33
34   ~HelloWorldController()
35   {
36     // Remove Hello World actor from stage
37     Stage::GetCurrent().Remove(mTextLabel);
38   }
39
40   // The Init signal is received once (only) during the Application lifetime
41   void Create( Application& application )
42   {
43     // Get a handle to the stage
44     Stage stage = Stage::GetCurrent();
45
46     mTextLabel = TextLabel::New( "Hello World" );
47     mTextLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
48     stage.Add( mTextLabel );
49
50     // Respond to a click anywhere on the stage
51     stage.GetRootLayer().TouchedSignal().Connect( this, &HelloWorldController::OnTouch );
52   }
53
54   bool OnTouch( Actor actor, const TouchEvent& touch )
55   {
56     // quit the application
57     mApplication.Quit();
58     return true;
59   }
60
61 private:
62   Application&  mApplication;
63   TextLabel mTextLabel;
64 };
65
66 void RunTest( Application& application )
67 {
68   HelloWorldController test( application );
69
70   application.MainLoop();
71 }
72
73 // Entry point for Linux & Tizen applications
74 //
75 int main( int argc, char **argv )
76 {
77   Application application = Application::New( &argc, &argv );
78
79   RunTest( application );
80
81   return 0;
82 }
83
84 \endcode
85
86  There are a couple of steps which are very important to understand.
87
88  <h2 class="pg"> Initializing Dali </h2>
89  The application should not use the Dali library until it has sent the init complete signal!
90  That's why we connect our ExampleApp::Create callback to Dali::Application's SignalInit signal:
91  \code
92    ...
93    app.SignalInit().Connect(this, &ExampleApp::Create);
94    ...
95  \endcode
96
97  <h2 class="pg"> Reference counting </h2>
98  The application should store Actors' and resources' handles.
99  Dali objects are reference counted, which makes sure they exist only as long as they are needed.
100  That's why we store the Actor's handle:
101  \code
102    ...
103    mTextLabel = TextLabel::New("Hello World");
104    ...
105  \endcode
106  Even if the TextLabel is removed from the stage, it will be kept alive through our reference.\n
107  You can read more about implicit smart-pointer semantics in chapter \link handle-body-idiom Handle – body\endlink.
108
109  <h2 class="pg"> Main loop </h2>
110  To 'run' the application, it's main loop should be started.
111  This ensures that images are displayed, events, signals are dispatched and captured and so on.
112  \code
113    ...
114    daliApp.MainLoop();
115    ...
116  \endcode
117  \n \n
118  On X11 platform you can compile the above example with:
119  \code
120  g++ `pkg-config --libs --cflags dali` hello-dali.cpp -o hello
121  \endcode
122
123  After running './hello' this should be visible on the screen:
124
125 <table border=0 cellpadding=10>
126 <tr>
127   <td>
128   \image html Text-Label.png "Hello world example"
129   </td>
130 </tr>
131 </table>
132
133 */