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