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/public-api/text/layouts/layout-engine.h>
29 #include <dali-toolkit/public-api/text/rendering/text-backend.h>
31 using namespace Dali::Toolkit::Text;
36 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
47 const Property::Index TextField::PROPERTY_RENDERING_BACKEND( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX );
48 const Property::Index TextField::PROPERTY_PLACEHOLDER_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 1 );
49 const Property::Index TextField::PROPERTY_TEXT( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 2 );
50 const Property::Index TextField::PROPERTY_CURSOR_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 3 );
51 const Property::Index TextField::PROPERTY_PRIMARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 4 );
52 const Property::Index TextField::PROPERTY_SECONDARY_CURSOR_COLOR( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 5 );
53 const Property::Index TextField::PROPERTY_ENABLE_CURSOR_BLINK( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 6 );
54 const Property::Index TextField::PROPERTY_CURSOR_BLINK_INTERVAL( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 7 );
55 const Property::Index TextField::PROPERTY_CURSOR_BLINK_DURATION( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 8 );
56 const Property::Index TextField::PROPERTY_GRAB_HANDLE_IMAGE( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 9 );
57 const Property::Index TextField::PROPERTY_DECORATION_BOUNDING_BOX( Internal::TextField::TEXTFIELD_PROPERTY_START_INDEX + 10 );
68 return Toolkit::TextField::New();
71 TypeRegistration mType( typeid(Toolkit::TextField), typeid(Toolkit::Control), Create );
73 PropertyRegistration property01( mType, "rendering-backend", Toolkit::TextField::PROPERTY_RENDERING_BACKEND, Property::INTEGER, &TextField::SetProperty, &TextField::GetProperty );
74 PropertyRegistration property02( mType, "placeholder-text", Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
75 PropertyRegistration property03( mType, "text", Toolkit::TextField::PROPERTY_TEXT, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
76 PropertyRegistration property04( mType, "cursor-image", Toolkit::TextField::PROPERTY_CURSOR_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
77 PropertyRegistration property05( mType, "primary-cursor-color", Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
78 PropertyRegistration property06( mType, "secondary-cursor-color", Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR, Property::VECTOR4, &TextField::SetProperty, &TextField::GetProperty );
79 PropertyRegistration property07( mType, "enable-cursor-blink", Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK, Property::BOOLEAN, &TextField::SetProperty, &TextField::GetProperty );
80 PropertyRegistration property08( mType, "cursor-blink-interval", Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
81 PropertyRegistration property09( mType, "cursor-blink-duration", Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION, Property::FLOAT, &TextField::SetProperty, &TextField::GetProperty );
82 PropertyRegistration property10( mType, "grab-handle-image", Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE, Property::STRING, &TextField::SetProperty, &TextField::GetProperty );
83 PropertyRegistration property11( mType, "decoration bounding-box", Toolkit::TextField::PROPERTY_DECORATION_BOUNDING_BOX, Property::RECTANGLE, &TextField::SetProperty, &TextField::GetProperty );
86 Toolkit::TextField TextField::New()
88 // Create the implementation, temporarily owned by this handle on stack
89 IntrusivePtr< TextField > impl = new TextField();
91 // Pass ownership to CustomActor handle
92 Toolkit::TextField handle( *impl );
94 // Second-phase init of the implementation
95 // This can only be done after the CustomActor connection has been made...
101 void TextField::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
103 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
107 TextField& impl( GetImpl( textField ) );
111 case Toolkit::TextField::PROPERTY_RENDERING_BACKEND:
113 unsigned int backend = value.Get< unsigned int >();
115 if( impl.mRenderingBackend != backend )
117 impl.mRenderingBackend = backend;
118 impl.mRenderer.Reset();
122 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
124 if( impl.mController )
126 //impl.mController->SetPlaceholderText( value.Get< std::string >() ); TODO
130 case Toolkit::TextField::PROPERTY_TEXT:
132 if( impl.mController )
134 impl.mController->SetText( value.Get< std::string >() );
138 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
140 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
142 if( impl.mDecorator )
144 impl.mDecorator->SetCursorImage( image );
148 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
150 if( impl.mDecorator )
152 impl.mDecorator->SetColor( PRIMARY_CURSOR, value.Get< Vector4 >() );
156 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
158 if( impl.mDecorator )
160 impl.mDecorator->SetColor( SECONDARY_CURSOR, value.Get< Vector4 >() );
164 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
166 if( impl.mController )
168 //impl.mController->SetEnableCursorBlink( value.Get< bool >() ); TODO
172 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
174 if( impl.mDecorator )
176 impl.mDecorator->SetCursorBlinkInterval( value.Get< float >() );
180 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
182 if( impl.mDecorator )
184 impl.mDecorator->SetCursorBlinkDuration( value.Get< float >() );
188 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
190 ResourceImage image = ResourceImage::New( value.Get< std::string >() );
192 if( impl.mDecorator )
194 impl.mDecorator->SetGrabHandleImage( image );
198 case Toolkit::TextField::PROPERTY_DECORATION_BOUNDING_BOX:
200 if( impl.mDecorator )
202 impl.mDecorator->SetBoundingBox( value.Get< Rect<int> >() );
210 Property::Value TextField::GetProperty( BaseObject* object, Property::Index index )
212 Property::Value value;
214 Toolkit::TextField textField = Toolkit::TextField::DownCast( Dali::BaseHandle( object ) );
218 TextField& impl( GetImpl( textField ) );
222 case Toolkit::TextField::PROPERTY_RENDERING_BACKEND:
224 value = impl.mRenderingBackend;
227 case Toolkit::TextField::PROPERTY_PLACEHOLDER_TEXT:
229 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
232 case Toolkit::TextField::PROPERTY_TEXT:
234 DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
237 case Toolkit::TextField::PROPERTY_CURSOR_IMAGE:
239 if( impl.mDecorator )
241 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
244 value = image.GetUrl();
249 case Toolkit::TextField::PROPERTY_PRIMARY_CURSOR_COLOR:
251 if( impl.mDecorator )
253 value = impl.mDecorator->GetColor( PRIMARY_CURSOR );
257 case Toolkit::TextField::PROPERTY_SECONDARY_CURSOR_COLOR:
259 if( impl.mDecorator )
261 value = impl.mDecorator->GetColor( SECONDARY_CURSOR );
265 case Toolkit::TextField::PROPERTY_ENABLE_CURSOR_BLINK:
267 //value = impl.mController->GetEnableCursorBlink(); TODO
270 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_INTERVAL:
272 if( impl.mDecorator )
274 value = impl.mDecorator->GetCursorBlinkInterval();
278 case Toolkit::TextField::PROPERTY_CURSOR_BLINK_DURATION:
280 if( impl.mDecorator )
282 value = impl.mDecorator->GetCursorBlinkDuration();
286 case Toolkit::TextField::PROPERTY_GRAB_HANDLE_IMAGE:
288 if( impl.mDecorator )
290 ResourceImage image = ResourceImage::DownCast( impl.mDecorator->GetCursorImage() );
293 value = image.GetUrl();
298 case Toolkit::TextField::PROPERTY_DECORATION_BOUNDING_BOX:
300 if( impl.mDecorator )
302 value = impl.mDecorator->GetBoundingBox();
312 void TextField::OnInitialize()
314 mController = Text::Controller::New( *this );
316 mDecorator = Text::Decorator::New( *this, *mController );
318 mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX );
320 mController->EnableTextInput( mDecorator );
322 // Forward input events to controller
323 EnableGestureDetection( Gesture::Tap );
325 // TODO - Fix TapGestureDetector to support single and double tap
326 mDoubleTapDetector = TapGestureDetector::New();
327 mDoubleTapDetector.SetTapsRequired( 2 );
328 mDoubleTapDetector.DetectedSignal().Connect( this, &TextField::OnDoubleTap );
329 mDoubleTapDetector.Attach(Self());
331 // Set BoundingBox to stage size if not already set.
332 if ( mDecorator->GetBoundingBox().IsEmpty() )
334 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
335 mDecorator->SetBoundingBox( Rect<int>( 0.0f, 0.0f, stageSize.width, stageSize.height ) );
339 Vector3 TextField::GetNaturalSize()
341 return mController->GetNaturalSize();
344 float TextField::GetHeightForWidth( float width )
346 return mController->GetHeightForWidth( width );
349 void TextField::OnRelayout( const Vector2& size, ActorSizeContainer& container )
351 if( mController->Relayout( size ) )
355 mDecorator->Relayout( size );
360 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
365 Actor renderableActor = mRenderer->Render( mController->GetView() );
367 if( renderableActor )
369 Self().Add( renderableActor );
375 void TextField::OnTap( const TapGesture& tap )
377 mController->TapEvent( tap.numberOfTaps, tap.localPoint.x, tap.localPoint.y );
380 void TextField::OnDoubleTap( Actor actor, const TapGesture& tap )
382 mController->TapEvent( tap.numberOfTaps, tap.localPoint.x, tap.localPoint.y );
385 void TextField::RequestTextRelayout()
390 TextField::TextField()
391 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
392 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
396 TextField::~TextField()
400 } // namespace Internal
402 } // namespace Toolkit