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";
41 const char* const FOLDER_OPEN_ICON_IMAGE = DALI_IMAGE_DIR "folder_appicon_empty_open_bg.png";
43 const float BORDER_WIDTH = 4.0f;
45 const Vector3 POPUP_SIZE_FACTOR_TO_PARENT = Vector3( 0.0, 0.25, 0.0 );
47 } // unnamed namespace
50 * @brief The main class of the demo.
52 class TextFieldExample : public ConnectionTracker
56 TextFieldExample( Application& application )
57 : mApplication( application )
59 // Connect to the Application's Init signal
60 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
65 // Nothing to do here.
69 * One-time setup in response to Application InitSignal.
71 void Create( Application& application )
73 Stage stage = Stage::GetCurrent();
75 stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
76 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
78 mButton = CreateFolderButton();
79 mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
83 PushButton CreateFolderButton()
85 PushButton button = PushButton::New();
86 button.SetUnselectedImage( FOLDER_ICON_IMAGE );
87 button.SetSelectedImage( FOLDER_OPEN_ICON_IMAGE );
88 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
89 button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
90 ResourceImage imageClosed = ResourceImage::New( FOLDER_ICON_IMAGE );
91 button.SetSize( imageClosed.GetWidth(), imageClosed.GetHeight() );
96 bool OnButtonClicked( Toolkit::Button button )
98 Stage stage = Stage::GetCurrent();
99 Vector2 stageSize = stage.GetSize();
101 // Remove previously hidden pop-up
102 UnparentAndReset(mPopup);
104 // Launch a pop-up containing TextField
105 mField = CreateTextField( stageSize, mButtonLabel );
106 mPopup = CreatePopup( stageSize.width * 0.8f );
107 mPopup.Add( mField );
108 mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
110 mPopup.SetDisplayState( Popup::SHOWN );
115 TextField CreateTextField( const Vector2& stageSize, const std::string& text )
117 TextField field = TextField::New();
118 field.SetName("textField");
119 field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
120 field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
121 field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
122 field.SetProperty( TextField::Property::TEXT, text );
123 field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
124 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
125 field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
126 field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
131 Popup CreatePopup( float width )
133 Popup popup = Popup::New();
134 popup.SetParentOrigin( ParentOrigin::CENTER );
135 popup.SetAnchorPoint( AnchorPoint::CENTER );
136 popup.SetSize( width, 0.0f );
137 popup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
138 popup.SetSizeModeFactor( POPUP_SIZE_FACTOR_TO_PARENT );
139 popup.TouchedSignal().Connect( this, &TextFieldExample::OnPopupTouched );
144 void OnPopupOutsideTouched()
146 // Update the folder text
147 if( mButton && mField )
149 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
150 mButtonLabel = text.Get< std::string >();
151 mButton.SetLabelText( mButtonLabel );
154 // Hide & discard the pop-up
157 mPopup.SetDisplayState( Popup::HIDDEN );
162 bool OnPopupTouched( Actor actor, const TouchEvent& event )
164 // End edit mode for TextField if parent Popup touched.
165 if(event.GetPointCount() > 0)
167 const TouchPoint& point = event.GetPoint(0);
170 case TouchPoint::Down:
172 // Update the folder text and lose focus for Key events
173 if( mButton && mField )
175 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
176 mButtonLabel = text.Get< std::string >();
177 mButton.SetLabelText( mButtonLabel );
178 mField.ClearKeyInputFocus();
193 * Main key event handler
195 void OnKeyEvent(const KeyEvent& event)
197 if(event.state == KeyEvent::Down)
199 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
208 Application& mApplication;
210 // This button launches a pop-up containing TextField
212 std::string mButtonLabel;
219 void RunTest( Application& application )
221 TextFieldExample test( application );
223 application.MainLoop();
226 /** Entry point for Linux & Tizen applications */
227 int main( int argc, char **argv )
229 // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
230 Application application = Application::New( &argc, &argv );
232 RunTest( application );