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/images/resource-image.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
30 #include <dali-toolkit/internal/text/rendering/text-backend.h>
32 using namespace Dali::Toolkit::Text;
37 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
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, "cursor-image", STRING, CURSOR_IMAGE )
70 DALI_PROPERTY_REGISTRATION( TextField, "primary-cursor-color", VECTOR4, PRIMARY_CURSOR_COLOR )
71 DALI_PROPERTY_REGISTRATION( TextField, "secondary-cursor-color", VECTOR4, SECONDARY_CURSOR_COLOR )
72 DALI_PROPERTY_REGISTRATION( TextField, "enable-cursor-blink", BOOLEAN, ENABLE_CURSOR_BLINK )
73 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-interval", FLOAT, CURSOR_BLINK_INTERVAL )
74 DALI_PROPERTY_REGISTRATION( TextField, "cursor-blink-duration", FLOAT, CURSOR_BLINK_DURATION )
75 DALI_PROPERTY_REGISTRATION( TextField, "grab-handle-image", STRING, GRAB_HANDLE_IMAGE )
76 DALI_PROPERTY_REGISTRATION( TextField, "decoration bounding-box", RECTANGLE, DECORATION_BOUNDING_BOX )
78 DALI_TYPE_REGISTRATION_END()
82 Toolkit::TextField TextField::New()
84 // Create the implementation, temporarily owned by this handle on stack
85 IntrusivePtr< TextField > impl = new TextField();
87 // Pass ownership to CustomActor handle
88 Toolkit::TextField handle( *impl );
90 // Second-phase init of the implementation
91 // This can only be done after the CustomActor connection has been made...
97 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
99 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
103 TextField& impl( GetImpl( textField ) );
107 case Toolkit::TextField::Property::RENDERING_BACKEND:
109 unsigned int backend = value.Get< unsigned int >();
111 if( impl.mRenderingBackend != backend )
113 impl.mRenderingBackend = backend;
114 impl.mRenderer.Reset();
118 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
120 if( impl.mController )
122 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
126 case Toolkit::TextField::Property::TEXT:
128 if( impl.mController )
130 impl.mController->SetText( value.Get< std::string >() );
134 case Toolkit::TextField::Property::CURSOR_IMAGE:
136 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
138 if( impl.mDecorator )
140 impl.mDecorator->SetCursorImage( image );
144 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
146 if( impl.mDecorator )
148 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
152 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
154 if( impl.mDecorator )
156 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
160 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
162 if( impl.mController )
164 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
168 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
170 if( impl.mDecorator )
172 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
176 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
178 if( impl.mDecorator )
180 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
184 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
186 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
188 if( impl.mDecorator )
190 impl.mDecorator->SetGrabHandleImage( image );
194 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
196 if( impl.mDecorator )
198 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
206 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
208 Property::Value value;
210 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
214 TextField& impl( GetImpl( textField ) );
218 case Toolkit::TextField::Property::RENDERING_BACKEND:
220 value = impl.mRenderingBackend;
223 case Toolkit::TextField::Property::PLACEHOLDER_TEXT:
225 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
228 case Toolkit::TextField::Property::TEXT:
230 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
233 case Toolkit::TextField::Property::CURSOR_IMAGE:
235 if( impl.mDecorator )
237 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
240 value = image.GetUrl();
245 case Toolkit::TextField::Property::PRIMARY_CURSOR_COLOR:
247 if( impl.mDecorator )
249 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
253 case Toolkit::TextField::Property::SECONDARY_CURSOR_COLOR:
255 if( impl.mDecorator )
257 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
261 case Toolkit::TextField::Property::ENABLE_CURSOR_BLINK:
263 //value = impl.mController->GetEnableCursorBlink(); TODO
266 case Toolkit::TextField::Property::CURSOR_BLINK_INTERVAL:
268 if( impl.mDecorator )
270 value = impl.mDecorator->GetCursorBlinkInterval();
274 case Toolkit::TextField::Property::CURSOR_BLINK_DURATION:
276 if( impl.mDecorator )
278 value = impl.mDecorator->GetCursorBlinkDuration();
282 case Toolkit::TextField::Property::GRAB_HANDLE_IMAGE:
284 if( impl.mDecorator )
286 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
289 value = image.GetUrl();
294 case Toolkit::TextField::Property::DECORATION_BOUNDING_BOX:
296 if( impl.mDecorator )
298 value = impl.mDecorator->GetBoundingBox();
308 void TextField::OnInitialize()
310 mController = Text::Controller::New( *this );
312 mDecorator = Text::Decorator::New( *this, *mController );
314 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
316 mController->EnableTextInput( mDecorator );
318 // Forward input events to controller
319 mDoubleTapDetector = TapGestureDetector::New();
320 mDoubleTapDetector.SetMaximumTapsRequired( 2 );
321 mDoubleTapDetector.DetectedSignal().Connect( this, &TextField::OnTap );
322 mDoubleTapDetector.Attach(Self());
324 // Set BoundingBox to stage size if not already set.
325 if ( mDecorator->GetBoundingBox().IsEmpty() )
327 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
328 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
332 Vector3 TextField::GetNaturalSize()
334 return mController->GetNaturalSize();
337 float TextField::GetHeightForWidth( float width )
339 return mController->GetHeightForWidth( width );
342 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
344 if( mController->Relayout( size ) )
348 mDecorator->Relayout( size );
353 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
358 Actor renderableActor = mRenderer->Render( mController->GetView() );
360 if( renderableActor )
362 Self().Add( renderableActor );
368 void TextField::OnTap( Actor actor, const TapGesture& gesture )
370 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
373 void TextField::RequestTextRelayout()
378 TextField::TextField()
379 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
380 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
384 TextField::~TextField()
388 } // namespace Internal
390 } // namespace Toolkit