Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-demo.git] / examples / text-field / text-field-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-field-example.cpp
20  * @brief Basic usage of TextField 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 "edit-layout.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace
34 {
35
36 const float BORDER_WIDTH = 4.0f;
37
38 } // unnamed namespace
39
40 /**
41  * @brief The main class of the demo.
42  */
43 class TextFieldExample : public ConnectionTracker
44 {
45 public:
46
47   TextFieldExample( Application& application )
48   : mApplication( application )
49   {
50     // Connect to the Application's Init signal
51     mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
52   }
53
54   ~TextFieldExample()
55   {
56     // Nothing to do here.
57   }
58
59   /**
60    * One-time setup in response to Application InitSignal.
61    */
62   void Create( Application& application )
63   {
64     Stage stage = Stage::GetCurrent();
65
66     stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
67
68     Vector2 stageSize = stage.GetSize();
69
70     EditLayout layout = EditLayout::New();
71     layout.SetParentOrigin( ParentOrigin::CENTER );
72     layout.SetAnchorPoint( AnchorPoint::CENTER );
73     layout.SetSize( stageSize.width - BORDER_WIDTH*2.0f, stageSize.height*0.2f );
74     stage.Add( layout );
75
76     TextField field = TextField::New();
77     field.SetParentOrigin( ParentOrigin::CENTER );
78     field.SetBackgroundColor( Color::BLACK );
79     layout.SetTopPanel( field );
80
81     field.SetProperty( TextField::PROPERTY_TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
82
83     // TODO
84     //Property::Value fieldText = field.GetProperty( TextField::PROPERTY_TEXT );
85     //std::cout << "Got text from field: " << fieldText.Get< std::string >() << std::endl;
86   }
87
88   /**
89    * Main key event handler
90    */
91   void OnKeyEvent(const KeyEvent& event)
92   {
93     if(event.state == KeyEvent::Down)
94     {
95       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
96       {
97         mApplication.Quit();
98       }
99     }
100   }
101
102 private:
103
104   Application& mApplication;
105 };
106
107 void RunTest( Application& application )
108 {
109   TextFieldExample test( application );
110
111   application.MainLoop();
112 }
113
114 /** Entry point for Linux & Tizen applications */
115 int main( int argc, char **argv )
116 {
117   Application application = Application::New( &argc, &argv );
118
119   RunTest( application );
120
121   return 0;
122 }