Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / simple-text-field / simple-text-field.cpp
1 /*
2  * Copyright (c) 2020 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 simple-text-field-example.cpp
20  * @brief Very basic usage of TextField control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <iostream>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 /**
31  * @brief The main class of the demo.
32  */
33 class SimpleTextFieldExample : public ConnectionTracker
34 {
35 public:
36   SimpleTextFieldExample(Application& application)
37   : mApplication(application)
38   {
39     // Connect to the Application's Init signal
40     mApplication.InitSignal().Connect(this, &SimpleTextFieldExample::Create);
41   }
42
43   ~SimpleTextFieldExample()
44   {
45     // Nothing to do here.
46   }
47
48   /**
49    * One-time setup in response to Application InitSignal.
50    */
51   void Create(Application& application)
52   {
53     Window window = application.GetWindow();
54     window.KeyEventSignal().Connect(this, &SimpleTextFieldExample::OnKeyEvent);
55     window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));
56
57     TextField field = TextField::New();
58     field.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
59     field.SetProperty(Actor::Property::SIZE, Vector2(300.f, 60.f));
60     field.SetBackgroundColor(Color::WHITE);
61     field.SetBackgroundColor(Vector4(1.f, 1.f, 1.f, 0.15f));
62
63     field.SetProperty(TextField::Property::TEXT_COLOR, Color::BLACK);
64     field.SetProperty(TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder");
65     field.SetProperty(TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name.");
66
67     window.Add(field);
68   }
69
70   /**
71    * Main key event handler
72    */
73   void OnKeyEvent(const KeyEvent& event)
74   {
75     if(event.GetState() == KeyEvent::DOWN)
76     {
77       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
78       {
79         mApplication.Quit();
80       }
81     }
82   }
83
84 private:
85   Application& mApplication;
86 };
87
88 void RunTest(Application& application)
89 {
90   SimpleTextFieldExample test(application);
91
92   application.MainLoop();
93 }
94
95 /** Entry point for Linux & Tizen applications */
96 int DALI_EXPORT_API main(int argc, char** argv)
97 {
98   // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
99   Application application = Application::New(&argc, &argv);
100
101   RunTest(application);
102
103   return 0;
104 }