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