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/common/stage.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/rendering/text-backend.h>
31 using namespace Dali::Toolkit::Text;
36 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
56 return Toolkit::TextField::New();
59 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
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", INT, 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 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
229 case Toolkit::TextField::PROPERTY_TEXT:
231 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
234 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
236 if( impl.mDecorator )
238 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
241 value = image.GetUrl();
246 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
248 if( impl.mDecorator )
250 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
254 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
256 if( impl.mDecorator )
258 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
262 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
264 //value = impl.mController->GetEnableCursorBlink(); TODO
267 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
269 if( impl.mDecorator )
271 value = impl.mDecorator->GetCursorBlinkInterval();
275 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
277 if( impl.mDecorator )
279 value = impl.mDecorator->GetCursorBlinkDuration();
283 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
285 if( impl.mDecorator )
287 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
290 value = image.GetUrl();
295 case Toolkit::TextField::PROPERTY_DECORATION_BOUNDING_BOX:
297 if( impl.mDecorator )
299 value = impl.mDecorator->GetBoundingBox();
309 void TextField::OnInitialize()
311 mController = Text::Controller::New( *this );
313 mDecorator = Text::Decorator::New( *this, *mController );
315 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
317 mController->EnableTextInput( mDecorator );
319 // Forward input events to controller
320 mDoubleTapDetector = TapGestureDetector::New();
321 mDoubleTapDetector.SetMaximumTapsRequired( 2 );
322 mDoubleTapDetector.DetectedSignal().Connect( this, &TextField::OnTap );
323 mDoubleTapDetector.Attach(Self());
325 // Set BoundingBox to stage size if not already set.
326 if ( mDecorator->GetBoundingBox().IsEmpty() )
328 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
329 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
333 Vector3 TextField::GetNaturalSize()
335 return mController->GetNaturalSize();
338 float TextField::GetHeightForWidth( float width )
340 return mController->GetHeightForWidth( width );
343 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
345 if( mController->Relayout( size ) )
349 mDecorator->Relayout( size );
354 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
359 Actor renderableActor = mRenderer->Render( mController->GetView() );
361 if( renderableActor )
363 Self().Add( renderableActor );
369 void TextField::OnTap( Actor actor, const TapGesture& gesture )
371 mController->TapEvent( gesture.numberOfTaps, gesture.localPoint.x, gesture.localPoint.y );
374 void TextField::RequestTextRelayout()
379 TextField::TextField()
380 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
381 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
385 TextField::~TextField()
389 } // namespace Internal
391 } // namespace Toolkit