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-toolkit/devel-api/controls/popup/popup.h>
27 #include <dali/public-api/events/touch-point.h>
30 #include "shared/multi-language-strings.h"
31 #include "shared/view.h"
34 using namespace Dali::Toolkit;
35 using namespace MultiLanguageStrings;
40 const char* const FOLDER_ICON_IMAGE = DALI_IMAGE_DIR "folder_appicon_empty_bg.png";
42 const float BORDER_WIDTH = 4.0f;
44 const Vector3 POPUP_SIZE_FACTOR_TO_PARENT = Vector3( 0.0, 0.25, 0.0 );
46 } // unnamed namespace
49 * @brief The main class of the demo.
51 class TextFieldExample : public ConnectionTracker
55 TextFieldExample( Application& application )
56 : mApplication( application )
58 // Connect to the Application's Init signal
59 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
64 // Nothing to do here.
68 * One-time setup in response to Application InitSignal.
70 void Create( Application& application )
72 Stage stage = Stage::GetCurrent();
74 stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
75 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
77 mButton = CreateFolderButton();
78 mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
82 PushButton CreateFolderButton()
84 PushButton button = PushButton::New();
85 ResourceImage image = ResourceImage::New( FOLDER_ICON_IMAGE );
86 ImageActor folderButton = ImageActor::New( image );
87 folderButton.SetColor( Color::WHITE );
88 button.SetButtonImage( folderButton );
89 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
90 button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
91 button.SetSize( image.GetWidth(), image.GetHeight() );
96 bool OnButtonClicked( Toolkit::Button button )
98 Stage stage = Stage::GetCurrent();
99 Vector2 stageSize = stage.GetSize();
101 // Launch a pop-up containing TextField
102 mField = CreateTextField( stageSize, mButtonLabel );
103 mPopup = CreatePopup( stageSize.width * 0.8f );
104 mPopup.Add( mField );
105 mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
111 TextField CreateTextField( const Vector2& stageSize, const std::string& text )
113 TextField field = TextField::New();
114 field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
115 field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116 field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
117 field.SetProperty( TextField::Property::TEXT, text );
118 field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
119 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
120 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
121 field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
126 Popup CreatePopup( float width )
128 Popup popup = Popup::New();
129 popup.SetParentOrigin( ParentOrigin::CENTER );
130 popup.SetAnchorPoint( AnchorPoint::CENTER );
131 popup.SetSize( width, 0.0f );
133 popup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
134 popup.SetSizeModeFactor( POPUP_SIZE_FACTOR_TO_PARENT );
135 popup.TouchedSignal().Connect( this, &TextFieldExample::OnPopupTouched );
140 void OnPopupOutsideTouched()
142 // Update the folder text
143 if( mButton && mField )
145 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
146 mButtonLabel = text.Get< std::string >();
147 mButton.SetLabel( mButtonLabel );
150 // Hide & discard the pop-up
159 bool OnPopupTouched( Actor actor, const TouchEvent& event )
161 // End edit mode for TextField if parent Popup touched.
162 if(event.GetPointCount() > 0)
164 const TouchPoint& point = event.GetPoint(0);
167 case TouchPoint::Down:
169 // Update the folder text and lose focus for Key events
170 if( mButton && mField )
172 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
173 mButtonLabel = text.Get< std::string >();
174 mButton.SetLabel( mButtonLabel );
175 mField.ClearKeyInputFocus();
190 * Main key event handler
192 void OnKeyEvent(const KeyEvent& event)
194 if(event.state == KeyEvent::Down)
196 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
205 Application& mApplication;
207 // This button launches a pop-up containing TextField
209 std::string mButtonLabel;
216 void RunTest( Application& application )
218 TextFieldExample test( application );
220 application.MainLoop();
223 /** Entry point for Linux & Tizen applications */
224 int main( int argc, char **argv )
226 Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
228 RunTest( application );