TextLabels vertically aligned.
[platform/core/uifw/dali-demo.git] / examples / text-label / text-label-example.cpp
1 /*
2  * Copyright (c) 2015 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 /**
19  * @file text-label-example.cpp
20  * @brief Basic usage of TextLabel control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
26
27 // INTERNAL INCLUDES
28 #include "center-layout.h"
29 #include "shared/multi-language-strings.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 namespace
36 {
37   const unsigned int KEY_ZERO = 10;
38   const unsigned int KEY_ONE = 11;
39   const unsigned int KEY_A = 38;
40   const unsigned int KEY_M = 58;
41   const unsigned int KEY_L = 46;
42
43   const char* ALIGNMENT_STRING_TABLE[] =
44   {
45     "BEGIN",
46     "CENTER",
47     "END"
48   };
49
50   const unsigned int ALIGNMENT_STRING_COUNT = sizeof( ALIGNMENT_STRING_TABLE ) / sizeof( ALIGNMENT_STRING_TABLE[0u] );
51 }
52
53 /**
54  * @brief The main class of the demo.
55  */
56 class TextLabelExample : public ConnectionTracker
57 {
58 public:
59
60   TextLabelExample( Application& application )
61   : mApplication( application ),
62     mLanguageId( 0u ),
63     mAlignment( 0u )
64   {
65     // Connect to the Application's Init signal
66     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
67   }
68
69   ~TextLabelExample()
70   {
71     // Nothing to do here.
72   }
73
74   /**
75    * One-time setup in response to Application InitSignal.
76    */
77   void Create( Application& application )
78   {
79     Stage stage = Stage::GetCurrent();
80
81     stage.SetBackgroundColor( Color::BLUE );
82     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
83     Vector2 stageSize = stage.GetSize();
84
85     CenterLayout centerLayout = CenterLayout::New();
86     centerLayout.SetParentOrigin( ParentOrigin::CENTER );
87     centerLayout.SetSize( stageSize.width*0.6f, stageSize.width*0.6f );
88     stage.Add( centerLayout );
89
90     mLabel = TextLabel::New();
91     mLabel.SetBackgroundColor( Color::BLACK );
92     centerLayout.Add( mLabel );
93
94     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
95     mLabel.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
96
97     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
98     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
99   }
100
101   /**
102    * Main key event handler
103    */
104   void OnKeyEvent(const KeyEvent& event)
105   {
106     if(event.state == KeyEvent::Down)
107     {
108       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
109       {
110         mApplication.Quit();
111       }
112       else if( event.IsCtrlModifier() )
113       {
114         switch( event.keyCode )
115         {
116           case KEY_ZERO: // fall through
117           case KEY_ONE:
118           {
119             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
120             break;
121           }
122           case KEY_A:
123           {
124             if( ++mAlignment >= ALIGNMENT_STRING_COUNT )
125             {
126               mAlignment = 0u;
127             }
128
129             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, ALIGNMENT_STRING_TABLE[ mAlignment ] );
130             break;
131           }
132           case KEY_M:
133           {
134             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
135             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
136             break;
137           }
138           case KEY_L:
139           {
140             const Language& language = LANGUAGES[ mLanguageId ];
141
142             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
143
144             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
145             {
146               mLanguageId = 0u;
147             }
148             break;
149           }
150         }
151       }
152     }
153   }
154
155 private:
156
157   Application& mApplication;
158
159   TextLabel mLabel;
160
161   unsigned int mLanguageId;
162   unsigned int mAlignment;
163 };
164
165 void RunTest( Application& application )
166 {
167   TextLabelExample test( application );
168
169   application.MainLoop();
170 }
171
172 /** Entry point for Linux & Tizen applications */
173 int main( int argc, char **argv )
174 {
175   Application application = Application::New( &argc, &argv );
176
177   RunTest( application );
178
179   return 0;
180 }