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 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
22 #include <dali/public-api/adaptor-framework/key.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
31 #include <dali-toolkit/internal/text/rendering/text-backend.h>
33 using namespace Dali::Toolkit::Text;
38 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
58 return Toolkit::TextField::New();
61 // Setup properties, signals and actions using the type-registry.
62 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
64 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
65 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
66 DALI_PROPERTY_REGISTRATION( TextField, "text", STRING, TEXT )
67 DALI_PROPERTY_REGISTRATION( TextField, "font-family", STRING, FONT_FAMILY )
68 DALI_PROPERTY_REGISTRATION( TextField, "font-style", STRING, FONT_STYLE )
69 DALI_PROPERTY_REGISTRATION( TextField, "point-size", FLOAT, POINT_SIZE )
70 DALI_PROPERTY_REGISTRATION( TextField, "cursor-image", STRING, CURSOR_IMAGE )
71 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
72 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
73 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
74 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
75 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
76 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
77 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
79 DALI_TYPE_REGISTRATION_END()
83 Toolkit::TextField TextField::New()
85 // Create the implementation, temporarily owned by this handle on stack
86 IntrusivePtr< TextField > impl = new TextField();
88 // Pass ownership to CustomActor handle
89 Toolkit::TextField handle( *impl );
91 // Second-phase init of the implementation
92 // This can only be done after the CustomActor connection has been made...
98 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
100 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
104 TextField& impl( GetImpl( textField ) );
108 case Toolkit::TextField::Property::RENDERING_BACKEND:
110 unsigned int backend = value.Get< unsigned int >();
112 if( impl.mRenderingBackend != backend )
114 impl.mRenderingBackend = backend;
115 impl.mRenderer.Reset();
119 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
121 if( impl.mController )
123 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
127 case Toolkit::TextField::Property::TEXT:
129 if( impl.mController )
131 impl.mController->SetText( value.Get< std::string >() );
135 case Toolkit::TextField::Property::CURSOR_IMAGE:
137 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
139 if( impl.mDecorator )
141 impl.mDecorator->SetCursorImage( image );
145 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
147 if( impl.mDecorator )
149 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
153 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
155 if( impl.mDecorator )
157 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
161 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
163 if( impl.mController )
165 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
169 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
171 if( impl.mDecorator )
173 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
177 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
179 if( impl.mDecorator )
181 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
185 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
187 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
189 if( impl.mDecorator )
191 impl.mDecorator->SetGrabHandleImage( image );
195 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
197 if( impl.mDecorator )
199 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
207 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
209 Property::Value value;
211 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
215 TextField& impl( GetImpl( textField ) );
219 case Toolkit::TextField::Property::RENDERING_BACKEND:
221 value = impl.mRenderingBackend;
224 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
226 if( impl.mController )
229 impl.mController->GetPlaceholderText( text );
234 case Toolkit::TextField::Property::TEXT:
236 if( impl.mController )
239 impl.mController->GetText( text );
244 case Toolkit::TextField::Property::CURSOR_IMAGE:
246 if( impl.mDecorator )
248 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
251 value = image.GetUrl();
256 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
258 if( impl.mDecorator )
260 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
264 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
266 if( impl.mDecorator )
268 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
272 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
274 //value = impl.mController->GetEnableCursorBlink(); TODO
277 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
279 if( impl.mDecorator )
281 value = impl.mDecorator->GetCursorBlinkInterval();
285 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
287 if( impl.mDecorator )
289 value = impl.mDecorator->GetCursorBlinkDuration();
293 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
295 if( impl.mDecorator )
297 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
300 value = image.GetUrl();
305 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
307 if( impl.mDecorator )
309 value = impl.mDecorator->GetBoundingBox();
319 void TextField::OnInitialize()
321 mController = Text::Controller::New( *this );
323 mDecorator = Text::Decorator::New( *this, *mController );
325 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
327 mController->EnableTextInput( mDecorator );
329 // Forward input events to controller
330 EnableGestureDetection(Gesture::Tap);
331 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
333 // Set BoundingBox to stage size if not already set.
334 if ( mDecorator->GetBoundingBox().IsEmpty() )
336 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
337 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
341 Vector3 TextField::GetNaturalSize()
343 return mController->GetNaturalSize();
346 float TextField::GetHeightForWidth( float width )
348 return mController->GetHeightForWidth( width );
351 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
353 if( mController->Relayout( size ) )
357 mDecorator->Relayout( size );
362 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
367 Actor renderableActor = mRenderer->Render( mController->GetView() );
369 if( renderableActor )
371 Self().Add( renderableActor );
377 void TextField::OnTap( const TapGesture& gesture )
381 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
384 bool TextField::OnKeyEvent( const KeyEvent& event )
386 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
388 ClearKeyInputFocus();
391 return mController->KeyEvent( event );
394 void TextField::RequestTextRelayout()
399 TextField::TextField()
400 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
401 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
405 TextField::~TextField()
409 } // namespace Internal
411 } // namespace Toolkit