2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file text-field-example.cpp
20 * @brief Basic usage of TextField control
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.h"
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
38 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
40 const float BORDER_WIDTH = 4.0f;
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;
53 const char* H_ALIGNMENT_STRING_TABLE[] =
60 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
62 const char* V_ALIGNMENT_STRING_TABLE[] =
69 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
71 } // unnamed namespace
74 * @brief The main class of the demo.
76 class TextFieldExample : public ConnectionTracker
80 TextFieldExample( Application& application )
81 : mApplication( application ),
85 // Connect to the Application's Init signal
86 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
91 // Nothing to do here.
95 * One-time setup in response to Application InitSignal.
97 void Create( Application& application )
99 DemoHelper::RequestThemeChange();
101 Stage stage = Stage::GetCurrent();
103 mTapGestureDetector = TapGestureDetector::New();
104 mTapGestureDetector.Attach( stage.GetRootLayer() );
105 mTapGestureDetector.DetectedSignal().Connect( this, &TextFieldExample::OnTap );
107 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
109 Vector2 stageSize = stage.GetSize();
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 );
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 ) );
126 container.Add( mField );
128 Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
130 std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
133 void OnTap( Actor actor, const TapGesture& tapGesture )
135 mField.ClearKeyInputFocus();
139 * Main key event handler
141 void OnKeyEvent(const KeyEvent& event)
143 if(event.state == KeyEvent::Down)
145 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
149 else if( event.IsCtrlModifier() )
151 switch( event.keyCode )
153 // Select rendering back-end
154 case KEY_ZERO: // fall through
157 mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
160 case KEY_H: // Horizontal alignment
162 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
167 mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
170 case KEY_V: // Vertical alignment
172 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
177 mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
180 case KEY_L: // Language
182 const Language& language = LANGUAGES[ mLanguageId ];
184 mField.SetProperty( TextField::Property::TEXT, language.text );
186 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
192 case KEY_S: // Shadow color
194 if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
196 mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
200 mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
204 case KEY_PLUS: // Increase shadow offset
206 mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
209 case KEY_MINUS: // Decrease shadow offset
211 mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
221 Application& mApplication;
225 TapGestureDetector mTapGestureDetector;
227 unsigned int mLanguageId;
228 unsigned int mAlignment;
231 void RunTest( Application& application )
233 TextFieldExample test( application );
235 application.MainLoop();
238 /** Entry point for Linux & Tizen applications */
239 int main( int argc, char **argv )
241 Application application = Application::New( &argc, &argv );
243 RunTest( application );