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/public-api/text/rendering-backend.h>
32 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
33 #include <dali-toolkit/internal/text/rendering/text-backend.h>
34 #include <dali-toolkit/internal/styling/style-manager-impl.h>
36 using namespace Dali::Toolkit::Text;
49 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
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, "exceed-policy", INTEGER, EXCEED_POLICY )
71 DALI_PROPERTY_REGISTRATION( TextField, "cursor-image", STRING, CURSOR_IMAGE )
72 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
73 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
74 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
75 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
76 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
77 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
78 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
80 DALI_TYPE_REGISTRATION_END()
84 Toolkit::TextField TextField::New()
86 // Create the implementation, temporarily owned by this handle on stack
87 IntrusivePtr< TextField > impl = new TextField();
89 // Pass ownership to CustomActor handle
90 Toolkit::TextField handle( *impl );
92 // Second-phase init of the implementation
93 // This can only be done after the CustomActor connection has been made...
99 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
101 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
105 TextField& impl( GetImpl( textField ) );
109 case Toolkit::TextField::Property::RENDERING_BACKEND:
111 int backend = value.Get< int >();
113 if( impl.mRenderingBackend != backend )
115 impl.mRenderingBackend = backend;
116 impl.mRenderer.Reset();
120 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
122 if( impl.mController )
124 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
128 case Toolkit::TextField::Property::TEXT:
130 if( impl.mController )
132 impl.mController->SetText( value.Get< std::string >() );
136 case Toolkit::TextField::Property::FONT_FAMILY:
138 if( impl.mController )
140 std::string fontFamily = value.Get< std::string >();
142 if( impl.mController->GetDefaultFontFamily() != fontFamily )
144 impl.mController->SetDefaultFontFamily( fontFamily );
145 impl.RequestTextRelayout();
150 case Toolkit::TextField::Property::FONT_STYLE:
152 if( impl.mController )
154 std::string fontStyle = value.Get< std::string >();
156 if( impl.mController->GetDefaultFontStyle() != fontStyle )
158 impl.mController->SetDefaultFontStyle( fontStyle );
159 impl.RequestTextRelayout();
164 case Toolkit::TextField::Property::POINT_SIZE:
166 if( impl.mController )
168 float pointSize = value.Get< float >();
170 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
172 impl.mController->SetDefaultPointSize( pointSize );
173 impl.RequestTextRelayout();
178 case Toolkit::TextField::Property::EXCEED_POLICY:
180 impl.mExceedPolicy = value.Get< int >();
183 case Toolkit::TextField::Property::CURSOR_IMAGE:
185 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
187 if( impl.mDecorator )
189 impl.mDecorator->SetCursorImage( image );
193 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
195 if( impl.mDecorator )
197 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
201 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
203 if( impl.mDecorator )
205 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
209 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
211 if( impl.mController )
213 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
217 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
219 if( impl.mDecorator )
221 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
225 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
227 if( impl.mDecorator )
229 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
233 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
235 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
237 if( impl.mDecorator )
239 impl.mDecorator->SetGrabHandleImage( image );
243 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
245 if( impl.mDecorator )
247 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
255 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
257 Property::Value value;
259 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
263 TextField& impl( GetImpl( textField ) );
267 case Toolkit::TextField::Property::RENDERING_BACKEND:
269 value = impl.mRenderingBackend;
272 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
274 if( impl.mController )
277 impl.mController->GetPlaceholderText( text );
282 case Toolkit::TextField::Property::TEXT:
284 if( impl.mController )
287 impl.mController->GetText( text );
292 case Toolkit::TextField::Property::EXCEED_POLICY:
294 value = impl.mExceedPolicy;
297 case Toolkit::TextField::Property::CURSOR_IMAGE:
299 if( impl.mDecorator )
301 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
304 value = image.GetUrl();
309 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
311 if( impl.mDecorator )
313 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
317 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
319 if( impl.mDecorator )
321 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
325 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
327 //value = impl.mController->GetEnableCursorBlink(); TODO
330 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
332 if( impl.mDecorator )
334 value = impl.mDecorator->GetCursorBlinkInterval();
338 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
340 if( impl.mDecorator )
342 value = impl.mDecorator->GetCursorBlinkDuration();
346 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
348 if( impl.mDecorator )
350 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
353 value = image.GetUrl();
358 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
360 if( impl.mDecorator )
362 value = impl.mDecorator->GetBoundingBox();
372 void TextField::OnInitialize()
374 mController = Text::Controller::New( *this );
376 mDecorator = Text::Decorator::New( *this, *mController );
378 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
380 mController->EnableTextInput( mDecorator );
382 // Forward input events to controller
383 EnableGestureDetection(Gesture::Tap);
384 GetTapGestureDetector().SetMaximumTapsRequired( 2 );
386 // Set BoundingBox to stage size if not already set.
387 if ( mDecorator->GetBoundingBox().IsEmpty() )
389 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
390 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
394 void TextField::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
396 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
399 Vector3 TextField::GetNaturalSize()
401 return mController->GetNaturalSize();
404 float TextField::GetHeightForWidth( float width )
406 return mController->GetHeightForWidth( width );
409 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
411 if( mController->Relayout( size ) ||
416 mDecorator->Relayout( size );
421 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
424 RenderableActor renderableActor;
427 renderableActor = mRenderer->Render( mController->GetView() );
430 EnableClipping( (Dali::Toolkit::TextField::EXCEED_POLICY_CLIP == mExceedPolicy), size );
432 if( renderableActor != mRenderableActor )
434 UnparentAndReset( mRenderableActor );
435 mRenderableActor = renderableActor;
438 if( mRenderableActor )
440 // Make sure the actor is parented correctly with/without clipping
443 mClipper->GetRootActor().Add( mRenderableActor );
447 Self().Add( mRenderableActor );
453 void TextField::OnTap( const TapGesture& gesture )
457 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
460 bool TextField::OnKeyEvent( const KeyEvent& event )
462 if( Dali::DALI_KEY_ESCAPE == event.keyCode )
464 ClearKeyInputFocus();
467 return mController->KeyEvent( event );
470 void TextField::RequestTextRelayout()
475 void TextField::EnableClipping( bool clipping, const Vector2& size )
479 // Not worth to created clip actor if width or height is equal to zero.
480 if( size.width > Math::MACHINE_EPSILON_1000 && size.height > Math::MACHINE_EPSILON_1000 )
486 mClipper = Clipper::New( size );
487 self.Add( mClipper->GetRootActor() );
488 self.Add( mClipper->GetImageActor() );
492 mClipper->Refresh( size );
498 // Note - this will automatically remove the root & image actors
503 TextField::TextField()
504 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
505 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
506 mExceedPolicy( Dali::Toolkit::TextField::EXCEED_POLICY_CLIP )
510 TextField::~TextField()
515 } // namespace Internal
517 } // namespace Toolkit