Merge remote-tracking branch 'origin/tizen' into new_text
[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_M = 58;
40   const unsigned int KEY_L = 46;
41 }
42
43 /**
44  * @brief The main class of the demo.
45  */
46 class TextLabelExample : public ConnectionTracker
47 {
48 public:
49
50   TextLabelExample( Application& application )
51   : mApplication( application ),
52     mLanguageId( 0u )
53   {
54     // Connect to the Application's Init signal
55     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
56   }
57
58   ~TextLabelExample()
59   {
60     // Nothing to do here.
61   }
62
63   /**
64    * One-time setup in response to Application InitSignal.
65    */
66   void Create( Application& application )
67   {
68     Stage stage = Stage::GetCurrent();
69
70     stage.SetBackgroundColor( Color::BLUE );
71     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
72     Vector2 stageSize = stage.GetSize();
73
74     CenterLayout centerLayout = CenterLayout::New();
75     centerLayout.SetParentOrigin( ParentOrigin::CENTER );
76     centerLayout.SetSize( stageSize.width*0.6f, stageSize.width*0.6f );
77     stage.Add( centerLayout );
78
79     mLabel = TextLabel::New();
80     mLabel.SetBackgroundColor( Color::BLACK );
81     centerLayout.Add( mLabel );
82
83     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
84     mLabel.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
85
86     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
87     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
88   }
89
90   /**
91    * Main key event handler
92    */
93   void OnKeyEvent(const KeyEvent& event)
94   {
95     if(event.state == KeyEvent::Down)
96     {
97       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
98       {
99         mApplication.Quit();
100       }
101       else if( event.IsCtrlModifier() )
102       {
103         switch( event.keyCode )
104         {
105           case KEY_ZERO: // fall through
106           case KEY_ONE:
107           {
108             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
109             break;
110           }
111           case KEY_M:
112           {
113             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
114             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
115             break;
116           }
117           case KEY_L:
118           {
119             const Language& language = LANGUAGES[ mLanguageId ];
120
121             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
122
123             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
124             {
125               mLanguageId = 0u;
126             }
127           }
128         }
129       }
130     }
131   }
132
133 private:
134
135   Application& mApplication;
136
137   TextLabel mLabel;
138
139   unsigned int mLanguageId;
140 };
141
142 void RunTest( Application& application )
143 {
144   TextLabelExample test( application );
145
146   application.MainLoop();
147 }
148
149 /** Entry point for Linux & Tizen applications */
150 int main( int argc, char **argv )
151 {
152   Application application = Application::New( &argc, &argv );
153
154   RunTest( application );
155
156   return 0;
157 }