[dali_1.0.42] Merge branch 'tizen'
[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 <iostream>
26
27 // INTERNAL INCLUDES
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 namespace
36 {
37
38   const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
39
40   const float BORDER_WIDTH = 4.0f;
41
42   const unsigned int KEY_ZERO = 10;
43   const unsigned int KEY_ONE = 11;
44   const unsigned int KEY_F = 41;
45   const unsigned int KEY_H = 43;
46   const unsigned int KEY_V = 55;
47   const unsigned int KEY_M = 58;
48   const unsigned int KEY_L = 46;
49   const unsigned int KEY_S = 39;
50   const unsigned int KEY_PLUS = 21;
51   const unsigned int KEY_MINUS = 20;
52
53   const char* H_ALIGNMENT_STRING_TABLE[] =
54   {
55     "BEGIN",
56     "CENTER",
57     "END"
58   };
59
60   const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
61
62   const char* V_ALIGNMENT_STRING_TABLE[] =
63   {
64     "TOP",
65     "CENTER",
66     "BOTTOM"
67   };
68
69   const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
70
71 } // unnamed namespace
72
73 /**
74  * @brief The main class of the demo.
75  */
76 class TextFieldExample : public ConnectionTracker
77 {
78 public:
79
80   TextFieldExample( Application& application )
81   : mApplication( application ),
82     mLanguageId( 0u ),
83     mAlignment( 0u )
84   {
85     // Connect to the Application's Init signal
86     mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
87   }
88
89   ~TextFieldExample()
90   {
91     // Nothing to do here.
92   }
93
94   /**
95    * One-time setup in response to Application InitSignal.
96    */
97   void Create( Application& application )
98   {
99     Stage stage = Stage::GetCurrent();
100
101     mTapGestureDetector = TapGestureDetector::New();
102     mTapGestureDetector.Attach( stage.GetRootLayer() );
103     mTapGestureDetector.DetectedSignal().Connect( this, &TextFieldExample::OnTap );
104
105     stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
106
107     Vector2 stageSize = stage.GetSize();
108
109     Control container = Control::New();
110     container.SetName( "Container" );
111     container.SetParentOrigin( ParentOrigin::CENTER );
112     container.SetSize( Vector2(stageSize.width*0.6f, stageSize.width*0.6f) );
113     container.SetBackgroundColor( Color::WHITE );
114     container.GetChildAt(0).SetZ(-1.0f);
115     stage.Add( container );
116
117     mField = TextField::New();
118     mField.SetAnchorPoint( AnchorPoint::TOP_LEFT );
119     mField.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
120     mField.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
121     mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
122     mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
123     mField.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
124
125     container.Add( mField );
126
127     Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
128
129     std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
130   }
131
132   void OnTap( Actor actor, const TapGesture& tapGesture )
133   {
134     mField.ClearKeyInputFocus();
135   }
136
137   /**
138    * Main key event handler
139    */
140   void OnKeyEvent(const KeyEvent& event)
141   {
142     if(event.state == KeyEvent::Down)
143     {
144       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
145       {
146         mApplication.Quit();
147       }
148       else if( event.IsCtrlModifier() )
149       {
150         switch( event.keyCode )
151         {
152           // Select rendering back-end
153           case KEY_ZERO: // fall through
154           case KEY_ONE:
155           {
156             mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
157             break;
158           }
159           case KEY_H: // Horizontal alignment
160           {
161             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
162             {
163               mAlignment = 0u;
164             }
165
166             mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
167             break;
168           }
169           case KEY_V: // Vertical alignment
170           {
171             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
172             {
173               mAlignment = 0u;
174             }
175
176             mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
177             break;
178           }
179           case KEY_L: // Language
180           {
181             const Language& language = LANGUAGES[ mLanguageId ];
182
183             mField.SetProperty( TextField::Property::TEXT, language.text );
184
185             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
186             {
187               mLanguageId = 0u;
188             }
189             break;
190           }
191           case KEY_S: // Shadow color
192           {
193             if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
194             {
195               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
196             }
197             else
198             {
199               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
200             }
201             break;
202           }
203           case KEY_PLUS: // Increase shadow offset
204           {
205             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
206             break;
207           }
208           case KEY_MINUS: // Decrease shadow offset
209           {
210             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
211             break;
212           }
213         }
214       }
215     }
216   }
217
218 private:
219
220   Application& mApplication;
221
222   TextField mField;
223
224   TapGestureDetector mTapGestureDetector;
225
226   unsigned int mLanguageId;
227   unsigned int mAlignment;
228 };
229
230 void RunTest( Application& application )
231 {
232   TextFieldExample test( application );
233
234   application.MainLoop();
235 }
236
237 /** Entry point for Linux & Tizen applications */
238 int main( int argc, char **argv )
239 {
240   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
241
242   RunTest( application );
243
244   return 0;
245 }