[dali_1.0.39] Merge branch 'tizen'
[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 "shared/view.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace
34 {
35
36 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
37
38 const float BORDER_WIDTH = 4.0f;
39
40 } // unnamed namespace
41
42 /**
43  * @brief The main class of the demo.
44  */
45 class TextFieldExample : public ConnectionTracker
46 {
47 public:
48
49   TextFieldExample( Application& application )
50   : mApplication( application )
51   {
52     // Connect to the Application's Init signal
53     mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
54   }
55
56   ~TextFieldExample()
57   {
58     // Nothing to do here.
59   }
60
61   /**
62    * One-time setup in response to Application InitSignal.
63    */
64   void Create( Application& application )
65   {
66     DemoHelper::RequestThemeChange();
67
68     Stage stage = Stage::GetCurrent();
69
70     stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
71
72     Vector2 stageSize = stage.GetSize();
73
74     mContainer = Control::New();
75     mContainer.SetName( "Container" );
76     mContainer.SetParentOrigin( ParentOrigin::CENTER );
77     mContainer.SetSize( Vector2(stageSize.width*0.6f, stageSize.width*0.6f) );
78     mContainer.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
79     mContainer.GetChildAt(0).SetZ(-1.0f);
80     stage.Add( mContainer );
81
82     TextField field = TextField::New();
83     field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
84     field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
85     field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
86
87     mContainer.Add( field );
88
89     field.SetProperty( TextField::Property::TEXT, "Hello" );
90     field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
91
92     Property::Value fieldText = field.GetProperty( TextField::Property::TEXT );
93
94     std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
95   }
96
97   /**
98    * Main key event handler
99    */
100   void OnKeyEvent(const KeyEvent& event)
101   {
102     if(event.state == KeyEvent::Down)
103     {
104       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
105       {
106         mApplication.Quit();
107       }
108     }
109   }
110
111 private:
112
113   Application& mApplication;
114
115   Control mContainer;
116 };
117
118 void RunTest( Application& application )
119 {
120   TextFieldExample test( application );
121
122   application.MainLoop();
123 }
124
125 /** Entry point for Linux & Tizen applications */
126 int main( int argc, char **argv )
127 {
128   Application application = Application::New( &argc, &argv );
129
130   RunTest( application );
131
132   return 0;
133 }