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