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>
23 #include <dali/public-api/adaptor-framework/key.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/integration-api/debug.h>
31 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
32 #include <dali-toolkit/internal/text/rendering/text-backend.h>
33 #include <dali-toolkit/public-api/text/rendering-backend.h>
35 using namespace Dali::Toolkit::Text;
48 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
57 return Toolkit::TextField::New();
60 // Setup properties, signals and actions using the type-registry.
61 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextField, Toolkit::Control, Create );
63 DALI_PROPERTY_REGISTRATION( TextField, "rendering-backend", INTEGER, RENDERING_BACKEND )
64 DALI_PROPERTY_REGISTRATION( TextField, "placeholder-text", STRING, PLACEHOLDER_TEXT )
65 DALI_PROPERTY_REGISTRATION( TextField, "text", STRING, TEXT )
66 DALI_PROPERTY_REGISTRATION( TextField, "font-family", STRING, FONT_FAMILY )
67 DALI_PROPERTY_REGISTRATION( TextField, "font-style", STRING, FONT_STYLE )
68 DALI_PROPERTY_REGISTRATION( TextField, "point-size", FLOAT, POINT_SIZE )
69 DALI_PROPERTY_REGISTRATION( TextField, "exceed-policy", INTEGER, EXCEED_POLICY )
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 int backend = value.Get< 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::FONT_FAMILY:
137 if( impl.mController )
139 std::string fontFamily = value.Get< std::string >();
141 if( impl.mController->GetDefaultFontFamily() != fontFamily )
143 impl.mController->SetDefaultFontFamily( fontFamily );
144 impl.RequestTextRelayout();
149 case Toolkit::TextField::Property::FONT_STYLE:
151 if( impl.mController )
153 std::string fontStyle = value.Get< std::string >();
155 if( impl.mController->GetDefaultFontStyle() != fontStyle )
157 impl.mController->SetDefaultFontStyle( fontStyle );
158 impl.RequestTextRelayout();
163 case Toolkit::TextField::Property::POINT_SIZE:
165 if( impl.mController )
167 float pointSize = value.Get< float >();
169 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
171 impl.mController->SetDefaultPointSize( pointSize );
172 impl.RequestTextRelayout();
177 case Toolkit::TextField::Property::EXCEED_POLICY:
179 impl.mExceedPolicy = value.Get< int >();
182 case Toolkit::TextField::Property::CURSOR_IMAGE:
184 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
186 if( impl.mDecorator )
188 impl.mDecorator->SetCursorImage( image );
192 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
194 if( impl.mDecorator )
196 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
200 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
202 if( impl.mDecorator )
204 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
208 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
210 if( impl.mController )
212 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
216 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
218 if( impl.mDecorator )
220 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
224 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
226 if( impl.mDecorator )
228 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
232 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
234 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
236 if( impl.mDecorator )
238 impl.mDecorator->SetGrabHandleImage( image );
242 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
244 if( impl.mDecorator )
246 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
254 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
256 Property::Value value;
258 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
262 TextField& impl( GetImpl( textField ) );
266 case Toolkit::TextField::Property::RENDERING_BACKEND:
268 value = impl.mRenderingBackend;
271 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
273 if( impl.mController )
276 impl.mController->GetPlaceholderText( text );
281 case Toolkit::TextField::Property::TEXT:
283 if( impl.mController )
286 impl.mController->GetText( text );
291 case Toolkit::TextField::Property::EXCEED_POLICY:
293 value = impl.mExceedPolicy;
296 case Toolkit::TextField::Property::CURSOR_IMAGE:
298 if( impl.mDecorator )
300 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
303 value = image.GetUrl();
308 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
310 if( impl.mDecorator )
312 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
316 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
318 if( impl.mDecorator )
320 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
324 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
326 //value = impl.mController->GetEnableCursorBlink(); TODO
329 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
331 if( impl.mDecorator )
333 value = impl.mDecorator->GetCursorBlinkInterval();
337 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
339 if( impl.mDecorator )
341 value = impl.mDecorator->GetCursorBlinkDuration();
345 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
347 if( impl.mDecorator )
349 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
352 value = image.GetUrl();
357 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
359 if( impl.mDecorator )
361 value = impl.mDecorator->GetBoundingBox();
371 void TextField::OnInitialize()
373 mController = Text::Controller::New( *this );
375 mDecorator = Text::Decorator::New( *this, *mController );
377 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
379 mController->EnableTextInput( mDecorator );
381 // Forward input events to controller
382 EnableGestureDetection(Gesture::Tap);
383 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
385 // Set BoundingBox to stage size if not already set.
386 if ( mDecorator->GetBoundingBox().IsEmpty() )
388 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
389 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
393 Vector3 TextField::GetNaturalSize()
395 return mController->GetNaturalSize();
398 float TextField::GetHeightForWidth( float width )
400 return mController->GetHeightForWidth( width );
403 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
405 if( mController->Relayout( size ) ||
410 mDecorator->Relayout( size );
415 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
418 RenderableActor renderableActor;
421 renderableActor = mRenderer->Render( mController->GetView() );
424 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
426 if( renderableActor != mRenderableActor )
428 UnparentAndReset( mRenderableActor );
429 mRenderableActor = renderableActor;
432 if( mRenderableActor )
434 // Make sure the actor is parented correctly with/without clipping
437 mClipper->GetRootActor().Add( mRenderableActor );
441 Self().Add( mRenderableActor );
447 void TextField::OnTap( const TapGesture& gesture )
451 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
454 bool TextField::OnKeyEvent( const KeyEvent& event )
456 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
458 ClearKeyInputFocus();
461 return mController->KeyEvent( event );
464 void TextField::RequestTextRelayout()
469 void TextField::EnableClipping( bool clipping, const Vector2& size )
473 // Not worth to created clip actor if width or height is equal to zero.
474 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
480 mClipper = Clipper::New( size );
481 self.Add( mClipper->GetRootActor() );
482 self.Add( mClipper->GetImageActor() );
486 mClipper->Refresh( size );
492 // Note - this will automatically remove the root & image actors
497 TextField::TextField()
498 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
499 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
500 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
504 TextField::~TextField()
509 } // namespace Internal
511 } // namespace Toolkit