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