Prefer black text / white backgrounds by default
[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::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
125
126     container.Add( mField );
127
128     Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
129
130     std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
131   }
132
133   void OnTap( Actor actor, const TapGesture& tapGesture )
134   {
135     mField.ClearKeyInputFocus();
136   }
137
138   /**
139    * Main key event handler
140    */
141   void OnKeyEvent(const KeyEvent& event)
142   {
143     if(event.state == KeyEvent::Down)
144     {
145       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
146       {
147         mApplication.Quit();
148       }
149       else if( event.IsCtrlModifier() )
150       {
151         switch( event.keyCode )
152         {
153           // Select rendering back-end
154           case KEY_ZERO: // fall through
155           case KEY_ONE:
156           {
157             mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
158             break;
159           }
160           case KEY_H: // Horizontal alignment
161           {
162             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
163             {
164               mAlignment = 0u;
165             }
166
167             mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
168             break;
169           }
170           case KEY_V: // Vertical alignment
171           {
172             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
173             {
174               mAlignment = 0u;
175             }
176
177             mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
178             break;
179           }
180           case KEY_L: // Language
181           {
182             const Language& language = LANGUAGES[ mLanguageId ];
183
184             mField.SetProperty( TextField::Property::TEXT, language.text );
185
186             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
187             {
188               mLanguageId = 0u;
189             }
190             break;
191           }
192           case KEY_S: // Shadow color
193           {
194             if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
195             {
196               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
197             }
198             else
199             {
200               mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
201             }
202             break;
203           }
204           case KEY_PLUS: // Increase shadow offset
205           {
206             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
207             break;
208           }
209           case KEY_MINUS: // Decrease shadow offset
210           {
211             mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
212             break;
213           }
214         }
215       }
216     }
217   }
218
219 private:
220
221   Application& mApplication;
222
223   TextField mField;
224
225   TapGestureDetector mTapGestureDetector;
226
227   unsigned int mLanguageId;
228   unsigned int mAlignment;
229 };
230
231 void RunTest( Application& application )
232 {
233   TextFieldExample test( application );
234
235   application.MainLoop();
236 }
237
238 /** Entry point for Linux & Tizen applications */
239 int main( int argc, char **argv )
240 {
241   Application application = Application::New( &argc, &argv );
242
243   RunTest( application );
244
245   return 0;
246 }