[dali_1.0.41] 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 <dali/public-api/text-abstraction/text-abstraction.h>
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     DemoHelper::RequestThemeChange();
100
101     Stage stage = Stage::GetCurrent();
102
103     mTapGestureDetector = TapGestureDetector::New();
104     mTapGestureDetector.Attach( stage.GetRootLayer() );
105     mTapGestureDetector.DetectedSignal().Connect( this, &TextFieldExample::OnTap );
106
107     stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
108
109     Vector2 stageSize = stage.GetSize();
110
111     Control container = Control::New();
112     container.SetName( "Container" );
113     container.SetParentOrigin( ParentOrigin::CENTER );
114     container.SetSize( Vector2(stageSize.width*0.6f, stageSize.width*0.6f) );
115     container.SetBackgroundColor( Color::WHITE );
116     container.GetChildAt(0).SetZ(-1.0f);
117     stage.Add( container );
118
119     mField = TextField::New();
120     mField.SetAnchorPoint( AnchorPoint::TOP_LEFT );
121     mField.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
122     mField.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
123     mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
124     mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
125     mField.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
126
127     container.Add( mField );
128
129     Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
130
131     std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
132   }
133
134   void OnTap( Actor actor, const TapGesture& tapGesture )
135   {
136     mField.ClearKeyInputFocus();
137   }
138
139   /**
140    * Main key event handler
141    */
142   void OnKeyEvent(const KeyEvent& event)
143   {
144     if(event.state == KeyEvent::Down)
145     {
146       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
147       {
148         mApplication.Quit();
149       }
150       else if( event.IsCtrlModifier() )
151       {
152         switch( event.keyCode )
153         {
154           // Select rendering back-end
155           case KEY_ZERO: // fall through
156           case KEY_ONE:
157           {
158             mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
159             break;
160           }
161           case KEY_H: // Horizontal alignment
162           {
163             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
164             {
165               mAlignment = 0u;
166             }
167
168             mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
169             break;
170           }
171           case KEY_V: // Vertical alignment
172           {
173             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
174             {
175               mAlignment = 0u;
176             }
177
178             mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
179             break;
180           }
181           case KEY_L: // Language
182           {
183             const Language& language = LANGUAGES[ mLanguageId ];
184
185             mField.SetProperty( TextField::Property::TEXT, language.text );
186
187             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
188             {
189               mLanguageId = 0u;
190             }
191             break;
192           }
193           case KEY_S: // Shadow color
194           {
195             if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
196             {
197               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
198             }
199             else
200             {
201               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
202             }
203             break;
204           }
205           case KEY_PLUS: // Increase shadow offset
206           {
207             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
208             break;
209           }
210           case KEY_MINUS: // Decrease shadow offset
211           {
212             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
213             break;
214           }
215         }
216       }
217     }
218   }
219
220 private:
221
222   Application& mApplication;
223
224   TextField mField;
225
226   TapGestureDetector mTapGestureDetector;
227
228   unsigned int mLanguageId;
229   unsigned int mAlignment;
230 };
231
232 void RunTest( Application& application )
233 {
234   TextFieldExample test( application );
235
236   application.MainLoop();
237 }
238
239 /** Entry point for Linux & Tizen applications */
240 int main( int argc, char **argv )
241 {
242   Application application = Application::New( &argc, &argv );
243
244   RunTest( application );
245
246   return 0;
247 }