2 * Copyright (c) 2019 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>
29 #include "shared/multi-language-strings.h"
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
34 using namespace MultiLanguageStrings;
39 const char* const FOLDER_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_bg.png";
40 const char* const FOLDER_OPEN_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_open_bg.png";
42 const float BORDER_WIDTH = 4.0f;
44 } // unnamed namespace
47 * @brief The main class of the demo.
49 class TextFieldExample : public ConnectionTracker
53 TextFieldExample( Application& application )
54 : mApplication( application )
56 // Connect to the Application's Init signal
57 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
62 // Nothing to do here.
66 * One-time setup in response to Application InitSignal.
68 void Create( Application& application )
70 Stage stage = Stage::GetCurrent();
72 stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
73 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
75 // Hide the indicator bar
76 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
78 mButton = CreateFolderButton();
79 mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
83 PushButton CreateFolderButton()
85 PushButton button = PushButton::New();
86 button.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FOLDER_ICON_IMAGE );
87 button.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FOLDER_OPEN_ICON_IMAGE );
88 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
92 bool OnButtonClicked( Toolkit::Button button )
94 Stage stage = Stage::GetCurrent();
95 Vector2 stageSize = stage.GetSize();
97 // Remove previously hidden pop-up
98 UnparentAndReset(mPopup);
100 // Launch a pop-up containing TextField
101 mField = CreateTextField( stageSize, mButtonLabel );
102 mPopup = CreatePopup();
103 mPopup.Add( mField );
104 mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
106 mPopup.SetDisplayState( Popup::SHOWN );
111 TextField CreateTextField( const Vector2& stageSize, const std::string& text )
113 TextField field = TextField::New();
114 field.SetName("textField");
115 field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
116 field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
117 field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
118 field.SetProperty( TextField::Property::TEXT, text );
119 field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
120 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
121 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
122 field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
129 Popup popup = Popup::New();
130 popup.SetParentOrigin( ParentOrigin::CENTER );
131 popup.SetAnchorPoint( AnchorPoint::CENTER );
132 popup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH );
133 popup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
134 popup.TouchSignal().Connect( this, &TextFieldExample::OnPopupTouched );
139 void OnPopupOutsideTouched()
141 // Update the folder text
142 if( mButton && mField )
144 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
145 mButtonLabel = text.Get< std::string >();
146 mButton.SetProperty( Toolkit::Button::Property::LABEL, mButtonLabel );
149 // Hide & discard the pop-up
152 mPopup.SetDisplayState( Popup::HIDDEN );
157 bool OnPopupTouched( Actor actor, const TouchData& event )
159 // End edit mode for TextField if parent Popup touched.
160 if(event.GetPointCount() > 0)
162 switch( event.GetState( 0 ) )
164 case PointState::DOWN:
166 // Update the folder text and lose focus for Key events
167 if( mButton && mField )
169 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
170 mButtonLabel = text.Get< std::string >();
171 mButton.SetProperty( Toolkit::Button::Property::LABEL, mButtonLabel );
172 mField.ClearKeyInputFocus();
187 * Main key event handler
189 void OnKeyEvent(const KeyEvent& event)
191 if(event.state == KeyEvent::Down)
193 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
202 Application& mApplication;
204 // This button launches a pop-up containing TextField
206 std::string mButtonLabel;
213 int DALI_EXPORT_API main( int argc, char **argv )
215 // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
216 Application application = Application::New( &argc, &argv );
217 TextFieldExample test( application );
218 application.MainLoop();