Fixed memory leak in text-field demo
[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 = DALI_IMAGE_DIR "folder_appicon_empty_bg.png";
41
42   const float BORDER_WIDTH = 4.0f;
43
44   const Vector3 POPUP_SIZE_FACTOR_TO_PARENT = Vector3( 0.0, 0.25, 0.0 );
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     mButton = CreateFolderButton();
78     mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
79     stage.Add( mButton );
80   }
81
82   PushButton CreateFolderButton()
83   {
84     PushButton button = PushButton::New();
85     ResourceImage image = ResourceImage::New( FOLDER_ICON_IMAGE );
86     ImageActor folderButton = ImageActor::New( image );
87     folderButton.SetColor( Color::WHITE );
88     button.SetButtonImage( folderButton );
89     button.SetSelectedImage( Actor() );
90     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
91     button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
92     button.SetSize( image.GetWidth(), image.GetHeight() );
93
94     return button;
95   }
96
97   bool OnButtonClicked( Toolkit::Button button )
98   {
99     Stage stage = Stage::GetCurrent();
100     Vector2 stageSize = stage.GetSize();
101
102     // Remove previously hidden pop-up
103     UnparentAndReset(mPopup);
104
105     // Launch a pop-up containing TextField
106     mField = CreateTextField( stageSize, mButtonLabel );
107     mPopup = CreatePopup( stageSize.width * 0.8f );
108     mPopup.Add( mField );
109     mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
110     mPopup.Show();
111
112     return true;
113   }
114
115   TextField CreateTextField( const Vector2& stageSize, const std::string& text )
116   {
117     TextField field = TextField::New();
118     field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
119     field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
120     field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
121     field.SetProperty( TextField::Property::TEXT, text );
122     field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
123     field.SetProperty( TextField::Property::PRIMARY_CURSOR_COLOR, Color::WHITE );
124     field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
125     field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
126     field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
127
128     return field;
129   }
130
131   Popup CreatePopup( float width )
132   {
133     Popup popup = Popup::New();
134     popup.SetParentOrigin( ParentOrigin::CENTER );
135     popup.SetAnchorPoint( AnchorPoint::CENTER );
136     popup.SetSize( width, 0.0f );
137     popup.HideTail();
138     popup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
139     popup.SetSizeModeFactor( POPUP_SIZE_FACTOR_TO_PARENT );
140     popup.TouchedSignal().Connect( this, &TextFieldExample::OnPopupTouched );
141
142     return popup;
143   }
144
145   void OnPopupOutsideTouched()
146   {
147     // Update the folder text
148     if( mButton && mField )
149     {
150       Property::Value text = mField.GetProperty( TextField::Property::TEXT );
151       mButtonLabel = text.Get< std::string >();
152       mButton.SetLabel( mButtonLabel );
153     }
154
155     // Hide & discard the pop-up
156     if( mPopup )
157     {
158       mPopup.Hide();
159     }
160     mField.Reset();
161   }
162
163   bool OnPopupTouched( Actor actor, const TouchEvent& event )
164   {
165     // End edit mode for TextField if parent Popup touched.
166     if(event.GetPointCount() > 0)
167     {
168       const TouchPoint& point = event.GetPoint(0);
169       switch(point.state)
170       {
171         case TouchPoint::Down:
172         {
173           // Update the folder text and lose focus for Key events
174           if( mButton && mField )
175           {
176             Property::Value text = mField.GetProperty( TextField::Property::TEXT );
177             mButtonLabel = text.Get< std::string >();
178             mButton.SetLabel( mButtonLabel );
179             mField.ClearKeyInputFocus();
180           }
181           break;
182         }
183         default:
184         {
185           break;
186         }
187       } // end switch
188     }
189
190     return true;
191   }
192
193   /**
194    * Main key event handler
195    */
196   void OnKeyEvent(const KeyEvent& event)
197   {
198     if(event.state == KeyEvent::Down)
199     {
200       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
201       {
202         mApplication.Quit();
203       }
204     }
205   }
206
207 private:
208
209   Application& mApplication;
210
211   // This button launches a pop-up containing TextField
212   PushButton mButton;
213   std::string mButtonLabel;
214
215   // Pop-up contents
216   TextField mField;
217   Popup mPopup;
218 };
219
220 void RunTest( Application& application )
221 {
222   TextFieldExample test( application );
223
224   application.MainLoop();
225 }
226
227 /** Entry point for Linux & Tizen applications */
228 int main( int argc, char **argv )
229 {
230   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
231
232   RunTest( application );
233
234   return 0;
235 }