601e6a5bd583840572d427bf917a73b51ff243ed
[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 <dali-toolkit/devel-api/controls/text-controls/text-field-devel.h>
26 #include <iostream>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 /**
32  * @brief The main class of the demo.
33  */
34 class SimpleTextFieldExample : public ConnectionTracker
35 {
36 public:
37   SimpleTextFieldExample(Application& application)
38   : mApplication(application)
39   {
40     // Connect to the Application's Init signal
41     mApplication.InitSignal().Connect(this, &SimpleTextFieldExample::Create);
42   }
43
44   ~SimpleTextFieldExample()
45   {
46     // Nothing to do here.
47   }
48
49   /**
50    * One-time setup in response to Application InitSignal.
51    */
52   void Create(Application& application)
53   {
54     Window window = application.GetWindow();
55     window.KeyEventSignal().Connect(this, &SimpleTextFieldExample::OnKeyEvent);
56     window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));
57
58     mTextField = TextField::New();
59     mTextField.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
60     mTextField.SetProperty(Actor::Property::SIZE, Vector2(300.f, 60.f));
61     mTextField.SetBackgroundColor(Color::WHITE);
62     mTextField.SetBackgroundColor(Vector4(1.f, 1.f, 1.f, 0.15f));
63
64     mTextField.SetProperty(TextField::Property::TEXT_COLOR, Color::BLACK);
65     mTextField.SetProperty(TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder");
66     mTextField.SetProperty(TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name.");
67
68     mButtonSelectionStart = PushButton::New();
69     mButtonSelectionEnd = PushButton::New();
70
71     mButtonSelectionStart.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
72     mButtonSelectionStart.SetProperty(Actor::Property::SIZE, Vector2(140.f, 50.f));
73     mButtonSelectionStart.SetProperty(Actor::Property::POSITION, Vector2(0.f, 80.f));
74     mButtonSelectionStart.SetBackgroundColor(Color::BLUE);
75     mButtonSelectionStart.SetProperty(Button::Property::LABEL, "select <--");
76     mButtonSelectionStart.ClickedSignal().Connect(this, &SimpleTextFieldExample::OnButtonClicked);
77
78     mButtonSelectionEnd.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
79     mButtonSelectionEnd.SetProperty(Actor::Property::SIZE, Vector2(140.f, 50.f));
80     mButtonSelectionEnd.SetProperty(Actor::Property::POSITION, Vector2(0.f, 140.f));
81     mButtonSelectionEnd.SetBackgroundColor(Color::BLUE);
82     mButtonSelectionEnd.SetProperty(Button::Property::LABEL, "select -->");
83     mButtonSelectionEnd.ClickedSignal().Connect(this, &SimpleTextFieldExample::OnButtonClicked);
84
85     window.Add(mTextField);
86     window.Add(mButtonSelectionStart);
87     window.Add(mButtonSelectionEnd);
88   }
89
90
91   bool OnButtonClicked(Button button)
92   {
93     if(button == mButtonSelectionStart)
94     {
95       int iStart = mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_START).Get<int>() - 1;
96       if (iStart < 0)
97       {
98         iStart = 0;
99       }
100       mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_START, iStart);
101     }
102     else if(button == mButtonSelectionEnd)
103     {
104       mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_END , mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_END).Get<int>() + 1);
105     }
106
107     return true;
108   }
109
110   /**
111    * Main key event handler
112    */
113   void OnKeyEvent(const KeyEvent& event)
114   {
115     if(event.GetState() == KeyEvent::DOWN)
116     {
117       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
118       {
119         mApplication.Quit();
120       }
121     }
122   }
123
124 private:
125   Application& mApplication;
126   TextField mTextField;
127   PushButton mButtonSelectionStart;
128   PushButton mButtonSelectionEnd;
129 };
130
131 void RunTest(Application& application)
132 {
133   SimpleTextFieldExample test(application);
134
135   application.MainLoop();
136 }
137
138 /** Entry point for Linux & Tizen applications */
139 int DALI_EXPORT_API main(int argc, char** argv)
140 {
141   // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
142   Application application = Application::New(&argc, &argv);
143
144   RunTest(application);
145
146   return 0;
147 }