2 * Copyright (c) 2020 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);
62 mTextField.SetBackgroundColor(Vector4(1.f, 1.f, 1.f, 0.15f));
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.");
68 mButtonSelectionStart = PushButton::New();
69 mButtonSelectionEnd = PushButton::New();
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);
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);
85 window.Add(mTextField);
86 window.Add(mButtonSelectionStart);
87 window.Add(mButtonSelectionEnd);
91 bool OnButtonClicked(Button button)
93 if(button == mButtonSelectionStart)
95 int iStart = mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_START).Get<int>() - 1;
100 mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_START, iStart);
102 else if(button == mButtonSelectionEnd)
104 mTextField.SetProperty(DevelTextField::Property::SELECTED_TEXT_END , mTextField.GetProperty(DevelTextField::Property::SELECTED_TEXT_END).Get<int>() + 1);
111 * Main key event handler
113 void OnKeyEvent(const KeyEvent& event)
115 if(event.GetState() == KeyEvent::DOWN)
117 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
125 Application& mApplication;
126 TextField mTextField;
127 PushButton mButtonSelectionStart;
128 PushButton mButtonSelectionEnd;
131 void RunTest(Application& application)
133 SimpleTextFieldExample test(application);
135 application.MainLoop();
138 /** Entry point for Linux & Tizen applications */
139 int DALI_EXPORT_API main(int argc, char** argv)
141 // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
142 Application application = Application::New(&argc, &argv);
144 RunTest(application);