DALi Version 2.0.11
[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
63     mTextField.SetProperty(TextField::Property::TEXT_COLOR, Color::BLACK);
64     mTextField.SetProperty(TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder");
65     mTextField.SetProperty(TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name.");
66
67     mButtonSelectionStart = PushButton::New();
68     mButtonSelectionEnd = PushButton::New();
69
70     mButtonSelectionStart.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
71     mButtonSelectionStart.SetProperty(Actor::Property::SIZE, Vector2(140.f, 50.f));
72     mButtonSelectionStart.SetProperty(Actor::Property::POSITION, Vector2(0.f, 80.f));
73     mButtonSelectionStart.SetBackgroundColor(Color::BLUE);
74     mButtonSelectionStart.SetProperty(Button::Property::LABEL, "select <--");
75     mButtonSelectionStart.ClickedSignal().Connect(this, &SimpleTextFieldExample::OnButtonClicked);
76
77     mButtonSelectionEnd.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
78     mButtonSelectionEnd.SetProperty(Actor::Property::SIZE, Vector2(140.f, 50.f));
79     mButtonSelectionEnd.SetProperty(Actor::Property::POSITION, Vector2(0.f, 140.f));
80     mButtonSelectionEnd.SetBackgroundColor(Color::BLUE);
81     mButtonSelectionEnd.SetProperty(Button::Property::LABEL, "select -->");
82     mButtonSelectionEnd.ClickedSignal().Connect(this, &SimpleTextFieldExample::OnButtonClicked);
83
84     mBtnEditable = PushButton::New();
85     mBtnEditable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
86     mBtnEditable.SetProperty(Actor::Property::SIZE, Vector2(250.f, 80.f));
87     mBtnEditable.SetProperty(Actor::Property::POSITION, Vector2(0, 220.f));
88     mBtnEditable.SetBackgroundColor(Color::RED);
89     mBtnEditable.SetProperty(Button::Property::LABEL, "Non-editable");
90     mBtnEditable.ClickedSignal().Connect(this, &SimpleTextFieldExample::OnButtonClicked);
91
92     window.Add(mTextField);
93     window.Add(mButtonSelectionStart);
94     window.Add(mButtonSelectionEnd);
95     window.Add(mBtnEditable);
96   }
97
98   bool OnButtonClicked(Button button)
99   {
100     if(button == mButtonSelectionStart)
101     {
102       int iStart = mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_START).Get<int>() - 1;
103       if (iStart < 0)
104       {
105         iStart = 0;
106       }
107       mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_START, iStart);
108     }
109     else if(button == mButtonSelectionEnd)
110     {
111       mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_END , mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_END).Get<int>() + 1);
112     }
113     else if (mBtnEditable == button)
114     {
115       bool bEditable = !mTextField.GetProperty( DevelTextField::Property::ENABLE_EDITING).Get<int>();
116       mTextField.SetProperty( DevelTextField::Property::ENABLE_EDITING , bEditable);
117       if (bEditable)
118         {
119           mBtnEditable.SetProperty(Button::Property::LABEL, "Non-editable");
120           mBtnEditable.SetBackgroundColor(Color::RED);
121         }
122       else
123         {
124           mBtnEditable.SetProperty(Button::Property::LABEL, "editable");
125           mBtnEditable.SetBackgroundColor(Color::GREEN);
126         }
127     }
128
129     return true;
130   }
131
132   /**
133    * Main key event handler
134    */
135   void OnKeyEvent(const KeyEvent& event)
136   {
137     if(event.GetState() == KeyEvent::DOWN)
138     {
139       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
140       {
141         mApplication.Quit();
142       }
143     }
144   }
145
146 private:
147   Application& mApplication;
148   TextField mTextField;
149   PushButton mButtonSelectionStart;
150   PushButton mButtonSelectionEnd;
151   PushButton mBtnEditable;
152 };
153
154 void RunTest(Application& application)
155 {
156   SimpleTextFieldExample test(application);
157
158   application.MainLoop();
159 }
160
161 /** Entry point for Linux & Tizen applications */
162 int DALI_EXPORT_API main(int argc, char** argv)
163 {
164   // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
165   Application application = Application::New(&argc, &argv);
166
167   RunTest(application);
168
169   return 0;
170 }