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>
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 Stage stage = Stage::GetCurrent();
101 mTapGestureDetector = TapGestureDetector::New();
102 mTapGestureDetector.Attach( stage.GetRootLayer() );
103 mTapGestureDetector.DetectedSignal().Connect( this, &TextFieldExample::OnTap );
105 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
107 Vector2 stageSize = stage.GetSize();
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 );
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 ) );
125 container.Add( mField );
127 Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
129 std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
132 void OnTap( Actor actor, const TapGesture& tapGesture )
134 mField.ClearKeyInputFocus();
138 * Main key event handler
140 void OnKeyEvent(const KeyEvent& event)
142 if(event.state == KeyEvent::Down)
144 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
148 else if( event.IsCtrlModifier() )
150 switch( event.keyCode )
152 // Select rendering back-end
153 case KEY_ZERO: // fall through
156 mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
159 case KEY_H: // Horizontal alignment
161 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
166 mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
169 case KEY_V: // Vertical alignment
171 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
176 mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
179 case KEY_L: // Language
181 const Language& language = LANGUAGES[ mLanguageId ];
183 mField.SetProperty( TextField::Property::TEXT, language.text );
185 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
191 case KEY_S: // Shadow color
193 if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
195 mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
199 mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
203 case KEY_PLUS: // Increase shadow offset
205 mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
208 case KEY_MINUS: // Decrease shadow offset
210 mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
220 Application& mApplication;
224 TapGestureDetector mTapGestureDetector;
226 unsigned int mLanguageId;
227 unsigned int mAlignment;
230 void RunTest( Application& application )
232 TextFieldExample test( application );
234 application.MainLoop();
237 /** Entry point for Linux & Tizen applications */
238 int main( int argc, char **argv )
240 Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
242 RunTest( application );