TextView example.
[platform/core/uifw/dali-demo.git] / examples / text-view / text-view-example.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 "../shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26
27 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
28 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
29
30 const char* const TOOLBAR_TITLE = "Text View";
31 const int TOOLBAR_HEIGHT = 80;
32
33 const int NUM_TABLE_ROWS = 12;
34 const int NUM_TABLE_COLUMNS = 12;
35
36 }  // namespace
37
38 /**
39  * Helper structs
40  */
41 struct TableCell
42 {
43   unsigned int row;
44   unsigned int column;
45   unsigned int rowSpan;
46   unsigned int columnSpan;
47 };
48
49 struct TableString
50 {
51   std::string fontName;
52   std::string fontStyle;
53   float fontSize;
54   Dali::TextStyle::Weight fontWeight;
55   Vector4 fontColour;
56
57   std::string text;
58
59   Toolkit::Alignment::Type horizontalAlignment;
60   Toolkit::Alignment::Type verticalAlignment;
61   float padding;
62
63   float orientation;
64
65   TableCell cellPosition;
66 };
67
68 namespace
69 {
70 //                                      Font            Font style  Font point size  Font weight                  Text colour                           Text                          Horizontal alignment                  Vertical alignment                  Padding   Orientation   Table cell position and span
71 const TableString TABLE_STRINGS[] = { { "HelveticaNue", "Regular",  8.0f,            Dali::TextStyle::REGULAR,    Vector4( 0.5f, 1.0f, 0.0f, 1.0f ),    "Howdy",                      Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 0, 0, 2, 4 } },
72                                       { "HelveticaNue", "Regular",  13.0f,           Dali::TextStyle::EXTRABOLD,  Vector4( 1.0f, 0.5f, 0.0f, 1.0f ),    "Hello!",                     Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    0.0f,         { 0, 4, 1, 4 } },
73                                       { "HelveticaNue", "Regular",  18.0f,           Dali::TextStyle::REGULAR,    Vector4( 1.0f, 0.75f, 0.25f, 1.0f ),  "שלום!",                                               Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 1, 4, 1, 4 } },
74                                       { "HelveticaNue", "Regular",  8.0f,            Dali::TextStyle::REGULAR,    Vector4( 0.5f, 0.0f, 1.0f, 1.0f ),    "Hi there",                   Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalBottom, 10.0f,    0.0f,         { 0, 8, 2, 4 } },
75                                       { "HelveticaNue", "Regular",  12.0f,           Dali::TextStyle::REGULAR,    Vector4( 0.5f, 1.0f, 1.0f, 1.0f ),    "Hola",                       Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    90.0f,        { 2, 0, 4, 2 } },
76                                       { "HelveticaNue", "Regular",  18.0f,           Dali::TextStyle::BOLD,       Vector4( 0.5f, 1.0f, 0.5f, 1.0f ),    "Bonjour",                    Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 2, 2, 2, 4 } },
77                                       { "HelveticaNue", "Regular",  12.0f,           Dali::TextStyle::REGULAR,    Vector4( 1.0f, 1.0f, 0.5f, 1.0f ),    "Ciao",                       Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    0.0f,         { 2, 7, 2, 3 } },
78                                       { "HelveticaNue", "Regular",  26.0f,           Dali::TextStyle::EXTRABLACK, Vector4( 0.5f, 0.0f, 0.0f, 1.0f ),    "Hello",                      Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    0.0f,         { 4, 2, 1, 6 } },
79                                       { "HelveticaNue", "Regular",  8.0f,            Dali::TextStyle::DEMIBOLD,   Vector4( 0.0f, 0.5f, 0.0f, 1.0f ),    "Top of the morning to you",  Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    90.0f,        { 4, 10, 8, 2 } },
80                                       { "HelveticaNue", "Regular",  13.0f,           Dali::TextStyle::DEMIBOLD,   Vector4( 1.0f, 1.0f, 1.0f, 1.0f ),    "हैलो",                                                   Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 6, 1, 1, 3 } },
81                                       { "HelveticaNue", "Regular",  8.0f,            Dali::TextStyle::DEMIBOLD,   Vector4( 1.0f, 1.0f, 0.0f, 1.0f ),    "สวัสดี",                       Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    90.0f,        { 6, 5, 2, 1 } },
82                                       { "HelveticaNue", "Regular",  18.0f,           Dali::TextStyle::REGULAR,    Vector4( 0.0f, 1.0f, 1.0f, 1.0f ),    "你好",                        Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalBottom, 10.0f,    0.0f,         { 6, 6, 1, 3 } },
83                                       { "HelveticaNue", "Regular",  34.0f,           Dali::TextStyle::REGULAR,    Vector4( 0.0f, 0.0f, 1.0f, 1.0f ),    "G'day",                      Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    0.0f,         { 7, 0, 2, 10 } },
84                                       { "HelveticaNue", "Regular",  16.0f,           Dali::TextStyle::EXTRABLACK, Vector4( 0.0f, 0.5f, 1.0f, 1.0f ),    "مرحبا",                      Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 9, 1, 2, 4 } },
85                                       { "HelveticaNue", "Regular",  10.0f,           Dali::TextStyle::EXTRABLACK, Vector4( 1.0f, 0.0f, 0.0f, 1.0f ),    "こんにちは",                                         Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f,    0.0f,         { 10, 0, 2, 6 } },
86                                       { "HelveticaNue", "Regular",  14.0f,           Dali::TextStyle::REGULAR,    Vector4( 0.0f, 1.0f, 0.0f, 1.0f ),    "aloha",                      Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop,    10.0f,    0.0f,         { 10, 6, 2, 4 } }
87                                     };
88
89 const int NUM_TABLE_STRINGS = sizeof( TABLE_STRINGS ) / sizeof( TABLE_STRINGS[0] );
90
91 } // namespace
92
93 /**
94  * This example shows the usage of TextView.
95  */
96 class TextViewController: public ConnectionTracker
97 {
98 public:
99
100   TextViewController( Application& application )
101     : mApplication( application )
102   {
103     // Connect to the Application's Init signal
104     mApplication.InitSignal().Connect( this, &TextViewController::Create );
105   }
106
107   ~TextViewController()
108   {
109     // Nothing to do here
110   }
111
112   void Create( Application& application )
113   {
114     // The Init signal is received once (only) during the Application lifetime
115
116     Stage stage = Stage::GetCurrent();
117
118     // Respond to key events
119     stage.KeyEventSignal().Connect(this, &TextViewController::OnKeyEvent);
120
121     // Creates a default view with a default tool bar.
122     // The view is added to the stage.
123     mContentLayer = DemoHelper::CreateView( application,
124                                             mView,
125                                             mToolBar,
126                                             BACKGROUND_IMAGE,
127                                             TOOLBAR_IMAGE,
128                                             TOOLBAR_TITLE );
129
130     // Create a table view the height of the stage
131     Toolkit::TableView textContainer = Toolkit::TableView::New( NUM_TABLE_ROWS, NUM_TABLE_COLUMNS );
132     textContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
133     textContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
134     textContainer.SetPosition( 0, TOOLBAR_HEIGHT );
135     textContainer.SetSize( stage.GetSize().width, stage.GetSize().height - TOOLBAR_HEIGHT );
136
137     mContentLayer.Add( textContainer );
138
139     // Add data
140     for( int i = 0; i < NUM_TABLE_STRINGS; ++i )
141     {
142       const TableString& tableString = TABLE_STRINGS[ i ];
143
144       Dali::TextStyle textStyle;
145       textStyle.SetFontName( tableString.fontName );
146       textStyle.SetFontStyle( tableString.fontStyle );
147       textStyle.SetFontPointSize( Dali::PointSize( DemoHelper::ScalePointSize( tableString.fontSize ) ) );
148       textStyle.SetWeight( tableString.fontWeight );
149       textStyle.SetTextColor( tableString.fontColour );
150
151       Toolkit::TextView textView = Toolkit::TextView::New( tableString.text );
152       textView.SetStyleToCurrentText( textStyle );
153       textView.SetRotation( Dali::Degree( tableString.orientation ), Vector3( 0.0f, 0.0f, 1.0f ) );
154
155       Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( tableString.horizontalAlignment, tableString.verticalAlignment );
156       alignmentContainer.SetPadding( Toolkit::Alignment::Padding( tableString.padding, tableString.padding, tableString.padding, tableString.padding ) );
157       alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
158       alignmentContainer.Add( textView );
159
160       textContainer.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( tableString.cellPosition.row, tableString.cellPosition.column, tableString.cellPosition.rowSpan, tableString.cellPosition.columnSpan ) );
161     }
162   }
163
164   void OnKeyEvent( const KeyEvent& event )
165   {
166     if( event.state == KeyEvent::Down )
167     {
168       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
169       {
170         // Exit application when click back or escape.
171         mApplication.Quit();
172       }
173     }
174   }
175
176 private:
177
178   Application&      mApplication;
179   Toolkit::View     mView;                   ///< The View instance.
180   Toolkit::ToolBar  mToolBar;                ///< The View's Toolbar.
181   Layer             mContentLayer;           ///< Content layer
182
183 };
184
185 void RunTest( Application& application )
186 {
187   TextViewController test( application );
188
189   application.MainLoop();
190 }
191
192 // Entry point for Linux & SLP applications
193 //
194 int main( int argc, char **argv )
195 {
196   Application application = Application::New( &argc, &argv );
197
198   RunTest( application );
199
200   return 0;
201 }