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