2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file simple-text-field-example.cpp
20 * @brief Very basic usage of TextField control
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/text-controls/text-field-devel.h>
29 using namespace Dali::Toolkit;
32 * @brief The main class of the demo.
34 class SimpleTextFieldExample : public ConnectionTracker
37 SimpleTextFieldExample(Application& application)
38 : mApplication(application)
40 // Connect to the Application's Init signal
41 mApplication.InitSignal().Connect(this, &SimpleTextFieldExample::Create);
44 ~SimpleTextFieldExample()
46 // Nothing to do here.
50 * One-time setup in response to Application InitSignal.
52 void Create(Application& application)
54 Window window = application.GetWindow();
55 window.KeyEventSignal().Connect(this, &SimpleTextFieldExample::OnKeyEvent);
56 window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));
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);
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.");
67 mButtonSelectionStart = PushButton::New();
68 mButtonSelectionEnd = PushButton::New();
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);
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);
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);
92 window.Add(mTextField);
93 window.Add(mButtonSelectionStart);
94 window.Add(mButtonSelectionEnd);
95 window.Add(mBtnEditable);
98 bool OnButtonClicked(Button button)
100 if(button == mButtonSelectionStart)
102 int iStart = mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_START).Get<int>() - 1;
107 mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_START, iStart);
109 else if(button == mButtonSelectionEnd)
111 mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_END, mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_END).Get<int>() + 1);
113 else if(mBtnEditable == button)
115 bool bEditable = !mTextField.GetProperty(DevelTextField::Property::ENABLE_EDITING).Get<int>();
116 mTextField.SetProperty(DevelTextField::Property::ENABLE_EDITING, bEditable);
119 mBtnEditable.SetProperty(Button::Property::LABEL, "Non-editable");
120 mBtnEditable.SetBackgroundColor(Color::RED);
124 mBtnEditable.SetProperty(Button::Property::LABEL, "editable");
125 mBtnEditable.SetBackgroundColor(Color::GREEN);
133 * Main key event handler
135 void OnKeyEvent(const KeyEvent& event)
137 if(event.GetState() == KeyEvent::DOWN)
139 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
147 Application& mApplication;
148 TextField mTextField;
149 PushButton mButtonSelectionStart;
150 PushButton mButtonSelectionEnd;
151 PushButton mBtnEditable;
154 void RunTest(Application& application)
156 SimpleTextFieldExample test(application);
158 application.MainLoop();
161 /** Entry point for Linux & Tizen applications */
162 int DALI_EXPORT_API main(int argc, char** argv)
164 // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
165 Application application = Application::New(&argc, &argv);
167 RunTest(application);