Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / text-field / text-field-example.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 text-field-example.cpp
20  * @brief Basic usage of TextField control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/popup/popup.h>
26 #include <iostream>
27
28 // INTERNAL INCLUDES
29 #include "shared/multi-language-strings.h"
30 #include "shared/view.h"
31
32 using namespace Dali;
33 using namespace Dali::Toolkit;
34 using namespace MultiLanguageStrings;
35
36 namespace
37 {
38 const char* const FOLDER_ICON_IMAGE      = DEMO_IMAGE_DIR "folder_appicon_empty_bg.png";
39 const char* const FOLDER_OPEN_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_open_bg.png";
40
41 const float BORDER_WIDTH = 4.0f;
42
43 } // unnamed namespace
44
45 /**
46  * @brief The main class of the demo.
47  */
48 class TextFieldExample : public ConnectionTracker
49 {
50 public:
51   TextFieldExample(Application& application)
52   : mApplication(application)
53   {
54     // Connect to the Application's Init signal
55     mApplication.InitSignal().Connect(this, &TextFieldExample::Create);
56   }
57
58   ~TextFieldExample()
59   {
60     // Nothing to do here.
61   }
62
63   /**
64    * One-time setup in response to Application InitSignal.
65    */
66   void Create(Application& application)
67   {
68     Window window = application.GetWindow();
69
70     window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));
71     window.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
72
73     mButton = CreateFolderButton();
74     mButton.ClickedSignal().Connect(this, &TextFieldExample::OnButtonClicked);
75     window.Add(mButton);
76   }
77
78   PushButton CreateFolderButton()
79   {
80     PushButton button = PushButton::New();
81     button.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FOLDER_ICON_IMAGE);
82     button.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FOLDER_OPEN_ICON_IMAGE);
83     button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
84     return button;
85   }
86
87   bool OnButtonClicked(Toolkit::Button button)
88   {
89     Window  window     = mApplication.GetWindow();
90     Vector2 windowSize = window.GetSize();
91
92     // Remove previously hidden pop-up
93     UnparentAndReset(mPopup);
94
95     // Launch a pop-up containing TextField
96     mField = CreateTextField(windowSize, mButtonLabel);
97     mPopup = CreatePopup();
98     mPopup.Add(mField);
99     mPopup.OutsideTouchedSignal().Connect(this, &TextFieldExample::OnPopupOutsideTouched);
100     window.Add(mPopup);
101     mPopup.SetDisplayState(Popup::SHOWN);
102
103     return true;
104   }
105
106   TextField CreateTextField(const Vector2& windowSize, const std::string& text)
107   {
108     TextField field = TextField::New();
109     field.SetProperty(Dali::Actor::Property::NAME, "textField");
110     field.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
111     field.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
112     field.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
113     field.SetProperty(TextField::Property::TEXT, text);
114     field.SetProperty(TextField::Property::TEXT_COLOR, Vector4(0.0f, 1.0f, 1.0f, 1.0f)); // CYAN
115     field.SetProperty(TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder");
116     field.SetProperty(TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name.");
117     field.SetProperty(TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>(BORDER_WIDTH, BORDER_WIDTH, windowSize.width - BORDER_WIDTH * 2, windowSize.height - BORDER_WIDTH * 2));
118
119     return field;
120   }
121
122   Popup CreatePopup()
123   {
124     Popup popup = Popup::New();
125     popup.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
126     popup.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
127     popup.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH);
128     popup.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT);
129     popup.TouchedSignal().Connect(this, &TextFieldExample::OnPopupTouched);
130
131     return popup;
132   }
133
134   void OnPopupOutsideTouched()
135   {
136     // Update the folder text
137     if(mButton && mField)
138     {
139       Property::Value text = mField.GetProperty(TextField::Property::TEXT);
140       mButtonLabel         = text.Get<std::string>();
141       mButton.SetProperty(Toolkit::Button::Property::LABEL, mButtonLabel);
142     }
143
144     // Hide & discard the pop-up
145     if(mPopup)
146     {
147       mPopup.SetDisplayState(Popup::HIDDEN);
148     }
149     mField.Reset();
150   }
151
152   bool OnPopupTouched(Actor actor, const TouchEvent& event)
153   {
154     // End edit mode for TextField if parent Popup touched.
155     if((event.GetPointCount() > 0) && (mPopup == event.GetHitActor(0)))
156     {
157       switch(event.GetState(0))
158       {
159         case PointState::DOWN:
160         {
161           // Update the folder text and lose focus for Key events
162           if(mButton && mField)
163           {
164             Property::Value text = mField.GetProperty(TextField::Property::TEXT);
165             mButtonLabel         = text.Get<std::string>();
166             mButton.SetProperty(Toolkit::Button::Property::LABEL, mButtonLabel);
167             mField.ClearKeyInputFocus();
168           }
169           break;
170         }
171         default:
172         {
173           break;
174         }
175       } // end switch
176     }
177
178     return false;
179   }
180
181   /**
182    * Main key event handler
183    */
184   void OnKeyEvent(const KeyEvent& event)
185   {
186     if(event.GetState() == KeyEvent::DOWN)
187     {
188       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
189       {
190         mApplication.Quit();
191       }
192     }
193   }
194
195 private:
196   Application& mApplication;
197
198   // This button launches a pop-up containing TextField
199   PushButton  mButton;
200   std::string mButtonLabel;
201
202   // Pop-up contents
203   TextField mField;
204   Popup     mPopup;
205 };
206
207 int DALI_EXPORT_API main(int argc, char** argv)
208 {
209   // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
210   Application      application = Application::New(&argc, &argv);
211   TextFieldExample test(application);
212   application.MainLoop();
213   return 0;
214 }